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