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