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&¤t->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"); }
2008 May 15th Thursday (五月 十五日 木曜日)
最新推荐文章于 2020-11-03 08:07:51 发布