不使用 sudo 访问 GPIO(树莓派的) [英] Accessing the GPIO (of a raspberry pi) without ``sudo``

查看:31
本文介绍了不使用 sudo 访问 GPIO(树莓派的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然,这个问题可能不是针对树莓派的.另外,我对 Linux 比较陌生.

This question might not be specific to the raspberry pi, of course. Also, I'm relatively new to Linux.

我想写一个小库(在 node.js 中,如果这很重要)来使用 sysfs 访问 raspberry pi 的 GPIO.但是,访问 sysfs 需要 sudo 访问权限,这显然很糟糕.

I want to write a little library (in node.js, if that matters) to access the GPIO of the raspberry pi using the sysfs. However, accessing the sysfs requires sudo access, and that's bad for obvious reasons.

Quick2Wire 似乎有一个解决方案,但我想更好地理解它,而不是盲目地使用它.他们当然使用过 C,但据我所知,代码并不复杂,可能只用 bash 就可以完成,即使不那么优雅.然而,最重要的是,我不确定为什么它有效.

Quick2Wire seems to have a solution, but I want to understand it better and not just blindly use it. They've used C of course, but from what I understand, the code isn't complex, and probably can be pulled off with just bash, even if less elegantly. However, more than anything, I'm not sure why it works.

任何帮助都会很棒.

感谢您的评论.很明显我需要重新表述这个问题.开始:一旦安装(以 root 身份),应用程序如何不再需要任何 root 权限来使用?在这种情况下,将某人添加到群组有何帮助?/sys/devices/virtual/gpio 不是 gpio sysfs 可用的位置,那么它有什么技巧呢?我真的是一个 n00b,所以这些问题可能是 n00b-ish,所以请耐心等待.

Thanks for the comments. It's clear I need to rephrase the question. Here goes: How is it that once installed (as root), the app doesn't require any more root perms to use? How does adding someone to a group help in this case? /sys/devices/virtual/gpio isn't the location where the gpio sysfs is available, so what's the trickery with that? I'm really a n00b, so these questions might be n00b-ish, so please bear with me.

推荐答案

Rakesh,我一直在试图找出完全相同的问题,我想我已经解决了.

Rakesh, I've just been trying to figure out exactly the same thing, and I think I've solved it.

您根本不需要了解很多 makefile.重要的几行如下,当你运行 sudo make install

You don't need to understand much of the makefile at all. The important lines are the following, which are executed in bash when you run sudo make install

install: install-files
    groupadd -f --system gpio
    chgrp gpio $(DESTDIR)/bin/gpio-admin
    chmod u=rwxs,g=rx,o= $(DESTDIR)/bin/gpio-admin

groupadd -f --system gpio 创建一个名为 gpio 的系统组.chgrp gpio $(DESTDIR)/bin/gpio-admin 将二进制文件的组(C 文件 gpio-admin.c 编译为该组)更改为 gpio.二进制文件的所有者仍然是 root(因为你以 root 身份运行 make.)chmod u=rwxs,g=rx,o= $(DESTDIR)/bin/gpio-admin 做了两个重要的事情事物.首先,它让 gpio 组的成员运行 gpio-admin.其次,它在 gpio-admin 上设置 setuid 位.

groupadd -f --system gpio creates a system group called gpio. chgrp gpio $(DESTDIR)/bin/gpio-admin changes the group of the binary (which the C file gpio-admin.c was compiled to) to gpio. The owner of the binary is still root (since you're running make as root.) chmod u=rwxs,g=rx,o= $(DESTDIR)/bin/gpio-admin does two important things. Firstly, it lets a member of the gpio group run gpio-admin. Secondly, it sets the setuid bit on gpio-admin.

当您将自己添加到 gpio 组时,您可以运行 gpio-admin,而无需使用 sudo,但 gpio admin 会表现得就像在 sudo 下运行一样.这允许它写入/sys/class/gpio/export 文件.它还允许它更改创建的文件/sys/class/gpio/gpio[pin number]/direction 等的所有者.

When you add yourself to the gpio group, you can run gpio-admin, without using sudo, but gpio admin will act like it is being run under sudo. This allows it to write to the /sys/class/gpio/export file. It also allows it to change the owner of the files /sys/class/gpio/gpio[pin number]/direction etc. that get created.

即使你把/sys/class/gpio/export的组改成gpio,并设置权限允许你写入

Even if you change the group of /sys/class/gpio/export to gpio, and set permissions to allow you to write to it

sudo chgrp gpio /sys/class/gpio/export /sys/class/gpio/unexport
sudo chmod g+rwx /sys/class/gpio/export /sys/class/gpio/unexport

无需超级用户权限即可导出 pin

you can export a pin without superuser powers

echo 22 > /sys/class/gpio/export

但文件/sys/class/gpio/gpio22/direction 等仍将以 root 作为所有者和组创建,您需要使用 sudo 来更改它们.此外,每次重新启动后,导出和取消导出文件的所有权将恢复为 root.

but the files /sys/class/gpio/gpio22/direction etc. will still be create with root as the owner and group, and you'll need to use sudo to change them. Also, ownership of the export and unexport files will revert to root after each reboot.

这篇关于不使用 sudo 访问 GPIO(树莓派的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆