struct proc_dir_entry * my_proc_entry, create_proc_entry()를 사용하면, /proc에 넣을 수 있다.
proc_dir_entry에 read_proc, write_proc 함수 포인터 타입을 지정할 수 있다.


소스


#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>

#include <linux/proc_fs.h>

int my_proc_read(char *page, char **start, off_t off,int count,int *eof, void *data_unused)
{
    int len;

    len = sprintf(page, "Hi~ I'm my_proc!!!\n");
    return len;
}

static int __init my_proc_init(void)
{
    struct proc_dir_entry * my_proc_entry;

    printk("MY_PROC Module Loading!!\n");

    if((my_proc_entry = create_proc_entry("my_proc", S_IRUGO , NULL)) == NULL)
        return -EACCES;

    my_proc_entry->read_proc = my_proc_read;

    return 0;
}

static void __exit my_proc_exit(void)
{
    printk("MY_PROC Module UnLoading!!\n");
    remove_proc_entry("my_proc", NULL);
}

module_init(my_proc_init);
module_exit(my_proc_exit);


실행


[root@Xhyper255 /root]$ls /proc

1    4   9        execdomains  kcore    mmc         slabinfo  version

11   43  bus      fb           kmsg     modules     stat

12   44  cmdline  filesystems  ksyms    mounts      swaps

2    5   cpu      fs           loadavg  mtd         sys

21   55  cpuinfo  interrupts   locks    net         sysvipc

223  6   devices  iomem        meminfo  partitions  tty

34   7   driver   ioports      misc     self        uptime

[root@Xhyper255 /root]$insmod my_proc.o

MY_PROC Module Loading!!

[root@Xhyper255 /root]$ls /proc

1    4   9        execdomains  kcore    mmc         self      uptime

11   43  bus      fb           kmsg     modules     slabinfo  version

12   44  cmdline  filesystems  ksyms    mounts      stat

2    5   cpu      fs           loadavg  mtd         swaps

21   55  cpuinfo  interrupts   locks    my_proc     sys

225  6   devices  iomem        meminfo  net         sysvipc

34   7   driver   ioports      misc     partitions  tty


[root@Xhyper255 /root]$cat /proc/my_proc

Hi~ I'm my_proc!!!






다른 예제


#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <asm/uaccess.h> // copy_from_user
#include <linux/proc_fs.h>

struct proc_dir_entry * time_dir, *my_dir;

    
int my_proc_read(char *page, char **start, off_t off,int count,int *eof, void *data_unused) {
    int len1, len2;
    len1 = sprintf(page, "Hi~ read!!!\n");
    len2 = sprintf(page + len1, "Hello!!!\n");
    return len1 + len2;
}


int my_proc_write(struct file *file, const char *buffer, unsigned long count, void *data) 
{
  char input;
copy_from_user(&input, buffer, count);
    printk("User data : %c\n", input);
    return count;

}


static int __init my_proc_init(void)
{

struct proc_dir_entry *my_proc_entry;
    printk("my_proc loading!!\n");

time_dir = proc_mkdir("time", NULL); // directory
    if((my_proc_entry = create_proc_entry("my", 0644 , time_dir)) == NULL) {//file
        return -EACCES;
    }

    my_proc_entry->read_proc = my_proc_read;
my_proc_entry->write_proc = my_proc_write;

    return 0;
}

static void __exit my_proc_exit(void)
{
    printk("MY_PROC Module UnLoading!!\n");
    remove_proc_entry("my", time_dir); // file
    remove_proc_entry("time", NULL); // directory 
}

module_init(my_proc_init);
module_exit(my_proc_exit);


실행


[root@Xhyper255 /root]$insmod my_proc.o

my_proc loading!!

[root@Xhyper255 /root]$echo 1 > /proc/time/my

User data : 1

[root@Xhyper255 /root]$cat /proc/time/my

Hi~ read!!!

Hello!!!





* 주의점
 

int my_proc_read(char *page, char **start, off_t off,int count,int *eof, void *data_unused) {
    int len;
    len = sprintf(page, "Hi~ read!!!\n");     
    len = sprintf(page, "Hello!!!\n");
    return len;
}

이렇게 하면 override되기 때문에 문제가 된다.

int my_proc_read(char *page, char **start, off_t off,int count,int *eof, void *data_unused) {
    int len1, len2;
    len1 = sprintf(page, "Hi~ read!!!\n");
    len2 = sprintf(page + len1, "Hello!!!\n");
    
    return len1 + len2;
}

이렇게 고쳐야 한다.

Posted by '김용환'
,