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 /* supervised items */
55 /* link to the next supervised */
56 struct supervised *next;
58 /* credentials of the supervised */
59 struct afb_cred *cred;
61 /* connection with the supervised */
62 struct afb_stub_ws *stub;
65 /* api and apiset name */
66 static const char supervision_apiname[] = AFS_SURPERVISION_APINAME;
69 struct afb_apiset *main_apiset;
71 /* the empty apiset */
72 static struct afb_apiset *empty_apiset;
74 /* supervision socket path */
75 static const char supervision_socket_path[] = AFS_SURPERVISION_SOCKET;
78 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
80 /* list of supervised daemons */
81 static struct supervised *superviseds;
83 /*************************************************************************************/
85 static int afb_init_supervision_api();
87 /*************************************************************************************/
90 * Creates the supervisor socket for 'path' and return it
91 * return -1 in case of failure
93 static int create_supervision_socket(const char *path)
96 struct sockaddr_un addr;
99 /* check the path's length */
100 length = strlen(path);
102 ERROR("Path name of supervision socket too long: %d", (int)length);
103 errno = ENAMETOOLONG;
107 /* create a socket */
108 fd = socket(AF_UNIX, SOCK_STREAM, 0);
110 ERROR("Can't create socket: %m");
114 /* setup the bind to a path */
115 memset(&addr, 0, sizeof addr);
116 addr.sun_family = AF_UNIX;
117 strcpy(addr.sun_path, path);
118 if (addr.sun_path[0] == '@')
119 addr.sun_path[0] = 0; /* abstract sockets */
123 /* binds the socket to the path */
124 rc = bind(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
126 ERROR("can't bind socket to %s", path);
134 * send on 'fd' an initiator with 'command'
135 * return 0 on success or -1 on failure
137 static int send_initiator(int fd, const char *command)
141 struct afs_supervision_initiator asi;
144 memset(&asi, 0, sizeof asi);
145 strcpy(asi.interface, AFS_SURPERVISION_INTERFACE_1);
147 strncpy(asi.extra, command, sizeof asi.extra - 1);
149 /* send the initiator */
150 swr = write(fd, &asi, sizeof asi);
152 ERROR("Can't send initiator: %m");
154 } else if (swr < sizeof asi) {
155 ERROR("Sending incomplete initiator: %m");
163 * checks whether the incomming supervised represented by its creds
164 * is to be accepted or not.
165 * return 1 if yes or 0 otherwise.
167 static int should_accept(struct afb_cred *cred)
169 return cred && cred->pid != getpid(); /* not me! */
172 static void on_supervised_hangup(struct afb_stub_ws *stub)
174 struct supervised *s, **ps;
175 pthread_mutex_lock(&mutex);
177 while ((s = *ps) && s->stub != stub)
181 afb_stub_ws_unref(stub);
183 pthread_mutex_unlock(&mutex);
187 * create a supervised for socket 'fd' and 'cred'
188 * return 0 in case of success or -1 in case of error
190 static int make_supervised(int fd, struct afb_cred *cred)
192 struct supervised *s;
194 s = malloc(sizeof *s);
199 s->stub = afb_stub_ws_create_client(fd, supervision_apiname, empty_apiset);
204 pthread_mutex_lock(&mutex);
205 s->next = superviseds;
207 pthread_mutex_unlock(&mutex);
208 afb_stub_ws_on_hangup(s->stub, on_supervised_hangup);
213 * handles incoming connection on 'sock'
215 static void accept_supervision_link(int sock)
218 struct sockaddr addr;
220 struct afb_cred *cred;
222 lenaddr = (socklen_t)sizeof addr;
223 fd = accept(sock, &addr, &lenaddr);
225 cred = afb_cred_create_for_socket(fd);
226 rc = should_accept(cred);
228 rc = send_initiator(fd, NULL);
230 rc = make_supervised(fd, cred);
235 afb_cred_unref(cred);
241 * handle even on server socket
243 static int listening(sd_event_source *src, int fd, uint32_t revents, void *closure)
245 if ((revents & EPOLLIN) != 0)
246 accept_supervision_link(fd);
247 if ((revents & EPOLLHUP) != 0) {
248 ERROR("supervision socket closed");
256 * initalize the supervision
258 static int init(const char *spec)
264 ERROR("invalid socket spec");
268 rc = afb_session_init(100, 600, "");
269 /* TODO check that value */
271 /* create the apisets */
272 main_apiset = afb_apiset_create(supervision_apiname, 0);
274 ERROR("Can't create supervision's apiset");
277 empty_apiset = afb_apiset_create(supervision_apiname, 0);
279 ERROR("Can't create supervised apiset");
284 /* init the main apiset */
285 rc = afb_init_supervision_api();
287 ERROR("Can't create supervision's apiset: %m");
291 /* create the supervision socket */
292 fd = create_supervision_socket(supervision_socket_path);
296 /* listen the socket */
299 ERROR("refused to listen on socket");
303 /* integrate the socket to the loop */
304 rc = sd_event_add_io(afb_common_get_event_loop(),
308 ERROR("handling socket event isn't possible");
312 /* adds the server socket */
313 rc = afb_api_ws_add_server(spec, main_apiset);
315 ERROR("can't start the server socket");
321 /* start should not be null but */
322 static void start(int signum, void *arg)
334 sd_notify(1, "READY=1");
338 * initalize the supervision
340 int main(int ac, char **av)
342 /* enter job processing */
343 jobs_start(3, 0, 10, start, av[1]);
344 WARNING("hoops returned from jobs_enter! [report bug]");
348 /*********************************************************************************************************/
350 static struct afb_binding_data_v2 datav2;
352 static void f_list(struct afb_req req)
355 struct json_object *resu, *item;
356 struct supervised *s;
358 resu = json_object_new_object();
361 sprintf(pid, "%d", (int)s->cred->pid);
363 wrap_json_pack(&item, "{si si si ss ss ss}",
364 "pid", (int)s->cred->pid,
365 "uid", (int)s->cred->uid,
366 "gid", (int)s->cred->gid,
368 "label", s->cred->label,
369 "user", s->cred->user
371 json_object_object_add(resu, pid, item);
374 afb_req_success(req, resu, NULL);
377 static void propagate(struct afb_req req, const char *verb)
379 struct afb_xreq *xreq;
380 struct json_object *args, *item;
381 struct supervised *s;
385 xreq = xreq_from_request(req.closure);
386 args = afb_xreq_json(xreq);
387 if (!json_object_object_get_ex(args, "pid", &item)) {
388 afb_xreq_fail(xreq, "no-pid", NULL);
392 p = json_object_get_int(item);
394 afb_xreq_fail(xreq, "bad-pid", NULL);
398 while (s && p != (int)s->cred->pid)
401 afb_req_fail(req, "unknown-pid", NULL);
404 json_object_object_del(args, "pid");
406 xreq->request.verb = verb;
407 api = afb_stub_ws_client_api(s->stub);
408 api.itf->call(api.closure, xreq);
411 static void f_do(struct afb_req req)
413 propagate(req, NULL);
416 static void f_trace(struct afb_req req)
418 propagate(req, NULL);
421 static void f_sessions(struct afb_req req)
423 propagate(req, "slist");
426 static void f_session_close(struct afb_req req)
428 propagate(req, "sclose");
431 static void f_exit(struct afb_req req)
433 propagate(req, NULL);
436 static void f_debug_wait(struct afb_req req)
438 propagate(req, "wait");
441 static void f_debug_break(struct afb_req req)
443 propagate(req, "break");
446 static const struct afb_auth _afb_auths_v2_supervision[] = {
449 .type = afb_auth_Permission,
450 .text = "urn:AGL:permission:#supervision:platform:access"
454 static const struct afb_verb_v2 _afb_verbs_v2_supervision[] = {
458 .auth = &_afb_auths_v2_supervision[0],
460 .session = AFB_SESSION_NONE_V2
465 .auth = &_afb_auths_v2_supervision[0],
467 .session = AFB_SESSION_NONE_V2
472 .auth = &_afb_auths_v2_supervision[0],
474 .session = AFB_SESSION_NONE_V2
478 .callback = f_sessions,
479 .auth = &_afb_auths_v2_supervision[0],
481 .session = AFB_SESSION_NONE_V2
484 .verb = "session-close",
485 .callback = f_session_close,
486 .auth = &_afb_auths_v2_supervision[0],
488 .session = AFB_SESSION_NONE_V2
493 .auth = &_afb_auths_v2_supervision[0],
495 .session = AFB_SESSION_NONE_V2
498 .verb = "debug-wait",
499 .callback = f_debug_wait,
500 .auth = &_afb_auths_v2_supervision[0],
502 .session = AFB_SESSION_NONE_V2
505 .verb = "debug-break",
506 .callback = f_debug_break,
507 .auth = &_afb_auths_v2_supervision[0],
509 .session = AFB_SESSION_NONE_V2
514 static const struct afb_binding_v2 _afb_binding_v2_supervision = {
515 .api = supervision_apiname,
516 .specification = NULL,
518 .verbs = _afb_verbs_v2_supervision,
525 static int afb_init_supervision_api()
527 return afb_api_so_v2_add_binding(&_afb_binding_v2_supervision, NULL, main_apiset, &datav2);