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