Improve parsing of json string
[src/app-framework-binder.git] / src / main-afb-daemon.c
1 /*
2  * Copyright (C) 2015-2018 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
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
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30
31 #if !defined(NO_CALL_PERSONALITY)
32 #include <sys/personality.h>
33 #endif
34
35 #include <json-c/json.h>
36
37 #include <systemd/sd-daemon.h>
38
39 #include "afb-config.h"
40 #include "afb-hswitch.h"
41 #include "afb-apiset.h"
42 #include "afb-autoset.h"
43 #include "afb-api-so.h"
44 #if defined(WITH_DBUS_TRANSPARENCY)
45 #   include "afb-api-dbus.h"
46 #endif
47 #include "afb-api-ws.h"
48 #include "afb-hsrv.h"
49 #include "afb-hreq.h"
50 #include "afb-xreq.h"
51 #include "jobs.h"
52 #include "afb-session.h"
53 #include "verbose.h"
54 #include "afb-common.h"
55 #include "afb-monitor.h"
56 #include "afb-hook.h"
57 #include "afb-hook-flags.h"
58 #include "afb-debug.h"
59 #include "process-name.h"
60 #include "wrap-json.h"
61 #if defined(WITH_SUPERVISION)
62 #   include "afb-supervision.h"
63 #endif
64
65 /*
66    if SELF_PGROUP == 0 the launched command is the group leader
67    if SELF_PGROUP != 0 afb-daemon is the group leader
68 */
69 #define SELF_PGROUP 0
70
71 struct afb_apiset *main_apiset;
72 struct json_object *main_config;
73
74 static pid_t childpid;
75
76 /*----------------------------------------------------------
77  |   helpers for handling list of arguments
78  +--------------------------------------------------------- */
79
80 static const char *run_for_config_array_opt(const char *name,
81                                             int (*run) (void *closure, const char *value),
82                                             void *closure)
83 {
84         int i, n, rc;
85         struct json_object *array, *value;
86
87         if (json_object_object_get_ex(main_config, name, &array)) {
88                 if (!json_object_is_type(array, json_type_array))
89                         return json_object_get_string(array);
90                 n = (int)json_object_array_length(array);
91                 for (i = 0 ; i < n ; i++) {
92                         value = json_object_array_get_idx(array, i);
93                         rc = run(closure, json_object_get_string(value));
94                         if (!rc)
95                                 return json_object_get_string(value);
96                 }
97         }
98         return NULL;
99 }
100
101 static int run_start(void *closure, const char *value)
102 {
103         int (*starter) (const char *value, struct afb_apiset *declare_set, struct afb_apiset *call_set) = closure;
104         return starter(value, main_apiset, main_apiset) >= 0;
105 }
106
107 static void apiset_start_list(const char *name,
108                         int (*starter) (const char *value, struct afb_apiset *declare_set, struct afb_apiset *call_set),
109                         const char *message)
110 {
111         const char *item = run_for_config_array_opt(name, run_start, starter);
112         if (item) {
113                 ERROR("can't start %s %s", message, item);
114                 exit(1);
115         }
116 }
117
118 /*----------------------------------------------------------
119  | exit_handler
120  |   Handles on exit specific actions
121  +--------------------------------------------------------- */
122 static void exit_handler()
123 {
124         struct sigaction siga;
125
126         memset(&siga, 0, sizeof siga);
127         siga.sa_handler = SIG_IGN;
128         sigaction(SIGTERM, &siga, NULL);
129
130         if (SELF_PGROUP)
131                 killpg(0, SIGTERM);
132         else if (childpid > 0)
133                 killpg(childpid, SIGTERM);
134 }
135
136 static void on_sigterm(int signum, siginfo_t *info, void *uctx)
137 {
138         NOTICE("Received SIGTERM");
139         exit(0);
140 }
141
142 static void on_sighup(int signum, siginfo_t *info, void *uctx)
143 {
144         NOTICE("Received SIGHUP");
145         /* TODO */
146 }
147
148 static void setup_daemon()
149 {
150         struct sigaction siga;
151
152         /* install signal handlers */
153         memset(&siga, 0, sizeof siga);
154         siga.sa_flags = SA_SIGINFO;
155
156         siga.sa_sigaction = on_sigterm;
157         sigaction(SIGTERM, &siga, NULL);
158
159         siga.sa_sigaction = on_sighup;
160         sigaction(SIGHUP, &siga, NULL);
161
162         /* handle groups */
163         atexit(exit_handler);
164
165         /* ignore any SIGPIPE */
166         signal(SIGPIPE, SIG_IGN);
167 }
168
169 /*----------------------------------------------------------
170  | daemonize
171  |   set the process in background
172  +--------------------------------------------------------- */
173 static void daemonize()
174 {
175         int fd, daemon;
176         const char *output;
177         pid_t pid;
178
179         daemon = 0;
180         output = NULL;
181         wrap_json_unpack(main_config, "{s?b s?s}", "daemon", &daemon, "output", &output);
182
183         if (output) {
184                 fd = open(output, O_WRONLY | O_APPEND | O_CREAT, 0640);
185                 if (fd < 0) {
186                         ERROR("Can't open output %s", output);
187                         exit(1);
188                 }
189         }
190
191         if (daemon) {
192                 INFO("entering background mode");
193
194                 pid = fork();
195                 if (pid == -1) {
196                         ERROR("Failed to fork daemon process");
197                         exit(1);
198                 }
199                 if (pid != 0)
200                         _exit(0);
201         }
202
203         /* closes the input */
204         if (output) {
205                 NOTICE("Redirecting output to %s", output);
206                 close(2);
207                 dup(fd);
208                 close(1);
209                 dup(fd);
210                 close(fd);
211         }
212
213         /* after that ctrl+C still works */
214         close(0);
215 }
216
217 /*---------------------------------------------------------
218  | http server
219  |   Handles the HTTP server
220  +--------------------------------------------------------- */
221 static int init_alias(void *closure, const char *spec)
222 {
223         struct afb_hsrv *hsrv = closure;
224         char *path = strchr(spec, ':');
225
226         if (path == NULL) {
227                 ERROR("Missing ':' in alias %s. Alias ignored", spec);
228                 return 1;
229         }
230         *path++ = 0;
231         INFO("Alias for url=%s to path=%s", spec, path);
232         return afb_hsrv_add_alias(hsrv, spec, afb_common_rootdir_get_fd(), path,
233                                   0, 0);
234 }
235
236 static int init_http_server(struct afb_hsrv *hsrv)
237 {
238         int rc;
239         const char *rootapi, *roothttp, *rootbase;
240
241         roothttp = NULL;
242         rc = wrap_json_unpack(main_config, "{ss ss s?s}",
243                                 "rootapi", &rootapi,
244                                 "rootbase", &rootbase,
245                                 "roothttp", &roothttp);
246         if (rc < 0) {
247                 ERROR("Can't get HTTP server config");
248                 exit(1);
249         }
250
251         if (!afb_hsrv_add_handler(hsrv, rootapi,
252                         afb_hswitch_websocket_switch, main_apiset, 20))
253                 return 0;
254
255         if (!afb_hsrv_add_handler(hsrv, rootapi,
256                         afb_hswitch_apis, main_apiset, 10))
257                 return 0;
258
259         if (run_for_config_array_opt("alias", init_alias, hsrv))
260                 return 0;
261
262         if (roothttp != NULL) {
263                 if (!afb_hsrv_add_alias(hsrv, "",
264                         afb_common_rootdir_get_fd(), roothttp, -10, 1))
265                         return 0;
266         }
267
268         if (!afb_hsrv_add_handler(hsrv, rootbase,
269                         afb_hswitch_one_page_api_redirect, NULL, -20))
270                 return 0;
271
272         return 1;
273 }
274
275 static struct afb_hsrv *start_http_server()
276 {
277         int rc;
278         const char *uploaddir, *rootdir;
279         struct afb_hsrv *hsrv;
280         int cache_timeout, http_port;
281
282         rc = wrap_json_unpack(main_config, "{ss ss si si}",
283                                 "uploaddir", &uploaddir,
284                                 "rootdir", &rootdir,
285                                 "cache-eol", &cache_timeout,
286                                 "port", &http_port);
287         if (rc < 0) {
288                 ERROR("Can't get HTTP server start config");
289                 exit(1);
290         }
291
292         if (afb_hreq_init_download_path(uploaddir)) {
293                 ERROR("unable to set the upload directory %s", uploaddir);
294                 return NULL;
295         }
296
297         hsrv = afb_hsrv_create();
298         if (hsrv == NULL) {
299                 ERROR("memory allocation failure");
300                 return NULL;
301         }
302
303         if (!afb_hsrv_set_cache_timeout(hsrv, cache_timeout)
304             || !init_http_server(hsrv)) {
305                 ERROR("initialisation of httpd failed");
306                 afb_hsrv_put(hsrv);
307                 return NULL;
308         }
309
310         NOTICE("Waiting port=%d rootdir=%s", http_port, rootdir);
311         NOTICE("Browser URL= http://localhost:%d", http_port);
312
313         rc = afb_hsrv_start(hsrv, (uint16_t) http_port, 15);
314         if (!rc) {
315                 ERROR("starting of httpd failed");
316                 afb_hsrv_put(hsrv);
317                 return NULL;
318         }
319
320         return hsrv;
321 }
322
323 /*---------------------------------------------------------
324  | execute_command
325  +--------------------------------------------------------- */
326
327 static void on_sigchld(int signum, siginfo_t *info, void *uctx)
328 {
329         if (info->si_pid == childpid) {
330                 switch (info->si_code) {
331                 case CLD_EXITED:
332                 case CLD_KILLED:
333                 case CLD_DUMPED:
334                         childpid = 0;
335                         if (!SELF_PGROUP)
336                                 killpg(info->si_pid, SIGKILL);
337                         waitpid(info->si_pid, NULL, 0);
338                         exit(0);
339                 }
340         }
341 }
342
343 /*
344 # @@ @
345 # @p port
346 # @t token
347 */
348
349 #define SUBST_CHAR  '@'
350 #define SUBST_STR   "@"
351
352 static char *instanciate_string(const char *arg, const char *port, const char *token)
353 {
354         char *resu, *it, *wr;
355         int chg, dif;
356
357         /* get the changes */
358         chg = 0;
359         dif = 0;
360         it = strchrnul(arg, SUBST_CHAR);
361         while (*it) {
362                 switch(*++it) {
363                 case 'p': chg++; dif += (int)strlen(port) - 2; break;
364                 case 't': chg++; dif += (int)strlen(token) - 2; break;
365                 case SUBST_CHAR: it++; chg++; dif--; break;
366                 default: break;
367                 }
368                 it = strchrnul(it, SUBST_CHAR);
369         }
370
371         /* return arg when no change */
372         if (!chg)
373                 return strdup(arg);
374
375         /* allocates the result */
376         resu = malloc((it - arg) + dif + 1);
377         if (!resu) {
378                 ERROR("out of memory");
379                 return NULL;
380         }
381
382         /* instanciate the arguments */
383         wr = resu;
384         for (;;) {
385                 it = strchrnul(arg, SUBST_CHAR);
386                 wr = mempcpy(wr, arg, it - arg);
387                 if (!*it)
388                         break;
389                 switch(*++it) {
390                 case 'p': wr = stpcpy(wr, port); break;
391                 case 't': wr = stpcpy(wr, token); break;
392                 default: *wr++ = SUBST_CHAR; /*@fallthrough@*/
393                 case SUBST_CHAR: *wr++ = *it;
394                 }
395                 arg = ++it;
396         }
397
398         *wr = 0;
399         return resu;
400 }
401
402 static int instanciate_environ(const char *port, const char *token)
403 {
404         extern char **environ;
405         char *repl;
406         int i;
407
408         /* instantiate the environment */
409         for (i = 0 ; environ[i] ; i++) {
410                 repl = instanciate_string(environ[i], port, token);
411                 if (!repl)
412                         return -1;
413                 environ[i] = repl;
414         }
415         return 0;
416 }
417
418 static char **instanciate_command_args(struct json_object *exec, const char *port, const char *token)
419 {
420         char **result;
421         char *repl;
422         int i, n;
423
424         /* allocates the result */
425         n = (int)json_object_array_length(exec);
426         result = malloc((n + 1) * sizeof * result);
427         if (!result) {
428                 ERROR("out of memory");
429                 return NULL;
430         }
431
432         /* instanciate the arguments */
433         for (i = 0 ; i < n ; i++) {
434                 repl = instanciate_string(json_object_get_string(json_object_array_get_idx(exec, i)), port, token);
435                 if (!repl) {
436                         while(i)
437                                 free(result[--i]);
438                         free(result);
439                         return NULL;
440                 }
441                 result[i] = repl;
442         }
443         result[i] = NULL;
444         return result;
445 }
446
447 static int execute_command()
448 {
449         struct json_object *exec, *oport;
450         struct sigaction siga;
451         char port[20];
452         const char *token;
453         char **args;
454         int rc;
455
456         /* check whether a command is to execute or not */
457         if (!json_object_object_get_ex(main_config, "exec", &exec))
458                 return 0;
459
460         if (SELF_PGROUP)
461                 setpgid(0, 0);
462
463         /* install signal handler */
464         memset(&siga, 0, sizeof siga);
465         siga.sa_sigaction = on_sigchld;
466         siga.sa_flags = SA_SIGINFO;
467         sigaction(SIGCHLD, &siga, NULL);
468
469         /* fork now */
470         childpid = fork();
471         if (childpid)
472                 return 0;
473
474         /* compute the string for port */
475         if (json_object_object_get_ex(main_config, "port", &oport))
476                 rc = snprintf(port, sizeof port, "%s", json_object_get_string(oport));
477         else
478                 rc = snprintf(port, sizeof port, "%cp", SUBST_CHAR);
479         if (rc < 0 || rc >= (int)(sizeof port)) {
480                 ERROR("port->txt failed");
481         }
482         else {
483                 /* instanciate arguments and environment */
484                 token = afb_session_initial_token();
485                 args = instanciate_command_args(exec, port, token);
486                 if (args && instanciate_environ(port, token) >= 0) {
487                         /* run */
488                         if (!SELF_PGROUP)
489                                 setpgid(0, 0);
490                         execv(args[0], args);
491                         ERROR("can't launch %s: %m", args[0]);
492                 }
493         }
494         exit(1);
495         return -1;
496 }
497
498 /*---------------------------------------------------------
499  | startup calls
500  +--------------------------------------------------------- */
501
502 struct startup_req
503 {
504         struct afb_xreq xreq;
505         char *api;
506         char *verb;
507         struct json_object *calls;
508         int index;
509         int count;
510         const char *callspec;
511         struct afb_session *session;
512 };
513
514 static void startup_call_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
515 {
516         struct startup_req *sreq = CONTAINER_OF_XREQ(struct startup_req, xreq);
517
518         info = info ?: "";
519         if (!error) {
520                 NOTICE("startup call %s returned %s (%s)", sreq->callspec, json_object_get_string(object), info);
521                 json_object_put(object);
522         } else {
523                 ERROR("startup call %s ERROR! %s (%s)", sreq->callspec, error, info);
524                 exit(1);
525         }
526 }
527
528 static void startup_call_current(struct startup_req *sreq);
529
530 static void startup_call_unref(struct afb_xreq *xreq)
531 {
532         struct startup_req *sreq = CONTAINER_OF_XREQ(struct startup_req, xreq);
533
534         free(sreq->api);
535         free(sreq->verb);
536         json_object_put(sreq->xreq.json);
537         if (++sreq->index < sreq->count)
538                 startup_call_current(sreq);
539         else {
540                 afb_session_close(sreq->session);
541                 afb_session_unref(sreq->session);
542                 free(sreq);
543         }
544 }
545
546 static struct afb_xreq_query_itf startup_xreq_itf =
547 {
548         .reply = startup_call_reply,
549         .unref = startup_call_unref
550 };
551
552 static void startup_call_current(struct startup_req *sreq)
553 {
554         const char *api, *verb, *json;
555         enum json_tokener_error jerr;
556
557         sreq->callspec = json_object_get_string(json_object_array_get_idx(sreq->calls, sreq->index)),
558         api = sreq->callspec;
559         verb = strchr(api, '/');
560         if (verb) {
561                 json = strchr(verb, ':');
562                 if (json) {
563                         afb_xreq_init(&sreq->xreq, &startup_xreq_itf);
564                         afb_context_init(&sreq->xreq.context, sreq->session, NULL);
565                         sreq->xreq.context.validated = 1;
566                         sreq->api = strndup(api, verb - api);
567                         sreq->verb = strndup(verb + 1, json - verb - 1);
568                         sreq->xreq.request.called_api = sreq->api;
569                         sreq->xreq.request.called_verb = sreq->verb;
570                         sreq->xreq.json = json_tokener_parse_verbose(json + 1, &jerr);
571                         if (sreq->api && sreq->verb && jerr == json_tokener_success) {
572                                 afb_xreq_process(&sreq->xreq, main_apiset);
573                                 return;
574                         }
575                 }
576         }
577         ERROR("Bad call specification %s", sreq->callspec);
578         exit(1);
579 }
580
581 static void run_startup_calls()
582 {
583         struct json_object *calls;
584         struct startup_req *sreq;
585         int count;
586
587         if (json_object_object_get_ex(main_config, "call", &calls)
588          && json_object_is_type(calls, json_type_array)
589          && (count = (int)json_object_array_length(calls))) {
590                 sreq = calloc(1, sizeof *sreq);
591                 sreq->session = afb_session_create(3600);
592                 sreq->calls = calls;
593                 sreq->index = 0;
594                 sreq->count = count;
595                 startup_call_current(sreq);
596         }
597 }
598
599 /*---------------------------------------------------------
600  | job for starting the daemon
601  +--------------------------------------------------------- */
602
603 static void start(int signum, void *arg)
604 {
605         const char *tracereq, *traceapi, *traceevt, *traceses, *tracesvc, *traceditf, *traceglob;
606         const char *workdir, *rootdir, *token, *rootapi;
607         struct afb_hsrv *hsrv;
608         int max_session_count, session_timeout, api_timeout;
609         int no_httpd, http_port;
610         int rc;
611
612
613         afb_debug("start-entry");
614
615         if (signum) {
616                 ERROR("start aborted: received signal %s", strsignal(signum));
617                 exit(1);
618         }
619
620         token = rootapi = tracesvc = traceditf = tracereq =
621                 traceapi = traceevt = traceses = traceglob = NULL;
622         no_httpd = http_port = 0;
623         rc = wrap_json_unpack(main_config, "{"
624                         "ss ss s?s"
625                         "si si si"
626                         "s?b s?i s?s"
627 #if !defined(REMOVE_LEGACY_TRACE)
628                         "s?s s?s"
629 #endif
630                         "s?s s?s s?s s?s s?s"
631                         "}",
632
633                         "rootdir", &rootdir,
634                         "workdir", &workdir,
635                         "token", &token,
636
637                         "apitimeout", &api_timeout,
638                         "cntxtimeout", &session_timeout,
639                         "session-max", &max_session_count,
640
641                         "no-httpd", &no_httpd,
642                         "port", &http_port,
643                         "rootapi", &rootapi,
644
645 #if !defined(REMOVE_LEGACY_TRACE)
646                         "tracesvc", &tracesvc,
647                         "traceditf", &traceditf,
648 #endif
649                         "tracereq", &tracereq,
650                         "traceapi", &traceapi,
651                         "traceevt", &traceevt,
652                         "traceses",  &traceses,
653                         "traceglob", &traceglob
654                         );
655         if (rc < 0) {
656                 ERROR("Unable to get start config");
657                 exit(1);
658         }
659
660         /* set the directories */
661         mkdir(workdir, S_IRWXU | S_IRGRP | S_IXGRP);
662         if (chdir(workdir) < 0) {
663                 ERROR("Can't enter working dir %s", workdir);
664                 goto error;
665         }
666         if (afb_common_rootdir_set(rootdir) < 0) {
667                 ERROR("failed to set common root directory");
668                 goto error;
669         }
670
671         /* configure the daemon */
672         if (afb_session_init(max_session_count, session_timeout, token)) {
673                 ERROR("initialisation of session manager failed");
674                 goto error;
675         }
676         main_apiset = afb_apiset_create("main", api_timeout);
677         if (!main_apiset) {
678                 ERROR("can't create main api set");
679                 goto error;
680         }
681         if (afb_monitor_init(main_apiset, main_apiset) < 0) {
682                 ERROR("failed to setup monitor");
683                 goto error;
684         }
685 #if defined(WITH_SUPERVISION)
686         if (afb_supervision_init(main_apiset, main_config) < 0) {
687                 ERROR("failed to setup supervision");
688                 goto error;
689         }
690 #endif
691
692         /* install hooks */
693         if (tracereq)
694                 afb_hook_create_xreq(NULL, NULL, NULL, afb_hook_flags_xreq_from_text(tracereq), NULL, NULL);
695 #if !defined(REMOVE_LEGACY_TRACE)
696         if (traceapi || tracesvc || traceditf)
697                 afb_hook_create_api(NULL, afb_hook_flags_api_from_text(traceapi)
698                         | afb_hook_flags_legacy_ditf_from_text(traceditf)
699                         | afb_hook_flags_legacy_svc_from_text(tracesvc), NULL, NULL);
700 #else
701         if (traceapi)
702                 afb_hook_create_api(NULL, afb_hook_flags_api_from_text(traceapi), NULL, NULL);
703 #endif
704         if (traceevt)
705                 afb_hook_create_evt(NULL, afb_hook_flags_evt_from_text(traceevt), NULL, NULL);
706         if (traceses)
707                 afb_hook_create_session(NULL, afb_hook_flags_session_from_text(traceses), NULL, NULL);
708         if (traceglob)
709                 afb_hook_create_global(afb_hook_flags_global_from_text(traceglob), NULL, NULL);
710
711         /* load bindings */
712         afb_debug("start-load");
713         apiset_start_list("binding", afb_api_so_add_binding, "the binding");
714         apiset_start_list("ldpaths", afb_api_so_add_pathset_fails, "the binding path set");
715         apiset_start_list("weak-ldpaths", afb_api_so_add_pathset_nofails, "the weak binding path set");
716         apiset_start_list("auto-api", afb_autoset_add_any, "the automatic api path set");
717         apiset_start_list("ws-server", afb_api_ws_add_server, "the afb-websocket service");
718 #if defined(WITH_DBUS_TRANSPARENCY)
719         apiset_start_list("dbus-server", afb_api_dbus_add_server, "the afb-dbus service");
720         apiset_start_list("dbus-client", afb_api_dbus_add_client, "the afb-dbus client");
721 #endif
722         apiset_start_list("ws-client", afb_api_ws_add_client_weak, "the afb-websocket client");
723
724         DEBUG("Init config done");
725
726         /* start the services */
727         afb_debug("start-start");
728 #if !defined(NO_CALL_PERSONALITY)
729         personality((unsigned long)-1L);
730 #endif
731         if (afb_apiset_start_all_services(main_apiset) < 0)
732                 goto error;
733
734         /* start the HTTP server */
735         afb_debug("start-http");
736         if (!no_httpd) {
737                 if (http_port <= 0) {
738                         ERROR("no port is defined");
739                         goto error;
740                 }
741
742                 if (!afb_hreq_init_cookie(http_port, rootapi, session_timeout)) {
743                         ERROR("initialisation of HTTP cookies failed");
744                         goto error;
745                 }
746
747                 hsrv = start_http_server();
748                 if (hsrv == NULL)
749                         goto error;
750         }
751
752         /* run the startup calls */
753         afb_debug("start-call");
754         run_startup_calls();
755
756         /* run the command */
757         afb_debug("start-exec");
758         if (execute_command() < 0)
759                 goto error;
760
761         /* ready */
762         sd_notify(1, "READY=1");
763         return;
764 error:
765         exit(1);
766 }
767
768 /*---------------------------------------------------------
769  | main
770  |   Parse option and launch action
771  +--------------------------------------------------------- */
772
773 int main(int argc, char *argv[])
774 {
775         struct json_object *name;
776         afb_debug("main-entry");
777
778         // ------------- Build session handler & init config -------
779         main_config = afb_config_parse_arguments(argc, argv);
780         if (json_object_object_get_ex(main_config, "name", &name)) {
781                 verbose_set_name(json_object_get_string(name), 0);
782                 process_name_set_name(json_object_get_string(name));
783                 process_name_replace_cmdline(argv, json_object_get_string(name));
784         }
785         afb_debug("main-args");
786
787         // --------- run -----------
788         daemonize();
789         INFO("running with pid %d", getpid());
790
791         /* set the daemon environment */
792         setup_daemon();
793
794         afb_debug("main-start");
795
796         /* enter job processing */
797         jobs_start(3, 0, 50, start, NULL);
798         WARNING("hoops returned from jobs_enter! [report bug]");
799         return 1;
800 }
801