2015-04-17 47 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 
相關問題