Typo: catched -> caught
[lttng-tools.git] / src / bin / lttng-consumerd / lttng-consumerd.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
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 *
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.
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>
30 #include <sys/resource.h>
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>
38 #include <sys/mman.h>
39 #include <assert.h>
40 #include <config.h>
41 #include <urcu/compiler.h>
42 #include <ulimit.h>
43
44 #include <common/defaults.h>
45 #include <common/common.h>
46 #include <common/consumer.h>
47 #include <common/consumer-timer.h>
48 #include <common/compat/poll.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/utils.h>
51
52 #include "lttng-consumerd.h"
53 #include "health-consumerd.h"
54
55 /* TODO : support UST (all direct kernel-ctl accesses). */
56
57 /* threads (channel handling, poll, metadata, sessiond) */
58
59 static pthread_t channel_thread, data_thread, metadata_thread,
60 sessiond_thread, metadata_timer_thread, health_thread;
61
62 /* to count the number of times the user pressed ctrl+c */
63 static int sigintcount = 0;
64
65 /* Argument variables */
66 int lttng_opt_quiet; /* not static in error.h */
67 int lttng_opt_verbose; /* not static in error.h */
68 int lttng_opt_mi; /* not static in error.h */
69
70 static int opt_daemon;
71 static const char *progname;
72 static char command_sock_path[PATH_MAX]; /* Global command socket path */
73 static char error_sock_path[PATH_MAX]; /* Global error path */
74 static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
75
76 /* the liblttngconsumerd context */
77 static struct lttng_consumer_local_data *ctx;
78
79 /* Consumerd health monitoring */
80 struct health_app *health_consumerd;
81
82 const char *tracing_group_name = DEFAULT_TRACING_GROUP;
83
84 int lttng_consumer_ready = NR_LTTNG_CONSUMER_READY;
85
86 enum lttng_consumer_type lttng_consumer_get_type(void)
87 {
88 if (!ctx) {
89 return LTTNG_CONSUMER_UNKNOWN;
90 }
91 return ctx->type;
92 }
93
94 /*
95 * Signal handler for the daemon
96 */
97 static void sighandler(int sig)
98 {
99 if (sig == SIGINT && sigintcount++ == 0) {
100 DBG("ignoring first SIGINT");
101 return;
102 }
103
104 /*
105 * Ignore SIGPIPE because it should not stop the consumer whenever a
106 * SIGPIPE is caught through a FD operation.
107 */
108 if (sig == SIGPIPE) {
109 return;
110 }
111
112 if (ctx) {
113 lttng_consumer_should_exit(ctx);
114 }
115 }
116
117 /*
118 * Setup signal handler for :
119 * SIGINT, SIGTERM, SIGPIPE
120 */
121 static 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
153 /*
154 * Usage function on stream file.
155 */
156 static void usage(FILE *fp)
157 {
158 fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname);
159 fprintf(fp, " -h, --help "
160 "Display this usage.\n");
161 fprintf(fp, " -c, --consumerd-cmd-sock PATH "
162 "Specify path for the command socket\n");
163 fprintf(fp, " -e, --consumerd-err-sock PATH "
164 "Specify path for the error socket\n");
165 fprintf(fp, " -d, --daemonize "
166 "Start as a daemon.\n");
167 fprintf(fp, " -q, --quiet "
168 "No output at all.\n");
169 fprintf(fp, " -v, --verbose "
170 "Verbose mode. Activate DBG() macro.\n");
171 fprintf(fp, " -V, --version "
172 "Show version number.\n");
173 fprintf(fp, " -g, --group NAME "
174 "Specify the tracing group name. (default: tracing)\n");
175 fprintf(fp, " -k, --kernel "
176 "Consumer kernel buffers (default).\n");
177 fprintf(fp, " -u, --ust "
178 "Consumer UST buffers.%s\n",
179 #ifdef HAVE_LIBLTTNG_UST_CTL
180 ""
181 #else
182 " (support not compiled in)"
183 #endif
184 );
185 }
186
187 /*
188 * daemon argument parsing
189 */
190 static void parse_args(int argc, char **argv)
191 {
192 int c;
193
194 static struct option long_options[] = {
195 { "consumerd-cmd-sock", 1, 0, 'c' },
196 { "consumerd-err-sock", 1, 0, 'e' },
197 { "daemonize", 0, 0, 'd' },
198 { "group", 1, 0, 'g' },
199 { "help", 0, 0, 'h' },
200 { "quiet", 0, 0, 'q' },
201 { "verbose", 0, 0, 'v' },
202 { "version", 0, 0, 'V' },
203 { "kernel", 0, 0, 'k' },
204 #ifdef HAVE_LIBLTTNG_UST_CTL
205 { "ust", 0, 0, 'u' },
206 #endif
207 { NULL, 0, 0, 0 }
208 };
209
210 while (1) {
211 int option_index = 0;
212 c = getopt_long(argc, argv, "dhqvVku" "c:e:g:", long_options, &option_index);
213 if (c == -1) {
214 break;
215 }
216
217 switch (c) {
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;
233 case 'g':
234 tracing_group_name = optarg;
235 break;
236 case 'h':
237 usage(stdout);
238 exit(EXIT_SUCCESS);
239 case 'q':
240 lttng_opt_quiet = 1;
241 break;
242 case 'v':
243 lttng_opt_verbose = 1;
244 break;
245 case 'V':
246 fprintf(stdout, "%s\n", VERSION);
247 exit(EXIT_SUCCESS);
248 case 'k':
249 opt_type = LTTNG_CONSUMER_KERNEL;
250 break;
251 #ifdef HAVE_LIBLTTNG_UST_CTL
252 case 'u':
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
260 break;
261 #endif
262 default:
263 usage(stderr);
264 exit(EXIT_FAILURE);
265 }
266 }
267 }
268
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 */
273 static 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
288 /*
289 * main
290 */
291 int main(int argc, char **argv)
292 {
293 int ret = 0;
294 void *status;
295 struct lttng_consumer_local_data *tmp_ctx;
296
297 /* Parse arguments */
298 progname = argv[0];
299 parse_args(argc, argv);
300
301 /* Daemonize */
302 if (opt_daemon) {
303 int i;
304
305 /*
306 * fork
307 * child: setsid, close FD 0, 1, 2, chdir /
308 * parent: exit (if fork is successful)
309 */
310 ret = daemon(0, 0);
311 if (ret < 0) {
312 PERROR("daemon");
313 goto error;
314 }
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 }
323 }
324
325 /* Set up max poll set size */
326 lttng_poll_set_max_size();
327
328 if (*command_sock_path == '\0') {
329 switch (opt_type) {
330 case LTTNG_CONSUMER_KERNEL:
331 snprintf(command_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
332 DEFAULT_LTTNG_RUNDIR);
333 break;
334 case LTTNG_CONSUMER64_UST:
335 snprintf(command_sock_path, PATH_MAX,
336 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
337 break;
338 case LTTNG_CONSUMER32_UST:
339 snprintf(command_sock_path, PATH_MAX,
340 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
341 break;
342 default:
343 WARN("Unknown consumerd type");
344 goto error;
345 }
346 }
347
348 /* Init */
349 if (lttng_consumer_init() < 0) {
350 goto error;
351 }
352
353 /* Init socket timeouts */
354 lttcomm_init();
355 lttcomm_inet_init();
356
357 if (!getuid()) {
358 /* Set limit for open files */
359 set_ulimit();
360 }
361
362 health_consumerd = health_app_create(NR_HEALTH_CONSUMERD_TYPES);
363 if (!health_consumerd) {
364 goto error;
365 }
366
367 /* create the consumer instance with and assign the callbacks */
368 ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
369 NULL, lttng_consumer_on_recv_stream, NULL);
370 if (ctx == NULL) {
371 goto error;
372 }
373
374 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
375 if (*error_sock_path == '\0') {
376 switch (opt_type) {
377 case LTTNG_CONSUMER_KERNEL:
378 snprintf(error_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
379 DEFAULT_LTTNG_RUNDIR);
380 break;
381 case LTTNG_CONSUMER64_UST:
382 snprintf(error_sock_path, PATH_MAX,
383 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
384 break;
385 case LTTNG_CONSUMER32_UST:
386 snprintf(error_sock_path, PATH_MAX,
387 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
388 break;
389 default:
390 WARN("Unknown consumerd type");
391 goto error;
392 }
393 }
394
395 if (set_signal_handler() < 0) {
396 goto error;
397 }
398
399 /* Connect to the socket created by lttng-sessiond to report errors */
400 DBG("Connecting to error socket %s", error_sock_path);
401 ret = lttcomm_connect_unix_sock(error_sock_path);
402 /* not a fatal error, but all communication with lttng-sessiond will fail */
403 if (ret < 0) {
404 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
405 }
406 lttng_consumer_set_error_sock(ctx, ret);
407
408 /*
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.
411 */
412 consumer_signal_init();
413
414 ctx->type = opt_type;
415
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
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)) {
434 usleep(100000);
435 }
436 cmm_smp_mb(); /* Read ready before following operations */
437
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");
443 goto channel_error;
444 }
445
446 /* Create thread to manage the polling/writing of trace metadata */
447 ret = pthread_create(&metadata_thread, NULL, consumer_thread_metadata_poll,
448 (void *) ctx);
449 if (ret != 0) {
450 perror("pthread_create");
451 goto metadata_error;
452 }
453
454 /* Create thread to manage the polling/writing of trace data */
455 ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll,
456 (void *) ctx);
457 if (ret != 0) {
458 perror("pthread_create");
459 goto data_error;
460 }
461
462 /* Create the thread to manage the receive of fd */
463 ret = pthread_create(&sessiond_thread, NULL, consumer_thread_sessiond_poll,
464 (void *) ctx);
465 if (ret != 0) {
466 perror("pthread_create");
467 goto sessiond_error;
468 }
469
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 }
480
481 ret = pthread_detach(metadata_timer_thread);
482 if (ret) {
483 errno = ret;
484 perror("pthread_detach");
485 }
486
487 metadata_timer_error:
488 ret = pthread_join(sessiond_thread, &status);
489 if (ret != 0) {
490 perror("pthread_join");
491 goto error;
492 }
493
494 sessiond_error:
495 ret = pthread_join(data_thread, &status);
496 if (ret != 0) {
497 perror("pthread_join");
498 goto error;
499 }
500
501 data_error:
502 ret = pthread_join(metadata_thread, &status);
503 if (ret != 0) {
504 perror("pthread_join");
505 goto error;
506 }
507
508 metadata_error:
509 ret = pthread_join(channel_thread, &status);
510 if (ret != 0) {
511 perror("pthread_join");
512 goto error;
513 }
514
515 channel_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
522 health_error:
523 utils_close_pipe(health_quit_pipe);
524
525 error_health_pipe:
526 if (!ret) {
527 ret = EXIT_SUCCESS;
528 goto end;
529 }
530
531 error:
532 ret = EXIT_FAILURE;
533
534 end:
535 tmp_ctx = ctx;
536 ctx = NULL;
537 cmm_barrier(); /* Clear ctx for signal handler. */
538 lttng_consumer_destroy(tmp_ctx);
539 lttng_consumer_cleanup();
540 if (health_consumerd) {
541 health_app_destroy(health_consumerd);
542 }
543
544 /* Ensure all prior call_rcu are done. */
545 rcu_barrier();
546 return ret;
547 }
This page took 0.04127 seconds and 5 git commands to generate.