X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-supervision.c;h=fa678ee9f5aed56b24734c6d34dca0d47932be10;hb=0bae7b4ed23310d368bdd2e0b167d8283bced4a0;hp=a7099a4a87a50ee3698bc535f38531c5b3b81e3e;hpb=b8c9d5de384efcfa53ebdb3f0053d7b3723777e1;p=src%2Fapp-framework-binder.git diff --git a/src/afb-supervision.c b/src/afb-supervision.c index a7099a4a..fa678ee9 100644 --- a/src/afb-supervision.c +++ b/src/afb-supervision.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016, 2017 "IoT.bzh" + * Copyright (C) 2016, 2017, 2018 "IoT.bzh" * Author José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -38,18 +38,24 @@ #include "afb-xreq.h" #include "afb-trace.h" #include "afb-session.h" +#include "afb-config.h" #include "afb-supervision.h" #include "afs-supervision.h" #include "afb-stub-ws.h" #include "afb-debug.h" +#include "afb-fdev.h" #include "verbose.h" #include "wrap-json.h" +#include "jobs.h" + +extern struct afb_config *main_config; /* api and apiset name */ -static const char supervision_apiname[] = AFS_SURPERVISION_APINAME; +static const char supervision_apiname[] = AFS_SUPERVISION_APINAME; +static const char supervisor_apiname[] = AFS_SUPERVISOR_APINAME; /* path of the supervision socket */ -static const char supervisor_socket_path[] = AFS_SURPERVISION_SOCKET; +static const char supervisor_socket_path[] = AFS_SUPERVISION_SOCKET; /* mutual exclusion */ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; @@ -108,20 +114,24 @@ static int open_supervisor_socket(const char *path) return fd; } -static void cleanup_supervisor(void *nada) +static void disconnect_supervisor() { - struct afb_trace *t = __atomic_exchange_n(&trace, NULL, __ATOMIC_RELAXED); + struct afb_stub_ws *s; + struct afb_trace *t; + + INFO("Disconnecting supervision"); + s = __atomic_exchange_n(&supervisor, NULL, __ATOMIC_RELAXED); + t = __atomic_exchange_n(&trace, NULL, __ATOMIC_RELAXED); + if (s) + afb_stub_ws_unref(s); if (t) afb_trace_unref(t); - supervisor = NULL; } -static void disconnect_supervisor() +static void on_supervisor_hangup(struct afb_stub_ws *s) { - struct afb_stub_ws *s = __atomic_exchange_n(&supervisor, NULL, __ATOMIC_RELAXED); - - if (s) - afb_stub_ws_unref(s); + if (s && s == supervisor) + disconnect_supervisor(); } /* try to connect to supervisor */ @@ -130,6 +140,7 @@ static void try_connect_supervisor() int fd; ssize_t srd; struct afs_supervision_initiator initiator; + struct fdev *fdev; /* get the mutex */ pthread_mutex_lock(&mutex); @@ -165,7 +176,7 @@ static void try_connect_supervisor() ERROR("Bad interface of supervisor %s", supervisor_socket_path); goto end2; } - if (strcmp(initiator.interface, AFS_SURPERVISION_INTERFACE_1)) { + if (strcmp(initiator.interface, AFS_SUPERVISION_INTERFACE_1)) { ERROR("Unknown interface %s for supervisor %s", initiator.interface, supervisor_socket_path); goto end2; } @@ -187,11 +198,17 @@ static void try_connect_supervisor() } /* make the supervisor link */ - supervisor = afb_stub_ws_create_server(fd, supervision_apiname, supervision_apiset); + fdev = afb_fdev_create(fd); + if (!fdev) { + ERROR("Creation of fdev failed: %m"); + goto end2; + } + supervisor = afb_stub_ws_create_server(fdev, supervision_apiname, supervision_apiset); if (!supervisor) { ERROR("Creation of supervisor failed: %m"); - goto end2; + goto end; } + afb_stub_ws_on_hangup(supervisor, on_supervisor_hangup); /* successful termination */ goto end; @@ -202,11 +219,18 @@ end: pthread_mutex_unlock(&mutex); } -static void on_sighup(int signum) +static void try_connect_supervisor_job(int signum, void *args) { + INFO("Try to connect supervisor after SIGHUP"); try_connect_supervisor(); } +static void on_sighup(int signum) +{ + INFO("Supervision received a SIGHUP"); + jobs_queue(NULL, 0, try_connect_supervisor_job, NULL); +} + /** * initalize the supervision */ @@ -265,9 +289,8 @@ static void slist(void *closure, struct afb_session *session) ******************************************************************************/ static const char *verbs[] = { - "break", "do", "exit", "sclose", "slist", "trace", "wait" }; -enum { Break , Do , Exit , Sclose , Slist , Trace , Wait }; - + "break", "config", "do", "exit", "sclose", "slist", "trace", "wait" }; +enum { Break , Config , Do , Exit , Sclose , Slist , Trace , Wait }; static void on_supervision_call(void *closure, struct afb_xreq *xreq) { @@ -293,7 +316,7 @@ static void on_supervision_call(void *closure, struct afb_xreq *xreq) i = 0; if (wrap_json_unpack(args, "i", &i)) wrap_json_unpack(args, "{si}", "code", &i); - ERROR("existing from supervision with code %d -> %d", i, i & 127); + ERROR("exiting from supervision with code %d -> %d", i, i & 127); exit(i & 127); break; case Sclose: @@ -319,11 +342,15 @@ static void on_supervision_call(void *closure, struct afb_xreq *xreq) afb_session_foreach(slist, list); afb_xreq_success(xreq, list, NULL); break; + case Config: + afb_xreq_success(xreq, afb_config_json(main_config), NULL); + break; case Trace: if (!trace) - trace = afb_trace_create(supervision_apiname, NULL /* not bound to any session */); + trace = afb_trace_create(supervisor_apiname, NULL /* not bound to any session */); - req = afb_xreq_unstore((struct afb_stored_req*)xreq); + req = xreq_to_req(xreq); + add = drop = NULL; wrap_json_unpack(args, "{s?o s?o}", "add", &add, "drop", &drop); if (add) { rc = afb_trace_add(req, add, trace);