我正在尝试添加一个系统调用到linux内核(版本:3.10.91),需要root权限才能运行。
你可以看到我的尝试如下:
#include <linux/kernel.h>
#include <linux/linkage.h>
#include <linux/sched.h>
#include <asm/current.h>
#include <asm/errno.h>
asmlinkage long sys_set_casper(pid_t pid, int value)
{
struct task_struct *process;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
//valid values are 0,1,2,3
if (value != 0 && value != 1 && value != 2 && value != 3 )
return -EINVAL;
process = find_task_by_vpid(pid);
if (process == NULL)
return -ESRCH;
//modify the casper field accordingly
process->casper = value;
return 0;
}
Casper只是我添加的一个任务描述符。基本上,当casper值为1时,我希望隐藏进程(对ps、tree、top等不可见)。内核重新编译得很好(我还在base.c等中做了必要的更改)。
我试着用下面的代码测试我的系统调用,test.c:#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define NR_set_casper 351
int main(int argc, char **argv)
{
long y;
printf("PID of current process: %dnn", getpid());
printf("Call set_casper system call to set flag 1n");
y = syscall(NR_set_casper, getpid(), 0);
printf("Return value of set_casper system call: %ldn", y);
if (y < 0)
{
printf("set_casper system call failed. Run with sudon");
return EXIT_FAILURE;
}
return 0;
}
我编译并运行如下:
gcc test.c
sudo ./a.o ut
输出为:
PID of current process: 3852
Call set_casper system call to set flag 1
Return value of set_casper system call: -1
set_casper system call failed. Run with sudo
奇怪的是,即使删除了sudo控制行:
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
和重新编译内核,我仍然得到相同的错误。
基本上,为什么我的sys_set_casper函数返回-1 ?
编辑
我已经添加了这个:351 i386 set_casper sys_set_casper到Arch/x86/syscalls$ gedit syscall_32.tbl
然而,我的系统是64位的。这就是问题所在吗?
问题是,正如其他人在评论中所说,我根本没有调用系统调用。我只是将我的调用添加到64位系统调用表中,并再次尝试所有操作,它成功了。