6

我想在目录下创建一个文件/proc/driver。我想使用类似proc_root_driver(或提供的其他东西)的宏,而不是明确使用“驱动程序/模块名称”。我使用create_proc_entry

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

谷歌搜索后,我发现了使用的建议proc_root_driver,但是当我使用它时,我得到了错误

此函数中未声明 proc_root_driver

而且,proc_root_driver在 linux/proc_fs.h 中不可用。

我试图声明这样的结构:

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

编译错误消失了,但文件没有出现在/proc/driveror下/proc。我怎样才能在其中创建一个条目/proc

4

3 回答 3

4

查看 proc_fs.h,proc_root_driver 定义为:

extern struct proc_dir_entry *proc_root_driver;

只要启用 CONFIG_PROC_FS。如果您在配置内核时选择了 CONFIG_PROC_FS,您应该能够按照您自己的建议使用它,即:

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

如果这不起作用,请检查您是否设置了 CONFIG_PROC_FS。为确保,您可以使用 -E 选项编译源文件并检查 create_proc_entry 调用是否包含非 NULL 参数作为最后一个参数。如果它为 NULL,或者调用根本不存在,则不启用 CONFIG_PROC_FS。

于 2009-03-02T17:05:49.507 回答
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  <md.ayyaz@gmail.com>");
MODULE_DESCRIPTION("proc files for ayyaz");

编译此驱动程序。如果它编译成功,那么您将看到/proc/ayyaz.

于 2010-03-12T11:37:48.957 回答
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;
}

然后就可以找到/proc/driver/XX入口了。

于 2015-10-20T13:00:49.290 回答