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

删除回忆录丶 提交于 2021-02-11 15:01:33

问题


I'm learning to write kernel modules for linux as a beginner. What I'm trying to do is to write every task and its child process into the kernel log using DFS algorithm. But when I compile the code using Makefile, it shows the above error:

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

It points out the task_struct keyword at the function DFS. Here's my code:

# 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");
}

Anyone knows how to fix this ??


回答1:


The kernel has a macro called current which is pointing to users currently executing process. As this book states,

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.

In other words, as @GilHamilton mentioned in the comments, current is #defined to the function get_current() in the kernel. Using current as a variable name will give a compile-time error!



来源:https://stackoverflow.com/questions/63798884/cant-access-task-struct-structure-from-another-header-file-in-kernel-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!