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