I am currently facing the following issue:
I have a snap with a daemon that spawns a few processes. I want to add a “stop-command” that executes another command before all these processes are terminated whenever the daemon is stopped. Now I found out, that the “stop-command” is not executed as long as I have the plugs “raw-usb” or “kubernetes-support” connected. I can find a workaround for “kubernetes-support”, but not for “raw-usb”.
I reproduced it with a minimum example: A snap that runs a daemon which spawns and kills a few dummy processes and logs that into a file in $SNAP_COMMON. Minimum snapcraft.yaml
:
name: test
base: core22
summary: Start/Stop Test
description: |
This snap tests starting/stopping services
confinement: strict
adopt-info: part-with-metadata
grade: devel
version: 0.0.1
parts:
scripts:
plugin: dump
source: ./dump/
source-type: local
apps:
daemon:
daemon: forking
command: test start
stop-command: test stop
reload-command: test restart
plugs:
- kubernetes-support
- raw-usb
- system-observe
The folder dump
contains a script test
that provides the start and stop commands:
#!/bin/bash
LOGFILE=$SNAP_COMMON/Test.log
echo "===========================================" >> $LOGFILE
echo "Execute ${0} ${@}" >> $LOGFILE
start_test() {
echo "Start Test Service" >> $LOGFILE
rm $SNAP_COMMON/*.pid
for i in {1..10}; do
sleep infinity &
PID=$(jobs -p | tail -n 1)
echo "PID $i: $PID" >> $LOGFILE
echo "$PID" > $SNAP_COMMON/Test_$i.pid
done
echo "Start Finished" >> $LOGFILE
}
stop_test(){
echo "Stop Test Service" >> $LOGFILE
for i in {1..10}; do
PID=$(cat $SNAP_COMMON/Test_$i.pid)
echo "PID $i: $PID" >> $LOGFILE
kill -s SIGTERM $PID
done
echo "Stop Finished" >> $LOGFILE
}
# Aktionen
case "$1" in
start)
start_test
;;
stop)
stop_test
;;
restart)
stop_test
start_test
;;
esac
echo "===========================================" >> $LOGFILE
exit 0
I tested that by switching between “sudo snap start test.daemon” and “sudo snap stop test.daemon”. Whenever either “raw-usb” or “kubernetes-support” are connected, I do not get any output in the log file.
I think it is related to the udev rules that are created in /etc/udev/rules.d/70-snap.test.rules
. If I uncomment a few lines as follows, the “stop-command” is executed:
# This file is automatically generated.
# kubernetes-support
#KERNEL=="kmsg", TAG+="snap_test_daemon"
# raw-usb
SUBSYSTEM=="tty", ENV{ID_BUS}=="usb", TAG+="snap_test_daemon"
# raw-usb
#SUBSYSTEM=="usb", TAG+="snap_test_daemon"
# raw-usb
#SUBSYSTEM=="usbmisc", TAG+="snap_test_daemon"
TAG=="snap_test_daemon", SUBSYSTEM!="module", SUBSYSTEM!="subsystem", RUN+="/usr/lib/snapd/snap-device-helper $env{ACTION} snap_test_daemon $devpath $major:$minor"
Further, if I disconnect the “raw-usb” interface, but re-add the udev rules above by hand (not uncommented), the “stop-command” is also not executed.
Is there any possibility I can have a “raw-usb” interface connected and the “stop-command” is executed? Many thanks in advance.