afb-config: Increase the default timeout of sessions
[src/app-framework-binder.git] / src / afb-config.c
1 /*
2  * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <getopt.h>
24 #include <limits.h>
25 #include <unistd.h>
26
27 #include "verbose.h"
28 #include "afb-config.h"
29 #include "afb-hook.h"
30
31 #include <afb/afb-binding-v1.h>
32
33 #if !defined(BINDING_INSTALL_DIR)
34 #error "you should define BINDING_INSTALL_DIR"
35 #endif
36 #if !defined(AFB_VERSION)
37 #error "you should define AFB_VERSION"
38 #endif
39
40 // default
41 #define DEFLT_CNTX_TIMEOUT  32000000    // default Client Connection
42                                         // Timeout: few more than one year
43 #define DEFLT_API_TIMEOUT   20          // default Plugin API Timeout [0=NoLimit
44                                         // for Debug Only]
45 #define DEFLT_CACHE_TIMEOUT 100000      // default Static File Chache
46                                         // [Client Side Cache
47                                         // 100000~=1day]
48 #define CTX_NBCLIENTS       10          // allow a default of 10 authenticated
49                                         // clients
50
51
52 // Define command line option
53 #define SET_BACKGROUND     2
54 #define SET_FORGROUND      3
55
56 #define SET_ROOT_DIR       6
57 #define SET_ROOT_BASE      7
58 #define SET_ROOT_API       8
59 #define SET_ALIAS          9
60
61 #define SET_CACHE_TIMEOUT  10
62 #define SET_SESSION_DIR    11
63
64 #define SET_LDPATH         13
65 #define SET_APITIMEOUT     14
66 #define SET_CNTXTIMEOUT    15
67 #define SET_WEAK_LDPATH    16
68 #define NO_LDPATH          17
69
70 #define SET_MODE           18
71
72 #define DBUS_CLIENT        20
73 #define DBUS_SERVICE       21
74 #define SO_BINDING         22
75
76 #define SET_SESSIONMAX     23
77
78 #define WS_CLIENT          24
79 #define WS_SERVICE         25
80
81 #define SET_ROOT_HTTP      26
82
83 #define SET_NO_HTTPD       28
84
85 #define ADD_CALL           'c'
86 #define SET_TRACEDITF      'D'
87 #define SET_TRACEEVT       'E'
88 #define SET_EXEC           'e'
89 #define DISPLAY_HELP       'h'
90 #if defined(WITH_MONITORING_OPTION)
91 #define SET_MONITORING     'M'
92 #endif
93 #define SET_NAME           'n'
94 #define SET_TCP_PORT       'p'
95 #define SET_QUIET          'q'
96 #define SET_RNDTOKEN       'r'
97 #define SET_TRACESVC       'S'
98 #define SET_TRACEREQ       'T'
99 #define SET_AUTH_TOKEN     't'
100 #define SET_UPLOAD_DIR     'u'
101 #define DISPLAY_VERSION    'V'
102 #define SET_VERBOSE        'v'
103 #define SET_WORK_DIR       'w'
104
105 const char shortopts[] =
106         "c:D:E:ehn:p:qrT:t:u:Vvw:"
107 #if defined(WITH_MONITORING_OPTION)
108         "M"
109 #endif
110 ;
111
112 // Command line structure hold cli --command + help text
113 typedef struct {
114         int val;                // command number within application
115         int has_arg;            // command number within application
116         char *name;             // command as used in --xxxx cli
117         char *help;             // help text
118 } AFB_options;
119
120 // Supported option
121 static AFB_options cliOptions[] = {
122 /* *INDENT-OFF* */
123         {SET_VERBOSE,       0, "verbose",     "Verbose Mode, repeat to increase verbosity"},
124         {SET_QUIET,         0, "quiet",       "Quiet Mode, repeat to decrease verbosity"},
125
126         {SET_FORGROUND,     0, "foreground",  "Get all in foreground mode"},
127         {SET_BACKGROUND,    0, "daemon",      "Get all in background mode"},
128
129         {SET_NAME,          1, "name",        "Set the visible name"},
130
131         {SET_TCP_PORT,      1, "port",        "HTTP listening TCP port  [default 1234]"},
132         {SET_ROOT_HTTP,     1, "roothttp",    "HTTP Root Directory [default no root http (files not served but apis still available)]"},
133         {SET_ROOT_BASE,     1, "rootbase",    "Angular Base Root URL [default /opa]"},
134         {SET_ROOT_API,      1, "rootapi",     "HTML Root API URL [default /api]"},
135         {SET_ALIAS,         1, "alias",       "Multiple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
136
137         {SET_APITIMEOUT,    1, "apitimeout",  "Binding API timeout in seconds [default 10]"},
138         {SET_CNTXTIMEOUT,   1, "cntxtimeout", "Client Session Context Timeout [default 900]"},
139         {SET_CACHE_TIMEOUT, 1, "cache-eol",   "Client cache end of live [default 3600]"},
140
141         {SET_WORK_DIR,      1, "workdir",     "Set the working directory [default: $PWD or current working directory]"},
142         {SET_UPLOAD_DIR,    1, "uploaddir",   "Directory for uploading files [default: workdir]"},
143         {SET_ROOT_DIR,      1, "rootdir",     "Root Directory of the application [default: workdir]"},
144         {SET_SESSION_DIR,   1, "sessiondir",  "OBSOLETE (was: Sessions file path)"},
145
146         {SET_LDPATH,        1, "ldpaths",     "Load bindings from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
147         {SO_BINDING,        1, "binding",     "Load the binding of path"},
148         {SET_WEAK_LDPATH,   1, "weak-ldpaths","Same as --ldpaths but ignore errors"},
149         {NO_LDPATH,         0, "no-ldpaths",  "Discard default ldpaths loading"},
150
151         {SET_AUTH_TOKEN,    1, "token",       "Initial Secret [default=no-session, --token= for session without authentication]"},
152         {SET_RNDTOKEN,      0, "random-token","Enforce a random token"},
153
154         {DISPLAY_VERSION,   0, "version",     "Display version and copyright"},
155         {DISPLAY_HELP,      0, "help",        "Display this help"},
156
157         {SET_MODE,          1, "mode",        "Set the mode: either local, remote or global"},
158
159         {DBUS_CLIENT,       1, "dbus-client", "Bind to an afb service through dbus"},
160         {DBUS_SERVICE,      1, "dbus-server", "Provides an afb service through dbus"},
161         {WS_CLIENT,         1, "ws-client",   "Bind to an afb service through websocket"},
162         {WS_SERVICE,        1, "ws-server",   "Provides an afb service through websockets"},
163
164         {SET_SESSIONMAX,    1, "session-max", "Max count of session simultaneously [default 10]"},
165
166         {SET_TRACEREQ,      1, "tracereq",    "Log the requests: no, common, extra, all"},
167         {SET_TRACEDITF,     1, "traceditf",   "Log the requests: no, common, extra, all"},
168         {SET_TRACESVC,      1, "tracesvc",    "Log the requests: no, all"},
169         {SET_TRACEEVT,      1, "traceevt",    "Log the requests: no, common, extra, all"},
170
171         {ADD_CALL,          1, "call",        "call at start format of val: API/VERB:json-args"},
172
173         {SET_NO_HTTPD,      0, "no-httpd",    "Forbids HTTP service"},
174         {SET_EXEC,          0, "exec",        "Execute the remaining arguments"},
175
176 #if defined(WITH_MONITORING_OPTION)
177         {SET_MONITORING,    0, "monitoring",  "enable HTTP monitoring at <ROOT>/monitoring/"},
178 #endif
179         {0, 0, NULL, NULL}
180 /* *INDENT-ON* */
181 };
182
183
184 struct enumdesc
185 {
186         const char *name;
187         int value;
188 };
189
190 static struct enumdesc tracereq_desc[] = {
191         { "no",     0 },
192         { "common", afb_hook_flags_req_common },
193         { "extra",  afb_hook_flags_req_extra },
194         { "all",    afb_hook_flags_req_all },
195         { NULL, 0 }
196 };
197
198 static struct enumdesc traceditf_desc[] = {
199         { "no",     0 },
200         { "common", afb_hook_flags_ditf_common },
201         { "extra",  afb_hook_flags_ditf_extra },
202         { "all",    afb_hook_flags_ditf_all },
203         { NULL, 0 }
204 };
205
206 static struct enumdesc tracesvc_desc[] = {
207         { "no",     0 },
208         { "all",    afb_hook_flags_svc_all },
209         { NULL, 0 }
210 };
211
212 static struct enumdesc traceevt_desc[] = {
213         { "no",     0 },
214         { "common", afb_hook_flags_evt_common },
215         { "extra",  afb_hook_flags_evt_extra },
216         { "all",    afb_hook_flags_evt_all },
217         { NULL, 0 }
218 };
219
220 static struct enumdesc mode_desc[] = {
221         { "local",  AFB_MODE_LOCAL },
222         { "remote", AFB_MODE_REMOTE },
223         { "global", AFB_MODE_GLOBAL },
224         { NULL, 0 }
225 };
226
227 /*----------------------------------------------------------
228  | printversion
229  |   print version and copyright
230  +--------------------------------------------------------- */
231 static void printVersion(FILE * file)
232 {
233         static const char version[] =
234                 "\n"
235                 "  AFB [Application Framework Binder] version="AFB_VERSION"\n"
236                 "\n"
237                 "  Copyright (C) 2015, 2016, 2017 \"IoT.bzh\"\n"
238                 "  AFB comes with ABSOLUTELY NO WARRANTY.\n"
239                 "  Licence Apache 2\n"
240                 "\n";
241
242         fprintf(file, "%s", version);
243 }
244
245 /*----------------------------------------------------------
246  | printHelp
247  |   print information from long option array
248  +--------------------------------------------------------- */
249
250 static void printHelp(FILE * file, const char *name)
251 {
252         int ind;
253         char command[50];
254
255         fprintf(file, "%s:\nallowed options\n", name);
256         for (ind = 0; cliOptions[ind].name != NULL; ind++) {
257                 strcpy(command, cliOptions[ind].name);
258                 if (cliOptions[ind].has_arg)
259                         strcat(command, "=xxxx");
260                 fprintf(file, "  --%-15s %s\n", command, cliOptions[ind].help);
261         }
262         fprintf(file,
263                 "Example:\n  %s  --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
264                 name);
265 }
266
267
268 /*----------------------------------------------------------
269  |   adds a string to the list
270  +--------------------------------------------------------- */
271 static void list_add(struct afb_config_list **head, char *value)
272 {
273         struct afb_config_list *item;
274
275         /*
276          * search tail
277          */
278         item = *head;
279         while (item != NULL) {
280                 head = &item->next;
281                 item = item->next;
282         }
283
284         /*
285          * alloc the item
286          */
287         item = malloc(sizeof *item);
288         if (item == NULL) {
289                 ERROR("out of memory");
290                 exit(1);
291         }
292
293         /*
294          * init the item
295          */
296         *head = item;
297         item->value = value;
298         item->next = NULL;
299 }
300
301 /*---------------------------------------------------------
302  |   helpers for argument scanning
303  +--------------------------------------------------------- */
304
305 static const char *name_of_option(int optc)
306 {
307         AFB_options *o = cliOptions;
308         while (o->name && o->val != optc)
309                 o++;
310         return o->name ? : "<unknown-option-name>";
311 }
312
313 static const char *current_argument(int optc)
314 {
315         if (optarg == 0) {
316                 ERROR("option [--%s] needs a value i.e. --%s=xxx",
317                       name_of_option(optc), name_of_option(optc));
318                 exit(1);
319         }
320         return optarg;
321 }
322
323 static char *argvalstr(int optc)
324 {
325         char *result = strdup(current_argument(optc));
326         if (result == NULL) {
327                 ERROR("can't alloc memory");
328                 exit(1);
329         }
330         return result;
331 }
332
333 static int argvalenum(int optc, struct enumdesc *desc)
334 {
335         int i;
336         size_t len;
337         char *list;
338         const char *name = current_argument(optc);
339
340         i = 0;
341         while(desc[i].name && strcmp(desc[i].name, name))
342                 i++;
343         if (!desc[i].name) {
344                 len = 0;
345                 i = 0;
346                 while(desc[i].name)
347                         len += strlen(desc[i++].name);
348                 list = malloc(len + i + i);
349                 if (!i || !list)
350                         ERROR("option [--%s] bad value (found %s)",
351                                 name_of_option(optc), name);
352                 else {
353                         i = 0;
354                         strcpy(list, desc[i].name ? : "");
355                         while(desc[++i].name)
356                                 strcat(strcat(list, ", "), desc[i].name);
357                         ERROR("option [--%s] bad value, only accepts values %s (found %s)",
358                                 name_of_option(optc), list, name);
359                 }
360                 free(list);
361                 exit(1);
362         }
363         return desc[i].value;
364 }
365
366 static int argvalint(int optc, int mini, int maxi, int base)
367 {
368         const char *beg, *end;
369         long int val;
370         beg = current_argument(optc);
371         val = strtol(beg, (char**)&end, base);
372         if (*end || end == beg) {
373                 ERROR("option [--%s] requires a valid integer (found %s)",
374                         name_of_option(optc), beg);
375                 exit(1);
376         }
377         if (val < (long int)mini || val > (long int)maxi) {
378                 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
379                         name_of_option(optc), mini, val, maxi);
380                 exit(1);
381         }
382         return (int)val;
383 }
384
385 static int argvalintdec(int optc, int mini, int maxi)
386 {
387         return argvalint(optc, mini, maxi, 10);
388 }
389
390 static void noarg(int optc)
391 {
392         if (optarg != 0) {
393                 ERROR("option [--%s] need no value (found %s)", name_of_option(optc), optarg);
394                 exit(1);
395         }
396 }
397
398 static char **make_exec(char **argv)
399 {
400         char **result, *iter;
401         size_t length;
402         int i;
403
404         length = 0;
405         for (i = 0 ; argv[i] ; i++)
406                 length += strlen(argv[i]) + 1;
407
408         result = malloc(length + ((unsigned)(i + 1)) * sizeof *result);
409         if (result == NULL) {
410                 ERROR("can't alloc memory");
411                 exit(1);
412         }
413
414         iter = (char*)&result[i+1];
415         for (i = 0 ; argv[i] ; i++) {
416                 result[i] = iter;
417                 iter = stpcpy(iter, argv[i]) + 1;
418         }
419         result[i] = NULL;
420         return result;
421 }
422
423 /*---------------------------------------------------------
424  |   Parse option and launch action
425  +--------------------------------------------------------- */
426
427 static void parse_arguments(int argc, char **argv, struct afb_config *config)
428 {
429         char *programName = argv[0];
430         int optc, ind;
431         int nbcmd;
432         struct option *gnuOptions;
433
434         // ------------------ Process Command Line -----------------------
435
436         // build GNU getopt info from cliOptions
437         nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
438         gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
439         for (ind = 0; ind < nbcmd; ind++) {
440                 gnuOptions[ind].name = cliOptions[ind].name;
441                 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
442                 gnuOptions[ind].flag = 0;
443                 gnuOptions[ind].val = cliOptions[ind].val;
444         }
445
446         // get all options from command line
447         while ((optc = getopt_long(argc, argv, shortopts, gnuOptions, NULL)) != EOF) {
448                 switch (optc) {
449                 case SET_VERBOSE:
450                         verbosity++;
451                         break;
452
453                 case SET_QUIET:
454                         verbosity--;
455                         break;
456
457                 case SET_TCP_PORT:
458                         config->httpdPort = argvalintdec(optc, 1024, 32767);
459                         break;
460
461                 case SET_APITIMEOUT:
462                         config->apiTimeout = argvalintdec(optc, 0, INT_MAX);
463                         break;
464
465                 case SET_CNTXTIMEOUT:
466                         config->cntxTimeout = argvalintdec(optc, 0, INT_MAX);
467                         break;
468
469                 case SET_ROOT_DIR:
470                         config->rootdir = argvalstr(optc);
471                         INFO("Forcing Rootdir=%s", config->rootdir);
472                         break;
473
474                 case SET_ROOT_HTTP:
475                         config->roothttp = argvalstr(optc);
476                         INFO("Forcing Root HTTP=%s", config->roothttp);
477                         break;
478
479                 case SET_ROOT_BASE:
480                         config->rootbase = argvalstr(optc);
481                         INFO("Forcing Rootbase=%s", config->rootbase);
482                         break;
483
484                 case SET_ROOT_API:
485                         config->rootapi = argvalstr(optc);
486                         INFO("Forcing Rootapi=%s", config->rootapi);
487                         break;
488
489                 case SET_ALIAS:
490                         list_add(&config->aliases, argvalstr(optc));
491                         break;
492
493                 case SET_AUTH_TOKEN:
494                         config->token = argvalstr(optc);
495                         break;
496
497                 case SET_LDPATH:
498                         list_add(&config->ldpaths, argvalstr(optc));
499                         break;
500
501                 case SET_WEAK_LDPATH:
502                         list_add(&config->weak_ldpaths, argvalstr(optc));
503                         break;
504
505                 case NO_LDPATH:
506                         noarg(optc);
507                         config->no_ldpaths = 1;
508                         break;
509
510                 case ADD_CALL:
511                         list_add(&config->calls, argvalstr(optc));
512                         break;
513
514                 case SET_SESSION_DIR:
515                         /* config->sessiondir = argvalstr(optc); */
516                         WARNING("Obsolete option %s ignored", name_of_option(optc));
517                         break;
518
519                 case SET_UPLOAD_DIR:
520                         config->uploaddir = argvalstr(optc);
521                         break;
522
523                 case SET_WORK_DIR:
524                         config->workdir = argvalstr(optc);
525                         break;
526
527                 case SET_CACHE_TIMEOUT:
528                         config->cacheTimeout = argvalintdec(optc, 0, INT_MAX);
529                         break;
530
531                 case SET_SESSIONMAX:
532                         config->nbSessionMax = argvalintdec(optc, 1, INT_MAX);
533                         break;
534
535                 case SET_FORGROUND:
536                         noarg(optc);
537                         config->background = 0;
538                         break;
539
540                 case SET_BACKGROUND:
541                         noarg(optc);
542                         config->background = 1;
543                         break;
544
545                 case SET_NAME:
546                         config->name = argvalstr(optc);
547                         break;
548
549                 case SET_MODE:
550                         config->mode = argvalenum(optc, mode_desc);
551                         break;
552
553                 case DBUS_CLIENT:
554                         list_add(&config->dbus_clients, argvalstr(optc));
555                         break;
556
557                 case DBUS_SERVICE:
558                         list_add(&config->dbus_servers, argvalstr(optc));
559                         break;
560
561                 case WS_CLIENT:
562                         list_add(&config->ws_clients, argvalstr(optc));
563                         break;
564
565                 case WS_SERVICE:
566                         list_add(&config->ws_servers, argvalstr(optc));
567                         break;
568
569                 case SO_BINDING:
570                         list_add(&config->so_bindings, argvalstr(optc));
571                         break;
572
573                 case SET_TRACEREQ:
574                         config->tracereq = argvalenum(optc, tracereq_desc);
575                         break;
576
577                 case SET_TRACEDITF:
578                         config->traceditf = argvalenum(optc, traceditf_desc);
579                         break;
580
581                 case SET_TRACESVC:
582                         config->tracesvc = argvalenum(optc, tracesvc_desc);
583                         break;
584
585                 case SET_TRACEEVT:
586                         config->traceevt = argvalenum(optc, traceevt_desc);
587                         break;
588
589                 case SET_NO_HTTPD:
590                         noarg(optc);
591                         config->noHttpd = 1;
592                         break;
593
594                 case SET_EXEC:
595                         config->exec = make_exec(&argv[optind]);
596                         optind = argc; /* stop option scanning */
597                         break;
598
599                 case SET_RNDTOKEN:
600                         config->random_token = 1;
601                         break;
602
603 #if defined(WITH_MONITORING_OPTION)
604                 case SET_MONITORING:
605                         config->monitoring = 1;
606                         break;
607 #endif
608
609                 case DISPLAY_VERSION:
610                         noarg(optc);
611                         printVersion(stdout);
612                         exit(0);
613
614                 case DISPLAY_HELP:
615                         printHelp(stdout, programName);
616                         exit(0);
617
618                 default:
619                         exit(1);
620                 }
621         }
622         free(gnuOptions);
623 }
624
625 static void fulfill_config(struct afb_config *config)
626 {
627         // default HTTP port
628         if (config->httpdPort == 0)
629                 config->httpdPort = 1234;
630
631         // default binding API timeout
632         if (config->apiTimeout == 0)
633                 config->apiTimeout = DEFLT_API_TIMEOUT;
634
635         // default AUTH_TOKEN
636         if (config->random_token)
637                 config->token = NULL;
638
639         // cache timeout default one hour
640         if (config->cacheTimeout == 0)
641                 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
642
643         // cache timeout default one hour
644         if (config->cntxTimeout == 0)
645                 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
646
647         // max count of sessions
648         if (config->nbSessionMax == 0)
649                 config->nbSessionMax = CTX_NBCLIENTS;
650
651         /* set directories */
652         if (config->workdir == NULL)
653                 config->workdir = ".";
654
655         if (config->rootdir == NULL)
656                 config->rootdir = ".";
657
658         if (config->uploaddir == NULL)
659                 config->uploaddir = ".";
660
661         // if no Angular/HTML5 rootbase let's try '/' as default
662         if (config->rootbase == NULL)
663                 config->rootbase = "/opa";
664
665         if (config->rootapi == NULL)
666                 config->rootapi = "/api";
667
668         if (config->ldpaths == NULL && config->weak_ldpaths == NULL && !config->no_ldpaths)
669                 list_add(&config->ldpaths, BINDING_INSTALL_DIR);
670
671 #if defined(WITH_MONITORING_OPTION)
672         if (config->monitoring)
673                 list_add(&config->aliases, strdup("/monitoring:"BINDING_INSTALL_DIR"/monitoring"));
674 #endif
675
676         // if no config dir create a default path from uploaddir
677         if (config->console == NULL) {
678                 config->console = malloc(512);
679                 strncpy(config->console, config->uploaddir, 512);
680                 strncat(config->console, "/AFB-console.out", 512);
681         }
682 }
683
684 void afb_config_dump(struct afb_config *config)
685 {
686         struct afb_config_list *l;
687         struct enumdesc *e;
688         char **v;
689
690 #define NN(x)   (x)?:""
691 #define P(...)  fprintf(stderr, __VA_ARGS__)
692 #define PF(x)   P("-- %15s: ", #x)
693 #define PE      P("\n")
694 #define S(x)    PF(x);P("%s",NN(config->x));PE;
695 #define D(x)    PF(x);P("%d",config->x);PE;
696 #define H(x)    PF(x);P("%x",config->x);PE;
697 #define B(x)    PF(x);P("%s",config->x?"yes":"no");PE;
698 #define L(x)    PF(x);l=config->x;if(l){P("%s\n",NN(l->value));for(l=l->next;l;l=l->next)P("-- %15s  %s\n","",NN(l->value));}else PE;
699 #define E(x,d)  for(e=d;e->name&&e->value!=config->x;e++);if(e->name){PF(x);P("%s",e->name);PE;}else{D(x);}
700 #define V(x)    P("-- %15s:", #x);for(v=config->x;v&&*v;v++)P(" %s",*v); PE;
701
702         P("---BEGIN-OF-CONFIG---\n");
703         S(console)
704         S(rootdir)
705         S(roothttp)
706         S(rootbase)
707         S(rootapi)
708         S(workdir)
709         S(uploaddir)
710         S(token)
711         S(name)
712
713         L(aliases)
714         L(dbus_clients)
715         L(dbus_servers)
716         L(ws_clients)
717         L(ws_servers)
718         L(so_bindings)
719         L(ldpaths)
720         L(weak_ldpaths)
721         L(calls)
722
723         V(exec)
724
725         D(httpdPort)
726         D(cacheTimeout)
727         D(apiTimeout)
728         D(cntxTimeout)
729         D(nbSessionMax)
730
731         E(mode,mode_desc)
732         E(tracereq,tracereq_desc)
733         E(traceditf,traceditf_desc)
734         E(tracesvc,tracesvc_desc)
735         E(traceevt,traceevt_desc)
736
737         B(no_ldpaths)
738         B(noHttpd)
739         B(background)
740         B(monitoring)
741         B(random_token)
742         P("---END-OF-CONFIG---\n");
743
744 #undef V
745 #undef E
746 #undef L
747 #undef B
748 #undef H
749 #undef D
750 #undef S
751 #undef PE
752 #undef PF
753 #undef P
754 #undef NN
755 }
756
757 static void on_environment_list(struct afb_config_list **to, const char *name)
758 {
759         char *value = getenv(name);
760
761         if (value)
762                 list_add(to, value);
763 }
764
765 static void on_environment_enum(int *to, const char *name, struct enumdesc *desc)
766 {
767         char *value = getenv(name);
768
769         if (value) {
770                 while (desc->name) {
771                         if (strcmp(desc->name, value))
772                                 desc++;
773                         else {
774                                 *to = desc->value;
775                                 return;
776                         }
777                 }
778                 WARNING("Unknown value %s for environment variable %s, ignored", value, name);
779         }
780 }
781
782 static void parse_environment(struct afb_config *config)
783 {
784         on_environment_enum(&config->tracereq, "AFB_TRACEREQ", tracereq_desc);
785         on_environment_enum(&config->traceditf, "AFB_TRACEDITF", traceditf_desc);
786         on_environment_enum(&config->tracesvc, "AFB_TRACESVC", tracesvc_desc);
787         on_environment_enum(&config->traceevt, "AFB_TRACEEVT", traceevt_desc);
788         on_environment_list(&config->ldpaths, "AFB_LDPATHS");
789 }
790
791 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
792 {
793         struct afb_config *result;
794
795         result = calloc(1, sizeof *result);
796
797         parse_environment(result);
798         parse_arguments(argc, argv, result);
799         fulfill_config(result);
800         if (verbosity >= 3)
801                 afb_config_dump(result);
802         return result;
803 }
804