Commit | Line | Data |
---|---|---|
d4a1283e JD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
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. | |
d4a1283e JD |
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 | * | |
d14d33bf AM |
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. | |
d4a1283e JD |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
6c1c0768 | 20 | #define _LGPL_SOURCE |
d4a1283e JD |
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> | |
1ccfc0e3 | 31 | #include <sys/resource.h> |
d4a1283e JD |
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> | |
03424a9b | 39 | #include <sys/mman.h> |
5348b470 | 40 | #include <assert.h> |
3bd1e081 | 41 | #include <config.h> |
7753dea8 | 42 | #include <urcu/compiler.h> |
1ccfc0e3 | 43 | #include <ulimit.h> |
d4a1283e | 44 | |
db758600 DG |
45 | #include <common/defaults.h> |
46 | #include <common/common.h> | |
f02e1e8a | 47 | #include <common/consumer.h> |
331744e3 | 48 | #include <common/consumer-timer.h> |
fb3a43a9 | 49 | #include <common/compat/poll.h> |
e8fa9fb0 | 50 | #include <common/compat/getenv.h> |
10a8a223 | 51 | #include <common/sessiond-comm/sessiond-comm.h> |
5c635c72 | 52 | #include <common/utils.h> |
10a8a223 DG |
53 | |
54 | #include "lttng-consumerd.h" | |
1fc79fb4 | 55 | #include "health-consumerd.h" |
d4a1283e | 56 | |
d8ef542d | 57 | /* threads (channel handling, poll, metadata, sessiond) */ |
331744e3 | 58 | |
5c635c72 MD |
59 | static pthread_t channel_thread, data_thread, metadata_thread, |
60 | sessiond_thread, metadata_timer_thread, health_thread; | |
d4a1283e | 61 | |
3183dbb0 | 62 | /* to count the number of times the user pressed ctrl+c */ |
13e44745 JD |
63 | static int sigintcount = 0; |
64 | ||
d4a1283e | 65 | /* Argument variables */ |
97e19046 DG |
66 | int lttng_opt_quiet; /* not static in error.h */ |
67 | int lttng_opt_verbose; /* not static in error.h */ | |
c7e35b03 JR |
68 | int lttng_opt_mi; /* not static in error.h */ |
69 | ||
d4a1283e JD |
70 | static int opt_daemon; |
71 | static const char *progname; | |
6533b585 DG |
72 | static char command_sock_path[PATH_MAX]; /* Global command socket path */ |
73 | static char error_sock_path[PATH_MAX]; /* Global error path */ | |
3bd1e081 | 74 | static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL; |
d4a1283e | 75 | |
7753dea8 | 76 | /* the liblttngconsumerd context */ |
3bd1e081 | 77 | static struct lttng_consumer_local_data *ctx; |
cb040cc1 | 78 | |
1fc79fb4 MD |
79 | /* Consumerd health monitoring */ |
80 | struct health_app *health_consumerd; | |
81 | ||
6c71277b MD |
82 | const char *tracing_group_name = DEFAULT_TRACING_GROUP; |
83 | ||
748b7b07 MD |
84 | int lttng_consumer_ready = NR_LTTNG_CONSUMER_READY; |
85 | ||
5c635c72 MD |
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 | ||
d4a1283e | 94 | /* |
6533b585 | 95 | * Signal handler for the daemon |
d4a1283e JD |
96 | */ |
97 | static void sighandler(int sig) | |
98 | { | |
13e44745 JD |
99 | if (sig == SIGINT && sigintcount++ == 0) { |
100 | DBG("ignoring first SIGINT"); | |
101 | return; | |
102 | } | |
103 | ||
ab1027f4 DG |
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 | ||
6a0caa9b MD |
112 | if (ctx) { |
113 | lttng_consumer_should_exit(ctx); | |
114 | } | |
d4a1283e JD |
115 | } |
116 | ||
117 | /* | |
6533b585 | 118 | * Setup signal handler for : |
d4a1283e JD |
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) { | |
8fdf2d4d | 128 | PERROR("sigemptyset"); |
d4a1283e JD |
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) { | |
8fdf2d4d | 136 | PERROR("sigaction"); |
d4a1283e JD |
137 | return ret; |
138 | } | |
139 | ||
140 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
8fdf2d4d | 141 | PERROR("sigaction"); |
d4a1283e JD |
142 | return ret; |
143 | } | |
144 | ||
145 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
8fdf2d4d | 146 | PERROR("sigaction"); |
d4a1283e JD |
147 | return ret; |
148 | } | |
149 | ||
150 | return ret; | |
151 | } | |
152 | ||
d4a1283e | 153 | /* |
3183dbb0 | 154 | * Usage function on stream file. |
d4a1283e | 155 | */ |
3183dbb0 | 156 | static void usage(FILE *fp) |
d4a1283e | 157 | { |
3183dbb0 DG |
158 | fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
159 | fprintf(fp, " -h, --help " | |
d4a1283e | 160 | "Display this usage.\n"); |
6c71277b | 161 | fprintf(fp, " -c, --consumerd-cmd-sock PATH " |
d4a1283e | 162 | "Specify path for the command socket\n"); |
6c71277b | 163 | fprintf(fp, " -e, --consumerd-err-sock PATH " |
d4a1283e | 164 | "Specify path for the error socket\n"); |
3183dbb0 | 165 | fprintf(fp, " -d, --daemonize " |
d4a1283e | 166 | "Start as a daemon.\n"); |
3183dbb0 | 167 | fprintf(fp, " -q, --quiet " |
d4a1283e | 168 | "No output at all.\n"); |
3183dbb0 | 169 | fprintf(fp, " -v, --verbose " |
d4a1283e | 170 | "Verbose mode. Activate DBG() macro.\n"); |
3183dbb0 | 171 | fprintf(fp, " -V, --version " |
d4a1283e | 172 | "Show version number.\n"); |
6c71277b MD |
173 | fprintf(fp, " -g, --group NAME " |
174 | "Specify the tracing group name. (default: tracing)\n"); | |
3183dbb0 | 175 | fprintf(fp, " -k, --kernel " |
3bd1e081 | 176 | "Consumer kernel buffers (default).\n"); |
3183dbb0 | 177 | fprintf(fp, " -u, --ust " |
3bd1e081 | 178 | "Consumer UST buffers.%s\n", |
74d0b642 | 179 | #ifdef HAVE_LIBLTTNG_UST_CTL |
3bd1e081 MD |
180 | "" |
181 | #else | |
182 | " (support not compiled in)" | |
183 | #endif | |
184 | ); | |
d4a1283e JD |
185 | } |
186 | ||
187 | /* | |
188 | * daemon argument parsing | |
189 | */ | |
8f7a281b | 190 | static int parse_args(int argc, char **argv) |
d4a1283e | 191 | { |
8f7a281b | 192 | int c, ret = 0; |
d4a1283e JD |
193 | |
194 | static struct option long_options[] = { | |
7753dea8 MD |
195 | { "consumerd-cmd-sock", 1, 0, 'c' }, |
196 | { "consumerd-err-sock", 1, 0, 'e' }, | |
d4a1283e | 197 | { "daemonize", 0, 0, 'd' }, |
6c71277b | 198 | { "group", 1, 0, 'g' }, |
d4a1283e JD |
199 | { "help", 0, 0, 'h' }, |
200 | { "quiet", 0, 0, 'q' }, | |
201 | { "verbose", 0, 0, 'v' }, | |
202 | { "version", 0, 0, 'V' }, | |
3bd1e081 | 203 | { "kernel", 0, 0, 'k' }, |
74d0b642 | 204 | #ifdef HAVE_LIBLTTNG_UST_CTL |
3bd1e081 MD |
205 | { "ust", 0, 0, 'u' }, |
206 | #endif | |
d4a1283e JD |
207 | { NULL, 0, 0, 0 } |
208 | }; | |
209 | ||
210 | while (1) { | |
211 | int option_index = 0; | |
5acdb082 MD |
212 | c = getopt_long(argc, argv, "dhqvVku" "c:e:g:", |
213 | long_options, &option_index); | |
d4a1283e JD |
214 | if (c == -1) { |
215 | break; | |
216 | } | |
217 | ||
218 | switch (c) { | |
914a571b | 219 | case 0: |
5acdb082 MD |
220 | fprintf(stderr, "option %s", |
221 | long_options[option_index].name); | |
914a571b JD |
222 | if (optarg) { |
223 | fprintf(stderr, " with arg %s\n", optarg); | |
8f7a281b MD |
224 | ret = -1; |
225 | goto end; | |
914a571b JD |
226 | } |
227 | break; | |
228 | case 'c': | |
e8fa9fb0 MD |
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 | } | |
914a571b JD |
235 | break; |
236 | case 'e': | |
e8fa9fb0 MD |
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 | } | |
914a571b JD |
243 | break; |
244 | case 'd': | |
245 | opt_daemon = 1; | |
246 | break; | |
6c71277b | 247 | case 'g': |
e8fa9fb0 MD |
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 | } | |
6c71277b | 254 | break; |
914a571b | 255 | case 'h': |
3183dbb0 DG |
256 | usage(stdout); |
257 | exit(EXIT_SUCCESS); | |
914a571b | 258 | case 'q': |
97e19046 | 259 | lttng_opt_quiet = 1; |
914a571b JD |
260 | break; |
261 | case 'v': | |
97e19046 | 262 | lttng_opt_verbose = 1; |
914a571b JD |
263 | break; |
264 | case 'V': | |
265 | fprintf(stdout, "%s\n", VERSION); | |
266 | exit(EXIT_SUCCESS); | |
3bd1e081 MD |
267 | case 'k': |
268 | opt_type = LTTNG_CONSUMER_KERNEL; | |
269 | break; | |
74d0b642 | 270 | #ifdef HAVE_LIBLTTNG_UST_CTL |
3bd1e081 | 271 | case 'u': |
7753dea8 MD |
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 | |
3bd1e081 MD |
279 | break; |
280 | #endif | |
914a571b | 281 | default: |
3183dbb0 | 282 | usage(stderr); |
8f7a281b MD |
283 | ret = -1; |
284 | goto end; | |
d4a1283e JD |
285 | } |
286 | } | |
8f7a281b MD |
287 | end: |
288 | return ret; | |
d4a1283e JD |
289 | } |
290 | ||
1ccfc0e3 DG |
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 | ||
d4a1283e JD |
310 | /* |
311 | * main | |
312 | */ | |
313 | int main(int argc, char **argv) | |
314 | { | |
72f11bd7 | 315 | int ret = 0, retval = 0; |
d4a1283e | 316 | void *status; |
6a0caa9b | 317 | struct lttng_consumer_local_data *tmp_ctx; |
d4a1283e | 318 | |
72f11bd7 MD |
319 | if (set_signal_handler()) { |
320 | retval = -1; | |
321 | goto exit_set_signal_handler; | |
322 | } | |
323 | ||
d4a1283e JD |
324 | /* Parse arguments */ |
325 | progname = argv[0]; | |
72f11bd7 MD |
326 | if (parse_args(argc, argv)) { |
327 | retval = -1; | |
328 | goto exit_options; | |
329 | } | |
d4a1283e JD |
330 | |
331 | /* Daemonize */ | |
332 | if (opt_daemon) { | |
ceed52b5 MD |
333 | int i; |
334 | ||
335 | /* | |
336 | * fork | |
337 | * child: setsid, close FD 0, 1, 2, chdir / | |
338 | * parent: exit (if fork is successful) | |
339 | */ | |
d4a1283e JD |
340 | ret = daemon(0, 0); |
341 | if (ret < 0) { | |
ceed52b5 | 342 | PERROR("daemon"); |
72f11bd7 MD |
343 | retval = -1; |
344 | goto exit_options; | |
d4a1283e | 345 | } |
ceed52b5 MD |
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 | } | |
d4a1283e JD |
354 | } |
355 | ||
72f11bd7 MD |
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 | ||
fb3a43a9 | 367 | /* Set up max poll set size */ |
25b397f9 MD |
368 | if (lttng_poll_set_max_size()) { |
369 | retval = -1; | |
370 | goto exit_init_data; | |
371 | } | |
fb3a43a9 | 372 | |
9d035200 | 373 | if (*command_sock_path == '\0') { |
7753dea8 MD |
374 | switch (opt_type) { |
375 | case LTTNG_CONSUMER_KERNEL: | |
72f11bd7 MD |
376 | ret = snprintf(command_sock_path, PATH_MAX, |
377 | DEFAULT_KCONSUMERD_CMD_SOCK_PATH, | |
990570ed | 378 | DEFAULT_LTTNG_RUNDIR); |
72f11bd7 MD |
379 | if (ret < 0) { |
380 | retval = -1; | |
381 | goto exit_init_data; | |
382 | } | |
7753dea8 MD |
383 | break; |
384 | case LTTNG_CONSUMER64_UST: | |
72f11bd7 MD |
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 | } | |
7753dea8 MD |
392 | break; |
393 | case LTTNG_CONSUMER32_UST: | |
72f11bd7 MD |
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 | } | |
7753dea8 MD |
401 | break; |
402 | default: | |
72f11bd7 MD |
403 | ERR("Unknown consumerd type"); |
404 | retval = -1; | |
405 | goto exit_init_data; | |
7753dea8 | 406 | } |
d4a1283e | 407 | } |
e4421fec DG |
408 | |
409 | /* Init */ | |
72f11bd7 MD |
410 | if (lttng_consumer_init()) { |
411 | retval = -1; | |
412 | goto exit_init_data; | |
282dadbc MD |
413 | } |
414 | ||
72f11bd7 | 415 | /* Initialize communication library */ |
e98ec547 | 416 | lttcomm_init(); |
72f11bd7 | 417 | /* Initialize TCP timeout values */ |
e98ec547 | 418 | lttcomm_inet_init(); |
e4421fec | 419 | |
1ccfc0e3 DG |
420 | if (!getuid()) { |
421 | /* Set limit for open files */ | |
422 | set_ulimit(); | |
423 | } | |
424 | ||
5348b470 | 425 | /* create the consumer instance with and assign the callbacks */ |
d41f73b7 MD |
426 | ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer, |
427 | NULL, lttng_consumer_on_recv_stream, NULL); | |
72f11bd7 MD |
428 | if (!ctx) { |
429 | retval = -1; | |
430 | goto exit_init_data; | |
cb040cc1 JD |
431 | } |
432 | ||
3bd1e081 | 433 | lttng_consumer_set_command_sock_path(ctx, command_sock_path); |
9d035200 | 434 | if (*error_sock_path == '\0') { |
7753dea8 MD |
435 | switch (opt_type) { |
436 | case LTTNG_CONSUMER_KERNEL: | |
72f11bd7 MD |
437 | ret = snprintf(error_sock_path, PATH_MAX, |
438 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, | |
990570ed | 439 | DEFAULT_LTTNG_RUNDIR); |
72f11bd7 MD |
440 | if (ret < 0) { |
441 | retval = -1; | |
442 | goto exit_init_data; | |
443 | } | |
7753dea8 MD |
444 | break; |
445 | case LTTNG_CONSUMER64_UST: | |
72f11bd7 MD |
446 | ret = snprintf(error_sock_path, PATH_MAX, |
447 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, | |
448 | DEFAULT_LTTNG_RUNDIR); | |
449 | if (ret < 0) { | |
450 | retval = -1; | |
451 | goto exit_init_data; | |
452 | } | |
7753dea8 MD |
453 | break; |
454 | case LTTNG_CONSUMER32_UST: | |
72f11bd7 MD |
455 | ret = snprintf(error_sock_path, PATH_MAX, |
456 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, | |
457 | DEFAULT_LTTNG_RUNDIR); | |
458 | if (ret < 0) { | |
459 | retval = -1; | |
460 | goto exit_init_data; | |
461 | } | |
7753dea8 MD |
462 | break; |
463 | default: | |
72f11bd7 MD |
464 | ERR("Unknown consumerd type"); |
465 | retval = -1; | |
466 | goto exit_init_data; | |
7753dea8 | 467 | } |
d4a1283e JD |
468 | } |
469 | ||
32258573 | 470 | /* Connect to the socket created by lttng-sessiond to report errors */ |
d4a1283e | 471 | DBG("Connecting to error socket %s", error_sock_path); |
1ce86c9a | 472 | ret = lttcomm_connect_unix_sock(error_sock_path); |
72f11bd7 MD |
473 | /* |
474 | * Not a fatal error, but all communication with lttng-sessiond will | |
475 | * fail. | |
476 | */ | |
1ce86c9a | 477 | if (ret < 0) { |
99bab54f | 478 | WARN("Cannot connect to error socket (is lttng-sessiond started?)"); |
d4a1283e | 479 | } |
3bd1e081 | 480 | lttng_consumer_set_error_sock(ctx, ret); |
d4a1283e | 481 | |
331744e3 | 482 | /* |
d3e2ba59 JD |
483 | * Block RT signals used for UST periodical metadata flush and the live |
484 | * timer in main, and create a dedicated thread to handle these signals. | |
331744e3 | 485 | */ |
72f11bd7 MD |
486 | if (consumer_signal_init()) { |
487 | retval = -1; | |
488 | goto exit_init_data; | |
489 | } | |
d3e2ba59 | 490 | |
331744e3 JD |
491 | ctx->type = opt_type; |
492 | ||
72f11bd7 MD |
493 | if (utils_create_pipe(health_quit_pipe)) { |
494 | retval = -1; | |
495 | goto exit_health_pipe; | |
5c635c72 MD |
496 | } |
497 | ||
498 | /* Create thread to manage the client socket */ | |
499 | ret = pthread_create(&health_thread, NULL, | |
500 | thread_manage_health, (void *) NULL); | |
72f11bd7 MD |
501 | if (ret) { |
502 | errno = ret; | |
5c635c72 | 503 | PERROR("pthread_create health"); |
72f11bd7 MD |
504 | retval = -1; |
505 | goto exit_health_thread; | |
5c635c72 MD |
506 | } |
507 | ||
748b7b07 MD |
508 | /* |
509 | * Wait for health thread to be initialized before letting the | |
510 | * sessiond thread reply to the sessiond that we are ready. | |
511 | */ | |
512 | while (uatomic_read(<tng_consumer_ready)) { | |
1f3130d5 | 513 | usleep(100000); |
748b7b07 MD |
514 | } |
515 | cmm_smp_mb(); /* Read ready before following operations */ | |
516 | ||
d8ef542d | 517 | /* Create thread to manage channels */ |
72f11bd7 MD |
518 | ret = pthread_create(&channel_thread, NULL, |
519 | consumer_thread_channel_poll, | |
d8ef542d | 520 | (void *) ctx); |
72f11bd7 MD |
521 | if (ret) { |
522 | errno = ret; | |
523 | PERROR("pthread_create"); | |
524 | retval = -1; | |
525 | goto exit_channel_thread; | |
d8ef542d MD |
526 | } |
527 | ||
7d980def | 528 | /* Create thread to manage the polling/writing of trace metadata */ |
72f11bd7 MD |
529 | ret = pthread_create(&metadata_thread, NULL, |
530 | consumer_thread_metadata_poll, | |
7d980def | 531 | (void *) ctx); |
72f11bd7 MD |
532 | if (ret) { |
533 | errno = ret; | |
534 | PERROR("pthread_create"); | |
535 | retval = -1; | |
536 | goto exit_metadata_thread; | |
7d980def DG |
537 | } |
538 | ||
539 | /* Create thread to manage the polling/writing of trace data */ | |
df27ef7b | 540 | ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll, |
cb040cc1 | 541 | (void *) ctx); |
72f11bd7 MD |
542 | if (ret) { |
543 | errno = ret; | |
544 | PERROR("pthread_create"); | |
545 | retval = -1; | |
546 | goto exit_data_thread; | |
d4a1283e JD |
547 | } |
548 | ||
7d980def | 549 | /* Create the thread to manage the receive of fd */ |
72f11bd7 MD |
550 | ret = pthread_create(&sessiond_thread, NULL, |
551 | consumer_thread_sessiond_poll, | |
cb040cc1 | 552 | (void *) ctx); |
72f11bd7 MD |
553 | if (ret) { |
554 | errno = ret; | |
555 | PERROR("pthread_create"); | |
556 | retval = -1; | |
557 | goto exit_sessiond_thread; | |
df27ef7b DG |
558 | } |
559 | ||
d3e2ba59 JD |
560 | /* |
561 | * Create the thread to manage the UST metadata periodic timer and | |
562 | * live timer. | |
563 | */ | |
564 | ret = pthread_create(&metadata_timer_thread, NULL, | |
565 | consumer_timer_thread, (void *) ctx); | |
72f11bd7 MD |
566 | if (ret) { |
567 | errno = ret; | |
568 | PERROR("pthread_create"); | |
569 | retval = -1; | |
570 | goto exit_metadata_timer_thread; | |
d3e2ba59 | 571 | } |
331744e3 | 572 | |
d3e2ba59 JD |
573 | ret = pthread_detach(metadata_timer_thread); |
574 | if (ret) { | |
575 | errno = ret; | |
72f11bd7 MD |
576 | PERROR("pthread_detach"); |
577 | retval = -1; | |
578 | goto exit_metadata_timer_detach; | |
331744e3 JD |
579 | } |
580 | ||
72f11bd7 MD |
581 | /* |
582 | * This is where we start awaiting program completion (e.g. through | |
583 | * signal that asks threads to teardown. | |
584 | */ | |
585 | ||
586 | exit_metadata_timer_detach: | |
587 | exit_metadata_timer_thread: | |
df27ef7b | 588 | ret = pthread_join(sessiond_thread, &status); |
72f11bd7 MD |
589 | if (ret) { |
590 | errno = ret; | |
591 | PERROR("pthread_join sessiond_thread"); | |
592 | retval = -1; | |
d4a1283e | 593 | } |
72f11bd7 | 594 | exit_sessiond_thread: |
d4a1283e | 595 | |
df27ef7b | 596 | ret = pthread_join(data_thread, &status); |
72f11bd7 MD |
597 | if (ret) { |
598 | errno = ret; | |
599 | PERROR("pthread_join data_thread"); | |
600 | retval = -1; | |
df27ef7b | 601 | } |
72f11bd7 | 602 | exit_data_thread: |
df27ef7b | 603 | |
df27ef7b | 604 | ret = pthread_join(metadata_thread, &status); |
72f11bd7 MD |
605 | if (ret) { |
606 | errno = ret; | |
607 | PERROR("pthread_join metadata_thread"); | |
608 | retval = -1; | |
df27ef7b | 609 | } |
72f11bd7 | 610 | exit_metadata_thread: |
df27ef7b | 611 | |
d8ef542d | 612 | ret = pthread_join(channel_thread, &status); |
72f11bd7 MD |
613 | if (ret) { |
614 | errno = ret; | |
615 | PERROR("pthread_join channel_thread"); | |
616 | retval = -1; | |
d8ef542d | 617 | } |
72f11bd7 | 618 | exit_channel_thread: |
d8ef542d | 619 | |
5c635c72 | 620 | ret = pthread_join(health_thread, &status); |
72f11bd7 MD |
621 | if (ret) { |
622 | errno = ret; | |
623 | PERROR("pthread_join health_thread"); | |
624 | retval = -1; | |
5c635c72 | 625 | } |
72f11bd7 | 626 | exit_health_thread: |
5c635c72 | 627 | |
5c635c72 | 628 | utils_close_pipe(health_quit_pipe); |
72f11bd7 | 629 | exit_health_pipe: |
5c635c72 | 630 | |
72f11bd7 | 631 | exit_init_data: |
6a0caa9b MD |
632 | tmp_ctx = ctx; |
633 | ctx = NULL; | |
634 | cmm_barrier(); /* Clear ctx for signal handler. */ | |
635 | lttng_consumer_destroy(tmp_ctx); | |
3bd1e081 | 636 | lttng_consumer_cleanup(); |
72f11bd7 | 637 | |
1fc79fb4 MD |
638 | if (health_consumerd) { |
639 | health_app_destroy(health_consumerd); | |
640 | } | |
72f11bd7 | 641 | exit_health_consumerd_cleanup: |
d4a1283e | 642 | |
72f11bd7 MD |
643 | exit_options: |
644 | ||
645 | exit_set_signal_handler: | |
6bf8b7f3 MD |
646 | /* Ensure all prior call_rcu are done. */ |
647 | rcu_barrier(); | |
648 | ||
72f11bd7 MD |
649 | if (!retval) { |
650 | exit(EXIT_SUCCESS); | |
651 | } else { | |
652 | exit(EXIT_FAILURE); | |
653 | } | |
d4a1283e | 654 | } |