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