⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kcp.c

📁 fsmlabs的real time linux的内核
💻 C
字号:
/* vim: set ts=4: *//* * Copywrite 2002 Der Herr Hofrat * License GPL V2 * Author der.herr@hofr.at *//* * example of copying a file in kernel mode. */#include <linux/module.h>#include <linux/slab.h>#include <asm/uaccess.h>char *src = NULL;char *dst = NULL;/* for testing we allow src/dst to be module parameters - basically this makes * no sense for real code. */MODULE_PARM(src,"s");MODULE_PARM(dst,"s");static int orgfsuid,orgfsgid;static mm_segment_t orgfs;/* file access is as root:root !  * need to open up kernel space for parameters - dangorous ! * might be better to take init_fs - hofrat to escape chroot ??  */void kspace_init(void){  orgfsuid=current->fsuid;  orgfsgid=current->fsgid;  current->fsuid=current->fsgid=0;  orgfs=get_fs();  set_fs(KERNEL_DS);}/* reset fscontext again.  */voidkspace_releas(void){  set_fs(orgfs);  current->fsuid=orgfsuid;  current->fsgid=orgfsgid;}/* KernelCoPy - copy files on the filesystem from within kernel space */int kcp(char *src_file,char *dst_file){  struct file *fd0,*fd1;  int retval;  char *buffer;  unsigned long page;  /* make shure we got no null pointer - and copy    * ugly check but if we fail in kernel mode we hard-lock-up    */  if(src_file&&dst_file&&*src_file&&*dst_file){    printk("Copying %s to %s\n",src_file,dst_file);    /* Allocate one page for buffer -      * might be suboptimal but it works      * Note: this is not RT-safe !     */    page = __get_free_page(GFP_KERNEL);    if(page){      buffer=(char*)page;      /* src is opened read-only */      fd0 = filp_open(src_file, O_RDONLY, 0);      if(IS_ERR(fd0)){        printk("kcp: Error %ld opening %s\n",-PTR_ERR(fd0),src_file);      }else{        /* get the fs-specific read method for the source file */        if(fd0->f_op&&fd0->f_op->read){          fd1 = filp_open(dst_file,O_WRONLY|O_TRUNC|O_CREAT,0644);          if (IS_ERR(fd1)){            printk("kcp: Error %ld opening %s\n",-PTR_ERR(fd1),dst_file);          }else{            /* get the dest-file specific write method (if it exists) */            if(fd1->f_op&&fd1->f_op->write){              do{                /* Read to buffer, at most one page.                 */                retval=fd0->f_op->read(fd0,buffer,PAGE_SIZE,&fd0->f_pos);                if(retval<0){                  printk("kcp: Read error %d\n",-retval);                }                if(retval>0){                  int index=0,bufsize=retval;                  while((index<bufsize)&&((retval=fd1->f_op->write(fd1,buffer+index,bufsize-index,&fd1->f_pos))>0)){                    index+=retval;                  }                  if(index<bufsize){                    printk("kcp: Write error %d\n",-retval);                  }                }                /* continues until EOF or error.                 */              }while (retval>0);            }else{            /* clean up temporary resources             */              printk("kcp: %s does not have a write method\n",dst_file);           }         retval=filp_close(fd1,NULL);         if(retval){           printk("kcp: Error %d closing %s\n",-retval,dst_file);         }      }    }else{      printk("kcp: no read method for %s\n",src_file);    } retval=filp_close(fd0,NULL);      if(retval){        printk("kcp: Error %d closing %s\n",-retval,src_file);      }    }    free_page(page);    }else{      printk("kcp: Out of memory\n");    }  }else{    printk("kcp: src or dst seems to be NULL\n");    return -1;  }  return 0;}int init_module(void){	/* kernel side file copy */	kspace_init();	kcp(src,dst);	kspace_releas();		return 0;}void cleanup_module(void){	printk("exit\n");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -