2 * Copyright (C) 2015, 2016, 2017 "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.
20 #define NO_BINDING_VERBOSE_MACRO
29 #include <systemd/sd-event.h>
31 #include "afb-config.h"
32 #include "afb-hswitch.h"
34 #include "afb-api-so.h"
35 #include "afb-api-dbus.h"
36 #include "afb-api-ws.h"
38 #include "afb-context.h"
40 #include "afb-sig-handler.h"
41 #include "afb-thread.h"
42 #include "afb-session.h"
44 #include "afb-common.h"
47 #include <afb/afb-binding.h>
49 /*----------------------------------------------------------
50 | helpers for handling list of arguments
51 +--------------------------------------------------------- */
54 * Calls the callback 'run' for each value of the 'list'
55 * until the callback returns 0 or the end of the list is reached.
56 * Returns either NULL if the end of the list is reached or a pointer
57 * to the item whose value made 'run' return 0.
58 * 'closure' is used for passing user data.
60 static struct afb_config_list *run_for_list(struct afb_config_list *list,
61 int (*run) (void *closure, char *value),
64 while (list && run(closure, list->value))
69 static int run_start(void *closure, char *value)
71 int (*starter) (const char *value) = closure;
72 return starter(value) >= 0;
75 static void start_list(struct afb_config_list *list,
76 int (*starter) (const char *value), const char *message)
78 list = run_for_list(list, run_start, starter);
80 ERROR("can't start %s %s", message, list->value);
85 /*----------------------------------------------------------
87 | try to close everything before leaving
88 +--------------------------------------------------------- */
89 static void closeSession(int status, void *data)
91 /* struct afb_config *config = data; */
94 /*----------------------------------------------------------
96 | set the process in background
97 +--------------------------------------------------------- */
98 static void daemonize(struct afb_config *config)
103 // open /dev/console to redirect output messAFBes
104 consoleFD = open(config->console, O_WRONLY | O_APPEND | O_CREAT, 0640);
106 ERROR("AFB-daemon cannot open /dev/console (use --foreground)");
109 // fork process when running background mode
112 // if fail nothing much to do
114 ERROR("AFB-daemon Failed to fork son process");
117 // if in father process, just leave
121 // son process get all data in standalone mode
122 NOTICE("background mode [pid:%d console:%s]", getpid(),
125 // redirect default I/O on console
127 dup(consoleFD); // redirect stderr
129 dup(consoleFD); // redirect stdout
130 close(0); // no need for stdin
134 setsid(); // allow father process to fully exit
135 sleep(2); // allow main to leave and release port
139 /*---------------------------------------------------------
141 | Handles the HTTP server
142 +--------------------------------------------------------- */
143 static int init_alias(void *closure, char *spec)
145 struct afb_hsrv *hsrv = closure;
146 char *path = strchr(spec, ':');
149 ERROR("Missing ':' in alias %s. Alias ignored", spec);
153 INFO("Alias for url=%s to path=%s", spec, path);
154 return afb_hsrv_add_alias(hsrv, spec, afb_common_rootdir_get_fd(), path,
158 static int init_http_server(struct afb_hsrv *hsrv, struct afb_config *config)
160 if (!afb_hsrv_add_handler
161 (hsrv, config->rootapi, afb_hswitch_websocket_switch, NULL, 20))
164 if (!afb_hsrv_add_handler
165 (hsrv, config->rootapi, afb_hswitch_apis, NULL, 10))
168 if (run_for_list(config->aliases, init_alias, hsrv))
171 if (config->roothttp != NULL) {
172 if (!afb_hsrv_add_alias
173 (hsrv, "", afb_common_rootdir_get_fd(), config->roothttp,
178 if (!afb_hsrv_add_handler
179 (hsrv, config->rootbase, afb_hswitch_one_page_api_redirect, NULL,
186 static struct afb_hsrv *start_http_server(struct afb_config *config)
189 struct afb_hsrv *hsrv;
191 if (afb_hreq_init_download_path("/tmp")) { /* TODO: sessiondir? */
192 ERROR("unable to set the tmp directory");
196 hsrv = afb_hsrv_create();
198 ERROR("memory allocation failure");
202 if (!afb_hsrv_set_cache_timeout(hsrv, config->cacheTimeout)
203 || !init_http_server(hsrv, config)) {
204 ERROR("initialisation of httpd failed");
209 NOTICE("Waiting port=%d rootdir=%s", config->httpdPort,
211 NOTICE("Browser URL= http:/*localhost:%d", config->httpdPort);
213 rc = afb_hsrv_start(hsrv, (uint16_t) config->httpdPort, 15);
215 ERROR("starting of httpd failed");
223 /*---------------------------------------------------------
225 | Parse option and launch action
226 +--------------------------------------------------------- */
228 int main(int argc, char *argv[])
230 struct afb_hsrv *hsrv;
231 struct afb_config *config;
232 struct sd_event *eventloop;
234 LOGAUTH("afb-daemon");
236 // ------------- Build session handler & init config -------
237 config = afb_config_parse_arguments(argc, argv);
238 on_exit(closeSession, config);
240 // ------------------ sanity check ----------------------------------------
241 if (config->httpdPort <= 0) {
242 ERROR("no port is defined");
246 afb_session_init(config->nbSessionMax, config->cntxTimeout,
247 config->token, afb_apis_count());
249 afb_api_so_set_timeout(config->apiTimeout);
250 start_list(config->dbus_clients, afb_api_dbus_add_client, "the afb-dbus client");
251 start_list(config->ws_clients, afb_api_ws_add_client, "the afb-websocket client");
252 start_list(config->ldpaths, afb_api_so_add_pathset, "the binding path set");
253 start_list(config->so_bindings, afb_api_so_add_binding, "the binding");
254 start_list(config->dbus_servers, afb_api_dbus_add_server, "the afb-dbus service");
255 start_list(config->ws_servers, afb_api_ws_add_server, "the afb-websocket service");
257 if (!afb_hreq_init_cookie
258 (config->httpdPort, config->rootapi, config->cntxTimeout)) {
259 ERROR("initialisation of cookies failed");
263 if (afb_sig_handler_init() < 0) {
264 ERROR("failed to initialise signal handlers");
267 // if directory does not exist createit
268 mkdir(config->rootdir, O_RDWR | S_IRWXU | S_IRGRP);
269 if (afb_common_rootdir_set(config->rootdir) < 0) {
270 ERROR("failed to set common root directory");
274 if (afb_thread_init(3, 1, 20) < 0) {
275 ERROR("failed to initialise threading");
278 // let's run this program with a low priority
281 // ------------------ Finaly Process Commands -----------------------------
282 // let's not take the risk to run as ROOT
283 //if (getuid() == 0) goto errorNoRoot;
285 DEBUG("Init config done");
287 // --------- run -----------
288 if (config->background) {
289 // --------- in background mode -----------
290 INFO("entering background mode");
293 // ---- in foreground mode --------------------
294 INFO("entering foreground mode");
297 /* ignore any SIGPIPE */
298 signal(SIGPIPE, SIG_IGN);
300 /* install trace of requests */
301 if (config->tracereq)
302 afb_hook_req_create(NULL, NULL, NULL, config->tracereq, NULL, NULL);
304 /* start the HTTP server */
305 hsrv = start_http_server(config);
309 /* start the services */
310 if (afb_apis_start_all_services(1) < 0)
313 if (config->readyfd != 0) {
314 static const char readystr[] = "READY=1";
315 write(config->readyfd, readystr, sizeof(readystr) - 1);
316 close(config->readyfd);
319 eventloop = afb_common_get_event_loop();
321 sd_event_run(eventloop, 30000000);
323 WARNING("hoops returned from infinite loop [report bug]");