Pregunta

I'm trying to make a module of kernel that interacts with two /proc entries. I need to use them in separated ways, and i do not know how to assign a entry way to each one. I'll explain myself. I have two entries, i need to access one by using

echo "" > /proc/modconfig
cat /proc/modconfig

and the other one like a file, using Open() close() ... the first one is ok, i have used in the initial function:

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

But i do not know how to use this. ¿Could you help me?

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

This is the complete source (This is in an initial phase)

#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);
¿Fue útil?

Solución

This is the code i used and it worked:

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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top