Thread: Kernel module - List all processes / process information.

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    18

    Kernel module - List all processes / process information.

    Hey guys it's been awhile. Just started delving into a book "Operating System Concepts". I'm on chapter 3 Process concepts, in this chapter the end assignment is to create a kernel module that iterates through all processes running on the machine( a ubuntu 16.04.4 VM ) ,then list each process name/id/state. The problem i'm having is that the for_each_process() macro isn't defined in the linux/sched.h file. I google'd and came up short. Anyone know where this macro is defined on ubuntu 16.04.4???

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,745
    Are you sure it isn't in sched.h ?
    Linux source code: for_each_process identifier (v4.4.139) - Bootlin

    Do you have all the development files installed, and not just the bare minimum needed to boot an OS?

    Are you looking in the right include directory?

    grep -r is your friend.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    39
    Quote Originally Posted by Salem View Post
    Are you sure it isn't in sched.h ?
    Whoa, 3215 lines of code. What a beast!

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    18
    Whoa, 3215 lines of code. What a beast!
    @Salem
    Thanks for the grep reminder! I don't know why I didn't start with that :/ . I did grep my way around the VM, and finally found the for_each_process() definition. Which for some reason is in "linux/sched/signal.h" instead of "linux/sched.h" ( in the usr/src/linux-headers-$(uname -r)/include/........ ).. I'll post the code once i'm done since, the question didn't really end up being a linux programming question.

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    18
    main.c
    Code:
    #include <linux/kernel.h>
    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/sched/signal.h>
    #include <linux/sched.h>
    
    
    struct task_struct *task;        /*    Structure defined in sched.h for tasks/processes    */
    struct task_struct *task_child;        /*    Structure needed to iterate through task children    */
    struct list_head *list;            /*    Structure needed to iterate through the list in each task->children struct    */
    
    int iterate_init(void)                    /*    Init Module    */
    {
        printk(KERN_INFO "%s","LOADING MODULE\n");    /*    good practice to log when loading/removing modules    */
        
        for_each_process( task ){            /*    for_each_process() MACRO for iterating through each task in the os located in linux\sched\signal.h    */
            printk(KERN_INFO "\nPARENT PID: %d PROCESS: %s STATE: %ld",task->pid, task->comm, task->state);/*    log parent id/executable name/state    */
            list_for_each(list, &task->children){                        /*    list_for_each MACRO to iterate through task->children    */
    
                task_child = list_entry( list, struct task_struct, sibling );    /*    using list_entry to declare all vars in task_child struct    */
        
                printk(KERN_INFO "\nCHILD OF %s[%d] PID: %d PROCESS: %s STATE: %ld",task->comm, task->pid, /*    log child of and child pid/name/state    */
                    task_child->pid, task_child->comm, task_child->state);
            }
            printk("-----------------------------------------------------");    /*for aesthetics*/
        }    
        
    
        return 0;
    
    }                /*    End of Init Module    */
        
    void cleanup_exit(void)        /*    Exit Module    */
    {
    
    
        printk(KERN_INFO "%s","REMOVING MODULE\n");
    
    }                /*    End of Exit Module    */
    
    module_init(iterate_init);    /*    Load Module MACRO    */
    module_exit(cleanup_exit);    /*    Remove Module MACRO    */
    
    MODULE_LICENSE("GPL");
    MODULE_DESCRIPTION("ITERATE THROUGH ALL PROCESSES/CHILD PROCESSES IN THE OS");
    MODULE_AUTHOR("Laerehte");
    There's the solution I've come to. I'm not in any classes for this, just on my own. So if anyone has any constructive criticism, please feel free.

    Compiled with:
    Makefile
    Code:
    obj-m += main.o
    
    all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
    
    clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
    Executed with ./ins (with chmod +x)
    ins
    Code:
    sudo insmod main.ko
    sudo rmmod main
    sudo dmesg -c

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,745
    A nit-picky point, there doesn't seem to be a reason for your variables to be global.

    More for future reference, should you feel emboldened to take on some actual kernel work.
    Linux kernel coding style — The Linux Kernel documentation
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    18
    @Salem
    A nit-picky point, there doesn't seem to be a reason for your variables to be global.
    Yeah, i do remember that using globals should be kept to an absolute minimum. I think it sprouted from a compiling error that I was wobbling my way through (i definitely should have caught it :P). Thanks for the feedback, and the reference, I'll give it a read!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux kernel module help
    By Annonymous in forum Linux Programming
    Replies: 18
    Last Post: 05-01-2011, 10:59 PM
  2. Check for integer in kernel module
    By NuNn in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 11:27 AM
  3. linux kernel module programming
    By open_engg in forum Linux Programming
    Replies: 2
    Last Post: 08-05-2007, 04:41 PM
  4. linux kernel module programming
    By circuit in forum Linux Programming
    Replies: 10
    Last Post: 12-01-2004, 06:08 PM
  5. Kernel Module Programming in 2.6
    By KneeLess in forum Linux Programming
    Replies: 1
    Last Post: 04-13-2004, 11:51 AM

Tags for this Thread