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