Advertisement
HimikoWerckmeister

Untitled

Apr 17th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. /*
  2. #include <linux/kernel.h>
  3. #include "process_ancestors.h"
  4. #include <linux/slab.h>
  5. #include <asm/uaccess.h>
  6. #include <linux/sched.h>
  7. asmlinkage long sys_process_ancestors(struct process_info info_array[], long size, long *num_filled)
  8. {
  9. struct task_struct* tmp=current;
  10. long i;
  11. int err=0;
  12. for(i=0;i<size;i++) {
  13. struct process_info inf;
  14. memset(&inf,0,sizeof(inf));
  15. //copy fields
  16. inf.pid=(long)tmp->pid;
  17. memcpy(inf.name,tmp->comm,ANCESTOR_NAME_LEN);
  18. inf.state=tmp->state;
  19. inf.uid=tmp->cred->uid.val;
  20. inf.nvcsw=tmp->nvcsw;
  21. inf.nivcsw=tmp->nivcsw;
  22.  
  23. if((err=copy_to_user(info_array+i,&inf,sizeof(inf)))!=0) goto out;
  24. if(tmp->parent==NULL || tmp->parent==tmp) break;
  25. tmp=tmp->parent;
  26. }
  27. i++;
  28. err=copy_to_user(num_filled,&i,sizeof(i));
  29. out:
  30. return err;
  31. }
  32. */
  33.  
  34. #include "process_ancestors.h"
  35. #include <linux/kernel.h>
  36. #include <linux/sched.h>
  37. #include <linux/string.h>
  38. #include <linux/cred.h>
  39. #include <linux/list.h>
  40. #include <linux/uaccess.h>
  41.  
  42. asmlinkage long sys_process_ancestors(struct process_info info_array[],
  43. long size, long *num_filled) {
  44. struct task_struct *curr_task, *prev_task;
  45. long num_filled_kernel = 0;
  46. struct list_head *curr_node;
  47.  
  48. if (size < 1) {
  49. printk("size too small\n");
  50. return -EINVAL;
  51. }
  52.  
  53. //`current` is a pointer to a task_struct that gives us info about the process
  54. //that is currently executing
  55. curr_task = current;
  56. //get info associated with process in curr_task
  57. do {
  58. struct process_info inf;
  59. memset(&inf,0,sizeof(inf));
  60.  
  61. //pid
  62. //printk("pid: %d\n", curr_task->pid);
  63. inf.pid = (long)curr_task->pid;
  64.  
  65. //name
  66. //printk("name: %s\n", curr_task->comm);
  67. strcpy(inf.name, curr_task->comm);
  68.  
  69. //state: -1 unrunnable, 0 runnable, >0 stopped
  70. //printk("state: %ld\n", curr_task->state);
  71. inf.state = curr_task->state;
  72.  
  73. //uid
  74. //printk("uid: %ld\n", curr_task->cred->uid.val);
  75. inf.uid = curr_task->cred->uid.val;
  76.  
  77. //nvcsw
  78. //printk("nvcsw: %ld\n", curr_task->nvcsw);
  79. inf.nvcsw = curr_task->nvcsw;
  80.  
  81. //nivcsw
  82. //printk("nivcsw: %ld\n", curr_task->nivcsw);
  83. inf.nivcsw = curr_task->nivcsw;
  84.  
  85. //num_children
  86. //`list_for_each` macro from <linux/list.h> takes a node of a circularly linked list
  87. //and allows us to iterate through each node of the list it is a part of
  88. list_for_each(curr_node, &(curr_task->children)) {
  89. inf.num_children++;
  90. }
  91. //printk("num_children: %ld\n", inf.num_children);
  92.  
  93. //num_siblings
  94. list_for_each(curr_node, &(curr_task->sibling)) {
  95. inf.num_siblings++;
  96. }
  97. //printk("num_siblings: %ld\n", inf.num_siblings);
  98.  
  99. //copy filled process_info struct to user
  100. if(copy_to_user(&info_array[num_filled_kernel], &inf, sizeof(inf))) {
  101. return -EFAULT;
  102. }
  103.  
  104. //ascend one level in process hierarchy
  105. prev_task = curr_task;
  106. curr_task = curr_task->parent;
  107. num_filled_kernel++;
  108. } while (prev_task->pid != 0 && num_filled_kernel < size);
  109.  
  110. //copy number_filled to user
  111. if(copy_to_user(num_filled, &num_filled_kernel, sizeof(long))) {
  112. return -EFAULT;
  113. }
  114.  
  115. //successful
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement