2009-02-26 12 views
6

Tôi muốn tạo một tệp theo thư mục /proc/driver. Tôi muốn sử dụng macro như proc_root_driver (hoặc một cái gì đó khác được cung cấp) thay vì sử dụng "driver/MODULE_NAME" một cách rõ ràng. Tôi sử dụng create_proc_entry:Làm cách nào để tạo mục nhập proc trong/proc/driver?

struct proc_dir_entry *simpleproc_fops_entry; 
simpleproc_fops_entry = create_proc_entry(MODULE_NAME, 0400, NULL /* proc_root_dir */); 

Sau khi googling, tôi thấy gợi ý để sử dụng proc_root_driver, nhưng khi tôi sử dụng nó, tôi nhận được lỗi

proc_root_driver không khai báo trong hàm này

Và cũng , proc_root_driver không khả dụng trong linux/proc_fs.h.

Tôi đã cố gắng để khai báo cấu trúc như thế này:

struct proc_dir_entry proc_root; 
struct proc_dir_entry *proc_root_driver = &proc_root; 

Các lỗi biên dịch đi, nhưng các tập tin đã không xuất hiện dưới /proc/driver hoặc /proc. Làm cách nào để tạo một mục nhập trong /proc?

Trả lời

4

Nhìn vào proc_fs.h, proc_root_driver được định nghĩa là:

extern struct proc_dir_entry *proc_root_driver; 

chừng nào CONFIG_PROC_FS được kích hoạt. Nếu bạn có CONFIG_PROC_FS chọn khi bạn cấu hình kernel của bạn, bạn sẽ có thể sử dụng nó như bạn đề nghị cho mình ví dụ:

#include <linux/proc_fs.h> 
struct proc_dir_entry * procfile 
procfile = create_proc_entry("myprocfile", 0400, proc_root_driver); 

Nếu điều này không làm việc, kiểm tra xem bạn có CONFIG_PROC_FS thiết lập. Để đảm bảo, bạn có thể biên dịch tệp nguồn của mình bằng tùy chọn -E và kiểm tra xem lệnh create_proc_entry có chứa tham số không NULL như tham số cuối cùng hay không. Nếu nó là NULL, hoặc cuộc gọi là không có ở tất cả, sau đó CONFIG_PROC_FS không được kích hoạt.

3
/* proc entries for ayyaz */ 

#include <linux/module.h> 
#include <linux/kernel.h> 
#include <linux/slab.h> 
#include <linux/string.h> 
#include <linux/timer.h> 
#include <linux/major.h> 
#include <linux/fs.h> 
#include <linux/err.h> 
#include <linux/ioctl.h> 
#include <linux/init.h> 
#include <linux/proc_fs.h> 

#ifdef CONFIG_PROC_FS 

/*====================================================================*/ 
/* Support for /proc/ayyaz */ 

static struct proc_dir_entry *proc_ayyaz; 

DEFINE_MUTEX(ayyaz_table_mutex); 


/*====================================================================*/ 
/* Init code */ 
static int ayyaz_read_proc (char *page, char **start, off_t off, int count, 
          int *eof, void *data_unused) 
{ 
     int len, l, i; 
     off_t begin = 0; 

     mutex_lock(&ayyaz_table_mutex); 

     len = sprintf(page, "hello ayyaz here\n"); 
     mutex_unlock(&ayyaz_table_mutex); 
     if (off >= len+begin) 
       return 0; 
     *start = page + (off-begin); 
     return ((count < begin+len-off) ? count : begin+len-off); 
} 


static int __init init_ayyaz(void) 
{ 
     if ((proc_ayyaz = create_proc_entry("ayyaz_maps", 0, NULL))) 
       proc_ayyaz->read_proc = ayyaz_read_proc; 
     return 0; 
} 

static void __exit cleanup_ayyaz(void) 
{ 
     if (proc_ayyaz) 
       remove_proc_entry("ayyaz", NULL); 
} 

module_init(init_ayyaz); 
module_exit(cleanup_ayyaz); 
#else 
#error "Please add CONFIG_PROC_FS=y in your .config " 
#endif /* CONFIG_PROC_FS */ 


MODULE_LICENSE("proprietary"); 
MODULE_AUTHOR("Md.Ayyaz A Mulla <[email protected]>"); 
MODULE_DESCRIPTION("proc files for ayyaz"); 

Biên dịch trình điều khiển này. Nếu nó biên dịch thành công, thì bạn sẽ thấy /proc/ayyaz.

+0

@ Md.Ayyaz: Chào mừng bạn đến với Stack Overflow :). Tôi đã tự do sửa chữa định dạng mã của bạn - các mẫu mã cần được thụt vào (và có một nút trên thanh công cụ để thực hiện điều đó một cách tự động). –

0
#define PROC_ENTRY_NAME "driver/XX" 
static struct proc_dir_entry *proc_XX; 

static int XX_read_proc (char *page, char **start, off_t off, int count, 
    int *eof, void *data_unused) 
{ 
    return 0; 
} 
static int XX_write_proc (struct file *file, const char __user *buffer, 
    unsigned long count, void *data) 
{ 
    return 0; 
} 

static int __init XX_add_driver(void) 
{ 
    if ((proc_flash = XX_entry(PROC_ENTRY_NAME, 0, NULL))) { 
     proc_XX->read_proc = XX_read_proc; 
     proc_XX->write_proc = XX_write_proc; 
    } 
... 
} 

static void __exit XX_remove(void) 
{ 
    if (proc_flash) 
     remove_proc_entry(PROC_ENTRY_NAME, NULL); 

    return; 
} 

Sau đó, bạn có thể tìm thấy mục nhập /proc/driver/XX.

Các vấn đề liên quan