linux sysfs open,使用Linux sysfs_notify调用

我试图在内核驱动程序和用户空间程序之间异步通信(我知道这里有很多问题需要类似的信息,但我找不到任何处理sysfs_notify的问题).

我将离开Vilhelm的编辑,但是将源添加到使用sysfs的简单驱动程序和用户空间程序进行轮询.驱动程序工作正常(我从网上获得了大部分;它缺少了学分,但是当我回去添加它们时我找不到它们).

不幸的是,投票计划不起作用.它总是立即返回成功.有趣的是,如果我在轮询之前不执行两次读取,则将revents成员设置为POLLERR |如程序输出中所见,POLLIN而不仅仅是POLLIN.

节目输出:

root@ubuntu:/home/wmulcahy/demo# ./readhello

triggered

Attribute file value: 74 (t) [0]

revents[0]: 00000001

revents[1]: 00000001

这是驱动程序:hello.c(你可以看到我开始的地方……)

#include

#include

#include

#include

#include

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

#include

#include

#include

#include

#include

#include

#include

#include

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

}

Internally, the patch adds a wait queue head to every kobject on the

system; that queue is inserted into a poll table in response to a

poll() call. The sysfs code has no way of knowing, however, when the

value of any given sysfs attribute has changed, so the subsystem

implementing a pollable attribute must make explicit calls to:

06002

谢谢,

背风处

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值