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.
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"
47 #include "wrap-json.h"
49 extern struct afb_config *main_config;
51 /* api and apiset name */
52 static const char supervision_apiname[] = AFS_SURPERVISION_APINAME_INTERNAL;
54 /* path of the supervision socket */
55 static const char supervisor_socket_path[] = AFS_SURPERVISION_SOCKET;
57 /* mutual exclusion */
58 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
60 /* the standard apiset */
61 extern struct afb_apiset *main_apiset;
63 /* the supervision apiset (not exported) */
64 static struct afb_apiset *supervision_apiset;
66 /* local api implementation */
67 static void on_supervision_call(void *closure, struct afb_xreq *xreq);
68 static struct afb_api_itf supervision_api_itf =
70 .call = on_supervision_call
73 /* the supervisor link */
74 static struct afb_stub_ws *supervisor;
77 static struct afb_trace *trace;
80 static int open_supervisor_socket(const char *path)
83 struct sockaddr_un addr;
86 /* check path length */
87 length = strlen(path);
93 /* create the unix socket */
94 fd = socket(AF_UNIX, SOCK_STREAM, 0);
98 /* prepare the connection address */
99 memset(&addr, 0, sizeof addr);
100 addr.sun_family = AF_UNIX;
101 strcpy(addr.sun_path, path);
102 if (addr.sun_path[0] == '@')
103 addr.sun_path[0] = 0; /* implement abstract sockets */
105 /* connect the socket */
106 rc = connect(fd, (struct sockaddr *) &addr, (socklen_t)(sizeof addr));
114 static void cleanup_supervisor(void *nada)
116 struct afb_trace *t = __atomic_exchange_n(&trace, NULL, __ATOMIC_RELAXED);
122 static void disconnect_supervisor()
124 struct afb_stub_ws *s = __atomic_exchange_n(&supervisor, NULL, __ATOMIC_RELAXED);
127 afb_stub_ws_unref(s);
130 /* try to connect to supervisor */
131 static void try_connect_supervisor()
135 struct afs_supervision_initiator initiator;
138 pthread_mutex_lock(&mutex);
140 /* needs to connect? */
141 if (supervisor || !supervision_apiset)
144 /* check existing path */
145 if (supervisor_socket_path[0] != '@' && access(supervisor_socket_path, F_OK)) {
146 NOTICE("Can't acces socket path %s: %m", supervisor_socket_path);
150 /* socket connection */
151 fd = open_supervisor_socket(supervisor_socket_path);
153 NOTICE("Can't connect supervision socket to %s: %m", supervisor_socket_path);
158 do { srd = read(fd, &initiator, sizeof initiator); } while(srd < 0 && errno == EINTR);
160 NOTICE("Can't read supervisor %s: %m", supervisor_socket_path);
163 if ((size_t)srd != sizeof initiator) {
164 ERROR("When reading supervisor %s: %m", supervisor_socket_path);
167 if (strnlen(initiator.interface, sizeof initiator.interface) == sizeof initiator.interface) {
168 ERROR("Bad interface of supervisor %s", supervisor_socket_path);
171 if (strcmp(initiator.interface, AFS_SURPERVISION_INTERFACE_1)) {
172 ERROR("Unknown interface %s for supervisor %s", initiator.interface, supervisor_socket_path);
175 if (strnlen(initiator.extra, sizeof initiator.extra) == sizeof initiator.extra) {
176 ERROR("Bad extra of supervisor %s", supervisor_socket_path);
180 /* interprets extras */
181 if (!strcmp(initiator.extra, "CLOSE")) {
182 INFO("Supervisor asks to CLOSE");
185 if (!strcmp(initiator.extra, "WAIT")) {
186 afb_debug_wait("supervisor");
188 if (!strcmp(initiator.extra, "BREAK")) {
189 afb_debug_break("supervisor");
192 /* make the supervisor link */
193 supervisor = afb_stub_ws_create_server(fd, supervision_apiname, supervision_apiset);
195 ERROR("Creation of supervisor failed: %m");
199 /* successful termination */
205 pthread_mutex_unlock(&mutex);
208 static void on_sighup(int signum)
210 try_connect_supervisor();
214 * initalize the supervision
216 int afb_supervision_init()
222 if (supervision_apiset)
225 /* create the apiset */
226 supervision_apiset = afb_apiset_create(supervision_apiname, 0);
227 if (!supervision_apiset) {
228 ERROR("Can't create supervision's apiset");
232 /* init the apiset */
233 rc = afb_apiset_add(supervision_apiset, supervision_apiname,
234 (struct afb_api){ .itf = &supervision_api_itf, .closure = NULL});
236 ERROR("Can't create supervision's apiset: %m");
237 afb_apiset_unref(supervision_apiset);
238 supervision_apiset = NULL;
243 memset(&sa, 0, sizeof sa);
244 sa.sa_handler = on_sighup;
245 rc = sigaction(SIGHUP, &sa, NULL);
247 ERROR("Can't connect supervision to SIGHUP: %m");
249 /* connect to supervision */
250 try_connect_supervisor();
254 /******************************************************************************
255 **** Implementation monitoring verbs
256 ******************************************************************************/
257 static void slist(void *closure, struct afb_session *session)
259 struct json_object *list = closure;
260 struct json_object *item;
262 wrap_json_pack(&item, "{ss}", "token", afb_session_token(session));
263 json_object_object_add(list, afb_session_uuid(session), item);
266 /******************************************************************************
267 **** Implementation monitoring verbs
268 ******************************************************************************/
270 static const char *verbs[] = {
271 "break", "config", "do", "exit", "sclose", "slist", "trace", "wait" };
272 enum { Break , Config , Do , Exit , Sclose , Slist , Trace , Wait };
275 static void on_supervision_call(void *closure, struct afb_xreq *xreq)
278 struct json_object *args, *drop, *add, *sub, *list;
279 const char *api, *verb, *uuid;
280 struct afb_session *session;
281 const struct afb_api *xapi;
284 /* search the verb */
285 i = (int)(sizeof verbs / sizeof *verbs);
286 while(--i >= 0 && strcasecmp(verbs[i], xreq->request.verb));
288 afb_xreq_fail_unknown_verb(xreq);
293 args = afb_xreq_json(xreq);
297 if (wrap_json_unpack(args, "i", &i))
298 wrap_json_unpack(args, "{si}", "code", &i);
299 ERROR("existing from supervision with code %d -> %d", i, i & 127);
304 if (wrap_json_unpack(args, "s", &uuid))
305 wrap_json_unpack(args, "{ss}", "uuid", &uuid);
307 afb_xreq_fail(xreq, "invalid", NULL);
309 session = afb_session_search(uuid);
311 afb_xreq_fail(xreq, "not-found", NULL);
313 afb_session_close(session);
314 afb_session_unref(session);
316 afb_xreq_success(xreq, NULL, NULL);
321 list = json_object_new_object();
322 afb_session_foreach(slist, list);
323 afb_xreq_success(xreq, list, NULL);
326 afb_xreq_success(xreq, afb_config_json(main_config), NULL);
330 trace = afb_trace_create(supervision_apiname, NULL /* not bound to any session */);
332 req = afb_xreq_unstore((struct afb_stored_req*)xreq);
333 wrap_json_unpack(args, "{s?o s?o}", "add", &add, "drop", &drop);
335 rc = afb_trace_add(req, add, trace);
340 rc = afb_trace_drop(req, drop, trace);
344 afb_req_success(req, NULL, NULL);
348 if (wrap_json_unpack(args, "{ss ss s?o*}", "api", &api, "verb", &verb, "args", &sub))
349 afb_xreq_fail(xreq, "error", "bad request");
351 xapi = afb_apiset_lookup_started(main_apiset, api, 1);
353 afb_xreq_fail_unknown_api(xreq);
355 afb_cred_unref(xreq->cred);
357 xreq->request.api = api;
358 xreq->request.verb = verb;
359 xreq->json = json_object_get(sub);
360 xapi->itf->call(xapi->closure, xreq);
361 json_object_put(args);
366 afb_req_success(req, NULL, NULL);
367 afb_debug_wait("supervisor");
370 afb_req_success(req, NULL, NULL);
371 afb_debug_break("supervisor");