[英]Linux kernel module freezing the computer while iterating over running processes to access process opened files
[英]C - Linux - Custom kernel module to iterate over a process' children blows up kernel log and the computer
我是linux內核模塊的新手,在嘗試處理一些復雜的概念之前,我正在嘗試實現一些基本概念。 我編寫了一個代碼,該代碼帶有一個模塊參數(一個int),並檢查是否有帶有該pid的進程。 如果存在,則將其子項作為列表,並在打印子項的ID和說明時對其進行迭代。 這是代碼:
#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);
但是,當我使用
sudo insmod listtasks.ko mypid=1800(or a random pid)
它不會停止執行,並且會耗盡所有內核日志內存,最終凍結計算機。 我習慣於在恢復模式下重新啟動它並刪除炸毀的日志文件,但是我看不到如何解決該問題。 所有幫助將不勝感激。
親切的問候,
我通過初始化名為childtask的新task_struct來解決此問題:
struct task_struct *childtask;
然后將其分配給list_for_each循環內的list_entry:
childtask = list_entry(list, struct task_struct, sibling);
因此task和childtask是不同的指針。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.