C-Linux-遍历进程子进程的自定义内核模块会炸毁内核日志和计算机 [英] C - Linux - Custom kernel module to iterate over a process' children blows up kernel log and the computer
问题描述
我是Linux内核模块的新手,在尝试处理一些复杂的概念之前,我正在尝试实现一些基本概念.我编写了一个代码,该代码带有一个模块参数(一个int),并检查是否有带有该pid的进程.如果存在,则将其子项作为列表,并在打印子项的ID和说明时对其进行迭代.这是代码:
I'm new to linux kernel modules and I am trying to implement some basic concepts before handling complex ones. I wrote a code which takes a module parameter (an int) and checks if there is a process with that pid. If there is, it takes its children as a list and iterates over it while printing the children's ids and descriptions. Here is the code:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/moduleparam.h>
#include <linux/sched/signal.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Some guy");
int mypid = 0;
static int simple_init(void)
{
struct task_struct *task;
struct list_head *list;
printk(KERN_ALERT "Loading Module\nThe process id: %d\n", mypid);
for_each_process(task){
printk(KERN_ALERT "PID/NAME: %d/%s\n", task->pid, task->comm);
if(task->pid == mypid){
printk(KERN_ALERT "The common pid found: %d/%s\n", task->pid, task->comm);
list_for_each(list, &task->children){
task = list_entry(list, struct task_struct, sibling);
//printk(KERN_INFO "Parent ID/NAME: %d/%s\n", task->parent->pid, task->parent->comm);
printk(KERN_ALERT "Child PID/NAME: %d/%s\n", task->pid, task->comm);
}
}
return 0;
}
static void simple_exit(void){
printk(KERN_WARNING "Removing Module\n");
}
module_init(simple_init);
module_exit(simple_exit);
module_param(mypid, int, 0);
但是,当我使用以下代码运行
However, when I run this code with
sudo insmod listtasks.ko mypid=1800(or a random pid)
它不会停止执行,并且会耗尽所有内核日志内存,最终冻结计算机.我习惯于在恢复模式下重新启动它并删除炸掉的日志文件,但是我看不到如何解决该问题.所有帮助将不胜感激.
it doesn't stop executing and eats up all the kernel log memory, eventually freezing the computer. I got used to reboot it in the recovery mode and delete the log files that blew up, but I can't see how I can fix the problem. All help would be greatly appreciated.
亲切的问候,
推荐答案
我通过初始化名为childtask的新task_struct来解决此问题:
I fixed the problem with initializing a new task_struct named childtask:
struct task_struct *childtask;
,然后将其分配给list_for_each循环内的list_entry:
and then assigning it to the list_entry inside the list_for_each loop:
childtask = list_entry(list, struct task_struct, sibling);
,以便task和childtask是不同的指针.
so that task and childtask are distinct pointers.
这篇关于C-Linux-遍历进程子进程的自定义内核模块会炸毁内核日志和计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!