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