2011-12-15 11 views

cevap

30

Bu örnek, okuma erişimi sağlayan bir proc girişi oluşturacaktır. Fonksiyona iletilen mode argümanını değiştirerek diğer erişim türlerini etkinleştirebilirsiniz. Bir üst dizini geçmedim çünkü gerek yok. file_operations yapısı, okuma ve yazma geri aramalarınızı kurduğunuz yerdir.

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; 
} 

daha fazla detay için bu örneği kontrol edebilirsiniz: https://www.linux.com/learn/linux-training/37985-the-kernel-newbie-corner-kernel-debugging-using-proc-qsequenceq-files-part-1 İşte

+0

Çekirdek kaynaklara baktığımda bir açıklama yapmalıyım. "proc_file_fops" yığın belleğinde (işlev yerel değişkeni) kapatılmamalıdır. Bir global değişken olmalı ya da bir yığın bellek işleviyle ayrılmış bellekte bulunmalıdır. –

21

yeni 'proc_create()' arabirim kullanan bir 'hello_proc' koddur. sadece mevcut çekirdek kaynağı ve grep proc_create indirmek neden

#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); 

Bu kod http://pointer-overloading.blogspot.in/2013/09/linux-creating-entry-in-proc-file.html

+1

Bir bağlantı için teşekkürler, gerçekten yararlı. Nasıl çalıştığını anlamak isteyenlere okumayı öneriyorum. –

+1

Bu örnek [QEMU + Buildroot test boilerplate] ile (https://github.com/cirosantilli/linux-kernel-module-cheat/blob/4727fadcc8f6e0685f80dc88a2913995a8df01f3/kernel_module/procfs.c). –