I want to check if a process which pid I have is running from an kernel extension.
In user-space this would be simple:
if (kill(pid, 0) == 0) {
printf("Process %d is running\n", pid);
} else if (errno == ESRCH) {
printf("Process %d is not running\n", pid);
} else {
printf("This shouldn't happen oO\n");
But somehow kill() is not available in kernel. Is there another approach to do this?
You can use pid_task() or get_pid_task() to get a pointer to the struct task_struct of the process. If the call returns NULL then the process does not exist.
Note that you may need to be careful as the pid might have been recycled and is now used by a different process.
After Christophe's answer I found the a way to solve this.
//necessary imports
#include <linux/sched.h>
#include <linux/pid.h>
//Rest of your module
pid_t pid = myPid; //integer value of pid
struct pid *pid_struct = find_get_pid(pid); //function to find the pid_struct
struct task_struct *task = pid_task(pid_struct,PIDTYPE_PID); //find the task_struct
if (task == NULL)
{
printk (KERN_INFO "Process with pid %d is not running \n",argument);
}
else{
printk (KERN_INFO "Process with pid %d is running \n",argument);
}