Commit | Line | Data |
---|---|---|
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 |
46 | static pthread_t channel_thread, data_thread, metadata_thread, sessiond_thread, |
47 | metadata_timer_thread, health_thread; | |
13675d0e | 48 | static bool metadata_timer_thread_online; |
d4a1283e | 49 | |
3183dbb0 | 50 | /* to count the number of times the user pressed ctrl+c */ |
13e44745 JD |
51 | static int sigintcount = 0; |
52 | ||
d4a1283e | 53 | /* Argument variables */ |
28ab034a JG |
54 | int lttng_opt_quiet; /* not static in error.h */ |
55 | int lttng_opt_verbose; /* not static in error.h */ | |
56 | int lttng_opt_mi; /* not static in error.h */ | |
c7e35b03 | 57 | |
d4a1283e JD |
58 | static int opt_daemon; |
59 | static const char *progname; | |
6533b585 DG |
60 | static char command_sock_path[PATH_MAX]; /* Global command socket path */ |
61 | static char error_sock_path[PATH_MAX]; /* Global error path */ | |
3bd1e081 | 62 | static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL; |
d4a1283e | 63 | |
7753dea8 | 64 | /* the liblttngconsumerd context */ |
d84afb7e | 65 | static struct lttng_consumer_local_data *the_consumer_context; |
cb040cc1 | 66 | |
1fc79fb4 MD |
67 | /* Consumerd health monitoring */ |
68 | struct health_app *health_consumerd; | |
69 | ||
6c71277b MD |
70 | const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
71 | ||
748b7b07 MD |
72 | int lttng_consumer_ready = NR_LTTNG_CONSUMER_READY; |
73 | ||
5c635c72 MD |
74 | enum 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 | 85 | static 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 JD |
117 | */ |
118 | static int set_signal_handler(void) | |
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; |
d4a1283e | 133 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
8fdf2d4d | 134 | PERROR("sigaction"); |
d4a1283e JD |
135 | return ret; |
136 | } | |
137 | ||
138 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
8fdf2d4d | 139 | PERROR("sigaction"); |
d4a1283e JD |
140 | return ret; |
141 | } | |
142 | ||
881fc67f MD |
143 | if ((ret = sigaction(SIGBUS, &sa, NULL)) < 0) { |
144 | PERROR("sigaction"); | |
145 | return ret; | |
146 | } | |
147 | ||
148 | sa.sa_flags = 0; | |
0072e5e2 | 149 | sa.sa_handler = SIG_IGN; |
d4a1283e | 150 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 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 | 161 | static 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 | 205 | static int parse_args(int argc, char **argv) |
d4a1283e | 206 | { |
8f7a281b | 207 | int c, ret = 0; |
d4a1283e | 208 | |
28ab034a JG |
209 | static struct option long_options[] = { { "consumerd-cmd-sock", 1, 0, 'c' }, |
210 | { "consumerd-err-sock", 1, 0, 'e' }, | |
211 | { "daemonize", 0, 0, 'd' }, | |
212 | { "group", 1, 0, 'g' }, | |
213 | { "help", 0, 0, 'h' }, | |
214 | { "quiet", 0, 0, 'q' }, | |
215 | { "verbose", 0, 0, 'v' }, | |
216 | { "version", 0, 0, 'V' }, | |
217 | { "kernel", 0, 0, 'k' }, | |
74d0b642 | 218 | #ifdef HAVE_LIBLTTNG_UST_CTL |
28ab034a | 219 | { "ust", 0, 0, 'u' }, |
3bd1e081 | 220 | #endif |
28ab034a | 221 | { NULL, 0, 0, 0 } }; |
d4a1283e JD |
222 | |
223 | while (1) { | |
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 |
303 | end: |
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 | */ | |
311 | static void set_ulimit(void) | |
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 | */ | |
329 | int 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 | ||
5e8e3a00 JG |
337 | if (run_as_create_worker(argv[0], NULL, NULL) < 0) { |
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 */ |
28ab034a JG |
445 | the_consumer_context = lttng_consumer_create( |
446 | opt_type, lttng_consumer_read_subbuffer, NULL, lttng_consumer_on_recv_stream, NULL); | |
d84afb7e | 447 | if (!the_consumer_context) { |
72f11bd7 MD |
448 | retval = -1; |
449 | goto exit_init_data; | |
cb040cc1 JD |
450 | } |
451 | ||
d84afb7e | 452 | lttng_consumer_set_command_sock_path(the_consumer_context, command_sock_path); |
9d035200 | 453 | if (*error_sock_path == '\0') { |
7753dea8 MD |
454 | switch (opt_type) { |
455 | case LTTNG_CONSUMER_KERNEL: | |
28ab034a JG |
456 | ret = snprintf(error_sock_path, |
457 | PATH_MAX, | |
458 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, | |
459 | DEFAULT_LTTNG_RUNDIR); | |
72f11bd7 MD |
460 | if (ret < 0) { |
461 | retval = -1; | |
462 | goto exit_init_data; | |
463 | } | |
7753dea8 MD |
464 | break; |
465 | case LTTNG_CONSUMER64_UST: | |
28ab034a JG |
466 | ret = snprintf(error_sock_path, |
467 | PATH_MAX, | |
468 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, | |
469 | DEFAULT_LTTNG_RUNDIR); | |
72f11bd7 MD |
470 | if (ret < 0) { |
471 | retval = -1; | |
472 | goto exit_init_data; | |
473 | } | |
7753dea8 MD |
474 | break; |
475 | case LTTNG_CONSUMER32_UST: | |
28ab034a JG |
476 | ret = snprintf(error_sock_path, |
477 | PATH_MAX, | |
478 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, | |
479 | DEFAULT_LTTNG_RUNDIR); | |
72f11bd7 MD |
480 | if (ret < 0) { |
481 | retval = -1; | |
482 | goto exit_init_data; | |
483 | } | |
7753dea8 MD |
484 | break; |
485 | default: | |
72f11bd7 MD |
486 | ERR("Unknown consumerd type"); |
487 | retval = -1; | |
488 | goto exit_init_data; | |
7753dea8 | 489 | } |
d4a1283e JD |
490 | } |
491 | ||
32258573 | 492 | /* Connect to the socket created by lttng-sessiond to report errors */ |
d4a1283e | 493 | DBG("Connecting to error socket %s", error_sock_path); |
1ce86c9a | 494 | ret = lttcomm_connect_unix_sock(error_sock_path); |
72f11bd7 MD |
495 | /* |
496 | * Not a fatal error, but all communication with lttng-sessiond will | |
497 | * fail. | |
498 | */ | |
1ce86c9a | 499 | if (ret < 0) { |
99bab54f | 500 | WARN("Cannot connect to error socket (is lttng-sessiond started?)"); |
d4a1283e | 501 | } |
d84afb7e | 502 | lttng_consumer_set_error_sock(the_consumer_context, ret); |
d4a1283e | 503 | |
331744e3 | 504 | /* |
d3e2ba59 JD |
505 | * Block RT signals used for UST periodical metadata flush and the live |
506 | * timer in main, and create a dedicated thread to handle these signals. | |
331744e3 | 507 | */ |
72f11bd7 MD |
508 | if (consumer_signal_init()) { |
509 | retval = -1; | |
510 | goto exit_init_data; | |
511 | } | |
d3e2ba59 | 512 | |
d84afb7e | 513 | the_consumer_context->type = opt_type; |
331744e3 | 514 | |
72f11bd7 MD |
515 | if (utils_create_pipe(health_quit_pipe)) { |
516 | retval = -1; | |
517 | goto exit_health_pipe; | |
5c635c72 MD |
518 | } |
519 | ||
520 | /* Create thread to manage the client socket */ | |
28ab034a JG |
521 | ret = pthread_create(&health_thread, |
522 | default_pthread_attr(), | |
523 | thread_manage_health_consumerd, | |
524 | (void *) NULL); | |
72f11bd7 MD |
525 | if (ret) { |
526 | errno = ret; | |
5c635c72 | 527 | PERROR("pthread_create health"); |
72f11bd7 MD |
528 | retval = -1; |
529 | goto exit_health_thread; | |
5c635c72 MD |
530 | } |
531 | ||
748b7b07 MD |
532 | /* |
533 | * Wait for health thread to be initialized before letting the | |
534 | * sessiond thread reply to the sessiond that we are ready. | |
535 | */ | |
536 | while (uatomic_read(<tng_consumer_ready)) { | |
1f3130d5 | 537 | usleep(100000); |
748b7b07 | 538 | } |
28ab034a | 539 | cmm_smp_mb(); /* Read ready before following operations */ |
748b7b07 | 540 | |
13675d0e MD |
541 | /* |
542 | * Create the thread to manage the UST metadata periodic timer and | |
543 | * live timer. | |
544 | */ | |
28ab034a JG |
545 | ret = pthread_create( |
546 | &metadata_timer_thread, NULL, consumer_timer_thread, (void *) the_consumer_context); | |
13675d0e MD |
547 | if (ret) { |
548 | errno = ret; | |
549 | PERROR("pthread_create"); | |
550 | retval = -1; | |
551 | goto exit_metadata_timer_thread; | |
552 | } | |
553 | metadata_timer_thread_online = true; | |
554 | ||
d8ef542d | 555 | /* Create thread to manage channels */ |
28ab034a JG |
556 | ret = pthread_create(&channel_thread, |
557 | default_pthread_attr(), | |
558 | consumer_thread_channel_poll, | |
559 | (void *) the_consumer_context); | |
72f11bd7 MD |
560 | if (ret) { |
561 | errno = ret; | |
562 | PERROR("pthread_create"); | |
563 | retval = -1; | |
564 | goto exit_channel_thread; | |
d8ef542d MD |
565 | } |
566 | ||
7d980def | 567 | /* Create thread to manage the polling/writing of trace metadata */ |
28ab034a JG |
568 | ret = pthread_create(&metadata_thread, |
569 | default_pthread_attr(), | |
570 | consumer_thread_metadata_poll, | |
571 | (void *) the_consumer_context); | |
72f11bd7 MD |
572 | if (ret) { |
573 | errno = ret; | |
574 | PERROR("pthread_create"); | |
575 | retval = -1; | |
576 | goto exit_metadata_thread; | |
7d980def DG |
577 | } |
578 | ||
579 | /* Create thread to manage the polling/writing of trace data */ | |
28ab034a JG |
580 | ret = pthread_create(&data_thread, |
581 | default_pthread_attr(), | |
582 | consumer_thread_data_poll, | |
583 | (void *) the_consumer_context); | |
72f11bd7 MD |
584 | if (ret) { |
585 | errno = ret; | |
586 | PERROR("pthread_create"); | |
587 | retval = -1; | |
588 | goto exit_data_thread; | |
d4a1283e JD |
589 | } |
590 | ||
7b336484 | 591 | /* Create the thread to manage the reception of fds */ |
28ab034a JG |
592 | ret = pthread_create(&sessiond_thread, |
593 | default_pthread_attr(), | |
594 | consumer_thread_sessiond_poll, | |
595 | (void *) the_consumer_context); | |
72f11bd7 MD |
596 | if (ret) { |
597 | errno = ret; | |
598 | PERROR("pthread_create"); | |
599 | retval = -1; | |
600 | goto exit_sessiond_thread; | |
df27ef7b DG |
601 | } |
602 | ||
72f11bd7 MD |
603 | /* |
604 | * This is where we start awaiting program completion (e.g. through | |
605 | * signal that asks threads to teardown. | |
606 | */ | |
607 | ||
df27ef7b | 608 | ret = pthread_join(sessiond_thread, &status); |
72f11bd7 MD |
609 | if (ret) { |
610 | errno = ret; | |
611 | PERROR("pthread_join sessiond_thread"); | |
612 | retval = -1; | |
d4a1283e | 613 | } |
72f11bd7 | 614 | exit_sessiond_thread: |
d4a1283e | 615 | |
df27ef7b | 616 | ret = pthread_join(data_thread, &status); |
72f11bd7 MD |
617 | if (ret) { |
618 | errno = ret; | |
619 | PERROR("pthread_join data_thread"); | |
620 | retval = -1; | |
df27ef7b | 621 | } |
72f11bd7 | 622 | exit_data_thread: |
df27ef7b | 623 | |
df27ef7b | 624 | ret = pthread_join(metadata_thread, &status); |
72f11bd7 MD |
625 | if (ret) { |
626 | errno = ret; | |
627 | PERROR("pthread_join metadata_thread"); | |
628 | retval = -1; | |
df27ef7b | 629 | } |
72f11bd7 | 630 | exit_metadata_thread: |
df27ef7b | 631 | |
d8ef542d | 632 | ret = pthread_join(channel_thread, &status); |
72f11bd7 MD |
633 | if (ret) { |
634 | errno = ret; | |
635 | PERROR("pthread_join channel_thread"); | |
636 | retval = -1; | |
d8ef542d | 637 | } |
72f11bd7 | 638 | exit_channel_thread: |
d8ef542d | 639 | |
13675d0e MD |
640 | exit_metadata_timer_thread: |
641 | ||
5c635c72 | 642 | ret = pthread_join(health_thread, &status); |
72f11bd7 MD |
643 | if (ret) { |
644 | errno = ret; | |
645 | PERROR("pthread_join health_thread"); | |
646 | retval = -1; | |
5c635c72 | 647 | } |
72f11bd7 | 648 | exit_health_thread: |
5c635c72 | 649 | |
5c635c72 | 650 | utils_close_pipe(health_quit_pipe); |
72f11bd7 | 651 | exit_health_pipe: |
5c635c72 | 652 | |
72f11bd7 | 653 | exit_init_data: |
4d62fbf8 MD |
654 | /* |
655 | * Wait for all pending call_rcu work to complete before tearing | |
656 | * down data structures. call_rcu worker may be trying to | |
657 | * perform lookups in those structures. | |
658 | */ | |
659 | rcu_barrier(); | |
3bd1e081 | 660 | lttng_consumer_cleanup(); |
13675d0e MD |
661 | /* |
662 | * Tearing down the metadata timer thread in a | |
663 | * non-fully-symmetric fashion compared to its creation in case | |
664 | * lttng_consumer_cleanup() ends up tearing down timers (which | |
665 | * requires the timer thread to be alive). | |
666 | */ | |
667 | if (metadata_timer_thread_online) { | |
668 | /* | |
669 | * Ensure the metadata timer thread exits only after all other | |
670 | * threads are gone, because it is required to perform timer | |
671 | * teardown synchronization. | |
672 | */ | |
673 | kill(getpid(), LTTNG_CONSUMER_SIG_EXIT); | |
674 | ret = pthread_join(metadata_timer_thread, &status); | |
675 | if (ret) { | |
676 | errno = ret; | |
677 | PERROR("pthread_join metadata_timer_thread"); | |
678 | retval = -1; | |
679 | } | |
99b597c0 JR |
680 | ret = consumer_timer_thread_get_channel_monitor_pipe(); |
681 | if (ret >= 0) { | |
682 | ret = close(ret); | |
683 | if (ret) { | |
684 | PERROR("close channel monitor pipe"); | |
685 | } | |
686 | } | |
13675d0e MD |
687 | metadata_timer_thread_online = false; |
688 | } | |
d84afb7e JG |
689 | tmp_ctx = the_consumer_context; |
690 | the_consumer_context = NULL; | |
28ab034a | 691 | cmm_barrier(); /* Clear ctx for signal handler. */ |
13675d0e | 692 | lttng_consumer_destroy(tmp_ctx); |
72f11bd7 | 693 | |
1fc79fb4 MD |
694 | if (health_consumerd) { |
695 | health_app_destroy(health_consumerd); | |
696 | } | |
38303ec8 JG |
697 | /* Ensure all prior call_rcu are done. */ |
698 | rcu_barrier(); | |
d4a1283e | 699 | |
38303ec8 | 700 | run_as_destroy_worker(); |
72f11bd7 | 701 | |
38303ec8 JG |
702 | exit_health_consumerd_cleanup: |
703 | exit_options: | |
72f11bd7 | 704 | exit_set_signal_handler: |
6bf8b7f3 | 705 | |
fe19a07a MD |
706 | rcu_unregister_thread(); |
707 | ||
72f11bd7 MD |
708 | if (!retval) { |
709 | exit(EXIT_SUCCESS); | |
710 | } else { | |
711 | exit(EXIT_FAILURE); | |
712 | } | |
d4a1283e | 713 | } |