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