linux内核sysfs详解-1,Linux内核通信-基于sysfs

问题来源

写了个内核模块的demo,想在用户态跟它交互,如何把用户态数据发送给它?

相关概念

2c86954f9275

image.png

基于sysfs的通信

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);

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

#include

#include

#include

#include

#include

#include

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

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值