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