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