Beginning of integration as systemd service
[src/app-framework-binder.git] / src / main.c
index d2a0842..f1ebd96 100644 (file)
@@ -28,6 +28,9 @@
 #include <sys/wait.h>
 
 #include <systemd/sd-event.h>
+#include <systemd/sd-daemon.h>
+
+#include <afb/afb-binding.h>
 
 #include "afb-config.h"
 #include "afb-hswitch.h"
@@ -44,8 +47,7 @@
 #include "verbose.h"
 #include "afb-common.h"
 #include "afb-hook.h"
-
-#include <afb/afb-binding.h>
+#include "sd-fds.h"
 
 /*
    if SELF_PGROUP == 0 the launched command is the group leader
@@ -202,8 +204,8 @@ static struct afb_hsrv *start_http_server()
        int rc;
        struct afb_hsrv *hsrv;
 
-       if (afb_hreq_init_download_path("/tmp")) {      /* TODO: sessiondir? */
-               ERROR("unable to set the tmp directory");
+       if (afb_hreq_init_download_path(config->uploaddir)) {
+               ERROR("unable to set the upload directory %s", config->uploaddir);
                return NULL;
        }
 
@@ -220,8 +222,7 @@ static struct afb_hsrv *start_http_server()
                return NULL;
        }
 
-       NOTICE("Waiting port=%d rootdir=%s", config->httpdPort,
-              config->rootdir);
+       NOTICE("Waiting port=%d rootdir=%s", config->httpdPort, config->rootdir);
        NOTICE("Browser URL= http:/*localhost:%d", config->httpdPort);
 
        rc = afb_hsrv_start(hsrv, (uint16_t) config->httpdPort, 15);
@@ -359,6 +360,8 @@ int main(int argc, char *argv[])
 
        LOGAUTH("afb-daemon");
 
+       sd_fds_init();
+
        // ------------- Build session handler & init config -------
        config = afb_config_parse_arguments(argc, argv);
        atexit(exit_handler);
@@ -369,13 +372,20 @@ int main(int argc, char *argv[])
                exit(1);
        }
 
-       afb_session_init(config->nbSessionMax, config->cntxTimeout, config->token, afb_apis_count());
+       mkdir(config->workdir, S_IRWXU | S_IRGRP | S_IXGRP);
+       if (chdir(config->workdir) < 0) {
+               ERROR("Can't enter working dir %s", config->workdir);
+               exit(1);
+       }
 
        afb_api_so_set_timeout(config->apiTimeout);
        start_list(config->dbus_clients, afb_api_dbus_add_client, "the afb-dbus client");
        start_list(config->ws_clients, afb_api_ws_add_client, "the afb-websocket client");
        start_list(config->ldpaths, afb_api_so_add_pathset, "the binding path set");
        start_list(config->so_bindings, afb_api_so_add_binding, "the binding");
+
+       afb_session_init(config->nbSessionMax, config->cntxTimeout, config->token, afb_apis_count());
+
        start_list(config->dbus_servers, afb_api_dbus_add_server, "the afb-dbus service");
        start_list(config->ws_servers, afb_api_ws_add_server, "the afb-websocket service");
 
@@ -388,8 +398,8 @@ int main(int argc, char *argv[])
                ERROR("failed to initialise signal handlers");
                return 1;
        }
-       // if directory does not exist createit
-       mkdir(config->rootdir, O_RDWR | S_IRWXU | S_IRGRP);
+
+       // set the root dir
        if (afb_common_rootdir_set(config->rootdir) < 0) {
                ERROR("failed to set common root directory");
                return 1;
@@ -449,6 +459,7 @@ int main(int argc, char *argv[])
 
        // infinite loop
        eventloop = afb_common_get_event_loop();
+       sd_notify(1, "READY=1");
        for (;;)
                sd_event_run(eventloop, 30000000);