TrkDgnr
TrkDgnr

Reputation: 139

C - Linux - Custom kernel module to iterate over a process' children blows up kernel log and the computer

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.

Kind regards,

Upvotes: 2

Views: 1504

Answers (1)

TrkDgnr
TrkDgnr

Reputation: 139

I fixed the problem with initializing a new task_struct named childtask:

struct task_struct *childtask;

and then assigning it to the list_entry inside the list_for_each loop:

childtask = list_entry(list, struct task_struct, sibling);  

so that task and childtask are distinct pointers.

Upvotes: 3

Related Questions