将一些数据发送到已加载和运行的内核模块的正确方法是什么,而不使用netlink并且不使用可能没有的功能(例如debugfs)?
我希望看到一种干净而安全的方法,这种方法应该适用于大多数内核(或者最好是现代内核),或者最好是近似内核。
想要向模块发送数据的用户是root用户,数据量可能低于64 kiB并且由一系列字符串组成。
我已经考虑过尝试从模块中读取文件,这不仅因为各种原因而非常不受欢迎,而且很难做到。 我看过netlink,socket()告诉我我的内核不支持。 我查看了debugfs,我的内核也不支持它。
显然我可以使用不同的内核,但正如我所提到的,我想要一个正确的方法来做到这一点。如果有人可以向我展示一个模块的简单示例,它只会执行从用户空间发送的字符串的printk(),这将非常棒。
答案 0 :(得分:1)
...一个模块的简单示例,它只执行从用户空间发送的字符串的printk(),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");
}
使用示例:
root@kw:~# insmod printkm.ko
root@kw:~# echo a string >/proc/printk
root@kw:~# dmesg|tail -1
[193634.164459] a string
答案 1 :(得分:0)
我认为你可以使用char设备。请查看Linux Device Driver 3th第3章。使用函数* copy_to_user *和* copy_from_user *,您可以安全地将数据复制到用户空间和从用户空间复制数据。