How to create proc entry under /proc/driver?

隐身守侯 提交于 2019-12-04 06:13:36

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.

Md.Ayyaz A Mulla
/* 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.

akenhsu
#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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!