/* procfile.c * Variation of Ch 5.1 proc file module from * Linux Kernel Module Programmer's Guide for 2.4 Kernel */ /* kernel module programming */ #ifndef MODULE #define MODULE #endif #ifndef LINUX #define LINUX #endif #ifndef __KERNEL__ #define __KERNEL__ #endif #include /* needed by all modules */ #include /* needed for KERN_ALERT */ #include /* working with the proc fs */ /* prototypes */ int my_get_info(char *sys_buf, char **user_buf, off_t file_pos, /* position in user_buf */ int buf_len); /* for system buf_len ?? */ int my_read_proc(char *sys_buffer, /* system supplied buffer */ char **user_buffer, /* user provided buffer */ off_t file_pos, /* position in proc file read */ int buflen, /* for system buf_len ?? */ int *eof, /* I could guess */ void *data); /* no clue */ int init_module(void); void cleanup_module(void); /* What is this old one for, then? */ int my_get_info(char *sys_buf, char **user_buf, off_t file_pos, /* position in user_buf */ int buf_len) /* for system buf_len ?? */ { printk(KERN_ALERT "my_get_info of proc module: entering.\n"); printk(KERN_ALERT "my_get_info of proc module: leaving.\n"); return 0; } /* My function to supply the proc file read information */ int my_read_proc(char *sys_buffer, /* system supplied buffer */ char **user_buffer, /* user provided buffer */ off_t file_pos, /* position in proc file read */ int buflen, /* buffer size (system buffer?) */ int *eof, /* I could guess */ void *data) /* no clue */ { printk(KERN_ALERT "my_read_proc of proc module: entering.\n"); printk(KERN_ALERT "my_read_proc of proc module: leaving.\n"); return 0; } /* Directory structure for proc files (a psuedo inode?) * This is created dynamically in kernel 2.4, see init_module below */ struct proc_dir_entry *my_proc_file; /* struct proc_dir_entry my_proc_file = { 0, * inode number - 0 means system will auto-generate * 4, * length of file name * "test", * file name * S_IFREG | S_IRUGO, * file mod - regular file with ugo read permission * 1, * number of links * 0,0, * uid, gid - make both root * 0, * size of file as reported by ls (most proc files report 0) * NULL, * inode operations - none * NULL, * file operations - none * my_get_info, * I supply the get_info function defined below * NULL, * no module owner - I hope this works * NULL, NULL, NULL, * struct proc_dir_entry *next, *parent, *subdir; * NULL, * void *data; * my_read_proc, * read_proc - do I supply the read_proc function? What for? * NULL * write_proc * * 0, * atomic_t count; use count (what value? Where is atomic_t?) * * 0, * int deleted; delete flag (what value?) * * {0,0} * kdev_t rdev; see kdev_t.h the type is a struct of two short int * }; */ /* create_proc_read_entry registers the read function. See proc_fs.h */ int init_module(void) { printk(KERN_ALERT "procfile module loaded\n"); my_proc_file = create_proc_read_entry( "test", /* proc file name */ 0444, /* uid gid mode */ NULL, /* dir entry base */ my_read_proc, /* read function */ NULL); /* data for function ?? */ /* A non-zero return means init_module failed; module can't be loaded */ if (my_proc_file == NULL) return -ENOMEM; return 0; } void cleanup_module(void) { printk(KERN_ALERT "procfile module unloaded\n"); remove_proc_entry("test", NULL); /* file name and parent */ } MODULE_LICENSE("GPL");