clang-tidy: add most bugprone warnings
[lttng-tools.git] / src / bin / lttng-sessiond / main.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10#define _LGPL_SOURCE
11#include "agent-thread.hpp"
12#include "agent.hpp"
13#include "buffer-registry.hpp"
14#include "channel.hpp"
15#include "client.hpp"
16#include "cmd.hpp"
17#include "consumer.hpp"
18#include "context.hpp"
19#include "dispatch.hpp"
20#include "event-notifier-error-accounting.hpp"
21#include "event.hpp"
22#include "fd-limit.hpp"
23#include "health-sessiond.hpp"
24#include "kernel-consumer.hpp"
25#include "kernel.hpp"
26#include "lttng-sessiond.hpp"
27#include "lttng-ust-ctl.hpp"
28#include "manage-apps.hpp"
29#include "manage-kernel.hpp"
30#include "modprobe.hpp"
31#include "notification-thread-commands.hpp"
32#include "notification-thread.hpp"
33#include "notify-apps.hpp"
34#include "register.hpp"
35#include "rotation-thread.hpp"
36#include "save.hpp"
37#include "sessiond-config.hpp"
38#include "testpoint.hpp"
39#include "thread.hpp"
40#include "timer.hpp"
41#include "ust-consumer.hpp"
42#include "ust-sigbus.hpp"
43#include "utils.hpp"
44
45#include <common/common.hpp>
46#include <common/compat/getenv.hpp>
47#include <common/compat/socket.hpp>
48#include <common/config/session-config.hpp>
49#include <common/daemonize.hpp>
50#include <common/defaults.hpp>
51#include <common/dynamic-buffer.hpp>
52#include <common/futex.hpp>
53#include <common/ini-config/ini-config.hpp>
54#include <common/kernel-consumer/kernel-consumer.hpp>
55#include <common/logging-utils.hpp>
56#include <common/path.hpp>
57#include <common/relayd/relayd.hpp>
58#include <common/utils.hpp>
59
60#include <lttng/event-internal.hpp>
61
62#include <ctype.h>
63#include <getopt.h>
64#include <grp.h>
65#include <inttypes.h>
66#include <limits.h>
67#include <paths.h>
68#include <pthread.h>
69#include <signal.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#include <sys/mman.h>
74#include <sys/mount.h>
75#include <sys/resource.h>
76#include <sys/socket.h>
77#include <sys/stat.h>
78#include <sys/types.h>
79#include <sys/wait.h>
80#include <unistd.h>
81#include <urcu/uatomic.h>
82
83static const char *help_msg =
84#ifdef LTTNG_EMBED_HELP
85#include <lttng-sessiond.8.h>
86#else
87 nullptr
88#endif
89 ;
90
91#define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
92#define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "event-notifier-error-buffer-size"
93#define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \
94 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel"
95#define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \
96 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-userspace"
97
98const char *progname;
99static int lockfile_fd = -1;
100static int opt_print_version;
101
102/* Set to 1 when a SIGUSR1 signal is received. */
103static int recv_child_signal;
104
105/* Command line options */
106static const struct option long_options[] = {
107 { "client-sock", required_argument, nullptr, 'c' },
108 { "apps-sock", required_argument, nullptr, 'a' },
109 { "kconsumerd-cmd-sock", required_argument, nullptr, '\0' },
110 { "kconsumerd-err-sock", required_argument, nullptr, '\0' },
111 { "ustconsumerd32-cmd-sock", required_argument, nullptr, '\0' },
112 { "ustconsumerd32-err-sock", required_argument, nullptr, '\0' },
113 { "ustconsumerd64-cmd-sock", required_argument, nullptr, '\0' },
114 { "ustconsumerd64-err-sock", required_argument, nullptr, '\0' },
115 { "consumerd32-path", required_argument, nullptr, '\0' },
116 { "consumerd32-libdir", required_argument, nullptr, '\0' },
117 { "consumerd64-path", required_argument, nullptr, '\0' },
118 { "consumerd64-libdir", required_argument, nullptr, '\0' },
119 { "daemonize", no_argument, nullptr, 'd' },
120 { "background", no_argument, nullptr, 'b' },
121 { "sig-parent", no_argument, nullptr, 'S' },
122 { "help", no_argument, nullptr, 'h' },
123 { "group", required_argument, nullptr, 'g' },
124 { "version", no_argument, nullptr, 'V' },
125 { "quiet", no_argument, nullptr, 'q' },
126 { "verbose", no_argument, nullptr, 'v' },
127 { "verbose-consumer", no_argument, nullptr, '\0' },
128 { "no-kernel", no_argument, nullptr, '\0' },
129 { "pidfile", required_argument, nullptr, 'p' },
130 { "agent-tcp-port", required_argument, nullptr, '\0' },
131 { "config", required_argument, nullptr, 'f' },
132 { "load", required_argument, nullptr, 'l' },
133 { "kmod-probes", required_argument, nullptr, '\0' },
134 { "extra-kmod-probes", required_argument, nullptr, '\0' },
135 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR, required_argument, nullptr, '\0' },
136 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR, required_argument, nullptr, '\0' },
137 { nullptr, 0, nullptr, 0 }
138};
139
140/* Command line options to ignore from configuration file */
141static const char *config_ignore_options[] = { "help", "version", "config" };
142
143/*
144 * This pipe is used to inform the thread managing application communication
145 * that a command is queued and ready to be processed.
146 */
147static int apps_cmd_pipe[2] = { -1, -1 };
148static int apps_cmd_notify_pipe[2] = { -1, -1 };
149
150/*
151 * UST registration command queue. This queue is tied with a futex and uses a N
152 * wakers / 1 waiter implemented and detailed in futex.c/.h
153 *
154 * The thread_registration_apps and thread_dispatch_ust_registration uses this
155 * queue along with the wait/wake scheme. The thread_manage_apps receives down
156 * the line new application socket and monitors it for any I/O error or clean
157 * close that triggers an unregistration of the application.
158 */
159static struct ust_cmd_queue ust_cmd_queue;
160
161/*
162 * Section name to look for in the daemon configuration file.
163 */
164static const char *const config_section_name = "sessiond";
165
166/* Am I root or not. Set to 1 if the daemon is running as root */
167static int is_root;
168
169/*
170 * Notify the main thread to initiate the teardown of the worker threads by
171 * writing to the main quit pipe.
172 */
173static void notify_main_quit_pipe()
174{
175 int ret;
176
177 /* Stopping all threads */
178 DBG("Notify the main thread to terminate all worker threads");
179 ret = sessiond_notify_main_quit_pipe();
180 if (ret < 0) {
181 ERR("write error on main quit pipe");
182 }
183}
184
185/*
186 * Close every consumer sockets.
187 */
188static void close_consumer_sockets()
189{
190 int ret;
191
192 if (the_kconsumer_data.err_sock >= 0) {
193 ret = close(the_kconsumer_data.err_sock);
194 if (ret < 0) {
195 PERROR("kernel consumer err_sock close");
196 }
197 }
198 if (the_ustconsumer32_data.err_sock >= 0) {
199 ret = close(the_ustconsumer32_data.err_sock);
200 if (ret < 0) {
201 PERROR("UST consumerd32 err_sock close");
202 }
203 }
204 if (the_ustconsumer64_data.err_sock >= 0) {
205 ret = close(the_ustconsumer64_data.err_sock);
206 if (ret < 0) {
207 PERROR("UST consumerd64 err_sock close");
208 }
209 }
210 if (the_kconsumer_data.cmd_sock >= 0) {
211 ret = close(the_kconsumer_data.cmd_sock);
212 if (ret < 0) {
213 PERROR("kernel consumer cmd_sock close");
214 }
215 }
216 if (the_ustconsumer32_data.cmd_sock >= 0) {
217 ret = close(the_ustconsumer32_data.cmd_sock);
218 if (ret < 0) {
219 PERROR("UST consumerd32 cmd_sock close");
220 }
221 }
222 if (the_ustconsumer64_data.cmd_sock >= 0) {
223 ret = close(the_ustconsumer64_data.cmd_sock);
224 if (ret < 0) {
225 PERROR("UST consumerd64 cmd_sock close");
226 }
227 }
228 if (the_kconsumer_data.channel_monitor_pipe >= 0) {
229 ret = close(the_kconsumer_data.channel_monitor_pipe);
230 if (ret < 0) {
231 PERROR("kernel consumer channel monitor pipe close");
232 }
233 }
234 if (the_ustconsumer32_data.channel_monitor_pipe >= 0) {
235 ret = close(the_ustconsumer32_data.channel_monitor_pipe);
236 if (ret < 0) {
237 PERROR("UST consumerd32 channel monitor pipe close");
238 }
239 }
240 if (the_ustconsumer64_data.channel_monitor_pipe >= 0) {
241 ret = close(the_ustconsumer64_data.channel_monitor_pipe);
242 if (ret < 0) {
243 PERROR("UST consumerd64 channel monitor pipe close");
244 }
245 }
246}
247
248/*
249 * Wait on consumer process termination.
250 *
251 * Need to be called with the consumer data lock held or from a context
252 * ensuring no concurrent access to data (e.g: cleanup).
253 */
254static void wait_consumer(struct consumer_data *consumer_data)
255{
256 pid_t ret;
257 int status;
258
259 if (consumer_data->pid <= 0) {
260 return;
261 }
262
263 DBG("Waiting for complete teardown of consumerd (PID: %d)", consumer_data->pid);
264 ret = waitpid(consumer_data->pid, &status, 0);
265 if (ret == -1) {
266 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
267 } else if (!WIFEXITED(status)) {
268 ERR("consumerd termination with error: %d", WEXITSTATUS(ret));
269 }
270 consumer_data->pid = 0;
271}
272
273/*
274 * Cleanup the session daemon's data structures.
275 */
276static void sessiond_cleanup()
277{
278 int ret;
279 struct ltt_session_list *session_list = session_get_list();
280
281 DBG("Cleanup sessiond");
282
283 /*
284 * Close the main quit pipe. It has already done its job, since we are
285 * now cleaning up.
286 */
287 sessiond_close_main_quit_pipe();
288
289 /* Close all other pipes. */
290 utils_close_pipe(apps_cmd_pipe);
291 utils_close_pipe(apps_cmd_notify_pipe);
292 utils_close_pipe(the_kernel_poll_pipe);
293
294 ret = remove(the_config.pid_file_path.value);
295 if (ret < 0) {
296 PERROR("remove pidfile %s", the_config.pid_file_path.value);
297 }
298
299 DBG("Removing sessiond and consumerd content of directory %s", the_config.rundir.value);
300
301 /* sessiond */
302 DBG("Removing %s", the_config.pid_file_path.value);
303 (void) unlink(the_config.pid_file_path.value);
304
305 DBG("Removing %s", the_config.agent_port_file_path.value);
306 (void) unlink(the_config.agent_port_file_path.value);
307
308 /* kconsumerd */
309 DBG("Removing %s", the_kconsumer_data.err_unix_sock_path);
310 (void) unlink(the_kconsumer_data.err_unix_sock_path);
311
312 DBG("Removing directory %s", the_config.kconsumerd_path.value);
313 (void) rmdir(the_config.kconsumerd_path.value);
314
315 /* ust consumerd 32 */
316 DBG("Removing %s", the_config.consumerd32_err_unix_sock_path.value);
317 (void) unlink(the_config.consumerd32_err_unix_sock_path.value);
318
319 DBG("Removing directory %s", the_config.consumerd32_path.value);
320 (void) rmdir(the_config.consumerd32_path.value);
321
322 /* ust consumerd 64 */
323 DBG("Removing %s", the_config.consumerd64_err_unix_sock_path.value);
324 (void) unlink(the_config.consumerd64_err_unix_sock_path.value);
325
326 DBG("Removing directory %s", the_config.consumerd64_path.value);
327 (void) rmdir(the_config.consumerd64_path.value);
328
329 pthread_mutex_destroy(&session_list->lock);
330
331 DBG("Cleaning up all per-event notifier domain agents");
332 agent_by_event_notifier_domain_ht_destroy();
333
334 DBG("Cleaning up all agent apps");
335 agent_app_ht_clean();
336 DBG("Closing all UST sockets");
337 ust_app_clean_list();
338 buffer_reg_destroy_registries();
339
340 close_consumer_sockets();
341
342 wait_consumer(&the_kconsumer_data);
343 wait_consumer(&the_ustconsumer64_data);
344 wait_consumer(&the_ustconsumer32_data);
345
346 if (is_root && !the_config.no_kernel) {
347 cleanup_kernel_tracer();
348 }
349
350 /*
351 * We do NOT rmdir rundir because there are other processes
352 * using it, for instance lttng-relayd, which can start in
353 * parallel with this teardown.
354 */
355}
356
357/*
358 * Cleanup the daemon's option data structures.
359 */
360static void sessiond_cleanup_options()
361{
362 DBG("Cleaning up options");
363
364 sessiond_config_fini(&the_config);
365
366 run_as_destroy_worker();
367}
368
369static int string_match(const char *str1, const char *str2)
370{
371 return (str1 && str2) && !strcmp(str1, str2);
372}
373
374/*
375 * Take an option from the getopt output and set it in the right variable to be
376 * used later.
377 *
378 * Return 0 on success else a negative value.
379 */
380static int set_option(int opt, const char *arg, const char *optname)
381{
382 int ret = 0;
383
384 if (string_match(optname, "client-sock") || opt == 'c') {
385 if (!arg || *arg == '\0') {
386 ret = -EINVAL;
387 goto end;
388 }
389 if (lttng_is_setuid_setgid()) {
390 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
391 "-c, --client-sock");
392 } else {
393 config_string_set(&the_config.client_unix_sock_path, strdup(arg));
394 if (!the_config.client_unix_sock_path.value) {
395 ret = -ENOMEM;
396 PERROR("strdup");
397 }
398 }
399 } else if (string_match(optname, "apps-sock") || opt == 'a') {
400 if (!arg || *arg == '\0') {
401 ret = -EINVAL;
402 goto end;
403 }
404 if (lttng_is_setuid_setgid()) {
405 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
406 "-a, --apps-sock");
407 } else {
408 config_string_set(&the_config.apps_unix_sock_path, strdup(arg));
409 if (!the_config.apps_unix_sock_path.value) {
410 ret = -ENOMEM;
411 PERROR("strdup");
412 }
413 }
414 } else if (string_match(optname, "daemonize") || opt == 'd') {
415 the_config.daemonize = true;
416 } else if (string_match(optname, "background") || opt == 'b') {
417 the_config.background = true;
418 } else if (string_match(optname, "group") || opt == 'g') {
419 if (!arg || *arg == '\0') {
420 ret = -EINVAL;
421 goto end;
422 }
423 if (lttng_is_setuid_setgid()) {
424 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
425 "-g, --group");
426 } else {
427 config_string_set(&the_config.tracing_group_name, strdup(arg));
428 if (!the_config.tracing_group_name.value) {
429 ret = -ENOMEM;
430 PERROR("strdup");
431 }
432 }
433 } else if (string_match(optname, "help") || opt == 'h') {
434 ret = utils_show_help(8, "lttng-sessiond", help_msg);
435 if (ret) {
436 ERR("Cannot show --help for `lttng-sessiond`");
437 perror("exec");
438 }
439 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
440 } else if (string_match(optname, "version") || opt == 'V') {
441 opt_print_version = 1;
442 } else if (string_match(optname, "sig-parent") || opt == 'S') {
443 the_config.sig_parent = true;
444 } else if (string_match(optname, "kconsumerd-err-sock")) {
445 if (!arg || *arg == '\0') {
446 ret = -EINVAL;
447 goto end;
448 }
449 if (lttng_is_setuid_setgid()) {
450 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
451 "--kconsumerd-err-sock");
452 } else {
453 config_string_set(&the_config.kconsumerd_err_unix_sock_path, strdup(arg));
454 if (!the_config.kconsumerd_err_unix_sock_path.value) {
455 ret = -ENOMEM;
456 PERROR("strdup");
457 }
458 }
459 } else if (string_match(optname, "kconsumerd-cmd-sock")) {
460 if (!arg || *arg == '\0') {
461 ret = -EINVAL;
462 goto end;
463 }
464 if (lttng_is_setuid_setgid()) {
465 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
466 "--kconsumerd-cmd-sock");
467 } else {
468 config_string_set(&the_config.kconsumerd_cmd_unix_sock_path, strdup(arg));
469 if (!the_config.kconsumerd_cmd_unix_sock_path.value) {
470 ret = -ENOMEM;
471 PERROR("strdup");
472 }
473 }
474 } else if (string_match(optname, "ustconsumerd64-err-sock")) {
475 if (!arg || *arg == '\0') {
476 ret = -EINVAL;
477 goto end;
478 }
479 if (lttng_is_setuid_setgid()) {
480 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
481 "--ustconsumerd64-err-sock");
482 } else {
483 config_string_set(&the_config.consumerd64_err_unix_sock_path, strdup(arg));
484 if (!the_config.consumerd64_err_unix_sock_path.value) {
485 ret = -ENOMEM;
486 PERROR("strdup");
487 }
488 }
489 } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
490 if (!arg || *arg == '\0') {
491 ret = -EINVAL;
492 goto end;
493 }
494 if (lttng_is_setuid_setgid()) {
495 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
496 "--ustconsumerd64-cmd-sock");
497 } else {
498 config_string_set(&the_config.consumerd64_cmd_unix_sock_path, strdup(arg));
499 if (!the_config.consumerd64_cmd_unix_sock_path.value) {
500 ret = -ENOMEM;
501 PERROR("strdup");
502 }
503 }
504 } else if (string_match(optname, "ustconsumerd32-err-sock")) {
505 if (!arg || *arg == '\0') {
506 ret = -EINVAL;
507 goto end;
508 }
509 if (lttng_is_setuid_setgid()) {
510 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
511 "--ustconsumerd32-err-sock");
512 } else {
513 config_string_set(&the_config.consumerd32_err_unix_sock_path, strdup(arg));
514 if (!the_config.consumerd32_err_unix_sock_path.value) {
515 ret = -ENOMEM;
516 PERROR("strdup");
517 }
518 }
519 } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
520 if (!arg || *arg == '\0') {
521 ret = -EINVAL;
522 goto end;
523 }
524 if (lttng_is_setuid_setgid()) {
525 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
526 "--ustconsumerd32-cmd-sock");
527 } else {
528 config_string_set(&the_config.consumerd32_cmd_unix_sock_path, strdup(arg));
529 if (!the_config.consumerd32_cmd_unix_sock_path.value) {
530 ret = -ENOMEM;
531 PERROR("strdup");
532 }
533 }
534 } else if (string_match(optname, "no-kernel")) {
535 the_config.no_kernel = true;
536 } else if (string_match(optname, "quiet") || opt == 'q') {
537 the_config.quiet = true;
538 } else if (string_match(optname, "verbose") || opt == 'v') {
539 /* Verbose level can increase using multiple -v */
540 if (arg) {
541 /* Value obtained from config file */
542 the_config.verbose = config_parse_value(arg);
543 } else {
544 /* -v used on command line */
545 the_config.verbose++;
546 }
547 /* Clamp value to [0, 3] */
548 the_config.verbose = the_config.verbose < 0 ?
549 0 :
550 (the_config.verbose <= 3 ? the_config.verbose : 3);
551 } else if (string_match(optname, "verbose-consumer")) {
552 if (arg) {
553 the_config.verbose_consumer = config_parse_value(arg);
554 } else {
555 the_config.verbose_consumer++;
556 }
557 } else if (string_match(optname, "consumerd32-path")) {
558 if (!arg || *arg == '\0') {
559 ret = -EINVAL;
560 goto end;
561 }
562 if (lttng_is_setuid_setgid()) {
563 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
564 "--consumerd32-path");
565 } else {
566 config_string_set(&the_config.consumerd32_bin_path, strdup(arg));
567 if (!the_config.consumerd32_bin_path.value) {
568 PERROR("strdup");
569 ret = -ENOMEM;
570 }
571 }
572 } else if (string_match(optname, "consumerd32-libdir")) {
573 if (!arg || *arg == '\0') {
574 ret = -EINVAL;
575 goto end;
576 }
577 if (lttng_is_setuid_setgid()) {
578 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
579 "--consumerd32-libdir");
580 } else {
581 config_string_set(&the_config.consumerd32_lib_dir, strdup(arg));
582 if (!the_config.consumerd32_lib_dir.value) {
583 PERROR("strdup");
584 ret = -ENOMEM;
585 }
586 }
587 } else if (string_match(optname, "consumerd64-path")) {
588 if (!arg || *arg == '\0') {
589 ret = -EINVAL;
590 goto end;
591 }
592 if (lttng_is_setuid_setgid()) {
593 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
594 "--consumerd64-path");
595 } else {
596 config_string_set(&the_config.consumerd64_bin_path, strdup(arg));
597 if (!the_config.consumerd64_bin_path.value) {
598 PERROR("strdup");
599 ret = -ENOMEM;
600 }
601 }
602 } else if (string_match(optname, "consumerd64-libdir")) {
603 if (!arg || *arg == '\0') {
604 ret = -EINVAL;
605 goto end;
606 }
607 if (lttng_is_setuid_setgid()) {
608 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
609 "--consumerd64-libdir");
610 } else {
611 config_string_set(&the_config.consumerd64_lib_dir, strdup(arg));
612 if (!the_config.consumerd64_lib_dir.value) {
613 PERROR("strdup");
614 ret = -ENOMEM;
615 }
616 }
617 } else if (string_match(optname, "pidfile") || opt == 'p') {
618 if (!arg || *arg == '\0') {
619 ret = -EINVAL;
620 goto end;
621 }
622 if (lttng_is_setuid_setgid()) {
623 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
624 "-p, --pidfile");
625 } else {
626 config_string_set(&the_config.pid_file_path, strdup(arg));
627 if (!the_config.pid_file_path.value) {
628 PERROR("strdup");
629 ret = -ENOMEM;
630 }
631 }
632 } else if (string_match(optname, "agent-tcp-port")) {
633 if (!arg || *arg == '\0') {
634 ret = -EINVAL;
635 goto end;
636 }
637 if (lttng_is_setuid_setgid()) {
638 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
639 "--agent-tcp-port");
640 } else {
641 unsigned long v;
642
643 errno = 0;
644 v = strtoul(arg, nullptr, 0);
645 if (errno != 0 || !isdigit(arg[0])) {
646 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
647 return -1;
648 }
649 if (v == 0 || v >= 65535) {
650 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
651 return -1;
652 }
653 the_config.agent_tcp_port.begin = the_config.agent_tcp_port.end = (int) v;
654 DBG3("Agent TCP port set to non default: %i", (int) v);
655 }
656 } else if (string_match(optname, "load") || opt == 'l') {
657 if (!arg || *arg == '\0') {
658 ret = -EINVAL;
659 goto end;
660 }
661 if (lttng_is_setuid_setgid()) {
662 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
663 "-l, --load");
664 } else {
665 config_string_set(&the_config.load_session_path, strdup(arg));
666 if (!the_config.load_session_path.value) {
667 PERROR("strdup");
668 ret = -ENOMEM;
669 }
670 }
671 } else if (string_match(optname, "kmod-probes")) {
672 if (!arg || *arg == '\0') {
673 ret = -EINVAL;
674 goto end;
675 }
676 if (lttng_is_setuid_setgid()) {
677 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
678 "--kmod-probes");
679 } else {
680 config_string_set(&the_config.kmod_probes_list, strdup(arg));
681 if (!the_config.kmod_probes_list.value) {
682 PERROR("strdup");
683 ret = -ENOMEM;
684 }
685 }
686 } else if (string_match(optname, "extra-kmod-probes")) {
687 if (!arg || *arg == '\0') {
688 ret = -EINVAL;
689 goto end;
690 }
691 if (lttng_is_setuid_setgid()) {
692 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
693 "--extra-kmod-probes");
694 } else {
695 config_string_set(&the_config.kmod_extra_probes_list, strdup(arg));
696 if (!the_config.kmod_extra_probes_list.value) {
697 PERROR("strdup");
698 ret = -ENOMEM;
699 }
700 }
701 } else if (string_match(optname, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR)) {
702 unsigned long v;
703
704 errno = 0;
705 v = strtoul(arg, nullptr, 0);
706 if (errno != 0 || !isdigit(arg[0])) {
707 ERR("Wrong value in --%s parameter: %s",
708 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR,
709 arg);
710 return -1;
711 }
712 if (v == 0 || v >= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX) {
713 ERR("Value out of range for --%s parameter: %s",
714 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR,
715 arg);
716 return -1;
717 }
718 the_config.event_notifier_buffer_size_kernel = (int) v;
719 DBG3("Number of event notifier error buffer kernel size to non default: %i",
720 the_config.event_notifier_buffer_size_kernel);
721 goto end;
722 } else if (string_match(optname, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR)) {
723 unsigned long v;
724
725 errno = 0;
726 v = strtoul(arg, nullptr, 0);
727 if (errno != 0 || !isdigit(arg[0])) {
728 ERR("Wrong value in --%s parameter: %s",
729 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR,
730 arg);
731 return -1;
732 }
733 if (v == 0 || v >= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX) {
734 ERR("Value out of range for --%s parameter: %s",
735 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR,
736 arg);
737 return -1;
738 }
739 the_config.event_notifier_buffer_size_userspace = (int) v;
740 DBG3("Number of event notifier error buffer userspace size to non default: %i",
741 the_config.event_notifier_buffer_size_userspace);
742 goto end;
743 } else if (string_match(optname, "config") || opt == 'f') {
744 /* This is handled in set_options() thus silent skip. */
745 goto end;
746 } else {
747 /* Unknown option or other error.
748 * Error is printed by getopt, just return */
749 ret = -1;
750 }
751
752end:
753 if (ret == -EINVAL) {
754 const char *opt_name = "unknown";
755 int i;
756
757 for (i = 0; i < sizeof(long_options) / sizeof(struct option); i++) {
758 if (opt == long_options[i].val) {
759 opt_name = long_options[i].name;
760 break;
761 }
762 }
763
764 WARN("Invalid argument provided for option \"%s\", using default value.", opt_name);
765 }
766
767 return ret;
768}
769
770/*
771 * config_entry_handler_cb used to handle options read from a config file.
772 * See config_entry_handler_cb comment in common/config/session-config.h for the
773 * return value conventions.
774 */
775static int config_entry_handler(const struct config_entry *entry,
776 void *unused __attribute__((unused)))
777{
778 int ret = 0, i;
779
780 if (!entry || !entry->name || !entry->value) {
781 ret = -EINVAL;
782 goto end;
783 }
784
785 /* Check if the option is to be ignored */
786 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
787 if (!strcmp(entry->name, config_ignore_options[i])) {
788 goto end;
789 }
790 }
791
792 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; i++) {
793 /* Ignore if not fully matched. */
794 if (strcmp(entry->name, long_options[i].name) != 0) {
795 continue;
796 }
797
798 /*
799 * If the option takes no argument on the command line, we have to
800 * check if the value is "true". We support non-zero numeric values,
801 * true, on and yes.
802 */
803 if (!long_options[i].has_arg) {
804 ret = config_parse_value(entry->value);
805 if (ret <= 0) {
806 if (ret) {
807 WARN("Invalid configuration value \"%s\" for option %s",
808 entry->value,
809 entry->name);
810 }
811 /* False, skip boolean config option. */
812 goto end;
813 }
814 }
815
816 ret = set_option(long_options[i].val, entry->value, entry->name);
817 goto end;
818 }
819
820 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
821
822end:
823 return ret;
824}
825
826static void print_version()
827{
828 fprintf(stdout, "%s\n", VERSION);
829}
830
831/*
832 * daemon configuration loading and argument parsing
833 */
834static int set_options(int argc, char **argv)
835{
836 int ret = 0, c = 0, option_index = 0;
837 int orig_optopt = optopt, orig_optind = optind;
838 char *optstring;
839 char *config_path = nullptr;
840
841 optstring = utils_generate_optstring(long_options,
842 sizeof(long_options) / sizeof(struct option));
843 if (!optstring) {
844 ret = -ENOMEM;
845 goto end;
846 }
847
848 /* Check for the --config option */
849 while ((c = getopt_long(argc, argv, optstring, long_options, &option_index)) != -1) {
850 if (c == '?') {
851 ret = -EINVAL;
852 goto end;
853 } else if (c != 'f') {
854 /* if not equal to --config option. */
855 continue;
856 }
857
858 if (lttng_is_setuid_setgid()) {
859 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
860 "-f, --config");
861 } else {
862 free(config_path);
863 config_path = utils_expand_path(optarg);
864 if (!config_path) {
865 ERR("Failed to resolve path: %s", optarg);
866 }
867 }
868 }
869
870 ret = config_get_section_entries(
871 config_path, config_section_name, config_entry_handler, nullptr);
872 if (ret) {
873 if (ret > 0) {
874 ERR("Invalid configuration option at line %i", ret);
875 ret = -1;
876 }
877 goto end;
878 }
879
880 /* Reset getopt's global state */
881 optopt = orig_optopt;
882 optind = orig_optind;
883 while (true) {
884 option_index = -1;
885 /*
886 * getopt_long() will not set option_index if it encounters a
887 * short option.
888 */
889 c = getopt_long(argc, argv, optstring, long_options, &option_index);
890 if (c == -1) {
891 break;
892 }
893
894 /*
895 * Pass NULL as the long option name if popt left the index
896 * unset.
897 */
898 ret = set_option(
899 c, optarg, option_index < 0 ? nullptr : long_options[option_index].name);
900 if (ret < 0) {
901 break;
902 }
903 }
904
905end:
906 free(config_path);
907 free(optstring);
908 return ret;
909}
910
911/*
912 * Create lockfile using the rundir and return its fd.
913 */
914static int create_lockfile()
915{
916 return utils_create_lock_file(the_config.lock_file_path.value);
917}
918
919/*
920 * Check if the global socket is available, and if a daemon is answering at the
921 * other side. If yes, error is returned.
922 *
923 * Also attempts to create and hold the lock file.
924 */
925static int check_existing_daemon()
926{
927 int ret = 0;
928
929 /* Is there anybody out there ? */
930 if (lttng_session_daemon_alive()) {
931 ret = -EEXIST;
932 goto end;
933 }
934
935 lockfile_fd = create_lockfile();
936 if (lockfile_fd < 0) {
937 ret = -EEXIST;
938 goto end;
939 }
940end:
941 return ret;
942}
943
944static void sessiond_cleanup_lock_file()
945{
946 int ret;
947
948 /*
949 * Cleanup lock file by deleting it and finaly closing it which will
950 * release the file system lock.
951 */
952 if (lockfile_fd >= 0) {
953 ret = remove(the_config.lock_file_path.value);
954 if (ret < 0) {
955 PERROR("remove lock file");
956 }
957 ret = close(lockfile_fd);
958 if (ret < 0) {
959 PERROR("close lock file");
960 }
961 }
962}
963
964/*
965 * Set the tracing group gid onto the client socket.
966 *
967 * Race window between mkdir and chown is OK because we are going from more
968 * permissive (root.root) to less permissive (root.tracing).
969 */
970static int set_permissions(char *rundir)
971{
972 int ret;
973 gid_t gid;
974
975 ret = utils_get_group_id(the_config.tracing_group_name.value, true, &gid);
976 if (ret) {
977 /* Default to root group. */
978 gid = 0;
979 }
980
981 /* Set lttng run dir */
982 ret = chown(rundir, 0, gid);
983 if (ret < 0) {
984 ERR("Unable to set group on %s", rundir);
985 PERROR("chown");
986 }
987
988 /*
989 * Ensure all applications and tracing group can search the run
990 * dir. Allow everyone to read the directory, since it does not
991 * buy us anything to hide its content.
992 */
993 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
994 if (ret < 0) {
995 ERR("Unable to set permissions on %s", rundir);
996 PERROR("chmod");
997 }
998
999 /* lttng client socket path */
1000 ret = chown(the_config.client_unix_sock_path.value, 0, gid);
1001 if (ret < 0) {
1002 ERR("Unable to set group on %s", the_config.client_unix_sock_path.value);
1003 PERROR("chown");
1004 }
1005
1006 /* kconsumer error socket path */
1007 ret = chown(the_kconsumer_data.err_unix_sock_path, 0, 0);
1008 if (ret < 0) {
1009 ERR("Unable to set group on %s", the_kconsumer_data.err_unix_sock_path);
1010 PERROR("chown");
1011 }
1012
1013 /* 64-bit ustconsumer error socket path */
1014 ret = chown(the_ustconsumer64_data.err_unix_sock_path, 0, 0);
1015 if (ret < 0) {
1016 ERR("Unable to set group on %s", the_ustconsumer64_data.err_unix_sock_path);
1017 PERROR("chown");
1018 }
1019
1020 /* 32-bit ustconsumer compat32 error socket path */
1021 ret = chown(the_ustconsumer32_data.err_unix_sock_path, 0, 0);
1022 if (ret < 0) {
1023 ERR("Unable to set group on %s", the_ustconsumer32_data.err_unix_sock_path);
1024 PERROR("chown");
1025 }
1026
1027 DBG("All permissions are set");
1028
1029 return ret;
1030}
1031
1032/*
1033 * Create the lttng run directory needed for all global sockets and pipe.
1034 */
1035static int create_lttng_rundir()
1036{
1037 int ret;
1038
1039 DBG3("Creating LTTng run directory: %s", the_config.rundir.value);
1040
1041 ret = mkdir(the_config.rundir.value, S_IRWXU);
1042 if (ret < 0) {
1043 if (errno != EEXIST) {
1044 ERR("Unable to create %s", the_config.rundir.value);
1045 goto error;
1046 } else {
1047 ret = 0;
1048 }
1049 }
1050
1051error:
1052 return ret;
1053}
1054
1055/*
1056 * Setup sockets and directory needed by the consumerds' communication with the
1057 * session daemon.
1058 */
1059static int set_consumer_sockets(struct consumer_data *consumer_data)
1060{
1061 int ret;
1062 char *path = nullptr;
1063
1064 switch (consumer_data->type) {
1065 case LTTNG_CONSUMER_KERNEL:
1066 path = the_config.kconsumerd_path.value;
1067 break;
1068 case LTTNG_CONSUMER64_UST:
1069 path = the_config.consumerd64_path.value;
1070 break;
1071 case LTTNG_CONSUMER32_UST:
1072 path = the_config.consumerd32_path.value;
1073 break;
1074 default:
1075 ERR("Consumer type unknown");
1076 ret = -EINVAL;
1077 goto error;
1078 }
1079 LTTNG_ASSERT(path);
1080
1081 DBG2("Creating consumer directory: %s", path);
1082
1083 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
1084 if (ret < 0 && errno != EEXIST) {
1085 PERROR("mkdir");
1086 ERR("Failed to create %s", path);
1087 goto error;
1088 }
1089 if (is_root) {
1090 gid_t gid;
1091
1092 ret = utils_get_group_id(the_config.tracing_group_name.value, true, &gid);
1093 if (ret) {
1094 /* Default to root group. */
1095 gid = 0;
1096 }
1097
1098 ret = chown(path, 0, gid);
1099 if (ret < 0) {
1100 ERR("Unable to set group on %s", path);
1101 PERROR("chown");
1102 goto error;
1103 }
1104 }
1105
1106 /* Create the consumerd error unix socket */
1107 consumer_data->err_sock = lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
1108 if (consumer_data->err_sock < 0) {
1109 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
1110 ret = -1;
1111 goto error;
1112 }
1113
1114 /*
1115 * Set the CLOEXEC flag. Return code is useless because either way, the
1116 * show must go on.
1117 */
1118 ret = utils_set_fd_cloexec(consumer_data->err_sock);
1119 if (ret < 0) {
1120 PERROR("utils_set_fd_cloexec");
1121 /* continue anyway */
1122 }
1123
1124 /* File permission MUST be 660 */
1125 ret = chmod(consumer_data->err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1126 if (ret < 0) {
1127 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
1128 PERROR("chmod");
1129 goto error;
1130 }
1131
1132error:
1133 return ret;
1134}
1135
1136/*
1137 * Signal handler for the daemon
1138 *
1139 * Simply stop all worker threads, leaving main() return gracefully after
1140 * joining all threads and calling cleanup().
1141 */
1142static void sighandler(int sig, siginfo_t *siginfo, void *arg __attribute__((unused)))
1143{
1144 switch (sig) {
1145 case SIGINT:
1146 DBG("SIGINT caught");
1147 notify_main_quit_pipe();
1148 break;
1149 case SIGTERM:
1150 DBG("SIGTERM caught");
1151 notify_main_quit_pipe();
1152 break;
1153 case SIGUSR1:
1154 CMM_STORE_SHARED(recv_child_signal, 1);
1155 break;
1156 case SIGBUS:
1157 {
1158 int write_ret;
1159 const char msg[] = "Received SIGBUS, aborting program.\n";
1160
1161 lttng_ust_handle_sigbus(siginfo->si_addr);
1162 /*
1163 * If ustctl did not catch this signal (triggering a
1164 * siglongjmp), abort the program. Otherwise, the execution
1165 * will resume from the ust-ctl call which caused this error.
1166 *
1167 * The return value is ignored since the program aborts anyhow.
1168 */
1169 write_ret = write(STDERR_FILENO, msg, sizeof(msg));
1170 (void) write_ret;
1171 abort();
1172 }
1173 default:
1174 break;
1175 }
1176}
1177
1178/*
1179 * Setup signal handler for :
1180 * SIGINT, SIGTERM, SIGPIPE
1181 */
1182static int set_signal_handler()
1183{
1184 int ret = 0;
1185 struct sigaction sa;
1186 sigset_t sigset;
1187
1188 if ((ret = sigemptyset(&sigset)) < 0) {
1189 PERROR("sigemptyset");
1190 return ret;
1191 }
1192
1193 sa.sa_mask = sigset;
1194 sa.sa_flags = SA_SIGINFO;
1195
1196 sa.sa_sigaction = sighandler;
1197 if ((ret = sigaction(SIGTERM, &sa, nullptr)) < 0) {
1198 PERROR("sigaction");
1199 return ret;
1200 }
1201
1202 if ((ret = sigaction(SIGINT, &sa, nullptr)) < 0) {
1203 PERROR("sigaction");
1204 return ret;
1205 }
1206
1207 if ((ret = sigaction(SIGUSR1, &sa, nullptr)) < 0) {
1208 PERROR("sigaction");
1209 return ret;
1210 }
1211
1212 if ((ret = sigaction(SIGBUS, &sa, nullptr)) < 0) {
1213 PERROR("sigaction");
1214 return ret;
1215 }
1216
1217 sa.sa_flags = 0;
1218 sa.sa_handler = SIG_IGN;
1219 if ((ret = sigaction(SIGPIPE, &sa, nullptr)) < 0) {
1220 PERROR("sigaction");
1221 return ret;
1222 }
1223
1224 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS");
1225
1226 return ret;
1227}
1228
1229/*
1230 * Set open files limit to unlimited. This daemon can open a large number of
1231 * file descriptors in order to consume multiple kernel traces.
1232 */
1233static void set_ulimit()
1234{
1235 int ret;
1236 struct rlimit lim;
1237
1238 /* The kernel does not allow an infinite limit for open files */
1239 lim.rlim_cur = 65535;
1240 lim.rlim_max = 65535;
1241
1242 ret = setrlimit(RLIMIT_NOFILE, &lim);
1243 if (ret < 0) {
1244 PERROR("failed to set open files limit");
1245 }
1246}
1247
1248static int write_pidfile()
1249{
1250 return utils_create_pid_file(getpid(), the_config.pid_file_path.value);
1251}
1252
1253static int set_clock_plugin_env()
1254{
1255 int ret = 0;
1256 char *env_value = nullptr;
1257
1258 if (!the_config.lttng_ust_clock_plugin.value) {
1259 goto end;
1260 }
1261
1262 ret = asprintf(
1263 &env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", the_config.lttng_ust_clock_plugin.value);
1264 if (ret < 0) {
1265 PERROR("asprintf");
1266 goto end;
1267 }
1268
1269 ret = putenv(env_value);
1270 if (ret) {
1271 free(env_value);
1272 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1273 goto end;
1274 }
1275
1276 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1277 the_config.lttng_ust_clock_plugin.value);
1278end:
1279 return ret;
1280}
1281
1282static void destroy_all_sessions_and_wait()
1283{
1284 struct ltt_session *session, *tmp;
1285 struct ltt_session_list *session_list;
1286
1287 session_list = session_get_list();
1288 DBG("Initiating destruction of all sessions");
1289
1290 if (!session_list) {
1291 return;
1292 }
1293
1294 session_lock_list();
1295 /* Initiate the destruction of all sessions. */
1296 cds_list_for_each_entry_safe (session, tmp, &session_list->head, list) {
1297 if (!session_get(session)) {
1298 continue;
1299 }
1300
1301 session_lock(session);
1302 if (session->destroyed) {
1303 goto unlock_session;
1304 }
1305 (void) cmd_stop_trace(session);
1306 (void) cmd_destroy_session(session, the_notification_thread_handle, nullptr);
1307 unlock_session:
1308 session_unlock(session);
1309 session_put(session);
1310 }
1311 session_unlock_list();
1312
1313 /* Wait for the destruction of all sessions to complete. */
1314 DBG("Waiting for the destruction of all sessions to complete");
1315 session_list_wait_empty();
1316 DBG("Destruction of all sessions completed");
1317}
1318
1319static void unregister_all_triggers()
1320{
1321 enum lttng_error_code ret_code;
1322 enum lttng_trigger_status trigger_status;
1323 struct lttng_triggers *triggers = nullptr;
1324 unsigned int trigger_count, i;
1325 const struct lttng_credentials creds = {
1326 .uid = LTTNG_OPTIONAL_INIT_VALUE(0),
1327 .gid = LTTNG_OPTIONAL_INIT_UNSET,
1328 };
1329
1330 DBG("Unregistering all triggers");
1331
1332 /*
1333 * List all triggers as "root" since we wish to unregister all triggers.
1334 */
1335 ret_code = notification_thread_command_list_triggers(
1336 the_notification_thread_handle, creds.uid.value, &triggers);
1337 if (ret_code != LTTNG_OK) {
1338 ERR("Failed to list triggers while unregistering all triggers");
1339 goto end;
1340 }
1341
1342 trigger_status = lttng_triggers_get_count(triggers, &trigger_count);
1343 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1344
1345 for (i = 0; i < trigger_count; i++) {
1346 uid_t trigger_owner;
1347 const char *trigger_name;
1348 const struct lttng_trigger *trigger = lttng_triggers_get_at_index(triggers, i);
1349
1350 LTTNG_ASSERT(trigger);
1351
1352 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
1353 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1354
1355 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
1356 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name :
1357 "(anonymous)";
1358
1359 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1360 (int) trigger_owner,
1361 trigger_name);
1362
1363 ret_code = cmd_unregister_trigger(&creds, trigger, the_notification_thread_handle);
1364 if (ret_code != LTTNG_OK) {
1365 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1366 (int) trigger_owner,
1367 trigger_name,
1368 lttng_strerror(-ret_code));
1369 /* Continue to unregister the remaining triggers. */
1370 }
1371 }
1372end:
1373 lttng_triggers_destroy(triggers);
1374}
1375
1376static int run_as_worker_post_fork_cleanup(void *data)
1377{
1378 struct sessiond_config *sessiond_config = (struct sessiond_config *) data;
1379
1380 sessiond_config_fini(sessiond_config);
1381 return 0;
1382}
1383
1384static int launch_run_as_worker(const char *procname)
1385{
1386 /*
1387 * Clean-up before forking the run-as worker. Any dynamically
1388 * allocated memory of which the worker is not aware will
1389 * be leaked as the process forks a run-as worker (and performs
1390 * no exec*()). The same would apply to any opened fd.
1391 */
1392 return run_as_create_worker(procname, run_as_worker_post_fork_cleanup, &the_config);
1393}
1394
1395static void sessiond_uuid_log()
1396{
1397 char uuid_str[LTTNG_UUID_STR_LEN];
1398
1399 lttng_uuid_to_str(the_sessiond_uuid, uuid_str);
1400 DBG("Starting lttng-sessiond {%s}", uuid_str);
1401}
1402
1403/*
1404 * main
1405 */
1406int main(int argc, char **argv)
1407{
1408 int ret = 0, retval = 0;
1409 const char *env_app_timeout;
1410 struct lttng_pipe *ust32_channel_monitor_pipe = nullptr,
1411 *ust64_channel_monitor_pipe = nullptr,
1412 *kernel_channel_monitor_pipe = nullptr;
1413 struct timer_thread_parameters timer_thread_parameters;
1414 /* Rotation thread handle. */
1415 struct rotation_thread_handle *rotation_thread_handle = nullptr;
1416 /* Queue of rotation jobs populated by the sessiond-timer. */
1417 struct rotation_thread_timer_queue *rotation_timer_queue = nullptr;
1418 struct lttng_thread *client_thread = nullptr;
1419 struct lttng_thread *notification_thread = nullptr;
1420 struct lttng_thread *register_apps_thread = nullptr;
1421 enum event_notifier_error_accounting_status event_notifier_error_accounting_status;
1422
1423 logger_set_thread_name("Main", false);
1424 init_kernel_workarounds();
1425
1426 rcu_register_thread();
1427
1428 if (set_signal_handler()) {
1429 retval = -1;
1430 goto exit_set_signal_handler;
1431 }
1432
1433 if (timer_signal_init()) {
1434 retval = -1;
1435 goto exit_set_signal_handler;
1436 }
1437
1438 the_page_size = sysconf(_SC_PAGE_SIZE);
1439 if (the_page_size < 0) {
1440 PERROR("sysconf _SC_PAGE_SIZE");
1441 the_page_size = LONG_MAX;
1442 WARN("Fallback page size to %ld", the_page_size);
1443 }
1444
1445 ret = sessiond_config_init(&the_config);
1446 if (ret) {
1447 retval = -1;
1448 goto exit_set_signal_handler;
1449 }
1450
1451 /*
1452 * Init config from environment variables.
1453 * Command line option override env configuration per-doc. Do env first.
1454 */
1455 sessiond_config_apply_env_config(&the_config);
1456
1457 /*
1458 * Parse arguments and load the daemon configuration file.
1459 *
1460 * We have an exit_options exit path to free memory reserved by
1461 * set_options.
1462 */
1463 progname = argv[0];
1464 if (set_options(argc, argv)) {
1465 retval = -1;
1466 goto exit_options;
1467 }
1468
1469 /*
1470 * Resolve all paths received as arguments, configuration option, or
1471 * through environment variable as absolute paths. This is necessary
1472 * since daemonizing causes the sessiond's current working directory
1473 * to '/'.
1474 */
1475 ret = sessiond_config_resolve_paths(&the_config);
1476 if (ret) {
1477 goto exit_options;
1478 }
1479
1480 /* Apply config. */
1481 lttng_opt_verbose = the_config.verbose;
1482 lttng_opt_quiet = the_config.quiet;
1483 the_kconsumer_data.err_unix_sock_path = the_config.kconsumerd_err_unix_sock_path.value;
1484 the_kconsumer_data.cmd_unix_sock_path = the_config.kconsumerd_cmd_unix_sock_path.value;
1485 the_ustconsumer32_data.err_unix_sock_path = the_config.consumerd32_err_unix_sock_path.value;
1486 the_ustconsumer32_data.cmd_unix_sock_path = the_config.consumerd32_cmd_unix_sock_path.value;
1487 the_ustconsumer64_data.err_unix_sock_path = the_config.consumerd64_err_unix_sock_path.value;
1488 the_ustconsumer64_data.cmd_unix_sock_path = the_config.consumerd64_cmd_unix_sock_path.value;
1489 set_clock_plugin_env();
1490
1491 sessiond_config_log(&the_config);
1492 sessiond_uuid_log();
1493 lttng::logging::log_system_information(PRINT_DBG);
1494
1495 if (opt_print_version) {
1496 print_version();
1497 retval = 0;
1498 goto exit_options;
1499 }
1500
1501 if (create_lttng_rundir()) {
1502 retval = -1;
1503 goto exit_options;
1504 }
1505
1506 /* Abort launch if a session daemon is already running. */
1507 if (check_existing_daemon()) {
1508 ERR("A session daemon is already running.");
1509 retval = -1;
1510 goto exit_options;
1511 }
1512
1513 /* Daemonize */
1514 if (the_config.daemonize || the_config.background) {
1515 int i;
1516
1517 ret = lttng_daemonize(&the_child_ppid, &recv_child_signal, !the_config.background);
1518 if (ret < 0) {
1519 retval = -1;
1520 goto exit_options;
1521 }
1522
1523 /*
1524 * We are in the child. Make sure all other file descriptors are
1525 * closed, in case we are called with more opened file
1526 * descriptors than the standard ones and the lock file.
1527 */
1528 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1529 if (i == lockfile_fd) {
1530 continue;
1531 }
1532 (void) close(i);
1533 }
1534 }
1535
1536 if (launch_run_as_worker(argv[0]) < 0) {
1537 goto exit_create_run_as_worker_cleanup;
1538 }
1539
1540 /*
1541 * Starting from here, we can create threads. This needs to be after
1542 * lttng_daemonize due to RCU.
1543 */
1544
1545 /*
1546 * Initialize the health check subsystem. This call should set the
1547 * appropriate time values.
1548 */
1549 the_health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
1550 if (!the_health_sessiond) {
1551 PERROR("health_app_create error");
1552 retval = -1;
1553 goto stop_threads;
1554 }
1555
1556 /* Create main quit pipe */
1557 if (sessiond_init_main_quit_pipe()) {
1558 retval = -1;
1559 goto stop_threads;
1560 }
1561
1562 /* Check if daemon is UID = 0 */
1563 is_root = !getuid();
1564 if (is_root) {
1565 /* Create global run dir with root access */
1566
1567 kernel_channel_monitor_pipe = lttng_pipe_open(0);
1568 if (!kernel_channel_monitor_pipe) {
1569 ERR("Failed to create kernel consumer channel monitor pipe");
1570 retval = -1;
1571 goto stop_threads;
1572 }
1573 the_kconsumer_data.channel_monitor_pipe =
1574 lttng_pipe_release_writefd(kernel_channel_monitor_pipe);
1575 if (the_kconsumer_data.channel_monitor_pipe < 0) {
1576 retval = -1;
1577 goto stop_threads;
1578 }
1579 }
1580
1581 /* Set consumer initial state */
1582 the_kernel_consumerd_state = CONSUMER_STOPPED;
1583 the_ust_consumerd_state = CONSUMER_STOPPED;
1584
1585 ust32_channel_monitor_pipe = lttng_pipe_open(0);
1586 if (!ust32_channel_monitor_pipe) {
1587 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1588 retval = -1;
1589 goto stop_threads;
1590 }
1591 the_ustconsumer32_data.channel_monitor_pipe =
1592 lttng_pipe_release_writefd(ust32_channel_monitor_pipe);
1593 if (the_ustconsumer32_data.channel_monitor_pipe < 0) {
1594 retval = -1;
1595 goto stop_threads;
1596 }
1597
1598 /*
1599 * The rotation_thread_timer_queue structure is shared between the
1600 * sessiond timer thread and the rotation thread. The main thread keeps
1601 * its ownership and destroys it when both threads have been joined.
1602 */
1603 rotation_timer_queue = rotation_thread_timer_queue_create();
1604 if (!rotation_timer_queue) {
1605 retval = -1;
1606 goto stop_threads;
1607 }
1608 timer_thread_parameters.rotation_thread_job_queue = rotation_timer_queue;
1609
1610 ust64_channel_monitor_pipe = lttng_pipe_open(0);
1611 if (!ust64_channel_monitor_pipe) {
1612 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1613 retval = -1;
1614 goto stop_threads;
1615 }
1616 the_ustconsumer64_data.channel_monitor_pipe =
1617 lttng_pipe_release_writefd(ust64_channel_monitor_pipe);
1618 if (the_ustconsumer64_data.channel_monitor_pipe < 0) {
1619 retval = -1;
1620 goto stop_threads;
1621 }
1622
1623 /*
1624 * Init UST app hash table. Alloc hash table before this point since
1625 * cleanup() can get called after that point.
1626 */
1627 if (ust_app_ht_alloc()) {
1628 ERR("Failed to allocate UST app hash table");
1629 retval = -1;
1630 goto stop_threads;
1631 }
1632
1633 event_notifier_error_accounting_status = event_notifier_error_accounting_init(
1634 the_config.event_notifier_buffer_size_kernel,
1635 the_config.event_notifier_buffer_size_userspace);
1636 if (event_notifier_error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
1637 ERR("Failed to initialize event notifier error accounting system");
1638 retval = -1;
1639 goto stop_threads;
1640 }
1641
1642 /*
1643 * Initialize agent app hash table. We allocate the hash table here
1644 * since cleanup() can get called after this point.
1645 */
1646 if (agent_app_ht_alloc()) {
1647 ERR("Failed to allocate Agent app hash table");
1648 retval = -1;
1649 goto stop_threads;
1650 }
1651
1652 if (agent_by_event_notifier_domain_ht_create()) {
1653 ERR("Failed to allocate per-event notifier domain agent hash table");
1654 retval = -1;
1655 goto stop_threads;
1656 }
1657 /*
1658 * These actions must be executed as root. We do that *after* setting up
1659 * the sockets path because we MUST make the check for another daemon using
1660 * those paths *before* trying to set the kernel consumer sockets and init
1661 * kernel tracer.
1662 */
1663 if (is_root) {
1664 if (set_consumer_sockets(&the_kconsumer_data)) {
1665 retval = -1;
1666 goto stop_threads;
1667 }
1668
1669 /* Setup kernel tracer */
1670 if (!the_config.no_kernel) {
1671 init_kernel_tracer();
1672 }
1673
1674 /* Set ulimit for open files */
1675 set_ulimit();
1676 }
1677 /* init lttng_fd tracking must be done after set_ulimit. */
1678 lttng_fd_init();
1679
1680 if (set_consumer_sockets(&the_ustconsumer64_data)) {
1681 retval = -1;
1682 goto stop_threads;
1683 }
1684
1685 if (set_consumer_sockets(&the_ustconsumer32_data)) {
1686 retval = -1;
1687 goto stop_threads;
1688 }
1689
1690 /* Get parent pid if -S, --sig-parent is specified. */
1691 if (the_config.sig_parent) {
1692 the_ppid = getppid();
1693 }
1694
1695 /* Setup the kernel pipe for waking up the kernel thread */
1696 if (is_root && !the_config.no_kernel) {
1697 if (utils_create_pipe_cloexec(the_kernel_poll_pipe)) {
1698 retval = -1;
1699 goto stop_threads;
1700 }
1701 }
1702
1703 /* Setup the thread apps communication pipe. */
1704 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
1705 retval = -1;
1706 goto stop_threads;
1707 }
1708
1709 /* Setup the thread apps notify communication pipe. */
1710 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
1711 retval = -1;
1712 goto stop_threads;
1713 }
1714
1715 /* Initialize global buffer per UID and PID registry. */
1716 buffer_reg_init_uid_registry();
1717 buffer_reg_init_pid_registry();
1718
1719 /* Init UST command queue. */
1720 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1721
1722 cmd_init();
1723
1724 /* Check for the application socket timeout env variable. */
1725 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
1726 if (env_app_timeout) {
1727 the_config.app_socket_timeout = atoi(env_app_timeout);
1728 } else {
1729 the_config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
1730 }
1731
1732 ret = write_pidfile();
1733 if (ret) {
1734 ERR("Error in write_pidfile");
1735 retval = -1;
1736 goto stop_threads;
1737 }
1738
1739 /* Initialize communication library */
1740 lttcomm_init();
1741 /* Initialize TCP timeout values */
1742 lttcomm_inet_init();
1743
1744 /* Create health-check thread. */
1745 if (!launch_health_management_thread()) {
1746 retval = -1;
1747 goto stop_threads;
1748 }
1749
1750 /* notification_thread_data acquires the pipes' read side. */
1751 the_notification_thread_handle =
1752 notification_thread_handle_create(ust32_channel_monitor_pipe,
1753 ust64_channel_monitor_pipe,
1754 kernel_channel_monitor_pipe);
1755 if (!the_notification_thread_handle) {
1756 retval = -1;
1757 ERR("Failed to create notification thread shared data");
1758 goto stop_threads;
1759 }
1760
1761 /* Create notification thread. */
1762 notification_thread = launch_notification_thread(the_notification_thread_handle);
1763 if (!notification_thread) {
1764 retval = -1;
1765 goto stop_threads;
1766 }
1767
1768 /* Create timer thread. */
1769 if (!launch_timer_thread(&timer_thread_parameters)) {
1770 retval = -1;
1771 goto stop_threads;
1772 }
1773
1774 /* rotation_thread_data acquires the pipes' read side. */
1775 rotation_thread_handle =
1776 rotation_thread_handle_create(rotation_timer_queue, the_notification_thread_handle);
1777 if (!rotation_thread_handle) {
1778 retval = -1;
1779 ERR("Failed to create rotation thread shared data");
1780 goto stop_threads;
1781 }
1782
1783 /* Create rotation thread. */
1784 if (!launch_rotation_thread(rotation_thread_handle)) {
1785 retval = -1;
1786 goto stop_threads;
1787 }
1788
1789 /* Create thread to manage the client socket */
1790 client_thread = launch_client_thread();
1791 if (!client_thread) {
1792 retval = -1;
1793 goto stop_threads;
1794 }
1795
1796 /* Set credentials of the client socket and rundir */
1797 if (is_root && set_permissions(the_config.rundir.value)) {
1798 retval = -1;
1799 goto stop_threads;
1800 }
1801
1802 if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1], apps_cmd_notify_pipe[1])) {
1803 retval = -1;
1804 goto stop_threads;
1805 }
1806
1807 /* Create thread to manage application registration. */
1808 register_apps_thread = launch_application_registration_thread(&ust_cmd_queue);
1809 if (!register_apps_thread) {
1810 retval = -1;
1811 goto stop_threads;
1812 }
1813
1814 /* Create thread to manage application socket */
1815 if (!launch_application_management_thread(apps_cmd_pipe[0])) {
1816 retval = -1;
1817 goto stop_threads;
1818 }
1819
1820 /* Create thread to manage application notify socket */
1821 if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
1822 retval = -1;
1823 goto stop_threads;
1824 }
1825
1826 /* Create agent management thread. */
1827 if (!launch_agent_management_thread()) {
1828 retval = -1;
1829 goto stop_threads;
1830 }
1831
1832 /* Don't start this thread if kernel tracing is not requested nor root */
1833 if (is_root && !the_config.no_kernel) {
1834 /* Create kernel thread to manage kernel event */
1835 if (!launch_kernel_management_thread(the_kernel_poll_pipe[0])) {
1836 retval = -1;
1837 goto stop_threads;
1838 }
1839
1840 if (kernel_get_notification_fd() >= 0) {
1841 ret = notification_thread_command_add_tracer_event_source(
1842 the_notification_thread_handle,
1843 kernel_get_notification_fd(),
1844 LTTNG_DOMAIN_KERNEL);
1845 if (ret != LTTNG_OK) {
1846 ERR("Failed to add kernel trigger event source to notification thread");
1847 retval = -1;
1848 goto stop_threads;
1849 }
1850 }
1851 }
1852
1853 /* Load sessions. */
1854 ret = config_load_session(the_config.load_session_path.value, nullptr, 1, 1, nullptr);
1855 if (ret) {
1856 ERR("Session load failed: %s", error_get_str(ret));
1857 retval = -1;
1858 goto stop_threads;
1859 }
1860
1861 /* Initialization completed. */
1862 sessiond_signal_parents();
1863
1864 /*
1865 * This is where we start awaiting program completion (e.g. through
1866 * signal that asks threads to teardown).
1867 */
1868
1869 /* Initiate teardown once activity occurs on the main quit pipe. */
1870 sessiond_wait_for_main_quit_pipe(-1);
1871
1872stop_threads:
1873
1874 DBG("Terminating all threads");
1875
1876 /*
1877 * Ensure that the client thread is no longer accepting new commands,
1878 * which could cause new sessions to be created.
1879 */
1880 if (client_thread) {
1881 lttng_thread_shutdown(client_thread);
1882 lttng_thread_put(client_thread);
1883 }
1884
1885 destroy_all_sessions_and_wait();
1886
1887 /*
1888 * At this point no new trigger can be registered (no sessions are
1889 * running/rotating) and clients can't connect to the session daemon
1890 * anymore. Unregister all triggers.
1891 */
1892 unregister_all_triggers();
1893
1894 if (register_apps_thread) {
1895 lttng_thread_shutdown(register_apps_thread);
1896 lttng_thread_put(register_apps_thread);
1897 }
1898 lttng_thread_list_shutdown_orphans();
1899
1900 /*
1901 * Wait for all pending call_rcu work to complete before tearing
1902 * down data structures. call_rcu worker may be trying to
1903 * perform lookups in those structures.
1904 */
1905 rcu_barrier();
1906
1907 rcu_thread_online();
1908 sessiond_cleanup();
1909
1910 /*
1911 * Wait for all pending call_rcu work to complete before shutting down
1912 * the notification thread. This call_rcu work includes shutting down
1913 * UST apps and event notifier pipes.
1914 */
1915 rcu_barrier();
1916
1917 if (notification_thread) {
1918 lttng_thread_shutdown(notification_thread);
1919 lttng_thread_put(notification_thread);
1920 }
1921
1922 /*
1923 * Error accounting teardown has to be done after the teardown of all
1924 * event notifier pipes to ensure that no tracer may try to use the
1925 * error accounting facilities.
1926 */
1927 event_notifier_error_accounting_fini();
1928
1929 /*
1930 * Unloading the kernel modules needs to be done after all kernel
1931 * ressources have been released. In our case, this includes the
1932 * notification fd, the event notifier group fd, error accounting fd,
1933 * all event and event notifier fds, etc.
1934 *
1935 * In short, at this point, we need to have called close() on all fds
1936 * received from the kernel tracer.
1937 */
1938 if (is_root && !the_config.no_kernel) {
1939 DBG("Unloading kernel modules");
1940 modprobe_remove_lttng_all();
1941 }
1942
1943 rcu_thread_offline();
1944 rcu_unregister_thread();
1945
1946 if (rotation_thread_handle) {
1947 rotation_thread_handle_destroy(rotation_thread_handle);
1948 }
1949
1950 /*
1951 * After the rotation and timer thread have quit, we can safely destroy
1952 * the rotation_timer_queue.
1953 */
1954 rotation_thread_timer_queue_destroy(rotation_timer_queue);
1955 /*
1956 * The teardown of the notification system is performed after the
1957 * session daemon's teardown in order to allow it to be notified
1958 * of the active session and channels at the moment of the teardown.
1959 */
1960 if (the_notification_thread_handle) {
1961 notification_thread_handle_destroy(the_notification_thread_handle);
1962 }
1963 lttng_pipe_destroy(ust32_channel_monitor_pipe);
1964 lttng_pipe_destroy(ust64_channel_monitor_pipe);
1965 lttng_pipe_destroy(kernel_channel_monitor_pipe);
1966
1967 if (the_health_sessiond) {
1968 health_app_destroy(the_health_sessiond);
1969 }
1970exit_create_run_as_worker_cleanup:
1971exit_options:
1972 sessiond_cleanup_lock_file();
1973 sessiond_cleanup_options();
1974
1975exit_set_signal_handler:
1976 if (!retval) {
1977 exit(EXIT_SUCCESS);
1978 } else {
1979 exit(EXIT_FAILURE);
1980 }
1981}
This page took 0.039869 seconds and 4 git commands to generate.