Advertisement
ItsTotallyRSX

Unlink files within a linux kernel module

Jun 6th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. int unlink_file(const char * req_path)
  2. {
  3. int length;
  4. char file_path[256];
  5. char file_dir[256];
  6. struct file * target;
  7. struct file * path;
  8. struct inode *delegated_inode;
  9. int i;
  10.  
  11. length = strlen(req_path);
  12.  
  13. memcpy(file_path, req_path, length + 1);
  14. memcpy(file_dir, req_path, length + 1);
  15.  
  16. target = filp_open(file_path, O_RDONLY, 0600);
  17.  
  18. if (!target)
  19. return 0; /* file not found */
  20.  
  21. for (i = length; i > 0; i--)
  22. {
  23. if (file_dir[i] == '/')
  24. {
  25. *(char **)(&(file_dir[i + 1])) = (char *)0x00;
  26. break;
  27. }
  28. }
  29.  
  30. if (!i) return -1; /* dir err */
  31.  
  32. path = filp_open(file_dir, O_RDONLY, 0600);
  33.  
  34. if (!path) return -2;
  35.  
  36. vfs_unlink(path->f_path.dentry->d_inode, target->f_path.dentry, &delegated_inode); //TODO: check response
  37.  
  38. return 1;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement