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