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