两个open / proc条目处理方式不同

时间:2014-01-16 19:47:16

标签: linux file-io module kernel proc

我正在尝试创建一个与两个/ proc条目交互的内核模块。我需要以分开的方式使用它们,我不知道如何为每个方式分配一个输入方式。我会自己解释一下。我有两个条目,我需要使用

访问一个
echo "" > /proc/modconfig
cat /proc/modconfig

和另一个像文件一样,使用Open()close()......第一个是好的,我在初始函数中使用过:

    proc_entry->read_proc = procOpsRead;
    proc_entry->write_proc =procOpsWrite;

但我不知道如何使用它。你能帮帮我吗?

static const struct file_operations my_fops = {
    .open = modtimer_open,
    .read = modtimer_read,
    .release = modtimer_close,
};

这是完整的来源(这是初始阶段)

#define PROC_ENTRY "modtimer"
#define PROC_ENTRY_OPTS "modconfig"

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Pseudo-Random number generator for DSO");
MODULE_AUTHOR("kaostias");

static const struct file_operations my_fops = { //I want to use this just with the modtimer entry
    .open = modtimer_open, 
    .read = modtimer_read,
    .release = modtimer_close,
};


int install_module(void){
    int ret = 0;
    proc_entry = create_proc_entry(PROC_ENTRY,0777, NULL);
        if (proc_entry == NULL) {
            ret = -ENOMEM;
            printk(KERN_INFO "Error: /proc/%s entry couldn't be created\n",PROC_ENTRY);
    } else {
            printk(KERN_INFO "/proc/%s entry created.\n", PROC_ENTRY);
    }

    proc_entry_opts = create_proc_entry(PROC_ENTRY_OPTS,0777, NULL);
    if (proc_entry_opts == NULL) {
            ret = -ENOMEM;
            printk(KERN_INFO "Error: /proc/%s entry couldn't be created\n",PROC_ENTRY_OPS);
        remove_proc_entry(PROC_ENTRY, NULL);
            printk(KERN_INFO "/proc/%s entry was deleted.\n", PROC_ENTRY);      
    } else {
            proc_entry->read_proc = procOpsRead;
            proc_entry->write_proc =procOpsWrite;
            printk(KERN_INFO "/proc/%s entry created.\n", PROC_ENTRY_OPS);
    }

    return ret;
}
/*Al desinstalar el módulo*/
void uninstall_module(void){

    remove_proc_entry(PROC_ENTRY, NULL);
    printk(KERN_INFO "/proc/%s entry was deleted.\n", PROC_ENTRY);  
    remove_proc_entry(PROC_ENTRY_OPS, NULL);
    printk(KERN_INFO "/proc/%s entry was deleted.\n", PROC_ENTRY_OPS);  

}


/*Proc is written*/
int procOpsWrite( struct file *punterofichero, const char __user *bufferusuario,
                        unsigned long longitud, void *data){

}
/*proc is read*/
int procOpsRead( char *buffer, char **bufferlocation, off_t offset,
                   int buffer_lenghth, int *eof, void *data ){

}

int modtimer_open (struct inode *, struct file *){

}

int modtimer_release (struct inode*, struct file *){

}

ssize_t modtimer_read (struct file * char __user*, size_t nbits, loff_t * offset){

}

module_init(install_module);
module_exit(uninstall_module);

1 个答案:

答案 0 :(得分:0)

这是我使用的代码,它起作用了:

proc_entry = create_proc_entry(PROC_ENTRY,0777, NULL);
if (proc_entry == NULL) {
        ret = -ENOMEM;
} else {
    proc_entry->proc_fops=&my_fops;
}

proc_entry_opts = create_proc_entry(PROC_ENTRY_OPS,0777, NULL);
if (proc_entry_opts == NULL) {
        ret = -ENOMEM;
    remove_proc_entry(PROC_ENTRY, NULL);
} else {
        proc_entry_opts->read_proc = procOpsRead;
        proc_entry_opts->write_proc =procOpsWrite;
}