Skip to content

Instantly share code, notes, and snippets.

@umedaikiti
Created August 30, 2013 02:17
Show Gist options
  • Save umedaikiti/6385643 to your computer and use it in GitHub Desktop.
Save umedaikiti/6385643 to your computer and use it in GitHub Desktop.
procインターフェース
obj-m = proctest.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
/*
* 基本的に下のURLのパクリ
* http://d.hatena.ne.jp/masami256/20100315/1268662409
* create_proc_entry -> proc_create が主な変更
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <asm/uaccess.h>
MODULE_DESCRIPTION("proc file system test");
MODULE_LICENSE("GPL");
static const char const module_name[] = "proc_test";
static int proc_test_read(struct file *file, char __user *buf, size_t size, loff_t *ppos);
static int proc_test_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos);
static struct file_operations proctest_fop = {
.read = proc_test_read,
.write = proc_test_write,
};
static struct proc_dir_entry *pdir_entry;
static int proc_test_init(void)
{
printk(KERN_INFO "%s\n", __FUNCTION__);
// pdir_entry = create_proc_entry(module_name, 0644, NULL);
// kernel 3.10では使えない?
pdir_entry = proc_create(module_name, 0644, NULL, &proctest_fop);
if (!pdir_entry) {
remove_proc_entry(module_name, NULL);
return -EFAULT;
}
// pdir_entry->mode = S_IRUGO;
// pdir_entry->gid = 0;
// pdir_entry->uid = 0;
// pdir_entry->read_proc = (read_proc_t *) proc_test_read;
// pdir_entry->write_proc = (write_proc_t *) proc_test_write;
return 0;
}
static void proc_test_cleanup(void)
{
printk(KERN_INFO "%s\n", __FUNCTION__);
remove_proc_entry(module_name, NULL);
}
static char proc_test_buf[256] = "Hello, World";
static int proc_test_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if (*ppos >= 256)
return -EINVAL;
if (*ppos + size > 256)
size = 256 - *ppos;
if (!buf)
return -EINVAL;
size = snprintf(buf, size, "%s", proc_test_buf + *ppos);
*ppos += size;
return size;
}
static int proc_test_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
{
if (*ppos >= 256)
return -EINVAL;
if (*ppos + size > 256)
size = 256 - *ppos;
if (!buf)
return -EINVAL;
memset(proc_test_buf, 0x0, sizeof(proc_test_buf));
if (copy_from_user(proc_test_buf + *ppos, buf, size))
return -EFAULT;
return size;
}
module_init(proc_test_init);
module_exit(proc_test_cleanup);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment