简体   繁体   中英

Kernel module get data from user space

What is the proper way of sending some data to a loaded and running kernel module, without using netlink and without using features that may not be in place (eg debugfs)?

I'd like to see a clean and safe way of doing this which should work on most kernels (or preferably all modern ones), or at best an approximation of that.

The user who wants to send data to the module is the root user, the amount of data is probably under 64 kiB and consists of a series of strings.

I've already looked into trying to read files from the module, which is not only highly frowned upon for various reasons but also hard to do. I've looked at netlink, which socket() tells me on my kernel is not supported. I've looked at debugfs, which is not supported either on my kernel.

Obviously I could use a different kernel but as I mentioned I'd like a proper way of doing this. If someone could show me a simple example of a module that will just do a printk() of a string sent from user space that would be great.

... a simple example of a module that will just do a printk() of a string sent from user space, printkm.c:

#include <linux/module.h>
#include <linux/proc_fs.h>
MODULE_DESCRIPTION("printk example module");
MODULE_AUTHOR("Dietmar.Schindler@manroland-web.com");
MODULE_LICENSE("GPL");

static
ssize_t write(struct file *file, const char *buf, size_t count, loff_t *pos)
{
    printk("%.*s", count, buf);
    return count;
}

static struct file_operations file_ops;

int init_module(void)
{
    printk("init printk example module\n");
    struct proc_dir_entry *entry = proc_create("printk", 0, NULL, &file_ops);
    if (!entry) return -ENOENT;

    file_ops.owner = THIS_MODULE,
    file_ops.write = write;
    return 0;
}

void cleanup_module(void)
{
    remove_proc_entry("printk", NULL);
    printk("exit printk example module\n");
}

Example use:

root@kw:~# insmod printkm.ko
root@kw:~# echo a string >/proc/printk 
root@kw:~# dmesg|tail -1
[193634.164459] a string

I think you can use a char device. Take a look at Linux Device Driver 3th Chapter 3. With the function *copy_to_user* and *copy_from_user* you can copy data safely to and from userspace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM