sessiond: notification: receive incoming notifications from tracers
[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 void unregister_all_triggers(void)
1258 {
1259 enum lttng_error_code ret_code;
1260 enum lttng_trigger_status trigger_status;
1261 struct lttng_triggers *triggers = NULL;
1262 unsigned int trigger_count, i;
1263 const struct lttng_credentials creds = {
1264 .uid = LTTNG_OPTIONAL_INIT_VALUE(0),
1265 };
1266
1267 DBG("Unregistering all triggers");
1268
1269 /*
1270 * List all triggers as "root" since we wish to unregister all triggers.
1271 */
1272 ret_code = notification_thread_command_list_triggers(
1273 notification_thread_handle, creds.uid.value, &triggers);
1274 if (ret_code != LTTNG_OK) {
1275 ERR("Failed to list triggers while unregistering all triggers");
1276 goto end;
1277 }
1278
1279 trigger_status = lttng_triggers_get_count(triggers, &trigger_count);
1280 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1281
1282 for (i = 0; i < trigger_count; i++) {
1283 enum lttng_error_code ret_code;
1284 uid_t trigger_owner;
1285 const char *trigger_name;
1286 const struct lttng_trigger *trigger =
1287 lttng_triggers_get_at_index(triggers, i);
1288
1289 assert(trigger);
1290
1291 trigger_status = lttng_trigger_get_owner_uid(
1292 trigger, &trigger_owner);
1293 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1294
1295 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
1296 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1297
1298 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1299 (int) trigger_owner, trigger_name);
1300
1301 ret_code = cmd_unregister_trigger(
1302 &creds, trigger, notification_thread_handle);
1303 if (ret_code != LTTNG_OK) {
1304 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1305 (int) trigger_owner, trigger_name,
1306 lttng_strerror(-ret_code));
1307 /* Continue to unregister the remaining triggers. */
1308 }
1309 }
1310 end:
1311 lttng_triggers_destroy(triggers);
1312 }
1313
1314 static int run_as_worker_post_fork_cleanup(void *data)
1315 {
1316 struct sessiond_config *sessiond_config = data;
1317
1318 sessiond_config_fini(sessiond_config);
1319 return 0;
1320 }
1321
1322 static int launch_run_as_worker(const char *procname)
1323 {
1324 /*
1325 * Clean-up before forking the run-as worker. Any dynamically
1326 * allocated memory of which the worker is not aware will
1327 * be leaked as the process forks a run-as worker (and performs
1328 * no exec*()). The same would apply to any opened fd.
1329 */
1330 return run_as_create_worker(procname, run_as_worker_post_fork_cleanup,
1331 &config);
1332 }
1333
1334 static void sessiond_uuid_log(void)
1335 {
1336 char uuid_str[LTTNG_UUID_STR_LEN];
1337
1338 lttng_uuid_to_str(sessiond_uuid, uuid_str);
1339 DBG("Starting lttng-sessiond {%s}", uuid_str);
1340 }
1341
1342 /*
1343 * main
1344 */
1345 int main(int argc, char **argv)
1346 {
1347 int ret = 0, retval = 0;
1348 const char *env_app_timeout;
1349 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
1350 *ust64_channel_monitor_pipe = NULL,
1351 *kernel_channel_monitor_pipe = NULL;
1352 struct lttng_thread *ht_cleanup_thread = NULL;
1353 struct timer_thread_parameters timer_thread_parameters;
1354 /* Rotation thread handle. */
1355 struct rotation_thread_handle *rotation_thread_handle = NULL;
1356 /* Queue of rotation jobs populated by the sessiond-timer. */
1357 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
1358 struct lttng_thread *client_thread = NULL;
1359 struct lttng_thread *notification_thread = NULL;
1360 struct lttng_thread *register_apps_thread = NULL;
1361
1362 logger_set_thread_name("Main", false);
1363 init_kernel_workarounds();
1364
1365 rcu_register_thread();
1366
1367 if (set_signal_handler()) {
1368 retval = -1;
1369 goto exit_set_signal_handler;
1370 }
1371
1372 if (timer_signal_init()) {
1373 retval = -1;
1374 goto exit_set_signal_handler;
1375 }
1376
1377 page_size = sysconf(_SC_PAGESIZE);
1378 if (page_size < 0) {
1379 PERROR("sysconf _SC_PAGESIZE");
1380 page_size = LONG_MAX;
1381 WARN("Fallback page size to %ld", page_size);
1382 }
1383
1384 ret = sessiond_config_init(&config);
1385 if (ret) {
1386 retval = -1;
1387 goto exit_set_signal_handler;
1388 }
1389
1390 /*
1391 * Init config from environment variables.
1392 * Command line option override env configuration per-doc. Do env first.
1393 */
1394 sessiond_config_apply_env_config(&config);
1395
1396 /*
1397 * Parse arguments and load the daemon configuration file.
1398 *
1399 * We have an exit_options exit path to free memory reserved by
1400 * set_options. This is needed because the rest of sessiond_cleanup()
1401 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1402 * depends on set_options.
1403 */
1404 progname = argv[0];
1405 if (set_options(argc, argv)) {
1406 retval = -1;
1407 goto exit_options;
1408 }
1409
1410 /*
1411 * Resolve all paths received as arguments, configuration option, or
1412 * through environment variable as absolute paths. This is necessary
1413 * since daemonizing causes the sessiond's current working directory
1414 * to '/'.
1415 */
1416 ret = sessiond_config_resolve_paths(&config);
1417 if (ret) {
1418 goto exit_options;
1419 }
1420
1421 /* Apply config. */
1422 lttng_opt_verbose = config.verbose;
1423 lttng_opt_quiet = config.quiet;
1424 kconsumer_data.err_unix_sock_path =
1425 config.kconsumerd_err_unix_sock_path.value;
1426 kconsumer_data.cmd_unix_sock_path =
1427 config.kconsumerd_cmd_unix_sock_path.value;
1428 ustconsumer32_data.err_unix_sock_path =
1429 config.consumerd32_err_unix_sock_path.value;
1430 ustconsumer32_data.cmd_unix_sock_path =
1431 config.consumerd32_cmd_unix_sock_path.value;
1432 ustconsumer64_data.err_unix_sock_path =
1433 config.consumerd64_err_unix_sock_path.value;
1434 ustconsumer64_data.cmd_unix_sock_path =
1435 config.consumerd64_cmd_unix_sock_path.value;
1436 set_clock_plugin_env();
1437
1438 sessiond_config_log(&config);
1439 sessiond_uuid_log();
1440
1441 if (opt_print_version) {
1442 print_version();
1443 retval = 0;
1444 goto exit_options;
1445 }
1446
1447 if (create_lttng_rundir()) {
1448 retval = -1;
1449 goto exit_options;
1450 }
1451
1452 /* Abort launch if a session daemon is already running. */
1453 if (check_existing_daemon()) {
1454 ERR("A session daemon is already running.");
1455 retval = -1;
1456 goto exit_options;
1457 }
1458
1459 /* Daemonize */
1460 if (config.daemonize || config.background) {
1461 int i;
1462
1463 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
1464 !config.background);
1465 if (ret < 0) {
1466 retval = -1;
1467 goto exit_options;
1468 }
1469
1470 /*
1471 * We are in the child. Make sure all other file descriptors are
1472 * closed, in case we are called with more opened file
1473 * descriptors than the standard ones and the lock file.
1474 */
1475 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1476 if (i == lockfile_fd) {
1477 continue;
1478 }
1479 (void) close(i);
1480 }
1481 }
1482
1483 if (launch_run_as_worker(argv[0]) < 0) {
1484 goto exit_create_run_as_worker_cleanup;
1485 }
1486
1487 /*
1488 * Starting from here, we can create threads. This needs to be after
1489 * lttng_daemonize due to RCU.
1490 */
1491
1492 /*
1493 * Initialize the health check subsystem. This call should set the
1494 * appropriate time values.
1495 */
1496 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
1497 if (!health_sessiond) {
1498 PERROR("health_app_create error");
1499 retval = -1;
1500 goto stop_threads;
1501 }
1502
1503 /* Create thread to clean up RCU hash tables */
1504 ht_cleanup_thread = launch_ht_cleanup_thread();
1505 if (!ht_cleanup_thread) {
1506 retval = -1;
1507 goto stop_threads;
1508 }
1509
1510 /* Create thread quit pipe */
1511 if (sessiond_init_thread_quit_pipe()) {
1512 retval = -1;
1513 goto stop_threads;
1514 }
1515
1516 /* Check if daemon is UID = 0 */
1517 is_root = !getuid();
1518 if (is_root) {
1519 /* Create global run dir with root access */
1520
1521 kernel_channel_monitor_pipe = lttng_pipe_open(0);
1522 if (!kernel_channel_monitor_pipe) {
1523 ERR("Failed to create kernel consumer channel monitor pipe");
1524 retval = -1;
1525 goto stop_threads;
1526 }
1527 kconsumer_data.channel_monitor_pipe =
1528 lttng_pipe_release_writefd(
1529 kernel_channel_monitor_pipe);
1530 if (kconsumer_data.channel_monitor_pipe < 0) {
1531 retval = -1;
1532 goto stop_threads;
1533 }
1534 }
1535
1536 /* Set consumer initial state */
1537 kernel_consumerd_state = CONSUMER_STOPPED;
1538 ust_consumerd_state = CONSUMER_STOPPED;
1539
1540 ust32_channel_monitor_pipe = lttng_pipe_open(0);
1541 if (!ust32_channel_monitor_pipe) {
1542 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1543 retval = -1;
1544 goto stop_threads;
1545 }
1546 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1547 ust32_channel_monitor_pipe);
1548 if (ustconsumer32_data.channel_monitor_pipe < 0) {
1549 retval = -1;
1550 goto stop_threads;
1551 }
1552
1553 /*
1554 * The rotation_thread_timer_queue structure is shared between the
1555 * sessiond timer thread and the rotation thread. The main thread keeps
1556 * its ownership and destroys it when both threads have been joined.
1557 */
1558 rotation_timer_queue = rotation_thread_timer_queue_create();
1559 if (!rotation_timer_queue) {
1560 retval = -1;
1561 goto stop_threads;
1562 }
1563 timer_thread_parameters.rotation_thread_job_queue =
1564 rotation_timer_queue;
1565
1566 ust64_channel_monitor_pipe = lttng_pipe_open(0);
1567 if (!ust64_channel_monitor_pipe) {
1568 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1569 retval = -1;
1570 goto stop_threads;
1571 }
1572 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1573 ust64_channel_monitor_pipe);
1574 if (ustconsumer64_data.channel_monitor_pipe < 0) {
1575 retval = -1;
1576 goto stop_threads;
1577 }
1578
1579 /*
1580 * Init UST app hash table. Alloc hash table before this point since
1581 * cleanup() can get called after that point.
1582 */
1583 if (ust_app_ht_alloc()) {
1584 ERR("Failed to allocate UST app hash table");
1585 retval = -1;
1586 goto stop_threads;
1587 }
1588
1589 /*
1590 * Initialize agent app hash table. We allocate the hash table here
1591 * since cleanup() can get called after this point.
1592 */
1593 if (agent_app_ht_alloc()) {
1594 ERR("Failed to allocate Agent app hash table");
1595 retval = -1;
1596 goto stop_threads;
1597 }
1598
1599 /*
1600 * These actions must be executed as root. We do that *after* setting up
1601 * the sockets path because we MUST make the check for another daemon using
1602 * those paths *before* trying to set the kernel consumer sockets and init
1603 * kernel tracer.
1604 */
1605 if (is_root) {
1606 if (set_consumer_sockets(&kconsumer_data)) {
1607 retval = -1;
1608 goto stop_threads;
1609 }
1610
1611 /* Setup kernel tracer */
1612 if (!config.no_kernel) {
1613 init_kernel_tracer();
1614 }
1615
1616 /* Set ulimit for open files */
1617 set_ulimit();
1618 }
1619 /* init lttng_fd tracking must be done after set_ulimit. */
1620 lttng_fd_init();
1621
1622 if (set_consumer_sockets(&ustconsumer64_data)) {
1623 retval = -1;
1624 goto stop_threads;
1625 }
1626
1627 if (set_consumer_sockets(&ustconsumer32_data)) {
1628 retval = -1;
1629 goto stop_threads;
1630 }
1631
1632 /* Get parent pid if -S, --sig-parent is specified. */
1633 if (config.sig_parent) {
1634 ppid = getppid();
1635 }
1636
1637 /* Setup the kernel pipe for waking up the kernel thread */
1638 if (is_root && !config.no_kernel) {
1639 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
1640 retval = -1;
1641 goto stop_threads;
1642 }
1643 }
1644
1645 /* Setup the thread apps communication pipe. */
1646 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
1647 retval = -1;
1648 goto stop_threads;
1649 }
1650
1651 /* Setup the thread apps notify communication pipe. */
1652 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
1653 retval = -1;
1654 goto stop_threads;
1655 }
1656
1657 /* Initialize global buffer per UID and PID registry. */
1658 buffer_reg_init_uid_registry();
1659 buffer_reg_init_pid_registry();
1660
1661 /* Init UST command queue. */
1662 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1663
1664 cmd_init();
1665
1666 /* Check for the application socket timeout env variable. */
1667 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
1668 if (env_app_timeout) {
1669 config.app_socket_timeout = atoi(env_app_timeout);
1670 } else {
1671 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
1672 }
1673
1674 ret = write_pidfile();
1675 if (ret) {
1676 ERR("Error in write_pidfile");
1677 retval = -1;
1678 goto stop_threads;
1679 }
1680
1681 /* Initialize communication library */
1682 lttcomm_init();
1683 /* Initialize TCP timeout values */
1684 lttcomm_inet_init();
1685
1686 /* Create health-check thread. */
1687 if (!launch_health_management_thread()) {
1688 retval = -1;
1689 goto stop_threads;
1690 }
1691
1692 /* notification_thread_data acquires the pipes' read side. */
1693 notification_thread_handle = notification_thread_handle_create(
1694 ust32_channel_monitor_pipe,
1695 ust64_channel_monitor_pipe,
1696 kernel_channel_monitor_pipe);
1697 if (!notification_thread_handle) {
1698 retval = -1;
1699 ERR("Failed to create notification thread shared data");
1700 goto stop_threads;
1701 }
1702
1703 /* Create notification thread. */
1704 notification_thread = launch_notification_thread(
1705 notification_thread_handle);
1706 if (!notification_thread) {
1707 retval = -1;
1708 goto stop_threads;
1709 }
1710
1711 /* Create timer thread. */
1712 if (!launch_timer_thread(&timer_thread_parameters)) {
1713 retval = -1;
1714 goto stop_threads;
1715 }
1716
1717 /* rotation_thread_data acquires the pipes' read side. */
1718 rotation_thread_handle = rotation_thread_handle_create(
1719 rotation_timer_queue,
1720 notification_thread_handle);
1721 if (!rotation_thread_handle) {
1722 retval = -1;
1723 ERR("Failed to create rotation thread shared data");
1724 stop_threads();
1725 goto stop_threads;
1726 }
1727
1728 /* Create rotation thread. */
1729 if (!launch_rotation_thread(rotation_thread_handle)) {
1730 retval = -1;
1731 goto stop_threads;
1732 }
1733
1734 /* Create thread to manage the client socket */
1735 client_thread = launch_client_thread();
1736 if (!client_thread) {
1737 retval = -1;
1738 goto stop_threads;
1739 }
1740
1741 /* Set credentials of the client socket and rundir */
1742 if (is_root && set_permissions(config.rundir.value)) {
1743 retval = -1;
1744 goto stop_threads;
1745 }
1746
1747 if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1],
1748 apps_cmd_notify_pipe[1])) {
1749 retval = -1;
1750 goto stop_threads;
1751 }
1752
1753 /* Create thread to manage application registration. */
1754 register_apps_thread = launch_application_registration_thread(
1755 &ust_cmd_queue);
1756 if (!register_apps_thread) {
1757 retval = -1;
1758 goto stop_threads;
1759 }
1760
1761 /* Create thread to manage application socket */
1762 if (!launch_application_management_thread(apps_cmd_pipe[0])) {
1763 retval = -1;
1764 goto stop_threads;
1765 }
1766
1767 /* Create thread to manage application notify socket */
1768 if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
1769 retval = -1;
1770 goto stop_threads;
1771 }
1772
1773 /* Create agent management thread. */
1774 if (!launch_agent_management_thread()) {
1775 retval = -1;
1776 goto stop_threads;
1777 }
1778
1779 /* Don't start this thread if kernel tracing is not requested nor root */
1780 if (is_root && !config.no_kernel) {
1781 /* Create kernel thread to manage kernel event */
1782 if (!launch_kernel_management_thread(kernel_poll_pipe[0])) {
1783 retval = -1;
1784 goto stop_threads;
1785 }
1786
1787 if (kernel_get_notification_fd() >= 0) {
1788 ret = notification_thread_command_add_tracer_event_source(
1789 notification_thread_handle,
1790 kernel_get_notification_fd(),
1791 LTTNG_DOMAIN_KERNEL);
1792 if (ret != LTTNG_OK) {
1793 ERR("Failed to add kernel trigger event source to notification thread");
1794 retval = -1;
1795 goto stop_threads;
1796 }
1797 }
1798 }
1799
1800 /* Load sessions. */
1801 ret = config_load_session(config.load_session_path.value,
1802 NULL, 1, 1, NULL);
1803 if (ret) {
1804 ERR("Session load failed: %s", error_get_str(ret));
1805 retval = -1;
1806 goto stop_threads;
1807 }
1808
1809 /* Initialization completed. */
1810 sessiond_signal_parents();
1811
1812 /*
1813 * This is where we start awaiting program completion (e.g. through
1814 * signal that asks threads to teardown).
1815 */
1816
1817 /* Initiate teardown once activity occurs on the quit pipe. */
1818 sessiond_wait_for_quit_pipe(-1);
1819
1820 stop_threads:
1821 /*
1822 * Ensure that the client thread is no longer accepting new commands,
1823 * which could cause new sessions to be created.
1824 */
1825 if (client_thread) {
1826 lttng_thread_shutdown(client_thread);
1827 lttng_thread_put(client_thread);
1828 }
1829
1830 destroy_all_sessions_and_wait();
1831
1832 /*
1833 * At this point no new trigger can be registered (no sessions are
1834 * running/rotating) and clients can't connect to the session daemon
1835 * anymore. Unregister all triggers.
1836 */
1837 unregister_all_triggers();
1838
1839 if (register_apps_thread) {
1840 lttng_thread_shutdown(register_apps_thread);
1841 lttng_thread_put(register_apps_thread);
1842 }
1843 lttng_thread_list_shutdown_orphans();
1844
1845 /*
1846 * Wait for all pending call_rcu work to complete before tearing
1847 * down data structures. call_rcu worker may be trying to
1848 * perform lookups in those structures.
1849 */
1850 rcu_barrier();
1851 /*
1852 * sessiond_cleanup() is called when no other thread is running, except
1853 * the ht_cleanup thread, which is needed to destroy the hash tables.
1854 */
1855 rcu_thread_online();
1856 sessiond_cleanup();
1857
1858 /*
1859 * Wait for all pending call_rcu work to complete tearing shutting down
1860 * the notification thread. This call_rcu work includes shutting down
1861 * UST apps and event notifier pipes.
1862 */
1863 rcu_barrier();
1864
1865 if (notification_thread) {
1866 lttng_thread_shutdown(notification_thread);
1867 lttng_thread_put(notification_thread);
1868 }
1869
1870 /*
1871 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1872 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1873 * the queue is empty before shutting down the clean-up thread.
1874 */
1875 rcu_barrier();
1876
1877 if (ht_cleanup_thread) {
1878 lttng_thread_shutdown(ht_cleanup_thread);
1879 lttng_thread_put(ht_cleanup_thread);
1880 }
1881
1882 rcu_thread_offline();
1883 rcu_unregister_thread();
1884
1885 if (rotation_thread_handle) {
1886 rotation_thread_handle_destroy(rotation_thread_handle);
1887 }
1888
1889 /*
1890 * After the rotation and timer thread have quit, we can safely destroy
1891 * the rotation_timer_queue.
1892 */
1893 rotation_thread_timer_queue_destroy(rotation_timer_queue);
1894 /*
1895 * The teardown of the notification system is performed after the
1896 * session daemon's teardown in order to allow it to be notified
1897 * of the active session and channels at the moment of the teardown.
1898 */
1899 if (notification_thread_handle) {
1900 notification_thread_handle_destroy(notification_thread_handle);
1901 }
1902 lttng_pipe_destroy(ust32_channel_monitor_pipe);
1903 lttng_pipe_destroy(ust64_channel_monitor_pipe);
1904 lttng_pipe_destroy(kernel_channel_monitor_pipe);
1905
1906 if (health_sessiond) {
1907 health_app_destroy(health_sessiond);
1908 }
1909 exit_create_run_as_worker_cleanup:
1910 exit_options:
1911 sessiond_cleanup_lock_file();
1912 sessiond_cleanup_options();
1913
1914 exit_set_signal_handler:
1915 if (!retval) {
1916 exit(EXIT_SUCCESS);
1917 } else {
1918 exit(EXIT_FAILURE);
1919 }
1920 }
This page took 0.074367 seconds and 4 git commands to generate.