/dev/dog

After reading a post on edd's blog about a cat and that it wasn't a device I though ?Why not?
14:12 <> the cat is not a device driver ffs
14:13 < #4> cat /dev/cat ;)
So her you go. Now you can have a device called dog in Linux and feed it with
echo "Food" > /dev/dog
cat /dev/dog

If you want more features just tell me :)

 1: /*
2: * The Dog kernel module
3: * Creates a /dev/dog
4: *
5: * Hoffmann Geerd-Dietger
6: * Thx to:
7: * Valerie Henson
8: *
9: */

10: #define BUFFER 80
11:
12: #include <linux/fs.h>
13: #include <linux/init.h>
14: #include <linux/miscdevice.h>
15: #include <linux/module.h>
16: #include <asm/uaccess.h>
17:
18: MODULE_LICENSE("DUAL GPL/BSD");
19: MODULE_AUTHOR("Hoffmann Geerd-Dietger ");
20: MODULE_DESCRIPTION("A little dog living in /dev");
21: MODULE_VERSION("0.1");
22:
23:
24: static char hello_str[BUFFER]="Hello Master, just echo to me and then cat\n";
25:
26: static ssize_t dog_write(struct file *filp, const char __user *buff,
27: size_t count, loff_t *offp)
28: {
29: /* We don't care just chop it off */
30: if (count >= BUFFER)
31: count = BUFFER;
32:
33: if (copy_from_user(hello_str,buff, count))
34: return -EINVAL;
35:
36: return count;
37: }
38:
39:
40: static ssize_t dog_read(struct file * file, char * buf,
41: size_t count, loff_t *ppos)
42: {
43:
44: if (count < BUFFER)
45: return -EINVAL;
46:
47: if (*ppos != 0)
48: return 0;
49:
50: if (copy_to_user(buf, hello_str, BUFFER))
51: return -EINVAL;
52:
53: *ppos = BUFFER;
54:
55: return BUFFER;
56: }
57:
58:
59: static const struct file_operations dog_fops = {
60: .owner = THIS_MODULE,
61: .read = dog_read,
62: .write = dog_write,
63: };
64:
65: static struct miscdevice dog_dev = {MISC_DYNAMIC_MINOR,"dog",&dog_fops };
66:
67: static int __init
68: dog_init(void)
69: {
70: int ret;
71:
72: ret = misc_register(&dog_dev);
73: if (ret)
74: printk(KERN_ERR
75: "Unable to register \"Dog\" misc device\n");
76:
77: printk("dog: Woof Woof\n");
78:
79: return ret;
80: }
81:
82: module_init(dog_init);
83:
84: static void __exit
85: dog_exit(void)
86: {
87: misc_deregister(&dog_dev);
88: printk("dog: Grrrrrrrrrrrrrrrrr");
89: }
90:
91: module_exit(dog_exit);


Makefile:

obj-m := dog_dev.o

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:
$(MAKE) -C $(KDIR) M=$(PWD) modules


Of course you have to insmod the module

No comments: