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