From 5eae9089e874fe8485dc551b31a0afc0593060ed Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 2 Oct 2014 22:33:29 -0400 Subject: [PATCH] adding a simple ioctl to a new /dev/helloctl --- mod_hello.c | 85 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/mod_hello.c b/mod_hello.c index 02b5b36..ff5f356 100644 --- a/mod_hello.c +++ b/mod_hello.c @@ -6,11 +6,41 @@ #include #include +#include +#include + MODULE_LICENSE("GPL"); MODULE_AUTHOR("Vincent Batts "); MODULE_DESCRIPTION("just getting familiar"); MODULE_VERSION("1.0"); +static long +hello_control_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + //int ret = -ENOTTY; + + //switch (cmd) { + //} + printk(KERN_INFO "[%s] received command: %d\n", __this_module.name, cmd); + return 0; +} + +static const struct file_operations hello_ctl_fops = { + .unlocked_ioctl = hello_control_ioctl, + .compat_ioctl = hello_control_ioctl, + .owner = THIS_MODULE, + .llseek = noop_llseek, +}; + +static struct miscdevice hello_misc = { + .minor = MISC_DYNAMIC_MINOR, + .name = "helloctl", + .fops = &hello_ctl_fops, +}; + +MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR); +MODULE_ALIAS("devname:helloctl"); + //static char __initdata hello_hellomsg[] = KERN_NOTICE "Hello, Kernel, I'm in\n"; //static char __exitdata hello_byemsg[] = KERN_NOTICE "Bye, I'm out\n"; @@ -39,45 +69,54 @@ long hello_fib(long times, long n1, long n2) } +// module parameters static char *sym_name = "files"; module_param(sym_name, charp, S_IRUGO); -static int __init hello_init(void) -{ - long f_int, f_cap, f_start1, f_start2; - char filename[255]; +static void hello_fib_stuff(long f_cap, long f_start1, long f_start2) { + long f_int; unsigned long jiffies_start, jiffies_diff; - unsigned long sym_addr; - - // in linux/sched.h - //struct task_struct *current; - printk("[%s] pid: %d, comm: %s\n", __this_module.name, current->pid, current->comm); - - hello_print_hello(); - - printk("looking up '%s'\n", sym_name); - sym_addr = kallsyms_lookup_name(sym_name); - printk("%s 0x%lx\n", sym_name, sym_addr); - - strncpy(filename, (char *)sym_addr, 255); - printk(KERN_INFO "[%s] %s (0x%lx): %s\n", __this_module.name, sym_name, sym_addr, filename); - jiffies_start = jiffies; - f_cap = 10000000; - f_start1 = 0; - f_start2 = 1; f_int = hello_fib(f_cap, f_start1, f_start2); jiffies_diff = jiffies - jiffies_start; printk("[%s] fib of %ld and %ld (up to %ld): %ld (in only %lu jiffies)\n", __this_module.name, f_start1, f_start2, f_cap, f_int, jiffies_diff); +} + +#define FILENAME_LENGTH 255 + +static int __init hello_init(void) +{ + char filename[FILENAME_LENGTH]; + unsigned long sym_addr; + + // in linux/sched.h + //struct task_struct *current; + printk(KERN_INFO "[%s] pid: %d, comm: %s\n", __this_module.name, current->pid, current->comm); + + hello_print_hello(); + + printk(KERN_INFO "[%s] looking up '%s'\n", __this_module.name, sym_name); + sym_addr = kallsyms_lookup_name(sym_name); + printk(KERN_INFO "[%s] %s 0x%lx\n", __this_module.name, sym_name, sym_addr); + + if ((char *)sym_addr != NULL) { + strncpy(filename, (char *)sym_addr, FILENAME_LENGTH); + printk(KERN_INFO "[%s] %s (0x%lx): %s\n", __this_module.name, sym_name, sym_addr, filename); + } + + hello_fib_stuff(10000000, 0, 1); - return 0; + return misc_register(&hello_misc); } static void __exit hello_cleanup(void) { + if (misc_deregister(&hello_misc) < 0) { + printk(KERN_INFO "[%s] misc_deregister failed for control device.\n", __this_module.name); + } printk(KERN_INFO "[%s] module unloaded.\n", __this_module.name); }