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