How to create proc entry under /proc/driver?

2020-07-10 11:15发布

I want to create a file under a /proc/driver directory. I would like to use a macro like proc_root_driver (or something else provided) rather than use "driver/MODULE_NAME" explicitly. I use create_proc_entry :

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

After googling, I found suggestion to use proc_root_driver, but when I use it, I get the error

proc_root_driver undeclared in this function

And also, proc_root_driver is not available in linux/proc_fs.h.

I have tried to declare structure like this:

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

The compilation errors gone, but the file didn't appear under /proc/driver or /proc. How can I make create an entry in /proc?

3条回答
做个烂人
2楼-- · 2020-07-10 12:01

Looking at proc_fs.h, proc_root_driver is defined as :

extern struct proc_dir_entry *proc_root_driver;

so long as CONFIG_PROC_FS is enabled. If you have CONFIG_PROC_FS selected when you configure your kernel, you should be able to use it as you suggested yourself i.e. :

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

If this does not work, check that you have CONFIG_PROC_FS set. To make sure, you can compile your source file with the -E option and check that the create_proc_entry call includes a non NULL parameter as the last parameter. If it is NULL, or the call is not there at all, then CONFIG_PROC_FS is not enabled.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-07-10 12:06
#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;
}

Then you can find the /proc/driver/XX entry.

查看更多
Root(大扎)
4楼-- · 2020-07-10 12:13
/* 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");

Compile this driver. If it compiles sucessfully, then you will see /proc/ayyaz.

查看更多
登录 后发表回答