【问题标题】:Create Proc fs dir and entry under a already existing subdir kernel 3.11 or higher?在已经存在的子目录内核 3.11 或更高版本下创建 Proc fs 目录和条目?
【发布时间】:2015-04-17 13:22:49
【问题描述】:

我知道这个问题曾经被多次问过。但真的无处好回答。尽管我已经使用了很长时间的答案,但这是我第一次找不到解决方案。 这里我的代码非常适合:

首先在 /proc 下创建一个目录,然后是第二个目录,然后是一个条目。 该条目为空但可写。完美运行。

一些额外的信息 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";

【问题讨论】:

  • 我找到了解决方案,我在这里提到它,因为很多人都在寻找它。只是为了在 subdir /proc/driver 下创建我的 /proc 目录和文件。
  • 很多人都在寻找这个答案。这对我来说很完美。

标签: linux proc createfile


【解决方案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

【讨论】:

    猜你喜欢
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多