SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10#define _LGPL_SOURCE
11#include <getopt.h>
12#include <grp.h>
13#include <limits.h>
14#include <paths.h>
15#include <pthread.h>
16#include <signal.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <inttypes.h>
21#include <sys/mman.h>
22#include <sys/mount.h>
23#include <sys/resource.h>
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/wait.h>
28#include <urcu/uatomic.h>
29#include <unistd.h>
30#include <ctype.h>
31
32#include <common/common.h>
33#include <common/compat/socket.h>
34#include <common/compat/getenv.h>
35#include <common/defaults.h>
36#include <common/kernel-consumer/kernel-consumer.h>
37#include <common/futex.h>
38#include <common/relayd/relayd.h>
39#include <common/utils.h>
40#include <common/daemonize.h>
41#include <common/config/session-config.h>
42#include <common/dynamic-buffer.h>
43#include <lttng/event-internal.h>
44
45#include "lttng-sessiond.h"
46#include "buffer-registry.h"
47#include "channel.h"
48#include "cmd.h"
49#include "consumer.h"
50#include "context.h"
51#include "event.h"
52#include "kernel.h"
53#include "kernel-consumer.h"
54#include "lttng-ust-ctl.h"
55#include "ust-consumer.h"
56#include "utils.h"
57#include "fd-limit.h"
58#include "health-sessiond.h"
59#include "testpoint.h"
60#include "notify-apps.h"
61#include "agent-thread.h"
62#include "save.h"
63#include "notification-thread.h"
64#include "notification-thread-commands.h"
65#include "rotation-thread.h"
66#include "agent.h"
67#include "ht-cleanup.h"
68#include "sessiond-config.h"
69#include "timer.h"
70#include "thread.h"
71#include "client.h"
72#include "dispatch.h"
73#include "register.h"
74#include "manage-apps.h"
75#include "manage-kernel.h"
76#include "trigger-error-accounting.h"
77
78static const char *help_msg =
79#ifdef LTTNG_EMBED_HELP
80#include <lttng-sessiond.8.h>
81#else
82NULL
83#endif
84;
85
86#define TRIGGER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
87
88const char *progname;
89static int lockfile_fd = -1;
90static int opt_print_version;
91
92/* Set to 1 when a SIGUSR1 signal is received. */
93static int recv_child_signal;
94
95/* Command line options */
96static 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 { "trigger-error-number-of-bucket", required_argument, 0, '\0' },
126 { NULL, 0, 0, 0 }
127};
128
129/* Command line options to ignore from configuration file */
130static 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 */
136static int apps_cmd_pipe[2] = { -1, -1 };
137static 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 */
148static struct ust_cmd_queue ust_cmd_queue;
149
150/*
151 * Section name to look for in the daemon configuration file.
152 */
153static const char * const config_section_name = "sessiond";
154
155/* Am I root or not. Set to 1 if the daemon is running as root */
156static int is_root;
157
158/*
159 * Stop all threads by closing the thread quit pipe.
160 */
161static 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 */
176static 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 */
242static 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 */
266static 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 trigger agents");
321 trigger_agent_ht_clean();
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 */
349static 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
358static 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 */
369static 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, "trigger-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 --trigger-error-number-of-bucket parameter: %s", arg);
710 return -1;
711 }
712 if (v == 0 || v >= TRIGGER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX) {
713 ERR("Value out of range for --trigger-error-number-of-bucket parameter: %s", arg);
714 return -1;
715 }
716 config.trigger_error_counter_bucket = (int) v;
717 DBG3("Number of error counter set to non default: %i",
718 config.trigger_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
729end:
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 */
754static 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
801end:
802 return ret;
803}
804
805static void print_version(void) {
806 fprintf(stdout, "%s\n", VERSION);
807}
808
809/*
810 * daemon configuration loading and argument parsing
811 */
812static 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
885end:
886 free(optstring);
887 return ret;
888}
889
890/*
891 * Create lockfile using the rundir and return its fd.
892 */
893static 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 */
904static 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 }
919end:
920 return ret;
921}
922
923static 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 */
949static 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 */
1014static 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
1030error:
1031 return ret;
1032}
1033
1034/*
1035 * Setup sockets and directory needed by the consumerds' communication with the
1036 * session daemon.
1037 */
1038static 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
1114error:
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 */
1124static 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 */
1147static 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 */
1192static 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
1207static int write_pidfile(void)
1208{
1209 return utils_create_pid_file(getpid(), config.pid_file_path.value);
1210}
1211
1212static 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);
1237end:
1238 return ret;
1239}
1240
1241static 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
1280static int run_as_worker_post_fork_cleanup(void *data)
1281{
1282 struct sessiond_config *sessiond_config = data;
1283
1284 sessiond_config_fini(sessiond_config);
1285 return 0;
1286}
1287
1288static int launch_run_as_worker(const char *procname)
1289{
1290 /*
1291 * Clean-up before forking the run-as worker. Any dynamically
1292 * allocated memory of which the worker is not aware will
1293 * be leaked as the process forks a run-as worker (and performs
1294 * no exec*()). The same would apply to any opened fd.
1295 */
1296 return run_as_create_worker(procname, run_as_worker_post_fork_cleanup,
1297 &config);
1298}
1299
1300static void sessiond_uuid_log(void)
1301{
1302 char uuid_str[LTTNG_UUID_STR_LEN];
1303
1304 lttng_uuid_to_str(sessiond_uuid, uuid_str);
1305 DBG("Starting lttng-sessiond {%s}", uuid_str);
1306}
1307
1308/*
1309 * main
1310 */
1311int main(int argc, char **argv)
1312{
1313 int ret = 0, retval = 0;
1314 const char *env_app_timeout;
1315 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
1316 *ust64_channel_monitor_pipe = NULL,
1317 *kernel_channel_monitor_pipe = NULL;
1318 struct lttng_thread *ht_cleanup_thread = NULL;
1319 struct timer_thread_parameters timer_thread_parameters;
1320 /* Rotation thread handle. */
1321 struct rotation_thread_handle *rotation_thread_handle = NULL;
1322 /* Queue of rotation jobs populated by the sessiond-timer. */
1323 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
1324 struct lttng_thread *client_thread = NULL;
1325 struct lttng_thread *notification_thread = NULL;
1326 struct lttng_thread *register_apps_thread = NULL;
1327
1328 logger_set_thread_name("Main", false);
1329 init_kernel_workarounds();
1330
1331 rcu_register_thread();
1332
1333 if (set_signal_handler()) {
1334 retval = -1;
1335 goto exit_set_signal_handler;
1336 }
1337
1338 if (timer_signal_init()) {
1339 retval = -1;
1340 goto exit_set_signal_handler;
1341 }
1342
1343 page_size = sysconf(_SC_PAGESIZE);
1344 if (page_size < 0) {
1345 PERROR("sysconf _SC_PAGESIZE");
1346 page_size = LONG_MAX;
1347 WARN("Fallback page size to %ld", page_size);
1348 }
1349
1350 ret = sessiond_config_init(&config);
1351 if (ret) {
1352 retval = -1;
1353 goto exit_set_signal_handler;
1354 }
1355
1356 /*
1357 * Init config from environment variables.
1358 * Command line option override env configuration per-doc. Do env first.
1359 */
1360 sessiond_config_apply_env_config(&config);
1361
1362 /*
1363 * Parse arguments and load the daemon configuration file.
1364 *
1365 * We have an exit_options exit path to free memory reserved by
1366 * set_options. This is needed because the rest of sessiond_cleanup()
1367 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1368 * depends on set_options.
1369 */
1370 progname = argv[0];
1371 if (set_options(argc, argv)) {
1372 retval = -1;
1373 goto exit_options;
1374 }
1375
1376 /*
1377 * Resolve all paths received as arguments, configuration option, or
1378 * through environment variable as absolute paths. This is necessary
1379 * since daemonizing causes the sessiond's current working directory
1380 * to '/'.
1381 */
1382 ret = sessiond_config_resolve_paths(&config);
1383 if (ret) {
1384 goto exit_options;
1385 }
1386
1387 /* Apply config. */
1388 lttng_opt_verbose = config.verbose;
1389 lttng_opt_quiet = config.quiet;
1390 kconsumer_data.err_unix_sock_path =
1391 config.kconsumerd_err_unix_sock_path.value;
1392 kconsumer_data.cmd_unix_sock_path =
1393 config.kconsumerd_cmd_unix_sock_path.value;
1394 ustconsumer32_data.err_unix_sock_path =
1395 config.consumerd32_err_unix_sock_path.value;
1396 ustconsumer32_data.cmd_unix_sock_path =
1397 config.consumerd32_cmd_unix_sock_path.value;
1398 ustconsumer64_data.err_unix_sock_path =
1399 config.consumerd64_err_unix_sock_path.value;
1400 ustconsumer64_data.cmd_unix_sock_path =
1401 config.consumerd64_cmd_unix_sock_path.value;
1402 set_clock_plugin_env();
1403
1404 sessiond_config_log(&config);
1405 sessiond_uuid_log();
1406
1407 if (opt_print_version) {
1408 print_version();
1409 retval = 0;
1410 goto exit_options;
1411 }
1412
1413 if (create_lttng_rundir()) {
1414 retval = -1;
1415 goto exit_options;
1416 }
1417
1418 /* Abort launch if a session daemon is already running. */
1419 if (check_existing_daemon()) {
1420 ERR("A session daemon is already running.");
1421 retval = -1;
1422 goto exit_options;
1423 }
1424
1425 /* Daemonize */
1426 if (config.daemonize || config.background) {
1427 int i;
1428
1429 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
1430 !config.background);
1431 if (ret < 0) {
1432 retval = -1;
1433 goto exit_options;
1434 }
1435
1436 /*
1437 * We are in the child. Make sure all other file descriptors are
1438 * closed, in case we are called with more opened file
1439 * descriptors than the standard ones and the lock file.
1440 */
1441 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1442 if (i == lockfile_fd) {
1443 continue;
1444 }
1445 (void) close(i);
1446 }
1447 }
1448
1449 if (launch_run_as_worker(argv[0]) < 0) {
1450 goto exit_create_run_as_worker_cleanup;
1451 }
1452
1453 /*
1454 * Starting from here, we can create threads. This needs to be after
1455 * lttng_daemonize due to RCU.
1456 */
1457
1458 /*
1459 * Initialize the health check subsystem. This call should set the
1460 * appropriate time values.
1461 */
1462 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
1463 if (!health_sessiond) {
1464 PERROR("health_app_create error");
1465 retval = -1;
1466 goto stop_threads;
1467 }
1468
1469 /* Create thread to clean up RCU hash tables */
1470 ht_cleanup_thread = launch_ht_cleanup_thread();
1471 if (!ht_cleanup_thread) {
1472 retval = -1;
1473 goto stop_threads;
1474 }
1475
1476 /* Create thread quit pipe */
1477 if (sessiond_init_thread_quit_pipe()) {
1478 retval = -1;
1479 goto stop_threads;
1480 }
1481
1482 /* Check if daemon is UID = 0 */
1483 is_root = !getuid();
1484 if (is_root) {
1485 /* Create global run dir with root access */
1486
1487 kernel_channel_monitor_pipe = lttng_pipe_open(0);
1488 if (!kernel_channel_monitor_pipe) {
1489 ERR("Failed to create kernel consumer channel monitor pipe");
1490 retval = -1;
1491 goto stop_threads;
1492 }
1493 kconsumer_data.channel_monitor_pipe =
1494 lttng_pipe_release_writefd(
1495 kernel_channel_monitor_pipe);
1496 if (kconsumer_data.channel_monitor_pipe < 0) {
1497 retval = -1;
1498 goto stop_threads;
1499 }
1500 }
1501
1502 /* Set consumer initial state */
1503 kernel_consumerd_state = CONSUMER_STOPPED;
1504 ust_consumerd_state = CONSUMER_STOPPED;
1505
1506 ust32_channel_monitor_pipe = lttng_pipe_open(0);
1507 if (!ust32_channel_monitor_pipe) {
1508 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1509 retval = -1;
1510 goto stop_threads;
1511 }
1512 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1513 ust32_channel_monitor_pipe);
1514 if (ustconsumer32_data.channel_monitor_pipe < 0) {
1515 retval = -1;
1516 goto stop_threads;
1517 }
1518
1519 /*
1520 * The rotation_thread_timer_queue structure is shared between the
1521 * sessiond timer thread and the rotation thread. The main thread keeps
1522 * its ownership and destroys it when both threads have been joined.
1523 */
1524 rotation_timer_queue = rotation_thread_timer_queue_create();
1525 if (!rotation_timer_queue) {
1526 retval = -1;
1527 goto stop_threads;
1528 }
1529 timer_thread_parameters.rotation_thread_job_queue =
1530 rotation_timer_queue;
1531
1532 ust64_channel_monitor_pipe = lttng_pipe_open(0);
1533 if (!ust64_channel_monitor_pipe) {
1534 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1535 retval = -1;
1536 goto stop_threads;
1537 }
1538 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1539 ust64_channel_monitor_pipe);
1540 if (ustconsumer64_data.channel_monitor_pipe < 0) {
1541 retval = -1;
1542 goto stop_threads;
1543 }
1544
1545 /*
1546 * Init UST app hash table. Alloc hash table before this point since
1547 * cleanup() can get called after that point.
1548 */
1549 if (ust_app_ht_alloc()) {
1550 ERR("Failed to allocate UST app hash table");
1551 retval = -1;
1552 goto stop_threads;
1553 }
1554
1555 trigger_error_accounting_init(config.trigger_error_counter_bucket);
1556
1557 /*
1558 * Initialize agent app hash table. We allocate the hash table here
1559 * since cleanup() can get called after this point.
1560 */
1561 if (agent_app_ht_alloc()) {
1562 ERR("Failed to allocate Agent app hash table");
1563 retval = -1;
1564 goto stop_threads;
1565 }
1566
1567 if (trigger_agent_ht_alloc()) {
1568 ERR("Failed to allocate trigger agent hash table");
1569 retval = -1;
1570 goto stop_threads;
1571 }
1572 /*
1573 * These actions must be executed as root. We do that *after* setting up
1574 * the sockets path because we MUST make the check for another daemon using
1575 * those paths *before* trying to set the kernel consumer sockets and init
1576 * kernel tracer.
1577 */
1578 if (is_root) {
1579 if (set_consumer_sockets(&kconsumer_data)) {
1580 retval = -1;
1581 goto stop_threads;
1582 }
1583
1584 /* Setup kernel tracer */
1585 if (!config.no_kernel) {
1586 init_kernel_tracer();
1587 }
1588
1589 /* Set ulimit for open files */
1590 set_ulimit();
1591 }
1592 /* init lttng_fd tracking must be done after set_ulimit. */
1593 lttng_fd_init();
1594
1595 if (set_consumer_sockets(&ustconsumer64_data)) {
1596 retval = -1;
1597 goto stop_threads;
1598 }
1599
1600 if (set_consumer_sockets(&ustconsumer32_data)) {
1601 retval = -1;
1602 goto stop_threads;
1603 }
1604
1605 /* Get parent pid if -S, --sig-parent is specified. */
1606 if (config.sig_parent) {
1607 ppid = getppid();
1608 }
1609
1610 /* Setup the kernel pipe for waking up the kernel thread */
1611 if (is_root && !config.no_kernel) {
1612 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
1613 retval = -1;
1614 goto stop_threads;
1615 }
1616 }
1617
1618 /* Setup the thread apps communication pipe. */
1619 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
1620 retval = -1;
1621 goto stop_threads;
1622 }
1623
1624 /* Setup the thread apps notify communication pipe. */
1625 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
1626 retval = -1;
1627 goto stop_threads;
1628 }
1629
1630 /* Initialize global buffer per UID and PID registry. */
1631 buffer_reg_init_uid_registry();
1632 buffer_reg_init_pid_registry();
1633
1634 /* Init UST command queue. */
1635 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1636
1637 cmd_init();
1638
1639 /* Check for the application socket timeout env variable. */
1640 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
1641 if (env_app_timeout) {
1642 config.app_socket_timeout = atoi(env_app_timeout);
1643 } else {
1644 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
1645 }
1646
1647 ret = write_pidfile();
1648 if (ret) {
1649 ERR("Error in write_pidfile");
1650 retval = -1;
1651 goto stop_threads;
1652 }
1653
1654 /* Initialize communication library */
1655 lttcomm_init();
1656 /* Initialize TCP timeout values */
1657 lttcomm_inet_init();
1658
1659 /* Create health-check thread. */
1660 if (!launch_health_management_thread()) {
1661 retval = -1;
1662 goto stop_threads;
1663 }
1664
1665 /* notification_thread_data acquires the pipes' read side. */
1666 notification_thread_handle = notification_thread_handle_create(
1667 ust32_channel_monitor_pipe,
1668 ust64_channel_monitor_pipe,
1669 kernel_channel_monitor_pipe,
1670 kernel_get_notification_fd());
1671 if (!notification_thread_handle) {
1672 retval = -1;
1673 ERR("Failed to create notification thread shared data");
1674 goto stop_threads;
1675 }
1676
1677 /* Create notification thread. */
1678 notification_thread = launch_notification_thread(
1679 notification_thread_handle);
1680 if (!notification_thread) {
1681 retval = -1;
1682 goto stop_threads;
1683 }
1684
1685 /* Create timer thread. */
1686 if (!launch_timer_thread(&timer_thread_parameters)) {
1687 retval = -1;
1688 goto stop_threads;
1689 }
1690
1691 /* rotation_thread_data acquires the pipes' read side. */
1692 rotation_thread_handle = rotation_thread_handle_create(
1693 rotation_timer_queue,
1694 notification_thread_handle);
1695 if (!rotation_thread_handle) {
1696 retval = -1;
1697 ERR("Failed to create rotation thread shared data");
1698 stop_threads();
1699 goto stop_threads;
1700 }
1701
1702 /* Create rotation thread. */
1703 if (!launch_rotation_thread(rotation_thread_handle)) {
1704 retval = -1;
1705 goto stop_threads;
1706 }
1707
1708 /* Create thread to manage the client socket */
1709 client_thread = launch_client_thread();
1710 if (!client_thread) {
1711 retval = -1;
1712 goto stop_threads;
1713 }
1714
1715 /* Set credentials of the client socket and rundir */
1716 if (is_root && set_permissions(config.rundir.value)) {
1717 retval = -1;
1718 goto stop_threads;
1719 }
1720
1721 if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1],
1722 apps_cmd_notify_pipe[1])) {
1723 retval = -1;
1724 goto stop_threads;
1725 }
1726
1727 /* Create thread to manage application registration. */
1728 register_apps_thread = launch_application_registration_thread(
1729 &ust_cmd_queue);
1730 if (!register_apps_thread) {
1731 retval = -1;
1732 goto stop_threads;
1733 }
1734
1735 /* Create thread to manage application socket */
1736 if (!launch_application_management_thread(apps_cmd_pipe[0])) {
1737 retval = -1;
1738 goto stop_threads;
1739 }
1740
1741 /* Create thread to manage application notify socket */
1742 if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
1743 retval = -1;
1744 goto stop_threads;
1745 }
1746
1747 /* Create agent management thread. */
1748 if (!launch_agent_management_thread()) {
1749 retval = -1;
1750 goto stop_threads;
1751 }
1752
1753 /* Don't start this thread if kernel tracing is not requested nor root */
1754 if (is_root && !config.no_kernel) {
1755 /* Create kernel thread to manage kernel event */
1756 if (!launch_kernel_management_thread(kernel_poll_pipe[0])) {
1757 retval = -1;
1758 goto stop_threads;
1759 }
1760
1761 if (kernel_get_notification_fd() > -1) {
1762 ret = notification_thread_command_add_application(
1763 notification_thread_handle, kernel_get_notification_fd(), LTTNG_DOMAIN_KERNEL);
1764 if (ret != LTTNG_OK) {
1765 ERR("Failed to add kernel trigger event source to notification thread");
1766 retval = -1;
1767 goto stop_threads;
1768 }
1769 }
1770 }
1771
1772 /* Load sessions. */
1773 ret = config_load_session(config.load_session_path.value,
1774 NULL, 1, 1, NULL);
1775 if (ret) {
1776 ERR("Session load failed: %s", error_get_str(ret));
1777 retval = -1;
1778 goto stop_threads;
1779 }
1780
1781 /* Initialization completed. */
1782 sessiond_signal_parents();
1783
1784 /*
1785 * This is where we start awaiting program completion (e.g. through
1786 * signal that asks threads to teardown).
1787 */
1788
1789 /* Initiate teardown once activity occurs on the quit pipe. */
1790 sessiond_wait_for_quit_pipe(-1);
1791
1792stop_threads:
1793
1794 trigger_error_accounting_fini();
1795 /*
1796 * Ensure that the client thread is no longer accepting new commands,
1797 * which could cause new sessions to be created.
1798 */
1799 if (client_thread) {
1800 lttng_thread_shutdown(client_thread);
1801 lttng_thread_put(client_thread);
1802 }
1803
1804 destroy_all_sessions_and_wait();
1805
1806 if (register_apps_thread) {
1807 lttng_thread_shutdown(register_apps_thread);
1808 lttng_thread_put(register_apps_thread);
1809 }
1810 lttng_thread_list_shutdown_orphans();
1811
1812 /*
1813 * Wait for all pending call_rcu work to complete before tearing
1814 * down data structures. call_rcu worker may be trying to
1815 * perform lookups in those structures.
1816 */
1817 rcu_barrier();
1818 /*
1819 * sessiond_cleanup() is called when no other thread is running, except
1820 * the ht_cleanup thread, which is needed to destroy the hash tables.
1821 */
1822 rcu_thread_online();
1823 sessiond_cleanup();
1824
1825 if (notification_thread) {
1826 lttng_thread_shutdown(notification_thread);
1827 lttng_thread_put(notification_thread);
1828 }
1829
1830 /*
1831 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1832 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1833 * the queue is empty before shutting down the clean-up thread.
1834 */
1835 rcu_barrier();
1836
1837 if (ht_cleanup_thread) {
1838 lttng_thread_shutdown(ht_cleanup_thread);
1839 lttng_thread_put(ht_cleanup_thread);
1840 }
1841
1842 rcu_thread_offline();
1843 rcu_unregister_thread();
1844
1845 if (rotation_thread_handle) {
1846 rotation_thread_handle_destroy(rotation_thread_handle);
1847 }
1848
1849 /*
1850 * After the rotation and timer thread have quit, we can safely destroy
1851 * the rotation_timer_queue.
1852 */
1853 rotation_thread_timer_queue_destroy(rotation_timer_queue);
1854 /*
1855 * The teardown of the notification system is performed after the
1856 * session daemon's teardown in order to allow it to be notified
1857 * of the active session and channels at the moment of the teardown.
1858 */
1859 if (notification_thread_handle) {
1860 notification_thread_handle_destroy(notification_thread_handle);
1861 }
1862 lttng_pipe_destroy(ust32_channel_monitor_pipe);
1863 lttng_pipe_destroy(ust64_channel_monitor_pipe);
1864 lttng_pipe_destroy(kernel_channel_monitor_pipe);
1865
1866 if (health_sessiond) {
1867 health_app_destroy(health_sessiond);
1868 }
1869exit_create_run_as_worker_cleanup:
1870exit_options:
1871 sessiond_cleanup_lock_file();
1872 sessiond_cleanup_options();
1873
1874exit_set_signal_handler:
1875 if (!retval) {
1876 exit(EXIT_SUCCESS);
1877 } else {
1878 exit(EXIT_FAILURE);
1879 }
1880}
This page took 0.089893 seconds and 5 git commands to generate.