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.
18 #define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
26 #include <sys/types.h>
27 #include <sys/socket.h>
30 #include <json-c/json.h>
32 #define AFB_BINDING_VERSION 3
33 #include <afb/afb-binding.h>
37 #include "afb-apiset.h"
38 #include "afb-api-so-v2.h"
40 #include "afb-trace.h"
41 #include "afb-session.h"
42 #include "afb-config.h"
43 #include "afb-supervision.h"
44 #include "afs-supervision.h"
45 #include "afb-stub-ws.h"
46 #include "afb-debug.h"
49 #include "wrap-json.h"
52 extern struct afb_config *main_config;
54 /* api and apiset name */
55 static const char supervision_apiname[] = AFS_SUPERVISION_APINAME;
56 static const char supervisor_apiname[] = AFS_SUPERVISOR_APINAME;
58 /* path of the supervision socket */
59 static const char supervisor_socket_path[] = AFS_SUPERVISION_SOCKET;
61 /* mutual exclusion */
62 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
64 /* the standard apiset */
65 extern struct afb_apiset *main_apiset;
67 /* the supervision apiset (not exported) */
68 static struct afb_apiset *supervision_apiset;
70 /* local api implementation */
71 static void on_supervision_call(void *closure, struct afb_xreq *xreq);
72 static struct afb_api_itf supervision_api_itf =
74 .call = on_supervision_call
77 /* the supervisor link */
78 static struct afb_stub_ws *supervisor;
81 static struct afb_trace *trace;
84 static int open_supervisor_socket(const char *path)
87 struct sockaddr_un addr;
90 /* check path length */
91 length = strlen(path);
97 /* create the unix socket */
98 fd = socket(AF_UNIX, SOCK_STREAM, 0);
102 /* prepare the connection address */
103 memset(&addr, 0, sizeof addr);
104 addr.sun_family = AF_UNIX;
105 strcpy(addr.sun_path, path);
106 if (addr.sun_path[0] == '@')
107 addr.sun_path[0] = 0; /* implement abstract sockets */
109 /* connect the socket */
110 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
118 static void disconnect_supervisor()
120 struct afb_stub_ws *s;
123 INFO("Disconnecting supervision");
124 s = __atomic_exchange_n(&supervisor, NULL, __ATOMIC_RELAXED);
125 t = __atomic_exchange_n(&trace, NULL, __ATOMIC_RELAXED);
127 afb_stub_ws_unref(s);
132 static void on_supervisor_hangup(struct afb_stub_ws *s)
134 if (s && s == supervisor)
135 disconnect_supervisor();
138 /* try to connect to supervisor */
139 static void try_connect_supervisor()
143 struct afs_supervision_initiator initiator;
147 pthread_mutex_lock(&mutex);
149 /* needs to connect? */
150 if (supervisor || !supervision_apiset)
153 /* check existing path */
154 if (supervisor_socket_path[0] != '@' && access(supervisor_socket_path, F_OK)) {
155 NOTICE("Can't acces socket path %s: %m", supervisor_socket_path);
159 /* socket connection */
160 fd = open_supervisor_socket(supervisor_socket_path);
162 NOTICE("Can't connect supervision socket to %s: %m", supervisor_socket_path);
167 do { srd = read(fd, &initiator, sizeof initiator); } while(srd < 0 && errno == EINTR);
169 NOTICE("Can't read supervisor %s: %m", supervisor_socket_path);
172 if ((size_t)srd != sizeof initiator) {
173 ERROR("When reading supervisor %s: %m", supervisor_socket_path);
176 if (strnlen(initiator.interface, sizeof initiator.interface) == sizeof initiator.interface) {
177 ERROR("Bad interface of supervisor %s", supervisor_socket_path);
180 if (strcmp(initiator.interface, AFS_SUPERVISION_INTERFACE_1)) {
181 ERROR("Unknown interface %s for supervisor %s", initiator.interface, supervisor_socket_path);
184 if (strnlen(initiator.extra, sizeof initiator.extra) == sizeof initiator.extra) {
185 ERROR("Bad extra of supervisor %s", supervisor_socket_path);
189 /* interprets extras */
190 if (!strcmp(initiator.extra, "CLOSE")) {
191 INFO("Supervisor asks to CLOSE");
194 if (!strcmp(initiator.extra, "WAIT")) {
195 afb_debug_wait("supervisor");
197 if (!strcmp(initiator.extra, "BREAK")) {
198 afb_debug_break("supervisor");
201 /* make the supervisor link */
202 fdev = afb_fdev_create(fd);
204 ERROR("Creation of fdev failed: %m");
207 supervisor = afb_stub_ws_create_server(fdev, supervision_apiname, supervision_apiset);
209 ERROR("Creation of supervisor failed: %m");
212 afb_stub_ws_on_hangup(supervisor, on_supervisor_hangup);
214 /* successful termination */
220 pthread_mutex_unlock(&mutex);
223 static void try_connect_supervisor_job(int signum, void *args)
225 INFO("Try to connect supervisor after SIGHUP");
226 try_connect_supervisor();
229 static void on_sighup(int signum)
231 INFO("Supervision received a SIGHUP");
232 jobs_queue(NULL, 0, try_connect_supervisor_job, NULL);
236 * initialize the supervision
238 int afb_supervision_init()
244 if (supervision_apiset)
247 /* create the apiset */
248 supervision_apiset = afb_apiset_create(supervision_apiname, 0);
249 if (!supervision_apiset) {
250 ERROR("Can't create supervision's apiset");
254 /* init the apiset */
255 rc = afb_apiset_add(supervision_apiset, supervision_apiname,
256 (struct afb_api_item){ .itf = &supervision_api_itf, .closure = NULL});
258 ERROR("Can't create supervision's apiset: %m");
259 afb_apiset_unref(supervision_apiset);
260 supervision_apiset = NULL;
265 memset(&sa, 0, sizeof sa);
266 sa.sa_handler = on_sighup;
267 rc = sigaction(SIGHUP, &sa, NULL);
269 ERROR("Can't connect supervision to SIGHUP: %m");
271 /* connect to supervision */
272 try_connect_supervisor();
276 /******************************************************************************
277 **** Implementation monitoring verbs
278 ******************************************************************************/
279 static void slist(void *closure, struct afb_session *session)
281 struct json_object *list = closure;
282 struct json_object *item;
284 wrap_json_pack(&item, "{ss}", "token", afb_session_token(session));
285 json_object_object_add(list, afb_session_uuid(session), item);
288 /******************************************************************************
289 **** Implementation monitoring verbs
290 ******************************************************************************/
292 static const char *verbs[] = {
293 "break", "config", "do", "exit", "sclose", "slist", "trace", "wait" };
294 enum { Break , Config , Do , Exit , Sclose , Slist , Trace , Wait };
296 static void on_supervision_call(void *closure, struct afb_xreq *xreq)
299 struct json_object *args, *drop, *add, *sub, *list;
300 const char *api, *verb, *uuid;
301 struct afb_session *session;
302 const struct afb_api_item *xapi;
305 /* search the verb */
306 i = (int)(sizeof verbs / sizeof *verbs);
307 while(--i >= 0 && strcasecmp(verbs[i], xreq->request.called_verb));
309 afb_xreq_reply_unknown_verb(xreq);
314 args = afb_xreq_json(xreq);
318 if (wrap_json_unpack(args, "i", &i))
319 wrap_json_unpack(args, "{si}", "code", &i);
320 ERROR("exiting from supervision with code %d -> %d", i, i & 127);
325 if (wrap_json_unpack(args, "s", &uuid))
326 wrap_json_unpack(args, "{ss}", "uuid", &uuid);
328 afb_xreq_reply(xreq, NULL, "invalid", NULL);
330 session = afb_session_search(uuid);
332 afb_xreq_reply(xreq, NULL, "not-found", NULL);
334 afb_session_close(session);
335 afb_session_unref(session);
337 afb_xreq_reply(xreq, NULL, NULL, NULL);
342 list = json_object_new_object();
343 afb_session_foreach(slist, list);
344 afb_xreq_reply(xreq, list, NULL, NULL);
347 afb_xreq_reply(xreq, afb_config_json(main_config), NULL, NULL);
351 trace = afb_trace_create(supervisor_apiname, NULL /* not bound to any session */);
353 req = xreq_to_req_x2(xreq);
355 wrap_json_unpack(args, "{s?o s?o}", "add", &add, "drop", &drop);
357 rc = afb_trace_add(req, add, trace);
362 rc = afb_trace_drop(req, drop, trace);
366 afb_req_success(req, NULL, NULL);
370 if (wrap_json_unpack(args, "{ss ss s?o*}", "api", &api, "verb", &verb, "args", &sub))
371 afb_xreq_reply(xreq, NULL, "error", "bad request");
373 xapi = afb_apiset_lookup_started(main_apiset, api, 1);
375 afb_xreq_reply_unknown_api(xreq);
377 afb_cred_unref(xreq->cred);
379 xreq->request.called_api = api;
380 xreq->request.called_verb = verb;
381 xreq->json = json_object_get(sub);
382 xapi->itf->call(xapi->closure, xreq);
383 json_object_put(args);
388 afb_xreq_reply(xreq, NULL, NULL, NULL);
389 afb_debug_wait("supervisor");
392 afb_xreq_reply(xreq, NULL, NULL, NULL);
393 afb_debug_break("supervisor");