2015-04-17 48 views
0

我知道这个问题已经被问过更多一次了。但真的没有一个好的答案。由于我已经在很长一段时间使用了这个答案,所以这是我第一次找不到解决方案。 这里我的代码完美地适用于:在已有的subdir内核3.11或更高版本下创建Proc fs目录和条目?

首先在/ proc下创建一个dir,然后在第二个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。

但是,如果设置了NULL的位置,需要/ proc/driver。我试过所有的东西。什么都没有

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

static char *dirname="mydriver"; 

就在上面的代码替换

static char *dirname="driver/mydriver"; 
+0

我找到了解决办法,我在这里提到它,因为大量的下岗失业人员在那里寻找它。只需在subdir/proc/driver下创建我的/ proc目录和文件即可。 –

+0

很多人在哪里寻找这个答案。这对我来说很完美。 –

回答

1

我尝试了编译内核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 
相关问题