Add signal handlers to shairplay to make it shut down gracefully
authorJuho Vähä-Herttua <juhovh@iki.fi>
Tue, 9 Apr 2013 09:40:26 +0000 (12:40 +0300)
committerJuho Vähä-Herttua <juhovh@iki.fi>
Tue, 9 Apr 2013 09:40:26 +0000 (12:40 +0300)
src/shairplay.c

index 9cccf030d0945d09143e9395e4c6aa2f34c66a1d..09210bf3d226d825b761b5cd36189766be34e406 100644 (file)
@@ -6,7 +6,7 @@
 #include <assert.h>
 
 #ifdef WIN32
-#include <windows.h>
+# include <windows.h>
 #endif
 
 #include <shairplay/dnssd.h>
@@ -39,6 +39,33 @@ typedef struct {
 
 static int running;
 
+#ifndef WIN32
+
+#include <signal.h>
+static void
+signal_handler(int sig)
+{
+       switch (sig) {
+       case SIGINT:
+       case SIGTERM:
+               running = 0;
+               break;
+       }
+}
+static void
+init_signals(void)
+{
+       struct sigaction sigact;
+
+       sigact.sa_handler = signal_handler;
+       sigemptyset(&sigact.sa_mask);
+       sigact.sa_flags = 0;
+       sigaction(SIGINT, &sigact, NULL);
+       sigaction(SIGTERM, &sigact, NULL);
+}
+
+#endif
+
 
 static ao_device *
 audio_open_device(shairplay_options_t *opt, int bits, int channels, int samplerate)
@@ -238,6 +265,10 @@ main(int argc, char *argv[])
 
        int error;
 
+#ifndef WIN32
+       init_signals();
+#endif
+
        memset(&options, 0, sizeof(options));
        if (parse_options(&options, argc, argv)) {
                return 0;