问题来源
写了个内核模块的demo,想在用户态跟它交互,如何把用户态数据发送给它?
相关概念
image.png基于sysfs的通信
struct kobj_attribute 和 __ATTR 宏
struct kobj_attribute {
struct attribute attr;
ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, char *buf);
ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count);
};
#define __ATTR(_name, _mode, _show, _store) { \
.attr = {.name = __stringify(_name), \
.mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \
.show = _show, \
.store = _store, \
}
初始化一个kobj_attribute
static ssize_t kcom_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
...
return 0;
}
static ssize_t kcom_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
...
return count;
}
static struct kobj_attribute kcom_attribute = __ATTR(kcom, 0644, kcom_show, kcom_store);
kobject
struct kobject {
const char *name;
struct list_head entry;
struct kobject *parent;
struct kset *kset;
struct kobj_type *ktype;
struct kernfs_node *sd; /* sysfs directory entry */
struct kref kref;
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
struct delayed_work release;
#endif
unsigned int state_initialized:1;
unsigned int state_in_sysfs:1;
unsigned int state_add_uevent_sent:1;
unsigned int state_remove_uevent_sent:1;
unsigned int uevent_suppress:1;
};
kobject_create_and_add 和 kobject_put
Let’s first create sysfs with kobject_create_and_add
function.
This function creates a kobject
structure dynamically and registers it with sysfs.
If the kobject was not able to be created, NULL will be returned.
When you are finished with this structure, call kobject_put
and the structure will be dynamically freed when it is no longer being used.
struct kobject *kobject_create_and_add(const char *name, struct kobject *parent);
Where, name
is the name for the kobject, parent
is the parent kobject of this kobject, if any.
If we pass kernel_kobj
to the second argument, it will create the directory under /sys/kernel/
, and under /sys/firmware/
for firmware_kobj
, under /sys/fs
for fs_kobj
respectively. To create directly under /sys/ we may pass NULL
.
static struct kobject *kcom_kobject;
kcom_kobject = kobject_create_and_add("kcom", kernel_kobj);
kobject_put(kcom_kobject);
sysfs_create_file 和 sysfs_remove_file
int sysfs_create_file(struct kobject *kobj, const struct attribute *attr);
void sysfs_remove_file(struct kobject *kobj, const struct attribute *attr);
sysfs_create_file(kcom_kobject, &kcom_attribute.attr);
sysfs_remove_file(kcom_kobject, &kcom_attribute.attr);
完整代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
static struct kobject *kcom_kobject;
static char kcom_buffer[4096] = {0, };
static ssize_t kcom_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
int ret = sprintf(buf, "Kernel response to msg: %s\n", kcom_buffer);
return ret;
}
static ssize_t kcom_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
snprintf(kcom_buffer, sizeof(kcom_buffer), "%s", buf);
kcom_buffer[sizeof(kcom_buffer) - 1] = '\0';
return count;
}
static struct kobj_attribute kcom_attribute = __ATTR(value, 0644, kcom_show, kcom_store);
static int __init kcom_init(void)
{
int ret = 0;
pr_info("kcom: Initialize module\n");
kcom_kobject = kobject_create_and_add("kcom", kernel_kobj);
if (!kcom_kobject) {
pr_err("kcom: Failed to create kernel object\n");
return -ENOMEM;
}
ret = sysfs_create_file(kcom_kobject, &kcom_attribute.attr);
if (ret)
pr_err("kcom: Failed to create sysfs file\n");
return ret;
}
static void __exit kcom_exit(void)
{
pr_info("kcom: Deinitialize module\n");
sysfs_remove_file(kcom_kobject, &kcom_attribute.attr);
kobject_put(kcom_kobject);
}
module_init(kcom_init);
module_exit(kcom_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ikx");
MODULE_DESCRIPTION("Kernel Communication Module.");
MODULE_VERSION("0.1");
编译和测试
Makefile
obj-m += kcom.o
KDIR := /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean
$ make
$ sudo insmod kcom.ko
$ su
root@~# echo Hello > /sys/kernel/kcom/value
root@~# cat /sys/kernel/kcom/value
Kernel response to msg: Hello
root@~# rmmod kcom.ko
网友评论