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