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