From 7eff509839af1997c475d124f6bc4154283007ba Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Wed, 19 May 2010 15:22:57 -0400 Subject: [PATCH 1/1] Uses seq_file to print our best greeting. --- Makefile | 13 ++++++++ helloproc.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 Makefile create mode 100644 helloproc.c diff --git a/Makefile b/Makefile new file mode 100644 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 index 0000000..4fc799b --- /dev/null +++ b/helloproc.c @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2010 Thadeu Lima de Souza Cascardo + * + * 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 +#include +#include + +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); -- 2.20.1