2008 May 15th Thursday (五月 十五日 木曜日)

Block processing. #include <linux/module.h> /*Specifically,a module*/ #include <linux/kernel.h> /*We're doing kernel work*/ #include <linux/proc_fs.h> /*Necessary because we use the proc fs*/ #include <linux/sched.h> /*For putting processes to sleep and waking them up*/ #include <asm/uaccess.h> /*for get_user and put_user*/ #define MESSAGE_LENGTH 80 static char Message[MESSAGE_LENGTH]; static struct proc_dir_entry *Our_Proc_File; #define PROC_ENTRY_FILENAME "sleep" /* * Since we use the file operations struct, we can't use the special proc * output provisions - we have to use a standard read function, which is this * function */ static ssize_t module_output(struct file*file, /* see include/linux/fs.h */     char*buf, /*The buffer to put data to(in the user segment) */     size_t len, /*The length of the buffer*/     loff_t *offset) {     static int finished=0;     int i;     char message[MESSAGE_LENGTH+30];     /*     * Return 0 to signify end of file - that we have nothing     * more to say at this point.     */     if(finished){         finished = 0;         return 0;     }         /*     * If you don't understand this by now, you're hopeless as a kernel     * programmer.     */     sprintf(message,"Lastinput:%s/n",Message);     for(i=0; i<len&&message[i]; i++)         put_user(message[i],buf+i);     finished=1;     returni; /*Return the number of bytes "read"*/ } static ssize_t module_input(struct file *filp, const char *buff,size_t len,loff_t *off){     inti;     /*     * Put the input into Message, where module_output will later be     * able to use it     */     for(i=0;i<MESSAGE_LENGTH - 1 && i < length; i++)         get_user(Message[i],buf+i);     /*     * we want a standard, zero terminated string     */     Message[i]='/0';     /*     * We need to return the number of input characters used     */     return i; } /* * 1 if the file is currently open by somebody */ int Already_Open = 0; /* * Queue of processes who want our file */ DECLARE_WAIT_QUEUE_HEAD(WaitQ); /* * Called when the /proc file is closed */ int module_close(struct inode *inode, struct file *file) {     /*     * Set Already_Open to zero, so one of the processes in the WaitQ will     * be able to set Already_Open back to one and to open the file.  All     * the other processes will be called when Already_Open is back to one,     * so they'll go back to sleep.     */     Already_Open=0;     /*     * Wake up all the processes in WaitQ, so if anybody is waiting for the     * file, they can have it.     */     wake_up(&WaitQ);     module_put(THIS_MODULE);     return 0; /*success*/ } /* * This function decides whether to allow an operation (return zero) or not * allow it (return a non-zero which indicates why it is not allowed). * * The operation can be one of the following values: * 0 - Execute(run the"file" - meaningless in our case) * 2 - Write(input to the kernel module) * 4 - Read(output from the kernel module) * * This is the real function that checks file permissions. The permissions * returned by ls -l are for reference only, and can be overridden here. */ static int module_permission(struct inode *inode, int op, struct nameidata *nd) {     /*     * We allow everybody to read from our module, but only root (uid 0)     * may write to it     */     if(op==4||(op==2&&current->euid==0))         return 0;     return -EACCES; } staticstructfile_operationsFile_Ops_4_Our_Proc_File={     .read=module_output, /*"read" from the file*/     .write=module_input, /*"write" to the file*/     .open=module_open, /*called when the/proc file is opened*/     .release=module_close, /*called when it's closed*/ }; static struct inode_operationsInode_Ops_4_Our_Proc_File={     .permission=module_permission, /*check for permissions*/ }; int init_module(){     int rv = 0;         Our_Proc_File = create_proc_entry("test", 0644, NULL);         printk(KERN_INFO "Trying to create /proc/test:/n");         if (Our_Proc_File == NULL){         rv= -ENOMEM;         remove_proc_entry("test", &proc_root);         printk(KERN_INFO "Error: Could not initialize /proc/test/n");     }     else{         Our_Proc_File->read_proc = procfile_read;         Our_Proc_File->owner = THIS_MODULE;         Our_Proc_File->mode = S_IFREG | S_IRUGO;         Our_Proc_File->uid = 0;         Our_Proc_File->gid = 0;         Our_Proc_File->size = 37;                 printk(KERN_INFO "Success!/n");     }         return rv; } void cleanup_module(){     remove_proc_entry("test", &proc_root);     printk(KERN_INFO "/proc/test removed/n"); }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值