2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "lttng-ust-ctl.hpp"
9 #include "sessiond-config.hpp"
10 #include "version.hpp"
12 #include <common/compat/errno.hpp>
13 #include <common/compat/getenv.hpp>
14 #include <common/defaults.hpp>
15 #include <common/error.hpp>
16 #include <common/path.hpp>
17 #include <common/utils.hpp>
22 static struct sessiond_config sessiond_config_build_defaults
= {
24 .verbose_consumer
= 0,
25 .agent_tcp_port
= { .begin
= DEFAULT_AGENT_TCP_PORT_RANGE_BEGIN
,
26 .end
= DEFAULT_AGENT_TCP_PORT_RANGE_END
},
28 .event_notifier_buffer_size_kernel
= DEFAULT_EVENT_NOTIFIER_ERROR_COUNT_MAP_SIZE
,
29 .event_notifier_buffer_size_userspace
= DEFAULT_EVENT_NOTIFIER_ERROR_COUNT_MAP_SIZE
,
30 .app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
,
39 .tracing_group_name
= { (char *) DEFAULT_TRACING_GROUP
, false },
41 .kmod_probes_list
= { nullptr, false },
42 .kmod_extra_probes_list
= { nullptr, false },
44 .rundir
= { nullptr, false },
46 .apps_unix_sock_path
= { nullptr, false },
47 .client_unix_sock_path
= { nullptr, false },
48 .wait_shm_path
= { nullptr, false },
49 .health_unix_sock_path
= { nullptr, false },
50 .lttng_ust_clock_plugin
= { nullptr, false },
51 .pid_file_path
= { nullptr, false },
52 .lock_file_path
= { nullptr, false },
53 .load_session_path
= { nullptr, false },
54 .agent_port_file_path
= { nullptr, false },
56 .consumerd32_path
= { nullptr, false },
57 .consumerd32_bin_path
= { nullptr, false },
58 .consumerd32_lib_dir
= { nullptr, false },
59 .consumerd32_err_unix_sock_path
= { nullptr, false },
60 .consumerd32_cmd_unix_sock_path
= { nullptr, false },
62 .consumerd64_path
= { nullptr, false },
63 .consumerd64_bin_path
= { nullptr, false },
64 .consumerd64_lib_dir
= { nullptr, false },
65 .consumerd64_err_unix_sock_path
= { nullptr, false },
66 .consumerd64_cmd_unix_sock_path
= { nullptr, false },
68 .kconsumerd_path
= { nullptr, false },
69 .kconsumerd_err_unix_sock_path
= { nullptr, false },
70 .kconsumerd_cmd_unix_sock_path
= { nullptr, false },
73 static void config_string_fini(struct config_string
*str
)
75 config_string_set(str
, nullptr);
78 static void config_string_set_static(struct config_string
*config_str
, const char *value
)
80 config_string_set(config_str
, (char *) value
);
81 config_str
->should_free
= false;
84 /* Only use for dynamically-allocated strings. */
85 void config_string_set(struct config_string
*config_str
, char *value
)
87 LTTNG_ASSERT(config_str
);
88 if (config_str
->should_free
) {
89 free(config_str
->value
);
90 config_str
->should_free
= false;
93 config_str
->should_free
= !!value
;
94 config_str
->value
= value
;
97 int sessiond_config_apply_env_config(struct sessiond_config
*config
)
100 const char *env_value
;
102 env_value
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
108 int_val
= strtoul(env_value
, &endptr
, 0);
109 if (errno
!= 0 || int_val
> INT_MAX
|| (int_val
< 0 && int_val
!= -1)) {
110 ERR("Invalid value \"%s\" used for \"%s\" environment variable",
112 DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
117 config
->app_socket_timeout
= int_val
;
120 env_value
= lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
122 config_string_set_static(&config
->consumerd32_bin_path
, env_value
);
124 env_value
= lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
126 config_string_set_static(&config
->consumerd64_bin_path
, env_value
);
129 env_value
= lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
131 config_string_set_static(&config
->consumerd32_lib_dir
, env_value
);
133 env_value
= lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
135 config_string_set_static(&config
->consumerd64_lib_dir
, env_value
);
138 env_value
= lttng_secure_getenv("LTTNG_UST_CLOCK_PLUGIN");
140 config_string_set_static(&config
->lttng_ust_clock_plugin
, env_value
);
143 env_value
= lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES
);
145 config_string_set_static(&config
->kmod_probes_list
, env_value
);
148 env_value
= lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES
);
150 config_string_set_static(&config
->kmod_extra_probes_list
, env_value
);
156 static int config_set_paths_root(struct sessiond_config
*config
)
160 config_string_set(&config
->rundir
, strdup(DEFAULT_LTTNG_RUNDIR
));
161 if (!config
->rundir
.value
) {
162 ERR("Failed to set rundir");
167 config_string_set_static(&config
->apps_unix_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
168 config_string_set_static(&config
->client_unix_sock_path
, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
169 config_string_set_static(&config
->wait_shm_path
, DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
170 config_string_set_static(&config
->health_unix_sock_path
, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
175 static int config_set_paths_non_root(struct sessiond_config
*config
)
178 const char *home_path
= utils_get_home_dir();
181 if (home_path
== nullptr) {
182 ERR("Can't get HOME directory for sockets creation.");
188 * Create rundir from home path. This will create something like
191 ret
= asprintf(&str
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
193 ERR("Failed to set rundir");
196 config_string_set(&config
->rundir
, str
);
199 ret
= asprintf(&str
, DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
201 ERR("Failed to set default home apps unix socket path");
204 config_string_set(&config
->apps_unix_sock_path
, str
);
207 ret
= asprintf(&str
, DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
209 ERR("Failed to set default home client unix socket path");
212 config_string_set(&config
->client_unix_sock_path
, str
);
215 ret
= asprintf(&str
, DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
217 ERR("Failed to set default home apps wait shm path");
220 config_string_set(&config
->wait_shm_path
, str
);
223 ret
= asprintf(&str
, DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
225 ERR("Failed to set default home health UNIX socket path");
228 config_string_set(&config
->health_unix_sock_path
, str
);
236 int sessiond_config_init(struct sessiond_config
*config
)
239 bool is_root
= (getuid() == 0);
242 LTTNG_ASSERT(config
);
243 memcpy(config
, &sessiond_config_build_defaults
, sizeof(*config
));
246 ret
= config_set_paths_root(config
);
248 ret
= config_set_paths_non_root(config
);
254 /* 32 bits consumerd path setup */
255 ret
= asprintf(&str
, DEFAULT_USTCONSUMERD32_PATH
, config
->rundir
.value
);
257 ERR("Failed to set 32-bit consumer path");
260 config_string_set(&config
->consumerd32_path
, str
);
263 ret
= asprintf(&str
, DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, config
->rundir
.value
);
265 ERR("Failed to set 32-bit consumer error socket path");
268 config_string_set(&config
->consumerd32_err_unix_sock_path
, str
);
271 ret
= asprintf(&str
, DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, config
->rundir
.value
);
273 ERR("Failed to set 32-bit consumer command socket path");
276 config_string_set(&config
->consumerd32_cmd_unix_sock_path
, str
);
279 /* 64 bits consumerd path setup */
280 ret
= asprintf(&str
, DEFAULT_USTCONSUMERD64_PATH
, config
->rundir
.value
);
282 ERR("Failed to set 64-bit consumer path");
285 config_string_set(&config
->consumerd64_path
, str
);
288 ret
= asprintf(&str
, DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, config
->rundir
.value
);
290 ERR("Failed to set 64-bit consumer error socket path");
293 config_string_set(&config
->consumerd64_err_unix_sock_path
, str
);
296 ret
= asprintf(&str
, DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, config
->rundir
.value
);
298 ERR("Failed to set 64-bit consumer command socket path");
301 config_string_set(&config
->consumerd64_cmd_unix_sock_path
, str
);
304 /* kconsumerd consumerd path setup */
305 ret
= asprintf(&str
, DEFAULT_KCONSUMERD_PATH
, config
->rundir
.value
);
307 ERR("Failed to set kernel consumer path");
310 config_string_set(&config
->kconsumerd_path
, str
);
313 ret
= asprintf(&str
, DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, config
->rundir
.value
);
315 ERR("Failed to set kernel consumer error socket path");
318 config_string_set(&config
->kconsumerd_err_unix_sock_path
, str
);
321 ret
= asprintf(&str
, DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, config
->rundir
.value
);
323 ERR("Failed to set kernel consumer command socket path");
326 config_string_set(&config
->kconsumerd_cmd_unix_sock_path
, str
);
329 ret
= asprintf(&str
, "%s/%s", config
->rundir
.value
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
331 ERR("Failed to set PID file path");
334 config_string_set(&config
->pid_file_path
, str
);
337 ret
= asprintf(&str
, "%s/%s", config
->rundir
.value
, DEFAULT_LTTNG_SESSIOND_LOCKFILE
);
339 ERR("Failed to set lock file path");
342 config_string_set(&config
->lock_file_path
, str
);
345 ret
= asprintf(&str
, "%s/%s", config
->rundir
.value
, DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
347 ERR("Failed to set agent port file path");
350 config_string_set(&config
->agent_port_file_path
, str
);
354 * Allow INSTALL_BIN_PATH to be used as a target path for the
355 * native architecture size consumer if CONFIG_CONSUMER*_PATH
356 * has not been defined.
358 #if (CAA_BITS_PER_LONG == 32)
359 config_string_set_static(&config
->consumerd32_bin_path
,
360 INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
);
361 config_string_set_static(&config
->consumerd32_lib_dir
, INSTALL_LIB_PATH
);
362 #elif (CAA_BITS_PER_LONG == 64)
363 config_string_set_static(&config
->consumerd64_bin_path
,
364 INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
);
365 config_string_set_static(&config
->consumerd64_lib_dir
, INSTALL_LIB_PATH
);
367 #error "Unknown bitness"
372 sessiond_config_fini(config
);
376 void sessiond_config_fini(struct sessiond_config
*config
)
378 config_string_fini(&config
->tracing_group_name
);
379 config_string_fini(&config
->kmod_probes_list
);
380 config_string_fini(&config
->kmod_extra_probes_list
);
381 config_string_fini(&config
->rundir
);
382 config_string_fini(&config
->apps_unix_sock_path
);
383 config_string_fini(&config
->client_unix_sock_path
);
384 config_string_fini(&config
->wait_shm_path
);
385 config_string_fini(&config
->health_unix_sock_path
);
386 config_string_fini(&config
->lttng_ust_clock_plugin
);
387 config_string_fini(&config
->pid_file_path
);
388 config_string_fini(&config
->lock_file_path
);
389 config_string_fini(&config
->load_session_path
);
390 config_string_fini(&config
->agent_port_file_path
);
391 config_string_fini(&config
->consumerd32_path
);
392 config_string_fini(&config
->consumerd32_bin_path
);
393 config_string_fini(&config
->consumerd32_lib_dir
);
394 config_string_fini(&config
->consumerd32_err_unix_sock_path
);
395 config_string_fini(&config
->consumerd32_cmd_unix_sock_path
);
396 config_string_fini(&config
->consumerd64_path
);
397 config_string_fini(&config
->consumerd64_bin_path
);
398 config_string_fini(&config
->consumerd64_lib_dir
);
399 config_string_fini(&config
->consumerd64_err_unix_sock_path
);
400 config_string_fini(&config
->consumerd64_cmd_unix_sock_path
);
401 config_string_fini(&config
->kconsumerd_path
);
402 config_string_fini(&config
->kconsumerd_err_unix_sock_path
);
403 config_string_fini(&config
->kconsumerd_cmd_unix_sock_path
);
406 static int resolve_path(struct config_string
*path
)
411 if (!path
->value
|| path
->value
[0] == '/') {
415 absolute_path
= utils_expand_path(path
->value
);
416 if (!absolute_path
) {
421 config_string_set(path
, absolute_path
);
426 #define RESOLVE_CHECK(path_config_str) \
427 if (resolve_path(path_config_str)) \
430 int sessiond_config_resolve_paths(struct sessiond_config
*config
)
432 RESOLVE_CHECK(&config
->apps_unix_sock_path
);
433 RESOLVE_CHECK(&config
->client_unix_sock_path
);
434 RESOLVE_CHECK(&config
->wait_shm_path
);
435 RESOLVE_CHECK(&config
->health_unix_sock_path
);
436 RESOLVE_CHECK(&config
->lttng_ust_clock_plugin
);
437 RESOLVE_CHECK(&config
->pid_file_path
);
438 RESOLVE_CHECK(&config
->lock_file_path
);
439 RESOLVE_CHECK(&config
->load_session_path
);
440 RESOLVE_CHECK(&config
->agent_port_file_path
);
441 RESOLVE_CHECK(&config
->consumerd32_path
);
442 RESOLVE_CHECK(&config
->consumerd32_bin_path
);
443 RESOLVE_CHECK(&config
->consumerd32_lib_dir
);
444 RESOLVE_CHECK(&config
->consumerd32_err_unix_sock_path
);
445 RESOLVE_CHECK(&config
->consumerd32_cmd_unix_sock_path
);
446 RESOLVE_CHECK(&config
->consumerd64_path
);
447 RESOLVE_CHECK(&config
->consumerd64_bin_path
);
448 RESOLVE_CHECK(&config
->consumerd64_lib_dir
);
449 RESOLVE_CHECK(&config
->consumerd64_err_unix_sock_path
);
450 RESOLVE_CHECK(&config
->consumerd64_cmd_unix_sock_path
);
451 RESOLVE_CHECK(&config
->kconsumerd_path
);
452 RESOLVE_CHECK(&config
->kconsumerd_err_unix_sock_path
);
453 RESOLVE_CHECK(&config
->kconsumerd_cmd_unix_sock_path
);
457 void sessiond_config_log(struct sessiond_config
*config
)
459 DBG_NO_LOC("[sessiond configuration]");
460 DBG_NO_LOC("\tversion %s", VERSION
);
461 if (GIT_VERSION
[0] != '\0') {
462 DBG_NO_LOC("\tgit version %s", GIT_VERSION
);
464 if (EXTRA_VERSION_NAME
[0] != '\0') {
465 DBG_NO_LOC("\textra version name %s", EXTRA_VERSION_NAME
);
467 if (EXTRA_VERSION_DESCRIPTION
[0] != '\0') {
468 DBG_NO_LOC("\textra version description:\n\t%s", EXTRA_VERSION_DESCRIPTION
);
470 if (EXTRA_VERSION_PATCHES
[0] != '\0') {
471 DBG_NO_LOC("\textra version patches:\n\t%s", EXTRA_VERSION_PATCHES
);
473 DBG_NO_LOC("\tverbose: %i", config
->verbose
);
474 DBG_NO_LOC("\tverbose consumer: %i", config
->verbose_consumer
);
475 DBG_NO_LOC("\tquiet mode: %s", config
->quiet
? "True" : "False");
476 if (config
->agent_tcp_port
.begin
== config
->agent_tcp_port
.end
) {
477 DBG_NO_LOC("\tagent_tcp_port: %i", config
->agent_tcp_port
.begin
);
479 DBG_NO_LOC("\tagent_tcp_port: [%i, %i]",
480 config
->agent_tcp_port
.begin
,
481 config
->agent_tcp_port
.end
);
483 DBG_NO_LOC("\tapplication socket timeout: %i", config
->app_socket_timeout
);
484 DBG_NO_LOC("\tno-kernel: %s", config
->no_kernel
? "True" : "False");
485 DBG_NO_LOC("\tbackground: %s", config
->background
? "True" : "False");
486 DBG_NO_LOC("\tdaemonize: %s", config
->daemonize
? "True" : "False");
487 DBG_NO_LOC("\tsignal parent on start: %s", config
->sig_parent
? "True" : "False");
488 DBG_NO_LOC("\ttracing group name: %s",
489 config
->tracing_group_name
.value
?: "Unknown");
490 DBG_NO_LOC("\tkmod_probe_list: %s", config
->kmod_probes_list
.value
?: "None");
491 DBG_NO_LOC("\tkmod_extra_probe_list: %s",
492 config
->kmod_extra_probes_list
.value
?: "None");
493 DBG_NO_LOC("\trundir: %s", config
->rundir
.value
?: "Unknown");
494 DBG_NO_LOC("\tapplication socket path: %s",
495 config
->apps_unix_sock_path
.value
?: "Unknown");
496 DBG_NO_LOC("\tclient socket path: %s",
497 config
->client_unix_sock_path
.value
?: "Unknown");
498 DBG_NO_LOC("\twait shm path: %s", config
->wait_shm_path
.value
?: "Unknown");
499 DBG_NO_LOC("\thealth socket path: %s",
500 config
->health_unix_sock_path
.value
?: "Unknown");
501 DBG_NO_LOC("\tLTTNG_UST_CLOCK_PLUGIN: %s",
502 config
->lttng_ust_clock_plugin
.value
?: "None");
503 DBG_NO_LOC("\tpid file path: %s", config
->pid_file_path
.value
?: "Unknown");
504 DBG_NO_LOC("\tlock file path: %s",
505 config
->lock_file_path
.value
?: "Unknown");
506 DBG_NO_LOC("\tsession load path: %s",
507 config
->load_session_path
.value
?: "None");
508 DBG_NO_LOC("\tagent port file path: %s",
509 config
->agent_port_file_path
.value
?: "Unknown");
510 DBG_NO_LOC("\tconsumerd32 path: %s",
511 config
->consumerd32_path
.value
?: "Unknown");
512 DBG_NO_LOC("\tconsumerd32 bin path: %s",
513 config
->consumerd32_bin_path
.value
?: "Unknown");
514 DBG_NO_LOC("\tconsumerd32 lib dir: %s",
515 config
->consumerd32_lib_dir
.value
?: "Unknown");
516 DBG_NO_LOC("\tconsumerd32 err unix sock path:%s",
517 config
->consumerd32_err_unix_sock_path
.value
?: "Unknown");
518 DBG_NO_LOC("\tconsumerd32 cmd unix sock path:%s",
519 config
->consumerd32_cmd_unix_sock_path
.value
?: "Unknown");
520 DBG_NO_LOC("\tconsumerd64 path: %s",
521 config
->consumerd64_path
.value
?: "Unknown");
522 DBG_NO_LOC("\tconsumerd64 bin path: %s",
523 config
->consumerd64_bin_path
.value
?: "Unknown");
524 DBG_NO_LOC("\tconsumerd64 lib dir: %s",
525 config
->consumerd64_lib_dir
.value
?: "Unknown");
526 DBG_NO_LOC("\tconsumerd64 err unix sock path:%s",
527 config
->consumerd64_err_unix_sock_path
.value
?: "Unknown");
528 DBG_NO_LOC("\tconsumerd64 cmd unix sock path:%s",
529 config
->consumerd64_cmd_unix_sock_path
.value
?: "Unknown");
530 DBG_NO_LOC("\tkconsumerd path: %s",
531 config
->kconsumerd_path
.value
?: "Unknown");
532 DBG_NO_LOC("\tkconsumerd err unix sock path: %s",
533 config
->kconsumerd_err_unix_sock_path
.value
?: "Unknown");
534 DBG_NO_LOC("\tkconsumerd cmd unix sock path: %s",
535 config
->kconsumerd_cmd_unix_sock_path
.value
?: "Unknown");