Run clang-format on the whole tree
[lttng-tools.git] / src / bin / lttng-consumerd / lttng-consumerd.cpp
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
46 static pthread_t channel_thread, data_thread, metadata_thread, sessiond_thread,
47 metadata_timer_thread, health_thread;
48 static bool metadata_timer_thread_online;
49
50 /* to count the number of times the user pressed ctrl+c */
51 static int sigintcount = 0;
52
53 /* Argument variables */
54 int lttng_opt_quiet; /* not static in error.h */
55 int lttng_opt_verbose; /* not static in error.h */
56 int lttng_opt_mi; /* not static in error.h */
57
58 static int opt_daemon;
59 static const char *progname;
60 static char command_sock_path[PATH_MAX]; /* Global command socket path */
61 static char error_sock_path[PATH_MAX]; /* Global error path */
62 static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
63
64 /* the liblttngconsumerd context */
65 static struct lttng_consumer_local_data *the_consumer_context;
66
67 /* Consumerd health monitoring */
68 struct health_app *health_consumerd;
69
70 const char *tracing_group_name = DEFAULT_TRACING_GROUP;
71
72 int lttng_consumer_ready = NR_LTTNG_CONSUMER_READY;
73
74 enum 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 */
85 static 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 */
118 static int set_signal_handler(void)
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, NULL)) < 0) {
134 PERROR("sigaction");
135 return ret;
136 }
137
138 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
139 PERROR("sigaction");
140 return ret;
141 }
142
143 if ((ret = sigaction(SIGBUS, &sa, NULL)) < 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, NULL)) < 0) {
151 PERROR("sigaction");
152 return ret;
153 }
154
155 return ret;
156 }
157
158 /*
159 * Usage function on stream file.
160 */
161 static 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 */
205 static int parse_args(int argc, char **argv)
206 {
207 int c, ret = 0;
208
209 static struct option long_options[] = { { "consumerd-cmd-sock", 1, 0, 'c' },
210 { "consumerd-err-sock", 1, 0, 'e' },
211 { "daemonize", 0, 0, 'd' },
212 { "group", 1, 0, 'g' },
213 { "help", 0, 0, 'h' },
214 { "quiet", 0, 0, 'q' },
215 { "verbose", 0, 0, 'v' },
216 { "version", 0, 0, 'V' },
217 { "kernel", 0, 0, 'k' },
218 #ifdef HAVE_LIBLTTNG_UST_CTL
219 { "ust", 0, 0, 'u' },
220 #endif
221 { NULL, 0, 0, 0 } };
222
223 while (1) {
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 }
303 end:
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 */
311 static void set_ulimit(void)
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 */
329 int 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], NULL, NULL) < 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(
446 opt_type, lttng_consumer_read_subbuffer, NULL, lttng_consumer_on_recv_stream, NULL);
447 if (!the_consumer_context) {
448 retval = -1;
449 goto exit_init_data;
450 }
451
452 lttng_consumer_set_command_sock_path(the_consumer_context, command_sock_path);
453 if (*error_sock_path == '\0') {
454 switch (opt_type) {
455 case LTTNG_CONSUMER_KERNEL:
456 ret = snprintf(error_sock_path,
457 PATH_MAX,
458 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
459 DEFAULT_LTTNG_RUNDIR);
460 if (ret < 0) {
461 retval = -1;
462 goto exit_init_data;
463 }
464 break;
465 case LTTNG_CONSUMER64_UST:
466 ret = snprintf(error_sock_path,
467 PATH_MAX,
468 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
469 DEFAULT_LTTNG_RUNDIR);
470 if (ret < 0) {
471 retval = -1;
472 goto exit_init_data;
473 }
474 break;
475 case LTTNG_CONSUMER32_UST:
476 ret = snprintf(error_sock_path,
477 PATH_MAX,
478 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
479 DEFAULT_LTTNG_RUNDIR);
480 if (ret < 0) {
481 retval = -1;
482 goto exit_init_data;
483 }
484 break;
485 default:
486 ERR("Unknown consumerd type");
487 retval = -1;
488 goto exit_init_data;
489 }
490 }
491
492 /* Connect to the socket created by lttng-sessiond to report errors */
493 DBG("Connecting to error socket %s", error_sock_path);
494 ret = lttcomm_connect_unix_sock(error_sock_path);
495 /*
496 * Not a fatal error, but all communication with lttng-sessiond will
497 * fail.
498 */
499 if (ret < 0) {
500 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
501 }
502 lttng_consumer_set_error_sock(the_consumer_context, ret);
503
504 /*
505 * Block RT signals used for UST periodical metadata flush and the live
506 * timer in main, and create a dedicated thread to handle these signals.
507 */
508 if (consumer_signal_init()) {
509 retval = -1;
510 goto exit_init_data;
511 }
512
513 the_consumer_context->type = opt_type;
514
515 if (utils_create_pipe(health_quit_pipe)) {
516 retval = -1;
517 goto exit_health_pipe;
518 }
519
520 /* Create thread to manage the client socket */
521 ret = pthread_create(&health_thread,
522 default_pthread_attr(),
523 thread_manage_health_consumerd,
524 (void *) NULL);
525 if (ret) {
526 errno = ret;
527 PERROR("pthread_create health");
528 retval = -1;
529 goto exit_health_thread;
530 }
531
532 /*
533 * Wait for health thread to be initialized before letting the
534 * sessiond thread reply to the sessiond that we are ready.
535 */
536 while (uatomic_read(&lttng_consumer_ready)) {
537 usleep(100000);
538 }
539 cmm_smp_mb(); /* Read ready before following operations */
540
541 /*
542 * Create the thread to manage the UST metadata periodic timer and
543 * live timer.
544 */
545 ret = pthread_create(
546 &metadata_timer_thread, NULL, consumer_timer_thread, (void *) the_consumer_context);
547 if (ret) {
548 errno = ret;
549 PERROR("pthread_create");
550 retval = -1;
551 goto exit_metadata_timer_thread;
552 }
553 metadata_timer_thread_online = true;
554
555 /* Create thread to manage channels */
556 ret = pthread_create(&channel_thread,
557 default_pthread_attr(),
558 consumer_thread_channel_poll,
559 (void *) the_consumer_context);
560 if (ret) {
561 errno = ret;
562 PERROR("pthread_create");
563 retval = -1;
564 goto exit_channel_thread;
565 }
566
567 /* Create thread to manage the polling/writing of trace metadata */
568 ret = pthread_create(&metadata_thread,
569 default_pthread_attr(),
570 consumer_thread_metadata_poll,
571 (void *) the_consumer_context);
572 if (ret) {
573 errno = ret;
574 PERROR("pthread_create");
575 retval = -1;
576 goto exit_metadata_thread;
577 }
578
579 /* Create thread to manage the polling/writing of trace data */
580 ret = pthread_create(&data_thread,
581 default_pthread_attr(),
582 consumer_thread_data_poll,
583 (void *) the_consumer_context);
584 if (ret) {
585 errno = ret;
586 PERROR("pthread_create");
587 retval = -1;
588 goto exit_data_thread;
589 }
590
591 /* Create the thread to manage the reception of fds */
592 ret = pthread_create(&sessiond_thread,
593 default_pthread_attr(),
594 consumer_thread_sessiond_poll,
595 (void *) the_consumer_context);
596 if (ret) {
597 errno = ret;
598 PERROR("pthread_create");
599 retval = -1;
600 goto exit_sessiond_thread;
601 }
602
603 /*
604 * This is where we start awaiting program completion (e.g. through
605 * signal that asks threads to teardown.
606 */
607
608 ret = pthread_join(sessiond_thread, &status);
609 if (ret) {
610 errno = ret;
611 PERROR("pthread_join sessiond_thread");
612 retval = -1;
613 }
614 exit_sessiond_thread:
615
616 ret = pthread_join(data_thread, &status);
617 if (ret) {
618 errno = ret;
619 PERROR("pthread_join data_thread");
620 retval = -1;
621 }
622 exit_data_thread:
623
624 ret = pthread_join(metadata_thread, &status);
625 if (ret) {
626 errno = ret;
627 PERROR("pthread_join metadata_thread");
628 retval = -1;
629 }
630 exit_metadata_thread:
631
632 ret = pthread_join(channel_thread, &status);
633 if (ret) {
634 errno = ret;
635 PERROR("pthread_join channel_thread");
636 retval = -1;
637 }
638 exit_channel_thread:
639
640 exit_metadata_timer_thread:
641
642 ret = pthread_join(health_thread, &status);
643 if (ret) {
644 errno = ret;
645 PERROR("pthread_join health_thread");
646 retval = -1;
647 }
648 exit_health_thread:
649
650 utils_close_pipe(health_quit_pipe);
651 exit_health_pipe:
652
653 exit_init_data:
654 /*
655 * Wait for all pending call_rcu work to complete before tearing
656 * down data structures. call_rcu worker may be trying to
657 * perform lookups in those structures.
658 */
659 rcu_barrier();
660 lttng_consumer_cleanup();
661 /*
662 * Tearing down the metadata timer thread in a
663 * non-fully-symmetric fashion compared to its creation in case
664 * lttng_consumer_cleanup() ends up tearing down timers (which
665 * requires the timer thread to be alive).
666 */
667 if (metadata_timer_thread_online) {
668 /*
669 * Ensure the metadata timer thread exits only after all other
670 * threads are gone, because it is required to perform timer
671 * teardown synchronization.
672 */
673 kill(getpid(), LTTNG_CONSUMER_SIG_EXIT);
674 ret = pthread_join(metadata_timer_thread, &status);
675 if (ret) {
676 errno = ret;
677 PERROR("pthread_join metadata_timer_thread");
678 retval = -1;
679 }
680 ret = consumer_timer_thread_get_channel_monitor_pipe();
681 if (ret >= 0) {
682 ret = close(ret);
683 if (ret) {
684 PERROR("close channel monitor pipe");
685 }
686 }
687 metadata_timer_thread_online = false;
688 }
689 tmp_ctx = the_consumer_context;
690 the_consumer_context = NULL;
691 cmm_barrier(); /* Clear ctx for signal handler. */
692 lttng_consumer_destroy(tmp_ctx);
693
694 if (health_consumerd) {
695 health_app_destroy(health_consumerd);
696 }
697 /* Ensure all prior call_rcu are done. */
698 rcu_barrier();
699
700 run_as_destroy_worker();
701
702 exit_health_consumerd_cleanup:
703 exit_options:
704 exit_set_signal_handler:
705
706 rcu_unregister_thread();
707
708 if (!retval) {
709 exit(EXIT_SUCCESS);
710 } else {
711 exit(EXIT_FAILURE);
712 }
713 }
This page took 0.043119 seconds and 5 git commands to generate.