SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[lttng-tools.git] / src / bin / lttng-sessiond / sessiond-config.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include "version.h"
9 #include "sessiond-config.h"
10 #include <assert.h>
11 #include "lttng-ust-ctl.h"
12 #include <common/defaults.h>
13 #include <limits.h>
14 #include <errno.h>
15 #include <ctype.h>
16 #include <common/error.h>
17 #include <common/utils.h>
18 #include <common/compat/getenv.h>
19
20 static
21 struct sessiond_config sessiond_config_build_defaults = {
22 .quiet = false,
23 .verbose = 0,
24 .verbose_consumer = 0,
25
26 .agent_tcp_port = { .begin = DEFAULT_AGENT_TCP_PORT_RANGE_BEGIN, .end = DEFAULT_AGENT_TCP_PORT_RANGE_END },
27 .trigger_error_counter_bucket = 4096,
28 .app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT,
29
30 .no_kernel = false,
31 .background = false,
32 .daemonize = false,
33 .sig_parent = false,
34
35 .tracing_group_name.value = (char *) DEFAULT_TRACING_GROUP,
36 .kmod_probes_list.value = NULL,
37 .kmod_extra_probes_list.value = NULL,
38
39 .rundir.value = NULL,
40
41 .apps_unix_sock_path.value = NULL,
42 .client_unix_sock_path.value = NULL,
43 .wait_shm_path.value = NULL,
44 .health_unix_sock_path.value = NULL,
45 .lttng_ust_clock_plugin.value = NULL,
46 .pid_file_path.value = NULL,
47 .lock_file_path.value = NULL,
48 .agent_port_file_path.value = NULL,
49 .load_session_path.value = NULL,
50
51 .consumerd32_path.value = NULL,
52 .consumerd32_bin_path.value = NULL,
53 .consumerd32_lib_dir.value = NULL,
54 .consumerd32_err_unix_sock_path.value = NULL,
55 .consumerd32_cmd_unix_sock_path.value = NULL,
56
57 .consumerd64_path.value = NULL,
58 .consumerd64_bin_path.value = NULL,
59 .consumerd64_lib_dir.value = NULL,
60 .consumerd64_err_unix_sock_path.value = NULL,
61 .consumerd64_cmd_unix_sock_path.value = NULL,
62
63 .kconsumerd_path.value = NULL,
64 .kconsumerd_err_unix_sock_path.value = NULL,
65 .kconsumerd_cmd_unix_sock_path.value = NULL,
66 };
67
68 static
69 void config_string_fini(struct config_string *str)
70 {
71 config_string_set(str, NULL);
72 }
73
74 static
75 void config_string_set_static(struct config_string *config_str,
76 const char *value)
77 {
78 config_string_set(config_str, (char *) value);
79 config_str->should_free = false;
80 }
81
82 /* Only use for dynamically-allocated strings. */
83 LTTNG_HIDDEN
84 void config_string_set(struct config_string *config_str, char *value)
85 {
86 assert(config_str);
87 if (config_str->should_free) {
88 free(config_str->value);
89 config_str->should_free = false;
90 }
91
92 config_str->should_free = !!value;
93 config_str->value = value;
94 }
95
96 LTTNG_HIDDEN
97 int sessiond_config_apply_env_config(struct sessiond_config *config)
98 {
99 int ret = 0;
100 const char *env_value;
101
102 env_value = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
103 if (env_value) {
104 char *endptr;
105 long int_val;
106
107 errno = 0;
108 int_val = strtoul(env_value, &endptr, 0);
109 if (errno != 0 || int_val > INT_MAX ||
110 (int_val < 0 && int_val != -1)) {
111 ERR("Invalid value \"%s\" used for \"%s\" environment variable",
112 env_value, DEFAULT_APP_SOCKET_TIMEOUT_ENV);
113 ret = -1;
114 goto end;
115 }
116
117 config->app_socket_timeout = int_val;
118 }
119
120 env_value = lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
121 if (env_value) {
122 config_string_set_static(&config->consumerd32_bin_path,
123 env_value);
124 }
125 env_value = lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
126 if (env_value) {
127 config_string_set_static(&config->consumerd64_bin_path,
128 env_value);
129 }
130
131 env_value = lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
132 if (env_value) {
133 config_string_set_static(&config->consumerd32_lib_dir,
134 env_value);
135 }
136 env_value = lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
137 if (env_value) {
138 config_string_set_static(&config->consumerd64_lib_dir,
139 env_value);
140 }
141
142 env_value = lttng_secure_getenv("LTTNG_UST_CLOCK_PLUGIN");
143 if (env_value) {
144 config_string_set_static(&config->lttng_ust_clock_plugin,
145 env_value);
146 }
147
148 env_value = lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES);
149 if (env_value) {
150 config_string_set_static(&config->kmod_probes_list,
151 env_value);
152 }
153
154 env_value = lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES);
155 if (env_value) {
156 config_string_set_static(&config->kmod_extra_probes_list,
157 env_value);
158 }
159 end:
160 return ret;
161 }
162
163 static
164 int config_set_paths_root(struct sessiond_config *config)
165 {
166 int ret = 0;
167
168 config_string_set(&config->rundir, strdup(DEFAULT_LTTNG_RUNDIR));
169 if (!config->rundir.value) {
170 ERR("Failed to set rundir");
171 ret = -1;
172 goto end;
173 }
174
175 config_string_set_static(&config->apps_unix_sock_path,
176 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
177 config_string_set_static(&config->client_unix_sock_path,
178 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
179 config_string_set_static(&config->wait_shm_path,
180 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
181 config_string_set_static(&config->health_unix_sock_path,
182 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
183 end:
184 return ret;
185 }
186
187 static
188 int config_set_paths_non_root(struct sessiond_config *config)
189 {
190 int ret = 0;
191 const char *home_path = utils_get_home_dir();
192 char *str;
193
194 if (home_path == NULL) {
195 ERR("Can't get HOME directory for sockets creation.");
196 ret = -1;
197 goto end;
198 }
199
200 /*
201 * Create rundir from home path. This will create something like
202 * $HOME/.lttng
203 */
204 ret = asprintf(&str, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
205 if (ret < 0) {
206 ERR("Failed to set rundir");
207 goto end;
208 }
209 config_string_set(&config->rundir, str);
210 str = NULL;
211
212 ret = asprintf(&str, DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
213 if (ret < 0) {
214 ERR("Failed to set default home apps unix socket path");
215 goto end;
216 }
217 config_string_set(&config->apps_unix_sock_path, str);
218 str = NULL;
219
220 ret = asprintf(&str, DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
221 if (ret < 0) {
222 ERR("Failed to set default home client unix socket path");
223 goto end;
224 }
225 config_string_set(&config->client_unix_sock_path, str);
226 str = NULL;
227
228 ret = asprintf(&str, DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
229 if (ret < 0) {
230 ERR("Failed to set default home apps wait shm path");
231 goto end;
232 }
233 config_string_set(&config->wait_shm_path, str);
234 str = NULL;
235
236 ret = asprintf(&str, DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
237 if (ret < 0) {
238 ERR("Failed to set default home health UNIX socket path");
239 goto end;
240 }
241 config_string_set(&config->health_unix_sock_path, str);
242 str = NULL;
243
244 ret = 0;
245 end:
246 return ret;
247 }
248
249 LTTNG_HIDDEN
250 int sessiond_config_init(struct sessiond_config *config)
251 {
252 int ret;
253 bool is_root = (getuid() == 0);
254 char *str;
255
256 assert(config);
257 memcpy(config, &sessiond_config_build_defaults, sizeof(*config));
258
259 if (is_root) {
260 ret = config_set_paths_root(config);
261 } else {
262 ret = config_set_paths_non_root(config);
263 }
264 if (ret < 0) {
265 goto error;
266 }
267
268 /* 32 bits consumerd path setup */
269 ret = asprintf(&str, DEFAULT_USTCONSUMERD32_PATH,
270 config->rundir.value);
271 if (ret < 0) {
272 ERR("Failed to set 32-bit consumer path");
273 goto error;
274 }
275 config_string_set(&config->consumerd32_path, str);
276 str = NULL;
277
278 ret = asprintf(&str, DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
279 config->rundir.value);
280 if (ret < 0) {
281 ERR("Failed to set 32-bit consumer error socket path");
282 goto error;
283 }
284 config_string_set(&config->consumerd32_err_unix_sock_path, str);
285 str = NULL;
286
287 ret = asprintf(&str, DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
288 config->rundir.value);
289 if (ret < 0) {
290 ERR("Failed to set 32-bit consumer command socket path");
291 goto error;
292 }
293 config_string_set(&config->consumerd32_cmd_unix_sock_path, str);
294 str = NULL;
295
296 /* 64 bits consumerd path setup */
297 ret = asprintf(&str, DEFAULT_USTCONSUMERD64_PATH,
298 config->rundir.value);
299 if (ret < 0) {
300 ERR("Failed to set 64-bit consumer path");
301 goto error;
302 }
303 config_string_set(&config->consumerd64_path, str);
304 str = NULL;
305
306 ret = asprintf(&str, DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
307 config->rundir.value);
308 if (ret < 0) {
309 ERR("Failed to set 64-bit consumer error socket path");
310 goto error;
311 }
312 config_string_set(&config->consumerd64_err_unix_sock_path, str);
313 str = NULL;
314
315 ret = asprintf(&str, DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
316 config->rundir.value);
317 if (ret < 0) {
318 ERR("Failed to set 64-bit consumer command socket path");
319 goto error;
320 }
321 config_string_set(&config->consumerd64_cmd_unix_sock_path, str);
322 str = NULL;
323
324 /* kconsumerd consumerd path setup */
325 ret = asprintf(&str, DEFAULT_KCONSUMERD_PATH,
326 config->rundir.value);
327 if (ret < 0) {
328 ERR("Failed to set kernel consumer path");
329 goto error;
330 }
331 config_string_set(&config->kconsumerd_path, str);
332 str = NULL;
333
334 ret = asprintf(&str, DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
335 config->rundir.value);
336 if (ret < 0) {
337 ERR("Failed to set kernel consumer error socket path");
338 goto error;
339 }
340 config_string_set(&config->kconsumerd_err_unix_sock_path, str);
341 str = NULL;
342
343 ret = asprintf(&str, DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
344 config->rundir.value);
345 if (ret < 0) {
346 ERR("Failed to set kernel consumer command socket path");
347 goto error;
348 }
349 config_string_set(&config->kconsumerd_cmd_unix_sock_path, str);
350 str = NULL;
351
352 ret = asprintf(&str, "%s/%s", config->rundir.value,
353 DEFAULT_LTTNG_SESSIOND_PIDFILE);
354 if (ret < 0) {
355 ERR("Failed to set PID file path");
356 goto error;
357 }
358 config_string_set(&config->pid_file_path, str);
359 str = NULL;
360
361 ret = asprintf(&str, "%s/%s", config->rundir.value,
362 DEFAULT_LTTNG_SESSIOND_LOCKFILE);
363 if (ret < 0) {
364 ERR("Failed to set lock file path");
365 goto error;
366 }
367 config_string_set(&config->lock_file_path, str);
368 str = NULL;
369
370 ret = asprintf(&str, "%s/%s", config->rundir.value,
371 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE);
372 if (ret < 0) {
373 ERR("Failed to set agent port file path");
374 goto error;
375 }
376 config_string_set(&config->agent_port_file_path, str);
377 str = NULL;
378
379 /*
380 * Allow INSTALL_BIN_PATH to be used as a target path for the
381 * native architecture size consumer if CONFIG_CONSUMER*_PATH
382 * has not been defined.
383 */
384 #if (CAA_BITS_PER_LONG == 32)
385 config_string_set_static(&config->consumerd32_bin_path,
386 INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE);
387 config_string_set_static(&config->consumerd32_lib_dir,
388 INSTALL_LIB_PATH);
389 #elif (CAA_BITS_PER_LONG == 64)
390 config_string_set_static(&config->consumerd64_bin_path,
391 INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE);
392 config_string_set_static(&config->consumerd64_lib_dir,
393 INSTALL_LIB_PATH);
394 #else
395 #error "Unknown bitness"
396 #endif
397 ret = 0;
398 return ret;
399 error:
400 sessiond_config_fini(config);
401 return ret;
402 }
403
404 LTTNG_HIDDEN
405 void sessiond_config_fini(struct sessiond_config *config)
406 {
407 config_string_fini(&config->tracing_group_name);
408 config_string_fini(&config->kmod_probes_list);
409 config_string_fini(&config->kmod_extra_probes_list);
410 config_string_fini(&config->rundir);
411 config_string_fini(&config->apps_unix_sock_path);
412 config_string_fini(&config->client_unix_sock_path);
413 config_string_fini(&config->wait_shm_path);
414 config_string_fini(&config->health_unix_sock_path);
415 config_string_fini(&config->lttng_ust_clock_plugin);
416 config_string_fini(&config->pid_file_path);
417 config_string_fini(&config->lock_file_path);
418 config_string_fini(&config->load_session_path);
419 config_string_fini(&config->agent_port_file_path);
420 config_string_fini(&config->consumerd32_path);
421 config_string_fini(&config->consumerd32_bin_path);
422 config_string_fini(&config->consumerd32_lib_dir);
423 config_string_fini(&config->consumerd32_err_unix_sock_path);
424 config_string_fini(&config->consumerd32_cmd_unix_sock_path);
425 config_string_fini(&config->consumerd64_path);
426 config_string_fini(&config->consumerd64_bin_path);
427 config_string_fini(&config->consumerd64_lib_dir);
428 config_string_fini(&config->consumerd64_err_unix_sock_path);
429 config_string_fini(&config->consumerd64_cmd_unix_sock_path);
430 config_string_fini(&config->kconsumerd_path);
431 config_string_fini(&config->kconsumerd_err_unix_sock_path);
432 config_string_fini(&config->kconsumerd_cmd_unix_sock_path);
433 }
434
435 static
436 int resolve_path(struct config_string *path)
437 {
438 int ret = 0;
439 char *absolute_path;
440
441 if (!path->value || path->value[0] == '/') {
442 goto end;
443 }
444
445 absolute_path = utils_expand_path(path->value);
446 if (!absolute_path) {
447 ret = -1;
448 goto end;
449 }
450
451 config_string_set(path, absolute_path);
452 end:
453 return ret;
454 }
455
456 #define RESOLVE_CHECK(path_config_str) \
457 if (resolve_path(path_config_str)) \
458 return -1
459
460 LTTNG_HIDDEN
461 int sessiond_config_resolve_paths(struct sessiond_config *config)
462 {
463 RESOLVE_CHECK(&config->apps_unix_sock_path);
464 RESOLVE_CHECK(&config->client_unix_sock_path);
465 RESOLVE_CHECK(&config->wait_shm_path);
466 RESOLVE_CHECK(&config->health_unix_sock_path);
467 RESOLVE_CHECK(&config->lttng_ust_clock_plugin);
468 RESOLVE_CHECK(&config->pid_file_path);
469 RESOLVE_CHECK(&config->lock_file_path);
470 RESOLVE_CHECK(&config->load_session_path);
471 RESOLVE_CHECK(&config->agent_port_file_path);
472 RESOLVE_CHECK(&config->consumerd32_path);
473 RESOLVE_CHECK(&config->consumerd32_bin_path);
474 RESOLVE_CHECK(&config->consumerd32_lib_dir);
475 RESOLVE_CHECK(&config->consumerd32_err_unix_sock_path);
476 RESOLVE_CHECK(&config->consumerd32_cmd_unix_sock_path);
477 RESOLVE_CHECK(&config->consumerd64_path);
478 RESOLVE_CHECK(&config->consumerd64_bin_path);
479 RESOLVE_CHECK(&config->consumerd64_lib_dir);
480 RESOLVE_CHECK(&config->consumerd64_err_unix_sock_path);
481 RESOLVE_CHECK(&config->consumerd64_cmd_unix_sock_path);
482 RESOLVE_CHECK(&config->kconsumerd_path);
483 RESOLVE_CHECK(&config->kconsumerd_err_unix_sock_path);
484 RESOLVE_CHECK(&config->kconsumerd_cmd_unix_sock_path);
485 return 0;
486 }
487
488 LTTNG_HIDDEN
489 void sessiond_config_log(struct sessiond_config *config)
490 {
491 DBG_NO_LOC("[sessiond configuration]");
492 DBG_NO_LOC("\tversion %s", VERSION);
493 if (GIT_VERSION[0] != '\0') {
494 DBG_NO_LOC("\tgit version %s", GIT_VERSION);
495 }
496 if (EXTRA_VERSION_NAME[0] != '\0') {
497 DBG_NO_LOC("\textra version name %s", EXTRA_VERSION_NAME);
498 }
499 if (EXTRA_VERSION_DESCRIPTION[0] != '\0') {
500 DBG_NO_LOC("\textra version description:\n\t%s", EXTRA_VERSION_DESCRIPTION);
501 }
502 if (EXTRA_VERSION_PATCHES[0] != '\0') {
503 DBG_NO_LOC("\textra version patches:\n\t%s", EXTRA_VERSION_PATCHES);
504 }
505 DBG_NO_LOC("\tverbose: %i", config->verbose);
506 DBG_NO_LOC("\tverbose consumer: %i", config->verbose_consumer);
507 DBG_NO_LOC("\tquiet mode: %s", config->quiet ? "True" : "False");
508 if (config->agent_tcp_port.begin == config->agent_tcp_port.end) {
509 DBG_NO_LOC("\tagent_tcp_port: %i", config->agent_tcp_port.begin);
510 } else {
511 DBG_NO_LOC("\tagent_tcp_port: [%i, %i]",
512 config->agent_tcp_port.begin,
513 config->agent_tcp_port.end);
514 }
515 DBG_NO_LOC("\tapplication socket timeout: %i", config->app_socket_timeout);
516 DBG_NO_LOC("\tno-kernel: %s", config->no_kernel ? "True" : "False");
517 DBG_NO_LOC("\tbackground: %s", config->background ? "True" : "False");
518 DBG_NO_LOC("\tdaemonize: %s", config->daemonize ? "True" : "False");
519 DBG_NO_LOC("\tsignal parent on start: %s", config->sig_parent ? "True" : "False");
520 DBG_NO_LOC("\ttracing group name: %s", config->tracing_group_name.value ? : "Unknown");
521 DBG_NO_LOC("\tkmod_probe_list: %s", config->kmod_probes_list.value ? : "None");
522 DBG_NO_LOC("\tkmod_extra_probe_list: %s", config->kmod_extra_probes_list.value ? : "None");
523 DBG_NO_LOC("\trundir: %s", config->rundir.value ? : "Unknown");
524 DBG_NO_LOC("\tapplication socket path: %s", config->apps_unix_sock_path.value ? : "Unknown");
525 DBG_NO_LOC("\tclient socket path: %s", config->client_unix_sock_path.value ? : "Unknown");
526 DBG_NO_LOC("\twait shm path: %s", config->wait_shm_path.value ? : "Unknown");
527 DBG_NO_LOC("\thealth socket path: %s", config->health_unix_sock_path.value ? : "Unknown");
528 DBG_NO_LOC("\tLTTNG_UST_CLOCK_PLUGIN: %s", config->lttng_ust_clock_plugin.value ? : "None");
529 DBG_NO_LOC("\tpid file path: %s", config->pid_file_path.value ? : "Unknown");
530 DBG_NO_LOC("\tlock file path: %s", config->lock_file_path.value ? : "Unknown");
531 DBG_NO_LOC("\tsession load path: %s", config->load_session_path.value ? : "None");
532 DBG_NO_LOC("\tagent port file path: %s", config->agent_port_file_path.value ? : "Unknown");
533 DBG_NO_LOC("\tconsumerd32 path: %s", config->consumerd32_path.value ? : "Unknown");
534 DBG_NO_LOC("\tconsumerd32 bin path: %s", config->consumerd32_bin_path.value ? : "Unknown");
535 DBG_NO_LOC("\tconsumerd32 lib dir: %s", config->consumerd32_lib_dir.value ? : "Unknown");
536 DBG_NO_LOC("\tconsumerd32 err unix sock path:%s", config->consumerd32_err_unix_sock_path.value ? : "Unknown");
537 DBG_NO_LOC("\tconsumerd32 cmd unix sock path:%s", config->consumerd32_cmd_unix_sock_path.value ? : "Unknown");
538 DBG_NO_LOC("\tconsumerd64 path: %s", config->consumerd64_path.value ? : "Unknown");
539 DBG_NO_LOC("\tconsumerd64 bin path: %s", config->consumerd64_bin_path.value ? : "Unknown");
540 DBG_NO_LOC("\tconsumerd64 lib dir: %s", config->consumerd64_lib_dir.value ? : "Unknown");
541 DBG_NO_LOC("\tconsumerd64 err unix sock path:%s", config->consumerd64_err_unix_sock_path.value ? : "Unknown");
542 DBG_NO_LOC("\tconsumerd64 cmd unix sock path:%s", config->consumerd64_cmd_unix_sock_path.value ? : "Unknown");
543 DBG_NO_LOC("\tkconsumerd path: %s", config->kconsumerd_path.value ? : "Unknown");
544 DBG_NO_LOC("\tkconsumerd err unix sock path: %s", config->kconsumerd_err_unix_sock_path.value ? : "Unknown");
545 DBG_NO_LOC("\tkconsumerd cmd unix sock path: %s", config->kconsumerd_cmd_unix_sock_path.value ? : "Unknown");
546 }
This page took 0.065002 seconds and 5 git commands to generate.