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