From 0c55afdb315f12b0f2a37f37a3ec711c75507c0d Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Tue, 19 May 2009 16:44:39 +0200 Subject: Let the daemon terminate only when no more device is attached --- src/main.c | 45 ++++++++++++++++++++++++++++++--------------- udev/85-usbmuxd.rules.in | 2 +- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/main.c b/src/main.c index b4b5283..3617e14 100644 --- a/src/main.c +++ b/src/main.c @@ -1006,18 +1006,6 @@ static int daemonize() return 0; } -/** - * signal handler function for cleaning up properly - */ -static void clean_exit(int sig) -{ - if (sig == SIGINT) { - if (verbose >= 1) - fprintf(stderr, "CTRL+C pressed\n"); - } - quit_flag = 1; -} - static void usage() { printf("usage: usbmuxd [options]\n"); @@ -1092,6 +1080,33 @@ static int devices_attached() return res; } +/** + * signal handler function for cleaning up properly + */ +static void handle_signal(int sig) +{ + if (sig == SIGTERM) { + quit_flag = 1; + } else { + if (sig == SIGINT) { + if (verbose >= 1) + fprintf(stderr, "CTRL+C pressed\n"); + } + + if (verbose >= 1) + fprintf(stderr, "Checking if we can terminate (no more devices attached)...\n"); + + if (devices_attached() > 0) { + // we can't quit, there are still devices attached. + if (verbose >= 1) + fprintf(stderr, "Refusing to terminate, there are still devices attached. Kill me with signal 15 (TERM) to force quit.\n"); + } else { + // it's safe to quit + quit_flag = 1; + } + } +} + /** * main function. Initializes all stuff and then loops waiting in accept. */ @@ -1121,9 +1136,9 @@ int main(int argc, char **argv) logmsg(LOG_NOTICE, "starting"); // signal(SIGHUP, reload_conf); // none yet - signal(SIGINT, clean_exit); - signal(SIGQUIT, clean_exit); - signal(SIGTERM, clean_exit); + signal(SIGINT, handle_signal); + signal(SIGQUIT, handle_signal); + signal(SIGTERM, handle_signal); signal(SIGPIPE, SIG_IGN); // check for other running instance diff --git a/udev/85-usbmuxd.rules.in b/udev/85-usbmuxd.rules.in index e661ccd..7820a36 100644 --- a/udev/85-usbmuxd.rules.in +++ b/udev/85-usbmuxd.rules.in @@ -31,7 +31,7 @@ ACTION=="add", SUBSYSTEM=="usb_endpoint", KERNEL=="usbdev*_ep85", SYMLINK+="usbm # Start and stop 'usbmuxd' as required. ACTION=="add", SUBSYSTEM=="usb_endpoint", KERNEL=="usbdev*_ep85", RUN+="/sbin/start-stop-daemon --start --oknodo --exec @prefix@/sbin/usbmuxd" -ACTION=="remove", SUBSYSTEM=="usb_endpoint", KERNEL=="usbdev*_ep85", RUN+="/sbin/start-stop-daemon --stop --signal 2 --exec @prefix@/sbin/usbmuxd" +ACTION=="remove", SUBSYSTEM=="usb_endpoint", KERNEL=="usbdev*_ep85", RUN+="/sbin/start-stop-daemon --stop --signal 3 --exec @prefix@/sbin/usbmuxd" # skip LABEL="usbmuxd_rules_end" -- cgit v1.1-32-gdbae