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