2014-09-17 20:51:24 +00:00
|
|
|
#include <linux/module.h> /* all kernel modules need this */
|
|
|
|
#include <linux/init.h> /* provides initialization routines */
|
|
|
|
|
2014-09-18 20:05:40 +00:00
|
|
|
#include <linux/sched.h>
|
|
|
|
|
2014-09-17 20:51:24 +00:00
|
|
|
#include <linux/kallsyms.h>
|
2014-09-18 15:48:25 +00:00
|
|
|
#include <linux/string.h>
|
2014-09-17 20:51:24 +00:00
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/miscdevice.h>
|
|
|
|
|
2014-09-18 15:48:25 +00:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Vincent Batts <vbatts@hashbangbash.com>");
|
|
|
|
MODULE_DESCRIPTION("just getting familiar");
|
|
|
|
MODULE_VERSION("1.0");
|
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:00:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
static const struct file_operations hello_ctl_fops = {
|
|
|
|
.unlocked_ioctl = hello_control_ioctl,
|
|
|
|
.compat_ioctl = hello_control_ioctl,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = noop_llseek,
|
2016-11-14 21:00:36 +00:00
|
|
|
.open = hello_control_open,
|
|
|
|
.read = hello_control_read,
|
2014-10-03 02:33:29 +00:00
|
|
|
};
|
|
|
|
|
2016-11-14 21:00:36 +00:00
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
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");
|
|
|
|
|
2014-09-18 15:48:25 +00:00
|
|
|
//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
|
|
|
|
2014-09-17 21:03:42 +00:00
|
|
|
static int hello_print_hello(void)
|
2014-09-17 20:51:24 +00:00
|
|
|
{
|
2014-09-18 15:48:25 +00:00
|
|
|
printk(KERN_INFO "[%s] module loaded.\n", __this_module.name);
|
2014-09-17 20:51:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-17 21:03:42 +00:00
|
|
|
long hello_fib(long times, long n1, long n2)
|
2014-09-17 20:51:24 +00:00
|
|
|
{
|
2014-09-17 21:03:42 +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;
|
|
|
|
}
|
|
|
|
|
2014-09-18 17:15:59 +00:00
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
// module parameters
|
2014-09-18 17:15:59 +00:00
|
|
|
static char *sym_name = "files";
|
|
|
|
module_param(sym_name, charp, S_IRUGO);
|
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
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
|
|
|
|
|
2014-09-18 15:48:25 +00:00
|
|
|
static int __init hello_init(void)
|
2014-09-17 20:51:24 +00:00
|
|
|
{
|
2014-10-03 02:33:29 +00:00
|
|
|
char filename[FILENAME_LENGTH];
|
2014-09-18 17:15:59 +00:00
|
|
|
unsigned long sym_addr;
|
|
|
|
|
2014-09-18 20:05:40 +00:00
|
|
|
// in linux/sched.h
|
|
|
|
//struct task_struct *current;
|
2014-10-03 02:33:29 +00:00
|
|
|
printk(KERN_INFO "[%s] pid: %d, comm: %s\n", __this_module.name, current->pid, current->comm);
|
2014-09-17 21:03:42 +00:00
|
|
|
|
|
|
|
hello_print_hello();
|
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
printk(KERN_INFO "[%s] looking up '%s'\n", __this_module.name, sym_name);
|
2014-09-18 15:48:25 +00:00
|
|
|
sym_addr = kallsyms_lookup_name(sym_name);
|
2014-10-03 02:33:29 +00:00
|
|
|
printk(KERN_INFO "[%s] %s 0x%lx\n", __this_module.name, sym_name, sym_addr);
|
2014-09-18 15:48:25 +00:00
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-09-18 15:48:25 +00:00
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
hello_fib_stuff(10000000, 0, 1);
|
2014-09-17 20:51:24 +00:00
|
|
|
|
2014-10-03 02:33:29 +00:00
|
|
|
return misc_register(&hello_misc);
|
2014-09-17 20:51:24 +00:00
|
|
|
}
|
|
|
|
|
2014-09-18 15:48:25 +00:00
|
|
|
static void __exit hello_cleanup(void)
|
2014-09-17 20:51:24 +00:00
|
|
|
{
|
2016-11-14 21:00:36 +00:00
|
|
|
misc_deregister(&hello_misc);
|
2014-09-18 15:48:25 +00:00
|
|
|
printk(KERN_INFO "[%s] module unloaded.\n", __this_module.name);
|
2014-09-17 20:51:24 +00:00
|
|
|
}
|
|
|
|
|
2014-09-18 15:48:25 +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:
|