docs: Add supported versions and fix-backport policy
[lttng-tools.git] / src / bin / lttng-consumerd / lttng-consumerd.cpp
CommitLineData
d4a1283e 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa 3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d4a1283e 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
d4a1283e 6 *
d4a1283e
JD
7 */
8
6c1c0768 9#define _LGPL_SOURCE
28ab034a
JG
10#include "health-consumerd.hpp"
11#include "lttng-consumerd.hpp"
12
13#include <common/common.hpp>
14#include <common/compat/getenv.hpp>
15#include <common/compat/poll.hpp>
16#include <common/consumer/consumer-timer.hpp>
17#include <common/consumer/consumer.hpp>
18#include <common/defaults.hpp>
19#include <common/sessiond-comm/sessiond-comm.hpp>
20#include <common/utils.hpp>
21
d4a1283e
JD
22#include <fcntl.h>
23#include <getopt.h>
24#include <grp.h>
25#include <limits.h>
28ab034a 26#include <poll.h>
d4a1283e
JD
27#include <pthread.h>
28#include <signal.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/ipc.h>
28ab034a 33#include <sys/mman.h>
1ccfc0e3 34#include <sys/resource.h>
d4a1283e
JD
35#include <sys/shm.h>
36#include <sys/socket.h>
37#include <sys/stat.h>
38#include <sys/types.h>
28ab034a 39#include <ulimit.h>
d4a1283e 40#include <unistd.h>
7753dea8 41#include <urcu/compiler.h>
28ab034a 42#include <urcu/list.h>
d4a1283e 43
d8ef542d 44/* threads (channel handling, poll, metadata, sessiond) */
331744e3 45
28ab034a
JG
46static pthread_t channel_thread, data_thread, metadata_thread, sessiond_thread,
47 metadata_timer_thread, health_thread;
13675d0e 48static bool metadata_timer_thread_online;
d4a1283e 49
3183dbb0 50/* to count the number of times the user pressed ctrl+c */
13e44745
JD
51static int sigintcount = 0;
52
d4a1283e 53/* Argument variables */
28ab034a
JG
54int lttng_opt_quiet; /* not static in error.h */
55int lttng_opt_verbose; /* not static in error.h */
56int lttng_opt_mi; /* not static in error.h */
c7e35b03 57
d4a1283e
JD
58static int opt_daemon;
59static const char *progname;
6533b585
DG
60static char command_sock_path[PATH_MAX]; /* Global command socket path */
61static char error_sock_path[PATH_MAX]; /* Global error path */
3bd1e081 62static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
d4a1283e 63
7753dea8 64/* the liblttngconsumerd context */
d84afb7e 65static struct lttng_consumer_local_data *the_consumer_context;
cb040cc1 66
1fc79fb4
MD
67/* Consumerd health monitoring */
68struct health_app *health_consumerd;
69
6c71277b
MD
70const char *tracing_group_name = DEFAULT_TRACING_GROUP;
71
748b7b07
MD
72int lttng_consumer_ready = NR_LTTNG_CONSUMER_READY;
73
5c635c72
MD
74enum lttng_consumer_type lttng_consumer_get_type(void)
75{
d84afb7e 76 if (!the_consumer_context) {
5c635c72
MD
77 return LTTNG_CONSUMER_UNKNOWN;
78 }
d84afb7e 79 return the_consumer_context->type;
5c635c72
MD
80}
81
d4a1283e 82/*
6533b585 83 * Signal handler for the daemon
d4a1283e 84 */
28ab034a 85static void sighandler(int sig, siginfo_t *siginfo, void *arg __attribute__((unused)))
d4a1283e 86{
13e44745
JD
87 if (sig == SIGINT && sigintcount++ == 0) {
88 DBG("ignoring first SIGINT");
89 return;
90 }
91
881fc67f
MD
92 if (sig == SIGBUS) {
93 int write_ret;
94 const char msg[] = "Received SIGBUS, aborting program.\n";
95
96 lttng_consumer_sigbus_handle(siginfo->si_addr);
97 /*
98 * If ustctl did not catch this signal (triggering a
99 * siglongjmp), abort the program. Otherwise, the execution
100 * will resume from the ust-ctl call which caused this error.
101 *
102 * The return value is ignored since the program aborts anyhow.
103 */
104 write_ret = write(STDERR_FILENO, msg, sizeof(msg));
105 (void) write_ret;
106 abort();
107 }
108
d84afb7e
JG
109 if (the_consumer_context) {
110 lttng_consumer_should_exit(the_consumer_context);
6a0caa9b 111 }
d4a1283e
JD
112}
113
114/*
6533b585 115 * Setup signal handler for :
881fc67f 116 * SIGINT, SIGTERM, SIGPIPE, SIGBUS
d4a1283e 117 */
cd9adb8b 118static int set_signal_handler()
d4a1283e
JD
119{
120 int ret = 0;
121 struct sigaction sa;
122 sigset_t sigset;
123
124 if ((ret = sigemptyset(&sigset)) < 0) {
8fdf2d4d 125 PERROR("sigemptyset");
d4a1283e
JD
126 return ret;
127 }
128
d4a1283e 129 sa.sa_mask = sigset;
881fc67f 130 sa.sa_flags = SA_SIGINFO;
0072e5e2 131
881fc67f 132 sa.sa_sigaction = sighandler;
cd9adb8b 133 if ((ret = sigaction(SIGTERM, &sa, nullptr)) < 0) {
8fdf2d4d 134 PERROR("sigaction");
d4a1283e
JD
135 return ret;
136 }
137
cd9adb8b 138 if ((ret = sigaction(SIGINT, &sa, nullptr)) < 0) {
8fdf2d4d 139 PERROR("sigaction");
d4a1283e
JD
140 return ret;
141 }
142
cd9adb8b 143 if ((ret = sigaction(SIGBUS, &sa, nullptr)) < 0) {
881fc67f
MD
144 PERROR("sigaction");
145 return ret;
146 }
147
148 sa.sa_flags = 0;
0072e5e2 149 sa.sa_handler = SIG_IGN;
cd9adb8b 150 if ((ret = sigaction(SIGPIPE, &sa, nullptr)) < 0) {
8fdf2d4d 151 PERROR("sigaction");
d4a1283e
JD
152 return ret;
153 }
154
155 return ret;
156}
157
d4a1283e 158/*
3183dbb0 159 * Usage function on stream file.
d4a1283e 160 */
3183dbb0 161static void usage(FILE *fp)
d4a1283e 162{
3183dbb0 163 fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname);
28ab034a
JG
164 fprintf(fp,
165 " -h, --help "
166 "Display this usage.\n");
167 fprintf(fp,
168 " -c, --consumerd-cmd-sock PATH "
169 "Specify path for the command socket\n");
170 fprintf(fp,
171 " -e, --consumerd-err-sock PATH "
172 "Specify path for the error socket\n");
173 fprintf(fp,
174 " -d, --daemonize "
175 "Start as a daemon.\n");
176 fprintf(fp,
177 " -q, --quiet "
178 "No output at all.\n");
179 fprintf(fp,
180 " -v, --verbose "
181 "Verbose mode. Activate DBG() macro.\n");
182 fprintf(fp,
183 " -V, --version "
184 "Show version number.\n");
185 fprintf(fp,
186 " -g, --group NAME "
187 "Specify the tracing group name. (default: tracing)\n");
188 fprintf(fp,
189 " -k, --kernel "
190 "Consumer kernel buffers (default).\n");
191 fprintf(fp,
192 " -u, --ust "
193 "Consumer UST buffers.%s\n",
74d0b642 194#ifdef HAVE_LIBLTTNG_UST_CTL
28ab034a 195 ""
3bd1e081 196#else
28ab034a 197 " (support not compiled in)"
3bd1e081 198#endif
28ab034a 199 );
d4a1283e
JD
200}
201
202/*
203 * daemon argument parsing
204 */
8f7a281b 205static int parse_args(int argc, char **argv)
d4a1283e 206{
8f7a281b 207 int c, ret = 0;
d4a1283e 208
cd9adb8b
JG
209 static struct option long_options[] = { { "consumerd-cmd-sock", 1, nullptr, 'c' },
210 { "consumerd-err-sock", 1, nullptr, 'e' },
211 { "daemonize", 0, nullptr, 'd' },
212 { "group", 1, nullptr, 'g' },
213 { "help", 0, nullptr, 'h' },
214 { "quiet", 0, nullptr, 'q' },
215 { "verbose", 0, nullptr, 'v' },
216 { "version", 0, nullptr, 'V' },
217 { "kernel", 0, nullptr, 'k' },
74d0b642 218#ifdef HAVE_LIBLTTNG_UST_CTL
cd9adb8b 219 { "ust", 0, nullptr, 'u' },
3bd1e081 220#endif
cd9adb8b 221 { nullptr, 0, nullptr, 0 } };
d4a1283e 222
cd9adb8b 223 while (true) {
d4a1283e 224 int option_index = 0;
28ab034a
JG
225 c = getopt_long(argc,
226 argv,
227 "dhqvVku"
228 "c:e:g:",
229 long_options,
230 &option_index);
d4a1283e
JD
231 if (c == -1) {
232 break;
233 }
234
235 switch (c) {
914a571b 236 case 0:
28ab034a 237 fprintf(stderr, "option %s", long_options[option_index].name);
914a571b
JD
238 if (optarg) {
239 fprintf(stderr, " with arg %s\n", optarg);
8f7a281b
MD
240 ret = -1;
241 goto end;
914a571b
JD
242 }
243 break;
244 case 'c':
e8fa9fb0
MD
245 if (lttng_is_setuid_setgid()) {
246 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
28ab034a 247 "-c, --consumerd-cmd-sock");
e8fa9fb0
MD
248 } else {
249 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
250 }
914a571b
JD
251 break;
252 case 'e':
e8fa9fb0
MD
253 if (lttng_is_setuid_setgid()) {
254 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
28ab034a 255 "-e, --consumerd-err-sock");
e8fa9fb0
MD
256 } else {
257 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
258 }
914a571b
JD
259 break;
260 case 'd':
261 opt_daemon = 1;
262 break;
6c71277b 263 case 'g':
e8fa9fb0
MD
264 if (lttng_is_setuid_setgid()) {
265 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
28ab034a 266 "-g, --group");
e8fa9fb0
MD
267 } else {
268 tracing_group_name = optarg;
269 }
6c71277b 270 break;
914a571b 271 case 'h':
3183dbb0
DG
272 usage(stdout);
273 exit(EXIT_SUCCESS);
914a571b 274 case 'q':
97e19046 275 lttng_opt_quiet = 1;
914a571b
JD
276 break;
277 case 'v':
0ca413e0 278 lttng_opt_verbose = 3;
914a571b
JD
279 break;
280 case 'V':
281 fprintf(stdout, "%s\n", VERSION);
282 exit(EXIT_SUCCESS);
3bd1e081
MD
283 case 'k':
284 opt_type = LTTNG_CONSUMER_KERNEL;
285 break;
74d0b642 286#ifdef HAVE_LIBLTTNG_UST_CTL
3bd1e081 287 case 'u':
28ab034a 288#if (CAA_BITS_PER_LONG == 64)
7753dea8 289 opt_type = LTTNG_CONSUMER64_UST;
28ab034a 290#elif (CAA_BITS_PER_LONG == 32)
7753dea8 291 opt_type = LTTNG_CONSUMER32_UST;
28ab034a
JG
292#else
293#error "Unknown bitness"
294#endif
3bd1e081
MD
295 break;
296#endif
914a571b 297 default:
3183dbb0 298 usage(stderr);
8f7a281b
MD
299 ret = -1;
300 goto end;
d4a1283e
JD
301 }
302 }
8f7a281b
MD
303end:
304 return ret;
d4a1283e
JD
305}
306
1ccfc0e3
DG
307/*
308 * Set open files limit to unlimited. This daemon can open a large number of
309 * file descriptors in order to consumer multiple kernel traces.
310 */
cd9adb8b 311static void set_ulimit()
1ccfc0e3
DG
312{
313 int ret;
314 struct rlimit lim;
315
316 /* The kernel does not allowed an infinite limit for open files */
317 lim.rlim_cur = 65535;
318 lim.rlim_max = 65535;
319
320 ret = setrlimit(RLIMIT_NOFILE, &lim);
321 if (ret < 0) {
322 PERROR("failed to set open files limit");
323 }
324}
325
d4a1283e
JD
326/*
327 * main
328 */
329int main(int argc, char **argv)
330{
72f11bd7 331 int ret = 0, retval = 0;
d4a1283e 332 void *status;
6a0caa9b 333 struct lttng_consumer_local_data *tmp_ctx;
d4a1283e 334
fe19a07a
MD
335 rcu_register_thread();
336
cd9adb8b 337 if (run_as_create_worker(argv[0], nullptr, nullptr) < 0) {
5e8e3a00
JG
338 goto exit_set_signal_handler;
339 }
340
72f11bd7
MD
341 if (set_signal_handler()) {
342 retval = -1;
343 goto exit_set_signal_handler;
344 }
345
d4a1283e
JD
346 /* Parse arguments */
347 progname = argv[0];
72f11bd7
MD
348 if (parse_args(argc, argv)) {
349 retval = -1;
350 goto exit_options;
351 }
d4a1283e
JD
352
353 /* Daemonize */
354 if (opt_daemon) {
ceed52b5
MD
355 int i;
356
357 /*
358 * fork
359 * child: setsid, close FD 0, 1, 2, chdir /
360 * parent: exit (if fork is successful)
361 */
d4a1283e
JD
362 ret = daemon(0, 0);
363 if (ret < 0) {
ceed52b5 364 PERROR("daemon");
72f11bd7
MD
365 retval = -1;
366 goto exit_options;
d4a1283e 367 }
ceed52b5
MD
368 /*
369 * We are in the child. Make sure all other file
370 * descriptors are closed, in case we are called with
371 * more opened file descriptors than the standard ones.
372 */
373 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
374 (void) close(i);
375 }
d4a1283e
JD
376 }
377
72f11bd7
MD
378 /*
379 * Starting from here, we can create threads. This needs to be after
380 * lttng_daemonize due to RCU.
381 */
382
383 health_consumerd = health_app_create(NR_HEALTH_CONSUMERD_TYPES);
384 if (!health_consumerd) {
385 retval = -1;
386 goto exit_health_consumerd_cleanup;
387 }
388
9d035200 389 if (*command_sock_path == '\0') {
7753dea8
MD
390 switch (opt_type) {
391 case LTTNG_CONSUMER_KERNEL:
28ab034a
JG
392 ret = snprintf(command_sock_path,
393 PATH_MAX,
394 DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
395 DEFAULT_LTTNG_RUNDIR);
72f11bd7
MD
396 if (ret < 0) {
397 retval = -1;
398 goto exit_init_data;
399 }
7753dea8
MD
400 break;
401 case LTTNG_CONSUMER64_UST:
28ab034a
JG
402 ret = snprintf(command_sock_path,
403 PATH_MAX,
404 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
405 DEFAULT_LTTNG_RUNDIR);
72f11bd7
MD
406 if (ret < 0) {
407 retval = -1;
408 goto exit_init_data;
409 }
7753dea8
MD
410 break;
411 case LTTNG_CONSUMER32_UST:
28ab034a
JG
412 ret = snprintf(command_sock_path,
413 PATH_MAX,
414 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
415 DEFAULT_LTTNG_RUNDIR);
72f11bd7
MD
416 if (ret < 0) {
417 retval = -1;
418 goto exit_init_data;
419 }
7753dea8
MD
420 break;
421 default:
72f11bd7
MD
422 ERR("Unknown consumerd type");
423 retval = -1;
424 goto exit_init_data;
7753dea8 425 }
d4a1283e 426 }
e4421fec
DG
427
428 /* Init */
72f11bd7
MD
429 if (lttng_consumer_init()) {
430 retval = -1;
431 goto exit_init_data;
282dadbc
MD
432 }
433
72f11bd7 434 /* Initialize communication library */
e98ec547 435 lttcomm_init();
72f11bd7 436 /* Initialize TCP timeout values */
e98ec547 437 lttcomm_inet_init();
e4421fec 438
1ccfc0e3
DG
439 if (!getuid()) {
440 /* Set limit for open files */
441 set_ulimit();
442 }
443
5348b470 444 /* create the consumer instance with and assign the callbacks */
cd9adb8b
JG
445 the_consumer_context = lttng_consumer_create(opt_type,
446 lttng_consumer_read_subbuffer,
447 nullptr,
448 lttng_consumer_on_recv_stream,
449 nullptr);
d84afb7e 450 if (!the_consumer_context) {
72f11bd7
MD
451 retval = -1;
452 goto exit_init_data;
cb040cc1
JD
453 }
454
d84afb7e 455 lttng_consumer_set_command_sock_path(the_consumer_context, command_sock_path);
9d035200 456 if (*error_sock_path == '\0') {
7753dea8
MD
457 switch (opt_type) {
458 case LTTNG_CONSUMER_KERNEL:
28ab034a
JG
459 ret = snprintf(error_sock_path,
460 PATH_MAX,
461 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
462 DEFAULT_LTTNG_RUNDIR);
72f11bd7
MD
463 if (ret < 0) {
464 retval = -1;
465 goto exit_init_data;
466 }
7753dea8
MD
467 break;
468 case LTTNG_CONSUMER64_UST:
28ab034a
JG
469 ret = snprintf(error_sock_path,
470 PATH_MAX,
471 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
472 DEFAULT_LTTNG_RUNDIR);
72f11bd7
MD
473 if (ret < 0) {
474 retval = -1;
475 goto exit_init_data;
476 }
7753dea8
MD
477 break;
478 case LTTNG_CONSUMER32_UST:
28ab034a
JG
479 ret = snprintf(error_sock_path,
480 PATH_MAX,
481 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
482 DEFAULT_LTTNG_RUNDIR);
72f11bd7
MD
483 if (ret < 0) {
484 retval = -1;
485 goto exit_init_data;
486 }
7753dea8
MD
487 break;
488 default:
72f11bd7
MD
489 ERR("Unknown consumerd type");
490 retval = -1;
491 goto exit_init_data;
7753dea8 492 }
d4a1283e
JD
493 }
494
32258573 495 /* Connect to the socket created by lttng-sessiond to report errors */
d4a1283e 496 DBG("Connecting to error socket %s", error_sock_path);
1ce86c9a 497 ret = lttcomm_connect_unix_sock(error_sock_path);
72f11bd7
MD
498 /*
499 * Not a fatal error, but all communication with lttng-sessiond will
500 * fail.
501 */
1ce86c9a 502 if (ret < 0) {
99bab54f 503 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
d4a1283e 504 }
d84afb7e 505 lttng_consumer_set_error_sock(the_consumer_context, ret);
d4a1283e 506
331744e3 507 /*
d3e2ba59
JD
508 * Block RT signals used for UST periodical metadata flush and the live
509 * timer in main, and create a dedicated thread to handle these signals.
331744e3 510 */
72f11bd7
MD
511 if (consumer_signal_init()) {
512 retval = -1;
513 goto exit_init_data;
514 }
d3e2ba59 515
d84afb7e 516 the_consumer_context->type = opt_type;
331744e3 517
72f11bd7
MD
518 if (utils_create_pipe(health_quit_pipe)) {
519 retval = -1;
520 goto exit_health_pipe;
5c635c72
MD
521 }
522
523 /* Create thread to manage the client socket */
28ab034a
JG
524 ret = pthread_create(&health_thread,
525 default_pthread_attr(),
526 thread_manage_health_consumerd,
cd9adb8b 527 (void *) nullptr);
72f11bd7
MD
528 if (ret) {
529 errno = ret;
5c635c72 530 PERROR("pthread_create health");
72f11bd7
MD
531 retval = -1;
532 goto exit_health_thread;
5c635c72
MD
533 }
534
748b7b07
MD
535 /*
536 * Wait for health thread to be initialized before letting the
537 * sessiond thread reply to the sessiond that we are ready.
538 */
539 while (uatomic_read(&lttng_consumer_ready)) {
1f3130d5 540 usleep(100000);
748b7b07 541 }
28ab034a 542 cmm_smp_mb(); /* Read ready before following operations */
748b7b07 543
13675d0e
MD
544 /*
545 * Create the thread to manage the UST metadata periodic timer and
546 * live timer.
547 */
cd9adb8b
JG
548 ret = pthread_create(&metadata_timer_thread,
549 nullptr,
550 consumer_timer_thread,
551 (void *) the_consumer_context);
13675d0e
MD
552 if (ret) {
553 errno = ret;
554 PERROR("pthread_create");
555 retval = -1;
556 goto exit_metadata_timer_thread;
557 }
558 metadata_timer_thread_online = true;
559
d8ef542d 560 /* Create thread to manage channels */
28ab034a
JG
561 ret = pthread_create(&channel_thread,
562 default_pthread_attr(),
563 consumer_thread_channel_poll,
564 (void *) the_consumer_context);
72f11bd7
MD
565 if (ret) {
566 errno = ret;
567 PERROR("pthread_create");
568 retval = -1;
569 goto exit_channel_thread;
d8ef542d
MD
570 }
571
7d980def 572 /* Create thread to manage the polling/writing of trace metadata */
28ab034a
JG
573 ret = pthread_create(&metadata_thread,
574 default_pthread_attr(),
575 consumer_thread_metadata_poll,
576 (void *) the_consumer_context);
72f11bd7
MD
577 if (ret) {
578 errno = ret;
579 PERROR("pthread_create");
580 retval = -1;
581 goto exit_metadata_thread;
7d980def
DG
582 }
583
584 /* Create thread to manage the polling/writing of trace data */
28ab034a
JG
585 ret = pthread_create(&data_thread,
586 default_pthread_attr(),
587 consumer_thread_data_poll,
588 (void *) the_consumer_context);
72f11bd7
MD
589 if (ret) {
590 errno = ret;
591 PERROR("pthread_create");
592 retval = -1;
593 goto exit_data_thread;
d4a1283e
JD
594 }
595
7b336484 596 /* Create the thread to manage the reception of fds */
28ab034a
JG
597 ret = pthread_create(&sessiond_thread,
598 default_pthread_attr(),
599 consumer_thread_sessiond_poll,
600 (void *) the_consumer_context);
72f11bd7
MD
601 if (ret) {
602 errno = ret;
603 PERROR("pthread_create");
604 retval = -1;
605 goto exit_sessiond_thread;
df27ef7b
DG
606 }
607
72f11bd7
MD
608 /*
609 * This is where we start awaiting program completion (e.g. through
610 * signal that asks threads to teardown.
611 */
612
df27ef7b 613 ret = pthread_join(sessiond_thread, &status);
72f11bd7
MD
614 if (ret) {
615 errno = ret;
616 PERROR("pthread_join sessiond_thread");
617 retval = -1;
d4a1283e 618 }
72f11bd7 619exit_sessiond_thread:
d4a1283e 620
df27ef7b 621 ret = pthread_join(data_thread, &status);
72f11bd7
MD
622 if (ret) {
623 errno = ret;
624 PERROR("pthread_join data_thread");
625 retval = -1;
df27ef7b 626 }
72f11bd7 627exit_data_thread:
df27ef7b 628
df27ef7b 629 ret = pthread_join(metadata_thread, &status);
72f11bd7
MD
630 if (ret) {
631 errno = ret;
632 PERROR("pthread_join metadata_thread");
633 retval = -1;
df27ef7b 634 }
72f11bd7 635exit_metadata_thread:
df27ef7b 636
d8ef542d 637 ret = pthread_join(channel_thread, &status);
72f11bd7
MD
638 if (ret) {
639 errno = ret;
640 PERROR("pthread_join channel_thread");
641 retval = -1;
d8ef542d 642 }
72f11bd7 643exit_channel_thread:
d8ef542d 644
13675d0e
MD
645exit_metadata_timer_thread:
646
5c635c72 647 ret = pthread_join(health_thread, &status);
72f11bd7
MD
648 if (ret) {
649 errno = ret;
650 PERROR("pthread_join health_thread");
651 retval = -1;
5c635c72 652 }
72f11bd7 653exit_health_thread:
5c635c72 654
5c635c72 655 utils_close_pipe(health_quit_pipe);
72f11bd7 656exit_health_pipe:
5c635c72 657
72f11bd7 658exit_init_data:
4d62fbf8
MD
659 /*
660 * Wait for all pending call_rcu work to complete before tearing
661 * down data structures. call_rcu worker may be trying to
662 * perform lookups in those structures.
663 */
664 rcu_barrier();
3bd1e081 665 lttng_consumer_cleanup();
13675d0e
MD
666 /*
667 * Tearing down the metadata timer thread in a
668 * non-fully-symmetric fashion compared to its creation in case
669 * lttng_consumer_cleanup() ends up tearing down timers (which
670 * requires the timer thread to be alive).
671 */
672 if (metadata_timer_thread_online) {
673 /*
674 * Ensure the metadata timer thread exits only after all other
675 * threads are gone, because it is required to perform timer
676 * teardown synchronization.
677 */
678 kill(getpid(), LTTNG_CONSUMER_SIG_EXIT);
679 ret = pthread_join(metadata_timer_thread, &status);
680 if (ret) {
681 errno = ret;
682 PERROR("pthread_join metadata_timer_thread");
683 retval = -1;
684 }
99b597c0
JR
685 ret = consumer_timer_thread_get_channel_monitor_pipe();
686 if (ret >= 0) {
687 ret = close(ret);
688 if (ret) {
689 PERROR("close channel monitor pipe");
690 }
691 }
13675d0e
MD
692 metadata_timer_thread_online = false;
693 }
d84afb7e 694 tmp_ctx = the_consumer_context;
cd9adb8b 695 the_consumer_context = nullptr;
28ab034a 696 cmm_barrier(); /* Clear ctx for signal handler. */
13675d0e 697 lttng_consumer_destroy(tmp_ctx);
72f11bd7 698
1fc79fb4
MD
699 if (health_consumerd) {
700 health_app_destroy(health_consumerd);
701 }
38303ec8
JG
702 /* Ensure all prior call_rcu are done. */
703 rcu_barrier();
d4a1283e 704
38303ec8 705 run_as_destroy_worker();
72f11bd7 706
38303ec8
JG
707exit_health_consumerd_cleanup:
708exit_options:
72f11bd7 709exit_set_signal_handler:
6bf8b7f3 710
fe19a07a
MD
711 rcu_unregister_thread();
712
72f11bd7
MD
713 if (!retval) {
714 exit(EXIT_SUCCESS);
715 } else {
716 exit(EXIT_FAILURE);
717 }
d4a1283e 718}
This page took 0.113419 seconds and 4 git commands to generate.