| 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 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 void parse_args(int argc, char **argv) |
| 189 | { |
| 190 | int c; |
| 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:", long_options, &option_index); |
| 211 | if (c == -1) { |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | switch (c) { |
| 216 | case 0: |
| 217 | fprintf(stderr, "option %s", long_options[option_index].name); |
| 218 | if (optarg) { |
| 219 | fprintf(stderr, " with arg %s\n", optarg); |
| 220 | } |
| 221 | break; |
| 222 | case 'c': |
| 223 | snprintf(command_sock_path, PATH_MAX, "%s", optarg); |
| 224 | break; |
| 225 | case 'e': |
| 226 | snprintf(error_sock_path, PATH_MAX, "%s", optarg); |
| 227 | break; |
| 228 | case 'd': |
| 229 | opt_daemon = 1; |
| 230 | break; |
| 231 | case 'g': |
| 232 | tracing_group_name = optarg; |
| 233 | break; |
| 234 | case 'h': |
| 235 | usage(stdout); |
| 236 | exit(EXIT_SUCCESS); |
| 237 | case 'q': |
| 238 | lttng_opt_quiet = 1; |
| 239 | break; |
| 240 | case 'v': |
| 241 | lttng_opt_verbose = 1; |
| 242 | break; |
| 243 | case 'V': |
| 244 | fprintf(stdout, "%s\n", VERSION); |
| 245 | exit(EXIT_SUCCESS); |
| 246 | case 'k': |
| 247 | opt_type = LTTNG_CONSUMER_KERNEL; |
| 248 | break; |
| 249 | #ifdef HAVE_LIBLTTNG_UST_CTL |
| 250 | case 'u': |
| 251 | # if (CAA_BITS_PER_LONG == 64) |
| 252 | opt_type = LTTNG_CONSUMER64_UST; |
| 253 | # elif (CAA_BITS_PER_LONG == 32) |
| 254 | opt_type = LTTNG_CONSUMER32_UST; |
| 255 | # else |
| 256 | # error "Unknown bitness" |
| 257 | # endif |
| 258 | break; |
| 259 | #endif |
| 260 | default: |
| 261 | usage(stderr); |
| 262 | exit(EXIT_FAILURE); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Set open files limit to unlimited. This daemon can open a large number of |
| 269 | * file descriptors in order to consumer multiple kernel traces. |
| 270 | */ |
| 271 | static void set_ulimit(void) |
| 272 | { |
| 273 | int ret; |
| 274 | struct rlimit lim; |
| 275 | |
| 276 | /* The kernel does not allowed an infinite limit for open files */ |
| 277 | lim.rlim_cur = 65535; |
| 278 | lim.rlim_max = 65535; |
| 279 | |
| 280 | ret = setrlimit(RLIMIT_NOFILE, &lim); |
| 281 | if (ret < 0) { |
| 282 | PERROR("failed to set open files limit"); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * main |
| 288 | */ |
| 289 | int main(int argc, char **argv) |
| 290 | { |
| 291 | int ret = 0; |
| 292 | void *status; |
| 293 | |
| 294 | /* Parse arguments */ |
| 295 | progname = argv[0]; |
| 296 | parse_args(argc, argv); |
| 297 | |
| 298 | /* Daemonize */ |
| 299 | if (opt_daemon) { |
| 300 | int i; |
| 301 | |
| 302 | /* |
| 303 | * fork |
| 304 | * child: setsid, close FD 0, 1, 2, chdir / |
| 305 | * parent: exit (if fork is successful) |
| 306 | */ |
| 307 | ret = daemon(0, 0); |
| 308 | if (ret < 0) { |
| 309 | PERROR("daemon"); |
| 310 | goto error; |
| 311 | } |
| 312 | /* |
| 313 | * We are in the child. Make sure all other file |
| 314 | * descriptors are closed, in case we are called with |
| 315 | * more opened file descriptors than the standard ones. |
| 316 | */ |
| 317 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { |
| 318 | (void) close(i); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* Set up max poll set size */ |
| 323 | lttng_poll_set_max_size(); |
| 324 | |
| 325 | if (*command_sock_path == '\0') { |
| 326 | switch (opt_type) { |
| 327 | case LTTNG_CONSUMER_KERNEL: |
| 328 | snprintf(command_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_CMD_SOCK_PATH, |
| 329 | DEFAULT_LTTNG_RUNDIR); |
| 330 | break; |
| 331 | case LTTNG_CONSUMER64_UST: |
| 332 | snprintf(command_sock_path, PATH_MAX, |
| 333 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
| 334 | break; |
| 335 | case LTTNG_CONSUMER32_UST: |
| 336 | snprintf(command_sock_path, PATH_MAX, |
| 337 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
| 338 | break; |
| 339 | default: |
| 340 | WARN("Unknown consumerd type"); |
| 341 | goto error; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* Init */ |
| 346 | if (lttng_consumer_init() < 0) { |
| 347 | goto error; |
| 348 | } |
| 349 | |
| 350 | /* Init socket timeouts */ |
| 351 | lttcomm_init(); |
| 352 | lttcomm_inet_init(); |
| 353 | |
| 354 | if (!getuid()) { |
| 355 | /* Set limit for open files */ |
| 356 | set_ulimit(); |
| 357 | } |
| 358 | |
| 359 | health_consumerd = health_app_create(NR_HEALTH_CONSUMERD_TYPES); |
| 360 | if (!health_consumerd) { |
| 361 | goto error; |
| 362 | } |
| 363 | |
| 364 | /* create the consumer instance with and assign the callbacks */ |
| 365 | ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer, |
| 366 | NULL, lttng_consumer_on_recv_stream, NULL); |
| 367 | if (ctx == NULL) { |
| 368 | goto error; |
| 369 | } |
| 370 | |
| 371 | lttng_consumer_set_command_sock_path(ctx, command_sock_path); |
| 372 | if (*error_sock_path == '\0') { |
| 373 | switch (opt_type) { |
| 374 | case LTTNG_CONSUMER_KERNEL: |
| 375 | snprintf(error_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
| 376 | DEFAULT_LTTNG_RUNDIR); |
| 377 | break; |
| 378 | case LTTNG_CONSUMER64_UST: |
| 379 | snprintf(error_sock_path, PATH_MAX, |
| 380 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
| 381 | break; |
| 382 | case LTTNG_CONSUMER32_UST: |
| 383 | snprintf(error_sock_path, PATH_MAX, |
| 384 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
| 385 | break; |
| 386 | default: |
| 387 | WARN("Unknown consumerd type"); |
| 388 | goto error; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if (set_signal_handler() < 0) { |
| 393 | goto error; |
| 394 | } |
| 395 | |
| 396 | /* Connect to the socket created by lttng-sessiond to report errors */ |
| 397 | DBG("Connecting to error socket %s", error_sock_path); |
| 398 | ret = lttcomm_connect_unix_sock(error_sock_path); |
| 399 | /* not a fatal error, but all communication with lttng-sessiond will fail */ |
| 400 | if (ret < 0) { |
| 401 | WARN("Cannot connect to error socket (is lttng-sessiond started?)"); |
| 402 | } |
| 403 | lttng_consumer_set_error_sock(ctx, ret); |
| 404 | |
| 405 | /* |
| 406 | * Block RT signals used for UST periodical metadata flush and the live |
| 407 | * timer in main, and create a dedicated thread to handle these signals. |
| 408 | */ |
| 409 | consumer_signal_init(); |
| 410 | |
| 411 | ctx->type = opt_type; |
| 412 | |
| 413 | ret = utils_create_pipe(health_quit_pipe); |
| 414 | if (ret < 0) { |
| 415 | goto error_health_pipe; |
| 416 | } |
| 417 | |
| 418 | /* Create thread to manage the client socket */ |
| 419 | ret = pthread_create(&health_thread, NULL, |
| 420 | thread_manage_health, (void *) NULL); |
| 421 | if (ret != 0) { |
| 422 | PERROR("pthread_create health"); |
| 423 | goto health_error; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Wait for health thread to be initialized before letting the |
| 428 | * sessiond thread reply to the sessiond that we are ready. |
| 429 | */ |
| 430 | while (uatomic_read(<tng_consumer_ready)) { |
| 431 | usleep(100000); |
| 432 | } |
| 433 | cmm_smp_mb(); /* Read ready before following operations */ |
| 434 | |
| 435 | /* Create thread to manage channels */ |
| 436 | ret = pthread_create(&channel_thread, NULL, consumer_thread_channel_poll, |
| 437 | (void *) ctx); |
| 438 | if (ret != 0) { |
| 439 | perror("pthread_create"); |
| 440 | goto channel_error; |
| 441 | } |
| 442 | |
| 443 | /* Create thread to manage the polling/writing of trace metadata */ |
| 444 | ret = pthread_create(&metadata_thread, NULL, consumer_thread_metadata_poll, |
| 445 | (void *) ctx); |
| 446 | if (ret != 0) { |
| 447 | perror("pthread_create"); |
| 448 | goto metadata_error; |
| 449 | } |
| 450 | |
| 451 | /* Create thread to manage the polling/writing of trace data */ |
| 452 | ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll, |
| 453 | (void *) ctx); |
| 454 | if (ret != 0) { |
| 455 | perror("pthread_create"); |
| 456 | goto data_error; |
| 457 | } |
| 458 | |
| 459 | /* Create the thread to manage the receive of fd */ |
| 460 | ret = pthread_create(&sessiond_thread, NULL, consumer_thread_sessiond_poll, |
| 461 | (void *) ctx); |
| 462 | if (ret != 0) { |
| 463 | perror("pthread_create"); |
| 464 | goto sessiond_error; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Create the thread to manage the UST metadata periodic timer and |
| 469 | * live timer. |
| 470 | */ |
| 471 | ret = pthread_create(&metadata_timer_thread, NULL, |
| 472 | consumer_timer_thread, (void *) ctx); |
| 473 | if (ret != 0) { |
| 474 | perror("pthread_create"); |
| 475 | goto metadata_timer_error; |
| 476 | } |
| 477 | |
| 478 | ret = pthread_detach(metadata_timer_thread); |
| 479 | if (ret) { |
| 480 | errno = ret; |
| 481 | perror("pthread_detach"); |
| 482 | } |
| 483 | |
| 484 | metadata_timer_error: |
| 485 | ret = pthread_join(sessiond_thread, &status); |
| 486 | if (ret != 0) { |
| 487 | perror("pthread_join"); |
| 488 | goto error; |
| 489 | } |
| 490 | |
| 491 | sessiond_error: |
| 492 | ret = pthread_join(data_thread, &status); |
| 493 | if (ret != 0) { |
| 494 | perror("pthread_join"); |
| 495 | goto error; |
| 496 | } |
| 497 | |
| 498 | data_error: |
| 499 | ret = pthread_join(metadata_thread, &status); |
| 500 | if (ret != 0) { |
| 501 | perror("pthread_join"); |
| 502 | goto error; |
| 503 | } |
| 504 | |
| 505 | metadata_error: |
| 506 | ret = pthread_join(channel_thread, &status); |
| 507 | if (ret != 0) { |
| 508 | perror("pthread_join"); |
| 509 | goto error; |
| 510 | } |
| 511 | |
| 512 | channel_error: |
| 513 | ret = pthread_join(health_thread, &status); |
| 514 | if (ret != 0) { |
| 515 | PERROR("pthread_join health thread"); |
| 516 | goto error; /* join error, exit without cleanup */ |
| 517 | } |
| 518 | |
| 519 | health_error: |
| 520 | utils_close_pipe(health_quit_pipe); |
| 521 | |
| 522 | error_health_pipe: |
| 523 | if (!ret) { |
| 524 | ret = EXIT_SUCCESS; |
| 525 | goto end; |
| 526 | } |
| 527 | |
| 528 | error: |
| 529 | ret = EXIT_FAILURE; |
| 530 | |
| 531 | end: |
| 532 | lttng_consumer_destroy(ctx); |
| 533 | lttng_consumer_cleanup(); |
| 534 | if (health_consumerd) { |
| 535 | health_app_destroy(health_consumerd); |
| 536 | } |
| 537 | |
| 538 | return ret; |
| 539 | } |