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