Make status common
[src/app-framework-binder.git] / src / main.c
index 01c0609..b932868 100644 (file)
  */
 
 #define _GNU_SOURCE
-#define NO_BINDING_VERBOSE_MACRO
+#define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdint.h>
+#include <signal.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
 
 #include <json-c/json.h>
 
-#include <systemd/sd-event.h>
 #include <systemd/sd-daemon.h>
 
-#include <afb/afb-binding.h>
-
 #include "afb-config.h"
 #include "afb-hswitch.h"
 #include "afb-apiset.h"
 #include "afb-api-dbus.h"
 #include "afb-api-ws.h"
 #include "afb-hsrv.h"
-#include "afb-context.h"
 #include "afb-hreq.h"
 #include "afb-xreq.h"
-#include "afb-cred.h"
 #include "jobs.h"
 #include "afb-session.h"
 #include "verbose.h"
 #include "afb-common.h"
+#include "afb-monitor.h"
 #include "afb-hook.h"
 #include "sd-fds.h"
 
@@ -115,7 +113,6 @@ static void exit_handler()
                killpg(0, SIGTERM);
        else if (childpid > 0)
                killpg(childpid, SIGTERM);
-       exit(0);
 }
 
 static void on_sigterm(int signum, siginfo_t *info, void *uctx)
@@ -450,11 +447,11 @@ struct startup_req
        struct afb_session *session;
 };
 
-static void startup_call_reply(struct afb_xreq *xreq, int iserror, struct json_object *obj)
+static void startup_call_reply(struct afb_xreq *xreq, int status, struct json_object *obj)
 {
        struct startup_req *sreq = CONTAINER_OF_XREQ(struct startup_req, xreq);
 
-       if (!iserror)
+       if (status >= 0)
                NOTICE("startup call %s returned %s", sreq->current->value, json_object_get_string(obj));
        else {
                ERROR("startup call %s ERROR! %s", sreq->current->value, json_object_get_string(obj));
@@ -471,7 +468,6 @@ static void startup_call_unref(struct afb_xreq *xreq)
        free(sreq->api);
        free(sreq->verb);
        json_object_put(sreq->xreq.json);
-       afb_cred_unref(sreq->xreq.cred);
        sreq->current = sreq->current->next;
        if (sreq->current)
                startup_call_current(sreq);
@@ -497,11 +493,9 @@ static void startup_call_current(struct startup_req *sreq)
        if (verb) {
                json = strchr(verb, ':');
                if (json) {
-                       memset(&sreq->xreq, 0, sizeof sreq->xreq);
                        afb_xreq_init(&sreq->xreq, &startup_xreq_itf);
                        afb_context_init(&sreq->xreq.context, sreq->session, NULL);
                        sreq->xreq.context.validated = 1;
-                       sreq->xreq.cred = afb_cred_current();
                        sreq->api = strndup(api, verb - api);
                        sreq->verb = strndup(verb + 1, json - verb - 1);
                        sreq->xreq.api = sreq->api;
@@ -535,10 +529,15 @@ static void run_startup_calls()
  | job for starting the daemon
  +--------------------------------------------------------- */
 
-static void start()
+static void start(int signum)
 {
        struct afb_hsrv *hsrv;
 
+       if (signum) {
+               ERROR("start aborted: received signal %s", strsignal(signum));
+               exit(1);
+       }
+
        // ------------------ sanity check ----------------------------------------
        if (config->httpdPort <= 0) {
                ERROR("no port is defined");
@@ -557,14 +556,18 @@ static void start()
        }
 
        /* configure the daemon */
-       main_apiset = afb_apiset_create("main", config->apiTimeout, NULL);
+       afb_session_init(config->nbSessionMax, config->cntxTimeout, config->token);
+       if (!afb_hreq_init_cookie(config->httpdPort, config->rootapi, config->cntxTimeout)) {
+               ERROR("initialisation of cookies failed");
+               goto error;
+       }
+       main_apiset = afb_apiset_create("main", config->apiTimeout);
        if (!main_apiset) {
                ERROR("can't create main api set");
                goto error;
        }
-       afb_session_init(config->nbSessionMax, config->cntxTimeout, config->token);
-       if (!afb_hreq_init_cookie(config->httpdPort, config->rootapi, config->cntxTimeout)) {
-               ERROR("initialisation of cookies failed");
+       if (afb_monitor_init() < 0) {
+               ERROR("failed to setup monitor");
                goto error;
        }
 
@@ -573,6 +576,10 @@ static void start()
                afb_hook_create_xreq(NULL, NULL, NULL, config->tracereq, NULL, NULL);
        if (config->traceditf)
                afb_hook_create_ditf(NULL, config->traceditf, NULL, NULL);
+       if (config->tracesvc)
+               afb_hook_create_svc(NULL, config->tracesvc, NULL, NULL);
+       if (config->traceevt)
+               afb_hook_create_evt(NULL, config->traceevt, NULL, NULL);
 
        /* load bindings */
        apiset_start_list(config->dbus_clients, afb_api_dbus_add_client, "the afb-dbus client");