mod_hello/mod_hello.c

138 lines
3.5 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>
#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)
{
char * temp = arg;
printk(KERN_INFO "[%s] received command: %d (arg %s)\n", __this_module.name, cmd, arg);
return 0;
}
static int
hello_control_open (struct inode *inode, struct file *filp)
{
return 0;
}
static ssize_t
hello_control_read (struct file *filp, char __user *buf, size_t size, loff_t *offp)
{
int ret = 0;
return ret;
}
static const struct file_operations hello_ctl_fops = {
.unlocked_ioctl = hello_control_ioctl,
.compat_ioctl = hello_control_ioctl,
.owner = THIS_MODULE,
.llseek = noop_llseek,
.open = hello_control_open,
.read = hello_control_read,
};
static struct miscdevice hello_misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = "helloctl",
.fops = &hello_ctl_fops,
.mode = 0666,
};
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";
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;
}
// 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)
{
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 misc_register(&hello_misc);
}
static void __exit hello_cleanup(void)
{
misc_deregister(&hello_misc);
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: