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