clang-tidy: add Chrome-inspired checks
[lttng-tools.git] / src / bin / lttng-sessiond / sessiond-config.cpp
... / ...
CommitLineData
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 "lttng-ust-ctl.hpp"
9#include "sessiond-config.hpp"
10#include "version.hpp"
11
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>
18
19#include <ctype.h>
20#include <limits.h>
21
22static struct sessiond_config sessiond_config_build_defaults = {
23 .verbose = 0,
24 .verbose_consumer = 0,
25 .agent_tcp_port = { .begin = DEFAULT_AGENT_TCP_PORT_RANGE_BEGIN,
26 .end = DEFAULT_AGENT_TCP_PORT_RANGE_END },
27
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,
31
32 .quiet = false,
33
34 .no_kernel = false,
35 .background = false,
36 .daemonize = false,
37 .sig_parent = false,
38
39 .tracing_group_name = { (char *) DEFAULT_TRACING_GROUP, false },
40
41 .kmod_probes_list = { nullptr, false },
42 .kmod_extra_probes_list = { nullptr, false },
43
44 .rundir = { nullptr, false },
45
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 },
55
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 },
61
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 },
67
68 .kconsumerd_path = { nullptr, false },
69 .kconsumerd_err_unix_sock_path = { nullptr, false },
70 .kconsumerd_cmd_unix_sock_path = { nullptr, false },
71};
72
73static void config_string_fini(struct config_string *str)
74{
75 config_string_set(str, nullptr);
76}
77
78static void config_string_set_static(struct config_string *config_str, const char *value)
79{
80 config_string_set(config_str, (char *) value);
81 config_str->should_free = false;
82}
83
84/* Only use for dynamically-allocated strings. */
85void config_string_set(struct config_string *config_str, char *value)
86{
87 LTTNG_ASSERT(config_str);
88 if (config_str->should_free) {
89 free(config_str->value);
90 config_str->should_free = false;
91 }
92
93 config_str->should_free = !!value;
94 config_str->value = value;
95}
96
97int 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 || (int_val < 0 && int_val != -1)) {
110 ERR("Invalid value \"%s\" used for \"%s\" environment variable",
111 env_value,
112 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, env_value);
123 }
124 env_value = lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
125 if (env_value) {
126 config_string_set_static(&config->consumerd64_bin_path, env_value);
127 }
128
129 env_value = lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
130 if (env_value) {
131 config_string_set_static(&config->consumerd32_lib_dir, env_value);
132 }
133 env_value = lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
134 if (env_value) {
135 config_string_set_static(&config->consumerd64_lib_dir, env_value);
136 }
137
138 env_value = lttng_secure_getenv("LTTNG_UST_CLOCK_PLUGIN");
139 if (env_value) {
140 config_string_set_static(&config->lttng_ust_clock_plugin, env_value);
141 }
142
143 env_value = lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES);
144 if (env_value) {
145 config_string_set_static(&config->kmod_probes_list, env_value);
146 }
147
148 env_value = lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES);
149 if (env_value) {
150 config_string_set_static(&config->kmod_extra_probes_list, env_value);
151 }
152end:
153 return ret;
154}
155
156static int config_set_paths_root(struct sessiond_config *config)
157{
158 int ret = 0;
159
160 config_string_set(&config->rundir, strdup(DEFAULT_LTTNG_RUNDIR));
161 if (!config->rundir.value) {
162 ERR("Failed to set rundir");
163 ret = -1;
164 goto end;
165 }
166
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);
171end:
172 return ret;
173}
174
175static int config_set_paths_non_root(struct sessiond_config *config)
176{
177 int ret = 0;
178 const char *home_path = utils_get_home_dir();
179 char *str;
180
181 if (home_path == nullptr) {
182 ERR("Can't get HOME directory for sockets creation.");
183 ret = -1;
184 goto end;
185 }
186
187 /*
188 * Create rundir from home path. This will create something like
189 * $HOME/.lttng
190 */
191 ret = asprintf(&str, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
192 if (ret < 0) {
193 ERR("Failed to set rundir");
194 goto end;
195 }
196 config_string_set(&config->rundir, str);
197 str = nullptr;
198
199 ret = asprintf(&str, DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
200 if (ret < 0) {
201 ERR("Failed to set default home apps unix socket path");
202 goto end;
203 }
204 config_string_set(&config->apps_unix_sock_path, str);
205 str = nullptr;
206
207 ret = asprintf(&str, DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
208 if (ret < 0) {
209 ERR("Failed to set default home client unix socket path");
210 goto end;
211 }
212 config_string_set(&config->client_unix_sock_path, str);
213 str = nullptr;
214
215 ret = asprintf(&str, DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
216 if (ret < 0) {
217 ERR("Failed to set default home apps wait shm path");
218 goto end;
219 }
220 config_string_set(&config->wait_shm_path, str);
221 str = nullptr;
222
223 ret = asprintf(&str, DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
224 if (ret < 0) {
225 ERR("Failed to set default home health UNIX socket path");
226 goto end;
227 }
228 config_string_set(&config->health_unix_sock_path, str);
229 str = nullptr;
230
231 ret = 0;
232end:
233 return ret;
234}
235
236int sessiond_config_init(struct sessiond_config *config)
237{
238 int ret;
239 bool is_root = (getuid() == 0);
240 char *str;
241
242 LTTNG_ASSERT(config);
243 memcpy(config, &sessiond_config_build_defaults, sizeof(*config));
244
245 if (is_root) {
246 ret = config_set_paths_root(config);
247 } else {
248 ret = config_set_paths_non_root(config);
249 }
250 if (ret < 0) {
251 goto error;
252 }
253
254 /* 32 bits consumerd path setup */
255 ret = asprintf(&str, DEFAULT_USTCONSUMERD32_PATH, config->rundir.value);
256 if (ret < 0) {
257 ERR("Failed to set 32-bit consumer path");
258 goto error;
259 }
260 config_string_set(&config->consumerd32_path, str);
261 str = nullptr;
262
263 ret = asprintf(&str, DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, config->rundir.value);
264 if (ret < 0) {
265 ERR("Failed to set 32-bit consumer error socket path");
266 goto error;
267 }
268 config_string_set(&config->consumerd32_err_unix_sock_path, str);
269 str = nullptr;
270
271 ret = asprintf(&str, DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, config->rundir.value);
272 if (ret < 0) {
273 ERR("Failed to set 32-bit consumer command socket path");
274 goto error;
275 }
276 config_string_set(&config->consumerd32_cmd_unix_sock_path, str);
277 str = nullptr;
278
279 /* 64 bits consumerd path setup */
280 ret = asprintf(&str, DEFAULT_USTCONSUMERD64_PATH, config->rundir.value);
281 if (ret < 0) {
282 ERR("Failed to set 64-bit consumer path");
283 goto error;
284 }
285 config_string_set(&config->consumerd64_path, str);
286 str = nullptr;
287
288 ret = asprintf(&str, DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, config->rundir.value);
289 if (ret < 0) {
290 ERR("Failed to set 64-bit consumer error socket path");
291 goto error;
292 }
293 config_string_set(&config->consumerd64_err_unix_sock_path, str);
294 str = nullptr;
295
296 ret = asprintf(&str, DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, config->rundir.value);
297 if (ret < 0) {
298 ERR("Failed to set 64-bit consumer command socket path");
299 goto error;
300 }
301 config_string_set(&config->consumerd64_cmd_unix_sock_path, str);
302 str = nullptr;
303
304 /* kconsumerd consumerd path setup */
305 ret = asprintf(&str, DEFAULT_KCONSUMERD_PATH, config->rundir.value);
306 if (ret < 0) {
307 ERR("Failed to set kernel consumer path");
308 goto error;
309 }
310 config_string_set(&config->kconsumerd_path, str);
311 str = nullptr;
312
313 ret = asprintf(&str, DEFAULT_KCONSUMERD_ERR_SOCK_PATH, config->rundir.value);
314 if (ret < 0) {
315 ERR("Failed to set kernel consumer error socket path");
316 goto error;
317 }
318 config_string_set(&config->kconsumerd_err_unix_sock_path, str);
319 str = nullptr;
320
321 ret = asprintf(&str, DEFAULT_KCONSUMERD_CMD_SOCK_PATH, config->rundir.value);
322 if (ret < 0) {
323 ERR("Failed to set kernel consumer command socket path");
324 goto error;
325 }
326 config_string_set(&config->kconsumerd_cmd_unix_sock_path, str);
327 str = nullptr;
328
329 ret = asprintf(&str, "%s/%s", config->rundir.value, DEFAULT_LTTNG_SESSIOND_PIDFILE);
330 if (ret < 0) {
331 ERR("Failed to set PID file path");
332 goto error;
333 }
334 config_string_set(&config->pid_file_path, str);
335 str = nullptr;
336
337 ret = asprintf(&str, "%s/%s", config->rundir.value, DEFAULT_LTTNG_SESSIOND_LOCKFILE);
338 if (ret < 0) {
339 ERR("Failed to set lock file path");
340 goto error;
341 }
342 config_string_set(&config->lock_file_path, str);
343 str = nullptr;
344
345 ret = asprintf(&str, "%s/%s", config->rundir.value, DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE);
346 if (ret < 0) {
347 ERR("Failed to set agent port file path");
348 goto error;
349 }
350 config_string_set(&config->agent_port_file_path, str);
351 str = nullptr;
352
353 /*
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.
357 */
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);
366#else
367#error "Unknown bitness"
368#endif
369 ret = 0;
370 return ret;
371error:
372 sessiond_config_fini(config);
373 return ret;
374}
375
376void sessiond_config_fini(struct sessiond_config *config)
377{
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);
404}
405
406static int resolve_path(struct config_string *path)
407{
408 int ret = 0;
409 char *absolute_path;
410
411 if (!path->value || path->value[0] == '/') {
412 goto end;
413 }
414
415 absolute_path = utils_expand_path(path->value);
416 if (!absolute_path) {
417 ret = -1;
418 goto end;
419 }
420
421 config_string_set(path, absolute_path);
422end:
423 return ret;
424}
425
426#define RESOLVE_CHECK(path_config_str) \
427 if (resolve_path(path_config_str)) \
428 return -1
429
430int sessiond_config_resolve_paths(struct sessiond_config *config)
431{
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);
454 return 0;
455}
456
457void sessiond_config_log(struct sessiond_config *config)
458{
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);
463 }
464 if (EXTRA_VERSION_NAME[0] != '\0') {
465 DBG_NO_LOC("\textra version name %s", EXTRA_VERSION_NAME);
466 }
467 if (EXTRA_VERSION_DESCRIPTION[0] != '\0') {
468 DBG_NO_LOC("\textra version description:\n\t%s", EXTRA_VERSION_DESCRIPTION);
469 }
470 if (EXTRA_VERSION_PATCHES[0] != '\0') {
471 DBG_NO_LOC("\textra version patches:\n\t%s", EXTRA_VERSION_PATCHES);
472 }
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);
478 } else {
479 DBG_NO_LOC("\tagent_tcp_port: [%i, %i]",
480 config->agent_tcp_port.begin,
481 config->agent_tcp_port.end);
482 }
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");
536}
This page took 0.023736 seconds and 4 git commands to generate.