SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
CommitLineData
826d496d 1/*
ab5be9fa
MJ
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
fac6795d 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
91d76f53 7 *
fac6795d
DG
8 */
9
6c1c0768 10#define _LGPL_SOURCE
fac6795d
DG
11#include <getopt.h>
12#include <grp.h>
13#include <limits.h>
0bb7724a 14#include <paths.h>
fac6795d
DG
15#include <pthread.h>
16#include <signal.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
331744e3 20#include <inttypes.h>
0fdd1e2c 21#include <sys/mman.h>
b73401da 22#include <sys/mount.h>
1e307fab 23#include <sys/resource.h>
fac6795d
DG
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
0fdd1e2c 27#include <sys/wait.h>
5c827ce0 28#include <urcu/uatomic.h>
fac6795d 29#include <unistd.h>
4f0b90ee 30#include <ctype.h>
fac6795d 31
990570ed 32#include <common/common.h>
d27c42b8 33#include <common/compat/socket.h>
e8fa9fb0 34#include <common/compat/getenv.h>
db758600
DG
35#include <common/defaults.h>
36#include <common/kernel-consumer/kernel-consumer.h>
50c8f484 37#include <common/futex.h>
00e2e675 38#include <common/relayd/relayd.h>
81b86775 39#include <common/utils.h>
3ccdf997 40#include <common/daemonize.h>
f40ef1d5 41#include <common/config/session-config.h>
dcabc190 42#include <common/dynamic-buffer.h>
76fcf151 43#include <lttng/event-internal.h>
fac6795d 44
10a8a223 45#include "lttng-sessiond.h"
7972aab2 46#include "buffer-registry.h"
54d01ffb 47#include "channel.h"
2f77fc4b 48#include "cmd.h"
00e2e675 49#include "consumer.h"
099e26bd 50#include "context.h"
54d01ffb 51#include "event.h"
4771f025 52#include "kernel.h"
f1e16794 53#include "kernel-consumer.h"
75018ab6 54#include "lttng-ust-ctl.h"
00e2e675 55#include "ust-consumer.h"
8e68d1c8 56#include "utils.h"
4063050c 57#include "fd-limit.h"
8782cc74 58#include "health-sessiond.h"
8ac94142 59#include "testpoint.h"
f9d2ba6a 60#include "notify-apps.h"
022d91ba 61#include "agent-thread.h"
fb198a11 62#include "save.h"
b3530820
JG
63#include "notification-thread.h"
64#include "notification-thread-commands.h"
db66e574 65#include "rotation-thread.h"
7c1d2758 66#include "agent.h"
5e97de00 67#include "ht-cleanup.h"
e6142f2e 68#include "sessiond-config.h"
8e319828 69#include "timer.h"
a3707772 70#include "thread.h"
917a718d 71#include "client.h"
5d1b0219 72#include "dispatch.h"
1785d7f2 73#include "register.h"
7649924e 74#include "manage-apps.h"
5b093681 75#include "manage-kernel.h"
2463b787 76#include "trigger-error-accounting.h"
ebaeda94 77
4fc83d94
PP
78static const char *help_msg =
79#ifdef LTTNG_EMBED_HELP
80#include <lttng-sessiond.8.h>
81#else
82NULL
83#endif
84;
85
2463b787
JR
86#define TRIGGER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
87
fac6795d 88const char *progname;
c9cb3e7d 89static int lockfile_fd = -1;
a3bc3918 90static int opt_print_version;
3bd1e081 91
0bb7724a
DG
92/* Set to 1 when a SIGUSR1 signal is received. */
93static int recv_child_signal;
94
26296c48
JG
95/* Command line options */
96static const struct option long_options[] = {
0f5ea17c
JG
97 { "client-sock", required_argument, 0, 'c' },
98 { "apps-sock", required_argument, 0, 'a' },
99 { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
100 { "kconsumerd-err-sock", required_argument, 0, '\0' },
101 { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
102 { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
103 { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
104 { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
105 { "consumerd32-path", required_argument, 0, '\0' },
106 { "consumerd32-libdir", required_argument, 0, '\0' },
107 { "consumerd64-path", required_argument, 0, '\0' },
108 { "consumerd64-libdir", required_argument, 0, '\0' },
109 { "daemonize", no_argument, 0, 'd' },
110 { "background", no_argument, 0, 'b' },
111 { "sig-parent", no_argument, 0, 'S' },
112 { "help", no_argument, 0, 'h' },
113 { "group", required_argument, 0, 'g' },
114 { "version", no_argument, 0, 'V' },
115 { "quiet", no_argument, 0, 'q' },
116 { "verbose", no_argument, 0, 'v' },
117 { "verbose-consumer", no_argument, 0, '\0' },
118 { "no-kernel", no_argument, 0, '\0' },
119 { "pidfile", required_argument, 0, 'p' },
120 { "agent-tcp-port", required_argument, 0, '\0' },
121 { "config", required_argument, 0, 'f' },
122 { "load", required_argument, 0, 'l' },
123 { "kmod-probes", required_argument, 0, '\0' },
124 { "extra-kmod-probes", required_argument, 0, '\0' },
2463b787 125 { "trigger-error-number-of-bucket", required_argument, 0, '\0' },
26296c48
JG
126 { NULL, 0, 0, 0 }
127};
128
129/* Command line options to ignore from configuration file */
130static const char *config_ignore_options[] = { "help", "version", "config" };
1d4b027a 131
099e26bd
DG
132/*
133 * This pipe is used to inform the thread managing application communication
134 * that a command is queued and ready to be processed.
135 */
76d7553f 136static int apps_cmd_pipe[2] = { -1, -1 };
971a61c6 137static int apps_cmd_notify_pipe[2] = { -1, -1 };
099e26bd 138
099e26bd
DG
139/*
140 * UST registration command queue. This queue is tied with a futex and uses a N
141 * wakers / 1 waiter implemented and detailed in futex.c/.h
142 *
b22c5da8
DG
143 * The thread_registration_apps and thread_dispatch_ust_registration uses this
144 * queue along with the wait/wake scheme. The thread_manage_apps receives down
145 * the line new application socket and monitors it for any I/O error or clean
146 * close that triggers an unregistration of the application.
099e26bd
DG
147 */
148static struct ust_cmd_queue ust_cmd_queue;
149
97bc1426 150/*
a7333da7 151 * Section name to look for in the daemon configuration file.
97bc1426 152 */
a7333da7 153static const char * const config_section_name = "sessiond";
5e97de00 154
a7333da7
JG
155/* Am I root or not. Set to 1 if the daemon is running as root */
156static int is_root;
5eb91c98 157
099e26bd
DG
158/*
159 * Stop all threads by closing the thread quit pipe.
160 */
cf3af59e
MD
161static void stop_threads(void)
162{
5eb91c98
DG
163 int ret;
164
cf3af59e
MD
165 /* Stopping all threads */
166 DBG("Terminating all threads");
a7333da7 167 ret = sessiond_notify_quit_pipe();
5eb91c98
DG
168 if (ret < 0) {
169 ERR("write error on thread quit pipe");
170 }
cf3af59e
MD
171}
172
e975f9f8
DG
173/*
174 * Close every consumer sockets.
175 */
176static void close_consumer_sockets(void)
177{
178 int ret;
179
180 if (kconsumer_data.err_sock >= 0) {
181 ret = close(kconsumer_data.err_sock);
182 if (ret < 0) {
183 PERROR("kernel consumer err_sock close");
184 }
185 }
186 if (ustconsumer32_data.err_sock >= 0) {
187 ret = close(ustconsumer32_data.err_sock);
188 if (ret < 0) {
a76cbd9f 189 PERROR("UST consumerd32 err_sock close");
e975f9f8
DG
190 }
191 }
192 if (ustconsumer64_data.err_sock >= 0) {
193 ret = close(ustconsumer64_data.err_sock);
194 if (ret < 0) {
a76cbd9f 195 PERROR("UST consumerd64 err_sock close");
e975f9f8
DG
196 }
197 }
198 if (kconsumer_data.cmd_sock >= 0) {
199 ret = close(kconsumer_data.cmd_sock);
200 if (ret < 0) {
201 PERROR("kernel consumer cmd_sock close");
202 }
203 }
204 if (ustconsumer32_data.cmd_sock >= 0) {
205 ret = close(ustconsumer32_data.cmd_sock);
206 if (ret < 0) {
a76cbd9f 207 PERROR("UST consumerd32 cmd_sock close");
e975f9f8
DG
208 }
209 }
210 if (ustconsumer64_data.cmd_sock >= 0) {
211 ret = close(ustconsumer64_data.cmd_sock);
212 if (ret < 0) {
a76cbd9f 213 PERROR("UST consumerd64 cmd_sock close");
e975f9f8
DG
214 }
215 }
b3530820
JG
216 if (kconsumer_data.channel_monitor_pipe >= 0) {
217 ret = close(kconsumer_data.channel_monitor_pipe);
218 if (ret < 0) {
219 PERROR("kernel consumer channel monitor pipe close");
220 }
221 }
222 if (ustconsumer32_data.channel_monitor_pipe >= 0) {
223 ret = close(ustconsumer32_data.channel_monitor_pipe);
224 if (ret < 0) {
225 PERROR("UST consumerd32 channel monitor pipe close");
226 }
227 }
228 if (ustconsumer64_data.channel_monitor_pipe >= 0) {
229 ret = close(ustconsumer64_data.channel_monitor_pipe);
230 if (ret < 0) {
231 PERROR("UST consumerd64 channel monitor pipe close");
232 }
233 }
e975f9f8
DG
234}
235
4e4714cb
JR
236/*
237 * Wait on consumer process termination.
238 *
239 * Need to be called with the consumer data lock held or from a context
240 * ensuring no concurrent access to data (e.g: cleanup).
241 */
242static void wait_consumer(struct consumer_data *consumer_data)
243{
244 pid_t ret;
245 int status;
246
247 if (consumer_data->pid <= 0) {
248 return;
249 }
250
251 DBG("Waiting for complete teardown of consumerd (PID: %d)",
252 consumer_data->pid);
253 ret = waitpid(consumer_data->pid, &status, 0);
254 if (ret == -1) {
255 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
1640c24c 256 } else if (!WIFEXITED(status)) {
4e4714cb
JR
257 ERR("consumerd termination with error: %d",
258 WEXITSTATUS(ret));
259 }
260 consumer_data->pid = 0;
261}
262
fac6795d 263/*
4a15001e 264 * Cleanup the session daemon's data structures.
fac6795d 265 */
4a15001e 266static void sessiond_cleanup(void)
fac6795d 267{
ef599319 268 int ret;
e32d7f27 269 struct ltt_session_list *session_list = session_get_list();
fac6795d 270
4a15001e 271 DBG("Cleanup sessiond");
e07ae692 272
4e449f3f
MD
273 /*
274 * Close the thread quit pipe. It has already done its job,
275 * since we are now called.
276 */
a7333da7 277 sessiond_close_quit_pipe();
971a61c6
JG
278 utils_close_pipe(apps_cmd_pipe);
279 utils_close_pipe(apps_cmd_notify_pipe);
5b093681 280 utils_close_pipe(kernel_poll_pipe);
2f77fc4b 281
c9a2957d
JG
282 ret = remove(config.pid_file_path.value);
283 if (ret < 0) {
284 PERROR("remove pidfile %s", config.pid_file_path.value);
35f90c40
DG
285 }
286
e6142f2e
JG
287 DBG("Removing sessiond and consumerd content of directory %s",
288 config.rundir.value);
8c6c56c2
MD
289
290 /* sessiond */
e6142f2e
JG
291 DBG("Removing %s", config.pid_file_path.value);
292 (void) unlink(config.pid_file_path.value);
8c6c56c2 293
e6142f2e
JG
294 DBG("Removing %s", config.agent_port_file_path.value);
295 (void) unlink(config.agent_port_file_path.value);
cd9290dd 296
8c6c56c2 297 /* kconsumerd */
e6142f2e
JG
298 DBG("Removing %s", kconsumer_data.err_unix_sock_path);
299 (void) unlink(kconsumer_data.err_unix_sock_path);
300
301 DBG("Removing directory %s", config.kconsumerd_path.value);
302 (void) rmdir(config.kconsumerd_path.value);
8c6c56c2
MD
303
304 /* ust consumerd 32 */
e6142f2e
JG
305 DBG("Removing %s", config.consumerd32_err_unix_sock_path.value);
306 (void) unlink(config.consumerd32_err_unix_sock_path.value);
307
308 DBG("Removing directory %s", config.consumerd32_path.value);
309 (void) rmdir(config.consumerd32_path.value);
8c6c56c2
MD
310
311 /* ust consumerd 64 */
e6142f2e
JG
312 DBG("Removing %s", config.consumerd64_err_unix_sock_path.value);
313 (void) unlink(config.consumerd64_err_unix_sock_path.value);
314
315 DBG("Removing directory %s", config.consumerd64_path.value);
316 (void) rmdir(config.consumerd64_path.value);
5461b305 317
99d688f2 318 pthread_mutex_destroy(&session_list->lock);
273ea72c 319
2463b787
JR
320 DBG("Cleaning up all trigger agents");
321 trigger_agent_ht_clean();
322
6a4e4039
JG
323 DBG("Cleaning up all agent apps");
324 agent_app_ht_clean();
099e26bd 325 DBG("Closing all UST sockets");
56fff090 326 ust_app_clean_list();
7972aab2 327 buffer_reg_destroy_registries();
099e26bd 328
0787fc4b
MD
329 close_consumer_sockets();
330
331 wait_consumer(&kconsumer_data);
332 wait_consumer(&ustconsumer64_data);
333 wait_consumer(&ustconsumer32_data);
334
e6142f2e 335 if (is_root && !config.no_kernel) {
7d268848 336 cleanup_kernel_tracer();
2f50c8a3 337 }
2f77fc4b 338
c9cb3e7d
JG
339 /*
340 * We do NOT rmdir rundir because there are other processes
341 * using it, for instance lttng-relayd, which can start in
342 * parallel with this teardown.
343 */
4a15001e
MD
344}
345
346/*
347 * Cleanup the daemon's option data structures.
348 */
349static void sessiond_cleanup_options(void)
350{
351 DBG("Cleaning up options");
352
e6142f2e 353 sessiond_config_fini(&config);
c9cb3e7d 354
7567352f 355 run_as_destroy_worker();
fac6795d
DG
356}
357
917a718d 358static int string_match(const char *str1, const char *str2)
54d01ffb 359{
917a718d 360 return (str1 && str2) && !strcmp(str1, str2);
54d01ffb
DG
361}
362
00e2e675 363/*
917a718d
JG
364 * Take an option from the getopt output and set it in the right variable to be
365 * used later.
36b588ed 366 *
917a718d 367 * Return 0 on success else a negative value.
00e2e675 368 */
917a718d 369static int set_option(int opt, const char *arg, const char *optname)
00e2e675 370{
917a718d 371 int ret = 0;
fac6795d 372
c0232ea5 373 if (string_match(optname, "client-sock") || opt == 'c') {
66b2ce8e
JG
374 if (!arg || *arg == '\0') {
375 ret = -EINVAL;
376 goto end;
377 }
e8fa9fb0
MD
378 if (lttng_is_setuid_setgid()) {
379 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
380 "-c, --client-sock");
381 } else {
e6142f2e
JG
382 config_string_set(&config.client_unix_sock_path,
383 strdup(arg));
384 if (!config.client_unix_sock_path.value) {
385 ret = -ENOMEM;
386 PERROR("strdup");
387 }
e8fa9fb0 388 }
c0232ea5 389 } else if (string_match(optname, "apps-sock") || opt == 'a') {
66b2ce8e
JG
390 if (!arg || *arg == '\0') {
391 ret = -EINVAL;
392 goto end;
393 }
e8fa9fb0
MD
394 if (lttng_is_setuid_setgid()) {
395 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
396 "-a, --apps-sock");
397 } else {
e6142f2e
JG
398 config_string_set(&config.apps_unix_sock_path,
399 strdup(arg));
400 if (!config.apps_unix_sock_path.value) {
401 ret = -ENOMEM;
402 PERROR("strdup");
403 }
e8fa9fb0 404 }
c0232ea5 405 } else if (string_match(optname, "daemonize") || opt == 'd') {
e6142f2e 406 config.daemonize = true;
c0232ea5 407 } else if (string_match(optname, "background") || opt == 'b') {
e6142f2e 408 config.background = true;
c0232ea5 409 } else if (string_match(optname, "group") || opt == 'g') {
66b2ce8e
JG
410 if (!arg || *arg == '\0') {
411 ret = -EINVAL;
412 goto end;
413 }
e8fa9fb0
MD
414 if (lttng_is_setuid_setgid()) {
415 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
416 "-g, --group");
417 } else {
e6142f2e
JG
418 config_string_set(&config.tracing_group_name,
419 strdup(arg));
420 if (!config.tracing_group_name.value) {
e8fa9fb0 421 ret = -ENOMEM;
e6142f2e 422 PERROR("strdup");
e8fa9fb0 423 }
db322c4d 424 }
c0232ea5 425 } else if (string_match(optname, "help") || opt == 'h') {
4fc83d94 426 ret = utils_show_help(8, "lttng-sessiond", help_msg);
8190767e 427 if (ret) {
4fc83d94 428 ERR("Cannot show --help for `lttng-sessiond`");
8190767e
PP
429 perror("exec");
430 }
431 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
c0232ea5 432 } else if (string_match(optname, "version") || opt == 'V') {
a3bc3918 433 opt_print_version = 1;
c0232ea5 434 } else if (string_match(optname, "sig-parent") || opt == 'S') {
e6142f2e 435 config.sig_parent = true;
c0232ea5 436 } else if (string_match(optname, "kconsumerd-err-sock")) {
66b2ce8e
JG
437 if (!arg || *arg == '\0') {
438 ret = -EINVAL;
439 goto end;
440 }
e8fa9fb0
MD
441 if (lttng_is_setuid_setgid()) {
442 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
443 "--kconsumerd-err-sock");
444 } else {
e6142f2e
JG
445 config_string_set(&config.kconsumerd_err_unix_sock_path,
446 strdup(arg));
447 if (!config.kconsumerd_err_unix_sock_path.value) {
448 ret = -ENOMEM;
449 PERROR("strdup");
450 }
e8fa9fb0 451 }
c0232ea5 452 } else if (string_match(optname, "kconsumerd-cmd-sock")) {
66b2ce8e
JG
453 if (!arg || *arg == '\0') {
454 ret = -EINVAL;
455 goto end;
456 }
e8fa9fb0
MD
457 if (lttng_is_setuid_setgid()) {
458 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
459 "--kconsumerd-cmd-sock");
460 } else {
e6142f2e
JG
461 config_string_set(&config.kconsumerd_cmd_unix_sock_path,
462 strdup(arg));
463 if (!config.kconsumerd_cmd_unix_sock_path.value) {
464 ret = -ENOMEM;
465 PERROR("strdup");
466 }
e8fa9fb0 467 }
c0232ea5 468 } else if (string_match(optname, "ustconsumerd64-err-sock")) {
66b2ce8e
JG
469 if (!arg || *arg == '\0') {
470 ret = -EINVAL;
471 goto end;
472 }
e8fa9fb0
MD
473 if (lttng_is_setuid_setgid()) {
474 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
475 "--ustconsumerd64-err-sock");
476 } else {
e6142f2e
JG
477 config_string_set(&config.consumerd64_err_unix_sock_path,
478 strdup(arg));
479 if (!config.consumerd64_err_unix_sock_path.value) {
480 ret = -ENOMEM;
481 PERROR("strdup");
482 }
e8fa9fb0 483 }
c0232ea5 484 } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
66b2ce8e
JG
485 if (!arg || *arg == '\0') {
486 ret = -EINVAL;
487 goto end;
488 }
e8fa9fb0
MD
489 if (lttng_is_setuid_setgid()) {
490 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
491 "--ustconsumerd64-cmd-sock");
492 } else {
e6142f2e
JG
493 config_string_set(&config.consumerd64_cmd_unix_sock_path,
494 strdup(arg));
495 if (!config.consumerd64_cmd_unix_sock_path.value) {
496 ret = -ENOMEM;
497 PERROR("strdup");
498 }
e8fa9fb0 499 }
c0232ea5 500 } else if (string_match(optname, "ustconsumerd32-err-sock")) {
66b2ce8e
JG
501 if (!arg || *arg == '\0') {
502 ret = -EINVAL;
503 goto end;
504 }
e8fa9fb0
MD
505 if (lttng_is_setuid_setgid()) {
506 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
507 "--ustconsumerd32-err-sock");
508 } else {
e6142f2e
JG
509 config_string_set(&config.consumerd32_err_unix_sock_path,
510 strdup(arg));
511 if (!config.consumerd32_err_unix_sock_path.value) {
512 ret = -ENOMEM;
513 PERROR("strdup");
514 }
e8fa9fb0 515 }
c0232ea5 516 } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
66b2ce8e
JG
517 if (!arg || *arg == '\0') {
518 ret = -EINVAL;
519 goto end;
520 }
e8fa9fb0
MD
521 if (lttng_is_setuid_setgid()) {
522 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
523 "--ustconsumerd32-cmd-sock");
524 } else {
e6142f2e
JG
525 config_string_set(&config.consumerd32_cmd_unix_sock_path,
526 strdup(arg));
527 if (!config.consumerd32_cmd_unix_sock_path.value) {
528 ret = -ENOMEM;
529 PERROR("strdup");
530 }
e8fa9fb0 531 }
c0232ea5 532 } else if (string_match(optname, "no-kernel")) {
e6142f2e 533 config.no_kernel = true;
c0232ea5 534 } else if (string_match(optname, "quiet") || opt == 'q') {
6d9a9c65 535 config.quiet = true;
c0232ea5 536 } else if (string_match(optname, "verbose") || opt == 'v') {
26296c48
JG
537 /* Verbose level can increase using multiple -v */
538 if (arg) {
13755a18 539 /* Value obtained from config file */
e6142f2e 540 config.verbose = config_parse_value(arg);
26296c48 541 } else {
13755a18 542 /* -v used on command line */
e6142f2e 543 config.verbose++;
26296c48 544 }
13755a18 545 /* Clamp value to [0, 3] */
e6142f2e
JG
546 config.verbose = config.verbose < 0 ? 0 :
547 (config.verbose <= 3 ? config.verbose : 3);
c0232ea5 548 } else if (string_match(optname, "verbose-consumer")) {
26296c48 549 if (arg) {
e6142f2e 550 config.verbose_consumer = config_parse_value(arg);
26296c48 551 } else {
e6142f2e 552 config.verbose_consumer++;
26296c48 553 }
c0232ea5 554 } else if (string_match(optname, "consumerd32-path")) {
66b2ce8e
JG
555 if (!arg || *arg == '\0') {
556 ret = -EINVAL;
557 goto end;
558 }
e8fa9fb0
MD
559 if (lttng_is_setuid_setgid()) {
560 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
561 "--consumerd32-path");
562 } else {
e6142f2e
JG
563 config_string_set(&config.consumerd32_bin_path,
564 strdup(arg));
565 if (!config.consumerd32_bin_path.value) {
e8fa9fb0
MD
566 PERROR("strdup");
567 ret = -ENOMEM;
568 }
db322c4d 569 }
c0232ea5 570 } else if (string_match(optname, "consumerd32-libdir")) {
66b2ce8e
JG
571 if (!arg || *arg == '\0') {
572 ret = -EINVAL;
573 goto end;
574 }
e8fa9fb0
MD
575 if (lttng_is_setuid_setgid()) {
576 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
577 "--consumerd32-libdir");
578 } else {
e6142f2e
JG
579 config_string_set(&config.consumerd32_lib_dir,
580 strdup(arg));
581 if (!config.consumerd32_lib_dir.value) {
e8fa9fb0
MD
582 PERROR("strdup");
583 ret = -ENOMEM;
584 }
db322c4d 585 }
c0232ea5 586 } else if (string_match(optname, "consumerd64-path")) {
66b2ce8e
JG
587 if (!arg || *arg == '\0') {
588 ret = -EINVAL;
589 goto end;
590 }
e8fa9fb0
MD
591 if (lttng_is_setuid_setgid()) {
592 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
593 "--consumerd64-path");
594 } else {
e6142f2e
JG
595 config_string_set(&config.consumerd64_bin_path,
596 strdup(arg));
597 if (!config.consumerd64_bin_path.value) {
e8fa9fb0
MD
598 PERROR("strdup");
599 ret = -ENOMEM;
600 }
db322c4d 601 }
c0232ea5 602 } else if (string_match(optname, "consumerd64-libdir")) {
66b2ce8e
JG
603 if (!arg || *arg == '\0') {
604 ret = -EINVAL;
605 goto end;
606 }
e8fa9fb0
MD
607 if (lttng_is_setuid_setgid()) {
608 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
609 "--consumerd64-libdir");
610 } else {
e6142f2e
JG
611 config_string_set(&config.consumerd64_lib_dir,
612 strdup(arg));
613 if (!config.consumerd64_lib_dir.value) {
e8fa9fb0
MD
614 PERROR("strdup");
615 ret = -ENOMEM;
616 }
db322c4d 617 }
c0232ea5 618 } else if (string_match(optname, "pidfile") || opt == 'p') {
66b2ce8e
JG
619 if (!arg || *arg == '\0') {
620 ret = -EINVAL;
621 goto end;
622 }
e8fa9fb0
MD
623 if (lttng_is_setuid_setgid()) {
624 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
625 "-p, --pidfile");
626 } else {
e6142f2e
JG
627 config_string_set(&config.pid_file_path, strdup(arg));
628 if (!config.pid_file_path.value) {
e8fa9fb0
MD
629 PERROR("strdup");
630 ret = -ENOMEM;
631 }
db322c4d 632 }
c0232ea5 633 } else if (string_match(optname, "agent-tcp-port")) {
66b2ce8e
JG
634 if (!arg || *arg == '\0') {
635 ret = -EINVAL;
636 goto end;
637 }
e8fa9fb0
MD
638 if (lttng_is_setuid_setgid()) {
639 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
640 "--agent-tcp-port");
641 } else {
642 unsigned long v;
4d076222 643
e8fa9fb0
MD
644 errno = 0;
645 v = strtoul(arg, NULL, 0);
646 if (errno != 0 || !isdigit(arg[0])) {
647 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
648 return -1;
649 }
650 if (v == 0 || v >= 65535) {
651 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
652 return -1;
653 }
2288467f
JG
654 config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v;
655 DBG3("Agent TCP port set to non default: %i", (int) v);
26296c48 656 }
c0232ea5 657 } else if (string_match(optname, "load") || opt == 'l') {
66b2ce8e
JG
658 if (!arg || *arg == '\0') {
659 ret = -EINVAL;
660 goto end;
661 }
e8fa9fb0
MD
662 if (lttng_is_setuid_setgid()) {
663 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
664 "-l, --load");
665 } else {
3cfcd698 666 config_string_set(&config.load_session_path, strdup(arg));
e6142f2e 667 if (!config.load_session_path.value) {
e8fa9fb0
MD
668 PERROR("strdup");
669 ret = -ENOMEM;
670 }
ef367a93 671 }
c0232ea5 672 } else if (string_match(optname, "kmod-probes")) {
66b2ce8e
JG
673 if (!arg || *arg == '\0') {
674 ret = -EINVAL;
675 goto end;
676 }
e8fa9fb0
MD
677 if (lttng_is_setuid_setgid()) {
678 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
679 "--kmod-probes");
680 } else {
e6142f2e
JG
681 config_string_set(&config.kmod_probes_list, strdup(arg));
682 if (!config.kmod_probes_list.value) {
e8fa9fb0
MD
683 PERROR("strdup");
684 ret = -ENOMEM;
685 }
c9d42407 686 }
c0232ea5 687 } else if (string_match(optname, "extra-kmod-probes")) {
66b2ce8e
JG
688 if (!arg || *arg == '\0') {
689 ret = -EINVAL;
690 goto end;
691 }
e8fa9fb0
MD
692 if (lttng_is_setuid_setgid()) {
693 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
694 "--extra-kmod-probes");
695 } else {
e6142f2e
JG
696 config_string_set(&config.kmod_extra_probes_list,
697 strdup(arg));
698 if (!config.kmod_extra_probes_list.value) {
e8fa9fb0
MD
699 PERROR("strdup");
700 ret = -ENOMEM;
701 }
db322c4d 702 }
2463b787
JR
703 } else if (string_match(optname, "trigger-error-number-of-bucket")) {
704 unsigned long v;
705
706 errno = 0;
707 v = strtoul(arg, NULL, 0);
708 if (errno != 0 || !isdigit(arg[0])) {
709 ERR("Wrong value in --trigger-error-number-of-bucket parameter: %s", arg);
710 return -1;
711 }
712 if (v == 0 || v >= TRIGGER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX) {
713 ERR("Value out of range for --trigger-error-number-of-bucket parameter: %s", arg);
714 return -1;
715 }
716 config.trigger_error_counter_bucket = (int) v;
717 DBG3("Number of error counter set to non default: %i",
718 config.trigger_error_counter_bucket);
719 goto end;
c0232ea5
JG
720 } else if (string_match(optname, "config") || opt == 'f') {
721 /* This is handled in set_options() thus silent skip. */
722 goto end;
723 } else {
26296c48
JG
724 /* Unknown option or other error.
725 * Error is printed by getopt, just return */
726 ret = -1;
727 }
728
f3dd7ce7 729end:
c5d350b2
JG
730 if (ret == -EINVAL) {
731 const char *opt_name = "unknown";
732 int i;
733
734 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
735 i++) {
736 if (opt == long_options[i].val) {
737 opt_name = long_options[i].name;
738 break;
739 }
740 }
741
742 WARN("Invalid argument provided for option \"%s\", using default value.",
743 opt_name);
744 }
f3dd7ce7 745
26296c48
JG
746 return ret;
747}
748
749/*
750 * config_entry_handler_cb used to handle options read from a config file.
f40ef1d5 751 * See config_entry_handler_cb comment in common/config/session-config.h for the
26296c48
JG
752 * return value conventions.
753 */
754static int config_entry_handler(const struct config_entry *entry, void *unused)
755{
756 int ret = 0, i;
757
758 if (!entry || !entry->name || !entry->value) {
759 ret = -EINVAL;
760 goto end;
761 }
762
763 /* Check if the option is to be ignored */
764 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
765 if (!strcmp(entry->name, config_ignore_options[i])) {
766 goto end;
767 }
768 }
769
770 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
771 i++) {
772
773 /* Ignore if not fully matched. */
774 if (strcmp(entry->name, long_options[i].name)) {
775 continue;
776 }
777
778 /*
779 * If the option takes no argument on the command line, we have to
780 * check if the value is "true". We support non-zero numeric values,
781 * true, on and yes.
782 */
783 if (!long_options[i].has_arg) {
784 ret = config_parse_value(entry->value);
785 if (ret <= 0) {
786 if (ret) {
787 WARN("Invalid configuration value \"%s\" for option %s",
788 entry->value, entry->name);
789 }
790 /* False, skip boolean config option. */
791 goto end;
4d076222 792 }
26296c48
JG
793 }
794
795 ret = set_option(long_options[i].val, entry->value, entry->name);
796 goto end;
797 }
798
799 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
800
801end:
802 return ret;
803}
804
a3bc3918
JR
805static void print_version(void) {
806 fprintf(stdout, "%s\n", VERSION);
807}
808
26296c48
JG
809/*
810 * daemon configuration loading and argument parsing
811 */
812static int set_options(int argc, char **argv)
813{
814 int ret = 0, c = 0, option_index = 0;
815 int orig_optopt = optopt, orig_optind = optind;
816 char *optstring;
817 const char *config_path = NULL;
818
819 optstring = utils_generate_optstring(long_options,
820 sizeof(long_options) / sizeof(struct option));
821 if (!optstring) {
822 ret = -ENOMEM;
823 goto end;
824 }
825
826 /* Check for the --config option */
827 while ((c = getopt_long(argc, argv, optstring, long_options,
828 &option_index)) != -1) {
829 if (c == '?') {
830 ret = -EINVAL;
831 goto end;
832 } else if (c != 'f') {
833 /* if not equal to --config option. */
834 continue;
835 }
836
e8fa9fb0
MD
837 if (lttng_is_setuid_setgid()) {
838 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
839 "-f, --config");
840 } else {
841 config_path = utils_expand_path(optarg);
842 if (!config_path) {
843 ERR("Failed to resolve path: %s", optarg);
844 }
26296c48
JG
845 }
846 }
847
848 ret = config_get_section_entries(config_path, config_section_name,
849 config_entry_handler, NULL);
850 if (ret) {
851 if (ret > 0) {
852 ERR("Invalid configuration option at line %i", ret);
853 ret = -1;
854 }
855 goto end;
856 }
857
858 /* Reset getopt's global state */
859 optopt = orig_optopt;
860 optind = orig_optind;
861 while (1) {
c0232ea5
JG
862 option_index = -1;
863 /*
864 * getopt_long() will not set option_index if it encounters a
865 * short option.
866 */
867 c = getopt_long(argc, argv, optstring, long_options,
868 &option_index);
26296c48 869 if (c == -1) {
4d076222
DG
870 break;
871 }
26296c48 872
c0232ea5
JG
873 /*
874 * Pass NULL as the long option name if popt left the index
875 * unset.
876 */
877 ret = set_option(c, optarg,
878 option_index < 0 ? NULL :
879 long_options[option_index].name);
26296c48
JG
880 if (ret < 0) {
881 break;
fac6795d
DG
882 }
883 }
884
26296c48
JG
885end:
886 free(optstring);
887 return ret;
fac6795d
DG
888}
889
f472090a
JG
890/*
891 * Create lockfile using the rundir and return its fd.
892 */
893static int create_lockfile(void)
894{
3cfcd698 895 return utils_create_lock_file(config.lock_file_path.value);
f472090a
JG
896}
897
fac6795d 898/*
54d01ffb
DG
899 * Check if the global socket is available, and if a daemon is answering at the
900 * other side. If yes, error is returned.
f472090a
JG
901 *
902 * Also attempts to create and hold the lock file.
fac6795d 903 */
cf3af59e 904static int check_existing_daemon(void)
fac6795d 905{
f472090a
JG
906 int ret = 0;
907
7d8234d9 908 /* Is there anybody out there ? */
099e26bd 909 if (lttng_session_daemon_alive()) {
f472090a
JG
910 ret = -EEXIST;
911 goto end;
099e26bd 912 }
b09c7c76 913
f472090a
JG
914 lockfile_fd = create_lockfile();
915 if (lockfile_fd < 0) {
916 ret = -EEXIST;
917 goto end;
918 }
919end:
920 return ret;
921}
922
923static void sessiond_cleanup_lock_file(void)
924{
925 int ret;
926
927 /*
928 * Cleanup lock file by deleting it and finaly closing it which will
929 * release the file system lock.
930 */
931 if (lockfile_fd >= 0) {
932 ret = remove(config.lock_file_path.value);
933 if (ret < 0) {
934 PERROR("remove lock file");
935 }
936 ret = close(lockfile_fd);
937 if (ret < 0) {
938 PERROR("close lock file");
939 }
940 }
fac6795d
DG
941}
942
fac6795d 943/*
d063d709 944 * Set the tracing group gid onto the client socket.
5e16da05 945 *
d063d709 946 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 947 * permissive (root.root) to less permissive (root.tracing).
fac6795d 948 */
be040666 949static int set_permissions(char *rundir)
fac6795d
DG
950{
951 int ret;
996b65c8 952 gid_t gid;
fac6795d 953
28ab59d0
JR
954 ret = utils_get_group_id(config.tracing_group_name.value, true, &gid);
955 if (ret) {
956 /* Default to root group. */
957 gid = 0;
958 }
fac6795d 959
d6f42150 960 /* Set lttng run dir */
be040666 961 ret = chown(rundir, 0, gid);
d6f42150 962 if (ret < 0) {
be040666 963 ERR("Unable to set group on %s", rundir);
76d7553f 964 PERROR("chown");
d6f42150
DG
965 }
966
6c71277b
MD
967 /*
968 * Ensure all applications and tracing group can search the run
969 * dir. Allow everyone to read the directory, since it does not
970 * buy us anything to hide its content.
971 */
972 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
d1613cf5
JN
973 if (ret < 0) {
974 ERR("Unable to set permissions on %s", rundir);
76d7553f 975 PERROR("chmod");
d1613cf5
JN
976 }
977
d6f42150 978 /* lttng client socket path */
e6142f2e 979 ret = chown(config.client_unix_sock_path.value, 0, gid);
fac6795d 980 if (ret < 0) {
e6142f2e 981 ERR("Unable to set group on %s", config.client_unix_sock_path.value);
76d7553f 982 PERROR("chown");
d6f42150
DG
983 }
984
3bd1e081 985 /* kconsumer error socket path */
6c71277b 986 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
d6f42150 987 if (ret < 0) {
3bd1e081 988 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 989 PERROR("chown");
3bd1e081
MD
990 }
991
7753dea8 992 /* 64-bit ustconsumer error socket path */
6c71277b 993 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
7753dea8
MD
994 if (ret < 0) {
995 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 996 PERROR("chown");
7753dea8
MD
997 }
998
999 /* 32-bit ustconsumer compat32 error socket path */
6c71277b 1000 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
3bd1e081 1001 if (ret < 0) {
7753dea8 1002 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 1003 PERROR("chown");
fac6795d
DG
1004 }
1005
d6f42150 1006 DBG("All permissions are set");
e07ae692 1007
fac6795d
DG
1008 return ret;
1009}
1010
d6f42150 1011/*
d063d709 1012 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 1013 */
e6142f2e 1014static int create_lttng_rundir(void)
d6f42150
DG
1015{
1016 int ret;
1017
e6142f2e 1018 DBG3("Creating LTTng run directory: %s", config.rundir.value);
67e40797 1019
e6142f2e 1020 ret = mkdir(config.rundir.value, S_IRWXU);
d6f42150 1021 if (ret < 0) {
b1f11e69 1022 if (errno != EEXIST) {
e6142f2e 1023 ERR("Unable to create %s", config.rundir.value);
b1f11e69
DG
1024 goto error;
1025 } else {
1026 ret = 0;
1027 }
d6f42150
DG
1028 }
1029
1030error:
1031 return ret;
1032}
1033
1034/*
e6142f2e 1035 * Setup sockets and directory needed by the consumerds' communication with the
d063d709 1036 * session daemon.
d6f42150 1037 */
e6142f2e 1038static int set_consumer_sockets(struct consumer_data *consumer_data)
d6f42150
DG
1039{
1040 int ret;
e6142f2e 1041 char *path = NULL;
d6f42150 1042
6c71277b 1043 switch (consumer_data->type) {
7753dea8 1044 case LTTNG_CONSUMER_KERNEL:
e6142f2e 1045 path = config.kconsumerd_path.value;
7753dea8
MD
1046 break;
1047 case LTTNG_CONSUMER64_UST:
e6142f2e 1048 path = config.consumerd64_path.value;
7753dea8
MD
1049 break;
1050 case LTTNG_CONSUMER32_UST:
e6142f2e 1051 path = config.consumerd32_path.value;
7753dea8
MD
1052 break;
1053 default:
1054 ERR("Consumer type unknown");
1055 ret = -EINVAL;
1056 goto error;
d6f42150 1057 }
e6142f2e 1058 assert(path);
d6f42150 1059
67e40797
DG
1060 DBG2("Creating consumer directory: %s", path);
1061
6c71277b 1062 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
e7e1ef17
JR
1063 if (ret < 0 && errno != EEXIST) {
1064 PERROR("mkdir");
1065 ERR("Failed to create %s", path);
1066 goto error;
d6f42150 1067 }
6c71277b 1068 if (is_root) {
28ab59d0
JR
1069 gid_t gid;
1070
1071 ret = utils_get_group_id(config.tracing_group_name.value, true,
1072 &gid);
1073 if (ret) {
1074 /* Default to root group. */
1075 gid = 0;
1076 }
1077
1078 ret = chown(path, 0, gid);
6c71277b
MD
1079 if (ret < 0) {
1080 ERR("Unable to set group on %s", path);
1081 PERROR("chown");
1082 goto error;
1083 }
1084 }
d6f42150 1085
e6142f2e 1086 /* Create the consumerd error unix socket */
3bd1e081
MD
1087 consumer_data->err_sock =
1088 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
1089 if (consumer_data->err_sock < 0) {
1090 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
1091 ret = -1;
1092 goto error;
1093 }
1094
a24f05ab
MD
1095 /*
1096 * Set the CLOEXEC flag. Return code is useless because either way, the
1097 * show must go on.
1098 */
1099 ret = utils_set_fd_cloexec(consumer_data->err_sock);
1100 if (ret < 0) {
1101 PERROR("utils_set_fd_cloexec");
1102 /* continue anyway */
1103 }
1104
d6f42150 1105 /* File permission MUST be 660 */
3bd1e081 1106 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 1107 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 1108 if (ret < 0) {
3bd1e081 1109 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 1110 PERROR("chmod");
d6f42150
DG
1111 goto error;
1112 }
1113
1114error:
1115 return ret;
1116}
1117
fac6795d 1118/*
d063d709 1119 * Signal handler for the daemon
cf3af59e 1120 *
54d01ffb
DG
1121 * Simply stop all worker threads, leaving main() return gracefully after
1122 * joining all threads and calling cleanup().
fac6795d
DG
1123 */
1124static void sighandler(int sig)
1125{
1126 switch (sig) {
cf3af59e 1127 case SIGINT:
af87c45a 1128 DBG("SIGINT caught");
cf3af59e
MD
1129 stop_threads();
1130 break;
1131 case SIGTERM:
af87c45a 1132 DBG("SIGTERM caught");
cf3af59e
MD
1133 stop_threads();
1134 break;
0bb7724a
DG
1135 case SIGUSR1:
1136 CMM_STORE_SHARED(recv_child_signal, 1);
1137 break;
cf3af59e
MD
1138 default:
1139 break;
fac6795d 1140 }
fac6795d
DG
1141}
1142
1143/*
d063d709 1144 * Setup signal handler for :
1d4b027a 1145 * SIGINT, SIGTERM, SIGPIPE
fac6795d 1146 */
1d4b027a 1147static int set_signal_handler(void)
fac6795d 1148{
1d4b027a
DG
1149 int ret = 0;
1150 struct sigaction sa;
1151 sigset_t sigset;
fac6795d 1152
1d4b027a 1153 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 1154 PERROR("sigemptyset");
1d4b027a
DG
1155 return ret;
1156 }
d6f42150 1157
1d4b027a
DG
1158 sa.sa_mask = sigset;
1159 sa.sa_flags = 0;
0072e5e2
MD
1160
1161 sa.sa_handler = sighandler;
1d4b027a 1162 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 1163 PERROR("sigaction");
1d4b027a 1164 return ret;
d6f42150
DG
1165 }
1166
1d4b027a 1167 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 1168 PERROR("sigaction");
1d4b027a 1169 return ret;
d6f42150 1170 }
aaf26714 1171
0072e5e2 1172 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
76d7553f 1173 PERROR("sigaction");
1d4b027a 1174 return ret;
8c0faa1d
DG
1175 }
1176
0072e5e2
MD
1177 sa.sa_handler = SIG_IGN;
1178 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
0bb7724a
DG
1179 PERROR("sigaction");
1180 return ret;
1181 }
1182
1183 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1d4b027a
DG
1184
1185 return ret;
fac6795d
DG
1186}
1187
f3ed775e 1188/*
d063d709 1189 * Set open files limit to unlimited. This daemon can open a large number of
514bb9f0 1190 * file descriptors in order to consume multiple kernel traces.
f3ed775e
DG
1191 */
1192static void set_ulimit(void)
1193{
1194 int ret;
1195 struct rlimit lim;
1196
514bb9f0 1197 /* The kernel does not allow an infinite limit for open files */
f3ed775e
DG
1198 lim.rlim_cur = 65535;
1199 lim.rlim_max = 65535;
1200
1201 ret = setrlimit(RLIMIT_NOFILE, &lim);
1202 if (ret < 0) {
76d7553f 1203 PERROR("failed to set open files limit");
f3ed775e
DG
1204 }
1205}
1206
4a15001e 1207static int write_pidfile(void)
35f90c40 1208{
3cfcd698 1209 return utils_create_pid_file(getpid(), config.pid_file_path.value);
35f90c40
DG
1210}
1211
e6142f2e
JG
1212static int set_clock_plugin_env(void)
1213{
1214 int ret = 0;
1215 char *env_value = NULL;
cd9290dd 1216
e6142f2e
JG
1217 if (!config.lttng_ust_clock_plugin.value) {
1218 goto end;
1219 }
1220
3cfcd698 1221 ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s",
e6142f2e 1222 config.lttng_ust_clock_plugin.value);
cd9290dd 1223 if (ret < 0) {
e6142f2e
JG
1224 PERROR("asprintf");
1225 goto end;
cd9290dd
DG
1226 }
1227
e6142f2e
JG
1228 ret = putenv(env_value);
1229 if (ret) {
1230 free(env_value);
1231 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1232 goto end;
1233 }
cd9290dd 1234
e6142f2e
JG
1235 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1236 config.lttng_ust_clock_plugin.value);
1237end:
4a15001e 1238 return ret;
ef367a93
JG
1239}
1240
99d688f2
JG
1241static void destroy_all_sessions_and_wait(void)
1242{
1243 struct ltt_session *session, *tmp;
1244 struct ltt_session_list *session_list;
1245
1246 session_list = session_get_list();
1247 DBG("Initiating destruction of all sessions");
1248
1249 if (!session_list) {
1250 return;
1251 }
1252
99d688f2
JG
1253 session_lock_list();
1254 /* Initiate the destruction of all sessions. */
1255 cds_list_for_each_entry_safe(session, tmp,
1256 &session_list->head, list) {
1257 if (!session_get(session)) {
1258 continue;
1259 }
1260
1261 session_lock(session);
1262 if (session->destroyed) {
1263 goto unlock_session;
1264 }
63763958 1265 (void) cmd_stop_trace(session);
3e3665b8
JG
1266 (void) cmd_destroy_session(session, notification_thread_handle,
1267 NULL);
99d688f2
JG
1268 unlock_session:
1269 session_unlock(session);
1270 session_put(session);
1271 }
1272 session_unlock_list();
1273
1274 /* Wait for the destruction of all sessions to complete. */
1275 DBG("Waiting for the destruction of all sessions to complete");
1276 session_list_wait_empty();
1277 DBG("Destruction of all sessions completed");
1278}
1279
929f71ec
JG
1280static int run_as_worker_post_fork_cleanup(void *data)
1281{
1282 struct sessiond_config *sessiond_config = data;
1283
1284 sessiond_config_fini(sessiond_config);
1285 return 0;
1286}
1287
1288static int launch_run_as_worker(const char *procname)
1289{
1290 /*
1291 * Clean-up before forking the run-as worker. Any dynamically
1292 * allocated memory of which the worker is not aware will
1293 * be leaked as the process forks a run-as worker (and performs
1294 * no exec*()). The same would apply to any opened fd.
1295 */
1296 return run_as_create_worker(procname, run_as_worker_post_fork_cleanup,
1297 &config);
1298}
1299
52a0e931
JG
1300static void sessiond_uuid_log(void)
1301{
c70636a7 1302 char uuid_str[LTTNG_UUID_STR_LEN];
52a0e931
JG
1303
1304 lttng_uuid_to_str(sessiond_uuid, uuid_str);
1305 DBG("Starting lttng-sessiond {%s}", uuid_str);
1306}
1307
fac6795d
DG
1308/*
1309 * main
1310 */
1311int main(int argc, char **argv)
1312{
4a15001e 1313 int ret = 0, retval = 0;
e6142f2e 1314 const char *env_app_timeout;
b3530820
JG
1315 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
1316 *ust64_channel_monitor_pipe = NULL,
1317 *kernel_channel_monitor_pipe = NULL;
a3707772 1318 struct lttng_thread *ht_cleanup_thread = NULL;
bc26e826 1319 struct timer_thread_parameters timer_thread_parameters;
64d9b072
JG
1320 /* Rotation thread handle. */
1321 struct rotation_thread_handle *rotation_thread_handle = NULL;
d086f507
JD
1322 /* Queue of rotation jobs populated by the sessiond-timer. */
1323 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
917a718d 1324 struct lttng_thread *client_thread = NULL;
4a91420c 1325 struct lttng_thread *notification_thread = NULL;
bd9addf7 1326 struct lttng_thread *register_apps_thread = NULL;
fac6795d 1327
f5fb86c1 1328 logger_set_thread_name("Main", false);
335a95b7
MD
1329 init_kernel_workarounds();
1330
f6a9efaa
DG
1331 rcu_register_thread();
1332
4a15001e
MD
1333 if (set_signal_handler()) {
1334 retval = -1;
1335 goto exit_set_signal_handler;
0bb7724a
DG
1336 }
1337
92816cc3 1338 if (timer_signal_init()) {
d086f507
JD
1339 retval = -1;
1340 goto exit_set_signal_handler;
1341 }
1342
12744796
DG
1343 page_size = sysconf(_SC_PAGESIZE);
1344 if (page_size < 0) {
1345 PERROR("sysconf _SC_PAGESIZE");
1346 page_size = LONG_MAX;
1347 WARN("Fallback page size to %ld", page_size);
1348 }
1349
e6142f2e
JG
1350 ret = sessiond_config_init(&config);
1351 if (ret) {
1352 retval = -1;
1353 goto exit_set_signal_handler;
1354 }
1355
2788b494
JR
1356 /*
1357 * Init config from environment variables.
1358 * Command line option override env configuration per-doc. Do env first.
1359 */
1360 sessiond_config_apply_env_config(&config);
1361
4a15001e
MD
1362 /*
1363 * Parse arguments and load the daemon configuration file.
1364 *
1365 * We have an exit_options exit path to free memory reserved by
1366 * set_options. This is needed because the rest of sessiond_cleanup()
1367 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1368 * depends on set_options.
1369 */
fac6795d 1370 progname = argv[0];
4a15001e
MD
1371 if (set_options(argc, argv)) {
1372 retval = -1;
1373 goto exit_options;
fac6795d
DG
1374 }
1375
e6142f2e
JG
1376 /*
1377 * Resolve all paths received as arguments, configuration option, or
1378 * through environment variable as absolute paths. This is necessary
1379 * since daemonizing causes the sessiond's current working directory
1380 * to '/'.
1381 */
1382 ret = sessiond_config_resolve_paths(&config);
1383 if (ret) {
1384 goto exit_options;
1385 }
1386
1387 /* Apply config. */
1388 lttng_opt_verbose = config.verbose;
1389 lttng_opt_quiet = config.quiet;
1390 kconsumer_data.err_unix_sock_path =
1391 config.kconsumerd_err_unix_sock_path.value;
1392 kconsumer_data.cmd_unix_sock_path =
1393 config.kconsumerd_cmd_unix_sock_path.value;
1394 ustconsumer32_data.err_unix_sock_path =
1395 config.consumerd32_err_unix_sock_path.value;
1396 ustconsumer32_data.cmd_unix_sock_path =
1397 config.consumerd32_cmd_unix_sock_path.value;
1398 ustconsumer64_data.err_unix_sock_path =
1399 config.consumerd64_err_unix_sock_path.value;
1400 ustconsumer64_data.cmd_unix_sock_path =
1401 config.consumerd64_cmd_unix_sock_path.value;
1402 set_clock_plugin_env();
1403
1404 sessiond_config_log(&config);
52a0e931 1405 sessiond_uuid_log();
e6142f2e 1406
a3bc3918
JR
1407 if (opt_print_version) {
1408 print_version();
1409 retval = 0;
1410 goto exit_options;
1411 }
1412
f472090a
JG
1413 if (create_lttng_rundir()) {
1414 retval = -1;
1415 goto exit_options;
1416 }
1417
1418 /* Abort launch if a session daemon is already running. */
1419 if (check_existing_daemon()) {
1420 ERR("A session daemon is already running.");
1421 retval = -1;
1422 goto exit_options;
1423 }
1424
fac6795d 1425 /* Daemonize */
e6142f2e 1426 if (config.daemonize || config.background) {
ceed52b5
MD
1427 int i;
1428
72dd7491 1429 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
e6142f2e 1430 !config.background);
53094c05 1431 if (ret < 0) {
4a15001e
MD
1432 retval = -1;
1433 goto exit_options;
53094c05 1434 }
0bb7724a 1435
ceed52b5 1436 /*
0bb7724a 1437 * We are in the child. Make sure all other file descriptors are
4a15001e 1438 * closed, in case we are called with more opened file
f472090a 1439 * descriptors than the standard ones and the lock file.
ceed52b5
MD
1440 */
1441 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
f472090a
JG
1442 if (i == lockfile_fd) {
1443 continue;
1444 }
ceed52b5
MD
1445 (void) close(i);
1446 }
1447 }
1448
929f71ec 1449 if (launch_run_as_worker(argv[0]) < 0) {
7567352f
MD
1450 goto exit_create_run_as_worker_cleanup;
1451 }
1452
4a15001e
MD
1453 /*
1454 * Starting from here, we can create threads. This needs to be after
1455 * lttng_daemonize due to RCU.
1456 */
1457
1458 /*
1459 * Initialize the health check subsystem. This call should set the
1460 * appropriate time values.
1461 */
1462 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
1463 if (!health_sessiond) {
1464 PERROR("health_app_create error");
1465 retval = -1;
0d64e8fb 1466 goto stop_threads;
4a15001e
MD
1467 }
1468
4a15001e 1469 /* Create thread to clean up RCU hash tables */
a3707772
JG
1470 ht_cleanup_thread = launch_ht_cleanup_thread();
1471 if (!ht_cleanup_thread) {
4a15001e 1472 retval = -1;
0d64e8fb 1473 goto stop_threads;
4a15001e
MD
1474 }
1475
ceed52b5 1476 /* Create thread quit pipe */
a7333da7 1477 if (sessiond_init_thread_quit_pipe()) {
4a15001e 1478 retval = -1;
0d64e8fb 1479 goto stop_threads;
fac6795d
DG
1480 }
1481
1482 /* Check if daemon is UID = 0 */
1483 is_root = !getuid();
fac6795d 1484 if (is_root) {
67e40797 1485 /* Create global run dir with root access */
67e40797 1486
b3530820
JG
1487 kernel_channel_monitor_pipe = lttng_pipe_open(0);
1488 if (!kernel_channel_monitor_pipe) {
1489 ERR("Failed to create kernel consumer channel monitor pipe");
1490 retval = -1;
0d64e8fb 1491 goto stop_threads;
b3530820
JG
1492 }
1493 kconsumer_data.channel_monitor_pipe =
1494 lttng_pipe_release_writefd(
1495 kernel_channel_monitor_pipe);
1496 if (kconsumer_data.channel_monitor_pipe < 0) {
1497 retval = -1;
0d64e8fb 1498 goto stop_threads;
b3530820 1499 }
fac6795d
DG
1500 }
1501
5c827ce0
DG
1502 /* Set consumer initial state */
1503 kernel_consumerd_state = CONSUMER_STOPPED;
1504 ust_consumerd_state = CONSUMER_STOPPED;
1505
b3530820
JG
1506 ust32_channel_monitor_pipe = lttng_pipe_open(0);
1507 if (!ust32_channel_monitor_pipe) {
1508 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1509 retval = -1;
0d64e8fb 1510 goto stop_threads;
b3530820
JG
1511 }
1512 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1513 ust32_channel_monitor_pipe);
1514 if (ustconsumer32_data.channel_monitor_pipe < 0) {
1515 retval = -1;
0d64e8fb 1516 goto stop_threads;
b3530820 1517 }
62c43103 1518
d086f507 1519 /*
92816cc3
JG
1520 * The rotation_thread_timer_queue structure is shared between the
1521 * sessiond timer thread and the rotation thread. The main thread keeps
1522 * its ownership and destroys it when both threads have been joined.
d086f507 1523 */
92816cc3 1524 rotation_timer_queue = rotation_thread_timer_queue_create();
d086f507
JD
1525 if (!rotation_timer_queue) {
1526 retval = -1;
0d64e8fb 1527 goto stop_threads;
d086f507 1528 }
bc26e826
JG
1529 timer_thread_parameters.rotation_thread_job_queue =
1530 rotation_timer_queue;
67e40797 1531
b3530820
JG
1532 ust64_channel_monitor_pipe = lttng_pipe_open(0);
1533 if (!ust64_channel_monitor_pipe) {
1534 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1535 retval = -1;
0d64e8fb 1536 goto stop_threads;
b3530820
JG
1537 }
1538 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1539 ust64_channel_monitor_pipe);
1540 if (ustconsumer64_data.channel_monitor_pipe < 0) {
1541 retval = -1;
0d64e8fb 1542 goto stop_threads;
b3530820 1543 }
847177cd 1544
1427f9b2
DG
1545 /*
1546 * Init UST app hash table. Alloc hash table before this point since
1547 * cleanup() can get called after that point.
1548 */
4a15001e 1549 if (ust_app_ht_alloc()) {
ddbeb0f6 1550 ERR("Failed to allocate UST app hash table");
4a15001e 1551 retval = -1;
0d64e8fb 1552 goto stop_threads;
4a15001e 1553 }
1427f9b2 1554
2463b787
JR
1555 trigger_error_accounting_init(config.trigger_error_counter_bucket);
1556
6a4e4039
JG
1557 /*
1558 * Initialize agent app hash table. We allocate the hash table here
1559 * since cleanup() can get called after this point.
1560 */
1561 if (agent_app_ht_alloc()) {
1562 ERR("Failed to allocate Agent app hash table");
4a15001e 1563 retval = -1;
0d64e8fb 1564 goto stop_threads;
f20baf8e
DG
1565 }
1566
2463b787
JR
1567 if (trigger_agent_ht_alloc()) {
1568 ERR("Failed to allocate trigger agent hash table");
1569 retval = -1;
1570 goto stop_threads;
1571 }
a88df331
DG
1572 /*
1573 * These actions must be executed as root. We do that *after* setting up
1574 * the sockets path because we MUST make the check for another daemon using
1575 * those paths *before* trying to set the kernel consumer sockets and init
1576 * kernel tracer.
1577 */
1578 if (is_root) {
e6142f2e 1579 if (set_consumer_sockets(&kconsumer_data)) {
4a15001e 1580 retval = -1;
0d64e8fb 1581 goto stop_threads;
7753dea8
MD
1582 }
1583
a88df331 1584 /* Setup kernel tracer */
e6142f2e 1585 if (!config.no_kernel) {
4fba7219
DG
1586 init_kernel_tracer();
1587 }
a88df331
DG
1588
1589 /* Set ulimit for open files */
1590 set_ulimit();
fac6795d 1591 }
4063050c
MD
1592 /* init lttng_fd tracking must be done after set_ulimit. */
1593 lttng_fd_init();
fac6795d 1594
e6142f2e 1595 if (set_consumer_sockets(&ustconsumer64_data)) {
4a15001e 1596 retval = -1;
0d64e8fb 1597 goto stop_threads;
67e40797
DG
1598 }
1599
e6142f2e 1600 if (set_consumer_sockets(&ustconsumer32_data)) {
4a15001e 1601 retval = -1;
0d64e8fb 1602 goto stop_threads;
67e40797
DG
1603 }
1604
5b8719f5 1605 /* Get parent pid if -S, --sig-parent is specified. */
e6142f2e 1606 if (config.sig_parent) {
5b8719f5
DG
1607 ppid = getppid();
1608 }
1609
7a485870 1610 /* Setup the kernel pipe for waking up the kernel thread */
e6142f2e 1611 if (is_root && !config.no_kernel) {
4a15001e
MD
1612 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
1613 retval = -1;
0d64e8fb 1614 goto stop_threads;
6620da75 1615 }
7a485870
DG
1616 }
1617
099e26bd 1618 /* Setup the thread apps communication pipe. */
4a15001e
MD
1619 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
1620 retval = -1;
0d64e8fb 1621 goto stop_threads;
099e26bd
DG
1622 }
1623
d0b96690 1624 /* Setup the thread apps notify communication pipe. */
4a15001e
MD
1625 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
1626 retval = -1;
0d64e8fb 1627 goto stop_threads;
d0b96690
DG
1628 }
1629
7972aab2
DG
1630 /* Initialize global buffer per UID and PID registry. */
1631 buffer_reg_init_uid_registry();
1632 buffer_reg_init_pid_registry();
1633
099e26bd 1634 /* Init UST command queue. */
8bdee6e2 1635 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 1636
2f77fc4b 1637 cmd_init();
00e2e675 1638
ae9e45b3
DG
1639 /* Check for the application socket timeout env variable. */
1640 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
1641 if (env_app_timeout) {
e6142f2e 1642 config.app_socket_timeout = atoi(env_app_timeout);
ae9e45b3 1643 } else {
e6142f2e 1644 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
ae9e45b3
DG
1645 }
1646
4a15001e
MD
1647 ret = write_pidfile();
1648 if (ret) {
1649 ERR("Error in write_pidfile");
1650 retval = -1;
0d64e8fb 1651 goto stop_threads;
4a15001e 1652 }
35f90c40 1653
554831e7
MD
1654 /* Initialize communication library */
1655 lttcomm_init();
4a15001e 1656 /* Initialize TCP timeout values */
d831c249 1657 lttcomm_inet_init();
554831e7 1658
12b4fb37 1659 /* Create health-check thread. */
173fca4f 1660 if (!launch_health_management_thread()) {
4a15001e 1661 retval = -1;
0d64e8fb 1662 goto stop_threads;
44a5e5eb
DG
1663 }
1664
b3530820
JG
1665 /* notification_thread_data acquires the pipes' read side. */
1666 notification_thread_handle = notification_thread_handle_create(
1667 ust32_channel_monitor_pipe,
1668 ust64_channel_monitor_pipe,
2463b787
JR
1669 kernel_channel_monitor_pipe,
1670 kernel_get_notification_fd());
b3530820
JG
1671 if (!notification_thread_handle) {
1672 retval = -1;
1673 ERR("Failed to create notification thread shared data");
0d64e8fb 1674 goto stop_threads;
b3530820
JG
1675 }
1676
1677 /* Create notification thread. */
4a91420c
JG
1678 notification_thread = launch_notification_thread(
1679 notification_thread_handle);
1680 if (!notification_thread) {
b3530820 1681 retval = -1;
0d64e8fb 1682 goto stop_threads;
b3530820
JG
1683 }
1684
d086f507 1685 /* Create timer thread. */
bc26e826 1686 if (!launch_timer_thread(&timer_thread_parameters)) {
d086f507 1687 retval = -1;
0d64e8fb 1688 goto stop_threads;
d086f507 1689 }
d086f507 1690
db66e574
JD
1691 /* rotation_thread_data acquires the pipes' read side. */
1692 rotation_thread_handle = rotation_thread_handle_create(
90936dcf 1693 rotation_timer_queue,
c8a9de5a 1694 notification_thread_handle);
db66e574
JD
1695 if (!rotation_thread_handle) {
1696 retval = -1;
1697 ERR("Failed to create rotation thread shared data");
1698 stop_threads();
0d64e8fb 1699 goto stop_threads;
db66e574 1700 }
db66e574
JD
1701
1702 /* Create rotation thread. */
64d9b072 1703 if (!launch_rotation_thread(rotation_thread_handle)) {
db66e574 1704 retval = -1;
0d64e8fb 1705 goto stop_threads;
db66e574
JD
1706 }
1707
cf3af59e 1708 /* Create thread to manage the client socket */
917a718d
JG
1709 client_thread = launch_client_thread();
1710 if (!client_thread) {
4a15001e 1711 retval = -1;
0d64e8fb 1712 goto stop_threads;
cf3af59e 1713 }
fac6795d 1714
308df7bb
JG
1715 /* Set credentials of the client socket and rundir */
1716 if (is_root && set_permissions(config.rundir.value)) {
1717 retval = -1;
1718 goto stop_threads;
1719 }
1720
5d1b0219
JG
1721 if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1],
1722 apps_cmd_notify_pipe[1])) {
4a15001e 1723 retval = -1;
0d64e8fb 1724 goto stop_threads;
099e26bd
DG
1725 }
1726
1727 /* Create thread to manage application registration. */
bd9addf7
JG
1728 register_apps_thread = launch_application_registration_thread(
1729 &ust_cmd_queue);
1730 if (!register_apps_thread) {
4a15001e 1731 retval = -1;
0d64e8fb 1732 goto stop_threads;
099e26bd
DG
1733 }
1734
cf3af59e 1735 /* Create thread to manage application socket */
7649924e 1736 if (!launch_application_management_thread(apps_cmd_pipe[0])) {
4a15001e 1737 retval = -1;
0d64e8fb 1738 goto stop_threads;
d0b96690
DG
1739 }
1740
1741 /* Create thread to manage application notify socket */
971a61c6 1742 if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
4a15001e 1743 retval = -1;
0d64e8fb 1744 goto stop_threads;
cf3af59e 1745 }
fac6795d 1746
8a7e4590
JG
1747 /* Create agent management thread. */
1748 if (!launch_agent_management_thread()) {
4a15001e 1749 retval = -1;
0d64e8fb 1750 goto stop_threads;
4d076222
DG
1751 }
1752
6620da75 1753 /* Don't start this thread if kernel tracing is not requested nor root */
e6142f2e 1754 if (is_root && !config.no_kernel) {
6620da75 1755 /* Create kernel thread to manage kernel event */
5b093681 1756 if (!launch_kernel_management_thread(kernel_poll_pipe[0])) {
4a15001e 1757 retval = -1;
0d64e8fb 1758 goto stop_threads;
6620da75 1759 }
2463b787
JR
1760
1761 if (kernel_get_notification_fd() > -1) {
1762 ret = notification_thread_command_add_application(
1763 notification_thread_handle, kernel_get_notification_fd(), LTTNG_DOMAIN_KERNEL);
1764 if (ret != LTTNG_OK) {
1765 ERR("Failed to add kernel trigger event source to notification thread");
1766 retval = -1;
1767 goto stop_threads;
1768 }
1769 }
ef367a93 1770 }
7a485870 1771
3316b9d6
JG
1772 /* Load sessions. */
1773 ret = config_load_session(config.load_session_path.value,
1774 NULL, 1, 1, NULL);
4a15001e 1775 if (ret) {
3316b9d6 1776 ERR("Session load failed: %s", error_get_str(ret));
4a15001e 1777 retval = -1;
0d64e8fb 1778 goto stop_threads;
4a15001e
MD
1779 }
1780
3316b9d6
JG
1781 /* Initialization completed. */
1782 sessiond_signal_parents();
1783
4a15001e
MD
1784 /*
1785 * This is where we start awaiting program completion (e.g. through
1786 * signal that asks threads to teardown).
1787 */
1788
99d688f2 1789 /* Initiate teardown once activity occurs on the quit pipe. */
2d54bfb6 1790 sessiond_wait_for_quit_pipe(-1);
917a718d 1791
0d64e8fb 1792stop_threads:
2463b787
JR
1793
1794 trigger_error_accounting_fini();
917a718d
JG
1795 /*
1796 * Ensure that the client thread is no longer accepting new commands,
1797 * which could cause new sessions to be created.
1798 */
0d64e8fb
JG
1799 if (client_thread) {
1800 lttng_thread_shutdown(client_thread);
917a718d 1801 lttng_thread_put(client_thread);
917a718d
JG
1802 }
1803
99d688f2 1804 destroy_all_sessions_and_wait();
0d64e8fb 1805
bd9addf7
JG
1806 if (register_apps_thread) {
1807 lttng_thread_shutdown(register_apps_thread);
1808 lttng_thread_put(register_apps_thread);
1809 }
a3707772 1810 lttng_thread_list_shutdown_orphans();
917a718d 1811
4d62fbf8
MD
1812 /*
1813 * Wait for all pending call_rcu work to complete before tearing
1814 * down data structures. call_rcu worker may be trying to
1815 * perform lookups in those structures.
1816 */
1817 rcu_barrier();
4a15001e
MD
1818 /*
1819 * sessiond_cleanup() is called when no other thread is running, except
1820 * the ht_cleanup thread, which is needed to destroy the hash tables.
1821 */
1822 rcu_thread_online();
1823 sessiond_cleanup();
06f525de 1824
4a91420c
JG
1825 if (notification_thread) {
1826 lttng_thread_shutdown(notification_thread);
1827 lttng_thread_put(notification_thread);
1828 }
1829
c4d5de9d
JG
1830 /*
1831 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1832 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1833 * the queue is empty before shutting down the clean-up thread.
1834 */
1835 rcu_barrier();
1836
a3707772
JG
1837 if (ht_cleanup_thread) {
1838 lttng_thread_shutdown(ht_cleanup_thread);
1839 lttng_thread_put(ht_cleanup_thread);
1840 }
1841
64d9b072
JG
1842 rcu_thread_offline();
1843 rcu_unregister_thread();
1844
1845 if (rotation_thread_handle) {
1846 rotation_thread_handle_destroy(rotation_thread_handle);
1847 }
1848
d086f507
JD
1849 /*
1850 * After the rotation and timer thread have quit, we can safely destroy
1851 * the rotation_timer_queue.
1852 */
92816cc3 1853 rotation_thread_timer_queue_destroy(rotation_timer_queue);
c8a9de5a
JG
1854 /*
1855 * The teardown of the notification system is performed after the
1856 * session daemon's teardown in order to allow it to be notified
1857 * of the active session and channels at the moment of the teardown.
1858 */
1859 if (notification_thread_handle) {
1860 notification_thread_handle_destroy(notification_thread_handle);
1861 }
b3530820
JG
1862 lttng_pipe_destroy(ust32_channel_monitor_pipe);
1863 lttng_pipe_destroy(ust64_channel_monitor_pipe);
1864 lttng_pipe_destroy(kernel_channel_monitor_pipe);
4a15001e 1865
9bc2ec5f
JR
1866 if (health_sessiond) {
1867 health_app_destroy(health_sessiond);
1868 }
7567352f 1869exit_create_run_as_worker_cleanup:
4a15001e 1870exit_options:
f472090a 1871 sessiond_cleanup_lock_file();
4a15001e
MD
1872 sessiond_cleanup_options();
1873
1874exit_set_signal_handler:
1875 if (!retval) {
cf3af59e 1876 exit(EXIT_SUCCESS);
4a15001e
MD
1877 } else {
1878 exit(EXIT_FAILURE);
67e40797 1879 }
fac6795d 1880}
This page took 0.582646 seconds and 5 git commands to generate.