小编典典

使用Linux sysfs_notify调用

linux

我正在尝试在内核驱动程序和用户空间程序之间进行异步通信(我知道这里有很多问题需要类似的信息,但是找不到与sysfs_notify相关的信息)。

我将在这里留下Vilhelm的编辑,但是将源代码添加到一个使用sysfs的简单驱动程序中,并在一个用户空间程序中对其进行轮询。驱动程序工作正常(我从网络上获得了大部分钱;它缺少功劳,但是我回去添加功劳时找不到它们)。不幸的是,轮询程序无法正常工作。它总是立即返回成功。有趣的是,如果我在轮询之前不执行两次读取,则revents成员将设置为POLLERR
| 在程序输出中看到的是POLLIN,而不仅仅是POLLIN。

程序输出:

root @ ubuntu:/ home / wmulcahy / demo#./readhello已
触发
属性文件值:74(t)[0]
清除[0]:00000001
清除[1]:00000001

这是驱动程序:hello.c(您可以看到我从哪里开始…)

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>

struct my_attr {
    struct attribute attr;
    int value;
};

static struct my_attr notify = {
    .attr.name="notify",
    .attr.mode = 0644,
    .value = 0,
};

static struct my_attr trigger = {
    .attr.name="trigger",
    .attr.mode = 0644,
    .value = 0,
};

static struct attribute * myattr[] = {
    &notify.attr,
    &trigger.attr,
    NULL
};

static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
{
    struct my_attr *a = container_of(attr, struct my_attr, attr);
    printk( "hello: show called (%s)\n", a->attr.name );
    return scnprintf(buf, PAGE_SIZE, "%s: %d\n", a->attr.name, a->value);
}
static struct kobject *mykobj;

static ssize_t store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t len)
{
    struct my_attr *a = container_of(attr, struct my_attr, attr);

    sscanf(buf, "%d", &a->value);
    notify.value = a->value;
    printk("sysfs_notify store %s = %d\n", a->attr.name, a->value);
    sysfs_notify(mykobj, NULL, "notify");
    return sizeof(int);
}

static struct sysfs_ops myops = {
    .show = show,
    .store = store,
};

static struct kobj_type mytype = {
    .sysfs_ops = &myops,
    .default_attrs = myattr,
};

static struct kobject *mykobj;
static int __init hello_module_init(void)
{
    int err = -1;
    printk("Hello: init\n");
    mykobj = kzalloc(sizeof(*mykobj), GFP_KERNEL);
    if (mykobj) {
        kobject_init(mykobj, &mytype);
        if (kobject_add(mykobj, NULL, "%s", "hello")) {
             err = -1;
             printk("Hello: kobject_add() failed\n");
             kobject_put(mykobj);
             mykobj = NULL;
        }
        err = 0;
    }
    return err;
}

static void __exit hello_module_exit(void)
{
    if (mykobj) {
        kobject_put(mykobj);
        kfree(mykobj);
    }
    printk("Hello: exit\n");
}

module_init(hello_module_init);
module_exit(hello_module_exit);
MODULE_LICENSE("GPL");

这是轮询程序:readhello.c

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h> 
#include <sys/stat.h> 
#include <poll.h>

#define TEST_SYSFS_TRIGGER  "/sys/hello/trigger"
#define TEST_SYSFS_NOTIFY   "/sys/hello/notify"

int main(int argc, char **argv)
{
    int cnt, notifyFd, triggerFd, rv;
    char attrData[100];
    struct pollfd ufds[2];

    // Open a connection to the attribute file.
    if ((notifyFd = open(TEST_SYSFS_NOTIFY, O_RDWR)) < 0)
    {
        perror("Unable to open notify");
        exit(1);
    }
    // Open a connection to the attribute file.
    if ((triggerFd = open(TEST_SYSFS_TRIGGER, O_RDWR)) < 0)
    {
        perror("Unable to open trigger");
        exit(1);
    }

    ufds[0].fd = notifyFd;
    ufds[0].events = POLLIN;
    ufds[1].fd = triggerFd;
    ufds[1].events = POLLIN;

    // Someone suggested dummy reads before the poll() call
    cnt = read( notifyFd, attrData, 100 );
    cnt = read( triggerFd, attrData, 100 );
    ufds[0].revents = 0;
    ufds[1].revents = 0;
    if (( rv = poll( ufds, 2, 10000)) < 0 )
    {
        perror("poll error");
    }
    else if (rv == 0)
    {
        printf("Timeout occurred!\n");
    }
    else if (ufds[0].revents & POLLIN)
    {
        printf("triggered\n");
        cnt = read( notifyFd, attrData, 1 );
        printf( "Attribute file value: %02X (%c) [%d]\n", attrData[0], attrData[0], cnt );
    }
    printf( "revents[0]: %08X\n", ufds[0].revents );
    printf( "revents[1]: %08X\n", ufds[1].revents );

    close( triggerFd );
    close( notifyFd );
}

即将推出的sysfs增强功能

在内部,补丁程序向系统上的每个kobject添加了一个等待队列头。将该队列插入到轮询表中,以响应poll()调用。sysfs代码无法知道,但是,任何给定sysfs属性的值何时更改,因此实现可轮询属性的子系统必须对以下内容进行显式调用:

void sysfs_notify(struct kobject *kobj, char *dir, char *attr);

谢谢李


阅读 1459

收藏
2020-06-07

共1个答案

小编典典

阻止民意调查来自用户方。用户代码可以简单地告诉内核它感兴趣的属性,然后将其阻塞poll()直到其中一个被更改为止。

sysfs_notify()是一个释放用户空间的内核端调用poll()。调整内核属性值之后,只需调用sysfs_notify()以允许任何用户空间应用程序响应其未完成的请求poll()

可以将其poll()视为“订阅”以关注感兴趣的属性的变化,并可以将sysfs_notify()其“发布”给任何订户。

2020-06-07