A GPIO monitoring daemon for Raspberry Pi devices that uses libgpiod to watch for signals from a Mausberry Circuits switch and safely power off the system.
This is a fork of t-richards/mausberry-switch that replaces the legacy sysfs GPIO interface with the modern libgpiod library.
- libgpiod instead of sysfs: Uses the modern Linux GPIO character device API via libgpiod instead of the deprecated
/sys/class/gpiointerface. - No more glib dependency: The only runtime dependency is now
libgpiod2. - Proper signal handling: Uses
sigaction()andsig_atomic_tfor safe, portable signal handling. - Configuration file support: Reads pin numbers, shutdown command, and delay from
/etc/mausberry-switch.conf. - Hardened systemd service: Runs with
ProtectSystem=strict,NoNewPrivileges, and other security restrictions.
Build dependencies:
libgpiod-dev(>= 1.0)build-essentialdh-autoreconf
Runtime dependency:
libgpiod2(>= 1.0)
autoreconf -i -f
./configure
make
sudo make installDownload the latest release (v0.9.1) and install it directly on your Pi:
# Download
wget https://github.com/tomcek42/mausberry-switch-libgpiod/releases/download/v0.9.1/mausberry-switch_0.9.1_armhf.deb
# Install
sudo dpkg -i mausberry-switch_0.9.1_armhf.deb
sudo apt-get -f installThe mausberry-switch systemd service will be automatically enabled and started when you install the package.
# Stop the service temporarily
sudo systemctl stop mausberry-switch
# Disable the service from automatically starting at boot
sudo systemctl disable mausberry-switchAll options are in /etc/mausberry-switch.conf:
[Pins]
Out=23
In=24
[Config]
ShutdownCommand=systemctl poweroff
Delay=0| Option | Default | Description |
|---|---|---|
Out |
23 | GPIO pin connected to the "out" lead |
In |
24 | GPIO pin connected to the "in" lead |
ShutdownCommand |
systemctl poweroff |
Command executed when the switch is toggled |
Delay |
0 | Seconds to wait before executing the shutdown command |
After changing the configuration, restart the service:
sudo systemctl restart mausberry-switchThe official Mausberry setup script polls GPIO state in a bash loop:
while [ 1 = 1 ]; do
cat /sys/class/gpio/gpio$GPIOpin1/value
sleep 1
doneThis daemon instead uses libgpiod's event-based API to wait for GPIO edge events without burning CPU cycles. The process sleeps until the kernel signals a GPIO state change.
The Linux kernel's sysfs GPIO interface (/sys/class/gpio) has been deprecated since Linux 4.8. libgpiod is the recommended replacement, providing:
- A stable, well-defined API
- Proper resource management (no manual export/unexport)
- Edge event detection without
poll(2)on file descriptors - Better error handling and diagnostics
Available as open source under the terms of the MIT License.