# Using GPIO add\_event\_detect() in non-priviledged mode

**URL:** <https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284>\
**Category:** balenaOS\
**Tags:** gpio\
**Created:** [May 22, 2020, 9:23am UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284 "2020-05-22T09:23:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhazley](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mhazley/32/4851_2.png) [@mhazley](https://forums.balena.io/u/mhazley)\
**Post date:** [May 22, 2020, 9:23am UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/1 "2020-05-22T09:23:21Z")

</div>

Hi - has anyone been able to use the RPi.GPIO edge detect when not running in privileged mode?

I was able to use the GPIO fine with the following settings:

```auto
    devices:
      - "/dev/gpiomem:/dev/gpiomem"
    cap_add:
      - SYS_RAWIO

```

Trying to use the above with `GPIO.add_event_detect` fails with `RuntimeError: Failed to add edge detection`.

I’ve had to go to privileged mode for this to work.

---

<div class="post-metadata">

**Author:** ![rahul-thakoor](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rahul-thakoor](https://forums.balena.io/u/rahul-thakoor)\
**Post date:** [May 22, 2020, 10:01am UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/3 "2020-05-22T10:01:19Z")

</div>

hey @mhazley,  
According to the source code for RPi.GPIO, adding edge detection would start a thread. You can look [here](https://sourceforge.net/p/raspberry-gpio-python/code/ci/default/tree/source/py_gpio.c#l721)

The error seems related to permissions and I believe you need to add more capabilities to the docker-compose file. Perhaps [this](http://man7.org/linux/man-pages/man7/capabilities.7.html) this will help.

Let us know if it works for you,  
Cheers 🙂

---

<div class="post-metadata">

**Author:** ![mhazley](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mhazley/32/4851_2.png) [@mhazley](https://forums.balena.io/u/mhazley)\
**Post date:** [May 22, 2020, 11:15am UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/5 "2020-05-22T11:15:39Z")

</div>

Thanks for the links, checked out the source and seems it fails either calling:

`epoll_create`  
`epoll_ctl`  
or  
`pthread_create`

Coming up a bit blank with the capabilities required… `pthread_create` says:

> The new thread inherits copies of the calling thread’s capability sets

So perhaps theres a capability for creating threads… I will keep reading, thanks for the sign-posts!

---

<div class="post-metadata">

**Author:** ![rahul-thakoor](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rahul-thakoor](https://forums.balena.io/u/rahul-thakoor)\
**Post date:** [May 22, 2020, 11:36am UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/6 "2020-05-22T11:36:43Z")

</div>

@mhazley,  
Perhaps you could also try adding  
`/dev/mem:/dev/mem` to devices ?

---

<div class="post-metadata">

**Author:** ![mhazley](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mhazley/32/4851_2.png) [@mhazley](https://forums.balena.io/u/mhazley)\
**Post date:** [May 22, 2020, 11:46am UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/7 "2020-05-22T11:46:48Z")

</div>

Thanks - same error.

I will release as privileged for now and then work to optimise. I’ll report back here when I work it out!

---

<div class="post-metadata">

**Author:** ![rahul-thakoor](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rahul-thakoor](https://forums.balena.io/u/rahul-thakoor)\
**Post date:** [May 22, 2020, 11:58am UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/8 "2020-05-22T11:58:10Z")

</div>

Thanks. Very grateful if you could do that. Would be helpful for other community members too.

Cheers!

---

<div class="post-metadata">

**Author:** ![rahul-thakoor](https://avatars.discourse-cdn.com/v4/letter/r/9de053/32.png) [@rahul-thakoor](https://forums.balena.io/u/rahul-thakoor)\
**Post date:** [May 22, 2020, 12:12pm UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/11 "2020-05-22T12:12:35Z")

</div>

As a side note,  
When working with interface and running in unprivileged containers , the [io.balena.features.sysfs](https://www.balena.io/docs/reference/supervisor/docker-compose/#labels) can be a good option. It is available as from supervisor v10.8.0.

---

<div class="post-metadata">

**Author:** ![mhazley](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mhazley/32/4851_2.png) [@mhazley](https://forums.balena.io/u/mhazley)\
**Post date:** [May 22, 2020, 12:31pm UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/13 "2020-05-22T12:31:09Z")

</div>

Thanks, I’ll try that, I am running 10.6.27 currently so will upgrade and try.

---

<div class="post-metadata">

**Author:** ![mltgames](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@mltgames](https://forums.balena.io/u/mltgames)\
**Post date:** [February 27, 2023, 12:35am UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/15 "2023-02-27T00:35:41Z")

</div>

I have the same issue with the add\_event\_detect, I tried to add :

```auto
    devices:
      - "/dev/gpiomem:/dev/gpiomem"
    cap_add:
      - SYS_RAWIO

```

OR

```auto
    devices:
      - "/dev/gpiomem:/dev/gpiomem"
    cap_add:
      - ALL

```

but i have the same error : `RuntimeError: Failed to add edge detection`.

I will leave it as privileged mode for now… but it’s not the best.

---

<div class="post-metadata">

**Author:** ![mltgames](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@mltgames](https://forums.balena.io/u/mltgames)\
**Post date:** [February 27, 2023, 2:27pm UTC](https://forums.balena.io/t/using-gpio-add-event-detect-in-non-priviledged-mode/118284/16 "2023-02-27T14:27:19Z")

</div>

I did some more test using strace.

When you mount

```auto
/dev/gpiomem

```

In strace you have a Read-only file system to /sys/class/gpio/gpio17/direction even if I should have the access.

```auto
openat(AT_FDCWD, "/sys/class/gpio/gpio17/direction", O_WRONLY) = -1 EROFS (Read-only file system)

```

If I try with a user that dont have the access on /sys/class/gpio/gpio17/direction I have a permission denied error.

```auto
openat(AT_FDCWD, "/sys/class/gpio/gpio17/direction", O_WRONLY) = -1 EACCES (Permission denied)

```

I did some research and also /sys/firmware/ seems to be empty which seems to be required by the gpio.

Not sure if a different distribution that contains the missing file /sys/firmware/ will fix the issue but I was using Ubuntu 22.04 Docker distribution.

I also tried to mount /sys/ or /sys/firmware/ but it is a docker limitation to block the mounting to /sys/firmware/ so it’s always stay empty unless you have privileged mode.
