daily pastebin goal
75%
SHARE
TWEET

func.c

a guest Feb 22nd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "mod_proc.h"
  2. #include <linux/list.h>
  3.  
  4. typedef struct PList
  5. {
  6.     struct task_struct* task;
  7.     struct PList* next;
  8. }PList;
  9.  
  10. bool is_number(const char *str, int n)
  11. {
  12.     int i; 
  13.     for(i = 0; i < n; i++)
  14.         if (str[i] < '0' || str[i] > '9')
  15.             return false;
  16.     return true;
  17. }
  18.  
  19. int str_to_number(const char *str, int n)
  20. {
  21.     int res = 0;
  22.     int i;
  23.     for(i = 0; i < n; i++)
  24.         res = res * 10 + ( str[i] - '0' );
  25.     return res;
  26. }
  27.  
  28. void mem_info(int id, char* buf)
  29. {
  30.     char *str = vmalloc(1000 * sizeof(char));
  31.     int offset = 0;
  32.     int cnt;
  33.  
  34.     bool found = false;
  35.     struct task_struct *task;
  36.     struct task_struct *t = current;
  37.     struct mm_struct *m;
  38.     struct vm_area_struct *v;
  39.     struct kstat *ks;
  40.     unsigned long t_size = 0;
  41.     rcu_read_lock();
  42.     for_each_node(task)
  43.     {
  44.         //task_lock(task);
  45.         int iid = task -> pid;
  46.         if(iid == id)
  47.         {  
  48.             t = task;
  49.             found = true;
  50.         }
  51.         //task_unlock(task);
  52.                
  53.     }
  54.     rcu_read_unlock();
  55.     if(found)
  56.     {
  57.         printk("//////VIRTUAL MEMORY INFORMATION//////\n\n");
  58.         v = t -> mm -> mmap;
  59.         if(v != NULL)
  60.         {
  61.             printk("Process: %s[%d]\n",t -> comm, t -> pid);
  62.             cnt = sprintf(str, "Process: %s[%d]\n",t -> comm, t -> pid);
  63.             str[cnt] = '\0';
  64.             memcpy(buf + offset, str, strlen(str));
  65.             offset += strlen(str);
  66.             while(v -> vm_next != NULL)
  67.             {
  68.                 unsigned long size = v -> vm_end - v -> vm_start;
  69.                 t_size = t_size + size;
  70.                 printk("Start: 0x%lx, End: 0x%lx, Block Size: 0x%lx\n", v -> vm_start, v -> vm_end, size);
  71.                 cnt = sprintf(str, "Start: 0x%lx, End: 0x%lx, Block Size: 0x%lx\n", v -> vm_start, v -> vm_end, size);
  72.                 str[cnt] = '\0';
  73.                 memcpy(buf + offset, str, strlen(str));
  74.                 offset += strlen(str);
  75.                 v = v -> vm_next;
  76.             }
  77.             printk("Total size of virtual space is: 0x%lx\n",t_size);
  78.             cnt = sprintf(str, "Total size of virtual space is: 0x%lx\n",t_size);
  79.             str[cnt] = '\0';
  80.             memcpy(buf + offset, str, strlen(str));
  81.             offset += strlen(str);
  82.         }
  83.     }
  84.     else
  85.     {
  86.         printk("ID %d not found\n", id);
  87.         cnt = sprintf(str, "ID %d not found\n", id);
  88.         str[cnt] = '\0';
  89.         memcpy(buf + offset, str, strlen(str));
  90.         offset += strlen(str);
  91.     }
  92.    
  93.     buf[offset] = '\0';
  94.     vfree(str);
  95.  
  96. }
  97.  
  98. void files_info(int id, char* buf)
  99. {
  100.     char *str = vmalloc(1000 * sizeof(char));
  101.     int offset = 0;
  102.     int cnt;
  103.  
  104.     bool found = false;
  105.     struct task_struct *task;
  106.     struct task_struct *t = current;
  107.     struct files_struct *open_files;
  108.     struct fdtable *files_table;
  109.     struct path files_path;
  110.     rcu_read_lock();
  111.     for_each_node(task)
  112.     {
  113.         //task_lock(task);
  114.         int iid = task -> pid;
  115.         if(iid == id)
  116.         {  
  117.             t = task;
  118.             found = true;
  119.         }
  120.         //task_unlock(task);
  121.                
  122.     }
  123.     rcu_read_unlock();
  124.     if(found)
  125.     {
  126.  
  127.         printk("//////OPEN FILES INFORMATION//////\n\n");
  128.         printk("Process: %s[%d]\n",t -> comm, t -> pid);
  129.         cnt = sprintf(str, "Process: %s[%d]\n",t -> comm, t -> pid);
  130.         str[cnt] = '\0';
  131.         memcpy(buf + offset, str, strlen(str));
  132.         offset += strlen(str);
  133.  
  134.         int i = 0;
  135.         open_files = t -> files;
  136.         files_table = files_fdtable(open_files);
  137.         char *path;
  138.         char *buf_tmp = (char*)kmalloc(10000 * sizeof(char), GFP_KERNEL);
  139.         while(files_table -> fd[i] != NULL)
  140.         {
  141.             files_path = files_table -> fd[i] -> f_path;
  142.             char* name = files_table-> fd[i] -> f_path.dentry -> d_iname;
  143.             long long size = i_size_read(files_table-> fd[i] -> f_path.dentry -> d_inode);
  144.             path = d_path(&files_path, buf_tmp, 10000 * sizeof(char));
  145.             printk("Name: %s, FD: %d, Size: 0x%llx bytes, Path: %s\n", name, i, size , path);
  146.             cnt = sprintf(str, "Name: %s, FD: %d, Size: 0x%llx bytes, Path: %s\n", name, i, size , path);
  147.             str[cnt] = '\0';
  148.             memcpy(buf + offset, str, strlen(str));
  149.             offset += strlen(str);
  150.             i++;
  151.         }
  152.         kfree(buf_tmp);
  153.     }
  154.     else
  155.     {
  156.         printk("ID %d not found\n", id);
  157.         cnt = sprintf(str, "ID %d not found\n", id);
  158.         str[cnt] = '\0';
  159.         memcpy(buf + offset, str, strlen(str));
  160.         offset += strlen(str);
  161.     }
  162.    
  163.     buf[offset] = '\0';
  164.     vfree(str);
  165.  
  166. }
  167.  
  168. void process_info(struct task_struct* task, int n, char* buf, int* offset)
  169. {
  170.     char *str = vmalloc(1000 * sizeof(char));
  171.     int cnt;
  172.    
  173.     int count = 0;
  174.     struct PList* head = kmalloc(sizeof(PList), GFP_KERNEL);
  175.     head -> task = NULL;
  176.     head -> next = NULL;
  177.     struct PList* cur = head;
  178.     struct list_head* pos;
  179.     list_for_each(pos, &task->children)
  180.     {
  181.         if (head -> task == NULL)
  182.             head -> task = list_entry(pos, struct task_struct, sibling);
  183.         else
  184.         {
  185.             cur -> next = kmalloc(sizeof(PList),GFP_KERNEL);
  186.             cur -> next -> task = list_entry(pos, struct task_struct, sibling);
  187.             cur -> next -> next = NULL;
  188.             cur = cur -> next;
  189.         }
  190.         count++;
  191.     }
  192.     printk("Process: %s[%d], Parent: %s[%d]\n", task -> comm, task -> pid , task -> parent -> comm, task -> parent -> pid);
  193.     cnt = sprintf(str, "Process: %s[%d], Parent: %s[%d]\n",
  194.                  task -> comm, task -> pid , task -> parent -> comm, task -> parent -> pid);
  195.     str[cnt] = '\0';
  196.     memcpy(buf + (*offset), str, strlen(str));
  197.     (*offset) += strlen(str);
  198.     if(count > 0)
  199.     {
  200.         struct PList* pr;
  201.         n = n - 1;
  202.         int i = 1;
  203.         if(n > 0)
  204.         for(pr = head; pr != NULL;)
  205.         {
  206.             int m = DEPTH;
  207.             for(; m > n; m--)
  208.             {
  209.                 printk("\t");
  210.                 memcpy(buf + (*offset), "\t", strlen("\t"));
  211.                 (*offset) += strlen("\t");
  212.             }
  213.             printk("--->Child: %d, ", i);
  214.             cnt = sprintf(str, "--->Child: %d, ", i);
  215.             str[cnt] = '\0';
  216.             memcpy(buf + (*offset), str, strlen(str));
  217.             (*offset) += strlen(str);
  218.             process_info(pr -> task, n, buf, offset);
  219.             i = i+1;
  220.             struct PList* temp = pr;
  221.             pr = pr -> next;
  222.             kfree(temp);
  223.             temp = NULL;
  224.             }
  225.     }
  226.    
  227.     buf[*offset] = '\0';
  228.     vfree(str);  
  229.     return 0;
  230. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top