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 | ||
a04821e0 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; |
a04821e0 | 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 | ||
0e904f3e MD |
425 | if (run_as_create_worker(argv[0]) < 0) { |
426 | goto exit_init_data; | |
427 | } | |
428 | ||
5348b470 | 429 | /* create the consumer instance with and assign the callbacks */ |
d41f73b7 MD |
430 | ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer, |
431 | NULL, lttng_consumer_on_recv_stream, NULL); | |
72f11bd7 MD |
432 | if (!ctx) { |
433 | retval = -1; | |
434 | goto exit_init_data; | |
cb040cc1 JD |
435 | } |
436 | ||
3bd1e081 | 437 | lttng_consumer_set_command_sock_path(ctx, command_sock_path); |
9d035200 | 438 | if (*error_sock_path == '\0') { |
7753dea8 MD |
439 | switch (opt_type) { |
440 | case LTTNG_CONSUMER_KERNEL: | |
72f11bd7 MD |
441 | ret = snprintf(error_sock_path, PATH_MAX, |
442 | DEFAULT_KCONSUMERD_ERR_SOCK_PATH, | |
990570ed | 443 | DEFAULT_LTTNG_RUNDIR); |
72f11bd7 MD |
444 | if (ret < 0) { |
445 | retval = -1; | |
446 | goto exit_init_data; | |
447 | } | |
7753dea8 MD |
448 | break; |
449 | case LTTNG_CONSUMER64_UST: | |
72f11bd7 MD |
450 | ret = snprintf(error_sock_path, PATH_MAX, |
451 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, | |
452 | DEFAULT_LTTNG_RUNDIR); | |
453 | if (ret < 0) { | |
454 | retval = -1; | |
455 | goto exit_init_data; | |
456 | } | |
7753dea8 MD |
457 | break; |
458 | case LTTNG_CONSUMER32_UST: | |
72f11bd7 MD |
459 | ret = snprintf(error_sock_path, PATH_MAX, |
460 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, | |
461 | DEFAULT_LTTNG_RUNDIR); | |
462 | if (ret < 0) { | |
463 | retval = -1; | |
464 | goto exit_init_data; | |
465 | } | |
7753dea8 MD |
466 | break; |
467 | default: | |
72f11bd7 MD |
468 | ERR("Unknown consumerd type"); |
469 | retval = -1; | |
470 | goto exit_init_data; | |
7753dea8 | 471 | } |
d4a1283e JD |
472 | } |
473 | ||
32258573 | 474 | /* Connect to the socket created by lttng-sessiond to report errors */ |
d4a1283e | 475 | DBG("Connecting to error socket %s", error_sock_path); |
1ce86c9a | 476 | ret = lttcomm_connect_unix_sock(error_sock_path); |
72f11bd7 MD |
477 | /* |
478 | * Not a fatal error, but all communication with lttng-sessiond will | |
479 | * fail. | |
480 | */ | |
1ce86c9a | 481 | if (ret < 0) { |
99bab54f | 482 | WARN("Cannot connect to error socket (is lttng-sessiond started?)"); |
d4a1283e | 483 | } |
3bd1e081 | 484 | lttng_consumer_set_error_sock(ctx, ret); |
d4a1283e | 485 | |
331744e3 | 486 | /* |
d3e2ba59 JD |
487 | * Block RT signals used for UST periodical metadata flush and the live |
488 | * timer in main, and create a dedicated thread to handle these signals. | |
331744e3 | 489 | */ |
72f11bd7 MD |
490 | if (consumer_signal_init()) { |
491 | retval = -1; | |
492 | goto exit_init_data; | |
493 | } | |
d3e2ba59 | 494 | |
331744e3 JD |
495 | ctx->type = opt_type; |
496 | ||
72f11bd7 MD |
497 | if (utils_create_pipe(health_quit_pipe)) { |
498 | retval = -1; | |
499 | goto exit_health_pipe; | |
5c635c72 MD |
500 | } |
501 | ||
502 | /* Create thread to manage the client socket */ | |
503 | ret = pthread_create(&health_thread, NULL, | |
504 | thread_manage_health, (void *) NULL); | |
72f11bd7 MD |
505 | if (ret) { |
506 | errno = ret; | |
5c635c72 | 507 | PERROR("pthread_create health"); |
72f11bd7 MD |
508 | retval = -1; |
509 | goto exit_health_thread; | |
5c635c72 MD |
510 | } |
511 | ||
748b7b07 MD |
512 | /* |
513 | * Wait for health thread to be initialized before letting the | |
514 | * sessiond thread reply to the sessiond that we are ready. | |
515 | */ | |
516 | while (uatomic_read(<tng_consumer_ready)) { | |
1f3130d5 | 517 | usleep(100000); |
748b7b07 MD |
518 | } |
519 | cmm_smp_mb(); /* Read ready before following operations */ | |
520 | ||
d8ef542d | 521 | /* Create thread to manage channels */ |
72f11bd7 MD |
522 | ret = pthread_create(&channel_thread, NULL, |
523 | consumer_thread_channel_poll, | |
d8ef542d | 524 | (void *) ctx); |
72f11bd7 MD |
525 | if (ret) { |
526 | errno = ret; | |
527 | PERROR("pthread_create"); | |
528 | retval = -1; | |
529 | goto exit_channel_thread; | |
d8ef542d MD |
530 | } |
531 | ||
7d980def | 532 | /* Create thread to manage the polling/writing of trace metadata */ |
72f11bd7 MD |
533 | ret = pthread_create(&metadata_thread, NULL, |
534 | consumer_thread_metadata_poll, | |
7d980def | 535 | (void *) ctx); |
72f11bd7 MD |
536 | if (ret) { |
537 | errno = ret; | |
538 | PERROR("pthread_create"); | |
539 | retval = -1; | |
540 | goto exit_metadata_thread; | |
7d980def DG |
541 | } |
542 | ||
543 | /* Create thread to manage the polling/writing of trace data */ | |
df27ef7b | 544 | ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll, |
cb040cc1 | 545 | (void *) ctx); |
72f11bd7 MD |
546 | if (ret) { |
547 | errno = ret; | |
548 | PERROR("pthread_create"); | |
549 | retval = -1; | |
550 | goto exit_data_thread; | |
d4a1283e JD |
551 | } |
552 | ||
7d980def | 553 | /* Create the thread to manage the receive of fd */ |
72f11bd7 MD |
554 | ret = pthread_create(&sessiond_thread, NULL, |
555 | consumer_thread_sessiond_poll, | |
cb040cc1 | 556 | (void *) ctx); |
72f11bd7 MD |
557 | if (ret) { |
558 | errno = ret; | |
559 | PERROR("pthread_create"); | |
560 | retval = -1; | |
561 | goto exit_sessiond_thread; | |
df27ef7b DG |
562 | } |
563 | ||
d3e2ba59 JD |
564 | /* |
565 | * Create the thread to manage the UST metadata periodic timer and | |
566 | * live timer. | |
567 | */ | |
568 | ret = pthread_create(&metadata_timer_thread, NULL, | |
569 | consumer_timer_thread, (void *) ctx); | |
72f11bd7 MD |
570 | if (ret) { |
571 | errno = ret; | |
572 | PERROR("pthread_create"); | |
573 | retval = -1; | |
574 | goto exit_metadata_timer_thread; | |
d3e2ba59 | 575 | } |
331744e3 | 576 | |
d3e2ba59 JD |
577 | ret = pthread_detach(metadata_timer_thread); |
578 | if (ret) { | |
579 | errno = ret; | |
72f11bd7 MD |
580 | PERROR("pthread_detach"); |
581 | retval = -1; | |
582 | goto exit_metadata_timer_detach; | |
331744e3 JD |
583 | } |
584 | ||
72f11bd7 MD |
585 | /* |
586 | * This is where we start awaiting program completion (e.g. through | |
587 | * signal that asks threads to teardown. | |
588 | */ | |
589 | ||
590 | exit_metadata_timer_detach: | |
591 | exit_metadata_timer_thread: | |
df27ef7b | 592 | ret = pthread_join(sessiond_thread, &status); |
72f11bd7 MD |
593 | if (ret) { |
594 | errno = ret; | |
595 | PERROR("pthread_join sessiond_thread"); | |
596 | retval = -1; | |
d4a1283e | 597 | } |
72f11bd7 | 598 | exit_sessiond_thread: |
d4a1283e | 599 | |
df27ef7b | 600 | ret = pthread_join(data_thread, &status); |
72f11bd7 MD |
601 | if (ret) { |
602 | errno = ret; | |
603 | PERROR("pthread_join data_thread"); | |
604 | retval = -1; | |
df27ef7b | 605 | } |
72f11bd7 | 606 | exit_data_thread: |
df27ef7b | 607 | |
df27ef7b | 608 | ret = pthread_join(metadata_thread, &status); |
72f11bd7 MD |
609 | if (ret) { |
610 | errno = ret; | |
611 | PERROR("pthread_join metadata_thread"); | |
612 | retval = -1; | |
df27ef7b | 613 | } |
72f11bd7 | 614 | exit_metadata_thread: |
df27ef7b | 615 | |
d8ef542d | 616 | ret = pthread_join(channel_thread, &status); |
72f11bd7 MD |
617 | if (ret) { |
618 | errno = ret; | |
619 | PERROR("pthread_join channel_thread"); | |
620 | retval = -1; | |
d8ef542d | 621 | } |
72f11bd7 | 622 | exit_channel_thread: |
d8ef542d | 623 | |
5c635c72 | 624 | ret = pthread_join(health_thread, &status); |
72f11bd7 MD |
625 | if (ret) { |
626 | errno = ret; | |
627 | PERROR("pthread_join health_thread"); | |
628 | retval = -1; | |
5c635c72 | 629 | } |
72f11bd7 | 630 | exit_health_thread: |
5c635c72 | 631 | |
5c635c72 | 632 | utils_close_pipe(health_quit_pipe); |
72f11bd7 | 633 | exit_health_pipe: |
5c635c72 | 634 | |
72f11bd7 | 635 | exit_init_data: |
a04821e0 MD |
636 | tmp_ctx = ctx; |
637 | ctx = NULL; | |
638 | cmm_barrier(); /* Clear ctx for signal handler. */ | |
639 | lttng_consumer_destroy(tmp_ctx); | |
3bd1e081 | 640 | lttng_consumer_cleanup(); |
72f11bd7 | 641 | |
1fc79fb4 MD |
642 | if (health_consumerd) { |
643 | health_app_destroy(health_consumerd); | |
644 | } | |
9c691628 JG |
645 | /* Ensure all prior call_rcu are done. */ |
646 | rcu_barrier(); | |
d4a1283e | 647 | |
9c691628 | 648 | run_as_destroy_worker(); |
72f11bd7 | 649 | |
9c691628 JG |
650 | exit_health_consumerd_cleanup: |
651 | exit_options: | |
72f11bd7 | 652 | exit_set_signal_handler: |
a5356923 | 653 | |
72f11bd7 MD |
654 | if (!retval) { |
655 | exit(EXIT_SUCCESS); | |
656 | } else { | |
657 | exit(EXIT_FAILURE); | |
658 | } | |
d4a1283e | 659 | } |