Uses seq_file to print our best greeting.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 19 May 2010 19:22:57 +0000 (15:22 -0400)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Wed, 19 May 2010 19:23:48 +0000 (15:23 -0400)
Makefile [new file with mode: 0644]
helloproc.c [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..3e65fae
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+ifneq ($(KERNELRELEASE),)
+       obj-m := helloproc.o
+else
+       KERNELDIR ?= /lib/modules/`uname -r`/build
+       PWD = `pwd`
+
+default:
+       make -C $(KERNELDIR) M=$(PWD) modules
+
+clean:
+       make -C $(KERNELDIR) M=$(PWD) clean
+
+endif
diff --git a/helloproc.c b/helloproc.c
new file mode 100644 (file)
index 0000000..4fc799b
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ *  Copyright (C) 2010  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <linux/module.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+
+MODULE_LICENSE("GPL");
+
+static char hello[] = "Hello, world!\n";
+
+static void *hp_start(struct seq_file *s, loff_t *pos)
+{
+       loff_t hp_pos = *pos;
+       *pos += 1;
+       if (hp_pos >= sizeof(hello))
+               return NULL;
+       return (void *) hello[hp_pos];
+}
+
+static void hp_stop(struct seq_file *s, void *v)
+{
+}
+
+static void *hp_next(struct seq_file *s, void *v, loff_t *pos)
+{
+       loff_t hp_pos;
+       hp_pos = *pos;
+       *pos += 1;
+       if (hp_pos >= sizeof(hello))
+               return NULL;
+       return (void *) hello[hp_pos];
+}
+
+static int hp_show(struct seq_file *s, void *v)
+{
+       seq_putc(s, (char) v);
+       return 0;
+}
+
+static const struct seq_operations hp_seq_ops = {
+       .start = hp_start,
+       .stop = hp_stop,
+       .next = hp_next,
+       .show = hp_show,
+};
+
+static int hp_open(struct inode *ino, struct file *fp)
+{
+       return seq_open(fp, &hp_seq_ops);
+}
+
+static const struct file_operations hp_fops = {
+       .owner = THIS_MODULE,
+       .open = hp_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = seq_release,
+};
+
+static __init int hp_init(void)
+{
+       struct proc_dir_entry *pde;
+       pde = proc_create("hello", 0666, NULL, &hp_fops);
+       if (!pde)
+               return -ENOMEM;
+       return 0;
+}
+
+static __exit void hp_exit(void)
+{
+       remove_proc_entry("hello", NULL);
+}
+
+module_init(hp_init);
+module_exit(hp_exit);