]> git.meshlink.io Git - catta/commitdiff
publish example: exit cleanly on SIGINT, SIGTERM
authorSven M. Hallberg <pesco@khjk.org>
Tue, 14 Oct 2014 13:27:45 +0000 (15:27 +0200)
committerSven M. Hallberg <pesco@khjk.org>
Tue, 14 Oct 2014 13:27:45 +0000 (15:27 +0200)
examples/core-publish-service.c

index 8d6377e6b70becd99ab37487d09fa44061ccf411..1d5ce9d317b9df34ff91ad8df70d7d60694c8f7e 100644 (file)
@@ -24,6 +24,8 @@
 #include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <signal.h>
+#include <errno.h>
 #include <assert.h>
 
 #include <catta/core.h>
@@ -189,6 +191,14 @@ static void server_callback(CattaServer *s, CattaServerState state, CATTA_GCC_UN
     }
 }
 
+static void signal_exit(int signum) {
+    int errnosave = errno;
+    catta_simple_poll_quit(simple_poll);
+    errno = errnosave;
+
+    (void)signum; // ignore
+}
+
 int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char*argv[]) {
     CattaServerConfig config;
     CattaServer *server = NULL;
@@ -224,6 +234,10 @@ int main(CATTA_GCC_UNUSED int argc, CATTA_GCC_UNUSED char*argv[]) {
         goto fail;
     }
 
+    /* exit cleanly on signals */
+    signal(SIGINT, signal_exit);
+    signal(SIGTERM, signal_exit);
+
     /* Run the main loop */
     catta_simple_poll_loop(simple_poll);