【问题标题】:Creating a proc entry under an existing entry在现有条目下创建 proc 条目
【发布时间】:2013-09-19 14:31:44
【问题描述】:

我的内核模块会在现有的 proc 条目下创建一个条目,例如 /proc/sys

所以对proc_create 的普通调用失败了。

然后我尝试通过检查proc_fs.h来查看是否有获取正确parent node的函数,但无济于事。

我现在该怎么办?

【问题讨论】:

    标签: linux kernel procfs


    【解决方案1】:

    希望这段代码能帮助你理解

    #include<linux/module.h>
    #include<linux/kernel.h>
    #include<linux/fs.h> /*this is the file structure, file open read close */
    #include<linux/cdev.h> /* this is for character device, makes cdev avilable*/
    #include<linux/semaphore.h> /* this is for the semaphore*/
    #include<linux/uaccess.h> /*this is for copy_user vice vers*/
    #include<linux/proc_fs.h>
    
    #define MAX_LEN 1024
    int read_info(char *page, char **start, off_t off,  int count, int *eof, void *data);
    int write_info(struct file *filp, const char __user *buffer, unsigned long length, void *data);
    int proc_init(void);
    void proc_clean(void);
    
    
    static struct proc_dir_entry *proc_entry;
    static char *info;
    static int write_index;
    static int read_index;
    
    int write_info(struct file *filp, const char __user *buffer, unsigned long length, void *data) {
        int capacity = (MAX_LEN - write_index) +1;
        if(length > capacity) {
            printk(KERN_INFO "NO sapce to write into peoc file \n");
            return -1;
        }
        if(copy_from_user(&info[write_index], buffer, length)) 
            return -2;
        write_index += length;
        printk(KERN_INFO " megharaj proc writing succesful, %d write \n", length);
        return length;
    }
    
    int read_info(char *page, char **start, off_t off, int count, int *eof, void *data) {
        int len;
        len = sprintf(page, "%s\n", &info[read_index]);
        read_index += len;
        printk(KERN_INFO " megharaj proc reading succesful, %d read \n", len);
        return len;
    }
    
    
    int proc_init(void) 
    {
        int ret = 0;
        info = (char *)vmalloc(MAX_LEN);
        memset(info, 0 , MAX_LEN);
    /*truct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
                                             struct proc_dir_entry *parent);*/
        proc_entry = create_proc_entry("megharaj_proc", 0666, NULL);
        if(proc_entry == NULL) {
            vfree(info);
            printk(KERN_INFO " megharaj proc not created \n");
            ret = -ENOMEM;
        }
        else {
            write_index = 0;
            read_index = 0;
            proc_entry->read_proc = read_info;
            proc_entry->write_proc = write_info;
            printk(KERN_INFO " megharaj proc created \n");
        }
        return ret;
    }
    
    void proc_clean(void) 
    {
        vfree(info);
        remove_proc_entry("megharaj_proc", NULL);
    
    }
    
    MODULE_LICENSE("GPL");   
    module_init(proc_init);
    module_exit(proc_clean);
    

    制作文件

    obj-m   := proc.o
    
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD       := $(shell pwd)
    
    all:
        $(MAKE) -C $(KERNELDIR) M=$(PWD)
    
    clean:
        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
    

    【讨论】:

    • 您正在/proc 下创建一个 proc 条目,而不是在现有的 proc 条目下。所以..你没有回答这个问题;-P
    • @warl0ck create_proc_entry("megharaj_proc", 0666, NULL); megharaj_proc 将是 proc 中的文件。如果 NULL 表示该条目是在 /proc 中创建的,如果您想在 /proc 的任何目录中创建 proc 条目,而不是 null 只需将 NULL 替换为 /proc/directory
    • @warl0ck struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, struct proc_dir_entry *parent);是语法。如果您有任何问题,请告诉我。让我知道您到底需要什么,这将非常有帮助:)
    • @warl0ck 也执行此代码并查看。做同样的事情,构建 make,然后 insmod 模块。你可以在 /proc/megharaj_proc 中找到一个文件。可以通过 cat /proc/megharaj_proc 读取,也可以通过 echo data 写入 > /proc/megharaj_proc.
    • 我认为您没有理解我的问题。请尝试在 /proc/sys 下创建一个条目。看看你能不能这样做..
    猜你喜欢
    • 1970-01-01
    • 2015-08-05
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 2010-10-14
    • 2012-09-09
    相关资源
    最近更新 更多