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