Fix: libc internal mutex races with run_as
[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/compat/getenv.h>
51 #include <common/sessiond-comm/sessiond-comm.h>
52 #include <common/utils.h>
53
54 #include "lttng-consumerd.h"
55 #include "health-consumerd.h"
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 catched 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 int parse_args(int argc, char **argv)
191 {
192 int c, ret = 0;
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:",
213 long_options, &option_index);
214 if (c == -1) {
215 break;
216 }
217
218 switch (c) {
219 case 0:
220 fprintf(stderr, "option %s",
221 long_options[option_index].name);
222 if (optarg) {
223 fprintf(stderr, " with arg %s\n", optarg);
224 ret = -1;
225 goto end;
226 }
227 break;
228 case 'c':
229 if (lttng_is_setuid_setgid()) {
230 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
231 "-c, --consumerd-cmd-sock");
232 } else {
233 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
234 }
235 break;
236 case 'e':
237 if (lttng_is_setuid_setgid()) {
238 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
239 "-e, --consumerd-err-sock");
240 } else {
241 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
242 }
243 break;
244 case 'd':
245 opt_daemon = 1;
246 break;
247 case 'g':
248 if (lttng_is_setuid_setgid()) {
249 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
250 "-g, --group");
251 } else {
252 tracing_group_name = optarg;
253 }
254 break;
255 case 'h':
256 usage(stdout);
257 exit(EXIT_SUCCESS);
258 case 'q':
259 lttng_opt_quiet = 1;
260 break;
261 case 'v':
262 lttng_opt_verbose = 1;
263 break;
264 case 'V':
265 fprintf(stdout, "%s\n", VERSION);
266 exit(EXIT_SUCCESS);
267 case 'k':
268 opt_type = LTTNG_CONSUMER_KERNEL;
269 break;
270 #ifdef HAVE_LIBLTTNG_UST_CTL
271 case 'u':
272 # if (CAA_BITS_PER_LONG == 64)
273 opt_type = LTTNG_CONSUMER64_UST;
274 # elif (CAA_BITS_PER_LONG == 32)
275 opt_type = LTTNG_CONSUMER32_UST;
276 # else
277 # error "Unknown bitness"
278 # endif
279 break;
280 #endif
281 default:
282 usage(stderr);
283 ret = -1;
284 goto end;
285 }
286 }
287 end:
288 return ret;
289 }
290
291 /*
292 * Set open files limit to unlimited. This daemon can open a large number of
293 * file descriptors in order to consumer multiple kernel traces.
294 */
295 static void set_ulimit(void)
296 {
297 int ret;
298 struct rlimit lim;
299
300 /* The kernel does not allowed an infinite limit for open files */
301 lim.rlim_cur = 65535;
302 lim.rlim_max = 65535;
303
304 ret = setrlimit(RLIMIT_NOFILE, &lim);
305 if (ret < 0) {
306 PERROR("failed to set open files limit");
307 }
308 }
309
310 /*
311 * main
312 */
313 int main(int argc, char **argv)
314 {
315 int ret = 0, retval = 0;
316 void *status;
317 struct lttng_consumer_local_data *tmp_ctx;
318
319 if (set_signal_handler()) {
320 retval = -1;
321 goto exit_set_signal_handler;
322 }
323
324 /* Parse arguments */
325 progname = argv[0];
326 if (parse_args(argc, argv)) {
327 retval = -1;
328 goto exit_options;
329 }
330
331 /* Daemonize */
332 if (opt_daemon) {
333 int i;
334
335 /*
336 * fork
337 * child: setsid, close FD 0, 1, 2, chdir /
338 * parent: exit (if fork is successful)
339 */
340 ret = daemon(0, 0);
341 if (ret < 0) {
342 PERROR("daemon");
343 retval = -1;
344 goto exit_options;
345 }
346 /*
347 * We are in the child. Make sure all other file
348 * descriptors are closed, in case we are called with
349 * more opened file descriptors than the standard ones.
350 */
351 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
352 (void) close(i);
353 }
354 }
355
356 /*
357 * Starting from here, we can create threads. This needs to be after
358 * lttng_daemonize due to RCU.
359 */
360
361 health_consumerd = health_app_create(NR_HEALTH_CONSUMERD_TYPES);
362 if (!health_consumerd) {
363 retval = -1;
364 goto exit_health_consumerd_cleanup;
365 }
366
367 /* Set up max poll set size */
368 if (lttng_poll_set_max_size()) {
369 retval = -1;
370 goto exit_init_data;
371 }
372
373 if (*command_sock_path == '\0') {
374 switch (opt_type) {
375 case LTTNG_CONSUMER_KERNEL:
376 ret = snprintf(command_sock_path, PATH_MAX,
377 DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
378 DEFAULT_LTTNG_RUNDIR);
379 if (ret < 0) {
380 retval = -1;
381 goto exit_init_data;
382 }
383 break;
384 case LTTNG_CONSUMER64_UST:
385 ret = snprintf(command_sock_path, PATH_MAX,
386 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
387 DEFAULT_LTTNG_RUNDIR);
388 if (ret < 0) {
389 retval = -1;
390 goto exit_init_data;
391 }
392 break;
393 case LTTNG_CONSUMER32_UST:
394 ret = snprintf(command_sock_path, PATH_MAX,
395 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
396 DEFAULT_LTTNG_RUNDIR);
397 if (ret < 0) {
398 retval = -1;
399 goto exit_init_data;
400 }
401 break;
402 default:
403 ERR("Unknown consumerd type");
404 retval = -1;
405 goto exit_init_data;
406 }
407 }
408
409 /* Init */
410 if (lttng_consumer_init()) {
411 retval = -1;
412 goto exit_init_data;
413 }
414
415 /* Initialize communication library */
416 lttcomm_init();
417 /* Initialize TCP timeout values */
418 lttcomm_inet_init();
419
420 if (!getuid()) {
421 /* Set limit for open files */
422 set_ulimit();
423 }
424
425 if (run_as_create_worker(argv[0]) < 0) {
426 goto exit_init_data;
427 }
428
429 /* create the consumer instance with and assign the callbacks */
430 ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
431 NULL, lttng_consumer_on_recv_stream, NULL);
432 if (!ctx) {
433 retval = -1;
434 goto exit_init_data;
435 }
436
437 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
438 if (*error_sock_path == '\0') {
439 switch (opt_type) {
440 case LTTNG_CONSUMER_KERNEL:
441 ret = snprintf(error_sock_path, PATH_MAX,
442 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
443 DEFAULT_LTTNG_RUNDIR);
444 if (ret < 0) {
445 retval = -1;
446 goto exit_init_data;
447 }
448 break;
449 case LTTNG_CONSUMER64_UST:
450 ret = snprintf(error_sock_path, PATH_MAX,
451 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
452 DEFAULT_LTTNG_RUNDIR);
453 if (ret < 0) {
454 retval = -1;
455 goto exit_init_data;
456 }
457 break;
458 case LTTNG_CONSUMER32_UST:
459 ret = snprintf(error_sock_path, PATH_MAX,
460 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
461 DEFAULT_LTTNG_RUNDIR);
462 if (ret < 0) {
463 retval = -1;
464 goto exit_init_data;
465 }
466 break;
467 default:
468 ERR("Unknown consumerd type");
469 retval = -1;
470 goto exit_init_data;
471 }
472 }
473
474 /* Connect to the socket created by lttng-sessiond to report errors */
475 DBG("Connecting to error socket %s", error_sock_path);
476 ret = lttcomm_connect_unix_sock(error_sock_path);
477 /*
478 * Not a fatal error, but all communication with lttng-sessiond will
479 * fail.
480 */
481 if (ret < 0) {
482 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
483 }
484 lttng_consumer_set_error_sock(ctx, ret);
485
486 /*
487 * Block RT signals used for UST periodical metadata flush and the live
488 * timer in main, and create a dedicated thread to handle these signals.
489 */
490 if (consumer_signal_init()) {
491 retval = -1;
492 goto exit_init_data;
493 }
494
495 ctx->type = opt_type;
496
497 if (utils_create_pipe(health_quit_pipe)) {
498 retval = -1;
499 goto exit_health_pipe;
500 }
501
502 /* Create thread to manage the client socket */
503 ret = pthread_create(&health_thread, NULL,
504 thread_manage_health, (void *) NULL);
505 if (ret) {
506 errno = ret;
507 PERROR("pthread_create health");
508 retval = -1;
509 goto exit_health_thread;
510 }
511
512 /*
513 * Wait for health thread to be initialized before letting the
514 * sessiond thread reply to the sessiond that we are ready.
515 */
516 while (uatomic_read(&lttng_consumer_ready)) {
517 usleep(100000);
518 }
519 cmm_smp_mb(); /* Read ready before following operations */
520
521 /* Create thread to manage channels */
522 ret = pthread_create(&channel_thread, NULL,
523 consumer_thread_channel_poll,
524 (void *) ctx);
525 if (ret) {
526 errno = ret;
527 PERROR("pthread_create");
528 retval = -1;
529 goto exit_channel_thread;
530 }
531
532 /* Create thread to manage the polling/writing of trace metadata */
533 ret = pthread_create(&metadata_thread, NULL,
534 consumer_thread_metadata_poll,
535 (void *) ctx);
536 if (ret) {
537 errno = ret;
538 PERROR("pthread_create");
539 retval = -1;
540 goto exit_metadata_thread;
541 }
542
543 /* Create thread to manage the polling/writing of trace data */
544 ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll,
545 (void *) ctx);
546 if (ret) {
547 errno = ret;
548 PERROR("pthread_create");
549 retval = -1;
550 goto exit_data_thread;
551 }
552
553 /* Create the thread to manage the receive of fd */
554 ret = pthread_create(&sessiond_thread, NULL,
555 consumer_thread_sessiond_poll,
556 (void *) ctx);
557 if (ret) {
558 errno = ret;
559 PERROR("pthread_create");
560 retval = -1;
561 goto exit_sessiond_thread;
562 }
563
564 /*
565 * Create the thread to manage the UST metadata periodic timer and
566 * live timer.
567 */
568 ret = pthread_create(&metadata_timer_thread, NULL,
569 consumer_timer_thread, (void *) ctx);
570 if (ret) {
571 errno = ret;
572 PERROR("pthread_create");
573 retval = -1;
574 goto exit_metadata_timer_thread;
575 }
576
577 ret = pthread_detach(metadata_timer_thread);
578 if (ret) {
579 errno = ret;
580 PERROR("pthread_detach");
581 retval = -1;
582 goto exit_metadata_timer_detach;
583 }
584
585 /*
586 * This is where we start awaiting program completion (e.g. through
587 * signal that asks threads to teardown.
588 */
589
590 exit_metadata_timer_detach:
591 exit_metadata_timer_thread:
592 ret = pthread_join(sessiond_thread, &status);
593 if (ret) {
594 errno = ret;
595 PERROR("pthread_join sessiond_thread");
596 retval = -1;
597 }
598 exit_sessiond_thread:
599
600 ret = pthread_join(data_thread, &status);
601 if (ret) {
602 errno = ret;
603 PERROR("pthread_join data_thread");
604 retval = -1;
605 }
606 exit_data_thread:
607
608 ret = pthread_join(metadata_thread, &status);
609 if (ret) {
610 errno = ret;
611 PERROR("pthread_join metadata_thread");
612 retval = -1;
613 }
614 exit_metadata_thread:
615
616 ret = pthread_join(channel_thread, &status);
617 if (ret) {
618 errno = ret;
619 PERROR("pthread_join channel_thread");
620 retval = -1;
621 }
622 exit_channel_thread:
623
624 ret = pthread_join(health_thread, &status);
625 if (ret) {
626 errno = ret;
627 PERROR("pthread_join health_thread");
628 retval = -1;
629 }
630 exit_health_thread:
631
632 utils_close_pipe(health_quit_pipe);
633 exit_health_pipe:
634
635 exit_init_data:
636 tmp_ctx = ctx;
637 ctx = NULL;
638 cmm_barrier(); /* Clear ctx for signal handler. */
639 lttng_consumer_destroy(tmp_ctx);
640 lttng_consumer_cleanup();
641
642 if (health_consumerd) {
643 health_app_destroy(health_consumerd);
644 }
645 exit_health_consumerd_cleanup:
646
647 exit_options:
648
649 exit_set_signal_handler:
650 /* Ensure all prior call_rcu are done. */
651 rcu_barrier();
652
653 if (!retval) {
654 exit(EXIT_SUCCESS);
655 } else {
656 exit(EXIT_FAILURE);
657 }
658 }
This page took 0.043829 seconds and 6 git commands to generate.