You are required to develop a Linux kernel loadable module c
Solution
#include<linux/version.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/module.h>
#include<linux/pid.h>
#define PARENT 0
#define CHILD 1
int pid_input = -1;
module_param(pid_input,int,0);// Read the parameter from command line and assign to pid_input variable
struct task_struct *child = NULL; // pointer to structure of task_struct to store the address of given process id
struct task_struct *parent = NULL;//pointer to structure of task_struct to store the address of given process id
void print(int process)
{
if(process == PARENT)// printing the requirements using printk
{
printk(\"OUR_MODULE:: process_id = %d, parent_process_id = %d, system_time = %ld, user_time = %ld, priority = %ld, user_id = %ld\ \",child->pid, child -> p_pptr -> pid, child -> stime, child -> utime, child -> priority, child -> uid);
}
else if(process == CHILD)
{
printk(\"OUR_MODULE:: process_id = %d, parent_process_id = %d, system_time = %ld, user_time = %ld, priority = %ld, user_id = %ld\ \",parent->pid, parent -> p_pptr -> pid, parent -> stime, parent -> utime, parent -> priority, parent -> uid);
}
}
int init_mod(void) // Function which executes during insertion of module
{
if(pid_input == -1)
{
printk(\"OUR_MODULE:: pid_input is not passed as an argument...\ \");
return 0;
}
child = pid_task(find_vpid(pid_input),PIDTYPE_PID);// Traversing and getting the correspoding task_struct structure for given process id
if(child == NULL)// If the process with given pid doesnot exist
{
printk(\"OUR_MODULE:: process with pid = %d doesnot exist\",pid_input);
return 0;
}
parent = child -> p_pptr; // Getting the parent task_struct from child task_struct
print(CHILD);
return 0;
}
void cleanup_mod(void)
{
if(pid_input == -1)
{
printk(\"OUR_MODULE:: pid_input is not passed as an argument...\ \");
return;
}
if(parent == NULL)
{
printk(\"OUR_MODULE:: process with pid = %d doesnot exist\",pid_input);
return;
}
print(PARENT);// call the printing function
return 0;
}
module_init(init_mod);// registering the function init_mod so that function executes during the insertion of module
//module_exit(cleanup_mod);
MODULE_AUTHOR(\"MALLESWARA REDDY MULA\");
MODULE_DESCRIPTION(\"Getting the Process info from given process id\");
MODULE_LICENSE(\"GPL\");

