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