Pazpar2 works as Windows Service.
[pazpar2-moved-to-github.git] / src / pazpar2.c
index 7eecacb..e3ab4d7 100644 (file)
@@ -31,6 +31,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "database.h"
 #include "settings.h"
 #include <yaz/daemon.h>
+#include <yaz/sc.h>
 
 void child_handler(void *data)
 {
@@ -81,7 +82,7 @@ static int tcpip_init (void)
 #endif
 
 
-int main(int argc, char **argv)
+static int sc_main(yaz_sc_t s, int argc, char **argv)
 {
     int daemon = 0;
     int ret;
@@ -89,7 +90,10 @@ int main(int argc, char **argv)
     char *arg;
     const char *pidfile = 0;
     const char *uid = 0;
+    int i;
 
+    for (i = 0; i < argc; i++)
+        yaz_log(YLOG_LOG, "arg%d: %s", i, argv[i]);
 #ifndef WIN32
     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
@@ -151,7 +155,7 @@ int main(int argc, char **argv)
                     "    -V                      show version\n"
                     "    -X                      debug mode\n"
                 );
-            exit(1);
+            return 1;
        }
     }
 
@@ -160,16 +164,21 @@ int main(int argc, char **argv)
     {
         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
                 "mode");
-        exit(1);
+        return 1;
     }
     if (!config)
     {
         yaz_log(YLOG_FATAL, "Load config with -f");
-        exit(1);
+        return 1;
     }
     global_parameters.server = config->servers;
 
-    start_http_listener();
+    ret = start_http_listener();
+    if (ret)
+        return ret; /* error starting http listener */
+
+    yaz_sc_running(s);
+
     yaz_daemon("pazpar2",
                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
@@ -179,6 +188,22 @@ int main(int argc, char **argv)
 }
 
 
+static void sc_stop(yaz_sc_t s)
+{
+    http_close_server();
+}
+
+int main(int argc, char **argv)
+{
+    int ret;
+    yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
+
+    ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
+
+    yaz_sc_destroy(&s);
+    exit(ret);
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4