【问题标题】:Getting 'ímplicit declaration of function' error while adding a system call in linux在 linux 中添加系统调用时出现“函数的隐式声明”错误
【发布时间】:2015-06-24 00:03:12
【问题描述】:

我正在尝试添加一个新的系统调用,以显示有关系统中当前正在运行的进程的一些信息。我创建了一个名为 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 构建内核时,出现以下错误

如上所示,我在该行中包含了 signal.h 标头:

#include <linux/signal.h>

我在这里缺少什么?有没有其他方法可以通过使用它的pid而不是kill函数来检查系统中是否存在给定进程?

编辑: 使用答案中的建议,我用 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;
    }

【问题讨论】:

  • 顺便说一句:您知道 procfs 已经提供了大部分(如果不是全部)这些信息?
  • 我刚刚意识到,由于您在内核中,所以您没有可见的kill。尝试使用以下命令终止进程:kill_proc_info(SIGKILL, SEND_SIG_PRIV, pid)
  • @mziccard 您能否详细说明我将如何使用 kill_proc_info(SIGKILL, SEND_SIG_PRIV, pid) 或给我一个可以参考的链接?
  • @avidProgrammer 看看linux/sched.h:定义了几个发送信号的函数,其中您可以找到kill_proc_infokill_pid_infokill_pid。这三个都在kernel/signal.c 中实现,并且通常kill_proc_infokill_pid 都是kill_pid_info 的包装器。所有的树函数都在内核周围使用来杀死进程。使用kill_proc_info(SIGKILL, SEND_SIG_PRIV, pid),您应该将SIGKILL 信号发送到pid 标识的进程。 SEND_SIG_PRIV 表示发送者有特权(内核)。
  • 我实际上并不想杀死进程,我想发送一个信号值为 0 的 kill 信号,以便在进程不存在时返回错误值。 kill_proc_info(SIGKILL, SEND_SIG_PRIV, pid) 可以达到与 kill(0, pid) 相同的结果吗? SEND_SIG_PRIV 参数的一些可能值是什么?如果您能举例说明,我将不胜感激。

标签: c linux linux-kernel system-calls


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    内核代码没有定义像kill这样的用户空间函数。

    对于搜索进程,您可以尝试for_each_process 宏,在&lt;linux/sched.h&gt; 中定义。

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

    另请注意,搜索任务(以任何形式)和读取其字段应在rcu_read_lock/rcu_read_unlock 关键部分内执行。这可以保护任务列表在搜索期间免受元素破坏。

    【讨论】:

    • Tsyvarev 和@Vadim Stupakov,我用你的答案编辑了这个问题。但是,还会引发另一个错误。
    • 您在搜索中忘记了break;。答案已更新为搜索任务的其他替代方案。
    【解决方案3】:

    尝试添加

    #define _POSIX_SOURCE
    

    在所有包含之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2020-09-21
      • 1970-01-01
      • 2015-09-27
      • 2013-03-20
      相关资源
      最近更新 更多