adding a simple ioctl to a new /dev/helloctl

This commit is contained in:
Vincent Batts 2014-10-02 22:33:29 -04:00
parent 26d1d42080
commit 5eae9089e8

View file

@ -6,11 +6,41 @@
#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)
{
//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);
}