diff --git a/LKMPG-3.16.html b/LKMPG-3.16.html index 97242b0..06d6254 100644 --- a/LKMPG-3.16.html +++ b/LKMPG-3.16.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + The Linux Kernel Module Programming Guide @@ -2065,6 +2065,7 @@ HelloWorld! #include <linux/kernel.h> #include <linux/proc_fs.h> #include <asm/uaccess.h> + #define procfs_name "helloworld" struct proc_dir_entry *Our_Proc_File; @@ -2076,9 +2077,9 @@ HelloWorld! int ret=0; if(strlen(buffer) ==0) { - printk(KERN_INFO "procfile read %s\n",filePointer->f_path.dentry->d_name.name); - ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n")); - ret=sizeof("HelloWorld!\n"); + printk(KERN_INFO "procfile read %s\n",filePointer->f_path.dentry->d_name.name); + ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n")); + ret=sizeof("HelloWorld!\n"); } return ret; @@ -2169,73 +2170,60 @@ The only memory segment accessible to a process is its own, so when writing regu * This function is called then the /proc file is read * */ -int -procfile_read(char *buffer, - char **buffer_location, - off_t offset, int buffer_length, int *eof, void *data) +ssize_t procfile_read(struct file *filePointer,char *buffer, + size_t buffer_length, loff_t * offset) { - int ret; - - printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME); - - if (offset > 0) { - /* we have finished to read, return 0 */ - ret = 0; - } else { - /* fill the buffer, return the buffer size */ - memcpy(buffer, procfs_buffer, procfs_buffer_size); - ret = procfs_buffer_size; + int ret=0; + if(strlen(buffer) ==0) + { + printk(KERN_INFO "procfile read %s\n",filePointer->f_path.dentry->d_name.name); + ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n")); + ret=sizeof("HelloWorld!\n"); } - return ret; } + /** * This function is called with the /proc file is written * */ -int procfile_write(struct file *file, const char *buffer, unsigned long count, - void *data) +static ssize_t procfile_write(struct file *file, const char *buff, + size_t len, loff_t *off) { - /* get buffer size */ - procfs_buffer_size = count; - if (procfs_buffer_size > PROCFS_MAX_SIZE ) { + procfs_buffer_size = len; + if (procfs_buffer_size > PROCFS_MAX_SIZE) procfs_buffer_size = PROCFS_MAX_SIZE; - } - /* write data to the buffer */ - if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) { + if (copy_from_user(procfs_buffer, buff, procfs_buffer_size)) return -EFAULT; - } + procfs_buffer[procfs_buffer_size] = '\0'; return procfs_buffer_size; } +static const struct file_operations proc_file_fops = { + .owner = THIS_MODULE, + .read = procfile_read, + .write = procfile_write, +}; + /** *This function is called when the module is loaded * */ int init_module() { - /* create the /proc file */ - Our_Proc_File = proc_create(PROCFS_NAME, 0, NULL, NULL); - - if (Our_Proc_File == NULL) { - remove_proc_entry(PROCFS_NAME, NULL); - printk(KERN_ALERT "Error: Could not initialize /proc/%s\n", - PROCFS_NAME); + Our_Proc_File = proc_create(PROCFS_NAME,0644,NULL,&proc_file_fops); + if(NULL==Our_Proc_File) + { + proc_remove(Our_Proc_File); + printk(KERN_ALERT "Error:Could not initialize /proc/%s\n",PROCFS_NAME); return -ENOMEM; } - Our_Proc_File->read_proc = procfile_read; - Our_Proc_File->write_proc = procfile_write; - 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 "/proc/%s created\n", PROCFS_NAME); - return 0; /* everything is ok */ + return 0; } /** @@ -2244,7 +2232,7 @@ The only memory segment accessible to a process is its own, so when writing regu */ void cleanup_module() { - remove_proc_entry(PROCFS_NAME, NULL); + proc_remove(Our_Proc_File); printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME); } diff --git a/LKMPG-3.16.org b/LKMPG-3.16.org index 74c1946..fe89172 100644 --- a/LKMPG-3.16.org +++ b/LKMPG-3.16.org @@ -1170,6 +1170,7 @@ HelloWorld! #include #include #include + #define procfs_name "helloworld" struct proc_dir_entry *Our_Proc_File; @@ -1181,9 +1182,9 @@ ssize_t procfile_read(struct file *filePointer,char *buffer, int ret=0; if(strlen(buffer) ==0) { - printk(KERN_INFO "procfile read %s\n",filePointer->f_path.dentry->d_name.name); - ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n")); - ret=sizeof("HelloWorld!\n"); + printk(KERN_INFO "procfile read %s\n",filePointer->f_path.dentry->d_name.name); + ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n")); + ret=sizeof("HelloWorld!\n"); } return ret; @@ -1260,73 +1261,60 @@ static unsigned long procfs_buffer_size = 0; * This function is called then the /proc file is read * */ -int -procfile_read(char *buffer, - char **buffer_location, - off_t offset, int buffer_length, int *eof, void *data) +ssize_t procfile_read(struct file *filePointer,char *buffer, + size_t buffer_length, loff_t * offset) { - int ret; - - printk(KERN_INFO "procfile_read (/proc/%s) called\n", PROCFS_NAME); - - if (offset > 0) { - /* we have finished to read, return 0 */ - ret = 0; - } else { - /* fill the buffer, return the buffer size */ - memcpy(buffer, procfs_buffer, procfs_buffer_size); - ret = procfs_buffer_size; - } - - return ret; + int ret=0; + if(strlen(buffer) ==0) + { + printk(KERN_INFO "procfile read %s\n",filePointer->f_path.dentry->d_name.name); + ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n")); + ret=sizeof("HelloWorld!\n"); + } + return ret; } + /** * This function is called with the /proc file is written * */ -int procfile_write(struct file *file, const char *buffer, unsigned long count, - void *data) +static ssize_t procfile_write(struct file *file, const char *buff, + size_t len, loff_t *off) { - /* get buffer size */ - procfs_buffer_size = count; - if (procfs_buffer_size > PROCFS_MAX_SIZE ) { + procfs_buffer_size = len; + if (procfs_buffer_size > PROCFS_MAX_SIZE) procfs_buffer_size = PROCFS_MAX_SIZE; - } - /* write data to the buffer */ - if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) { + if (copy_from_user(procfs_buffer, buff, procfs_buffer_size)) return -EFAULT; - } + procfs_buffer[procfs_buffer_size] = '\0'; return procfs_buffer_size; } +static const struct file_operations proc_file_fops = { + .owner = THIS_MODULE, + .read = procfile_read, + .write = procfile_write, +}; + /** *This function is called when the module is loaded * */ int init_module() { - /* create the /proc file */ - Our_Proc_File = proc_create(PROCFS_NAME, 0, NULL, NULL); + Our_Proc_File = proc_create(PROCFS_NAME,0644,NULL,&proc_file_fops); + if(NULL==Our_Proc_File) + { + proc_remove(Our_Proc_File); + printk(KERN_ALERT "Error:Could not initialize /proc/%s\n",PROCFS_NAME); + return -ENOMEM; + } - if (Our_Proc_File == NULL) { - remove_proc_entry(PROCFS_NAME, NULL); - printk(KERN_ALERT "Error: Could not initialize /proc/%s\n", - PROCFS_NAME); - return -ENOMEM; - } - - Our_Proc_File->read_proc = procfile_read; - Our_Proc_File->write_proc = procfile_write; - 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 "/proc/%s created\n", PROCFS_NAME); - return 0; /* everything is ok */ + printk(KERN_INFO "/proc/%s created\n", PROCFS_NAME); + return 0; } /** @@ -1335,8 +1323,8 @@ int init_module() */ void cleanup_module() { - remove_proc_entry(PROCFS_NAME, NULL); - printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME); + proc_remove(Our_Proc_File); + printk(KERN_INFO "/proc/%s removed\n", PROCFS_NAME); } #+END_SRC