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