aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/odp_libconfig.c
blob: 93095ed1a81cd1f86b25a8fdf8411ff3be482585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Copyright (c) 2018, Linaro Limited
 * Copyright (c) 2020, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libconfig.h>

#include <odp/api/version.h>
#include <odp_global_data.h>
#include <odp_debug_internal.h>
#include <odp_libconfig_internal.h>
#include <odp_libconfig_config.h>

int _odp_libconfig_init_global(void)
{
	const char *filename;
	const char *vers;
	const char *vers_rt;
	const char *impl;
	const char *impl_rt;
	config_t *config = &odp_global_ro.libconfig_default;
	config_t *config_rt = &odp_global_ro.libconfig_runtime;
	const char *impl_field = "odp_implementation";
	const char *vers_field = "config_file_version";

	config_init(config);
	config_init(config_rt);
	odp_global_ro.has_config_rt = 0;

	if (!config_read_string(config, config_builtin)) {
		ODP_ERR("Failed to read default config: %s(%d): %s\n",
			config_error_file(config), config_error_line(config),
			config_error_text(config));
		goto fail;
	}

	filename = getenv("ODP_CONFIG_FILE");
	if (filename == NULL)
		return 0;

	ODP_PRINT("ODP CONFIG FILE: %s\n", filename);

	if (!config_read_file(config_rt, filename)) {
		ODP_PRINT("  ERROR: failed to read config file: %s(%d): %s\n\n",
			  config_error_file(config_rt),
			  config_error_line(config_rt),
			  config_error_text(config_rt));
		goto fail;
	}

	/* Check runtime configuration's implementation name and version */
	if (!config_lookup_string(config, impl_field, &impl) ||
	    !config_lookup_string(config_rt, impl_field, &impl_rt)) {
		ODP_PRINT("  ERROR: missing mandatory field: %s\n\n",
			  impl_field);
		goto fail;
	}
	if (!config_lookup_string(config, vers_field, &vers) ||
	    !config_lookup_string(config_rt, vers_field, &vers_rt)) {
		ODP_PRINT("  ERROR: missing mandatory field: %s\n\n",
			  vers_field);
		goto fail;
	}
	if (strcmp(impl, impl_rt)) {
		ODP_PRINT("  ERROR: ODP implementation name mismatch:\n"
			  "    Expected: \"%s\"\n"
			  "    Found:    \"%s\"\n\n", impl, impl_rt);
		goto fail;
	}
	if (strcmp(vers, vers_rt)) {
		ODP_PRINT("  ERROR: config file version number mismatch:\n"
			  "    Expected: \"%s\"\n"
			  "    Found:    \"%s\"\n\n", vers, vers_rt);
		goto fail;
	}

	odp_global_ro.has_config_rt = 1;
	return 0;
fail:
	ODP_ERR("Config file failure\n");
	config_destroy(config);
	config_destroy(config_rt);
	return -1;
}

int _odp_libconfig_term_global(void)
{
	config_destroy(&odp_global_ro.libconfig_default);
	config_destroy(&odp_global_ro.libconfig_runtime);

	return 0;
}

int _odp_libconfig_lookup_int(const char *path, int *value)
{
	int ret_def = CONFIG_FALSE;
	int ret_rt = CONFIG_FALSE;

	ret_def = config_lookup_int(&odp_global_ro.libconfig_default, path,
				    value);

	/* Runtime option overrides default value */
	ret_rt = config_lookup_int(&odp_global_ro.libconfig_runtime, path,
				   value);

	return  (ret_def == CONFIG_TRUE || ret_rt == CONFIG_TRUE) ? 1 : 0;
}

int _odp_libconfig_lookup_array(const char *path, int value[], int max_num)
{
	const config_t *config;
	config_setting_t *setting;
	int num, i, j;
	int num_out = 0;

	for (j = 0; j < 2; j++) {
		if (j == 0)
			config = &odp_global_ro.libconfig_default;
		else
			config = &odp_global_ro.libconfig_runtime;

		setting = config_lookup(config, path);

		/* Runtime config may not define the array, whereas
		 * the default config has it always defined. When the array
		 * is defined, it must be correctly formatted. */
		if (setting == NULL)
			continue;

		if (config_setting_is_array(setting) == CONFIG_FALSE)
			return 0;

		num = config_setting_length(setting);

		if (num <= 0 || num > max_num)
			return 0;

		for (i = 0; i < num; i++)
			value[i] = config_setting_get_int_elem(setting, i);

		num_out = num;
	}

	/* Number of elements copied */
	return num_out;
}

static int lookup_int(config_t *cfg,
		      const char *base_path,
		      const char *local_path,
		      const char *name,
		      int *value)
{
	char path[256];

	if (local_path) {
		snprintf(path, sizeof(path), "%s.%s.%s", base_path,
			 local_path, name);
		if (config_lookup_int(cfg, path, value) == CONFIG_TRUE)
			return 1;
	}

	snprintf(path, sizeof(path), "%s.%s", base_path, name);
	if (config_lookup_int(cfg, path, value) == CONFIG_TRUE)
		return 1;

	return 0;
}

int _odp_libconfig_lookup_ext_int(const char *base_path,
				  const char *local_path,
				  const char *name,
				  int *value)
{
	if (lookup_int(&odp_global_ro.libconfig_runtime,
		       base_path, local_path, name, value))
		return 1;

	if (lookup_int(&odp_global_ro.libconfig_default,
		       base_path, local_path, name, value))
		return 1;

	return 0;
}

int _odp_libconfig_print(void)
{
	int c;
	/* Temp file for config_write() output. Suppress Coverity warning about tmpfile() usage. */
	/* coverity[secure_temp] */
	FILE *file = tmpfile();

	if (file == NULL)
		return -1;

	if (fprintf(file,
		    "\nODP_CONFIG_FILE default values:\n"
		    "-------------------------------\n\n") < 0)
		goto fail;

	config_write(&odp_global_ro.libconfig_default, file);

	if (odp_global_ro.has_config_rt) {
		if (fprintf(file,
			    "\nODP_CONFIG_FILE override values:\n"
			    "--------------------------------\n\n") < 0)
			goto fail;

		config_write(&odp_global_ro.libconfig_runtime, file);
	}

	/* Print temp file to the log */
	rewind(file);
	while ((c = fgetc(file)) != EOF)
		ODP_PRINT("%c", (char)c);

	fclose(file);
	return 0;

fail:
	fclose(file);
	return -1;
}