2 * Copyright (C) 2015-2018 "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
34 #include "afb-config.h"
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 #define DEFAULT_CACHE_TIMEOUT 100000
64 * The default maximum count of sessions
66 #define DEFAULT_MAX_SESSION_COUNT 200
69 * The default HTTP port to serve
71 #define DEFAULT_HTTP_PORT 1234
73 // Define command line option
74 #define SET_BACKGROUND 1
75 #define SET_FOREGROUND 2
76 #define SET_ROOT_DIR 3
77 #define SET_ROOT_BASE 4
78 #define SET_ROOT_API 5
81 #define SET_CACHE_TIMEOUT 7
87 #define SET_API_TIMEOUT 11
88 #define SET_SESSION_TIMEOUT 12
89 #define ADD_WEAK_LDPATH 13
90 #define SET_NO_LDPATH 14
92 #define SET_SESSIONMAX 15
94 #define ADD_WS_CLIENT 16
95 #define ADD_WS_SERVICE 17
97 #define SET_ROOT_HTTP 18
99 #define SET_NO_HTTPD 19
101 #define SET_TRACEEVT 20
102 #define SET_TRACESES 21
103 #define SET_TRACEREQ 22
104 #define SET_TRACEAPI 23
105 #define SET_TRACEGLOB 24
106 #if !defined(REMOVE_LEGACY_TRACE)
107 #define SET_TRACEDITF 25
108 #define SET_TRACESVC 26
110 #define SET_TRAP_FAULTS 27
112 #if defined(WITH_DBUS_TRANSPARENCY)
113 # define ADD_DBUS_CLIENT 30
114 # define ADD_DBUS_SERVICE 31
117 #define ADD_AUTO_API 'A'
118 #define ADD_BINDING 'b'
119 #define SET_CONFIG 'C'
120 #define SET_COLOR 'c'
121 #define SET_DAEMON 'D'
125 #if defined(WITH_MONITORING_OPTION)
126 #define SET_MONITORING 'M'
129 #define SET_OUTPUT 'o'
131 #define SET_QUIET 'q'
132 #define SET_RANDOM_TOKEN 'r'
134 #define SET_TOKEN 't'
135 #define SET_UPLOAD_DIR 'u'
136 #define GET_VERSION 'V'
137 #define SET_VERBOSE 'v'
138 #define SET_WORK_DIR 'w'
139 #define DUMP_CONFIG 'Z'
141 /* structure for defining of options */
143 int id; /* id of the option */
144 int has_arg; /* is a value required */
145 char *name; /* long name of the option */
146 char *help; /* help text */
149 /* definition of options */
150 static struct option_desc optdefs[] = {
152 {SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"},
153 {SET_COLOR, 0, "color", "Colorize the ouput"},
154 {SET_QUIET, 0, "quiet", "Quiet Mode, repeat to decrease verbosity"},
155 {SET_LOG, 1, "log", "Tune log level"},
157 {SET_FOREGROUND, 0, "foreground", "Get all in foreground mode"},
158 {SET_BACKGROUND, 0, "background", "Get all in background mode"},
159 {SET_DAEMON, 0, "daemon", "Get all in background mode"},
161 {SET_NAME, 1, "name", "Set the visible name"},
163 {SET_PORT, 1, "port", "HTTP listening TCP port [default " d2s(DEFAULT_HTTP_PORT) "]"},
164 {SET_ROOT_HTTP, 1, "roothttp", "HTTP Root Directory [default no root http (files not served but apis still available)]"},
165 {SET_ROOT_BASE, 1, "rootbase", "Angular Base Root URL [default /opa]"},
166 {SET_ROOT_API, 1, "rootapi", "HTML Root API URL [default /api]"},
167 {ADD_ALIAS, 1, "alias", "Multiple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
169 {SET_API_TIMEOUT, 1, "apitimeout", "Binding API timeout in seconds [default " d2s(DEFAULT_API_TIMEOUT) "]"},
170 {SET_SESSION_TIMEOUT, 1, "cntxtimeout", "Client Session Context Timeout [default " d2s(DEFAULT_SESSION_TIMEOUT) "]"},
171 {SET_CACHE_TIMEOUT, 1, "cache-eol", "Client cache end of live [default " d2s(DEFAULT_CACHE_TIMEOUT) "]"},
173 {SET_WORK_DIR, 1, "workdir", "Set the working directory [default: $PWD or current working directory]"},
174 {SET_UPLOAD_DIR, 1, "uploaddir", "Directory for uploading files [default: workdir] relative to workdir"},
175 {SET_ROOT_DIR, 1, "rootdir", "Root Directory of the application [default: workdir] relative to workdir"},
177 {ADD_LDPATH, 1, "ldpaths", "Load bindings from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
178 {ADD_BINDING, 1, "binding", "Load the binding of path"},
179 {ADD_WEAK_LDPATH, 1, "weak-ldpaths","Same as --ldpaths but ignore errors"},
180 {SET_NO_LDPATH, 0, "no-ldpaths", "Discard default ldpaths loading"},
182 {SET_TOKEN, 1, "token", "Initial Secret [default=random, use --token="" to allow any token]"},
183 {SET_RANDOM_TOKEN, 0, "random-token","Enforce a random token"},
185 {GET_VERSION, 0, "version", "Display version and copyright"},
186 {GET_HELP, 0, "help", "Display this help"},
188 #if defined(WITH_DBUS_TRANSPARENCY)
189 {ADD_DBUS_CLIENT, 1, "dbus-client", "Bind to an afb service through dbus"},
190 {ADD_DBUS_SERVICE, 1, "dbus-server", "Provide an afb service through dbus"},
192 {ADD_WS_CLIENT, 1, "ws-client", "Bind to an afb service through websocket"},
193 {ADD_WS_SERVICE, 1, "ws-server", "Provide an afb service through websockets"},
195 {ADD_AUTO_API, 1, "auto-api", "Automatic load of api of the given directory"},
197 {SET_SESSIONMAX, 1, "session-max", "Max count of session simultaneously [default " d2s(DEFAULT_MAX_SESSION_COUNT) "]"},
199 {SET_TRACEREQ, 1, "tracereq", "Log the requests: none, common, extra, all"},
200 {SET_TRACEEVT, 1, "traceevt", "Log the events: none, common, extra, all"},
201 {SET_TRACESES, 1, "traceses", "Log the sessions: none, all"},
202 {SET_TRACEAPI, 1, "traceapi", "Log the apis: none, common, api, event, all"},
203 {SET_TRACEGLOB, 1, "traceglob", "Log the globals: none, all"},
204 #if !defined(REMOVE_LEGACY_TRACE)
205 {SET_TRACEDITF, 1, "traceditf", "Log the daemons: no, common, all"},
206 {SET_TRACESVC, 1, "tracesvc", "Log the services: no, all"},
209 {ADD_CALL, 1, "call", "Call at start, format of val: API/VERB:json-args"},
211 {SET_NO_HTTPD, 0, "no-httpd", "Forbid HTTP service"},
212 {SET_EXEC, 0, "exec", "Execute the remaining arguments"},
214 #if defined(WITH_MONITORING_OPTION)
215 {SET_MONITORING, 0, "monitoring", "Enable HTTP monitoring at <ROOT>/monitoring/"},
218 {SET_CONFIG, 1, "config", "Load options from the given config file"},
219 {DUMP_CONFIG, 0, "dump-config", "Dump the config to stdout and exit"},
221 {ADD_SET, 1, "set", "Set parameters ([API]/[KEY]:JSON or {\"API\":{\"KEY\":JSON}}" },
222 {SET_OUTPUT, 1, "output", "Redirect stdout and stderr to output file (when --daemon)"},
224 {SET_TRAP_FAULTS, 1, "trap-faults", "Trap faults: on, off, yes, no, true, false, 1, 0 (default: true)"},
230 #if defined(WITH_MONITORING_OPTION)
231 static const char MONITORING_ALIAS[] = "/monitoring:"BINDING_INSTALL_DIR"/monitoring";
234 static const struct {
237 } default_optint_values[] = {
238 { SET_PORT, DEFAULT_HTTP_PORT },
239 { SET_API_TIMEOUT, DEFAULT_API_TIMEOUT },
240 { SET_CACHE_TIMEOUT, DEFAULT_CACHE_TIMEOUT },
241 { SET_SESSION_TIMEOUT, DEFAULT_SESSION_TIMEOUT },
242 { SET_SESSIONMAX, DEFAULT_MAX_SESSION_COUNT }
245 static const struct {
248 } default_optstr_values[] = {
249 { SET_WORK_DIR, "." },
250 { SET_ROOT_DIR, "." },
251 { SET_UPLOAD_DIR, "." },
252 { SET_ROOT_BASE, "/opa" },
253 { SET_ROOT_API, "/api" }
256 /**********************************
258 ***********************************/
260 static char *shortopts = NULL;
261 static int *id2idx = NULL;
263 static void *oomchk(void *ptr)
266 ERROR("Out of memory");
272 static char is_short_option(int val)
274 return (val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z') || (val >= '0' && val <= '9');
277 static void init_options()
284 for (i = 0 ; optdefs[i].name ; i++) {
288 if (is_short_option(id))
289 ns += 1 + !!optdefs[i].has_arg;
291 shortopts = oomchk(malloc(2 + ns));
292 id2idx = oomchk(calloc(1 + mi, sizeof *id2idx));
293 shortopts[ns = 0] = ':';
294 for (i = 0 ; optdefs[i].name ; i++) {
297 if (is_short_option(id)) {
298 shortopts[++ns] = (char)id;
299 if (optdefs[i].has_arg)
300 shortopts[++ns] = ':';
307 static const char *name_of_optid(int optid)
309 return optdefs[id2idx[optid]].name;
312 static int get_enum_val(const char *name, int optid, int (*func)(const char*))
318 ERROR("option [--%s] bad value (found %s)",
319 name_of_optid(optid), name);
326 /*----------------------------------------------------------
328 | print version and copyright
329 +--------------------------------------------------------- */
330 static void printVersion(FILE * file)
334 " AGL Framework Binder [AFB %s] "
335 #if defined(WITH_DBUS_TRANSPARENCY)
341 #if defined(WITH_MONITORING_OPTION)
347 #if defined(WITH_SUPERVISION)
352 "SUPERVISION [BINDINGS "
353 #if defined(WITH_LEGACY_BINDING_V1)
359 #if defined(WITH_LEGACY_BINDING_VDYN)
369 " Copyright (C) 2015-2018 \"IoT.bzh\"\n"
370 " AFB comes with ABSOLUTELY NO WARRANTY.\n"
371 " Licence Apache 2\n"
375 /*----------------------------------------------------------
377 | print information from long option array
378 +--------------------------------------------------------- */
380 static void printHelp(FILE * file, const char *name)
383 char command[50], sht[4];
385 fprintf(file, "%s:\nallowed options\n", strrchr(name, '/') ? strrchr(name, '/') + 1 : name);
387 for (ind = 0; optdefs[ind].name != NULL; ind++) {
388 if (is_short_option(optdefs[ind].id)) {
390 sht[1] = (char)optdefs[ind].id;
393 sht[0] = sht[1] = sht[2] = ' ';
395 strcpy(command, optdefs[ind].name);
396 if (optdefs[ind].has_arg)
397 strcat(command, "=xxxx");
398 fprintf(file, " %s --%-17s %s\n", sht, command, optdefs[ind].help);
401 "Example:\n %s --verbose --port="
402 d2s(DEFAULT_HTTP_PORT)
403 " --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
407 static void dump(struct json_object *config, FILE *file, const char *prefix, const char *title)
409 const char *head, *tail;
412 fprintf(file, "%s----BEGIN OF %s-----\n", prefix ?: "", title);
414 head = json_object_to_json_string_ext(config, JSON_C_TO_STRING_PRETTY
415 |JSON_C_TO_STRING_SPACED|JSON_C_TO_STRING_NOSLASHESCAPE);
418 fprintf(file, "%s\n", head);
421 for (tail = head ; *tail && *tail != '\n' ; tail++);
422 fprintf(file, "%s %.*s\n", prefix, (int)(tail - head), head);
423 head = tail + !!*tail;
428 fprintf(file, "%s----END OF %s-----\n", prefix ?: "", title);
431 /**********************************
433 ***********************************/
435 static struct json_object *joomchk(struct json_object *value)
437 return oomchk(value);
440 static struct json_object *to_jstr(const char *value)
442 return joomchk(json_object_new_string(value));
445 static struct json_object *to_jint(int value)
447 return joomchk(json_object_new_int(value));
450 static struct json_object *to_jbool(int value)
452 return joomchk(json_object_new_boolean(value));
455 /**********************************
457 ***********************************/
459 static int string_to_bool(const char *value)
461 static const char true_names[] = "1\0yes\0true\0on";
462 static const char false_names[] = "0\0no\0false\0off";
466 while (pos < sizeof true_names)
467 if (strcasecmp(value, &true_names[pos]))
468 pos += 1 + strlen(&true_names[pos]);
473 while (pos < sizeof false_names)
474 if (strcasecmp(value, &false_names[pos]))
475 pos += 1 + strlen(&false_names[pos]);
482 static void noarg(int optid)
485 ERROR("option [--%s] need no value (found %s)", name_of_optid(optid), optarg);
490 static const char *get_arg(int optid)
493 ERROR("option [--%s] needs a value i.e. --%s=xxx",
494 name_of_optid(optid), name_of_optid(optid));
500 static int get_arg_bool(int optid)
502 int value = string_to_bool(get_arg(optid));
504 ERROR("option [--%s] needs a boolean value: yes/no, true/false, on/off, 1/0",
505 name_of_optid(optid));
511 static void config_del(struct json_object *config, int optid)
513 return json_object_object_del(config, name_of_optid(optid));
516 static int config_has(struct json_object *config, int optid)
518 return json_object_object_get_ex(config, name_of_optid(optid), NULL);
521 static int config_has_bool(struct json_object *config, int optid)
523 struct json_object *x;
524 return json_object_object_get_ex(config, name_of_optid(optid), &x)
525 && json_object_get_boolean(x);
528 static int config_has_str(struct json_object *config, int optid, const char *val)
531 struct json_object *a;
533 if (!json_object_object_get_ex(config, name_of_optid(optid), &a))
536 if (!json_object_is_type(a, json_type_array))
537 return !strcmp(val, json_object_get_string(a));
539 n = (int)json_object_array_length(a);
540 for (i = 0 ; i < n ; i++) {
541 if (!strcmp(val, json_object_get_string(json_object_array_get_idx(a, i))))
547 static void config_set(struct json_object *config, int optid, struct json_object *val)
549 json_object_object_add(config, name_of_optid(optid), val);
552 static void config_set_str(struct json_object *config, int optid, const char *val)
554 config_set(config, optid, to_jstr(val));
557 static void config_set_optstr(struct json_object *config, int optid)
559 config_set_str(config, optid, get_arg(optid));
562 static void config_set_int(struct json_object *config, int optid, int value)
564 config_set(config, optid, to_jint(value));
567 static void config_set_bool(struct json_object *config, int optid, int value)
569 config_set(config, optid, to_jbool(value));
572 static void config_set_optint_base(struct json_object *config, int optid, int mini, int maxi, int base)
574 const char *beg, *end;
577 beg = get_arg(optid);
578 val = strtol(beg, (char**)&end, base);
579 if (*end || end == beg) {
580 ERROR("option [--%s] requires a valid integer (found %s)",
581 name_of_optid(optid), beg);
584 if (val < (long int)mini || val > (long int)maxi) {
585 ERROR("option [--%s] value %ld out of bounds (not in [%d , %d])",
586 name_of_optid(optid), val, mini, maxi);
589 config_set_int(config, optid, (int)val);
592 static void config_set_optint(struct json_object *config, int optid, int mini, int maxi)
594 return config_set_optint_base(config, optid, mini, maxi, 10);
597 static void config_set_optenum(struct json_object *config, int optid, int (*func)(const char*))
599 const char *name = get_arg(optid);
600 get_enum_val(name, optid, func);
601 config_set_str(config, optid, name);
604 static void config_add(struct json_object *config, int optid, struct json_object *val)
606 struct json_object *a;
607 if (!json_object_object_get_ex(config, name_of_optid(optid), &a)) {
608 a = joomchk(json_object_new_array());
609 json_object_object_add(config, name_of_optid(optid), a);
611 json_object_array_add(a, val);
614 static void config_add_str(struct json_object *config, int optid, const char *val)
616 config_add(config, optid, to_jstr(val));
619 static void config_add_optstr(struct json_object *config, int optid)
621 config_add_str(config, optid, get_arg(optid));
624 static void config_mix2_cb(void *closure, struct json_object *obj, const char *name)
626 struct json_object *dest, *base = closure;
631 if (!json_object_object_get_ex(base, name, &dest)) {
632 dest = joomchk(json_object_new_object());
633 json_object_object_add(base, name, dest);
635 if (json_object_is_type(obj, json_type_object))
636 wrap_json_object_add(dest, obj);
638 json_object_object_add(dest, "", json_object_get(obj));
641 static void config_mix2(struct json_object *config, int optid, struct json_object *val)
643 struct json_object *obj;
645 if (!json_object_object_get_ex(config, name_of_optid(optid), &obj)) {
646 obj = joomchk(json_object_new_object());
647 json_object_object_add(config, name_of_optid(optid), obj);
649 wrap_json_for_all(val, config_mix2_cb, obj);
652 static void config_mix2_str(struct json_object *config, int optid, const char *val)
655 const char *api, *key;
656 struct json_object *obj, *sub;
657 enum json_tokener_error jerr;
659 st1 = strcspn(val, "/:{[\"");
660 st2 = strcspn(&val[st1], ":{[\"");
661 if (val[st1] != '/' || val[st1 + st2] != ':') {
662 obj = json_tokener_parse_verbose(val, &jerr);
663 if (jerr != json_tokener_success)
664 obj = json_object_new_string(val);
666 api = st1 == 0 ? "*" : strndupa(val, st1);
668 key = st2 <= 1 || (st2 == 2 && *val == '*') ? NULL : strndupa(val, st2 - 1);
670 sub = json_tokener_parse_verbose(val, &jerr);
671 if (jerr != json_tokener_success)
672 sub = json_object_new_string(val);
675 obj = json_object_new_object();
676 json_object_object_add(obj, key, sub);
679 obj = json_object_new_object();
680 json_object_object_add(obj, api, sub);
682 config_mix2(config, optid, obj);
683 json_object_put(obj);
686 static void config_mix2_optstr(struct json_object *config, int optid)
688 config_mix2_str(config, optid, get_arg(optid));
691 /*---------------------------------------------------------
693 +--------------------------------------------------------- */
695 static void set_log(const char *args)
697 char o = 0, s, *p, *i = strdupa(args);
700 for(;;) switch (*i) {
713 while (isalpha(*p)) p++;
716 lvl = verbose_level_of_name(i);
720 ERROR("Bad log name '%s' in %s", i, args);
738 /*---------------------------------------------------------
739 | Parse option and launch action
740 +--------------------------------------------------------- */
742 static void parse_arguments_inner(int argc, char **argv, struct json_object *config, struct option *options)
744 struct json_object *conf;
745 int optid, cind, dodump = 0;
749 optid = getopt_long(argc, argv, shortopts, options, NULL);
768 set_log(get_arg(optid));
772 config_set_optint(config, optid, 1024, 32767);
775 case SET_API_TIMEOUT:
776 case SET_SESSION_TIMEOUT:
777 case SET_CACHE_TIMEOUT:
778 config_set_optint(config, optid, 0, INT_MAX);
782 config_set_optint(config, optid, 1, INT_MAX);
793 config_set_optstr(config, optid);
796 #if defined(WITH_DBUS_TRANSPARENCY)
797 case ADD_DBUS_CLIENT:
798 case ADD_DBUS_SERVICE:
802 case ADD_WEAK_LDPATH:
808 config_add_optstr(config, optid);
812 config_mix2_optstr(config, optid);
815 #if defined(WITH_MONITORING_OPTION)
818 case SET_RANDOM_TOKEN:
822 config_set_bool(config, optid, 1);
830 config_set_bool(config, SET_DAEMON, optid != SET_FOREGROUND);
833 case SET_TRAP_FAULTS:
834 config_set_bool(config, optid, get_arg_bool(optid));
839 config_set_optenum(config, optid, afb_hook_flags_xreq_from_text);
843 config_set_optenum(config, optid, afb_hook_flags_evt_from_text);
847 config_set_optenum(config, optid, afb_hook_flags_session_from_text);
851 config_set_optenum(config, optid, afb_hook_flags_api_from_text);
855 config_set_optenum(config, optid, afb_hook_flags_global_from_text);
858 #if !defined(REMOVE_LEGACY_TRACE)
860 config_set_optenum(config, optid, afb_hook_flags_legacy_ditf_from_text);
864 config_set_optenum(config, optid, afb_hook_flags_legacy_svc_from_text);
869 if (optind == argc) {
870 ERROR("The option --exec requires arguments");
873 while (optind != argc)
874 config_add_str(config, optid, argv[optind++]);
878 conf = json_object_from_file(get_arg(optid));
880 ERROR("Can't read config file %s", get_arg(optid));
883 wrap_json_object_add(config, conf);
884 json_object_put(conf);
894 printVersion(stdout);
898 printHelp(stdout, argv[0]);
902 ERROR("Bad option detected, check %s", argv[cind]);
906 /* TODO: check for extra value */
909 dump(config, stdout, NULL, NULL);
914 static void parse_arguments(int argc, char **argv, struct json_object *config)
917 struct option *options;
919 /* create GNU getopt options from optdefs */
920 options = malloc((sizeof optdefs / sizeof * optdefs) * sizeof * options);
921 for (ind = 0; optdefs[ind].name; ind++) {
922 options[ind].name = optdefs[ind].name;
923 options[ind].has_arg = optdefs[ind].has_arg;
924 options[ind].flag = NULL;
925 options[ind].val = optdefs[ind].id;
927 memset(&options[ind], 0, sizeof options[ind]);
929 /* parse the arguments */
930 parse_arguments_inner(argc, argv, config, options);
932 /* release the memory of options */
936 static void fulfill_config(struct json_object *config)
940 for (i = 0 ; i < sizeof default_optint_values / sizeof * default_optint_values ; i++)
941 if (!config_has(config, default_optint_values[i].optid))
942 config_set_int(config, default_optint_values[i].optid, default_optint_values[i].valdef);
944 for (i = 0 ; i < sizeof default_optstr_values / sizeof * default_optstr_values ; i++)
945 if (!config_has(config, default_optstr_values[i].optid))
946 config_set_str(config, default_optstr_values[i].optid, default_optstr_values[i].valdef);
948 // default AUTH_TOKEN
949 if (config_has_bool(config, SET_RANDOM_TOKEN))
950 config_del(config, SET_TOKEN);
952 if (!config_has(config, ADD_LDPATH) && !config_has(config, ADD_WEAK_LDPATH) && !config_has_bool(config, SET_NO_LDPATH))
953 config_add_str(config, ADD_LDPATH, BINDING_INSTALL_DIR);
955 #if defined(WITH_MONITORING_OPTION)
956 if (config_has_bool(config, SET_MONITORING) && !config_has_str(config, ADD_ALIAS, MONITORING_ALIAS))
957 config_add_str(config, ADD_ALIAS, MONITORING_ALIAS);
960 #if !defined(REMOVE_LEGACY_TRACE) && 0
961 config->traceapi |= config->traceditf | config->tracesvc;
965 void afb_config_dump(struct json_object *config)
967 dump(config, stderr, "--", "CONFIG");
970 static void on_environment(struct json_object *config, int optid, const char *name, void (*func)(struct json_object*, int, const char*))
972 char *value = getenv(name);
975 func(config, optid, value);
978 static void on_environment_enum(struct json_object *config, int optid, const char *name, int (*func)(const char*))
980 char *value = getenv(name);
983 if (func(value) == -1)
984 WARNING("Unknown value %s for environment variable %s, ignored", value, name);
986 config_set_str(config, optid, value);
990 static void on_environment_bool(struct json_object *config, int optid, const char *name)
992 char *value = getenv(name);
996 asbool = string_to_bool(value);
998 WARNING("Unknown value %s for environment variable %s, ignored", value, name);
1000 config_set_bool(config, optid, asbool);
1004 static void parse_environment(struct json_object *config)
1006 on_environment_enum(config, SET_TRACEREQ, "AFB_TRACEREQ", afb_hook_flags_xreq_from_text);
1007 on_environment_enum(config, SET_TRACEEVT, "AFB_TRACEEVT", afb_hook_flags_evt_from_text);
1008 on_environment_enum(config, SET_TRACESES, "AFB_TRACESES", afb_hook_flags_session_from_text);
1009 on_environment_enum(config, SET_TRACEAPI, "AFB_TRACEAPI", afb_hook_flags_api_from_text);
1010 on_environment_enum(config, SET_TRACEGLOB, "AFB_TRACEGLOB", afb_hook_flags_global_from_text);
1011 on_environment(config, ADD_LDPATH, "AFB_LDPATHS", config_add_str);
1012 on_environment(config, ADD_SET, "AFB_SET", config_mix2_str);
1013 #if !defined(REMOVE_LEGACY_TRACE)
1014 on_environment_enum(config, SET_TRACEDITF, "AFB_TRACEDITF", afb_hook_flags_legacy_ditf_from_text);
1015 on_environment_enum(config, SET_TRACESVC, "AFB_TRACESVC", afb_hook_flags_legacy_svc_from_text);
1017 on_environment_bool(config, SET_TRAP_FAULTS, "AFB_TRAP_FAULTS");
1020 struct json_object *afb_config_parse_arguments(int argc, char **argv)
1022 struct json_object *result;
1026 result = json_object_new_object();
1028 parse_environment(result);
1029 parse_arguments(argc, argv, result);
1030 fulfill_config(result);
1031 if (verbose_wants(Log_Level_Info))
1032 afb_config_dump(result);
1036 struct json_object *afb_config_json(struct json_object *config)