2 * Copyright (C) 2016, 2017, 2018 "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.
19 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <json-c/json.h>
32 #include <afb/afb-binding-v2.h>
36 #include "afb-apiset.h"
37 #include "afb-api-so-v2.h"
39 #include "afb-trace.h"
40 #include "afb-session.h"
41 #include "afb-config.h"
42 #include "afb-supervision.h"
43 #include "afs-supervision.h"
44 #include "afb-stub-ws.h"
45 #include "afb-debug.h"
48 #include "wrap-json.h"
51 extern struct afb_config *main_config;
53 /* api and apiset name */
54 static const char supervision_apiname[] = AFS_SUPERVISION_APINAME;
55 static const char supervisor_apiname[] = AFS_SUPERVISOR_APINAME;
57 /* path of the supervision socket */
58 static const char supervisor_socket_path[] = AFS_SUPERVISION_SOCKET;
60 /* mutual exclusion */
61 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
63 /* the standard apiset */
64 extern struct afb_apiset *main_apiset;
66 /* the supervision apiset (not exported) */
67 static struct afb_apiset *supervision_apiset;
69 /* local api implementation */
70 static void on_supervision_call(void *closure, struct afb_xreq *xreq);
71 static struct afb_api_itf supervision_api_itf =
73 .call = on_supervision_call
76 /* the supervisor link */
77 static struct afb_stub_ws *supervisor;
80 static struct afb_trace *trace;
83 static int open_supervisor_socket(const char *path)
86 struct sockaddr_un addr;
89 /* check path length */
90 length = strlen(path);
96 /* create the unix socket */
97 fd = socket(AF_UNIX, SOCK_STREAM, 0);
101 /* prepare the connection address */
102 memset(&addr, 0, sizeof addr);
103 addr.sun_family = AF_UNIX;
104 strcpy(addr.sun_path, path);
105 if (addr.sun_path[0] == '@')
106 addr.sun_path[0] = 0; /* implement abstract sockets */
108 /* connect the socket */
109 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
117 static void disconnect_supervisor()
119 struct afb_stub_ws *s;
122 INFO("Disconnecting supervision");
123 s = __atomic_exchange_n(&supervisor, NULL, __ATOMIC_RELAXED);
124 t = __atomic_exchange_n(&trace, NULL, __ATOMIC_RELAXED);
126 afb_stub_ws_unref(s);
131 static void on_supervisor_hangup(struct afb_stub_ws *s)
133 if (s && s == supervisor)
134 disconnect_supervisor();
137 /* try to connect to supervisor */
138 static void try_connect_supervisor()
142 struct afs_supervision_initiator initiator;
146 pthread_mutex_lock(&mutex);
148 /* needs to connect? */
149 if (supervisor || !supervision_apiset)
152 /* check existing path */
153 if (supervisor_socket_path[0] != '@' && access(supervisor_socket_path, F_OK)) {
154 NOTICE("Can't acces socket path %s: %m", supervisor_socket_path);
158 /* socket connection */
159 fd = open_supervisor_socket(supervisor_socket_path);
161 NOTICE("Can't connect supervision socket to %s: %m", supervisor_socket_path);
166 do { srd = read(fd, &initiator, sizeof initiator); } while(srd < 0 && errno == EINTR);
168 NOTICE("Can't read supervisor %s: %m", supervisor_socket_path);
171 if ((size_t)srd != sizeof initiator) {
172 ERROR("When reading supervisor %s: %m", supervisor_socket_path);
175 if (strnlen(initiator.interface, sizeof initiator.interface) == sizeof initiator.interface) {
176 ERROR("Bad interface of supervisor %s", supervisor_socket_path);
179 if (strcmp(initiator.interface, AFS_SUPERVISION_INTERFACE_1)) {
180 ERROR("Unknown interface %s for supervisor %s", initiator.interface, supervisor_socket_path);
183 if (strnlen(initiator.extra, sizeof initiator.extra) == sizeof initiator.extra) {
184 ERROR("Bad extra of supervisor %s", supervisor_socket_path);
188 /* interprets extras */
189 if (!strcmp(initiator.extra, "CLOSE")) {
190 INFO("Supervisor asks to CLOSE");
193 if (!strcmp(initiator.extra, "WAIT")) {
194 afb_debug_wait("supervisor");
196 if (!strcmp(initiator.extra, "BREAK")) {
197 afb_debug_break("supervisor");
200 /* make the supervisor link */
201 fdev = afb_fdev_create(fd);
203 ERROR("Creation of fdev failed: %m");
206 supervisor = afb_stub_ws_create_server(fdev, supervision_apiname, supervision_apiset);
208 ERROR("Creation of supervisor failed: %m");
211 afb_stub_ws_on_hangup(supervisor, on_supervisor_hangup);
213 /* successful termination */
219 pthread_mutex_unlock(&mutex);
222 static void try_connect_supervisor_job(int signum, void *args)
224 INFO("Try to connect supervisor after SIGHUP");
225 try_connect_supervisor();
228 static void on_sighup(int signum)
230 INFO("Supervision received a SIGHUP");
231 jobs_queue(NULL, 0, try_connect_supervisor_job, NULL);
235 * initalize the supervision
237 int afb_supervision_init()
243 if (supervision_apiset)
246 /* create the apiset */
247 supervision_apiset = afb_apiset_create(supervision_apiname, 0);
248 if (!supervision_apiset) {
249 ERROR("Can't create supervision's apiset");
253 /* init the apiset */
254 rc = afb_apiset_add(supervision_apiset, supervision_apiname,
255 (struct afb_api){ .itf = &supervision_api_itf, .closure = NULL});
257 ERROR("Can't create supervision's apiset: %m");
258 afb_apiset_unref(supervision_apiset);
259 supervision_apiset = NULL;
264 memset(&sa, 0, sizeof sa);
265 sa.sa_handler = on_sighup;
266 rc = sigaction(SIGHUP, &sa, NULL);
268 ERROR("Can't connect supervision to SIGHUP: %m");
270 /* connect to supervision */
271 try_connect_supervisor();
275 /******************************************************************************
276 **** Implementation monitoring verbs
277 ******************************************************************************/
278 static void slist(void *closure, struct afb_session *session)
280 struct json_object *list = closure;
281 struct json_object *item;
283 wrap_json_pack(&item, "{ss}", "token", afb_session_token(session));
284 json_object_object_add(list, afb_session_uuid(session), item);
287 /******************************************************************************
288 **** Implementation monitoring verbs
289 ******************************************************************************/
291 static const char *verbs[] = {
292 "break", "config", "do", "exit", "sclose", "slist", "trace", "wait" };
293 enum { Break , Config , Do , Exit , Sclose , Slist , Trace , Wait };
295 static void on_supervision_call(void *closure, struct afb_xreq *xreq)
298 struct json_object *args, *drop, *add, *sub, *list;
299 const char *api, *verb, *uuid;
300 struct afb_session *session;
301 const struct afb_api *xapi;
304 /* search the verb */
305 i = (int)(sizeof verbs / sizeof *verbs);
306 while(--i >= 0 && strcasecmp(verbs[i], xreq->request.verb));
308 afb_xreq_fail_unknown_verb(xreq);
313 args = afb_xreq_json(xreq);
317 if (wrap_json_unpack(args, "i", &i))
318 wrap_json_unpack(args, "{si}", "code", &i);
319 ERROR("exiting from supervision with code %d -> %d", i, i & 127);
324 if (wrap_json_unpack(args, "s", &uuid))
325 wrap_json_unpack(args, "{ss}", "uuid", &uuid);
327 afb_xreq_fail(xreq, "invalid", NULL);
329 session = afb_session_search(uuid);
331 afb_xreq_fail(xreq, "not-found", NULL);
333 afb_session_close(session);
334 afb_session_unref(session);
336 afb_xreq_success(xreq, NULL, NULL);
341 list = json_object_new_object();
342 afb_session_foreach(slist, list);
343 afb_xreq_success(xreq, list, NULL);
346 afb_xreq_success(xreq, afb_config_json(main_config), NULL);
350 trace = afb_trace_create(supervisor_apiname, NULL /* not bound to any session */);
352 req = xreq_to_req(xreq);
354 wrap_json_unpack(args, "{s?o s?o}", "add", &add, "drop", &drop);
356 rc = afb_trace_add(req, add, trace);
361 rc = afb_trace_drop(req, drop, trace);
365 afb_req_success(req, NULL, NULL);
369 if (wrap_json_unpack(args, "{ss ss s?o*}", "api", &api, "verb", &verb, "args", &sub))
370 afb_xreq_fail(xreq, "error", "bad request");
372 xapi = afb_apiset_lookup_started(main_apiset, api, 1);
374 afb_xreq_fail_unknown_api(xreq);
376 afb_cred_unref(xreq->cred);
378 xreq->request.api = api;
379 xreq->request.verb = verb;
380 xreq->json = json_object_get(sub);
381 xapi->itf->call(xapi->closure, xreq);
382 json_object_put(args);
387 afb_req_success(req, NULL, NULL);
388 afb_debug_wait("supervisor");
391 afb_req_success(req, NULL, NULL);
392 afb_debug_break("supervisor");