X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafs-supervisor.c;h=1e92b9f3424dabc964b204f6e77d251d2d5bafea;hb=9e8fda2954d951ea819dbddfe5afe466b2306074;hp=09df3639e606caa7ea1aa1240ce6c70a374b4525;hpb=b8c9d5de384efcfa53ebdb3f0053d7b3723777e1;p=src%2Fapp-framework-binder.git diff --git a/src/afs-supervisor.c b/src/afs-supervisor.c index 09df3639..1e92b9f3 100644 --- a/src/afs-supervisor.c +++ b/src/afs-supervisor.c @@ -36,7 +36,7 @@ #include #include "afs-supervision.h" -#include "afb-common.h" +#include "afb-systemd.h" #include "afb-session.h" #include "afb-cred.h" #include "afb-stub-ws.h" @@ -45,10 +45,13 @@ #include "afb-api-so-v2.h" #include "afb-api-ws.h" #include "afb-apiset.h" +#include "afb-fdev.h" #include "jobs.h" #include "verbose.h" #include "wrap-json.h" +extern void afs_discover(const char *pattern, void (*callback)(void *closure, pid_t pid), void *closure); + /* supervised items */ struct supervised { @@ -63,7 +66,8 @@ struct supervised }; /* api and apiset name */ -static const char supervision_apiname[] = AFS_SURPERVISION_APINAME; +static const char supervision_apiname[] = AFS_SURPERVISION_APINAME_INTERNAL; +static const char supervisor_apiname[] = "supervisor"; /* the main apiset */ struct afb_apiset *main_apiset; @@ -190,13 +194,20 @@ static void on_supervised_hangup(struct afb_stub_ws *stub) static int make_supervised(int fd, struct afb_cred *cred) { struct supervised *s; + struct fdev *fdev; s = malloc(sizeof *s); if (!s) return -1; + fdev = afb_fdev_create(fd); + if (!fdev) { + free(s); + return -1; + } + s->cred = cred; - s->stub = afb_stub_ws_create_client(fd, supervision_apiname, empty_apiset); + s->stub = afb_stub_ws_create_client(fdev, supervision_apiname, empty_apiset); if (!s->stub) { free(s); return -1; @@ -209,6 +220,22 @@ static int make_supervised(int fd, struct afb_cred *cred) return 0; } +/** + * Search the supervised of 'pid', return it or NULL. + */ +static struct supervised *supervised_of_pid(pid_t pid) +{ + struct supervised *s; + + pthread_mutex_lock(&mutex); + s = superviseds; + while (s && pid != s->cred->pid) + s = s->next; + pthread_mutex_unlock(&mutex); + + return s; +} + /* * handles incoming connection on 'sock' */ @@ -251,6 +278,25 @@ static int listening(sd_event_source *src, int fd, uint32_t revents, void *closu return 0; } +/* + */ +static void discovered_cb(void *closure, pid_t pid) +{ + struct supervised *s; + + s = supervised_of_pid(pid); + if (!s) { + (*(int*)closure)++; + kill(pid, SIGHUP); + } +} + +static int discover_supervised() +{ + int n = 0; + afs_discover("afb-daemon", discovered_cb, &n); + return n; +} /** * initalize the supervision @@ -269,14 +315,14 @@ static int init(const char *spec) /* TODO check that value */ /* create the apisets */ - main_apiset = afb_apiset_create(supervision_apiname, 0); + main_apiset = afb_apiset_create(supervisor_apiname, 0); if (!main_apiset) { - ERROR("Can't create supervision's apiset"); + ERROR("Can't create supervisor's apiset"); return -1; } empty_apiset = afb_apiset_create(supervision_apiname, 0); if (!empty_apiset) { - ERROR("Can't create supervised apiset"); + ERROR("Can't create supervision apiset"); return -1; } @@ -301,7 +347,7 @@ static int init(const char *spec) } /* integrate the socket to the loop */ - rc = sd_event_add_io(afb_common_get_event_loop(), + rc = sd_event_add_io(afb_systemd_get_event_loop(), NULL, fd, EPOLLIN, listening, NULL); if (rc < 0) { @@ -332,6 +378,8 @@ static void start(int signum, void *arg) exit(1); sd_notify(1, "READY=1"); + + discover_supervised(); } /** @@ -339,6 +387,7 @@ static void start(int signum, void *arg) */ int main(int ac, char **av) { + verbosity = Verbosity_Level_Debug; /* enter job processing */ jobs_start(3, 0, 10, start, av[1]); WARNING("hoops returned from jobs_enter! [report bug]"); @@ -374,6 +423,12 @@ static void f_list(struct afb_req req) afb_req_success(req, resu, NULL); } +static void f_discover(struct afb_req req) +{ + discover_supervised(); + afb_req_success(req, NULL, NULL); +} + static void propagate(struct afb_req req, const char *verb) { struct afb_xreq *xreq; @@ -394,9 +449,7 @@ static void propagate(struct afb_req req, const char *verb) afb_xreq_fail(xreq, "bad-pid", NULL); return; } - s = superviseds; - while (s && p != (int)s->cred->pid) - s = s->next; + s = supervised_of_pid((pid_t)p); if (!s) { afb_req_fail(req, "unknown-pid", NULL); return; @@ -413,6 +466,11 @@ static void f_do(struct afb_req req) propagate(req, NULL); } +static void f_config(struct afb_req req) +{ + propagate(req, NULL); +} + static void f_trace(struct afb_req req) { propagate(req, NULL); @@ -459,6 +517,13 @@ static const struct afb_verb_v2 _afb_verbs_v2_supervision[] = { .info = NULL, .session = AFB_SESSION_NONE_V2 }, + { + .verb = "config", + .callback = f_config, + .auth = &_afb_auths_v2_supervision[0], + .info = NULL, + .session = AFB_SESSION_NONE_V2 + }, { .verb = "do", .callback = f_do, @@ -508,11 +573,18 @@ static const struct afb_verb_v2 _afb_verbs_v2_supervision[] = { .info = NULL, .session = AFB_SESSION_NONE_V2 }, + { + .verb = "discover", + .callback = f_discover, + .auth = &_afb_auths_v2_supervision[0], + .info = NULL, + .session = AFB_SESSION_NONE_V2 + }, { .verb = NULL } }; static const struct afb_binding_v2 _afb_binding_v2_supervision = { - .api = supervision_apiname, + .api = supervisor_apiname, .specification = NULL, .info = NULL, .verbs = _afb_verbs_v2_supervision,