Rename project -> pfixtools.
[apps/pfixtools.git] / postlicyd.c
index 03675ad..051e83b 100644 (file)
@@ -1,5 +1,5 @@
 /******************************************************************************/
-/*          postlicyd: a postfix policy daemon with a lot of features         */
+/*          pfixtools: a collection of postfix related tools                  */
 /*          ~~~~~~~~~                                                         */
 /*  ________________________________________________________________________  */
 /*                                                                            */
 /******************************************************************************/
 
 /*
- * Copyright © 2006 Pierre Habouzit
+ * Copyright © 2006-2007 Pierre Habouzit
  */
 
-#include <stdbool.h>
-#include <stdio.h>
-#include <sysexits.h>
-#include <syslog.h>
+#include <signal.h>
+#include <time.h>
+#include <getopt.h>
 
-#include "job.h"
+#include "common.h"
 
-bool cleanexit = false;
+volatile int nbthreads = 0;
 
-void shutdown(void)
+static int main_initialize(void)
+{
+    openlog("postlicyd", LOG_PID, LOG_MAIL);
+    signal(SIGPIPE, SIG_IGN);
+    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(sock, NULL, 0);
+        pthread_attr_t attr;
+        pthread_t dummy;
+
+        if (fd < 0) {
+            if (errno != EINTR || errno != EAGAIN)
+                UNIXERR("accept");
+            continue;
+        }
+
+        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)
 {
     syslog(LOG_INFO, cleanexit ? "Stopping..." : "Unclean exit...");
-    job_shutdown();
     closelog();
 }
 
+module_init(main_initialize);
+module_exit(main_shutdown);
+
 int main(void)
 {
-    if (atexit(shutdown)) {
+    if (atexit(common_shutdown)) {
         fputs("Cannot hook my atexit function, quitting !\n", stderr);
-        return EX_CONFIG;
+        return EXIT_FAILURE;
     }
 
-    openlog("postlicyd", LOG_PID, LOG_MAIL);
-    job_initialize();
-    syslog(LOG_INFO, "Starting...");
-
-    job_loop();
-
-    cleanexit = true;
-    shutdown();
-    return 0;
+    common_initialize();
+    main_loop();
+    return EXIT_SUCCESS;
 }