2 * Copyright (C) 2015-2019 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <json-c/json.h>
29 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
30 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
35 #include "afb-hook-flags.h"
36 #include "wrap-json.h"
39 #define d2s(x) _d2s_(x)
41 #if !defined(BINDING_INSTALL_DIR)
42 #error "you should define BINDING_INSTALL_DIR"
44 #if !defined(AFB_VERSION)
45 #error "you should define AFB_VERSION"
49 * The default timeout of sessions in seconds
51 #define DEFAULT_SESSION_TIMEOUT 32000000
54 * The default timeout of api calls in seconds
56 #define DEFAULT_API_TIMEOUT 20
59 * The default timeout of cache in seconds
61 #if WITH_LIBMICROHTTPD
62 #define DEFAULT_CACHE_TIMEOUT 100000
66 * The default maximum count of sessions
68 #define DEFAULT_MAX_SESSION_COUNT 200
71 * The default HTTP port to serve
73 #if WITH_LIBMICROHTTPD
74 #define DEFAULT_HTTP_PORT 1234
77 // Define command line option
78 #define SET_BACKGROUND 1
79 #define SET_FOREGROUND 2
80 #define SET_ROOT_DIR 3
82 #if WITH_LIBMICROHTTPD
83 #define SET_ROOT_BASE 4
84 #define SET_ROOT_API 5
86 #define SET_CACHE_TIMEOUT 7
89 #if WITH_DYNAMIC_BINDING
91 #define ADD_WEAK_LDPATH 11
92 #define SET_NO_LDPATH 12
94 #define SET_API_TIMEOUT 13
95 #define SET_SESSION_TIMEOUT 14
97 #define SET_SESSIONMAX 15
99 #define ADD_WS_CLIENT 16
100 #define ADD_WS_SERVICE 17
102 #if WITH_LIBMICROHTTPD
103 #define SET_ROOT_HTTP 18
104 #define SET_NO_HTTPD 19
107 #define SET_TRACEEVT 20
108 #define SET_TRACESES 21
109 #define SET_TRACEREQ 22
110 #define SET_TRACEAPI 23
111 #define SET_TRACEGLOB 24
112 #if !defined(REMOVE_LEGACY_TRACE)
113 #define SET_TRACEDITF 25
114 #define SET_TRACESVC 26
116 #define SET_TRAP_FAULTS 27
118 #if WITH_DBUS_TRANSPARENCY
119 # define ADD_DBUS_CLIENT 30
120 # define ADD_DBUS_SERVICE 31
123 #define ADD_AUTO_API 'A'
124 #if WITH_DYNAMIC_BINDING
125 #define ADD_BINDING 'b'
127 #define SET_CONFIG 'C'
128 #define SET_COLOR 'c'
129 #define SET_DAEMON 'D'
134 #define SET_MONITORING 'M'
137 #define SET_OUTPUT 'o'
139 #define SET_QUIET 'q'
140 #if WITH_LIBMICROHTTPD
141 #define SET_RANDOM_TOKEN 'r'
144 #if WITH_LIBMICROHTTPD
145 #define SET_TOKEN 't'
146 #define SET_UPLOAD_DIR 'u'
148 #define GET_VERSION 'V'
149 #define SET_VERBOSE 'v'
150 #define SET_WORK_DIR 'w'
151 #define DUMP_CONFIG 'Z'
153 /* structure for defining of options */
155 int id; /* id of the option */
156 int has_arg; /* is a value required */
157 char *name; /* long name of the option */
158 char *help; /* help text */
161 /* definition of options */
162 static struct option_desc optdefs[] = {
164 {SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"},
165 {SET_COLOR, 0, "color", "Colorize the ouput"},
166 {SET_QUIET, 0, "quiet", "Quiet Mode, repeat to decrease verbosity"},
167 {SET_LOG, 1, "log", "Tune log level"},
169 {SET_FOREGROUND, 0, "foreground", "Get all in foreground mode"},
170 {SET_BACKGROUND, 0, "background", "Get all in background mode"},
171 {SET_DAEMON, 0, "daemon", "Get all in background mode"},
173 {SET_NAME, 1, "name", "Set the visible name"},
175 #if WITH_LIBMICROHTTPD
176 {SET_NO_HTTPD, 0, "no-httpd", "Forbid HTTP service"},
177 {SET_PORT, 1, "port", "HTTP listening TCP port [default " d2s(DEFAULT_HTTP_PORT) "]"},
178 {SET_ROOT_HTTP, 1, "roothttp", "HTTP Root Directory [default no root http (files not served but apis still available)]"},
179 {SET_ROOT_BASE, 1, "rootbase", "Angular Base Root URL [default /opa]"},
180 {SET_ROOT_API, 1, "rootapi", "HTML Root API URL [default /api]"},
181 {ADD_ALIAS, 1, "alias", "Multiple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
182 {SET_UPLOAD_DIR, 1, "uploaddir", "Directory for uploading files [default: workdir] relative to workdir"},
183 {SET_CACHE_TIMEOUT, 1, "cache-eol", "Client cache end of live [default " d2s(DEFAULT_CACHE_TIMEOUT) "]"},
184 {SET_TOKEN, 1, "token", "Initial Secret [default=random, use --token="" to allow any token]"},
185 {SET_RANDOM_TOKEN, 0, "random-token","Enforce a random token"},
188 {SET_API_TIMEOUT, 1, "apitimeout", "Binding API timeout in seconds [default " d2s(DEFAULT_API_TIMEOUT) "]"},
189 {SET_SESSION_TIMEOUT, 1, "cntxtimeout", "Client Session Context Timeout [default " d2s(DEFAULT_SESSION_TIMEOUT) "]"},
191 {SET_WORK_DIR, 1, "workdir", "Set the working directory [default: $PWD or current working directory]"},
192 {SET_ROOT_DIR, 1, "rootdir", "Root Directory of the application [default: workdir] relative to workdir"},
194 #if WITH_DYNAMIC_BINDING
195 {ADD_LDPATH, 1, "ldpaths", "Load bindings from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
196 {ADD_BINDING, 1, "binding", "Load the binding of path"},
197 {ADD_WEAK_LDPATH, 1, "weak-ldpaths","Same as --ldpaths but ignore errors"},
198 {SET_NO_LDPATH, 0, "no-ldpaths", "Discard default ldpaths loading"},
201 {GET_VERSION, 0, "version", "Display version and copyright"},
202 {GET_HELP, 0, "help", "Display this help"},
204 #if WITH_DBUS_TRANSPARENCY
205 {ADD_DBUS_CLIENT, 1, "dbus-client", "Bind to an afb service through dbus"},
206 {ADD_DBUS_SERVICE, 1, "dbus-server", "Provide an afb service through dbus"},
208 {ADD_WS_CLIENT, 1, "ws-client", "Bind to an afb service through websocket"},
209 {ADD_WS_SERVICE, 1, "ws-server", "Provide an afb service through websockets"},
211 {ADD_AUTO_API, 1, "auto-api", "Automatic load of api of the given directory"},
213 {SET_SESSIONMAX, 1, "session-max", "Max count of session simultaneously [default " d2s(DEFAULT_MAX_SESSION_COUNT) "]"},
216 {SET_TRACEREQ, 1, "tracereq", "Log the requests: none, common, extra, all"},
217 {SET_TRACEEVT, 1, "traceevt", "Log the events: none, common, extra, all"},
218 {SET_TRACESES, 1, "traceses", "Log the sessions: none, all"},
219 {SET_TRACEAPI, 1, "traceapi", "Log the apis: none, common, api, event, all"},
220 {SET_TRACEGLOB, 1, "traceglob", "Log the globals: none, all"},
221 #if !defined(REMOVE_LEGACY_TRACE)
222 {SET_TRACEDITF, 1, "traceditf", "Log the daemons: no, common, all"},
223 {SET_TRACESVC, 1, "tracesvc", "Log the services: no, all"},
227 {ADD_CALL, 1, "call", "Call at start, format of val: API/VERB:json-args"},
229 {SET_EXEC, 0, "exec", "Execute the remaining arguments"},
232 {SET_MONITORING, 0, "monitoring", "Enable HTTP monitoring at <ROOT>/monitoring/"},
235 {SET_CONFIG, 1, "config", "Load options from the given config file"},
236 {DUMP_CONFIG, 0, "dump-config", "Dump the config to stdout and exit"},
238 {ADD_SET, 1, "set", "Set parameters ([API]/[KEY]:JSON or {\"API\":{\"KEY\":JSON}}" },
239 {SET_OUTPUT, 1, "output", "Redirect stdout and stderr to output file (when --daemon)"},
241 {SET_TRAP_FAULTS, 1, "trap-faults", "Trap faults: on, off, yes, no, true, false, 1, 0 (default: true)"},
248 static const char MONITORING_ALIAS[] = "/monitoring:"BINDING_INSTALL_DIR"/monitoring";
251 static const struct {
254 } default_optint_values[] = {
255 #if WITH_LIBMICROHTTPD
256 { SET_PORT, DEFAULT_HTTP_PORT },
257 { SET_API_TIMEOUT, DEFAULT_API_TIMEOUT },
258 { SET_CACHE_TIMEOUT, DEFAULT_CACHE_TIMEOUT },
260 { SET_SESSION_TIMEOUT, DEFAULT_SESSION_TIMEOUT },
261 { SET_SESSIONMAX, DEFAULT_MAX_SESSION_COUNT }
264 static const struct {
267 } default_optstr_values[] = {
268 #if WITH_LIBMICROHTTPD
269 { SET_UPLOAD_DIR, "." },
270 { SET_ROOT_BASE, "/opa" },
271 { SET_ROOT_API, "/api" },
273 { SET_WORK_DIR, "." },
274 { SET_ROOT_DIR, "." }
277 /**********************************
279 ***********************************/
281 static char *shortopts = NULL;
282 static int *id2idx = NULL;
284 static void *oomchk(void *ptr)
287 ERROR("Out of memory");
293 static char is_short_option(int val)
295 return (val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z') || (val >= '0' && val <= '9');
298 static void init_options()
305 for (i = 0 ; optdefs[i].name ; i++) {
309 if (is_short_option(id))
310 ns += 1 + !!optdefs[i].has_arg;
312 shortopts = oomchk(malloc(2 + ns));
313 id2idx = oomchk(calloc(1 + mi, sizeof *id2idx));
314 shortopts[ns = 0] = ':';
315 for (i = 0 ; optdefs[i].name ; i++) {
318 if (is_short_option(id)) {
319 shortopts[++ns] = (char)id;
320 if (optdefs[i].has_arg)
321 shortopts[++ns] = ':';
328 static const char *name_of_optid(int optid)
330 return optdefs[id2idx[optid]].name;
333 static int get_enum_val(const char *name, int optid, int (*func)(const char*))
339 ERROR("option [--%s] bad value (found %s)",
340 name_of_optid(optid), name);
347 /*----------------------------------------------------------
349 | print version and copyright
350 +--------------------------------------------------------- */
351 static void printVersion(FILE * file)
355 " AGL Framework Binder [AFB %s] "
357 #if WITH_DBUS_TRANSPARENCY
392 #if WITH_DYNAMIC_BINDING
398 #if WITH_LEGACY_BINDING_V1
404 #if WITH_LEGACY_BINDING_VDYN
410 #if WITH_LEGACY_BINDING_V2
420 " Copyright (C) 2015-2019 \"IoT.bzh\"\n"
421 " AFB comes with ABSOLUTELY NO WARRANTY.\n"
422 " Licence Apache 2\n"
426 /*----------------------------------------------------------
428 | print information from long option array
429 +--------------------------------------------------------- */
431 static void printHelp(FILE * file, const char *name)
434 char command[50], sht[4];
436 fprintf(file, "%s:\nallowed options\n", strrchr(name, '/') ? strrchr(name, '/') + 1 : name);
438 for (ind = 0; optdefs[ind].name != NULL; ind++) {
439 if (is_short_option(optdefs[ind].id)) {
441 sht[1] = (char)optdefs[ind].id;
444 sht[0] = sht[1] = sht[2] = ' ';
446 strcpy(command, optdefs[ind].name);
447 if (optdefs[ind].has_arg)
448 strcat(command, "=xxxx");
449 fprintf(file, " %s --%-17s %s\n", sht, command, optdefs[ind].help);
452 "Example:\n %s --verbose --port="
453 d2s(DEFAULT_HTTP_PORT)
455 #if WITH_DYNAMIC_BINDING
456 " --ldpaths=build/bindings:/usr/lib64/agl/bindings"
462 static void dump(struct json_object *config, FILE *file, const char *prefix, const char *title)
464 const char *head, *tail;
467 fprintf(file, "%s----BEGIN OF %s-----\n", prefix ?: "", title);
469 head = json_object_to_json_string_ext(config, JSON_C_TO_STRING_PRETTY
470 |JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_NOSLASHESCAPE);
473 fprintf(file, "%s\n", head);
476 for (tail = head ; *tail && *tail != '\n' ; tail++);
477 fprintf(file, "%s %.*s\n", prefix, (int)(tail - head), head);
478 head = tail + !!*tail;
483 fprintf(file, "%s----END OF %s-----\n", prefix ?: "", title);
486 /**********************************
488 ***********************************/
490 static struct json_object *joomchk(struct json_object *value)
492 return oomchk(value);
495 static struct json_object *to_jstr(const char *value)
497 return joomchk(json_object_new_string(value));
500 static struct json_object *to_jint(int value)
502 return joomchk(json_object_new_int(value));
505 static struct json_object *to_jbool(int value)
507 return joomchk(json_object_new_boolean(value));
510 /**********************************
512 ***********************************/
514 static int string_to_bool(const char *value)
516 static const char true_names[] = "1\0yes\0true\0on";
517 static const char false_names[] = "0\0no\0false\0off";
521 while (pos < sizeof true_names)
522 if (strcasecmp(value, &true_names[pos]))
523 pos += 1 + strlen(&true_names[pos]);
528 while (pos < sizeof false_names)
529 if (strcasecmp(value, &false_names[pos]))
530 pos += 1 + strlen(&false_names[pos]);
537 static void noarg(int optid)
540 ERROR("option [--%s] need no value (found %s)", name_of_optid(optid), optarg);
545 static const char *get_arg(int optid)
548 ERROR("option [--%s] needs a value i.e. --%s=xxx",
549 name_of_optid(optid), name_of_optid(optid));
555 static int get_arg_bool(int optid)
557 int value = string_to_bool(get_arg(optid));
559 ERROR("option [--%s] needs a boolean value: yes/no, true/false, on/off, 1/0",
560 name_of_optid(optid));
566 __attribute__((unused))
567 static void config_del(struct json_object *config, int optid)
569 return json_object_object_del(config, name_of_optid(optid));
572 static int config_has(struct json_object *config, int optid)
574 return json_object_object_get_ex(config, name_of_optid(optid), NULL);
577 static int config_has_bool(struct json_object *config, int optid)
579 struct json_object *x;
580 return json_object_object_get_ex(config, name_of_optid(optid), &x)
581 && json_object_get_boolean(x);
584 __attribute__((unused))
585 static int config_has_str(struct json_object *config, int optid, const char *val)
588 struct json_object *a;
590 if (!json_object_object_get_ex(config, name_of_optid(optid), &a))
593 if (!json_object_is_type(a, json_type_array))
594 return !strcmp(val, json_object_get_string(a));
596 n = (int)json_object_array_length(a);
597 for (i = 0 ; i < n ; i++) {
598 if (!strcmp(val, json_object_get_string(json_object_array_get_idx(a, i))))
604 static void config_set(struct json_object *config, int optid, struct json_object *val)
606 json_object_object_add(config, name_of_optid(optid), val);
609 static void config_set_str(struct json_object *config, int optid, const char *val)
611 config_set(config, optid, to_jstr(val));
614 static void config_set_optstr(struct json_object *config, int optid)
616 config_set_str(config, optid, get_arg(optid));
619 static void config_set_int(struct json_object *config, int optid, int value)
621 config_set(config, optid, to_jint(value));
624 static void config_set_bool(struct json_object *config, int optid, int value)
626 config_set(config, optid, to_jbool(value));
629 static void config_set_optint_base(struct json_object *config, int optid, int mini, int maxi, int base)
631 const char *beg, *end;
634 beg = get_arg(optid);
635 val = strtol(beg, (char**)&end, base);
636 if (*end || end == beg) {
637 ERROR("option [--%s] requires a valid integer (found %s)",
638 name_of_optid(optid), beg);
641 if (val < (long int)mini || val > (long int)maxi) {
642 ERROR("option [--%s] value %ld out of bounds (not in [%d , %d])",
643 name_of_optid(optid), val, mini, maxi);
646 config_set_int(config, optid, (int)val);
649 static void config_set_optint(struct json_object *config, int optid, int mini, int maxi)
651 return config_set_optint_base(config, optid, mini, maxi, 10);
654 __attribute__((unused))
655 static void config_set_optenum(struct json_object *config, int optid, int (*func)(const char*))
657 const char *name = get_arg(optid);
658 get_enum_val(name, optid, func);
659 config_set_str(config, optid, name);
662 static void config_add(struct json_object *config, int optid, struct json_object *val)
664 struct json_object *a;
665 if (!json_object_object_get_ex(config, name_of_optid(optid), &a)) {
666 a = joomchk(json_object_new_array());
667 json_object_object_add(config, name_of_optid(optid), a);
669 json_object_array_add(a, val);
672 static void config_add_str(struct json_object *config, int optid, const char *val)
674 config_add(config, optid, to_jstr(val));
677 static void config_add_optstr(struct json_object *config, int optid)
679 config_add_str(config, optid, get_arg(optid));
682 static void config_mix2_cb(void *closure, struct json_object *obj, const char *name)
684 struct json_object *dest, *base = closure;
689 if (!json_object_object_get_ex(base, name, &dest)) {
690 dest = joomchk(json_object_new_object());
691 json_object_object_add(base, name, dest);
693 if (json_object_is_type(obj, json_type_object))
694 wrap_json_object_add(dest, obj);
696 json_object_object_add(dest, "", json_object_get(obj));
699 static void config_mix2(struct json_object *config, int optid, struct json_object *val)
701 struct json_object *obj;
703 if (!json_object_object_get_ex(config, name_of_optid(optid), &obj)) {
704 obj = joomchk(json_object_new_object());
705 json_object_object_add(config, name_of_optid(optid), obj);
707 wrap_json_for_all(val, config_mix2_cb, obj);
710 static void config_mix2_str(struct json_object *config, int optid, const char *val)
713 const char *api, *key;
714 struct json_object *obj, *sub;
715 enum json_tokener_error jerr;
717 st1 = strcspn(val, "/:{[\"");
718 st2 = strcspn(&val[st1], ":{[\"");
719 if (val[st1] != '/' || val[st1 + st2] != ':') {
720 obj = json_tokener_parse_verbose(val, &jerr);
721 if (jerr != json_tokener_success)
722 obj = json_object_new_string(val);
724 api = st1 == 0 ? "*" : strndupa(val, st1);
726 key = st2 <= 1 || (st2 == 2 && *val == '*') ? NULL : strndupa(val, st2 - 1);
728 sub = json_tokener_parse_verbose(val, &jerr);
729 if (jerr != json_tokener_success)
730 sub = json_object_new_string(val);
733 obj = json_object_new_object();
734 json_object_object_add(obj, key, sub);
737 obj = json_object_new_object();
738 json_object_object_add(obj, api, sub);
740 config_mix2(config, optid, obj);
741 json_object_put(obj);
744 static void config_mix2_optstr(struct json_object *config, int optid)
746 config_mix2_str(config, optid, get_arg(optid));
749 /*---------------------------------------------------------
751 +--------------------------------------------------------- */
753 static void set_log(const char *args)
755 char o = 0, s, *p, *i = strdupa(args);
758 for(;;) switch (*i) {
771 while (isalpha(*p)) p++;
774 lvl = verbose_level_of_name(i);
778 ERROR("Bad log name '%s' in %s", i, args);
796 /*---------------------------------------------------------
797 | Parse option and launch action
798 +--------------------------------------------------------- */
800 static void parse_arguments_inner(int argc, char **argv, struct json_object *config, struct option *options)
802 struct json_object *conf;
803 int optid, cind, dodump = 0;
807 optid = getopt_long(argc, argv, shortopts, options, NULL);
826 set_log(get_arg(optid));
830 config_set_optint(config, optid, 1024, 32767);
833 case SET_API_TIMEOUT:
834 case SET_SESSION_TIMEOUT:
835 #if WITH_LIBMICROHTTPD
836 case SET_CACHE_TIMEOUT:
838 config_set_optint(config, optid, 0, INT_MAX);
842 config_set_optint(config, optid, 1, INT_MAX);
846 #if WITH_LIBMICROHTTPD
855 config_set_optstr(config, optid);
858 #if WITH_DBUS_TRANSPARENCY
859 case ADD_DBUS_CLIENT:
860 case ADD_DBUS_SERVICE:
862 #if WITH_LIBMICROHTTPD
865 #if WITH_DYNAMIC_BINDING
867 case ADD_WEAK_LDPATH:
874 config_add_optstr(config, optid);
878 config_mix2_optstr(config, optid);
884 #if WITH_LIBMICROHTTPD
885 case SET_RANDOM_TOKEN:
888 #if WITH_DYNAMIC_BINDING
891 #if WITH_MONITORING || WITH_LIBMICROHTTPD || WITH_DYNAMIC_BINDING
893 config_set_bool(config, optid, 1);
901 config_set_bool(config, SET_DAEMON, optid != SET_FOREGROUND);
904 case SET_TRAP_FAULTS:
905 config_set_bool(config, optid, get_arg_bool(optid));
911 config_set_optenum(config, optid, afb_hook_flags_xreq_from_text);
915 config_set_optenum(config, optid, afb_hook_flags_evt_from_text);
919 config_set_optenum(config, optid, afb_hook_flags_session_from_text);
923 config_set_optenum(config, optid, afb_hook_flags_api_from_text);
927 config_set_optenum(config, optid, afb_hook_flags_global_from_text);
930 #if !defined(REMOVE_LEGACY_TRACE)
932 config_set_optenum(config, optid, afb_hook_flags_legacy_ditf_from_text);
936 config_set_optenum(config, optid, afb_hook_flags_legacy_svc_from_text);
942 if (optind == argc) {
943 ERROR("The option --exec requires arguments");
946 while (optind != argc)
947 config_add_str(config, optid, argv[optind++]);
951 conf = json_object_from_file(get_arg(optid));
953 ERROR("Can't read config file %s", get_arg(optid));
956 wrap_json_object_add(config, conf);
957 json_object_put(conf);
967 printVersion(stdout);
971 printHelp(stdout, argv[0]);
975 ERROR("Bad option detected, check %s", argv[cind]);
979 /* TODO: check for extra value */
982 dump(config, stdout, NULL, NULL);
987 static void parse_arguments(int argc, char **argv, struct json_object *config)
990 struct option *options;
992 /* create GNU getopt options from optdefs */
993 options = malloc((sizeof optdefs / sizeof * optdefs) * sizeof * options);
994 for (ind = 0; optdefs[ind].name; ind++) {
995 options[ind].name = optdefs[ind].name;
996 options[ind].has_arg = optdefs[ind].has_arg;
997 options[ind].flag = NULL;
998 options[ind].val = optdefs[ind].id;
1000 memset(&options[ind], 0, sizeof options[ind]);
1002 /* parse the arguments */
1003 parse_arguments_inner(argc, argv, config, options);
1005 /* release the memory of options */
1009 static void fulfill_config(struct json_object *config)
1013 for (i = 0 ; i < sizeof default_optint_values / sizeof * default_optint_values ; i++)
1014 if (!config_has(config, default_optint_values[i].optid))
1015 config_set_int(config, default_optint_values[i].optid, default_optint_values[i].valdef);
1017 for (i = 0 ; i < sizeof default_optstr_values / sizeof * default_optstr_values ; i++)
1018 if (!config_has(config, default_optstr_values[i].optid))
1019 config_set_str(config, default_optstr_values[i].optid, default_optstr_values[i].valdef);
1021 // default AUTH_TOKEN
1022 #if WITH_LIBMICROHTTPD
1023 if (config_has_bool(config, SET_RANDOM_TOKEN))
1024 config_del(config, SET_TOKEN);
1026 #if WITH_DYNAMIC_BINDING
1027 if (!config_has(config, ADD_LDPATH) && !config_has(config, ADD_WEAK_LDPATH) && !config_has_bool(config, SET_NO_LDPATH))
1028 config_add_str(config, ADD_LDPATH, BINDING_INSTALL_DIR);
1032 if (config_has_bool(config, SET_MONITORING) && !config_has_str(config, ADD_ALIAS, MONITORING_ALIAS))
1033 config_add_str(config, ADD_ALIAS, MONITORING_ALIAS);
1036 #if !defined(REMOVE_LEGACY_TRACE) && 0
1037 config->traceapi |= config->traceditf | config->tracesvc;
1041 static void on_environment(struct json_object *config, int optid, const char *name, void (*func)(struct json_object*, int, const char*))
1043 char *value = getenv(name);
1045 if (value && *value)
1046 func(config, optid, value);
1049 __attribute__((unused))
1050 static void on_environment_enum(struct json_object *config, int optid, const char *name, int (*func)(const char*))
1052 char *value = getenv(name);
1055 if (func(value) == -1)
1056 WARNING("Unknown value %s for environment variable %s, ignored", value, name);
1058 config_set_str(config, optid, value);
1062 static void on_environment_bool(struct json_object *config, int optid, const char *name)
1064 char *value = getenv(name);
1068 asbool = string_to_bool(value);
1070 WARNING("Unknown value %s for environment variable %s, ignored", value, name);
1072 config_set_bool(config, optid, asbool);
1076 static void parse_environment(struct json_object *config)
1079 on_environment_enum(config, SET_TRACEREQ, "AFB_TRACEREQ", afb_hook_flags_xreq_from_text);
1080 on_environment_enum(config, SET_TRACEEVT, "AFB_TRACEEVT", afb_hook_flags_evt_from_text);
1081 on_environment_enum(config, SET_TRACESES, "AFB_TRACESES", afb_hook_flags_session_from_text);
1082 on_environment_enum(config, SET_TRACEAPI, "AFB_TRACEAPI", afb_hook_flags_api_from_text);
1083 on_environment_enum(config, SET_TRACEGLOB, "AFB_TRACEGLOB", afb_hook_flags_global_from_text);
1084 #if !defined(REMOVE_LEGACY_TRACE)
1085 on_environment_enum(config, SET_TRACEDITF, "AFB_TRACEDITF", afb_hook_flags_legacy_ditf_from_text);
1086 on_environment_enum(config, SET_TRACESVC, "AFB_TRACESVC", afb_hook_flags_legacy_svc_from_text);
1089 #if WITH_DYNAMIC_BINDING
1090 on_environment(config, ADD_LDPATH, "AFB_LDPATHS", config_add_str);
1092 on_environment(config, ADD_SET, "AFB_SET", config_mix2_str);
1093 on_environment_bool(config, SET_TRAP_FAULTS, "AFB_TRAP_FAULTS");
1096 struct json_object *afb_args_parse(int argc, char **argv)
1098 struct json_object *result;
1102 result = json_object_new_object();
1104 parse_environment(result);
1105 parse_arguments(argc, argv, result);
1106 fulfill_config(result);
1107 if (verbose_wants(Log_Level_Info))
1108 dump(result, stderr, "--", "CONFIG");