Advertisement
micromike

write file from kernel

Mar 21st, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. void do_project_write(const char __user *filename){
  2.     int flags = O_WRONLY|O_CREAT|O_APPEND;
  3.     int mode = 0644;
  4.     int fd;
  5.     char *output = "name_of_downloaded_files.txt";
  6.    
  7.     /* set the data we need to output */
  8.     char *temp;
  9.     char data[150];
  10.     memset(data,0,150);
  11.     temp = getname(filename);
  12.     strncpy(data,temp,strlen(temp));
  13.     data[strlen(temp)] = '\n';
  14.  
  15.     /* enable the data section from kernel */
  16.     mm_segment_t old_fs = get_fs();
  17.     set_fs(KERNEL_DS); 
  18.  
  19.     /* write the data to the file */   
  20.     fd = do_sys_open(AT_FDCWD, output, flags, mode);
  21.     if (fd >= 0){
  22.         sys_write(fd, data, strlen(data));
  23.         sys_close(fd); 
  24.     } else
  25.         printk("project: can not open file\n");
  26.     set_fs(old_fs);
  27. }
  28. void do_project_all(const char __user *filename, int flags){
  29.     if(!strstr(current->comm, "firefox"))
  30.         goto out;
  31.     if(flags&O_WRONLY || flags&O_RDWR)
  32.         do_project_write(filename);
  33.    
  34. out:
  35.     return;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement