在现有的子目录内核3.11或更高版本下创建Proc fs目录和条目?

时间:2015-04-17 13:22:50

标签: linux proc createfile

我知道这个问题曾被问过一次。但真的没有一个好的答案。在这种情况下,我已经在很长一段时间内使用了这个答案,这是我第一次找不到解决方案。 这里我的代码非常适合:

首先在/ proc下创建一个目录,然后在第二个dir然后创建一个条目。 该条目为空但可写。效果很好。

一些额外的信息ubuntu 14.04内核更新3.13.0-49-generic。 x86_64的

这里的代码。

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/string.h>

static int len,temp;

static char *msg;
static char *dirname="mydriver";
static char *dirname2="settings";
struct proc_dir_entry *parent;
struct proc_dir_entry *parent2;
static ssize_t read_proc(struct file *filp,char *buf,size_t count,loff_t *offp )
{
    if(count>temp){count=temp;}
    temp=temp-count;
    copy_to_user(buf,msg, count);
    if(count==0){temp=len;}

    return count;
}

static ssize_t write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp)
{
    copy_from_user(msg,buf,count);
    len=count;
    temp=len;

    return count;
}

struct file_operations proc_fops = {
    read: read_proc,
    write: write_proc
};

static void create_new_proc_entry(void)
{
    parent = proc_mkdir(dirname, NULL);
    parent2 = proc_mkdir(dirname2,parent);
    proc_create("private_setting",0644,parent2,&proc_fops);
    msg=kmalloc(GFP_KERNEL,10*sizeof(char));
}


static int proc_init (void)
{
 create_new_proc_entry();
 return 0;
}

static void proc_cleanup(void)
{
    remove_proc_entry("private_setting",parent2);
    proc_remove(parent2);
    proc_remove(parent);
}

MODULE_LICENSE("GPL"); 
module_init(proc_init);
module_exit(proc_cleanup);

问题是如何在已经存在的子目录下创建目录和条目。比如/ proc / driver。

我知道第一个父项是用NULL创建的,这意味着/ proc。

但是要设置off NULL以使/ proc / driver。我试过这一切。什么都行不通。

我找到了在/ proc / driver下创建目录和条目的解决方案。

只需替换上面的代码:

static char *dirname="mydriver";

以下行:

static char *dirname="driver/mydriver";

1 个答案:

答案 0 :(得分:2)

我试着在内核3.2上编译这段代码。不幸的是它没有编译。我发现了很小的变化,因此它适用于内核3.2。 它的好处是,通过这个小改变它也适用于3.13。 换句话说,代码编译并且从内核3.2到3.13(已测试)完美地工作。它也适用于最后的Linux内核版本。

这里修改了完整的代码。

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/string.h>

static int len,temp;

static char *msg;
static char *dirname="driver/mydriver";
static char *dirname2="settings";
struct proc_dir_entry *subdirparent;
struct proc_dir_entry *parent;
struct proc_dir_entry *parent2;
static ssize_t read_proc(struct file *filp,char *buf,size_t count,loff_t *offp )
{
    if(count>temp){count=temp;}
    temp=temp-count;
    copy_to_user(buf,msg, count);
    if(count==0){temp=len;}

    return count;
}

static ssize_t write_proc(struct file *filp,const char *buf,size_t count,loff_t *offp)
{
    copy_from_user(msg,buf,count);
    len=count;
    temp=len;

    return count;
}

struct file_operations proc_fops = {
    read: read_proc,
    write: write_proc
};

static void create_new_proc_entry(void)
{
    parent = proc_mkdir(dirname, NULL);
    parent2 = proc_mkdir(dirname2,parent);
    proc_create("private_setting",0644,parent2,&proc_fops);
    msg=kmalloc(GFP_KERNEL,10*sizeof(char));
}


static int proc_init (void)
{
 create_new_proc_entry();
 return 0;
}

static void proc_cleanup(void)
{
    remove_proc_entry("private_setting",parent2);
    remove_proc_entry(dirname2,parent);
    remove_proc_entry(dirname,NULL);
}

MODULE_LICENSE("GPL"); 
module_init(proc_init);
module_exit(proc_cleanup);

这是一个编译代码的Makefile示例。

obj-m := proc_rw_map2.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build

PWD := $(shell pwd)

default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
相关问题