2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <systemd/sd-event.h>
32 #include <systemd/sd-daemon.h>
34 #include <uuid/uuid.h>
35 #include <json-c/json.h>
36 #include <afb/afb-binding-v2.h>
38 #include "afs-supervision.h"
39 #include "afb-common.h"
40 #include "afb-session.h"
42 #include "afb-stub-ws.h"
45 #include "afb-api-so-v2.h"
46 #include "afb-api-ws.h"
47 #include "afb-apiset.h"
50 #include "wrap-json.h"
52 extern void afs_discover(const char *pattern, void (*callback)(void *closure, pid_t pid), void *closure);
54 /* supervised items */
57 /* link to the next supervised */
58 struct supervised *next;
60 /* credentials of the supervised */
61 struct afb_cred *cred;
63 /* connection with the supervised */
64 struct afb_stub_ws *stub;
67 /* api and apiset name */
68 static const char supervision_apiname[] = AFS_SURPERVISION_APINAME_INTERNAL;
69 static const char supervisor_apiname[] = "supervisor";
72 struct afb_apiset *main_apiset;
74 /* the empty apiset */
75 static struct afb_apiset *empty_apiset;
77 /* supervision socket path */
78 static const char supervision_socket_path[] = AFS_SURPERVISION_SOCKET;
81 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
83 /* list of supervised daemons */
84 static struct supervised *superviseds;
86 /*************************************************************************************/
88 static int afb_init_supervision_api();
90 /*************************************************************************************/
93 * Creates the supervisor socket for 'path' and return it
94 * return -1 in case of failure
96 static int create_supervision_socket(const char *path)
99 struct sockaddr_un addr;
102 /* check the path's length */
103 length = strlen(path);
105 ERROR("Path name of supervision socket too long: %d", (int)length);
106 errno = ENAMETOOLONG;
110 /* create a socket */
111 fd = socket(AF_UNIX, SOCK_STREAM, 0);
113 ERROR("Can't create socket: %m");
117 /* setup the bind to a path */
118 memset(&addr, 0, sizeof addr);
119 addr.sun_family = AF_UNIX;
120 strcpy(addr.sun_path, path);
121 if (addr.sun_path[0] == '@')
122 addr.sun_path[0] = 0; /* abstract sockets */
126 /* binds the socket to the path */
127 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
129 ERROR("can't bind socket to %s", path);
137 * send on 'fd' an initiator with 'command'
138 * return 0 on success or -1 on failure
140 static int send_initiator(int fd, const char *command)
144 struct afs_supervision_initiator asi;
147 memset(&asi, 0, sizeof asi);
148 strcpy(asi.interface, AFS_SURPERVISION_INTERFACE_1);
150 strncpy(asi.extra, command, sizeof asi.extra - 1);
152 /* send the initiator */
153 swr = write(fd, &asi, sizeof asi);
155 ERROR("Can't send initiator: %m");
157 } else if (swr < sizeof asi) {
158 ERROR("Sending incomplete initiator: %m");
166 * checks whether the incomming supervised represented by its creds
167 * is to be accepted or not.
168 * return 1 if yes or 0 otherwise.
170 static int should_accept(struct afb_cred *cred)
172 return cred && cred->pid != getpid(); /* not me! */
175 static void on_supervised_hangup(struct afb_stub_ws *stub)
177 struct supervised *s, **ps;
178 pthread_mutex_lock(&mutex);
180 while ((s = *ps) && s->stub != stub)
184 afb_stub_ws_unref(stub);
186 pthread_mutex_unlock(&mutex);
190 * create a supervised for socket 'fd' and 'cred'
191 * return 0 in case of success or -1 in case of error
193 static int make_supervised(int fd, struct afb_cred *cred)
195 struct supervised *s;
197 s = malloc(sizeof *s);
202 s->stub = afb_stub_ws_create_client(fd, supervision_apiname, empty_apiset);
207 pthread_mutex_lock(&mutex);
208 s->next = superviseds;
210 pthread_mutex_unlock(&mutex);
211 afb_stub_ws_on_hangup(s->stub, on_supervised_hangup);
216 * Search the supervised of 'pid', return it or NULL.
218 static struct supervised *supervised_of_pid(pid_t pid)
220 struct supervised *s;
222 pthread_mutex_lock(&mutex);
224 while (s && pid != s->cred->pid)
226 pthread_mutex_unlock(&mutex);
232 * handles incoming connection on 'sock'
234 static void accept_supervision_link(int sock)
237 struct sockaddr addr;
239 struct afb_cred *cred;
241 lenaddr = (socklen_t)sizeof addr;
242 fd = accept(sock, &addr, &lenaddr);
244 cred = afb_cred_create_for_socket(fd);
245 rc = should_accept(cred);
247 rc = send_initiator(fd, NULL);
249 rc = make_supervised(fd, cred);
254 afb_cred_unref(cred);
260 * handle even on server socket
262 static int listening(sd_event_source *src, int fd, uint32_t revents, void *closure)
264 if ((revents & EPOLLIN) != 0)
265 accept_supervision_link(fd);
266 if ((revents & EPOLLHUP) != 0) {
267 ERROR("supervision socket closed");
275 static void discovered_cb(void *closure, pid_t pid)
277 struct supervised *s;
279 s = supervised_of_pid(pid);
284 static void discover_supervised()
286 afs_discover("afb-daemon", discovered_cb, NULL);
290 * initalize the supervision
292 static int init(const char *spec)
298 ERROR("invalid socket spec");
302 rc = afb_session_init(100, 600, "");
303 /* TODO check that value */
305 /* create the apisets */
306 main_apiset = afb_apiset_create(supervisor_apiname, 0);
308 ERROR("Can't create supervisor's apiset");
311 empty_apiset = afb_apiset_create(supervision_apiname, 0);
313 ERROR("Can't create supervision apiset");
318 /* init the main apiset */
319 rc = afb_init_supervision_api();
321 ERROR("Can't create supervision's apiset: %m");
325 /* create the supervision socket */
326 fd = create_supervision_socket(supervision_socket_path);
330 /* listen the socket */
333 ERROR("refused to listen on socket");
337 /* integrate the socket to the loop */
338 rc = sd_event_add_io(afb_common_get_event_loop(),
342 ERROR("handling socket event isn't possible");
346 /* adds the server socket */
347 rc = afb_api_ws_add_server(spec, main_apiset);
349 ERROR("can't start the server socket");
355 /* start should not be null but */
356 static void start(int signum, void *arg)
368 sd_notify(1, "READY=1");
370 discover_supervised();
374 * initalize the supervision
376 int main(int ac, char **av)
378 verbosity = Verbosity_Level_Debug;
379 /* enter job processing */
380 jobs_start(3, 0, 10, start, av[1]);
381 WARNING("hoops returned from jobs_enter! [report bug]");
385 /*********************************************************************************************************/
387 static struct afb_binding_data_v2 datav2;
389 static void f_list(struct afb_req req)
392 struct json_object *resu, *item;
393 struct supervised *s;
395 resu = json_object_new_object();
398 sprintf(pid, "%d", (int)s->cred->pid);
400 wrap_json_pack(&item, "{si si si ss ss ss}",
401 "pid", (int)s->cred->pid,
402 "uid", (int)s->cred->uid,
403 "gid", (int)s->cred->gid,
405 "label", s->cred->label,
406 "user", s->cred->user
408 json_object_object_add(resu, pid, item);
411 afb_req_success(req, resu, NULL);
414 static void propagate(struct afb_req req, const char *verb)
416 struct afb_xreq *xreq;
417 struct json_object *args, *item;
418 struct supervised *s;
422 xreq = xreq_from_request(req.closure);
423 args = afb_xreq_json(xreq);
424 if (!json_object_object_get_ex(args, "pid", &item)) {
425 afb_xreq_fail(xreq, "no-pid", NULL);
429 p = json_object_get_int(item);
431 afb_xreq_fail(xreq, "bad-pid", NULL);
434 s = supervised_of_pid((pid_t)p);
436 afb_req_fail(req, "unknown-pid", NULL);
439 json_object_object_del(args, "pid");
441 xreq->request.verb = verb;
442 api = afb_stub_ws_client_api(s->stub);
443 api.itf->call(api.closure, xreq);
446 static void f_do(struct afb_req req)
448 propagate(req, NULL);
451 static void f_config(struct afb_req req)
453 propagate(req, NULL);
456 static void f_trace(struct afb_req req)
458 propagate(req, NULL);
461 static void f_sessions(struct afb_req req)
463 propagate(req, "slist");
466 static void f_session_close(struct afb_req req)
468 propagate(req, "sclose");
471 static void f_exit(struct afb_req req)
473 propagate(req, NULL);
476 static void f_debug_wait(struct afb_req req)
478 propagate(req, "wait");
481 static void f_debug_break(struct afb_req req)
483 propagate(req, "break");
486 static const struct afb_auth _afb_auths_v2_supervision[] = {
489 .type = afb_auth_Permission,
490 .text = "urn:AGL:permission:#supervision:platform:access"
494 static const struct afb_verb_v2 _afb_verbs_v2_supervision[] = {
498 .auth = &_afb_auths_v2_supervision[0],
500 .session = AFB_SESSION_NONE_V2
504 .callback = f_config,
505 .auth = &_afb_auths_v2_supervision[0],
507 .session = AFB_SESSION_NONE_V2
512 .auth = &_afb_auths_v2_supervision[0],
514 .session = AFB_SESSION_NONE_V2
519 .auth = &_afb_auths_v2_supervision[0],
521 .session = AFB_SESSION_NONE_V2
525 .callback = f_sessions,
526 .auth = &_afb_auths_v2_supervision[0],
528 .session = AFB_SESSION_NONE_V2
531 .verb = "session-close",
532 .callback = f_session_close,
533 .auth = &_afb_auths_v2_supervision[0],
535 .session = AFB_SESSION_NONE_V2
540 .auth = &_afb_auths_v2_supervision[0],
542 .session = AFB_SESSION_NONE_V2
545 .verb = "debug-wait",
546 .callback = f_debug_wait,
547 .auth = &_afb_auths_v2_supervision[0],
549 .session = AFB_SESSION_NONE_V2
552 .verb = "debug-break",
553 .callback = f_debug_break,
554 .auth = &_afb_auths_v2_supervision[0],
556 .session = AFB_SESSION_NONE_V2
561 static const struct afb_binding_v2 _afb_binding_v2_supervision = {
562 .api = supervisor_apiname,
563 .specification = NULL,
565 .verbs = _afb_verbs_v2_supervision,
572 static int afb_init_supervision_api()
574 return afb_api_so_v2_add_binding(&_afb_binding_v2_supervision, NULL, main_apiset, &datav2);