2 * Copyright (C) 2015-2018 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
33 #if !defined(NO_CALL_PERSONALITY)
34 #include <sys/personality.h>
37 #include <json-c/json.h>
39 #include <systemd/sd-daemon.h>
41 #include "afb-config.h"
42 #include "afb-hswitch.h"
43 #include "afb-apiset.h"
44 #include "afb-autoset.h"
45 #include "afb-api-so.h"
46 #if defined(WITH_DBUS_TRANSPARENCY)
47 # include "afb-api-dbus.h"
49 #include "afb-api-ws.h"
53 #include "afb-session.h"
55 #include "afb-common.h"
56 #include "afb-export.h"
57 #include "afb-monitor.h"
59 #include "afb-hook-flags.h"
60 #include "afb-debug.h"
61 #if defined(WITH_SUPERVISION)
62 # include "afb-supervision.h"
65 #include "process-name.h"
66 #include "wrap-json.h"
68 #include "sig-monitor.h"
71 if SELF_PGROUP == 0 the launched command is the group leader
72 if SELF_PGROUP != 0 afb-daemon is the group leader
76 struct afb_apiset *main_apiset;
77 struct json_object *main_config;
79 static pid_t childpid;
82 * Tiny helper around putenv: add the variable name=value
84 * @param name name of the variable to set
85 * @param value value to set to the variable
87 * @return 0 in case of success or -1 in case of error (with errno set to ENOMEM)
89 static int addenv(const char *name, const char *value)
93 head = malloc(2 + strlen(name) + strlen(value));
98 middle = stpcpy(head, name);
100 strcpy(&middle[1], value);
105 * Tiny helper around addenv that export the real path
107 * @param name name of the variable to set
108 * @param path the path value to export to the variable
110 * @return 0 in case of success or -1 in case of error (with errno set to ENOMEM)
112 static int addenv_realpath(const char *name, const char *path)
114 char buffer[PATH_MAX];
115 char *p = realpath(path, buffer);
116 return p ? addenv(name, p) : -1;
120 * Tiny helper around addenv that export an integer
122 * @param name name of the variable to set
123 * @param value the integer value to export
125 * @return 0 in case of success or -1 in case of error (with errno set to ENOMEM)
127 static int addenv_int(const char *name, int value)
130 snprintf(buffer, sizeof buffer, "%d", value);
131 return addenv(name, buffer);
134 /*----------------------------------------------------------
135 | helpers for handling list of arguments
136 +--------------------------------------------------------- */
138 static const char *run_for_config_array_opt(const char *name,
139 int (*run) (void *closure, const char *value),
143 struct json_object *array, *value;
145 if (json_object_object_get_ex(main_config, name, &array)) {
146 if (!json_object_is_type(array, json_type_array))
147 return json_object_get_string(array);
148 n = (int)json_object_array_length(array);
149 for (i = 0 ; i < n ; i++) {
150 value = json_object_array_get_idx(array, i);
151 rc = run(closure, json_object_get_string(value));
153 return json_object_get_string(value);
159 static int run_start(void *closure, const char *value)
161 int (*starter) (const char *value, struct afb_apiset *declare_set, struct afb_apiset *call_set) = closure;
162 return starter(value, main_apiset, main_apiset) >= 0;
165 static void apiset_start_list(const char *name,
166 int (*starter) (const char *value, struct afb_apiset *declare_set, struct afb_apiset *call_set),
169 const char *item = run_for_config_array_opt(name, run_start, starter);
171 ERROR("can't start %s %s", message, item);
176 /*----------------------------------------------------------
178 | Handles on exit specific actions
179 +--------------------------------------------------------- */
180 static void exit_handler()
182 struct sigaction siga;
184 memset(&siga, 0, sizeof siga);
185 siga.sa_handler = SIG_IGN;
186 sigaction(SIGTERM, &siga, NULL);
190 else if (childpid > 0)
191 killpg(childpid, SIGTERM);
194 static void on_sigterm(int signum, siginfo_t *info, void *uctx)
196 NOTICE("Received SIGTERM");
200 static void on_sighup(int signum, siginfo_t *info, void *uctx)
202 NOTICE("Received SIGHUP");
206 static void setup_daemon()
208 struct sigaction siga;
210 /* install signal handlers */
211 memset(&siga, 0, sizeof siga);
212 siga.sa_flags = SA_SIGINFO;
214 siga.sa_sigaction = on_sigterm;
215 sigaction(SIGTERM, &siga, NULL);
217 siga.sa_sigaction = on_sighup;
218 sigaction(SIGHUP, &siga, NULL);
221 atexit(exit_handler);
223 /* ignore any SIGPIPE */
224 signal(SIGPIPE, SIG_IGN);
227 /*----------------------------------------------------------
229 | set the process in background
230 +--------------------------------------------------------- */
231 static void daemonize()
233 int fd = 0, daemon, nostdin;
239 wrap_json_unpack(main_config, "{s?b s?s}", "daemon", &daemon, "output", &output);
243 fd = open(output, O_WRONLY | O_APPEND | O_CREAT, 0640);
245 ERROR("Can't open output %s", output);
251 INFO("entering background mode");
255 ERROR("Failed to fork daemon process");
264 /* closes the input */
266 NOTICE("Redirecting output to %s", output);
275 close(0); /* except if 'daemon', ctrl+C still works after that */
278 /*---------------------------------------------------------
280 | Handles the HTTP server
281 +--------------------------------------------------------- */
282 static int init_alias(void *closure, const char *spec)
284 struct afb_hsrv *hsrv = closure;
285 char *path = strchr(spec, ':');
288 ERROR("Missing ':' in alias %s. Alias ignored", spec);
292 INFO("Alias for url=%s to path=%s", spec, path);
293 return afb_hsrv_add_alias(hsrv, spec, afb_common_rootdir_get_fd(), path,
297 static int init_http_server(struct afb_hsrv *hsrv)
300 const char *rootapi, *roothttp, *rootbase;
303 rc = wrap_json_unpack(main_config, "{ss ss s?s}",
305 "rootbase", &rootbase,
306 "roothttp", &roothttp);
308 ERROR("Can't get HTTP server config");
312 if (!afb_hsrv_add_handler(hsrv, rootapi,
313 afb_hswitch_websocket_switch, main_apiset, 20))
316 if (!afb_hsrv_add_handler(hsrv, rootapi,
317 afb_hswitch_apis, main_apiset, 10))
320 if (run_for_config_array_opt("alias", init_alias, hsrv))
323 if (roothttp != NULL) {
324 if (!afb_hsrv_add_alias(hsrv, "",
325 afb_common_rootdir_get_fd(), roothttp, -10, 1))
329 if (!afb_hsrv_add_handler(hsrv, rootbase,
330 afb_hswitch_one_page_api_redirect, NULL, -20))
336 static struct afb_hsrv *start_http_server()
339 const char *uploaddir, *rootdir;
340 struct afb_hsrv *hsrv;
341 int cache_timeout, http_port;
343 rc = wrap_json_unpack(main_config, "{ss ss si si}",
344 "uploaddir", &uploaddir,
346 "cache-eol", &cache_timeout,
349 ERROR("Can't get HTTP server start config");
353 if (afb_hreq_init_download_path(uploaddir)) {
354 ERROR("unable to set the upload directory %s", uploaddir);
358 hsrv = afb_hsrv_create();
360 ERROR("memory allocation failure");
364 if (!afb_hsrv_set_cache_timeout(hsrv, cache_timeout)
365 || !init_http_server(hsrv)) {
366 ERROR("initialisation of httpd failed");
371 NOTICE("Waiting port=%d rootdir=%s", http_port, rootdir);
372 NOTICE("Browser URL= http://localhost:%d", http_port);
374 rc = afb_hsrv_start(hsrv, (uint16_t) http_port, 15);
376 ERROR("starting of httpd failed");
384 /*---------------------------------------------------------
386 +--------------------------------------------------------- */
388 static void on_sigchld(int signum, siginfo_t *info, void *uctx)
390 if (info->si_pid == childpid) {
391 switch (info->si_code) {
397 killpg(info->si_pid, SIGKILL);
398 waitpid(info->si_pid, NULL, 0);
410 #define SUBST_CHAR '@'
411 #define SUBST_STR "@"
413 static char *instanciate_string(const char *arg, const char *port, const char *token)
415 char *resu, *it, *wr;
418 /* get the changes */
421 it = strchrnul(arg, SUBST_CHAR);
424 case 'p': chg++; dif += (int)strlen(port) - 2; break;
425 case 't': chg++; dif += (int)strlen(token) - 2; break;
426 case SUBST_CHAR: it++; chg++; dif--; break;
429 it = strchrnul(it, SUBST_CHAR);
432 /* return arg when no change */
436 /* allocates the result */
437 resu = malloc((it - arg) + dif + 1);
439 ERROR("out of memory");
443 /* instanciate the arguments */
446 it = strchrnul(arg, SUBST_CHAR);
447 wr = mempcpy(wr, arg, it - arg);
451 case 'p': wr = stpcpy(wr, port); break;
452 case 't': wr = stpcpy(wr, token); break;
453 default: *wr++ = SUBST_CHAR; /*@fallthrough@*/
454 case SUBST_CHAR: *wr++ = *it;
463 static int instanciate_environ(const char *port, const char *token)
465 extern char **environ;
469 /* instantiate the environment */
470 for (i = 0 ; environ[i] ; i++) {
471 repl = instanciate_string(environ[i], port, token);
479 static char **instanciate_command_args(struct json_object *exec, const char *port, const char *token)
485 /* allocates the result */
486 n = (int)json_object_array_length(exec);
487 result = malloc((n + 1) * sizeof * result);
489 ERROR("out of memory");
493 /* instanciate the arguments */
494 for (i = 0 ; i < n ; i++) {
495 repl = instanciate_string(json_object_get_string(json_object_array_get_idx(exec, i)), port, token);
508 static int execute_command()
510 struct json_object *exec, *oport;
511 struct sigaction siga;
517 /* check whether a command is to execute or not */
518 if (!json_object_object_get_ex(main_config, "exec", &exec))
524 /* install signal handler */
525 memset(&siga, 0, sizeof siga);
526 siga.sa_sigaction = on_sigchld;
527 siga.sa_flags = SA_SIGINFO;
528 sigaction(SIGCHLD, &siga, NULL);
535 /* compute the string for port */
536 if (json_object_object_get_ex(main_config, "port", &oport))
537 rc = snprintf(port, sizeof port, "%s", json_object_get_string(oport));
539 rc = snprintf(port, sizeof port, "%cp", SUBST_CHAR);
540 if (rc < 0 || rc >= (int)(sizeof port)) {
541 ERROR("port->txt failed");
544 /* instantiate arguments and environment */
545 token = afb_session_initial_token();
546 args = instanciate_command_args(exec, port, token);
547 if (args && instanciate_environ(port, token) >= 0) {
551 execv(args[0], args);
552 ERROR("can't launch %s: %m", args[0]);
559 /*---------------------------------------------------------
561 +--------------------------------------------------------- */
565 struct afb_xreq xreq;
568 struct json_object *calls;
571 const char *callspec;
572 struct afb_session *session;
575 static void startup_call_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
577 struct startup_req *sreq = CONTAINER_OF_XREQ(struct startup_req, xreq);
581 NOTICE("startup call %s returned %s (%s)", sreq->callspec, json_object_get_string(object), info);
582 json_object_put(object);
584 ERROR("startup call %s ERROR! %s (%s)", sreq->callspec, error, info);
589 static void startup_call_current(struct startup_req *sreq);
591 static void startup_call_unref(struct afb_xreq *xreq)
593 struct startup_req *sreq = CONTAINER_OF_XREQ(struct startup_req, xreq);
597 json_object_put(sreq->xreq.json);
598 if (++sreq->index < sreq->count)
599 startup_call_current(sreq);
601 afb_session_close(sreq->session);
602 afb_session_unref(sreq->session);
607 static struct afb_xreq_query_itf startup_xreq_itf =
609 .reply = startup_call_reply,
610 .unref = startup_call_unref
613 static void startup_call_current(struct startup_req *sreq)
615 const char *api, *verb, *json;
616 enum json_tokener_error jerr;
618 sreq->callspec = json_object_get_string(json_object_array_get_idx(sreq->calls, sreq->index)),
619 api = sreq->callspec;
620 verb = strchr(api, '/');
622 json = strchr(verb, ':');
624 afb_xreq_init(&sreq->xreq, &startup_xreq_itf);
625 afb_context_init(&sreq->xreq.context, sreq->session, NULL);
626 sreq->xreq.context.validated = 1;
627 sreq->api = strndup(api, verb - api);
628 sreq->verb = strndup(verb + 1, json - verb - 1);
629 sreq->xreq.request.called_api = sreq->api;
630 sreq->xreq.request.called_verb = sreq->verb;
631 sreq->xreq.json = json_tokener_parse_verbose(json + 1, &jerr);
632 if (sreq->api && sreq->verb && jerr == json_tokener_success) {
633 afb_xreq_process(&sreq->xreq, main_apiset);
638 ERROR("Bad call specification %s", sreq->callspec);
642 static void run_startup_calls()
644 struct json_object *calls;
645 struct startup_req *sreq;
648 if (json_object_object_get_ex(main_config, "call", &calls)
649 && json_object_is_type(calls, json_type_array)
650 && (count = (int)json_object_array_length(calls))) {
651 sreq = calloc(1, sizeof *sreq);
652 sreq->session = afb_session_create(3600);
656 startup_call_current(sreq);
660 /*---------------------------------------------------------
661 | job for starting the daemon
662 +--------------------------------------------------------- */
664 static void start(int signum, void *arg)
666 const char *tracereq, *traceapi, *traceevt, *traceses, *tracesvc, *traceditf, *traceglob;
667 const char *workdir, *rootdir, *token, *rootapi;
668 struct json_object *settings;
669 struct afb_hsrv *hsrv;
670 int max_session_count, session_timeout, api_timeout;
671 int no_httpd, http_port;
675 afb_debug("start-entry");
678 ERROR("start aborted: received signal %s", strsignal(signum));
683 token = rootapi = tracesvc = traceditf = tracereq =
684 traceapi = traceevt = traceses = traceglob = NULL;
687 rc = wrap_json_unpack(main_config, "{"
692 #if !defined(REMOVE_LEGACY_TRACE)
695 "s?s s?s s?s s?s s?s"
702 "apitimeout", &api_timeout,
703 "cntxtimeout", &session_timeout,
704 "session-max", &max_session_count,
706 "no-httpd", &no_httpd,
711 #if !defined(REMOVE_LEGACY_TRACE)
712 "tracesvc", &tracesvc,
713 "traceditf", &traceditf,
715 "tracereq", &tracereq,
716 "traceapi", &traceapi,
717 "traceevt", &traceevt,
718 "traceses", &traceses,
719 "traceglob", &traceglob
722 ERROR("Unable to get start config");
726 /* initialize session handling */
727 if (afb_session_init(max_session_count, session_timeout, token)) {
728 ERROR("initialisation of session manager failed");
732 /* set the directories */
733 mkdir(workdir, S_IRWXU | S_IRGRP | S_IXGRP);
734 if (chdir(workdir) < 0) {
735 ERROR("Can't enter working dir %s", workdir);
738 if (afb_common_rootdir_set(rootdir) < 0) {
739 ERROR("failed to set common root directory %s", rootdir);
742 if (addenv_realpath("AFB_WORKDIR", "." /* resolved by realpath */)
743 || addenv_realpath("AFB_ROOTDIR", rootdir /* relative to current directory */)) {
744 ERROR("can't set DIR environment");
751 ERROR("no port is defined");
754 if (http_port == 0) {
755 ERROR("random port is not implemented");
758 if (addenv_int("AFB_PORT", http_port)
759 || addenv("AFB_TOKEN", afb_session_initial_token())) {
760 ERROR("can't set HTTP environment");
765 /* configure the daemon */
766 afb_export_set_config(settings);
767 main_apiset = afb_apiset_create("main", api_timeout);
769 ERROR("can't create main api set");
772 if (afb_monitor_init(main_apiset, main_apiset) < 0) {
773 ERROR("failed to setup monitor");
776 #if defined(WITH_SUPERVISION)
777 if (afb_supervision_init(main_apiset, main_config) < 0) {
778 ERROR("failed to setup supervision");
785 afb_hook_create_xreq(NULL, NULL, NULL, afb_hook_flags_xreq_from_text(tracereq), NULL, NULL);
786 #if !defined(REMOVE_LEGACY_TRACE)
787 if (traceapi || tracesvc || traceditf)
788 afb_hook_create_api(NULL, afb_hook_flags_api_from_text(traceapi)
789 | afb_hook_flags_legacy_ditf_from_text(traceditf)
790 | afb_hook_flags_legacy_svc_from_text(tracesvc), NULL, NULL);
793 afb_hook_create_api(NULL, afb_hook_flags_api_from_text(traceapi), NULL, NULL);
796 afb_hook_create_evt(NULL, afb_hook_flags_evt_from_text(traceevt), NULL, NULL);
798 afb_hook_create_session(NULL, afb_hook_flags_session_from_text(traceses), NULL, NULL);
800 afb_hook_create_global(afb_hook_flags_global_from_text(traceglob), NULL, NULL);
802 /* load bindings and apis */
803 afb_debug("start-load");
804 apiset_start_list("binding", afb_api_so_add_binding, "the binding");
805 apiset_start_list("ldpaths", afb_api_so_add_pathset_fails, "the binding path set");
806 apiset_start_list("weak-ldpaths", afb_api_so_add_pathset_nofails, "the weak binding path set");
807 apiset_start_list("auto-api", afb_autoset_add_any, "the automatic api path set");
808 #if defined(WITH_DBUS_TRANSPARENCY)
809 apiset_start_list("dbus-client", afb_api_dbus_add_client, "the afb-dbus client");
811 apiset_start_list("ws-client", afb_api_ws_add_client_weak, "the afb-websocket client");
813 DEBUG("Init config done");
815 /* start the services */
816 afb_debug("start-start");
817 #if !defined(NO_CALL_PERSONALITY)
818 personality((unsigned long)-1L);
820 if (afb_apiset_start_all_services(main_apiset) < 0)
823 /* export started apis */
824 apiset_start_list("ws-server", afb_api_ws_add_server, "the afb-websocket service");
825 #if defined(WITH_DBUS_TRANSPARENCY)
826 apiset_start_list("dbus-server", afb_api_dbus_add_server, "the afb-dbus service");
829 /* start the HTTP server */
830 afb_debug("start-http");
832 if (!afb_hreq_init_cookie(http_port, rootapi, session_timeout)) {
833 ERROR("initialisation of HTTP cookies failed");
837 hsrv = start_http_server();
842 /* run the startup calls */
843 afb_debug("start-call");
846 /* run the command */
847 afb_debug("start-exec");
848 if (execute_command() < 0)
852 sd_notify(1, "READY=1");
858 /*---------------------------------------------------------
860 | Parse option and launch action
861 +--------------------------------------------------------- */
863 int main(int argc, char *argv[])
865 struct json_object *obj;
866 afb_debug("main-entry");
868 // ------------- Build session handler & init config -------
869 main_config = afb_config_parse_arguments(argc, argv);
870 if (sig_monitor_init(
871 !json_object_object_get_ex(main_config, "trap-faults", &obj)
872 || json_object_get_boolean(obj)) < 0) {
873 ERROR("failed to initialise signal handlers");
878 if (json_object_object_get_ex(main_config, "name", &obj)) {
879 verbose_set_name(json_object_get_string(obj), 0);
880 process_name_set_name(json_object_get_string(obj));
881 process_name_replace_cmdline(argv, json_object_get_string(obj));
883 afb_debug("main-args");
885 // --------- run -----------
887 INFO("running with pid %d", getpid());
889 /* set the daemon environment */
892 afb_debug("main-start");
894 /* enter job processing */
895 jobs_start(3, 0, 50, start, NULL);
896 WARNING("hoops returned from jobs_enter! [report bug]");