在linux中添加系统调用时出现“隐式声明函数”错误

时间:2015-06-24 00:03:13

标签: c linux linux-kernel system-calls

我正在尝试添加一个新的系统调用,该调用显示有关系统中当前正在运行的进程的一些信息。我创建了一个名为proc_info_struct的新结构,其中包含我想要显示的部分过程信息。这是procinfo.h头文件中定义的proc_info_struct代码

#include <linux/types.h>
struct proc_info_struct
{
    pid_t pid;
    pid_t parn_pid;
    pid_t gid;
    unsigned long user_time;
    unsigned long sys_time;
    long state;
    unsigned long long sched_avg_running;
    unsigned int time_slice;
    unsigned int policy;
    unsigned long num_cxs;

    int num_children;
    char* prog;
};

在系统调用方法(如下所示)中,我尝试使用this链接作为参考,使用signal.h中定义的kill()函数来检查指定的进程是否存在。以下是我用来获取流程信息的代码:

#include <linux/procinfo.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/linkage.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <asm-generic/current.h>
#include <linux/signal.h>

void fill_proc_info(struct proc_info_struct *proc_info, struct task_struct *task)
{
    int num_ch = 0;
    struct list_head* list;
    proc_info->pid = task->pid;
    proc_info->parn_pid = (task->parent)->pid;
    proc_info->gid = task->tgid;
    proc_info->user_time = task->utime;
    proc_info->sys_time = task->stime;
    proc_info->state = task->state;
    proc_info->sched_avg_running = task->cputime_expires.sum_exec_runtime; //average scheduled running time;
    proc_info->policy = task->policy;
    proc_info->time_slice = task->rt.time_slice;
    proc_info->num_cxs = (task->nvcsw) + (task->nivcsw); //number of context switches(both voluntary and involuntary)

    list_for_each(list, &task->children){
        num_ch++;
    }
    proc_info->num_children = num_ch;
    proc_info->prog = task->comm;
}



asmlinkage long sys_getprocinfo(pid_t pid, struct proc_info_struct *proc)
{
    struct task_struct *task;
    struct proc_info_struct *proc_info;

    if(pid)
    {
        //current process
        task = get_current();
        fill_proc_info(proc_info, task);

    }

    else{
        int exists = 0;//checks whether a process exists or not
        for_each_process(task){
        if(task->pid = pid){
            exists = 1;
        }
    }
    if(exists){
        //task = find_task_by_vpid(pid);  we don't need this line now since the task has now been set in for_each_process

         fill_proc_info(proc_info, task);
    }

    else{
        printk("Process doesn't exist");
        return -ESRCH:
    }

    }
    if (copy_to_user(proc, proc_info, sizeof(proc_info)))
        return -EFAULT;
    return 1;

}

当我尝试使用make构建内核时,我得到以下错误

Error

从上面可以看出,我已经在行中包含了signal.h标题:

#include <linux/signal.h>

我在这里缺少什么?有什么替代方法可以通过使用除了kill函数之外的pid来检查系统中是否存在给定进程?

修改 使用答案中的建议,我用 for_each_process 宏替换了 kill 函数调用。现在它成功编译。我写了一个测试代码来测试系统调用。当我提供不存在的pid时,系统调用通过在内核日志中显示“进程不存在”来正常工作。但是,当我提供现有代码时,它会在内核日志中引发显示错误:

  

BUG:无法在(null)

处理内核NULL指针解除引用

测试代码

int main()
    {
        struct proc_info_struct *proc_info;
        pid_t pid = 0; //I tried 
        /;

        syscall(314, pid, &proc_info);

        printf("Pid: %ld\nParent: %ld\nGid:%ld\nUtime: %lu\nSysTime: 
            %d\nState: %lu\n,Time_Slice: %d\nPolicy: %d\nNum_CXS: %lu\nNumChild: 
            %d\nProg: %s\n",proc_info->pid, proc_info->parn_pid, proc_info->gid, 
            proc_info->user_time, proc_info->sys_time, proc_info->state, proc_info->time_slice, 
            proc_info->policy, proc_info->num_cxs, proc_info->num_children, proc_info->prog);

        return 0;
    }

3 个答案:

答案 0 :(得分:1)

内核代码没有定义kill等用户空间函数。

对于搜索流程,您可以尝试for_each_process中定义的<linux/sched.h>宏。

更新:顺便说一句,根据您的代码中的评论,您出于某种目的使用了find_task_by_vpid。这个函数只返回任务,它给出了pid。你为什么不想用它?此外,您可以调查系统调用kill的实现:它如何搜索所需的任务。此系统调用在kernel/signal.c中定义为SYSCALL_DEFINE2(kill,...

另请注意,搜索任务(以任何形式)和阅读其字段应在rcu_read_lock / rcu_read_unlock关键部分内执行。这可以保护任务列表在搜索过程中不受元素破坏的影响。

答案 1 :(得分:1)

Tsyvarev对。 Linux内核不包含kill。但它包含sys_kill

此外,您可以查看我的test project。在那里你可以找到杀死进程的affl_kill_process()函数。

答案 2 :(得分:0)

尝试添加

nil

所有夹杂物之前。