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