2018年2月13日 星期二

Linux kernel - 建立唯讀屬性proc entry

#include <linux/module.h>
#include <linux/proc_fs.h>

MODULE_LICENSE("Dual BSD/GPL");

#define PROCNAME "sample"

static int sample_read_proc(char *buf, char **start, off_t offset,
    int count, int *eof, void *data)
{
 int len = 0;

#if 0
 printk(KERN_INFO "buf=%p, *start=%p, offset=%d, count=%d, *eof=%d, data=%p\n",
   buf, *start, (int)offset, count, *eof, data);
#endif

 len += sprintf(buf + len, "Hello world\n");

 if (len > PAGE_SIZE)
  return -ENOBUFS;

 return len;
}

static int sample_init(void)
{
 struct proc_dir_entry *entry;

 entry = create_proc_read_entry(PROCNAME, S_IRUGO | S_IWUGO, NULL,
    sample_read_proc, NULL);

 if (entry == NULL)
  printk(KERN_WARNING "proc: unable to create proc entry\n");

 printk(KERN_INFO "driver loaded\n");

 return 0;
}

static void sample_exit(void)
{
 remove_proc_entry(PROCNAME, NULL);

 printk(KERN_INFO "driver unloaded\n");
}

module_init(sample_init);
module_exit(sample_exit);

沒有留言:

張貼留言