|
| 1 | +/* |
| 2 | +language: c |
| 3 | +env: |
| 4 | +author: d-grossman |
| 5 | +github: |
| 6 | +
|
| 7 | +
|
| 8 | +*/ |
| 9 | + |
| 10 | + |
| 11 | +/* |
| 12 | +make |
| 13 | +insmod hello_world_d-grossman |
| 14 | +ls /proc/hello_world |
| 15 | +cat /proc/hello_world |
| 16 | +rmmod hello_world_d-grossman |
| 17 | +
|
| 18 | +*/ |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +#include <linux/module.h> |
| 23 | +#include <linux/init.h> |
| 24 | +#include <linux/kernel.h> |
| 25 | + |
| 26 | +#include <linux/fs.h> |
| 27 | +#include <linux/proc_fs.h> |
| 28 | +#include <linux/seq_file.h> |
| 29 | + |
| 30 | +#define procfs_msg "Hello World\n" |
| 31 | +#define procfs_name "hello_world" |
| 32 | +#define procfs_parent NULL |
| 33 | +#define procfs_perms 0644 |
| 34 | + |
| 35 | +static struct proc_dir_entry *hello_world_file; |
| 36 | + |
| 37 | +static int |
| 38 | +hello_world_show (struct seq_file *m, void *v) |
| 39 | +{ |
| 40 | + seq_printf (m, "%s\n", procfs_msg); |
| 41 | + |
| 42 | + return 0; |
| 43 | +} |
| 44 | + |
| 45 | +static int |
| 46 | +hello_world_open (struct inode *inode, struct file *file) |
| 47 | +{ |
| 48 | + return single_open (file, hello_world_show, NULL); |
| 49 | +} |
| 50 | + |
| 51 | +static const struct file_operations hello_world_fops = { |
| 52 | + .owner = THIS_MODULE, |
| 53 | + .open = hello_world_open, |
| 54 | + .read = seq_read, |
| 55 | + .llseek = seq_lseek, |
| 56 | + .release = single_release, |
| 57 | +}; |
| 58 | + |
| 59 | +static int __init |
| 60 | +hello_world_init (void) |
| 61 | +{ |
| 62 | + hello_world_file = |
| 63 | + proc_create (procfs_name, procfs_perms, procfs_parent, &hello_world_fops); |
| 64 | + |
| 65 | + if (!hello_world_file) |
| 66 | + { |
| 67 | + return -ENOMEM; |
| 68 | + } |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +static void __exit |
| 74 | +hello_world_exit (void) |
| 75 | +{ |
| 76 | + remove_proc_entry ("hello_world", procfs_parent); |
| 77 | +} |
| 78 | + |
| 79 | +module_init (hello_world_init); |
| 80 | +module_exit (hello_world_exit); |
| 81 | + |
| 82 | +MODULE_LICENSE ("GPL"); |
| 83 | +MODULE_AUTHOR ("d-grossman"); |
| 84 | +MODULE_DESCRIPTION ("procfs hello world"); |
0 commit comments