summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2011-08-21 18:45:47 +0200
committerGuillermo Ramos2011-08-21 18:45:47 +0200
commitc34d4c01c26ee6932568834bb2fd3a53019103aa (patch)
treea08be6f08360f62ede6cb66c884da6ac3f5e9d66
parentc985cada55b6dfc26d96b13d2697e0fedc8b471e (diff)
downloadlkm-c34d4c01c26ee6932568834bb2fd3a53019103aa.tar.gz
AƱadido sleep, modificaciones generales
-rw-r--r--handlers/chardev.c3
-rw-r--r--handlers/procfs1.c3
-rw-r--r--killuid/killuid.c9
-rw-r--r--lists/lists.c2
-rw-r--r--pid/pid.c1
-rwxr-xr-xpidevice/loader.sh2
-rw-r--r--pidevice/pidevice.c55
-rwxr-xr-xpidevice/unloader.sh2
-rw-r--r--sleep/Makefile7
-rwxr-xr-xsleep/loader.sh24
-rwxr-xr-xsleep/open.py4
-rw-r--r--sleep/sleep.c90
-rwxr-xr-xsleep/unloader.sh8
-rw-r--r--syscall/syscall.c1
14 files changed, 174 insertions, 37 deletions
diff --git a/handlers/chardev.c b/handlers/chardev.c
index 1c1f923..b16fd5f 100644
--- a/handlers/chardev.c
+++ b/handlers/chardev.c
@@ -120,3 +120,6 @@ static ssize_t device_write(struct file* filp, const char* buff, size_t len, lof
msg_ptr = chardev_buffer;
return bytes;
}
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Guillermo Ramos");
diff --git a/handlers/procfs1.c b/handlers/procfs1.c
index 9d96319..e43aa20 100644
--- a/handlers/procfs1.c
+++ b/handlers/procfs1.c
@@ -48,3 +48,6 @@ void cleanup_module(void)
//remove_proc_entry(procfs_name, &proc_root);
printk(KERN_INFO "/proc/%s removed\n", procfs_name);
}
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Guillermo Ramos");
diff --git a/killuid/killuid.c b/killuid/killuid.c
index 1e854e7..07d5c9a 100644
--- a/killuid/killuid.c
+++ b/killuid/killuid.c
@@ -63,7 +63,7 @@ struct file_operations fops = {
.write = pidev_write,
};
-static int __init init(void)
+static int __init killuid_init(void)
{
int err;
@@ -84,14 +84,15 @@ static int __init init(void)
return 0;
}
-static void __exit exit(void)
+static void __exit killuid_exit(void)
{
unregister_chrdev_region(mydev, 1);
cdev_del(&cdev);
printk(KERN_ALERT "%s unloaded\n", modname);
}
-module_init(init);
-module_exit(exit);
+module_init(killuid_init);
+module_exit(killuid_exit);
MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Guillermo Ramos");
diff --git a/lists/lists.c b/lists/lists.c
index 2eee70c..23d6e08 100644
--- a/lists/lists.c
+++ b/lists/lists.c
@@ -37,5 +37,5 @@ void cleanup_module(void)
{
}
-MODULE_AUTHOR("Guillermo Ramos");
MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Guillermo Ramos");
diff --git a/pid/pid.c b/pid/pid.c
index b930fe6..367eb5e 100644
--- a/pid/pid.c
+++ b/pid/pid.c
@@ -32,3 +32,4 @@ module_init(init);
module_exit(exit);
MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Guillermo Ramos");
diff --git a/pidevice/loader.sh b/pidevice/loader.sh
index 7887e1b..5862524 100755
--- a/pidevice/loader.sh
+++ b/pidevice/loader.sh
@@ -1,6 +1,6 @@
#!/bin/bash
module="pidevice"
-device="pidevice"
+device=$module
mode="644"
insmod ./$module.ko $* || exit -1
diff --git a/pidevice/pidevice.c b/pidevice/pidevice.c
index 3c5d3b4..d7edc4e 100644
--- a/pidevice/pidevice.c
+++ b/pidevice/pidevice.c
@@ -13,17 +13,15 @@ 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",
+ printk(KERN_ALERT "pidev_open: device (%d-%d) opened\n",
imajor(i), iminor(i));
- printk(KERN_ALERT "Mode: %c%c\n", filp->f_mode & FMODE_READ ? 'R' : '-',
+ printk(KERN_ALERT "pidev_open: mode %c%c\n", filp->f_mode & FMODE_READ ? 'R' : '-',
filp->f_mode & FMODE_WRITE ? 'W' : '-');
return 0;
}
@@ -32,7 +30,8 @@ ssize_t pidev_read(struct file *filp, char __user *buf,
size_t len, loff_t *off)
{
if (count--) {
- copy_to_user(buf, modname, 12);
+ if (copy_to_user(buf, modname, 12))
+ printk(KERN_ALERT "pidev_read: error reading from (__user) %p\n", buf);
return 12;
} else {
return 0;
@@ -43,62 +42,58 @@ 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);
+ if (copy_from_user(localbuf, buf, 40))
+ printk(KERN_ALERT "pidev_write: error writing in (__user) %p\n", buf);
localbuf[len] = '\0';
- printk(KERN_ALERT "Recibido: %s", localbuf);
+ printk(KERN_ALERT "pidev_write: received: %s", localbuf);
return -EPERM;
}
+long pidev_ioctl(struct file *filp, unsigned msg, unsigned long param)
+{
+ printk(KERN_ALERT "pidev_ioctl: received msg %d / param %ld\n", msg, param);
+ return 0;
+}
+
struct file_operations fops = {
.owner = THIS_MODULE,
.open = pidev_open,
.read = pidev_read,
.write = pidev_write,
+ .unlocked_ioctl = pidev_ioctl,
};
-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)
+static int __init pidev_init(void)
{
int err;
- printk(KERN_ALERT "%s loaded by %s (%d)\n",
+ printk(KERN_ALERT "pidev_init: %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);
+ printk(KERN_ALERT "pidev_init: error %d in alloc_chrdev_region\n", err);
else
- printk(KERN_ALERT "%s successfully registered with %d %d numbers\n",
- devname, MAJOR(mydev), MINOR(mydev));
+ printk(KERN_ALERT "pidev_init: %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);
+ printk(KERN_ALERT "pidev_init: error %d in cdev_add\n", err);
return 0;
}
-static void __exit exit(void)
+static void __exit pidev_exit(void)
{
unregister_chrdev_region(mydev, 1);
cdev_del(&cdev);
- printk(KERN_ALERT "%s unloaded\n", modname);
+ printk(KERN_ALERT "pidev_exit: %s unloaded\n", modname);
}
-module_init(init);
-module_exit(exit);
+module_init(pidev_init);
+module_exit(pidev_exit);
MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Guillermo Ramos");
diff --git a/pidevice/unloader.sh b/pidevice/unloader.sh
index ffa2c38..11c687c 100755
--- a/pidevice/unloader.sh
+++ b/pidevice/unloader.sh
@@ -1,7 +1,7 @@
#!/bin/bash
module="pidevice"
-device="pidevice"
+device=$module
#rm -f /dev/$device[0-3]
rm -f /dev/$device
diff --git a/sleep/Makefile b/sleep/Makefile
new file mode 100644
index 0000000..747de82
--- /dev/null
+++ b/sleep/Makefile
@@ -0,0 +1,7 @@
+obj-m += sleep.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/sleep/loader.sh b/sleep/loader.sh
new file mode 100755
index 0000000..e6b5f3b
--- /dev/null
+++ b/sleep/loader.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+module="sleep"
+device=$module
+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/sleep/open.py b/sleep/open.py
new file mode 100755
index 0000000..73f45a1
--- /dev/null
+++ b/sleep/open.py
@@ -0,0 +1,4 @@
+#!/usr/bin/python2
+
+f = open("/dev/sleep", "r+");
+f.close()
diff --git a/sleep/sleep.c b/sleep/sleep.c
new file mode 100644
index 0000000..3cc06e8
--- /dev/null
+++ b/sleep/sleep.c
@@ -0,0 +1,90 @@
+#include <asm/uaccess.h>
+#include <linux/cdev.h>
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/kdev_t.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/sched.h>
+#include <linux/types.h>
+#include <linux/errno.h>
+#include <linux/wait.h>
+
+static char devname[] = "sleep";
+static char modname[] = "sleep.ko";
+static dev_t mydev = 0;
+static struct cdev cdev;
+static int count = 0;
+
+DECLARE_WAIT_QUEUE_HEAD(myqueue);
+
+int sleep_open(struct inode *i, struct file *filp)
+{
+ printk(KERN_ALERT "sleep_open: device (%d-%d) opened\n",
+ imajor(i), iminor(i));
+ printk(KERN_ALERT "sleep_open: mode %c%c\n", filp->f_mode & FMODE_READ ?
+ 'R' : '-', filp->f_mode & FMODE_WRITE ? 'W' : '-');
+ return 0;
+}
+
+ssize_t sleep_read(struct file *filp, char __user *buf, size_t len,
+ loff_t *offset)
+{
+ printk(KERN_ALERT "sleep_read: putting process %d (%s) to sleep...\n",
+ current->pid, current->comm);
+ count = 0;
+ if (wait_event_interruptible(myqueue, count))
+ return -ERESTARTSYS;
+ return 0;
+}
+
+ssize_t sleep_write(struct file *filp, const char __user *buf, size_t len,
+ loff_t *offset)
+{
+ printk(KERN_ALERT "sleep_write: process %d (%s) brings salvation :D\n",
+ current->pid, current->comm);
+ count = 1;
+ wake_up_interruptible(&myqueue);
+ return -EPERM;
+}
+
+struct file_operations fops = {
+ .owner = THIS_MODULE,
+ .open = sleep_open,
+ .read = sleep_read,
+ .write = sleep_write,
+};
+
+static int __init sleep_init(void)
+{
+ int err;
+
+ printk(KERN_ALERT "sleep_init: %s loaded by %s (%d)\n",
+ modname, current->comm, current->pid);
+
+ if ((err = alloc_chrdev_region(&mydev, 0, 1, devname)))
+ printk(KERN_ALERT "sleep_init: error %d in alloc_chrdev_region\n", err);
+ else
+ printk(KERN_ALERT "sleep_init: %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 "sleep_init: error %d in cdev_add\n", err);
+
+ return 0;
+}
+
+static void __exit sleep_exit(void)
+{
+ unregister_chrdev_region(mydev, 1);
+ cdev_del(&cdev);
+ printk(KERN_ALERT "sleep_exit: %s unloaded\n", modname);
+}
+
+module_init(sleep_init);
+module_exit(sleep_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Guillermo Ramos");
diff --git a/sleep/unloader.sh b/sleep/unloader.sh
new file mode 100755
index 0000000..1a7346c
--- /dev/null
+++ b/sleep/unloader.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+module="sleep"
+device=$module
+
+#rm -f /dev/$device[0-3]
+rm -f /dev/$device
+rmmod $module
diff --git a/syscall/syscall.c b/syscall/syscall.c
index 8decf5c..597067d 100644
--- a/syscall/syscall.c
+++ b/syscall/syscall.c
@@ -75,3 +75,4 @@ void cleanup_module(void)
}
MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Guillermo Ramos");