I have a NetworkManager dispatcher script, which tries to start/stop systemd services. However, starting/stopping the service does not work from the dispatcher, while it works just fine (same command line) from the command line executed as root. Can anyone help me what’s going on?
The script `/etc/NetworkManager/dispatcher.d/toggle-syncthing-service.sh`
#!/bin/bash
set -eou pipefail
EXPECTED_INTERFACE="wlo1"
EXPECTED_INTERFACE_NAME="[censored]"
EXPECTED_INTERFACE_SSID="[censored]"
USERNAME="christian"
INTERFACE=$1
NM_ACTION=$2
if [[ -z ${INTERFACE:=} ]]
then
echo "No interface specified. Exiting."
fi
if [[ "${INTERFACE}" != "$EXPECTED_INTERFACE" ]]
then
echo "Found connection interface ${INTERFACE:=[undefined]} instead of $EXPECTED_INTERFACE, exiting."
exit
fi
if [[ "${CONNECTION_ID:=}" != "$EXPECTED_INTERFACE_NAME" ]]
then
echo "Found connection named ${CONNECTION_ID:=} instead of $EXPECTED_INTERFACE_NAME, exiting."
exit
fi
if [[ "${CONNECTION_UUID:=}" != "$EXPECTED_INTERFACE_SSID" ]]
then
echo "Found connection SSID ${CONNECTION_UUID:=} instead of $EXPECTED_INTERFACE_SSID, exiting."
exit
fi
case "$NM_ACTION" in
up)
echo "Starting syncthing as $USERNAME"
systemctl --user -M $USERNAME@ start syncthing.service
;;
down)
echo "Stopping syncthing…"
systemctl --user -M $USERNAME@ stop syncthing.service
;;
esac
Good case
When executing the commands directly as root,
systemctl --user -M christian@ start syncthing.service
systemctl --user -M christian@ start syncthing.service
they work as expected. No interesting logging.
Bad case
When executing the commands from NetworkManager dispatcher, the service does not start/stop. Instead, I get the following errors in syslog:
When starting:
nm-dispatcher[18473]: Starting syncthing as christian
nm-dispatcher[18475]: Failed to start transient service unit: Connection reset by peer
nm-dispatcher[18474]: Failed to start syncthing.service: Transport endpoint is not connected
nm-dispatcher[18474]: See user logs and 'systemctl --user status syncthing.service' for details.
nm-dispatcher[18441]: req:4 'up' [wlo1], "/etc/NetworkManager/dispatcher.d/toggle-syncthing-service.sh": complete: process failed with Script '/etc/NetworkManager/dispatcher.d/toggle-syncthing-service.sh' exited with status 1
NetworkManager[1003]: <warn> [1792178417.4367] dispatcher: (43) /etc/NetworkManager/dispatcher.d/toggle-syncthing-service.sh failed (failed): Script '/etc/NetworkManager/dispatcher.d/toggle-syncthing-service.sh' exited with status 1
When stopping:
nm-dispatcher[18349]: Stopping syncthing…
nm-dispatcher[18351]: Failed to start transient service unit: Connection reset by peer
nm-dispatcher[18350]: Failed to stop syncthing.service: Transport endpoint is not connected
nm-dispatcher[18350]: See user logs and 'systemctl --user status syncthing.service' for details.
nm-dispatcher[18331]: req:2 'down' [wlo1], "/etc/NetworkManager/dispatcher.d/toggle-syncthing-service.sh": complete: process failed with Script '/etc/NetworkManager/dispatcher.d/toggle-syncthing-service.sh' exited with status 1
NetworkManager[1003]: <warn> [1792178404.7480] dispatcher: (38) /etc/NetworkManager/dispatcher.d/toggle-syncthing-service.sh failed (failed): Script '/etc/NetworkManager/dispatcher.d/toggle-syncthing-service.sh' exited with status 1
In systemctl --user status syncthing.service, there is nothing. There is also nothing else in the syslogs, neither the system syslogs (as root) nor the user syslogs.
Is there something special about the NetworkManager dispatcher execution environment?
Is this a bug in systemd or NetworkManager?
Additional information
I extracted straces, but I cannot upload them here due to restrictions. Any suggested location where to upload them?