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(AFB_VERSION)
42 #error "you should define AFB_VERSION"
46 * The default timeout of sessions in seconds
48 #define DEFAULT_SESSION_TIMEOUT 32000000
51 * The default timeout of api calls in seconds
53 #define DEFAULT_API_TIMEOUT 20
56 * The default timeout of cache in seconds
58 #define DEFAULT_CACHE_TIMEOUT 100000
61 * The default maximum count of sessions
63 #define DEFAULT_MAX_SESSION_COUNT 200
66 * The default HTTP port to serve
68 #define DEFAULT_HTTP_PORT 1234
70 // Define command line option
71 #define SET_BACKGROUND 1
72 #define SET_FOREGROUND 2
73 #define SET_ROOT_DIR 3
74 #define SET_ROOT_BASE 4
75 #define SET_ROOT_API 5
78 #define SET_CACHE_TIMEOUT 7
80 #if WITH_DYNAMIC_BINDING
82 #define ADD_WEAK_LDPATH 11
83 #define SET_NO_LDPATH 12
85 #define SET_API_TIMEOUT 13
86 #define SET_SESSION_TIMEOUT 14
88 #define SET_SESSIONMAX 15
90 #define ADD_WS_CLIENT 16
91 #define ADD_WS_SERVICE 17
93 #define SET_ROOT_HTTP 18
95 #define SET_NO_HTTPD 19
97 #define SET_TRACEEVT 20
98 #define SET_TRACESES 21
99 #define SET_TRACEREQ 22
100 #define SET_TRACEAPI 23
101 #define SET_TRACEGLOB 24
102 #if !defined(REMOVE_LEGACY_TRACE)
103 #define SET_TRACEDITF 25
104 #define SET_TRACESVC 26
106 #define SET_TRAP_FAULTS 27
108 #if WITH_DBUS_TRANSPARENCY
109 # define ADD_DBUS_CLIENT 30
110 # define ADD_DBUS_SERVICE 31
113 #define ADD_AUTO_API 'A'
114 #if WITH_DYNAMIC_BINDING
115 #define ADD_BINDING 'b'
117 #define SET_CONFIG 'C'
118 #define SET_COLOR 'c'
119 #define SET_DAEMON 'D'
123 #if defined(WITH_MONITORING_OPTION)
124 #define SET_MONITORING 'M'
127 #define SET_OUTPUT 'o'
130 #define SET_QUIET 'q'
131 #define SET_RANDOM_TOKEN 'r'
133 #define SET_TOKEN 't'
134 #define SET_UPLOAD_DIR 'u'
135 #define GET_VERSION 'V'
136 #define SET_VERBOSE 'v'
137 #define SET_WORK_DIR 'w'
138 #define DUMP_CONFIG 'Z'
140 /* structure for defining of options */
142 int id; /* id of the option */
143 int has_arg; /* is a value required */
144 char *name; /* long name of the option */
145 char *help; /* help text */
148 /* definition of options */
149 static struct option_desc optdefs[] = {
151 {SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"},
152 {SET_COLOR, 0, "color", "Colorize the ouput"},
153 {SET_QUIET, 0, "quiet", "Quiet Mode, repeat to decrease verbosity"},
154 {SET_LOG, 1, "log", "Tune log level"},
156 {SET_FOREGROUND, 0, "foreground", "Get all in foreground mode"},
157 {SET_BACKGROUND, 0, "background", "Get all in background mode"},
158 {SET_DAEMON, 0, "daemon", "Get all in background mode"},
160 {SET_NAME, 1, "name", "Set the visible name"},
162 {SET_PORT, 1, "port", "HTTP listening TCP port [default " d2s(DEFAULT_HTTP_PORT) "]"},
163 {SET_ROOT_HTTP, 1, "roothttp", "HTTP Root Directory [default no root http (files not served but apis still available)]"},
164 {SET_ROOT_BASE, 1, "rootbase", "Angular Base Root URL [default /opa]"},
165 {SET_ROOT_API, 1, "rootapi", "HTML Root API URL [default /api]"},
166 {ADD_ALIAS, 1, "alias", "Multiple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
168 {SET_API_TIMEOUT, 1, "apitimeout", "Binding API timeout in seconds [default " d2s(DEFAULT_API_TIMEOUT) "]"},
169 {SET_SESSION_TIMEOUT, 1, "cntxtimeout", "Client Session Context Timeout [default " d2s(DEFAULT_SESSION_TIMEOUT) "]"},
170 {SET_CACHE_TIMEOUT, 1, "cache-eol", "Client cache end of live [default " d2s(DEFAULT_CACHE_TIMEOUT) "]"},
172 {SET_WORK_DIR, 1, "workdir", "Set the working directory [default: $PWD or current working directory]"},
173 {SET_UPLOAD_DIR, 1, "uploaddir", "Directory for uploading files [default: workdir] relative to workdir"},
174 {SET_ROOT_DIR, 1, "rootdir", "Root Directory of the application [default: workdir] relative to workdir"},
176 #if WITH_DYNAMIC_BINDING
178 {ADD_LDPATH, 1, "ldpaths", "Load bindings from dir1:dir2:..."
179 #if defined(INTRINSIC_BINDING_DIR)
180 "[default = " INTRINSIC_BINDING_DIR "]"
183 {ADD_BINDING, 1, "binding", "Load the binding of path"},
184 {ADD_WEAK_LDPATH, 1, "weak-ldpaths","Same as --ldpaths but ignore errors"},
185 {SET_NO_LDPATH, 0, "no-ldpaths", "Discard default ldpaths loading"},
187 {JS_API, 1, "jsapi", "Load the javascript api of path"},
189 {SET_TOKEN, 1, "token", "Initial Secret [default=random, use --token="" to allow any token]"},
190 {SET_RANDOM_TOKEN, 0, "random-token","Enforce a random token"},
192 {GET_VERSION, 0, "version", "Display version and copyright"},
193 {GET_HELP, 0, "help", "Display this help"},
195 #if WITH_DBUS_TRANSPARENCY
196 {ADD_DBUS_CLIENT, 1, "dbus-client", "Bind to an afb service through dbus"},
197 {ADD_DBUS_SERVICE, 1, "dbus-server", "Provide an afb service through dbus"},
199 {ADD_WS_CLIENT, 1, "ws-client", "Bind to an afb service through websocket"},
200 {ADD_WS_SERVICE, 1, "ws-server", "Provide an afb service through websockets"},
202 {ADD_AUTO_API, 1, "auto-api", "Automatic load of api of the given directory"},
204 {SET_SESSIONMAX, 1, "session-max", "Max count of session simultaneously [default " d2s(DEFAULT_MAX_SESSION_COUNT) "]"},
207 {SET_TRACEREQ, 1, "tracereq", "Log the requests: none, common, extra, all"},
208 {SET_TRACEEVT, 1, "traceevt", "Log the events: none, common, extra, all"},
209 {SET_TRACESES, 1, "traceses", "Log the sessions: none, all"},
210 {SET_TRACEAPI, 1, "traceapi", "Log the apis: none, common, api, event, all"},
211 {SET_TRACEGLOB, 1, "traceglob", "Log the globals: none, all"},
212 #if !defined(REMOVE_LEGACY_TRACE)
213 {SET_TRACEDITF, 1, "traceditf", "Log the daemons: no, common, all"},
214 {SET_TRACESVC, 1, "tracesvc", "Log the services: no, all"},
218 {ADD_CALL, 1, "call", "Call at start, format of val: API/VERB:json-args"},
220 {SET_NO_HTTPD, 0, "no-httpd", "Forbid HTTP service"},
221 {SET_EXEC, 0, "exec", "Execute the remaining arguments"},
223 #if defined(WITH_MONITORING_OPTION)
224 {SET_MONITORING, 0, "monitoring", "Enable HTTP monitoring at <ROOT>/monitoring/"},
227 {SET_CONFIG, 1, "config", "Load options from the given config file"},
228 {DUMP_CONFIG, 0, "dump-config", "Dump the config to stdout and exit"},
230 {ADD_SET, 1, "set", "Set parameters ([API]/[KEY]:JSON or {\"API\":{\"KEY\":JSON}}" },
231 {SET_OUTPUT, 1, "output", "Redirect stdout and stderr to output file (when --daemon)"},
233 {SET_TRAP_FAULTS, 1, "trap-faults", "Trap faults: on, off, yes, no, true, false, 1, 0 (default: true)"},
239 #if defined(WITH_MONITORING_OPTION)
240 static const char MONITORING_ALIAS[] = "/monitoring:"INTRINSIC_BINDING_DIR"/monitoring";
243 static const struct {
246 } default_optint_values[] = {
247 { SET_PORT, DEFAULT_HTTP_PORT },
248 { SET_API_TIMEOUT, DEFAULT_API_TIMEOUT },
249 { SET_CACHE_TIMEOUT, DEFAULT_CACHE_TIMEOUT },
250 { SET_SESSION_TIMEOUT, DEFAULT_SESSION_TIMEOUT },
251 { SET_SESSIONMAX, DEFAULT_MAX_SESSION_COUNT }
254 static const struct {
257 } default_optstr_values[] = {
258 { SET_WORK_DIR, "." },
259 { SET_ROOT_DIR, "." },
260 { SET_UPLOAD_DIR, "." },
261 { SET_ROOT_BASE, "/opa" },
262 { SET_ROOT_API, "/api" }
265 /**********************************
267 ***********************************/
269 static char *shortopts = NULL;
270 static int *id2idx = NULL;
272 static void *oomchk(void *ptr)
275 ERROR("Out of memory");
281 static char is_short_option(int val)
283 return (val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z') || (val >= '0' && val <= '9');
286 static void init_options()
293 for (i = 0 ; optdefs[i].name ; i++) {
297 if (is_short_option(id))
298 ns += 1 + !!optdefs[i].has_arg;
300 shortopts = oomchk(malloc(2 + ns));
301 id2idx = oomchk(calloc(1 + mi, sizeof *id2idx));
302 shortopts[ns = 0] = ':';
303 for (i = 0 ; optdefs[i].name ; i++) {
306 if (is_short_option(id)) {
307 shortopts[++ns] = (char)id;
308 if (optdefs[i].has_arg)
309 shortopts[++ns] = ':';
316 static const char *name_of_optid(int optid)
318 return optdefs[id2idx[optid]].name;
321 static int get_enum_val(const char *name, int optid, int (*func)(const char*))
327 ERROR("option [--%s] bad value (found %s)",
328 name_of_optid(optid), name);
335 /*----------------------------------------------------------
337 | print version and copyright
338 +--------------------------------------------------------- */
339 static void printVersion(FILE * file)
343 " AGL Framework Binder [AFB %s] "
345 #if WITH_DBUS_TRANSPARENCY
352 #if defined(WITH_MONITORING_OPTION)
380 #if WITH_DYNAMIC_BINDING
386 #if WITH_LEGACY_BINDING_V1
389 #if WITH_LEGACY_BINDING_VDYN
392 #if WITH_LEGACY_BINDING_V2
400 " Copyright (C) 2015-2019 \"IoT.bzh\"\n"
401 " AFB comes with ABSOLUTELY NO WARRANTY.\n"
402 " Licence Apache 2\n"
406 /*----------------------------------------------------------
408 | print information from long option array
409 +--------------------------------------------------------- */
411 static void printHelp(FILE * file, const char *name)
414 char command[50], sht[4];
416 fprintf(file, "%s:\nallowed options\n", strrchr(name, '/') ? strrchr(name, '/') + 1 : name);
418 for (ind = 0; optdefs[ind].name != NULL; ind++) {
419 if (is_short_option(optdefs[ind].id)) {
421 sht[1] = (char)optdefs[ind].id;
424 sht[0] = sht[1] = sht[2] = ' ';
426 strcpy(command, optdefs[ind].name);
427 if (optdefs[ind].has_arg)
428 strcat(command, "=xxxx");
429 fprintf(file, " %s --%-17s %s\n", sht, command, optdefs[ind].help);
432 "Example:\n %s --verbose --port="
433 d2s(DEFAULT_HTTP_PORT)
435 #if WITH_DYNAMIC_BINDING
436 " --ldpaths=build/bindings:/usr/lib64/agl/bindings"
442 static void dump(struct json_object *config, FILE *file, const char *prefix, const char *title)
444 const char *head, *tail;
447 fprintf(file, "%s----BEGIN OF %s-----\n", prefix ?: "", title);
449 head = json_object_to_json_string_ext(config, JSON_C_TO_STRING_PRETTY
450 |JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_NOSLASHESCAPE);
453 fprintf(file, "%s\n", head);
456 for (tail = head ; *tail && *tail != '\n' ; tail++);
457 fprintf(file, "%s %.*s\n", prefix, (int)(tail - head), head);
458 head = tail + !!*tail;
463 fprintf(file, "%s----END OF %s-----\n", prefix ?: "", title);
466 /**********************************
468 ***********************************/
470 static struct json_object *joomchk(struct json_object *value)
472 return oomchk(value);
475 static struct json_object *to_jstr(const char *value)
477 return joomchk(json_object_new_string(value));
480 static struct json_object *to_jint(int value)
482 return joomchk(json_object_new_int(value));
485 static struct json_object *to_jbool(int value)
487 return joomchk(json_object_new_boolean(value));
490 /**********************************
492 ***********************************/
494 static int string_to_bool(const char *value)
496 static const char true_names[] = "1\0yes\0true\0on";
497 static const char false_names[] = "0\0no\0false\0off";
501 while (pos < sizeof true_names)
502 if (strcasecmp(value, &true_names[pos]))
503 pos += 1 + strlen(&true_names[pos]);
508 while (pos < sizeof false_names)
509 if (strcasecmp(value, &false_names[pos]))
510 pos += 1 + strlen(&false_names[pos]);
517 static void noarg(int optid)
520 ERROR("option [--%s] need no value (found %s)", name_of_optid(optid), optarg);
525 static const char *get_arg(int optid)
528 ERROR("option [--%s] needs a value i.e. --%s=xxx",
529 name_of_optid(optid), name_of_optid(optid));
535 static int get_arg_bool(int optid)
537 int value = string_to_bool(get_arg(optid));
539 ERROR("option [--%s] needs a boolean value: yes/no, true/false, on/off, 1/0",
540 name_of_optid(optid));
546 static void config_del(struct json_object *config, int optid)
548 return json_object_object_del(config, name_of_optid(optid));
551 static int config_has(struct json_object *config, int optid)
553 return json_object_object_get_ex(config, name_of_optid(optid), NULL);
556 static int config_has_bool(struct json_object *config, int optid)
558 struct json_object *x;
559 return json_object_object_get_ex(config, name_of_optid(optid), &x)
560 && json_object_get_boolean(x);
563 __attribute__((unused))
564 static int config_has_str(struct json_object *config, int optid, const char *val)
567 struct json_object *a;
569 if (!json_object_object_get_ex(config, name_of_optid(optid), &a))
572 if (!json_object_is_type(a, json_type_array))
573 return !strcmp(val, json_object_get_string(a));
575 n = (int)json_object_array_length(a);
576 for (i = 0 ; i < n ; i++) {
577 if (!strcmp(val, json_object_get_string(json_object_array_get_idx(a, i))))
583 static void config_set(struct json_object *config, int optid, struct json_object *val)
585 json_object_object_add(config, name_of_optid(optid), val);
588 static void config_set_str(struct json_object *config, int optid, const char *val)
590 config_set(config, optid, to_jstr(val));
593 static void config_set_optstr(struct json_object *config, int optid)
595 config_set_str(config, optid, get_arg(optid));
598 static void config_set_int(struct json_object *config, int optid, int value)
600 config_set(config, optid, to_jint(value));
603 static void config_set_bool(struct json_object *config, int optid, int value)
605 config_set(config, optid, to_jbool(value));
608 static void config_set_optint_base(struct json_object *config, int optid, int mini, int maxi, int base)
610 const char *beg, *end;
613 beg = get_arg(optid);
614 val = strtol(beg, (char**)&end, base);
615 if (*end || end == beg) {
616 ERROR("option [--%s] requires a valid integer (found %s)",
617 name_of_optid(optid), beg);
620 if (val < (long int)mini || val > (long int)maxi) {
621 ERROR("option [--%s] value %ld out of bounds (not in [%d , %d])",
622 name_of_optid(optid), val, mini, maxi);
625 config_set_int(config, optid, (int)val);
628 static void config_set_optint(struct json_object *config, int optid, int mini, int maxi)
630 return config_set_optint_base(config, optid, mini, maxi, 10);
633 __attribute__((unused))
634 static void config_set_optenum(struct json_object *config, int optid, int (*func)(const char*))
636 const char *name = get_arg(optid);
637 get_enum_val(name, optid, func);
638 config_set_str(config, optid, name);
641 static void config_add(struct json_object *config, int optid, struct json_object *val)
643 struct json_object *a;
644 if (!json_object_object_get_ex(config, name_of_optid(optid), &a)) {
645 a = joomchk(json_object_new_array());
646 json_object_object_add(config, name_of_optid(optid), a);
648 json_object_array_add(a, val);
651 static void config_add_str(struct json_object *config, int optid, const char *val)
653 config_add(config, optid, to_jstr(val));
656 static void config_add_optstr(struct json_object *config, int optid)
658 config_add_str(config, optid, get_arg(optid));
661 static void config_mix2_cb(void *closure, struct json_object *obj, const char *name)
663 struct json_object *dest, *base = closure;
668 if (!json_object_object_get_ex(base, name, &dest)) {
669 dest = joomchk(json_object_new_object());
670 json_object_object_add(base, name, dest);
672 if (json_object_is_type(obj, json_type_object))
673 wrap_json_object_add(dest, obj);
675 json_object_object_add(dest, "", json_object_get(obj));
678 static void config_mix2(struct json_object *config, int optid, struct json_object *val)
680 struct json_object *obj;
682 if (!json_object_object_get_ex(config, name_of_optid(optid), &obj)) {
683 obj = joomchk(json_object_new_object());
684 json_object_object_add(config, name_of_optid(optid), obj);
686 wrap_json_for_all(val, config_mix2_cb, obj);
689 static void config_mix2_str(struct json_object *config, int optid, const char *val)
692 const char *api, *key;
693 struct json_object *obj, *sub;
694 enum json_tokener_error jerr;
696 st1 = strcspn(val, "/:{[\"");
697 st2 = strcspn(&val[st1], ":{[\"");
698 if (val[st1] != '/' || val[st1 + st2] != ':') {
699 obj = json_tokener_parse_verbose(val, &jerr);
700 if (jerr != json_tokener_success)
701 obj = json_object_new_string(val);
703 api = st1 == 0 ? "*" : strndupa(val, st1);
705 key = st2 <= 1 || (st2 == 2 && *val == '*') ? NULL : strndupa(val, st2 - 1);
707 sub = json_tokener_parse_verbose(val, &jerr);
708 if (jerr != json_tokener_success)
709 sub = json_object_new_string(val);
712 obj = json_object_new_object();
713 json_object_object_add(obj, key, sub);
716 obj = json_object_new_object();
717 json_object_object_add(obj, api, sub);
719 config_mix2(config, optid, obj);
720 json_object_put(obj);
723 static void config_mix2_optstr(struct json_object *config, int optid)
725 config_mix2_str(config, optid, get_arg(optid));
728 /*---------------------------------------------------------
730 +--------------------------------------------------------- */
732 static void set_log(const char *args)
734 char o = 0, s, *p, *i = strdupa(args);
737 for(;;) switch (*i) {
750 while (isalpha(*p)) p++;
753 lvl = verbose_level_of_name(i);
757 ERROR("Bad log name '%s' in %s", i, args);
775 /*---------------------------------------------------------
776 | Parse option and launch action
777 +--------------------------------------------------------- */
779 static void parse_arguments_inner(int argc, char **argv, struct json_object *config, struct option *options)
781 struct json_object *conf;
782 int optid, cind, dodump = 0;
786 optid = getopt_long(argc, argv, shortopts, options, NULL);
805 set_log(get_arg(optid));
809 config_set_optint(config, optid, 1024, 32767);
812 case SET_API_TIMEOUT:
813 case SET_SESSION_TIMEOUT:
814 case SET_CACHE_TIMEOUT:
815 config_set_optint(config, optid, 0, INT_MAX);
819 config_set_optint(config, optid, 1, INT_MAX);
830 config_set_optstr(config, optid);
833 #if WITH_DBUS_TRANSPARENCY
834 case ADD_DBUS_CLIENT:
835 case ADD_DBUS_SERVICE:
838 #if WITH_DYNAMIC_BINDING
840 case ADD_WEAK_LDPATH:
848 config_add_optstr(config, optid);
852 config_mix2_optstr(config, optid);
855 #if defined(WITH_MONITORING_OPTION)
858 case SET_RANDOM_TOKEN:
860 #if WITH_DYNAMIC_BINDING
864 config_set_bool(config, optid, 1);
872 config_set_bool(config, SET_DAEMON, optid != SET_FOREGROUND);
875 case SET_TRAP_FAULTS:
876 config_set_bool(config, optid, get_arg_bool(optid));
882 config_set_optenum(config, optid, afb_hook_flags_xreq_from_text);
886 config_set_optenum(config, optid, afb_hook_flags_evt_from_text);
890 config_set_optenum(config, optid, afb_hook_flags_session_from_text);
894 config_set_optenum(config, optid, afb_hook_flags_api_from_text);
898 config_set_optenum(config, optid, afb_hook_flags_global_from_text);
901 #if !defined(REMOVE_LEGACY_TRACE)
903 config_set_optenum(config, optid, afb_hook_flags_legacy_ditf_from_text);
907 config_set_optenum(config, optid, afb_hook_flags_legacy_svc_from_text);
913 if (optind == argc) {
914 ERROR("The option --exec requires arguments");
917 while (optind != argc)
918 config_add_str(config, optid, argv[optind++]);
922 conf = json_object_from_file(get_arg(optid));
924 ERROR("Can't read config file %s", get_arg(optid));
927 wrap_json_object_add(config, conf);
928 json_object_put(conf);
938 printVersion(stdout);
942 printHelp(stdout, argv[0]);
946 ERROR("Bad option detected, check %s", argv[cind]);
950 /* TODO: check for extra value */
953 dump(config, stdout, NULL, NULL);
958 static void parse_arguments(int argc, char **argv, struct json_object *config)
961 struct option *options;
963 /* create GNU getopt options from optdefs */
964 options = malloc((sizeof optdefs / sizeof * optdefs) * sizeof * options);
965 for (ind = 0; optdefs[ind].name; ind++) {
966 options[ind].name = optdefs[ind].name;
967 options[ind].has_arg = optdefs[ind].has_arg;
968 options[ind].flag = NULL;
969 options[ind].val = optdefs[ind].id;
971 memset(&options[ind], 0, sizeof options[ind]);
973 /* parse the arguments */
974 parse_arguments_inner(argc, argv, config, options);
976 /* release the memory of options */
980 static void fulfill_config(struct json_object *config)
984 for (i = 0 ; i < sizeof default_optint_values / sizeof * default_optint_values ; i++)
985 if (!config_has(config, default_optint_values[i].optid))
986 config_set_int(config, default_optint_values[i].optid, default_optint_values[i].valdef);
988 for (i = 0 ; i < sizeof default_optstr_values / sizeof * default_optstr_values ; i++)
989 if (!config_has(config, default_optstr_values[i].optid))
990 config_set_str(config, default_optstr_values[i].optid, default_optstr_values[i].valdef);
992 // default AUTH_TOKEN
993 if (config_has_bool(config, SET_RANDOM_TOKEN))
994 config_del(config, SET_TOKEN);
996 #if WITH_DYNAMIC_BINDING && defined(INTRINSIC_BINDING_DIR)
997 if (!config_has(config, ADD_LDPATH) && !config_has(config, ADD_WEAK_LDPATH) && !config_has_bool(config, SET_NO_LDPATH))
998 config_add_str(config, ADD_LDPATH, INTRINSIC_BINDING_DIR);
1001 #if defined(WITH_MONITORING_OPTION)
1002 if (config_has_bool(config, SET_MONITORING) && !config_has_str(config, ADD_ALIAS, MONITORING_ALIAS))
1003 config_add_str(config, ADD_ALIAS, MONITORING_ALIAS);
1006 #if !defined(REMOVE_LEGACY_TRACE) && 0
1007 config->traceapi |= config->traceditf | config->tracesvc;
1011 static void on_environment(struct json_object *config, int optid, const char *name, void (*func)(struct json_object*, int, const char*))
1013 char *value = secure_getenv(name);
1015 if (value && *value)
1016 func(config, optid, value);
1019 __attribute__((unused))
1020 static void on_environment_enum(struct json_object *config, int optid, const char *name, int (*func)(const char*))
1022 char *value = secure_getenv(name);
1025 if (func(value) == -1)
1026 WARNING("Unknown value %s for environment variable %s, ignored", value, name);
1028 config_set_str(config, optid, value);
1032 static void on_environment_bool(struct json_object *config, int optid, const char *name)
1034 char *value = secure_getenv(name);
1038 asbool = string_to_bool(value);
1040 WARNING("Unknown value %s for environment variable %s, ignored", value, name);
1042 config_set_bool(config, optid, asbool);
1046 static void parse_environment(struct json_object *config)
1049 on_environment_enum(config, SET_TRACEREQ, "AFB_TRACEREQ", afb_hook_flags_xreq_from_text);
1050 on_environment_enum(config, SET_TRACEEVT, "AFB_TRACEEVT", afb_hook_flags_evt_from_text);
1051 on_environment_enum(config, SET_TRACESES, "AFB_TRACESES", afb_hook_flags_session_from_text);
1052 on_environment_enum(config, SET_TRACEAPI, "AFB_TRACEAPI", afb_hook_flags_api_from_text);
1053 on_environment_enum(config, SET_TRACEGLOB, "AFB_TRACEGLOB", afb_hook_flags_global_from_text);
1054 #if !defined(REMOVE_LEGACY_TRACE)
1055 on_environment_enum(config, SET_TRACEDITF, "AFB_TRACEDITF", afb_hook_flags_legacy_ditf_from_text);
1056 on_environment_enum(config, SET_TRACESVC, "AFB_TRACESVC", afb_hook_flags_legacy_svc_from_text);
1059 #if WITH_DYNAMIC_BINDING
1060 on_environment(config, ADD_LDPATH, "AFB_LDPATHS", config_add_str);
1062 on_environment(config, ADD_SET, "AFB_SET", config_mix2_str);
1063 on_environment_bool(config, SET_TRAP_FAULTS, "AFB_TRAP_FAULTS");
1066 struct json_object *afb_args_parse(int argc, char **argv)
1068 struct json_object *result;
1072 result = json_object_new_object();
1074 parse_environment(result);
1075 parse_arguments(argc, argv, result);
1076 fulfill_config(result);
1077 if (verbose_wants(Log_Level_Info))
1078 dump(result, stderr, "--", "CONFIG");