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