简体   繁体   English

Linux Kernel IIO事件sysfs文件只能读取

[英]Linux Kernel IIO events sysfs files only readable

I have the problem that I registered IIO events for rising and falling thresholds. 我有一个问题,我注册了IIO事件的上升和下降阈值。 I can see the sysfs files in events subfolder and can read them, but when I try to write a new threshold it says "permission denied". 我可以在events子文件夹中看到sysfs文件,并且可以读取它们,但是当我尝试写一个新的阈值时,它会说“权限被拒绝”。

following setup: 以下设置:

static const struct iio_event_spec as6200_events[] = {
  {
    .type = IIO_EV_TYPE_THRESH,
    .dir = IIO_EV_DIR_RISING,
    .mask_separate = BIT(IIO_EV_INFO_VALUE),
  }, {
    .type = IIO_EV_TYPE_THRESH,
    .dir = IIO_EV_DIR_FALLING,
    .mask_separate = BIT(IIO_EV_INFO_VALUE),
  }
};

static const struct iio_chan_spec as6200_channels[] = {
  {
    .type = IIO_TEMP,
    .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
      BIT(IIO_CHAN_INFO_PROCESSED) |
      BIT(IIO_CHAN_INFO_SCALE),
    .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),
    .event_spec = as6200_events,
    .num_event_specs = ARRAY_SIZE(as6200_events),
  }
};

finding: it works when I change the permissions of the in_temp_thresh_rising_value file to 666 via sudo . 查找:当我通过sudoin_temp_thresh_rising_value文件的权限更改为666时,它可以正常工作。 But why is it not created with this permissions via IIO subsystem? 但是为什么不通过IIO子系统创建此权限?

This is common practice for sysfs files, as writing to those files can alter system's behavior and even compromise or break the system. 这是sysfs文件的常见做法,因为写入这些文件可能会改变系统的行为甚至危及或破坏系统。 So if you want to write to those files, you should do that from root, or add your user to corresponding group, or change that file mode (by udev rule or by hand). 因此,如果要写入这些文件,则应从root用户执行此操作,或将用户添加到相应的组,或更改该文件模式(通过udev规则或手动)。

Here is how it's done in IIO code: 以下是它在IIO代码中的完成方式:

  • IIO sysfs node names are derived from next tables in drivers/iio/industrialio-event.c : iio_ev_type_text , iio_ev_dir_text and iio_ev_info_text IIO sysfs节点名称来自drivers / iio / industrialio-event.c中的下一个表: iio_ev_type_textiio_ev_dir_textiio_ev_info_text
  • node creation path is next: iio_device_add_event() -> __iio_add_chan_devattr() -> __iio_device_attr_init() 节点创建路径是下一个: iio_device_add_event() - > __iio_add_chan_devattr() - > __iio_device_attr_init()
  • file mode for sysfs node is being set in __iio_device_attr_init() : sysfs节点的文件模式在__iio_device_attr_init()中设置:

    • for reading: dev_attr->attr.mode |= S_IRUGO; 用于阅读: dev_attr->attr.mode |= S_IRUGO;

      • so every user can read the node (because S_IRUGO allows R eading for U ser, G roup and O thers) 所以每个用户都可以读取节点(因为S_IRUGO允许对U ser, G roup和O thers进行R eading)
    • for writing: dev_attr->attr.mode |= S_IWUSR; 写作: dev_attr->attr.mode |= S_IWUSR;

      • so it only can be written by root (because S_IWUSR allows writing only for file owner, which is root) 所以它只能由root写入(因为S_IWUSR只允许写文件所有者,这是root)

Another solution to this problem is to use a combination of libiio's network and local contexts. 此问题的另一个解决方案是使用libiio的网络和本地上下文的组合。 In this case, a libiio daemon would be started with the appropriate privileges to write to the sysfs files, and the user application would then connect with this daemon using a libiio network context. 在这种情况下,将使用适当的权限启动libiio守护程序以写入sysfs文件,然后用户应用程序将使用libiio网络上下文与此守护程序连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM