bash 以非 root 用户身份访问 GPIO (/sys/class/gpio)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30938991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:13:57  来源:igfitidea点击:

Access GPIO (/sys/class/gpio) as non-root

bashraspberry-pichmodchownraspberry-pi2

提问by TiMESPLiNTER

The /sys/class/gpiocan only be access as rootby default. So I like that a new group gpiocan use the files and directories under /sys/class/gpio. To achieve that I added the following lines to /etc/rc.local(I'm on Debian):

/sys/class/gpio只能访问为root默认。所以我喜欢一个新组gpio可以使用/sys/class/gpio. 为了实现这一点,我添加了以下几行/etc/rc.local(我在 Debian 上):

sudo chown root:gpio /sys/class/gpio/unexport /sys/class/gpio/export
sudo chmod 220 /sys/class/gpio/unexport /sys/class/gpio/export

So this gives write permissions to all the gpiogroup members. So they can now exportand unexportpins fine.

因此,这为所有gpio组成员提供了写入权限。所以他们现在可以export并且unexport别针了。

The problem is they can't read/write the specific pin files after export(e.x. /sys/class/gpio/gpio17) beacause those are owned by root:rootagain.

问题是他们无法在export(ex /sys/class/gpio/gpio17)之后读取/写入特定的 pin 文件,因为它们root:root再次归于所有。

How can I change that they are created by default as root:gpiotoo? I mean I can do that manually each time I export a pin. But that's a bit uncomfy.

如何更改它们也是默认创建的root:gpio?我的意思是每次导出 pin 时我都可以手动执行此操作。但这有点不舒服。

UPDATE

更新

According to larsks' answer I created the missing rule file. Now it partially works:

根据 larsks 的回答,我创建了丢失的规则文件。现在它部分工作:

-rwxrwx---  1 root gpio 4096 Jun 19 16:48 export
lrwxrwxrwx  1 root gpio    0 Jun 19 16:51 gpio17 -> ../../devices/soc/3f200000.gpio/gpio/gpio17
lrwxrwxrwx  1 root gpio    0 Jun 19 16:45 gpiochip0 -> ../../devices/soc/3f200000.gpio/gpio/gpiochip0
-rwxrwx---  1 root gpio 4096 Jun 19 16:45 unexport

But for the ./gpio17/I still get root:root:

但对于./gpio17/我仍然得到root:root

-rw-r--r-- 1 root root 4096 Jun 19 16:52 active_low
lrwxrwxrwx 1 root root    0 Jun 19 16:52 device -> ../../../3f200000.gpio
-rw-r--r-- 1 root root 4096 Jun 19 16:52 direction
-rw-r--r-- 1 root root 4096 Jun 19 16:52 edge
drwxr-xr-x 2 root root    0 Jun 19 16:52 power
lrwxrwxrwx 1 root root    0 Jun 19 16:52 subsystem -> ../../../../../class/gpio
-rw-r--r-- 1 root root 4096 Jun 19 16:52 uevent
-rw-r--r-- 1 root root 4096 Jun 19 16:52 value

UPDATE 2

更新 2

Okay I solved the problem. Because I installed Raspbian over the RaspbianInstaller I never went through the raspi-configtool. This seems to be a problem. Because I was also missing the /sys/device/virtual/gpio/folder.

好的,我解决了这个问题。因为我通过 RaspbianInstaller 安装了 Raspbian,所以我从未使用过该raspi-config工具。这似乎是一个问题。因为我也缺少/sys/device/virtual/gpio/文件夹。

I followed this guide here: http://www.element14.com/community/message/139528/l/re-piface-digital-2--setup-and-use#139528

我在这里遵循了本指南:http: //www.element14.com/community/message/139528/l/re-piface-digital-2--setup-and-use#139528

And afterwards the permissions were correct (even for the pin-folders and their files value, direction, ...).

事后的权限是正确的(即使是脚文件夹及其文件valuedirection...)。

采纳答案by larsks

You can do this using udevrules, which can define actions to execute when the kernel instantiates new devices. Current versions of the Raspbiandistribution for Raspberry Pi devices contain the following in /etc/udev/rules.d/99-com.rules:

您可以使用udev规则来执行此操作,这些规则可以定义内核实例化新设备时要执行的操作。用于 Raspberry Pi 设备的Raspbian发行版的当前版本包含以下内容/etc/udev/rules.d/99-com.rules

SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio; chown -R root:gpio /sys/devices/virtual/gpio && chmod -R 770 /sys/devices/virtual/gpio'"

This ensures that entries under /sys/class/gpioare always available to members of the gpiogroup:

这可确保组/sys/class/gpio成员始终可以使用以下条目gpio

# ls -lL /sys/class/gpio/
total 0
-rwxrwx--- 1 root gpio 4096 May  6 23:36 export
drwxrwx--- 2 root gpio    0 Jan  1  1970 gpiochip0
-rwxrwx--- 1 root gpio 4096 May  6 23:37 unexport
# echo 11 > /sys/class/gpio/export 
# ls -lL /sys/class/gpio/
total 0
-rwxrwx--- 1 root gpio 4096 May  6 23:37 export
drwxrwx--- 2 root gpio    0 May  6 23:37 gpio11
drwxrwx--- 2 root gpio    0 Jan  1  1970 gpiochip0
-rwxrwx--- 1 root gpio 4096 May  6 23:37 unexport

Update

更新

Permissions are correct for individual pins as well:

单个引脚的权限也是正确的:

# ls -Ll /sys/class/gpio/gpio11/
total 0
-rwxrwx--- 1 root gpio 4096 May  6 23:37 active_low
drwxr-xr-x 3 root root    0 May  6 23:36 device
-rwxrwx--- 1 root gpio 4096 May  6 23:37 direction
-rwxrwx--- 1 root gpio 4096 May  6 23:37 edge
drwxrwx--- 2 root gpio    0 May  6 23:37 subsystem
-rwxrwx--- 1 root gpio 4096 May  6 23:37 uevent
-rwxrwx--- 1 root gpio 4096 May  6 23:37 value

回答by Roman Savrulin

More common rule for 4.x kernels will be the following

4.x 内核的更常见规则如下

SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'find -L /sys/class/gpio/ -maxdepth 2 -exec chown root:gpio {} \; -exec chmod 770 {} \; || true'"

The rule in the initial answer will fail to chown the exported gpio if there's a symbolic link in the path

如果路径中有符号链接,则初始答案中的规则将无法 chown 导出的 gpio

UPDplease beg in mind that when you export some GPIO via sysfs, you should wait for udev rule to fire and complete before you get desired access rights. The thing that worked for me was sleep about 100ms before trying to access GPIO files.

UPD请记住,当您通过 sysfs 导出某些 GPIO 时,您应该等待 udev 规则触发并完成,然后才能获得所需的访问权限。对我有用的是在尝试访问 GPIO 文件之前休眠大约 100 毫秒。

回答by Kevin P. Fleming

Expanding on the answer by @roman-savrulin, here's a simpler version.

扩展@roman-savrulin 的答案,这是一个更简单的版本。

There's no need to run the rule on REMOVE events, only ADD events. There's also no need to run 'find' as the udev environment will supply the exact path of the sysfs directory containing the new GPIO pin's files. You can also use 'chgrp' to change only the owning group, and symbolic modes in 'chmod' to only add the group-write permission bit.

无需在 REMOVE 事件上运行规则,只需在 ADD 事件上运行。也不需要运行“find”,因为 udev 环境将提供包含新 GPIO 引脚文件的 sysfs 目录的确切路径。您还可以使用 'chgrp' 仅更改所属组,并使用 'chmod' 中的符号模式仅添加组写入权限位。

You'll still have to wait for the completion of the rule processing before trying to open the pin's files, but the process should complete more quickly with a simpler rule which only touches the minimum number of files necessary.

在尝试打开 pin 的文件之前,您仍然需要等待规则处理完成,但该过程应该使用更简单的规则更快地完成,该规则仅涉及所需的最少文件数。

SUBSYSTEM=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chgrp -R gpio /sys/${DEVPATH} && chmod -R g+w /sys/${DEVPATH}'"