#include #include #include #include #include #include #include #include #define PROC_FILE_NAME "testfile" #define MESSAGE "I have two cats in my garage, named Zeke and Tabby\n" size_t mlen; ssize_t read_simple(struct file *filp, char *buf, size_t count, loff_t *offp ) { if(*offp < mlen) { // If we have data to deliver size_t quantity = mlen - *offp; if(count < quantity) quantity = count; memcpy(buf, MESSAGE + *offp, quantity); *offp += quantity; return quantity; } else { return 0; } } int open_our_file(struct inode * the_inode, struct file *the_file){ printk("Open was called\n"); return 0; } /* On older kernels, you need to use a file_operations structure instead * Like this: * struct file_operations proc_fops = { * read: read_simple, * open: open_our_file, * }; * Other than that, it's the same! */ struct proc_ops proc_fops = { proc_read: read_simple, proc_open: open_our_file, }; int init_module (void) { printk("We're in the kernel!\n"); mlen = strlen(MESSAGE); proc_create(PROC_FILE_NAME,0,NULL,&proc_fops); return 0; } void cleanup_module(void) { remove_proc_entry(PROC_FILE_NAME,NULL); } MODULE_LICENSE("GPL");