#include // Needed by all modules #include // for KERN_INFO #include // for_each_process, pr_info #include // for SYSCALL_DEFINE #include // for "for_each_process" #include // for get_current(), current macro #include // for copy_to_user, copy_from_user struct proc_info { pid_t pid; char name[16]; }; struct procinfos { long studentID; struct proc_info proc; struct proc_info parent_proc; struct proc_info oldest_child_proc; }; void charcpy(char *_dest, const char *_src); void printProc(struct procinfos *info); SYSCALL_DEFINE2(get_proc_info, pid_t, pid, struct procinfos *, info) { struct task_struct *process = NULL, *child_process = NULL; struct procinfos k_info; k_info.studentID = 1814015; printk("\n\n******************************\n"); if (pid == -1) { printk("PID = -1 passed sucessfully!\n"); pid = current->pid; } // We have 'current' macro and we can speed up some but, I'm too lazy for that. for_each_process(process) { if (process->pid == pid) { k_info.proc.pid = process->pid; charcpy(k_info.proc.name,process->comm); // PARENT if (process->real_parent == NULL) { k_info.parent_proc.pid = 0; charcpy(k_info.parent_proc.name,"\0"); } else { k_info.parent_proc.pid = process->real_parent->pid; charcpy(k_info.parent_proc.name, process->real_parent->comm); } // CHILD child_process = list_first_entry_or_null(&process->children, struct task_struct, sibling); if (child_process == NULL) { k_info.oldest_child_proc.pid = 0; charcpy(k_info.oldest_child_proc.name,"\0"); } else { k_info.oldest_child_proc.pid = child_process->pid; charcpy(k_info.oldest_child_proc.name,child_process->comm); } // copy_to_user(void __user *to, const void *from, unsigned long n) printProc(&p_info); printk("\n\n******************************\n\n"); copy_to_user(info,&p_info,sizeof(struct procinfos)); return 0; } } printk("No proccess with pid = %d",pid); printk("\n\n******************************\n\n"); return EINVAL; } void charcpy(char *_dest, const char *_src) { int i = 0; while (_src[i] != '\0' && i < 15) { _dest[i] = _src[i]; i++; } _dest[i] = '\0'; } void printProc(struct procinfos *info) { printk("%ld\n", info->studentID); printk("Process id : %d, process \'s name : %s\n", info->proc.pid, info->proc.name); printk("Parent process id : %d, process \'s name : %s\n", info->parent_proc.pid, info->parent_proc.name); printk("Child process id : %d, process \'s name : %s\n", info->oldest_child_proc.pid, info->oldest_child_proc.name); }