mod_hello/mod_hello.c

127 lines
3.3 KiB
C
Raw Normal View History

2014-09-17 20:51:24 +00:00
#include <linux/module.h> /* all kernel modules need this */
#include <linux/init.h> /* provides initialization routines */
#include <linux/sched.h>
2014-09-17 20:51:24 +00:00
#include <linux/kallsyms.h>
#include <linux/string.h>
2014-09-17 20:51:24 +00:00
#include <linux/fs.h>
#include <linux/miscdevice.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Vincent Batts <vbatts@hashbangbash.com>");
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";
2014-09-17 20:51:24 +00:00
static int hello_print_hello(void)
2014-09-17 20:51:24 +00:00
{
printk(KERN_INFO "[%s] module loaded.\n", __this_module.name);
2014-09-17 20:51:24 +00:00
return 0;
}
long hello_fib(long times, long n1, long n2)
2014-09-17 20:51:24 +00:00
{
long i = 0;
long prev = n1;
long curr = n2;
long next = 0;
2014-09-17 20:51:24 +00:00
while (i < times)
{
next = (prev + curr);
prev = curr;
curr = next;
i++;
}
return next;
}
// module parameters
static char *sym_name = "files";
module_param(sym_name, charp, S_IRUGO);
static void hello_fib_stuff(long f_cap, long f_start1, long f_start2) {
long f_int;
unsigned long jiffies_start, jiffies_diff;
jiffies_start = jiffies;
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)
2014-09-17 20:51:24 +00:00
{
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);
2014-09-17 20:51:24 +00:00
return misc_register(&hello_misc);
2014-09-17 20:51:24 +00:00
}
static void __exit hello_cleanup(void)
2014-09-17 20:51:24 +00:00
{
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);
2014-09-17 20:51:24 +00:00
}
module_init(hello_init);
module_exit(hello_cleanup);
2014-09-17 20:51:24 +00:00
// vim:set shiftwidth=4 softtabstop=4 expandtab: