From a722d196c465bae07075819a8bdfb9e9376d2651 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Wed, 10 Aug 2011 22:43:05 +0200 Subject: AƱadidos pidevice y hang --- hang/Makefile | 7 ++++ hang/hang1.c | 16 ++++++++ pid2/Makefile | 7 ---- pid2/loader.sh | 23 ------------ pid2/pid.c | 50 ------------------------- pidevice/Makefile | 7 ++++ pidevice/loader.sh | 24 ++++++++++++ pidevice/open.py | 4 ++ pidevice/pidevice.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ pidevice/unloader.sh | 8 ++++ 10 files changed, 170 insertions(+), 80 deletions(-) create mode 100644 hang/Makefile create mode 100644 hang/hang1.c delete mode 100644 pid2/Makefile delete mode 100644 pid2/loader.sh delete mode 100644 pid2/pid.c create mode 100644 pidevice/Makefile create mode 100755 pidevice/loader.sh create mode 100755 pidevice/open.py create mode 100644 pidevice/pidevice.c create mode 100755 pidevice/unloader.sh diff --git a/hang/Makefile b/hang/Makefile new file mode 100644 index 0000000..75750d3 --- /dev/null +++ b/hang/Makefile @@ -0,0 +1,7 @@ +obj-m += hang1.o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/hang/hang1.c b/hang/hang1.c new file mode 100644 index 0000000..b963281 --- /dev/null +++ b/hang/hang1.c @@ -0,0 +1,16 @@ +#include +#include + + +static int __init hang_init(void) +{ + while(1); + return 0; +} + +static void __exit hang_exit(void) +{ +} + +module_init(hang_init); +module_exit(hang_exit); diff --git a/pid2/Makefile b/pid2/Makefile deleted file mode 100644 index b9e7cfd..0000000 --- a/pid2/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -obj-m += pid.o - -all: - make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules - -clean: - make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/pid2/loader.sh b/pid2/loader.sh deleted file mode 100644 index 579acff..0000000 --- a/pid2/loader.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -module="pidevice" -device="pidevice" -mode="644" - -insmod ./$module.ko $* || exit -1 - -rm -f /dev/${device}[0-4] - -major=$(cat /proc/devices | grep $module | cut -f 1) - -echo major -exit -1 - -for i in {0..3}; do - mknod /dev/${device}$i c $major $i -done - -group="staff" -grep -q '^staff:' /etc/group || group="wheel" - -chgrp $group /dev/${device}[0-3] -chmod $mode /dev/${device}[0-3] diff --git a/pid2/pid.c b/pid2/pid.c deleted file mode 100644 index 6362f82..0000000 --- a/pid2/pid.c +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -static char devname[] = "pidevice"; -static char modname[] = "pid.ko"; -static dev_t mydev; -static struct task_struct *mytask; -static struct task_struct *parent; - -static void show_processes(void) -{ - mytask = current; - parent = mytask->parent; - - while (mytask->pid != parent->pid) { - printk(KERN_ALERT "current: %s (%d) - parent: %s (%d)\n", - mytask->comm, mytask->pid, parent->comm, parent->pid); - mytask = mytask->parent; - parent = mytask->parent; - } -} - -static int __init init(void) -{ - int err; - - printk(KERN_ALERT "%s loaded by %s (%d)\n", - modname, mytask->comm, mytask->pid); - - if ((err = alloc_chrdev_region(&mydev, 0, 1, devname))) - printk(KERN_ALERT "ERROR in alloc_chrdev_region: %d\n", err); - - return 0; -} - -static void __exit exit(void) -{ - unregister_chrdev_region(mydev, 1); - printk(KERN_ALERT "%s unloaded\n", modname); -} - -module_init(init); -module_exit(exit); - -MODULE_LICENSE("GPL"); diff --git a/pidevice/Makefile b/pidevice/Makefile new file mode 100644 index 0000000..55bcfe5 --- /dev/null +++ b/pidevice/Makefile @@ -0,0 +1,7 @@ +obj-m += pidevice.o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean diff --git a/pidevice/loader.sh b/pidevice/loader.sh new file mode 100755 index 0000000..7887e1b --- /dev/null +++ b/pidevice/loader.sh @@ -0,0 +1,24 @@ +#!/bin/bash +module="pidevice" +device="pidevice" +mode="644" + +insmod ./$module.ko $* || exit -1 + +rm -f /dev/${device}? + +major=$(cat /proc/devices | grep $module | cut -f 1 -d" ") + +#for i in {0..3}; do +# mknod /dev/${device}$i c $major $i +#done + +mknod /dev/${device} c $major 0 + +group="staff" +grep -q '^staff:' /etc/group || group="wheel" + +#chgrp $group /dev/${device}[0-3] +#chmod $mode /dev/${device}[0-3] +chgrp $group /dev/${device} +chmod $mode /dev/${device} diff --git a/pidevice/open.py b/pidevice/open.py new file mode 100755 index 0000000..d75f876 --- /dev/null +++ b/pidevice/open.py @@ -0,0 +1,4 @@ +#!/usr/bin/python2 + +f = open("/dev/pidevice", "r+"); +f.close() diff --git a/pidevice/pidevice.c b/pidevice/pidevice.c new file mode 100644 index 0000000..3c5d3b4 --- /dev/null +++ b/pidevice/pidevice.c @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static char devname[] = "pidevice"; +static char modname[] = "pidevice.ko"; +static dev_t mydev = 0; +static struct cdev cdev; +static struct task_struct *mytask; +static struct task_struct *parent; + +static int count = 1; + +int pidev_open(struct inode *i, struct file *filp) +{ + count = 1; + printk(KERN_ALERT "Device (%d-%d) opened\n", + imajor(i), iminor(i)); + printk(KERN_ALERT "Mode: %c%c\n", filp->f_mode & FMODE_READ ? 'R' : '-', + filp->f_mode & FMODE_WRITE ? 'W' : '-'); + return 0; +} + +ssize_t pidev_read(struct file *filp, char __user *buf, + size_t len, loff_t *off) +{ + if (count--) { + copy_to_user(buf, modname, 12); + return 12; + } else { + return 0; + } +} + +ssize_t pidev_write(struct file *filp, const char __user *buf, + size_t len, loff_t *off) +{ + char localbuf[40]; + copy_from_user(localbuf, buf, 40); + localbuf[len] = '\0'; + + printk(KERN_ALERT "Recibido: %s", localbuf); + return -EPERM; +} + +struct file_operations fops = { + .owner = THIS_MODULE, + .open = pidev_open, + .read = pidev_read, + .write = pidev_write, +}; + +static void show_processes(void) +{ + mytask = current; + parent = mytask->parent; + + while (mytask->pid != parent->pid) { + printk(KERN_ALERT "current: %s (%d) - parent: %s (%d)\n", + mytask->comm, mytask->pid, parent->comm, parent->pid); + mytask = mytask->parent; + parent = mytask->parent; + } +} + +static int __init init(void) +{ + int err; + + printk(KERN_ALERT "%s loaded by %s (%d)\n", + modname, current->comm, current->pid); + + if ((err = alloc_chrdev_region(&mydev, 0, 1, devname))) + printk(KERN_ALERT "ERROR in alloc_chrdev_region: %d\n", err); + else + printk(KERN_ALERT "%s successfully registered with %d %d numbers\n", + devname, MAJOR(mydev), MINOR(mydev)); + + cdev_init(&cdev, &fops); + cdev.owner = THIS_MODULE; + if ((err = cdev_add(&cdev, mydev, 1))) + printk(KERN_ALERT "ERROR in cdev_add: %d\n", err); + + return 0; +} + +static void __exit exit(void) +{ + unregister_chrdev_region(mydev, 1); + cdev_del(&cdev); + printk(KERN_ALERT "%s unloaded\n", modname); +} + +module_init(init); +module_exit(exit); + +MODULE_LICENSE("GPL"); diff --git a/pidevice/unloader.sh b/pidevice/unloader.sh new file mode 100755 index 0000000..ffa2c38 --- /dev/null +++ b/pidevice/unloader.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +module="pidevice" +device="pidevice" + +#rm -f /dev/$device[0-3] +rm -f /dev/$device +rmmod $module -- cgit v1.2.3