2011-12-15 18 views
27

¿Alguien puede darme el ejemplo proc_create()?ejemplo de proc_create() para el módulo kernel

Anteriormente usaron create_proc_entry() en el kernel pero ahora están usando proc_create().

+1

por qué no sólo tiene que descargar las fuentes del núcleo actual y grep para proc_create? – Roland

Respuesta

30

Este ejemplo creará una entrada de proceso que habilita el acceso de lectura. Creo que puede habilitar otros tipos de acceso cambiando el argumento mode pasado a la función. No pasé un directorio principal porque no es necesario. La estructura file_operations es donde configura sus devoluciones de llamadas de lectura y escritura.

struct proc_dir_entry *proc_file_entry; 

static const struct file_operations proc_file_fops = { 
.owner = THIS_MODULE, 
.open = open_callback, 
.read = read_callback, 
}; 

int __init init_module(void){ 
    proc_file_entry = proc_create("proc_file_name", 0, NULL, &proc_file_fops); 
    if(proc_file_entry == NULL) 
    return -ENOMEM; 
    return 0; 
} 

Puede comprobar este ejemplo para más detalles: https://www.linux.com/learn/linux-training/37985-the-kernel-newbie-corner-kernel-debugging-using-proc-qsequenceq-files-part-1

+0

En cuanto a las fuentes del núcleo, debería hacer una observación. 'proc_file_fops' no debe estar localizado en una memoria de pila (variable local de función). Debe ser una variable global o estar ubicado en la memoria asignada con alguna función de memoria de pila. –

21

Aquí es un código 'hello_proc', que hace uso de la 'proc_create) (' nueva interfaz.

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

static int hello_proc_show(struct seq_file *m, void *v) { 
    seq_printf(m, "Hello proc!\n"); 
    return 0; 
} 

static int hello_proc_open(struct inode *inode, struct file *file) { 
    return single_open(file, hello_proc_show, NULL); 
} 

static const struct file_operations hello_proc_fops = { 
    .owner = THIS_MODULE, 
    .open = hello_proc_open, 
    .read = seq_read, 
    .llseek = seq_lseek, 
    .release = single_release, 
}; 

static int __init hello_proc_init(void) { 
    proc_create("hello_proc", 0, NULL, &hello_proc_fops); 
    return 0; 
} 

static void __exit hello_proc_exit(void) { 
    remove_proc_entry("hello_proc", NULL); 
} 

MODULE_LICENSE("GPL"); 
module_init(hello_proc_init); 
module_exit(hello_proc_exit); 

Este código se ha tomado de http://pointer-overloading.blogspot.in/2013/09/linux-creating-entry-in-proc-file.html

+1

Gracias por un enlace, realmente útil. Recomiendo leer a aquellos que quieran entender cómo funciona. –

+1

Este ejemplo con [repetición de prueba QEMU + Buildroot] (https://github.com/cirosantilli/linux-kernel-module-cheat/blob/4727fadcc8f6e0685f80dc88a2913995a8df01f3/kernel_module/procfs.c). –

Cuestiones relacionadas