我正在尝试实现一个内核模块,它可以访问用户进程的task_struct,其进程ID已为我所知。我正在使用find_get_pid
和pid_task
来获取进程的task_struct:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <linux/pid_namespace.h>
int init_module( void )
{
//Declaring the variables
int p_id = 6980; //6980 is the process ID of my user process
struct pid *pid_struct;
struct task_struct *task;
// Trying to access the variables of the p_id
pid_struct = find_get_pid(p_id);
task = pid_task(pid_struct, PIDTYPE_PID);
//Printing the info from the task_struct
printk( KERN_INFO "*** [%d]\n",task->pid);
return 0;
}
void cleanup_module( void )
{
return;
}
它已成功编译并且我收到* .ko文件,但是当我尝试将其插入内核时,它给了我一个错误:
insmod: error inserting 'main.ko': -1 Unknown symbol in module
Dmesg给了我以下输出:
main: Unknown symbol find_get_pid (err 0)
我不知道如何继续,如果有人能帮助我,我们将非常感激。
答案 0 :(得分:2)
仔细检查您要使用的功能是什么。
还要记住,“核心内核”(可能包括frob_task_by_pid_hard
及其之类版本)的大部分内容仅限于GPL,因此,除非您将模块的许可证声明为GPL,否则您将不会去任何地方。也可以这样填写模块上的其他样板数据:MODULE_AUTHOR,MODULE_DESCRIPTION,MODULE_LICENSE至少。
答案 1 :(得分:1)
加载内核模块时可能还有另一个失败原因,例如显示错误为“ insmod:ERROR:不允许.ko操作”。您的内核正在使用最新PC上的安全启动选项启动,必须禁用该选项,以避免加载/插入内核模块失败。这可以通过使用以下命令来完成。 sudo apt安装mokutil sudo mokutil --disable-validation 或者,您可以搜索特定操作系统的如何禁用安全启动选项。