Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / bin / lttng-consumerd / lttng-consumerd.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9#define _LGPL_SOURCE
10#include "health-consumerd.hpp"
11#include "lttng-consumerd.hpp"
12
13#include <common/common.hpp>
14#include <common/compat/getenv.hpp>
15#include <common/compat/poll.hpp>
16#include <common/consumer/consumer-timer.hpp>
17#include <common/consumer/consumer.hpp>
18#include <common/defaults.hpp>
19#include <common/sessiond-comm/sessiond-comm.hpp>
20#include <common/utils.hpp>
21
22#include <fcntl.h>
23#include <getopt.h>
24#include <grp.h>
25#include <limits.h>
26#include <poll.h>
27#include <pthread.h>
28#include <signal.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/ipc.h>
33#include <sys/mman.h>
34#include <sys/resource.h>
35#include <sys/shm.h>
36#include <sys/socket.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39#include <ulimit.h>
40#include <unistd.h>
41#include <urcu/compiler.h>
42#include <urcu/list.h>
43
44/* threads (channel handling, poll, metadata, sessiond) */
45
46static pthread_t channel_thread, data_thread, metadata_thread, sessiond_thread,
47 metadata_timer_thread, health_thread;
48static bool metadata_timer_thread_online;
49
50/* to count the number of times the user pressed ctrl+c */
51static int sigintcount = 0;
52
53/* Argument variables */
54int lttng_opt_quiet; /* not static in error.h */
55int lttng_opt_verbose; /* not static in error.h */
56int lttng_opt_mi; /* not static in error.h */
57
58static int opt_daemon;
59static const char *progname;
60static char command_sock_path[PATH_MAX]; /* Global command socket path */
61static char error_sock_path[PATH_MAX]; /* Global error path */
62static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
63
64/* the liblttngconsumerd context */
65static struct lttng_consumer_local_data *the_consumer_context;
66
67/* Consumerd health monitoring */
68struct health_app *health_consumerd;
69
70const char *tracing_group_name = DEFAULT_TRACING_GROUP;
71
72int lttng_consumer_ready = NR_LTTNG_CONSUMER_READY;
73
74enum lttng_consumer_type lttng_consumer_get_type(void)
75{
76 if (!the_consumer_context) {
77 return LTTNG_CONSUMER_UNKNOWN;
78 }
79 return the_consumer_context->type;
80}
81
82/*
83 * Signal handler for the daemon
84 */
85static void sighandler(int sig, siginfo_t *siginfo, void *arg __attribute__((unused)))
86{
87 if (sig == SIGINT && sigintcount++ == 0) {
88 DBG("ignoring first SIGINT");
89 return;
90 }
91
92 if (sig == SIGBUS) {
93 int write_ret;
94 const char msg[] = "Received SIGBUS, aborting program.\n";
95
96 lttng_consumer_sigbus_handle(siginfo->si_addr);
97 /*
98 * If ustctl did not catch this signal (triggering a
99 * siglongjmp), abort the program. Otherwise, the execution
100 * will resume from the ust-ctl call which caused this error.
101 *
102 * The return value is ignored since the program aborts anyhow.
103 */
104 write_ret = write(STDERR_FILENO, msg, sizeof(msg));
105 (void) write_ret;
106 abort();
107 }
108
109 if (the_consumer_context) {
110 lttng_consumer_should_exit(the_consumer_context);
111 }
112}
113
114/*
115 * Setup signal handler for :
116 * SIGINT, SIGTERM, SIGPIPE, SIGBUS
117 */
118static int set_signal_handler()
119{
120 int ret = 0;
121 struct sigaction sa;
122 sigset_t sigset;
123
124 if ((ret = sigemptyset(&sigset)) < 0) {
125 PERROR("sigemptyset");
126 return ret;
127 }
128
129 sa.sa_mask = sigset;
130 sa.sa_flags = SA_SIGINFO;
131
132 sa.sa_sigaction = sighandler;
133 if ((ret = sigaction(SIGTERM, &sa, nullptr)) < 0) {
134 PERROR("sigaction");
135 return ret;
136 }
137
138 if ((ret = sigaction(SIGINT, &sa, nullptr)) < 0) {
139 PERROR("sigaction");
140 return ret;
141 }
142
143 if ((ret = sigaction(SIGBUS, &sa, nullptr)) < 0) {
144 PERROR("sigaction");
145 return ret;
146 }
147
148 sa.sa_flags = 0;
149 sa.sa_handler = SIG_IGN;
150 if ((ret = sigaction(SIGPIPE, &sa, nullptr)) < 0) {
151 PERROR("sigaction");
152 return ret;
153 }
154
155 return ret;
156}
157
158/*
159 * Usage function on stream file.
160 */
161static void usage(FILE *fp)
162{
163 fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname);
164 fprintf(fp,
165 " -h, --help "
166 "Display this usage.\n");
167 fprintf(fp,
168 " -c, --consumerd-cmd-sock PATH "
169 "Specify path for the command socket\n");
170 fprintf(fp,
171 " -e, --consumerd-err-sock PATH "
172 "Specify path for the error socket\n");
173 fprintf(fp,
174 " -d, --daemonize "
175 "Start as a daemon.\n");
176 fprintf(fp,
177 " -q, --quiet "
178 "No output at all.\n");
179 fprintf(fp,
180 " -v, --verbose "
181 "Verbose mode. Activate DBG() macro.\n");
182 fprintf(fp,
183 " -V, --version "
184 "Show version number.\n");
185 fprintf(fp,
186 " -g, --group NAME "
187 "Specify the tracing group name. (default: tracing)\n");
188 fprintf(fp,
189 " -k, --kernel "
190 "Consumer kernel buffers (default).\n");
191 fprintf(fp,
192 " -u, --ust "
193 "Consumer UST buffers.%s\n",
194#ifdef HAVE_LIBLTTNG_UST_CTL
195 ""
196#else
197 " (support not compiled in)"
198#endif
199 );
200}
201
202/*
203 * daemon argument parsing
204 */
205static int parse_args(int argc, char **argv)
206{
207 int c, ret = 0;
208
209 static struct option long_options[] = { { "consumerd-cmd-sock", 1, nullptr, 'c' },
210 { "consumerd-err-sock", 1, nullptr, 'e' },
211 { "daemonize", 0, nullptr, 'd' },
212 { "group", 1, nullptr, 'g' },
213 { "help", 0, nullptr, 'h' },
214 { "quiet", 0, nullptr, 'q' },
215 { "verbose", 0, nullptr, 'v' },
216 { "version", 0, nullptr, 'V' },
217 { "kernel", 0, nullptr, 'k' },
218#ifdef HAVE_LIBLTTNG_UST_CTL
219 { "ust", 0, nullptr, 'u' },
220#endif
221 { nullptr, 0, nullptr, 0 } };
222
223 while (true) {
224 int option_index = 0;
225 c = getopt_long(argc,
226 argv,
227 "dhqvVku"
228 "c:e:g:",
229 long_options,
230 &option_index);
231 if (c == -1) {
232 break;
233 }
234
235 switch (c) {
236 case 0:
237 fprintf(stderr, "option %s", long_options[option_index].name);
238 if (optarg) {
239 fprintf(stderr, " with arg %s\n", optarg);
240 ret = -1;
241 goto end;
242 }
243 break;
244 case 'c':
245 if (lttng_is_setuid_setgid()) {
246 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
247 "-c, --consumerd-cmd-sock");
248 } else {
249 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
250 }
251 break;
252 case 'e':
253 if (lttng_is_setuid_setgid()) {
254 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
255 "-e, --consumerd-err-sock");
256 } else {
257 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
258 }
259 break;
260 case 'd':
261 opt_daemon = 1;
262 break;
263 case 'g':
264 if (lttng_is_setuid_setgid()) {
265 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
266 "-g, --group");
267 } else {
268 tracing_group_name = optarg;
269 }
270 break;
271 case 'h':
272 usage(stdout);
273 exit(EXIT_SUCCESS);
274 case 'q':
275 lttng_opt_quiet = 1;
276 break;
277 case 'v':
278 lttng_opt_verbose = 3;
279 break;
280 case 'V':
281 fprintf(stdout, "%s\n", VERSION);
282 exit(EXIT_SUCCESS);
283 case 'k':
284 opt_type = LTTNG_CONSUMER_KERNEL;
285 break;
286#ifdef HAVE_LIBLTTNG_UST_CTL
287 case 'u':
288#if (CAA_BITS_PER_LONG == 64)
289 opt_type = LTTNG_CONSUMER64_UST;
290#elif (CAA_BITS_PER_LONG == 32)
291 opt_type = LTTNG_CONSUMER32_UST;
292#else
293#error "Unknown bitness"
294#endif
295 break;
296#endif
297 default:
298 usage(stderr);
299 ret = -1;
300 goto end;
301 }
302 }
303end:
304 return ret;
305}
306
307/*
308 * Set open files limit to unlimited. This daemon can open a large number of
309 * file descriptors in order to consumer multiple kernel traces.
310 */
311static void set_ulimit()
312{
313 int ret;
314 struct rlimit lim;
315
316 /* The kernel does not allowed an infinite limit for open files */
317 lim.rlim_cur = 65535;
318 lim.rlim_max = 65535;
319
320 ret = setrlimit(RLIMIT_NOFILE, &lim);
321 if (ret < 0) {
322 PERROR("failed to set open files limit");
323 }
324}
325
326/*
327 * main
328 */
329int main(int argc, char **argv)
330{
331 int ret = 0, retval = 0;
332 void *status;
333 struct lttng_consumer_local_data *tmp_ctx;
334
335 rcu_register_thread();
336
337 if (run_as_create_worker(argv[0], nullptr, nullptr) < 0) {
338 goto exit_set_signal_handler;
339 }
340
341 if (set_signal_handler()) {
342 retval = -1;
343 goto exit_set_signal_handler;
344 }
345
346 /* Parse arguments */
347 progname = argv[0];
348 if (parse_args(argc, argv)) {
349 retval = -1;
350 goto exit_options;
351 }
352
353 /* Daemonize */
354 if (opt_daemon) {
355 int i;
356
357 /*
358 * fork
359 * child: setsid, close FD 0, 1, 2, chdir /
360 * parent: exit (if fork is successful)
361 */
362 ret = daemon(0, 0);
363 if (ret < 0) {
364 PERROR("daemon");
365 retval = -1;
366 goto exit_options;
367 }
368 /*
369 * We are in the child. Make sure all other file
370 * descriptors are closed, in case we are called with
371 * more opened file descriptors than the standard ones.
372 */
373 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
374 (void) close(i);
375 }
376 }
377
378 /*
379 * Starting from here, we can create threads. This needs to be after
380 * lttng_daemonize due to RCU.
381 */
382
383 health_consumerd = health_app_create(NR_HEALTH_CONSUMERD_TYPES);
384 if (!health_consumerd) {
385 retval = -1;
386 goto exit_health_consumerd_cleanup;
387 }
388
389 if (*command_sock_path == '\0') {
390 switch (opt_type) {
391 case LTTNG_CONSUMER_KERNEL:
392 ret = snprintf(command_sock_path,
393 PATH_MAX,
394 DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
395 DEFAULT_LTTNG_RUNDIR);
396 if (ret < 0) {
397 retval = -1;
398 goto exit_init_data;
399 }
400 break;
401 case LTTNG_CONSUMER64_UST:
402 ret = snprintf(command_sock_path,
403 PATH_MAX,
404 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
405 DEFAULT_LTTNG_RUNDIR);
406 if (ret < 0) {
407 retval = -1;
408 goto exit_init_data;
409 }
410 break;
411 case LTTNG_CONSUMER32_UST:
412 ret = snprintf(command_sock_path,
413 PATH_MAX,
414 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
415 DEFAULT_LTTNG_RUNDIR);
416 if (ret < 0) {
417 retval = -1;
418 goto exit_init_data;
419 }
420 break;
421 default:
422 ERR("Unknown consumerd type");
423 retval = -1;
424 goto exit_init_data;
425 }
426 }
427
428 /* Init */
429 if (lttng_consumer_init()) {
430 retval = -1;
431 goto exit_init_data;
432 }
433
434 /* Initialize communication library */
435 lttcomm_init();
436 /* Initialize TCP timeout values */
437 lttcomm_inet_init();
438
439 if (!getuid()) {
440 /* Set limit for open files */
441 set_ulimit();
442 }
443
444 /* create the consumer instance with and assign the callbacks */
445 the_consumer_context = lttng_consumer_create(opt_type,
446 lttng_consumer_read_subbuffer,
447 nullptr,
448 lttng_consumer_on_recv_stream,
449 nullptr);
450 if (!the_consumer_context) {
451 retval = -1;
452 goto exit_init_data;
453 }
454
455 lttng_consumer_set_command_sock_path(the_consumer_context, command_sock_path);
456 if (*error_sock_path == '\0') {
457 switch (opt_type) {
458 case LTTNG_CONSUMER_KERNEL:
459 ret = snprintf(error_sock_path,
460 PATH_MAX,
461 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
462 DEFAULT_LTTNG_RUNDIR);
463 if (ret < 0) {
464 retval = -1;
465 goto exit_init_data;
466 }
467 break;
468 case LTTNG_CONSUMER64_UST:
469 ret = snprintf(error_sock_path,
470 PATH_MAX,
471 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
472 DEFAULT_LTTNG_RUNDIR);
473 if (ret < 0) {
474 retval = -1;
475 goto exit_init_data;
476 }
477 break;
478 case LTTNG_CONSUMER32_UST:
479 ret = snprintf(error_sock_path,
480 PATH_MAX,
481 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
482 DEFAULT_LTTNG_RUNDIR);
483 if (ret < 0) {
484 retval = -1;
485 goto exit_init_data;
486 }
487 break;
488 default:
489 ERR("Unknown consumerd type");
490 retval = -1;
491 goto exit_init_data;
492 }
493 }
494
495 /* Connect to the socket created by lttng-sessiond to report errors */
496 DBG("Connecting to error socket %s", error_sock_path);
497 ret = lttcomm_connect_unix_sock(error_sock_path);
498 /*
499 * Not a fatal error, but all communication with lttng-sessiond will
500 * fail.
501 */
502 if (ret < 0) {
503 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
504 }
505 lttng_consumer_set_error_sock(the_consumer_context, ret);
506
507 /*
508 * Block RT signals used for UST periodical metadata flush and the live
509 * timer in main, and create a dedicated thread to handle these signals.
510 */
511 if (consumer_signal_init()) {
512 retval = -1;
513 goto exit_init_data;
514 }
515
516 the_consumer_context->type = opt_type;
517
518 if (utils_create_pipe(health_quit_pipe)) {
519 retval = -1;
520 goto exit_health_pipe;
521 }
522
523 /* Create thread to manage the client socket */
524 ret = pthread_create(&health_thread,
525 default_pthread_attr(),
526 thread_manage_health_consumerd,
527 (void *) nullptr);
528 if (ret) {
529 errno = ret;
530 PERROR("pthread_create health");
531 retval = -1;
532 goto exit_health_thread;
533 }
534
535 /*
536 * Wait for health thread to be initialized before letting the
537 * sessiond thread reply to the sessiond that we are ready.
538 */
539 while (uatomic_read(&lttng_consumer_ready)) {
540 usleep(100000);
541 }
542 cmm_smp_mb(); /* Read ready before following operations */
543
544 /*
545 * Create the thread to manage the UST metadata periodic timer and
546 * live timer.
547 */
548 ret = pthread_create(&metadata_timer_thread,
549 nullptr,
550 consumer_timer_thread,
551 (void *) the_consumer_context);
552 if (ret) {
553 errno = ret;
554 PERROR("pthread_create");
555 retval = -1;
556 goto exit_metadata_timer_thread;
557 }
558 metadata_timer_thread_online = true;
559
560 /* Create thread to manage channels */
561 ret = pthread_create(&channel_thread,
562 default_pthread_attr(),
563 consumer_thread_channel_poll,
564 (void *) the_consumer_context);
565 if (ret) {
566 errno = ret;
567 PERROR("pthread_create");
568 retval = -1;
569 goto exit_channel_thread;
570 }
571
572 /* Create thread to manage the polling/writing of trace metadata */
573 ret = pthread_create(&metadata_thread,
574 default_pthread_attr(),
575 consumer_thread_metadata_poll,
576 (void *) the_consumer_context);
577 if (ret) {
578 errno = ret;
579 PERROR("pthread_create");
580 retval = -1;
581 goto exit_metadata_thread;
582 }
583
584 /* Create thread to manage the polling/writing of trace data */
585 ret = pthread_create(&data_thread,
586 default_pthread_attr(),
587 consumer_thread_data_poll,
588 (void *) the_consumer_context);
589 if (ret) {
590 errno = ret;
591 PERROR("pthread_create");
592 retval = -1;
593 goto exit_data_thread;
594 }
595
596 /* Create the thread to manage the reception of fds */
597 ret = pthread_create(&sessiond_thread,
598 default_pthread_attr(),
599 consumer_thread_sessiond_poll,
600 (void *) the_consumer_context);
601 if (ret) {
602 errno = ret;
603 PERROR("pthread_create");
604 retval = -1;
605 goto exit_sessiond_thread;
606 }
607
608 /*
609 * This is where we start awaiting program completion (e.g. through
610 * signal that asks threads to teardown.
611 */
612
613 ret = pthread_join(sessiond_thread, &status);
614 if (ret) {
615 errno = ret;
616 PERROR("pthread_join sessiond_thread");
617 retval = -1;
618 }
619exit_sessiond_thread:
620
621 ret = pthread_join(data_thread, &status);
622 if (ret) {
623 errno = ret;
624 PERROR("pthread_join data_thread");
625 retval = -1;
626 }
627exit_data_thread:
628
629 ret = pthread_join(metadata_thread, &status);
630 if (ret) {
631 errno = ret;
632 PERROR("pthread_join metadata_thread");
633 retval = -1;
634 }
635exit_metadata_thread:
636
637 ret = pthread_join(channel_thread, &status);
638 if (ret) {
639 errno = ret;
640 PERROR("pthread_join channel_thread");
641 retval = -1;
642 }
643exit_channel_thread:
644
645exit_metadata_timer_thread:
646
647 ret = pthread_join(health_thread, &status);
648 if (ret) {
649 errno = ret;
650 PERROR("pthread_join health_thread");
651 retval = -1;
652 }
653exit_health_thread:
654
655 utils_close_pipe(health_quit_pipe);
656exit_health_pipe:
657
658exit_init_data:
659 /*
660 * Wait for all pending call_rcu work to complete before tearing
661 * down data structures. call_rcu worker may be trying to
662 * perform lookups in those structures.
663 */
664 rcu_barrier();
665 lttng_consumer_cleanup();
666 /*
667 * Tearing down the metadata timer thread in a
668 * non-fully-symmetric fashion compared to its creation in case
669 * lttng_consumer_cleanup() ends up tearing down timers (which
670 * requires the timer thread to be alive).
671 */
672 if (metadata_timer_thread_online) {
673 /*
674 * Ensure the metadata timer thread exits only after all other
675 * threads are gone, because it is required to perform timer
676 * teardown synchronization.
677 */
678 kill(getpid(), LTTNG_CONSUMER_SIG_EXIT);
679 ret = pthread_join(metadata_timer_thread, &status);
680 if (ret) {
681 errno = ret;
682 PERROR("pthread_join metadata_timer_thread");
683 retval = -1;
684 }
685 ret = consumer_timer_thread_get_channel_monitor_pipe();
686 if (ret >= 0) {
687 ret = close(ret);
688 if (ret) {
689 PERROR("close channel monitor pipe");
690 }
691 }
692 metadata_timer_thread_online = false;
693 }
694 tmp_ctx = the_consumer_context;
695 the_consumer_context = nullptr;
696 cmm_barrier(); /* Clear ctx for signal handler. */
697 lttng_consumer_destroy(tmp_ctx);
698
699 if (health_consumerd) {
700 health_app_destroy(health_consumerd);
701 }
702 /* Ensure all prior call_rcu are done. */
703 rcu_barrier();
704
705 run_as_destroy_worker();
706
707exit_health_consumerd_cleanup:
708exit_options:
709exit_set_signal_handler:
710
711 rcu_unregister_thread();
712
713 if (!retval) {
714 exit(EXIT_SUCCESS);
715 } else {
716 exit(EXIT_FAILURE);
717 }
718}
This page took 0.027918 seconds and 5 git commands to generate.