c - Writing to proc file / give parameter by calling kernel module -


i'm supposed change configuration parameter of kernel using kernel module. kernel module should create proc file , should able change parameter using cat command, e.g. cat "foobar" > /proc/prompt supposed set parameter "foobar", prompt name of proc file created in module.

furthermore should able initialize parameter passing argument when calling module.

these 2 articles relevant sources have found:

http://www.tldp.org/ldp/lkmpg/2.6/html/x769.html writing proc file , http://www.tldp.org/ldp/lkmpg/2.6/html/x323.html initializing parameter command line.

now have couple of questions, first of module far:

#include <linux/kernel.h> #include <linux/version.h> #include <linux/list.h> #include <linux/module.h> #include <linux/proc_fs.h>  #include "sar_main.h"  #define procfs_name "sarlkm"  char procfs_buffer[procfs_max_size];  static unsigned long procfs_buffer_size = 0  struct proc_dir_entry *proc_file_entry;   int procfile_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data){     int ret;      printk(kern_info "procfile_read (/proc/%s) aufgerufen \n",  procfs_name);      if (offset > 0){         ret = 0;     }     else{         memcpy(buffer, procfs_buffer, procfs_buffer_size);         ret = procfs_buffer_size;     }     return ret; }  int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data){      procfs_buffer_size = count;     if (procfs_buffer_size > procfs_max_size){         procfs_buffer_size = procfs_max_size;     }     if ( copy_from_user(procfs_buffer, buffer, procfs_buffer)){         return -efault;     }     return procfs_buffer_size; }     static int __init sar_init(void) {     prompt_proc = create_proc_entry(procfs_name, 0644, null);      if (prompt_proc = null){         remove_proc_entry(procfs_name, &proc_root);         printk(kern_alert "error: konnte proc file nicht kreieren")         return -enomem;     }      prompt_proc->read_proc = procfile_read;     prompt_proc->write_proc = procfile_write;      printk(kern_info "proc/%s wurde erfolgreich kreiert", procfs_name);     return 0; }  static void __exit sar_cleanup(void) {     remove_proc_entry(procfs_name, &proc_root);     printk(kern_info "proc/%s gelöscht", procfs_name); }  module_init(sar_init); module_exit(sar_cleanup);  module_author(driver_author); module_description(driver_desc); module_license("gpl"); 

i think should don't understand why read , write functions supposed work when using cat command. main question configuration parameter stored in proc file? if write "foobar" proc file using cat , use cat proc/prompt read parameter, how read function new value of parameter, i.e. "foobar" stored in procfile?

if try initialize parameter using command line argument have use global variable in store value of parameter, how use global variable in read function, cat proc/prompt gives out value given module command line?

the cat command internally calls read() system call read data file (see man strace).

read() passes arguments vfs , vfs calls custom procfile_read() routine passed arguments (and additional ones passed vfs code). if want know more this, @ fs directory in kernel sources, file read_write.c.

your particular reading function copies, if conditions met, parameter value (which stored in procfs_buffer answer 1 of questions) user-supplied buffer allocated cat, called buffer in particular code. same 1 passed read() system call in:

read(proc_fd, userspace_buf, 10);    /* userspace_buf buffer! */ 

error checking omitted clearness's sake.

to pass value proc file have 2 options:

  • use module_param() , write buffer; can done once because module loadable once (or unload/reload every time want change parameter sounds inconvenient)
  • invoke write() userspace (like in cat) , modify buffer as want to (this used code)

btw, think reading function should check pointer user data, i.e. use copy_to_user(), not memcpy().

for further information, read linux device drivers. there's old edition available @ moment updated 1 being written.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -