From 00d5bce9f24e2114316bd1165ebdad05644b7365 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Wed, 20 Jul 2011 21:46:28 +0200 Subject: AƱadido pid2 y ejemplos de oreilly --- .gitignore | 1 + examples.tar.gz | Bin 0 -> 71527 bytes pid2/Makefile | 7 +++++++ pid2/loader.sh | 23 +++++++++++++++++++++++ pid2/pid.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 examples.tar.gz create mode 100644 pid2/Makefile create mode 100644 pid2/loader.sh create mode 100644 pid2/pid.c diff --git a/.gitignore b/.gitignore index ec70db6..71007a0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ Module.symvers modules.order .tmp_versions +examples diff --git a/examples.tar.gz b/examples.tar.gz new file mode 100644 index 0000000..6b51100 Binary files /dev/null and b/examples.tar.gz differ diff --git a/pid2/Makefile b/pid2/Makefile new file mode 100644 index 0000000..b9e7cfd --- /dev/null +++ b/pid2/Makefile @@ -0,0 +1,7 @@ +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 new file mode 100644 index 0000000..579acff --- /dev/null +++ b/pid2/loader.sh @@ -0,0 +1,23 @@ +#!/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 new file mode 100644 index 0000000..6362f82 --- /dev/null +++ b/pid2/pid.c @@ -0,0 +1,50 @@ +#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"); -- cgit v1.2.3