mod_hello/mod_hello.c
2014-09-18 16:05:40 -04:00

87 lines
2.1 KiB
C

#include <linux/module.h> /* all kernel modules need this */
#include <linux/init.h> /* provides initialization routines */
#include <linux/sched.h>
#include <linux/kallsyms.h>
#include <linux/string.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Vincent Batts <vbatts@hashbangbash.com>");
MODULE_DESCRIPTION("just getting familiar");
MODULE_VERSION("1.0");
//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";
static int hello_print_hello(void)
{
printk(KERN_INFO "[%s] module loaded.\n", __this_module.name);
return 0;
}
long hello_fib(long times, long n1, long n2)
{
long i = 0;
long prev = n1;
long curr = n2;
long next = 0;
while (i < times)
{
next = (prev + curr);
prev = curr;
curr = next;
i++;
}
return next;
}
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];
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);
return 0;
}
static void __exit hello_cleanup(void)
{
printk(KERN_INFO "[%s] module unloaded.\n", __this_module.name);
}
module_init(hello_init);
module_exit(hello_cleanup);
// vim:set shiftwidth=4 softtabstop=4 expandtab: