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