在内核模块中命名变量 `current` 会导致 "function declaration isn’t a prototype" 错误

Naming a variable `current` in a kernel module leads to "function declaration isn’t a prototype" error

作为初学者,我正在学习为 linux 编写内核模块。我要做的是使用 DFS 算法将每个任务及其子进程写入内核日志。但是当我使用 Makefile 编译代码时,它显示了上面的错误:

function declaration isn’t a prototype [-Werror=strict-prototypes]
struct task_struct *current;

它指出函数DFS处的task_struct关键字。 这是我的代码:

# include <linux/init.h>
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/sched.h>
# include <linux/list.h>

void DFS (struct task_struct *task)
{
    struct task_struct *current;
    struct list_head *list;

    list_for_each (list, &task->children)
    {
        current = list_entry(list, struct task_struct, sibling);
        printk(KERN_INFO "%d\t%d\t%s \n", (int)current->state, current->pid, current->comm);

        if (current != NULL)
        {
            DFS(current);
        }
    }
}

int DFS_init(void)
{
    printk(KERN_INFO "Loading the Second Module...\n");

    printk(KERN_INFO "State\tPID\tName\n");

    DFS(&init_task);   

    return 0;
}

void DFS_exit(void)
{
    printk(KERN_INFO "Removing the Second Module...\n");
}

有人知道如何解决这个问题吗??

内核有一个名为current的宏,它指向当前正在执行进程的用户。正如 this book 所述,

The current pointer refers to the user process currently executing. During the execution of a system call, such as open or read, the current process is the one that invoked the call.

换句话说,正如@GilHamilton 在评论中提到的,current#defined 到内核中的函数 get_current()。使用current作为变量名会报编译时错误!