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