Begin work on the greylist module.
[apps/pfixtools.git] / main-postlicyd.c
index b81a813..ecf60f0 100644 (file)
  * Copyright © 2006-2007 Pierre Habouzit
  */
 
-#include <signal.h>
-#include <time.h>
 #include <getopt.h>
 
-#include "common.h"
+#include "epoll.h"
+#include "threads.h"
 
-volatile int nbthreads = 0;
+#define DAEMON_NAME             "postlicyd"
+
+/* administrivia {{{ */
 
 static int main_initialize(void)
 {
@@ -52,55 +53,85 @@ static int main_initialize(void)
     return 0;
 }
 
-void *job_run(void *_fd)
+static void main_shutdown(void)
 {
-    int fd = (intptr_t)_fd;
+    closelog();
+}
+
+module_init(main_initialize);
+module_exit(main_shutdown);
 
+void usage(void)
+{
+    fputs("usage: "DAEMON_NAME" [options] config\n"
+          "\n"
+          "Options:\n"
+          "    -p <pidfile> file to write our pid to\n"
+         , stderr);
+}
+
+/* }}} */
+
+void *job_run(int fd, void *data)
+{
     close(fd);
     return NULL;
 }
 
-static void main_loop(void)
+static int main_loop(void)
 {
+    int exitcode = EXIT_SUCCESS;
     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);
+        thread_launch(job_run, fd, NULL);
+        threads_join();
     }
 
     close(sock);
+    return exitcode;
 }
 
-static void main_shutdown(void)
+int main(int argc, char *argv[])
 {
-    closelog();
-}
+    const char *pidfile = NULL;
+    int res;
+
+    for (int c = 0; (c = getopt(argc, argv, "h" "p:")) >= 0; ) {
+        switch (c) {
+          case 'p':
+            pidfile = optarg;
+            break;
+          default:
+            usage();
+            return EXIT_FAILURE;
+        }
+    }
 
-module_init(main_initialize);
-module_exit(main_shutdown);
+    if (argc - optind != 1) {
+        usage();
+        return EXIT_FAILURE;
+    }
 
-int main(void)
-{
-    if (atexit(common_shutdown)) {
-        fputs("Cannot hook my atexit function, quitting !\n", stderr);
+    if (pidfile_open(pidfile) < 0) {
+        syslog(LOG_CRIT, "unable to write pidfile %s", pidfile);
+        return EXIT_FAILURE;
+    }
+
+    if (daemon_detach() < 0) {
+        syslog(LOG_CRIT, "unable to fork");
         return EXIT_FAILURE;
     }
 
-    common_initialize();
-    main_loop();
-    syslog(LOG_INFO, cleanexit ? "Stopping..." : "Unclean exit...");
-    return EXIT_SUCCESS;
+    pidfile_refresh();
+    res = main_loop();
+    syslog(LOG_INFO, "Stopping...");
+    return res;
 }