在内核模块中命名变量

在内核模块中命名变量

本文介绍了在内核模块中命名变量“current"会导致“函数声明不是原型";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

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

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	%d	%s
", (int)current->state, current->pid, current->comm);

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

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

    printk(KERN_INFO "State	PID	Name
");

    DFS(&init_task);

    return 0;
}

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

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

Anyone knows how to fix this ??

推荐答案

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

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

当前指针指向当前正在执行的用户进程.在执行系统调用(例如 open 或 read)期间,当前进程是调用该调用的进程.

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

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!

这篇关于在内核模块中命名变量“current"会导致“函数声明不是原型";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:07