libamxrt  0.4.2
Ambiorix Run Time Library
amxrt_config.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #ifndef _GNU_SOURCE
56 #define _GNU_SOURCE
57 #endif
58 
59 #include <stdlib.h>
60 #include <string.h>
61 
62 #include <amxrt/amxrt.h>
63 #include <amxp/amxp_dir.h>
64 
65 #include "amxrt_priv.h"
66 
67 static void amxrt_config_read_env_vars(void) {
68  amxrt_config_read_env_var("AMXB_BACKENDS",
70  AMXC_VAR_ID_LIST);
71 
72  amxrt_config_read_env_var("AMXRT_PREFIX_PATH",
74  AMXC_VAR_ID_CSTRING);
75 
76  amxrt_config_read_env_var("AMXRT_PLUGIN_DIR",
78  AMXC_VAR_ID_CSTRING);
79 
80  amxrt_config_read_env_var("AMXRT_CFG_DIR",
82  AMXC_VAR_ID_CSTRING);
83 }
84 
85 static void amxrt_config_add_dir(amxc_var_t* var_dirs, const char* dir) {
86  bool found = false;
87  const amxc_llist_t* dirs = amxc_var_constcast(amxc_llist_t, var_dirs);
88 
89  amxc_llist_for_each(it, dirs) {
90  amxc_var_t* var_dir = amxc_var_from_llist_it(it);
91  const char* stored_dir = amxc_var_constcast(cstring_t, var_dir);
92  if((stored_dir != NULL) && (strcmp(dir, stored_dir) == 0)) {
93  found = true;
94  break;
95  }
96  }
97 
98  if(!found) {
99  amxc_var_add(cstring_t, var_dirs, dir);
100  }
101 }
102 
103 static void amxrt_config_set_default_dirs(amxo_parser_t* parser) {
104  amxc_var_t* inc_dirs = amxo_parser_claim_config(parser, AMXRT_COPT_INCDIRS);
105  amxc_var_t* lib_dirs = amxo_parser_claim_config(parser, AMXRT_COPT_LIBDIRS);
106  amxc_var_t* mib_dirs = amxo_parser_claim_config(parser, AMXRT_COPT_MIBDIRS);
107 
108  amxrt_config_add_dir(inc_dirs, ".");
109  amxrt_config_add_dir(inc_dirs, "${prefix}${cfg-dir}/${name}");
110  amxrt_config_add_dir(inc_dirs, "${prefix}${cfg-dir}/modules");
111 
112  amxrt_config_add_dir(lib_dirs, "${prefix}${plugin-dir}/${name}");
113  amxrt_config_add_dir(lib_dirs, "${prefix}${plugin-dir}/modules");
114  amxrt_config_add_dir(lib_dirs, "${prefix}/usr/local/lib/amx/${name}");
115  amxrt_config_add_dir(lib_dirs, "${prefix}/usr/local/lib/amx/modules");
116 
117  amxrt_config_add_dir(mib_dirs, "${prefix}${cfg-dir}/${name}/mibs");
118 
119 
120 }
121 
122 static int amxrt_config_add_backend(const char* name, void* priv) {
123  amxc_var_t* backends = (amxc_var_t*) priv;
124  amxc_var_add(cstring_t, backends, name);
125 
126  return 0;
127 }
128 
129 // Config options are scoped within the odl file, so when an include is done,
130 // it is possible the config options are back to the original value
131 static void amxrt_config_include_end(amxo_parser_t* parser,
132  UNUSED const char* file) {
133  amxrt_t* rt = amxrt_get();
134  amxd_dm_t* dm = &rt->dm;
135  amxp_sigmngr_enable(&dm->sigmngr, GET_BOOL(&parser->config, AMXRT_COPT_EVENT));
136 }
137 
138 // When a config section is done, check if any settings must be applied
139 static void amxrt_config_section_end(amxo_parser_t* parser,
140  int section_id) {
141  amxrt_t* rt = amxrt_get();
142  amxd_dm_t* dm = &rt->dm;
143  if(section_id == 0) {
144  amxp_sigmngr_enable(&dm->sigmngr, GET_BOOL(&parser->config, AMXRT_COPT_EVENT));
145  }
146 }
147 
148 static void amxrt_config_option_changed(amxo_parser_t* parser,
149  UNUSED const char* option,
150  UNUSED amxc_var_t* value) {
151  amxrt_t* rt = amxrt_get();
152  amxc_var_for_each(cmd, &rt->forced_options) {
153  const char* path = amxc_var_key(cmd);
154  amxc_var_set_path(&parser->config, path, cmd, AMXC_VAR_FLAG_UPDATE | AMXC_VAR_FLAG_COPY);
155  }
156 }
157 
158 static amxo_hooks_t amxrt_config_hooks = {
159  .it = { .next = NULL, .prev = NULL, .llist = NULL },
160  .comment = NULL,
161  .start = NULL,
162  .end = NULL,
163  .start_include = NULL,
164  .end_include = amxrt_config_include_end,
165  .set_config = amxrt_config_option_changed,
166  .start_section = NULL,
167  .end_section = amxrt_config_section_end,
168  .create_object = NULL,
169  .add_instance = NULL,
170  .select_object = NULL,
171  .end_object = NULL,
172  .add_param = NULL,
173  .set_param = NULL,
174  .end_param = NULL,
175  .add_func = NULL,
176  .add_func_arg = NULL,
177  .end_func = NULL,
178  .add_mib = NULL,
179  .set_counter = NULL,
180 };
181 
182 // PRIVATE FUNCTIONS
183 void amxrt_config_add_options(amxo_parser_t* parser) {
184  amxc_var_t* config = &parser->config;
185 
186  amxc_var_add_key(amxc_llist_t, config, AMXRT_COPT_BACKENDS, NULL);
187  amxc_var_add_key(amxc_llist_t, config, AMXRT_COPT_URIS, NULL);
188  amxc_var_add_key(amxc_llist_t, config, AMXRT_COPT_DATA_URIS, NULL);
189  amxc_var_add_key(bool, config, AMXRT_COPT_AUTO_DETECT, true);
190  amxc_var_add_key(bool, config, AMXRT_COPT_AUTO_CONNECT, true);
191  amxc_var_add_key(bool, config, AMXRT_COPT_DAEMON, false);
192  amxc_var_add_key(uint32_t, config, AMXRT_COPT_PRIORITY, 0);
193  amxc_var_add_key(bool, config, AMXRT_COPT_PID_FILE, true);
194  amxc_var_add_key(cstring_t, config, AMXRT_COPT_PREFIX_PATH, "");
195  amxc_var_add_key(cstring_t, config, AMXRT_COPT_PLUGIN_DIR, AMXRT_CVAL_PLUGIN_DIR);
196  amxc_var_add_key(cstring_t, config, AMXRT_COPT_CFG_DIR, AMXRT_CVAL_CFG_DIR);
197  amxc_var_add_key(amxc_llist_t, config, AMXRT_COPT_LIBDIRS, NULL);
198  amxc_var_add_key(amxc_llist_t, config, AMXRT_COPT_INCDIRS, NULL);
199  amxc_var_add_key(amxc_llist_t, config, AMXRT_COPT_MIBDIRS, NULL);
200  amxc_var_add_key(amxc_llist_t, config, AMXRT_COPT_LISTEN, NULL);
201  amxc_var_add_key(bool, config, AMXRT_COPT_EVENT, false);
202  amxc_var_add_key(bool, config, AMXRT_COPT_DUMP_CONFIG, false);
203  amxc_var_add_key(bool, config, AMXRT_COPT_DUMP_CAPS, false);
204  amxc_var_add_key(cstring_t, config, AMXRT_COPT_BACKENDS_DIR, AMXRT_CVAL_BACKEND_DIR);
205  amxc_var_add_key(cstring_t, config, AMXRT_COPT_STORAGE_TYPE, AMXRT_CVAL_STORAGE_TYPE);
206  amxc_var_add_key(bool, config, AMXRT_COPT_LOG, false);
207  amxc_var_add_key(cstring_t, config, AMXRT_COPT_RW_DATA_PATH, "${prefix}" RWDATAPATH);
208  amxc_var_add_key(cstring_t, config, AMXRT_COPT_STORAGE_DIR, "${rw_data_path}/${name}/");
209  amxc_var_add_key(amxc_llist_t, config, AMXRT_COPT_REQUIRES, NULL);
210  amxc_var_add_key(cstring_t, config, AMXRT_COPT_EXT_DIR, "extensions/");
211 
212  // set hooks to monitor config option changes
213  amxo_parser_set_hooks(parser, &amxrt_config_hooks);
214 }
215 
216 // PUBLIC FUNCTIONS
217 int amxrt_config_init(int argc, char* argv[], int* index, amxrt_arg_fn_t fn) {
218  int retval = 0;
219  char* base_name = NULL;
220  amxo_parser_t* parser = amxrt_get_parser();
221 
223 
225 
226  base_name = strdup(basename(argv[0]));
227  when_null_status(base_name, exit, retval = -1);
228  for(int i = strlen(base_name) - 1; i > 0; i--) {
229  if(base_name[i] == '.') {
230  base_name[i] = 0;
231  }
232  }
233  amxc_var_add_key(cstring_t, &parser->config, AMXRT_COPT_NAME, base_name);
234 
235  *index = amxrt_cmd_line_parse(argc, argv, fn);
236  if(*index < 0) {
237  retval = *index;
238  *index = argc;
239  }
240 
241 exit:
242  free(base_name);
243  return retval;
244 }
245 
247  amxo_parser_t* parser = amxrt_get_parser();
248 
249  if(amxc_var_constcast(bool, GET_ARG(&parser->config, AMXRT_COPT_AUTO_DETECT))) {
250  amxc_var_t* backends = GET_ARG(&parser->config, AMXRT_COPT_BACKENDS);
251  amxc_var_t* dirs = GET_ARG(&parser->config, AMXRT_COPT_BACKENDS_DIR);
252  if(amxc_var_type_of(dirs) == AMXC_VAR_ID_LIST) {
253  amxc_var_for_each(dir, dirs) {
254  const char* path = GET_CHAR(dir, NULL);
255  amxp_dir_scan(path, "d_name matches '.*\\.so'", false, amxrt_config_add_backend, backends);
256  }
257  } else {
258  const char* path = GET_CHAR(&parser->config, AMXRT_COPT_BACKENDS_DIR);
259  amxp_dir_scan(path, "d_name matches '.*\\.so'", false, amxrt_config_add_backend, backends);
260  }
261  }
262 }
263 
264 void amxrt_config_read_env_var(const char* var_name,
265  const char* config_name,
266  int32_t var_type) {
267  const char* env = getenv(var_name);
268  amxc_var_t* config = amxrt_get_config();
269  amxc_var_t* coption = GET_ARG(config, config_name);
270  amxc_var_t var_env;
271 
272  amxc_var_init(&var_env);
273 
274  if(env == NULL) {
275  goto exit;
276  }
277 
278  amxc_var_set(cstring_t, &var_env, env);
279 
280  if(coption == NULL) {
281  coption = amxc_var_add_new_key(config, config_name);
282  amxc_var_set_type(coption, var_type);
283  }
284 
285  switch(var_type) {
286  case AMXC_VAR_ID_LIST: {
287  amxc_string_t* str_env = amxc_var_take(amxc_string_t, &var_env);
288  amxc_llist_t parts;
289  amxc_llist_init(&parts);
290  amxc_string_split_to_llist(str_env, &parts, ';');
291  amxc_llist_for_each(it, (&parts)) {
292  amxc_string_t* part = amxc_string_from_llist_it(it);
293  amxc_var_add(cstring_t, coption, amxc_string_get(part, 0));
294  amxc_string_delete(&part);
295  }
296  amxc_llist_clean(&parts, amxc_string_list_it_free);
297  amxc_string_delete(&str_env);
298  }
299  break;
300  default: {
301  amxc_var_convert(coption, &var_env, var_type);
302  }
303  break;
304  }
305 
306 exit:
307  amxc_var_clean(&var_env);
308  return;
309 }
static amxrt_t rt
Definition: amxrt.c:74
#define AMXRT_COPT_BACKENDS
Definition: amxrt.h:75
#define AMXRT_COPT_LISTEN
Definition: amxrt.h:89
#define AMXRT_COPT_RW_DATA_PATH
Definition: amxrt.h:94
#define AMXRT_COPT_PLUGIN_DIR
Definition: amxrt.h:87
#define AMXRT_COPT_URIS
Definition: amxrt.h:73
#define AMXRT_COPT_PID_FILE
Definition: amxrt.h:84
#define AMXRT_COPT_EXT_DIR
Definition: amxrt.h:102
#define AMXRT_COPT_LIBDIRS
Definition: amxrt.h:79
int(* amxrt_arg_fn_t)(amxc_var_t *config, int arg_id, const char *value)
Definition: amxrt.h:115
#define AMXRT_COPT_CFG_DIR
Definition: amxrt.h:88
#define AMXRT_COPT_MIBDIRS
Definition: amxrt.h:80
#define AMXRT_COPT_DATA_URIS
Definition: amxrt.h:74
#define AMXRT_COPT_DUMP_CONFIG
Definition: amxrt.h:91
#define AMXRT_COPT_PRIORITY
Definition: amxrt.h:83
#define AMXRT_COPT_PREFIX_PATH
Definition: amxrt.h:86
#define AMXRT_COPT_STORAGE_DIR
Definition: amxrt.h:95
#define AMXRT_COPT_STORAGE_TYPE
Definition: amxrt.h:96
#define AMXRT_COPT_DUMP_CAPS
Definition: amxrt.h:92
#define AMXRT_COPT_NAME
Definition: amxrt.h:85
#define AMXRT_COPT_REQUIRES
Definition: amxrt.h:99
#define AMXRT_COPT_EVENT
Definition: amxrt.h:90
#define AMXRT_COPT_AUTO_DETECT
Definition: amxrt.h:76
#define AMXRT_COPT_LOG
Definition: amxrt.h:98
#define AMXRT_COPT_AUTO_CONNECT
Definition: amxrt.h:77
#define AMXRT_COPT_BACKENDS_DIR
Definition: amxrt.h:93
#define AMXRT_COPT_DAEMON
Definition: amxrt.h:82
#define AMXRT_COPT_INCDIRS
Definition: amxrt.h:78
static void amxrt_config_read_env_vars(void)
Definition: amxrt_config.c:67
static void amxrt_config_option_changed(amxo_parser_t *parser, UNUSED const char *option, UNUSED amxc_var_t *value)
Definition: amxrt_config.c:148
void amxrt_config_add_options(amxo_parser_t *parser)
Definition: amxrt_config.c:183
static void amxrt_config_set_default_dirs(amxo_parser_t *parser)
Definition: amxrt_config.c:103
static int amxrt_config_add_backend(const char *name, void *priv)
Definition: amxrt_config.c:122
static void amxrt_config_include_end(amxo_parser_t *parser, UNUSED const char *file)
Definition: amxrt_config.c:131
static void amxrt_config_add_dir(amxc_var_t *var_dirs, const char *dir)
Definition: amxrt_config.c:85
static amxo_hooks_t amxrt_config_hooks
Definition: amxrt_config.c:158
static void amxrt_config_section_end(amxo_parser_t *parser, int section_id)
Definition: amxrt_config.c:139
#define AMXRT_CVAL_CFG_DIR
Definition: amxrt_priv.h:65
#define AMXRT_CVAL_BACKEND_DIR
Definition: amxrt_priv.h:66
PRIVATE amxrt_t * amxrt_get(void)
Definition: amxrt.c:297
#define AMXRT_CVAL_STORAGE_TYPE
Definition: amxrt_priv.h:67
#define AMXRT_CVAL_PLUGIN_DIR
Definition: amxrt_priv.h:64
int amxrt_cmd_line_parse(int argc, char *argv[], amxrt_arg_fn_t fn)
Starts parsing the command line options.
Definition: amxrt_args.c:236
void amxrt_config_scan_backend_dirs(void)
Scan backend directories for available backends.
Definition: amxrt_config.c:246
amxc_var_t * amxrt_get_config(void)
Gets the htable variant containing the configuration options.
Definition: amxrt.c:301
void amxrt_config_read_env_var(const char *var_name, const char *config_name, int32_t var_type)
Helper function to read an environment variable and add it's value to the runtime configuration.
Definition: amxrt_config.c:264
amxo_parser_t * amxrt_get_parser(void)
Gets runtime odl parser.
Definition: amxrt.c:305
int amxrt_config_init(int argc, char *argv[], int *index, amxrt_arg_fn_t fn)
Initializes the default runtime configuration.
Definition: amxrt_config.c:217
amxd_dm_t dm
Definition: amxrt_priv.h:100
amxc_var_t forced_options
Definition: amxrt_priv.h:103
config
Definition: test.odl:54