how to get process id, name and status using module [closed]

I want to print the process id , process name and process status using module, as a test I try to print the process id by using getpid() function but the following error occur:

implicit declaration of function 'printf'

implicit declaration of function 'getpid'

How to solve these errors and how to print the process name and status.

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

/* This function is called when the module is loaded. */
int simple_init(void)
{
       printk(KERN_INFO "Loading Module\n");
       printf("The process id is %\n", (int) getpid());
       return 0;
}

/* This function is called when the module is removed. */
void simple_exit(void) {
    printk(KERN_INFO "Removing Module\n");
}

/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h> //task_pid_nr

/* This function is called when the module is loaded. */
int simple_init(void)
{
       printk(KERN_INFO "Loading Module\n");
       printk("The process id is %d\n", (int) task_pid_nr(current));
       printk("The process vid is %d\n", (int) task_pid_vnr(current));
       printk("The process name is %s\n", current->comm);
       printk("The process tty is %d\n", current->signal->tty);
       printk("The process group is %d\n", (int) task_tgid_nr(current));
       printk("\n\n");
   //return -1; //debug mode of working
   return 0;
}

/* This function is called when the module is removed. */
void simple_exit(void) {
    printk(KERN_INFO "Removing Module\n");
}

/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");

To answer your question in a very basic way : Some of the kernel's functions, including init() and exit(), execute "on behalf of" user space processes. That is, when you insert the module using insmod, the init() function gets executed, and when you remove the module using rmmod, the exit() function gets executed. So, we can say that the function init() is executing on behalf of the insmod process. Same is the situation with exit() and rmmod.

Now, the question is to find the relative process for that particular function. This can be simply achieved using the current macro. The macro is of type struct task_struct. It is to be noted that inside Linux, each process's task control block(or PCB), is represented by struct task_struct. This data structure contains all the information corresponding to a process, like, state, name, MM info, etc. I recommend you to go through the data structure once. https://elixir.bootlin.com/linux/v4.15.18/source/include/linux/sched.h#L520

When a particular function executes on behalf of a process(say init()), the kernel associates the current macro with that process's task_struct. Thus, by simply accessing various fields of struct task_struct, we could get information of the process relative to this function.

So, in your case, it could be something like this:

current->comm : Gives the name of the process

current->pid : Gives the pid of the process

Hope this helps.


Your question has no sense. getpid (and every other syscalls(2)...) can only work in user-land application code, not inside the kernel. The kernel might run some module code even without any specific process. In particular, module initialization happens early, when the module is loaded.

I recommend reading Advanced Linux Programming, the Linux kernel wikipage, and kernelnewbies before coding your module. A bad kernel module could lose all your data or (if unlucky) break your hardware.

You may want to query the current task from inside the kernel's scheduler. See this, but don't code any kernel related code before understanding much more.