Refactor consumerd main/cleanup
[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 #define _LGPL_SOURCE
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>
31 #include <sys/resource.h>
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>
39 #include <sys/mman.h>
40 #include <assert.h>
41 #include <config.h>
42 #include <urcu/compiler.h>
43 #include <ulimit.h>
44
45 #include <common/defaults.h>
46 #include <common/common.h>
47 #include <common/consumer.h>
48 #include <common/consumer-timer.h>
49 #include <common/compat/poll.h>
50 #include <common/sessiond-comm/sessiond-comm.h>
51 #include <common/utils.h>
52
53 #include "lttng-consumerd.h"
54 #include "health-consumerd.h"
55
56 /* threads (channel handling, poll, metadata, sessiond) */
57
58 static pthread_t channel_thread, data_thread, metadata_thread,
59 sessiond_thread, metadata_timer_thread, health_thread;
60
61 /* to count the number of times the user pressed ctrl+c */
62 static int sigintcount = 0;
63
64 /* Argument variables */
65 int lttng_opt_quiet; /* not static in error.h */
66 int lttng_opt_verbose; /* not static in error.h */
67 int lttng_opt_mi; /* not static in error.h */
68
69 static int opt_daemon;
70 static const char *progname;
71 static char command_sock_path[PATH_MAX]; /* Global command socket path */
72 static char error_sock_path[PATH_MAX]; /* Global error path */
73 static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
74
75 /* the liblttngconsumerd context */
76 static struct lttng_consumer_local_data *ctx;
77
78 /* Consumerd health monitoring */
79 struct health_app *health_consumerd;
80
81 const char *tracing_group_name = DEFAULT_TRACING_GROUP;
82
83 int lttng_consumer_ready = NR_LTTNG_CONSUMER_READY;
84
85 enum lttng_consumer_type lttng_consumer_get_type(void)
86 {
87 if (!ctx) {
88 return LTTNG_CONSUMER_UNKNOWN;
89 }
90 return ctx->type;
91 }
92
93 /*
94 * Signal handler for the daemon
95 */
96 static void sighandler(int sig)
97 {
98 if (sig == SIGINT && sigintcount++ == 0) {
99 DBG("ignoring first SIGINT");
100 return;
101 }
102
103 /*
104 * Ignore SIGPIPE because it should not stop the consumer whenever a
105 * SIGPIPE is catched through a FD operation.
106 */
107 if (sig == SIGPIPE) {
108 return;
109 }
110
111 lttng_consumer_should_exit(ctx);
112 }
113
114 /*
115 * Setup signal handler for :
116 * SIGINT, SIGTERM, SIGPIPE
117 */
118 static int set_signal_handler(void)
119 {
120 int ret = 0;
121 struct sigaction sa;
122 sigset_t sigset;
123
124 if ((ret = sigemptyset(&sigset)) < 0) {
125 PERROR("sigemptyset");
126 return ret;
127 }
128
129 sa.sa_handler = sighandler;
130 sa.sa_mask = sigset;
131 sa.sa_flags = 0;
132 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
133 PERROR("sigaction");
134 return ret;
135 }
136
137 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
138 PERROR("sigaction");
139 return ret;
140 }
141
142 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
143 PERROR("sigaction");
144 return ret;
145 }
146
147 return ret;
148 }
149
150 /*
151 * Usage function on stream file.
152 */
153 static void usage(FILE *fp)
154 {
155 fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname);
156 fprintf(fp, " -h, --help "
157 "Display this usage.\n");
158 fprintf(fp, " -c, --consumerd-cmd-sock PATH "
159 "Specify path for the command socket\n");
160 fprintf(fp, " -e, --consumerd-err-sock PATH "
161 "Specify path for the error socket\n");
162 fprintf(fp, " -d, --daemonize "
163 "Start as a daemon.\n");
164 fprintf(fp, " -q, --quiet "
165 "No output at all.\n");
166 fprintf(fp, " -v, --verbose "
167 "Verbose mode. Activate DBG() macro.\n");
168 fprintf(fp, " -V, --version "
169 "Show version number.\n");
170 fprintf(fp, " -g, --group NAME "
171 "Specify the tracing group name. (default: tracing)\n");
172 fprintf(fp, " -k, --kernel "
173 "Consumer kernel buffers (default).\n");
174 fprintf(fp, " -u, --ust "
175 "Consumer UST buffers.%s\n",
176 #ifdef HAVE_LIBLTTNG_UST_CTL
177 ""
178 #else
179 " (support not compiled in)"
180 #endif
181 );
182 }
183
184 /*
185 * daemon argument parsing
186 */
187 static int parse_args(int argc, char **argv)
188 {
189 int c, ret = 0;
190
191 static struct option long_options[] = {
192 { "consumerd-cmd-sock", 1, 0, 'c' },
193 { "consumerd-err-sock", 1, 0, 'e' },
194 { "daemonize", 0, 0, 'd' },
195 { "group", 1, 0, 'g' },
196 { "help", 0, 0, 'h' },
197 { "quiet", 0, 0, 'q' },
198 { "verbose", 0, 0, 'v' },
199 { "version", 0, 0, 'V' },
200 { "kernel", 0, 0, 'k' },
201 #ifdef HAVE_LIBLTTNG_UST_CTL
202 { "ust", 0, 0, 'u' },
203 #endif
204 { NULL, 0, 0, 0 }
205 };
206
207 while (1) {
208 int option_index = 0;
209 c = getopt_long(argc, argv, "dhqvVku" "c:e:g:",
210 long_options, &option_index);
211 if (c == -1) {
212 break;
213 }
214
215 switch (c) {
216 case 0:
217 fprintf(stderr, "option %s",
218 long_options[option_index].name);
219 if (optarg) {
220 fprintf(stderr, " with arg %s\n", optarg);
221 ret = -1;
222 goto end;
223 }
224 break;
225 case 'c':
226 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
227 break;
228 case 'e':
229 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
230 break;
231 case 'd':
232 opt_daemon = 1;
233 break;
234 case 'g':
235 tracing_group_name = optarg;
236 break;
237 case 'h':
238 usage(stdout);
239 exit(EXIT_SUCCESS);
240 case 'q':
241 lttng_opt_quiet = 1;
242 break;
243 case 'v':
244 lttng_opt_verbose = 1;
245 break;
246 case 'V':
247 fprintf(stdout, "%s\n", VERSION);
248 exit(EXIT_SUCCESS);
249 case 'k':
250 opt_type = LTTNG_CONSUMER_KERNEL;
251 break;
252 #ifdef HAVE_LIBLTTNG_UST_CTL
253 case 'u':
254 # if (CAA_BITS_PER_LONG == 64)
255 opt_type = LTTNG_CONSUMER64_UST;
256 # elif (CAA_BITS_PER_LONG == 32)
257 opt_type = LTTNG_CONSUMER32_UST;
258 # else
259 # error "Unknown bitness"
260 # endif
261 break;
262 #endif
263 default:
264 usage(stderr);
265 ret = -1;
266 goto end;
267 }
268 }
269 end:
270 return ret;
271 }
272
273 /*
274 * Set open files limit to unlimited. This daemon can open a large number of
275 * file descriptors in order to consumer multiple kernel traces.
276 */
277 static void set_ulimit(void)
278 {
279 int ret;
280 struct rlimit lim;
281
282 /* The kernel does not allowed an infinite limit for open files */
283 lim.rlim_cur = 65535;
284 lim.rlim_max = 65535;
285
286 ret = setrlimit(RLIMIT_NOFILE, &lim);
287 if (ret < 0) {
288 PERROR("failed to set open files limit");
289 }
290 }
291
292 /*
293 * main
294 */
295 int main(int argc, char **argv)
296 {
297 int ret = 0, retval = 0;
298 void *status;
299
300 if (set_signal_handler()) {
301 retval = -1;
302 goto exit_set_signal_handler;
303 }
304
305 /* Parse arguments */
306 progname = argv[0];
307 if (parse_args(argc, argv)) {
308 retval = -1;
309 goto exit_options;
310 }
311
312 /* Daemonize */
313 if (opt_daemon) {
314 int i;
315
316 /*
317 * fork
318 * child: setsid, close FD 0, 1, 2, chdir /
319 * parent: exit (if fork is successful)
320 */
321 ret = daemon(0, 0);
322 if (ret < 0) {
323 PERROR("daemon");
324 retval = -1;
325 goto exit_options;
326 }
327 /*
328 * We are in the child. Make sure all other file
329 * descriptors are closed, in case we are called with
330 * more opened file descriptors than the standard ones.
331 */
332 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
333 (void) close(i);
334 }
335 }
336
337 /*
338 * Starting from here, we can create threads. This needs to be after
339 * lttng_daemonize due to RCU.
340 */
341
342 health_consumerd = health_app_create(NR_HEALTH_CONSUMERD_TYPES);
343 if (!health_consumerd) {
344 retval = -1;
345 goto exit_health_consumerd_cleanup;
346 }
347
348 /* Set up max poll set size */
349 lttng_poll_set_max_size();
350
351 if (*command_sock_path == '\0') {
352 switch (opt_type) {
353 case LTTNG_CONSUMER_KERNEL:
354 ret = snprintf(command_sock_path, PATH_MAX,
355 DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
356 DEFAULT_LTTNG_RUNDIR);
357 if (ret < 0) {
358 retval = -1;
359 goto exit_init_data;
360 }
361 break;
362 case LTTNG_CONSUMER64_UST:
363 ret = snprintf(command_sock_path, PATH_MAX,
364 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
365 DEFAULT_LTTNG_RUNDIR);
366 if (ret < 0) {
367 retval = -1;
368 goto exit_init_data;
369 }
370 break;
371 case LTTNG_CONSUMER32_UST:
372 ret = snprintf(command_sock_path, PATH_MAX,
373 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
374 DEFAULT_LTTNG_RUNDIR);
375 if (ret < 0) {
376 retval = -1;
377 goto exit_init_data;
378 }
379 break;
380 default:
381 ERR("Unknown consumerd type");
382 retval = -1;
383 goto exit_init_data;
384 }
385 }
386
387 /* Init */
388 if (lttng_consumer_init()) {
389 retval = -1;
390 goto exit_init_data;
391 }
392
393 /* Initialize communication library */
394 lttcomm_init();
395 /* Initialize TCP timeout values */
396 lttcomm_inet_init();
397
398 if (!getuid()) {
399 /* Set limit for open files */
400 set_ulimit();
401 }
402
403 /* create the consumer instance with and assign the callbacks */
404 ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
405 NULL, lttng_consumer_on_recv_stream, NULL);
406 if (!ctx) {
407 retval = -1;
408 goto exit_init_data;
409 }
410
411 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
412 if (*error_sock_path == '\0') {
413 switch (opt_type) {
414 case LTTNG_CONSUMER_KERNEL:
415 ret = snprintf(error_sock_path, PATH_MAX,
416 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
417 DEFAULT_LTTNG_RUNDIR);
418 if (ret < 0) {
419 retval = -1;
420 goto exit_init_data;
421 }
422 break;
423 case LTTNG_CONSUMER64_UST:
424 ret = snprintf(error_sock_path, PATH_MAX,
425 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
426 DEFAULT_LTTNG_RUNDIR);
427 if (ret < 0) {
428 retval = -1;
429 goto exit_init_data;
430 }
431 break;
432 case LTTNG_CONSUMER32_UST:
433 ret = snprintf(error_sock_path, PATH_MAX,
434 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
435 DEFAULT_LTTNG_RUNDIR);
436 if (ret < 0) {
437 retval = -1;
438 goto exit_init_data;
439 }
440 break;
441 default:
442 ERR("Unknown consumerd type");
443 retval = -1;
444 goto exit_init_data;
445 }
446 }
447
448 /* Connect to the socket created by lttng-sessiond to report errors */
449 DBG("Connecting to error socket %s", error_sock_path);
450 ret = lttcomm_connect_unix_sock(error_sock_path);
451 /*
452 * Not a fatal error, but all communication with lttng-sessiond will
453 * fail.
454 */
455 if (ret < 0) {
456 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
457 }
458 lttng_consumer_set_error_sock(ctx, ret);
459
460 /*
461 * Block RT signals used for UST periodical metadata flush and the live
462 * timer in main, and create a dedicated thread to handle these signals.
463 */
464 if (consumer_signal_init()) {
465 retval = -1;
466 goto exit_init_data;
467 }
468
469 ctx->type = opt_type;
470
471 if (utils_create_pipe(health_quit_pipe)) {
472 retval = -1;
473 goto exit_health_pipe;
474 }
475
476 /* Create thread to manage the client socket */
477 ret = pthread_create(&health_thread, NULL,
478 thread_manage_health, (void *) NULL);
479 if (ret) {
480 errno = ret;
481 PERROR("pthread_create health");
482 retval = -1;
483 goto exit_health_thread;
484 }
485
486 /*
487 * Wait for health thread to be initialized before letting the
488 * sessiond thread reply to the sessiond that we are ready.
489 */
490 while (uatomic_read(&lttng_consumer_ready)) {
491 usleep(100000);
492 }
493 cmm_smp_mb(); /* Read ready before following operations */
494
495 /* Create thread to manage channels */
496 ret = pthread_create(&channel_thread, NULL,
497 consumer_thread_channel_poll,
498 (void *) ctx);
499 if (ret) {
500 errno = ret;
501 PERROR("pthread_create");
502 retval = -1;
503 goto exit_channel_thread;
504 }
505
506 /* Create thread to manage the polling/writing of trace metadata */
507 ret = pthread_create(&metadata_thread, NULL,
508 consumer_thread_metadata_poll,
509 (void *) ctx);
510 if (ret) {
511 errno = ret;
512 PERROR("pthread_create");
513 retval = -1;
514 goto exit_metadata_thread;
515 }
516
517 /* Create thread to manage the polling/writing of trace data */
518 ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll,
519 (void *) ctx);
520 if (ret) {
521 errno = ret;
522 PERROR("pthread_create");
523 retval = -1;
524 goto exit_data_thread;
525 }
526
527 /* Create the thread to manage the receive of fd */
528 ret = pthread_create(&sessiond_thread, NULL,
529 consumer_thread_sessiond_poll,
530 (void *) ctx);
531 if (ret) {
532 errno = ret;
533 PERROR("pthread_create");
534 retval = -1;
535 goto exit_sessiond_thread;
536 }
537
538 /*
539 * Create the thread to manage the UST metadata periodic timer and
540 * live timer.
541 */
542 ret = pthread_create(&metadata_timer_thread, NULL,
543 consumer_timer_thread, (void *) ctx);
544 if (ret) {
545 errno = ret;
546 PERROR("pthread_create");
547 retval = -1;
548 goto exit_metadata_timer_thread;
549 }
550
551 ret = pthread_detach(metadata_timer_thread);
552 if (ret) {
553 errno = ret;
554 PERROR("pthread_detach");
555 retval = -1;
556 goto exit_metadata_timer_detach;
557 }
558
559 /*
560 * This is where we start awaiting program completion (e.g. through
561 * signal that asks threads to teardown.
562 */
563
564 exit_metadata_timer_detach:
565 exit_metadata_timer_thread:
566 ret = pthread_join(sessiond_thread, &status);
567 if (ret) {
568 errno = ret;
569 PERROR("pthread_join sessiond_thread");
570 retval = -1;
571 }
572 exit_sessiond_thread:
573
574 ret = pthread_join(data_thread, &status);
575 if (ret) {
576 errno = ret;
577 PERROR("pthread_join data_thread");
578 retval = -1;
579 }
580 exit_data_thread:
581
582 ret = pthread_join(metadata_thread, &status);
583 if (ret) {
584 errno = ret;
585 PERROR("pthread_join metadata_thread");
586 retval = -1;
587 }
588 exit_metadata_thread:
589
590 ret = pthread_join(channel_thread, &status);
591 if (ret) {
592 errno = ret;
593 PERROR("pthread_join channel_thread");
594 retval = -1;
595 }
596 exit_channel_thread:
597
598 ret = pthread_join(health_thread, &status);
599 if (ret) {
600 errno = ret;
601 PERROR("pthread_join health_thread");
602 retval = -1;
603 }
604 exit_health_thread:
605
606 utils_close_pipe(health_quit_pipe);
607 exit_health_pipe:
608
609 exit_init_data:
610 lttng_consumer_destroy(ctx);
611 lttng_consumer_cleanup();
612
613 if (health_consumerd) {
614 health_app_destroy(health_consumerd);
615 }
616 exit_health_consumerd_cleanup:
617
618 exit_options:
619
620 exit_set_signal_handler:
621 if (!retval) {
622 exit(EXIT_SUCCESS);
623 } else {
624 exit(EXIT_FAILURE);
625 }
626 }
This page took 0.044516 seconds and 6 git commands to generate.