saned: foreground standalone mode leaves avahi process running on exit

If saned is run with -l to operate as a standalone server but without daemonisation, it cannot be terminated cleanly if the Avahi service has been spawned. In producing a fix for this I also noticed some other related errors in the management of child processes.

The main issue is a failure to register the necessary signal handlers when not also daemonising. So the main issue does not affect launching via initscript; it also does not affect socket activation, e.g. via systemd or inetd. I noticed this issue with the runit supervision system. This would also affect anyone choosing to launch via systemd but without using socket activation.

To reproduce:

Terminal A

$ sudo frontend/saned -e -d 128 -l -u saned
[saned] read_config: searching for config file
[saned] read_config: done reading config
[saned] saned (AF-indep+IPv6) from sane-backends 1.3.1.66-5c327-dirty starting up
[saned] do_bindings: trying to get port for service "sane-port" (getaddrinfo)
[saned] do_bindings: [1] socket () using IPv6
[saned] do_bindings: [1] setsockopt ()
[saned] do_bindings: [1] bind () to port 6566
[saned] do_bindings: [1] listen ()
[saned] do_bindings: [0] socket () using IPv4
[saned] do_bindings: [0] setsockopt ()
[saned] do_bindings: [0] bind () to port 6566
[saned] do_bindings: [0] bind failed: Address already in use
[saned] Dropped privileges to uid 114 gid 121
[saned] run_standalone: spawning Avahi process
[saned] run_standalone: waiting for control connection
[saned] saned_avahi_callback: AVAHI_CLIENT_S_RUNNING
[saned] saned_create_avahi_services: adding service 'saned'
[saned] saned_avahi_group_callback: service 'saned' successfully established
Terminated

Terminal B

$ sudo ps auwwx | grep saned
root       39495  0.0  0.0  17008  7488 pts/0    S+   01:37   0:00 sudo SANE_CONFIG_DIR=/etc/sane.d frontend/.libs/saned -e -d 128 -l -u saned
root       39496  0.0  0.0  17008  2628 pts/2    Ss   01:37   0:00 sudo SANE_CONFIG_DIR=/etc/sane.d frontend/.libs/saned -e -d 128 -l -u saned
saned      39497  0.0  0.0  46056  7488 pts/2    S+   01:37   0:00 frontend/.libs/saned -e -d 128 -l -u saned
saned      39498  0.0  0.0  46056  2148 pts/2    S+   01:37   0:00 frontend/.libs/saned -e -d 128 -l -u saned
luser      39500  0.0  0.0   9084  2496 pts/1    S+   01:37   0:00 grep --color=auto saned
$ sudo kill 39497
$ sudo ps auwwx | grep saned
saned      39498  0.0  0.0  46056  2148 ?        S    01:37   0:00 frontend/.libs/saned -e -d 128 -l -u saned
luser      39507  0.0  0.0   9084  2304 pts/1    S+   01:37   0:00 grep --color=auto saned

Note that the rogue subprocess remains and the 'bail_out' diagnostics that would be expected are absent.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information