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

问题描述 投票:0回答:3

我正在尝试添加一个新的系统调用,该调用显示有关系统中当前正在运行的进程的一些信息。我创建了一个名为 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;
};

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

#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;
    }
c linux linux-kernel system-calls
3个回答
1
投票

齐瓦列夫对。 Linux内核不包含kill。但它包含sys_kill

另外,你可以看看我的测试项目。在那里你可以找到杀死进程的 affl_kill_process() 函数。


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
关键部分内执行。这可以保护任务列表在搜索过程中免遭元素破坏。


0
投票

尝试添加

#define _POSIX_SOURCE

在所有包含内容之前。

© www.soinside.com 2019 - 2024. All rights reserved.