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