Skip to content

Commit

Permalink
add: procfs で実装。 read(2) に対応しています。
Browse files Browse the repository at this point in the history
  • Loading branch information
hiboma committed May 24, 2013
1 parent 4d4abec commit 828b19a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
10 changes: 10 additions & 0 deletions linux-kernel-procfs-module/Makefile
@@ -0,0 +1,10 @@
obj-m := sekigae.o

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

all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

clean:
rm -rf *.o *.ko *.mod.c *~
25 changes: 25 additions & 0 deletions linux-kernel-procfs-module/sample.md
@@ -0,0 +1,25 @@
## Build

```
$ make
$ sudo insmod sekigae.ko
```

## Usage

```
$ ls -hal /proc/sekigae
-rw-r--r-- 1 root root 0 5月 24 18:51 2013 /proc/sekigae
$ cat /proc/sekigae
kitak
keoken
gussan
okkun
$ cat /proc/sekigae
kitak
keoken
okkun
gussan
```
78 changes: 78 additions & 0 deletions linux-kernel-procfs-module/sekigae.c
@@ -0,0 +1,78 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/random.h>

MODULE_DESCRIPTION("Sekigae Module");
MODULE_AUTHOR("Sekigae");
MODULE_LICENSE("SUSHI");

#define PROCFS_ENTRY "sekigae"

static struct proc_dir_entry *sekigae_proc_dir_entry = NULL;

static int
procfile_read(char *buffer,
char **buffer_location,
off_t offset, int buffer_length, int *eof, void *data)
{
int ret = 0;
int length = 0;
int i;

const char *newcomers[] = {
"okkun",
"keoken",
"kitak",
"gussan",
};

unsigned int members_count = sizeof(newcomers) / sizeof(char *);
for (i = 0; i < members_count; i++) {
const char *swapped;
unsigned int r;
get_random_bytes(&r, sizeof r);
r = r % members_count;
swapped = newcomers[r];
newcomers[r] = newcomers[i];
newcomers[i] = swapped;
}

for (i = 0; i < members_count; i++) {
length += sprintf(buffer + length, "%s\n", newcomers[i]);
}

if (length > buffer_length) {
ret = -ENOMEM;
} else {
ret = length;
}

return ret;
}

static int sekigae_init_module(void)
{
sekigae_proc_dir_entry = create_proc_entry(PROCFS_ENTRY, 0644, NULL);
if( sekigae_proc_dir_entry == NULL ) {
remove_proc_entry(PROCFS_ENTRY, NULL);
printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
PROCFS_ENTRY);
return -ENOMEM;
}

sekigae_proc_dir_entry->read_proc = procfile_read;
sekigae_proc_dir_entry->uid = 0;
sekigae_proc_dir_entry->gid = 0;

return 0;
}

static void sekigae_cleanup_module(void)
{
remove_proc_entry(PROCFS_ENTRY, NULL);
printk("sekigae module is unloaded.\n");
}

module_init(sekigae_init_module);
module_exit(sekigae_cleanup_module);

0 comments on commit 828b19a

Please sign in to comment.