Srs tcp_table(5) daemon.
[apps/pfixtools.git] / postlicyd.c
index bc8eeea..d7878d3 100644 (file)
 #include <time.h>
 #include <getopt.h>
 
-#include "postlicyd.h"
+#include "common.h"
 
-static bool cleanexit = false;
-static bool sigint = false;
+volatile int nbthreads = 0;
 
-static void main_sighandler(int sig)
-{
-    static time_t lastintr = 0;
-    time_t now = time(NULL);
-
-    switch (sig) {
-      case SIGINT:
-        if (sigint) {
-            if (now - lastintr >= 1)
-                break;
-        } else {
-            lastintr = now;
-            sigint   = true;
-        }
-        return;
-
-      case SIGTERM:
-        break;
-
-      default:
-        return;
-    }
-
-    syslog(LOG_ERR, "Killed...");
-    exit(-1);
-}
-
-static void main_initialize(void)
+static int main_initialize(void)
 {
     openlog("postlicyd", LOG_PID, LOG_MAIL);
     signal(SIGPIPE, SIG_IGN);
-    signal(SIGINT,  &main_sighandler);
-    signal(SIGTERM, &main_sighandler);
+    signal(SIGINT,  &common_sighandler);
+    signal(SIGTERM, &common_sighandler);
     syslog(LOG_INFO, "Starting...");
+    return 0;
+}
+
+void *job_run(void *_fd)
+{
+    int fd = (intptr_t)_fd;
+
+    close(fd);
+    return NULL;
 }
 
 static void main_loop(void)
 {
+    int sock = -1;
+
     while (!sigint) {
-        int fd = accept(-1, NULL, 0);
+        int fd = accept(sock, NULL, 0);
+        pthread_attr_t attr;
+        pthread_t dummy;
 
         if (fd < 0) {
-            if (errno == EINTR || errno == EAGAIN)
-                continue;
-            syslog(LOG_ERR, "accept error: %m");
-            return;
+            if (errno != EINTR || errno != EAGAIN)
+                UNIXERR("accept");
+            continue;
         }
 
-        //pthread_create(NULL, NULL, job_run, (intptr_t)fd);
+        pthread_attr_init(&attr);
+        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+        pthread_create(&dummy, &attr, job_run, (void *)(intptr_t)fd);
+        pthread_attr_destroy(&attr);
     }
+
+    cleanexit = true;
+    close(sock);
 }
 
 static void main_shutdown(void)
@@ -100,16 +90,17 @@ static void main_shutdown(void)
     closelog();
 }
 
+module_init(main_initialize);
+module_exit(main_shutdown);
+
 int main(void)
 {
-    if (atexit(main_shutdown)) {
+    if (atexit(common_shutdown)) {
         fputs("Cannot hook my atexit function, quitting !\n", stderr);
         return EXIT_FAILURE;
     }
 
-    main_initialize();
+    common_initialize();
     main_loop();
-    cleanexit = true;
-    main_shutdown();
     return EXIT_SUCCESS;
 }