Advertisement
alsiva

UserApp

Dec 18th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include "character_dev.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <sys/ioctl.h>
  7.  
  8. #define VM_READ 0x00000001
  9. #define VM_WRITE 0x00000002
  10. #define VM_EXEC 0x00000004
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.     int fd;
  15.  
  16.     if (argc < 2 || (strcmp(argv[1], "help") == 0))
  17.     {
  18.         printf("Usage %s <PID> <PAGE-NUMBER>\n", argv[0]);
  19.         printf("<PID> must be integer more than zero\n");
  20.  
  21.         return 0;
  22.     }
  23.     int pid = atoi(argv[1]);
  24.     if (pid)
  25.     {
  26.         printf("Entered <PID> is %d\n", pid);
  27.     }
  28.     else
  29.     {
  30.         printf("Wrong <PID>\n");
  31.         return 1;
  32.     }
  33.  
  34.     fd = open(DEVICE_NAME, 0);
  35.  
  36.     if (fd < 0)
  37.     {
  38.         printf("Can't open device file: %s\n", DEVICE_NAME);
  39.         exit(2);
  40.     };
  41.  
  42.     struct pci_dev_info *pdi = malloc(sizeof(struct pci_dev_info));
  43.     struct vm_area_struct_info *vasi = malloc(sizeof(struct vm_area_struct_info));
  44.  
  45.     vasi->pid = pid;
  46.  
  47.     int ret_val = ioctl(fd, IOCTL_GET_PCI_DEV, pdi);
  48.     if (ret_val != 0)
  49.     {
  50.         printf("IOCTL_GET_PCI_DEV failed %d", ret_val);
  51.         exit(ret_val);
  52.     }
  53.     printf("<-- PCI DEV -->\n");
  54.     for (int i = 0; i < pdi->actual_count; i++)
  55.     {
  56.         printf("pci found [%d]\n", pdi->devices[i]);
  57.     }
  58.     ret_val = ioctl(fd, IOCTL_GET_VM_AREA_STRUCT, vasi);
  59.     printf("<-- VM AREA STRUCT -->\n");
  60.     if (ret_val != 0)
  61.     {
  62.         printf("IOCTL_GET_VM_AREA_STRUCT failed %d", ret_val);
  63.         if (ret_val == 1)
  64.         {
  65.             printf("Process with <PID> = %d doesn't exist\n", vasi->pid);
  66.         }
  67.         if (ret_val == 2)
  68.         {
  69.             printf("Can't find vm_area_struct for Process with <PID> = %d\n",vasi->pid);
  70.         }
  71.  
  72.         exit(ret_val);
  73.     }
  74.  
  75.     for (int i = 0; i < vasi->actual_count; i++)
  76.     {
  77.         printf("0x%0.8hx-0x%0.8hx\t", vasi->vapi[i].vm_start, vasi->vapi[i].vm_end);
  78.         printf("%c%c%c",
  79.                (vasi->vapi[i].permissions & VM_READ) ? 'r' : '-',
  80.                (vasi->vapi[i].permissions & VM_WRITE) ? 'w' : '-',
  81.                (vasi->vapi[i].permissions & VM_EXEC) ? 'x' : '-');
  82.         printf("\t%1d", vasi->vapi[i].rb_subtree_gap);
  83.         printf("\n");
  84.     }
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement