SoW-2020-0003: Trace Hit Counters
[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"
ebdb334b 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"
ebdb334b 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
ebdb334b
JR
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' },
ebdb334b 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 }
ebdb334b
JR
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);
ebdb334b 1267
3e3665b8
JG
1268 (void) cmd_destroy_session(session, notification_thread_handle,
1269 NULL);
99d688f2
JG
1270 unlock_session:
1271 session_unlock(session);
1272 session_put(session);
1273 }
1274 session_unlock_list();
1275
1276 /* Wait for the destruction of all sessions to complete. */
1277 DBG("Waiting for the destruction of all sessions to complete");
1278 session_list_wait_empty();
1279 DBG("Destruction of all sessions completed");
1280}
1281
b69a1b40
JG
1282static void unregister_all_triggers(void)
1283{
1284 enum lttng_error_code ret_code;
1285 enum lttng_trigger_status trigger_status;
1286 struct lttng_triggers *triggers = NULL;
1287 unsigned int trigger_count, i;
1288 const struct lttng_credentials creds = {
1289 .uid = LTTNG_OPTIONAL_INIT_VALUE(0),
1290 };
1291
1292 DBG("Unregistering all triggers");
1293
1294 /*
1295 * List all triggers as "root" since we wish to unregister all triggers.
1296 */
1297 ret_code = notification_thread_command_list_triggers(
1298 notification_thread_handle, creds.uid.value, &triggers);
1299 if (ret_code != LTTNG_OK) {
1300 ERR("Failed to list triggers while unregistering all triggers");
1301 goto end;
1302 }
1303
1304 trigger_status = lttng_triggers_get_count(triggers, &trigger_count);
1305 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1306
1307 for (i = 0; i < trigger_count; i++) {
1308 enum lttng_error_code ret_code;
1309 uid_t trigger_owner;
1310 const char *trigger_name;
1311 const struct lttng_trigger *trigger =
1312 lttng_triggers_get_at_index(triggers, i);
1313
1314 assert(trigger);
1315
1316 trigger_status = lttng_trigger_get_owner_uid(
1317 trigger, &trigger_owner);
1318 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1319
1320 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
1321 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1322
1323 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1324 (int) trigger_owner, trigger_name);
1325
1326 ret_code = cmd_unregister_trigger(
1327 &creds, trigger, notification_thread_handle);
1328 if (ret_code != LTTNG_OK) {
1329 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1330 (int) trigger_owner, trigger_name,
1331 lttng_strerror(-ret_code));
1332 /* Continue to unregister the remaining triggers. */
1333 }
1334 }
1335end:
1336 lttng_triggers_destroy(triggers);
1337}
1338
929f71ec
JG
1339static int run_as_worker_post_fork_cleanup(void *data)
1340{
1341 struct sessiond_config *sessiond_config = data;
1342
1343 sessiond_config_fini(sessiond_config);
1344 return 0;
1345}
1346
1347static int launch_run_as_worker(const char *procname)
1348{
1349 /*
1350 * Clean-up before forking the run-as worker. Any dynamically
1351 * allocated memory of which the worker is not aware will
1352 * be leaked as the process forks a run-as worker (and performs
1353 * no exec*()). The same would apply to any opened fd.
1354 */
1355 return run_as_create_worker(procname, run_as_worker_post_fork_cleanup,
1356 &config);
1357}
1358
52a0e931
JG
1359static void sessiond_uuid_log(void)
1360{
c70636a7 1361 char uuid_str[LTTNG_UUID_STR_LEN];
52a0e931
JG
1362
1363 lttng_uuid_to_str(sessiond_uuid, uuid_str);
1364 DBG("Starting lttng-sessiond {%s}", uuid_str);
1365}
1366
fac6795d
DG
1367/*
1368 * main
1369 */
1370int main(int argc, char **argv)
1371{
4a15001e 1372 int ret = 0, retval = 0;
e6142f2e 1373 const char *env_app_timeout;
b3530820
JG
1374 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
1375 *ust64_channel_monitor_pipe = NULL,
1376 *kernel_channel_monitor_pipe = NULL;
a3707772 1377 struct lttng_thread *ht_cleanup_thread = NULL;
bc26e826 1378 struct timer_thread_parameters timer_thread_parameters;
64d9b072
JG
1379 /* Rotation thread handle. */
1380 struct rotation_thread_handle *rotation_thread_handle = NULL;
d086f507
JD
1381 /* Queue of rotation jobs populated by the sessiond-timer. */
1382 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
917a718d 1383 struct lttng_thread *client_thread = NULL;
4a91420c 1384 struct lttng_thread *notification_thread = NULL;
bd9addf7 1385 struct lttng_thread *register_apps_thread = NULL;
fac6795d 1386
f5fb86c1 1387 logger_set_thread_name("Main", false);
335a95b7
MD
1388 init_kernel_workarounds();
1389
f6a9efaa
DG
1390 rcu_register_thread();
1391
4a15001e
MD
1392 if (set_signal_handler()) {
1393 retval = -1;
1394 goto exit_set_signal_handler;
0bb7724a
DG
1395 }
1396
92816cc3 1397 if (timer_signal_init()) {
d086f507
JD
1398 retval = -1;
1399 goto exit_set_signal_handler;
1400 }
1401
12744796
DG
1402 page_size = sysconf(_SC_PAGESIZE);
1403 if (page_size < 0) {
1404 PERROR("sysconf _SC_PAGESIZE");
1405 page_size = LONG_MAX;
1406 WARN("Fallback page size to %ld", page_size);
1407 }
1408
e6142f2e
JG
1409 ret = sessiond_config_init(&config);
1410 if (ret) {
1411 retval = -1;
1412 goto exit_set_signal_handler;
1413 }
1414
2788b494
JR
1415 /*
1416 * Init config from environment variables.
1417 * Command line option override env configuration per-doc. Do env first.
1418 */
1419 sessiond_config_apply_env_config(&config);
1420
4a15001e
MD
1421 /*
1422 * Parse arguments and load the daemon configuration file.
1423 *
1424 * We have an exit_options exit path to free memory reserved by
1425 * set_options. This is needed because the rest of sessiond_cleanup()
1426 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1427 * depends on set_options.
1428 */
fac6795d 1429 progname = argv[0];
4a15001e
MD
1430 if (set_options(argc, argv)) {
1431 retval = -1;
1432 goto exit_options;
fac6795d
DG
1433 }
1434
e6142f2e
JG
1435 /*
1436 * Resolve all paths received as arguments, configuration option, or
1437 * through environment variable as absolute paths. This is necessary
1438 * since daemonizing causes the sessiond's current working directory
1439 * to '/'.
1440 */
1441 ret = sessiond_config_resolve_paths(&config);
1442 if (ret) {
1443 goto exit_options;
1444 }
1445
1446 /* Apply config. */
1447 lttng_opt_verbose = config.verbose;
1448 lttng_opt_quiet = config.quiet;
1449 kconsumer_data.err_unix_sock_path =
1450 config.kconsumerd_err_unix_sock_path.value;
1451 kconsumer_data.cmd_unix_sock_path =
1452 config.kconsumerd_cmd_unix_sock_path.value;
1453 ustconsumer32_data.err_unix_sock_path =
1454 config.consumerd32_err_unix_sock_path.value;
1455 ustconsumer32_data.cmd_unix_sock_path =
1456 config.consumerd32_cmd_unix_sock_path.value;
1457 ustconsumer64_data.err_unix_sock_path =
1458 config.consumerd64_err_unix_sock_path.value;
1459 ustconsumer64_data.cmd_unix_sock_path =
1460 config.consumerd64_cmd_unix_sock_path.value;
1461 set_clock_plugin_env();
1462
1463 sessiond_config_log(&config);
52a0e931 1464 sessiond_uuid_log();
e6142f2e 1465
a3bc3918
JR
1466 if (opt_print_version) {
1467 print_version();
1468 retval = 0;
1469 goto exit_options;
1470 }
1471
f472090a
JG
1472 if (create_lttng_rundir()) {
1473 retval = -1;
1474 goto exit_options;
1475 }
1476
1477 /* Abort launch if a session daemon is already running. */
1478 if (check_existing_daemon()) {
1479 ERR("A session daemon is already running.");
1480 retval = -1;
1481 goto exit_options;
1482 }
1483
fac6795d 1484 /* Daemonize */
e6142f2e 1485 if (config.daemonize || config.background) {
ceed52b5
MD
1486 int i;
1487
72dd7491 1488 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
e6142f2e 1489 !config.background);
53094c05 1490 if (ret < 0) {
4a15001e
MD
1491 retval = -1;
1492 goto exit_options;
53094c05 1493 }
0bb7724a 1494
ceed52b5 1495 /*
0bb7724a 1496 * We are in the child. Make sure all other file descriptors are
4a15001e 1497 * closed, in case we are called with more opened file
f472090a 1498 * descriptors than the standard ones and the lock file.
ceed52b5
MD
1499 */
1500 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
f472090a
JG
1501 if (i == lockfile_fd) {
1502 continue;
1503 }
ceed52b5
MD
1504 (void) close(i);
1505 }
1506 }
1507
929f71ec 1508 if (launch_run_as_worker(argv[0]) < 0) {
7567352f
MD
1509 goto exit_create_run_as_worker_cleanup;
1510 }
1511
4a15001e
MD
1512 /*
1513 * Starting from here, we can create threads. This needs to be after
1514 * lttng_daemonize due to RCU.
1515 */
1516
1517 /*
1518 * Initialize the health check subsystem. This call should set the
1519 * appropriate time values.
1520 */
1521 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
1522 if (!health_sessiond) {
1523 PERROR("health_app_create error");
1524 retval = -1;
0d64e8fb 1525 goto stop_threads;
4a15001e
MD
1526 }
1527
4a15001e 1528 /* Create thread to clean up RCU hash tables */
a3707772
JG
1529 ht_cleanup_thread = launch_ht_cleanup_thread();
1530 if (!ht_cleanup_thread) {
4a15001e 1531 retval = -1;
0d64e8fb 1532 goto stop_threads;
4a15001e
MD
1533 }
1534
ceed52b5 1535 /* Create thread quit pipe */
a7333da7 1536 if (sessiond_init_thread_quit_pipe()) {
4a15001e 1537 retval = -1;
0d64e8fb 1538 goto stop_threads;
fac6795d
DG
1539 }
1540
1541 /* Check if daemon is UID = 0 */
1542 is_root = !getuid();
fac6795d 1543 if (is_root) {
67e40797 1544 /* Create global run dir with root access */
67e40797 1545
b3530820
JG
1546 kernel_channel_monitor_pipe = lttng_pipe_open(0);
1547 if (!kernel_channel_monitor_pipe) {
1548 ERR("Failed to create kernel consumer channel monitor pipe");
1549 retval = -1;
0d64e8fb 1550 goto stop_threads;
b3530820
JG
1551 }
1552 kconsumer_data.channel_monitor_pipe =
1553 lttng_pipe_release_writefd(
1554 kernel_channel_monitor_pipe);
1555 if (kconsumer_data.channel_monitor_pipe < 0) {
1556 retval = -1;
0d64e8fb 1557 goto stop_threads;
b3530820 1558 }
fac6795d
DG
1559 }
1560
5c827ce0
DG
1561 /* Set consumer initial state */
1562 kernel_consumerd_state = CONSUMER_STOPPED;
1563 ust_consumerd_state = CONSUMER_STOPPED;
1564
b3530820
JG
1565 ust32_channel_monitor_pipe = lttng_pipe_open(0);
1566 if (!ust32_channel_monitor_pipe) {
1567 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1568 retval = -1;
0d64e8fb 1569 goto stop_threads;
b3530820
JG
1570 }
1571 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1572 ust32_channel_monitor_pipe);
1573 if (ustconsumer32_data.channel_monitor_pipe < 0) {
1574 retval = -1;
0d64e8fb 1575 goto stop_threads;
b3530820 1576 }
62c43103 1577
d086f507 1578 /*
92816cc3
JG
1579 * The rotation_thread_timer_queue structure is shared between the
1580 * sessiond timer thread and the rotation thread. The main thread keeps
1581 * its ownership and destroys it when both threads have been joined.
d086f507 1582 */
92816cc3 1583 rotation_timer_queue = rotation_thread_timer_queue_create();
d086f507
JD
1584 if (!rotation_timer_queue) {
1585 retval = -1;
0d64e8fb 1586 goto stop_threads;
d086f507 1587 }
bc26e826
JG
1588 timer_thread_parameters.rotation_thread_job_queue =
1589 rotation_timer_queue;
67e40797 1590
b3530820
JG
1591 ust64_channel_monitor_pipe = lttng_pipe_open(0);
1592 if (!ust64_channel_monitor_pipe) {
1593 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1594 retval = -1;
0d64e8fb 1595 goto stop_threads;
b3530820
JG
1596 }
1597 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1598 ust64_channel_monitor_pipe);
1599 if (ustconsumer64_data.channel_monitor_pipe < 0) {
1600 retval = -1;
0d64e8fb 1601 goto stop_threads;
b3530820 1602 }
847177cd 1603
1427f9b2
DG
1604 /*
1605 * Init UST app hash table. Alloc hash table before this point since
1606 * cleanup() can get called after that point.
1607 */
4a15001e 1608 if (ust_app_ht_alloc()) {
ddbeb0f6 1609 ERR("Failed to allocate UST app hash table");
4a15001e 1610 retval = -1;
0d64e8fb 1611 goto stop_threads;
4a15001e 1612 }
1427f9b2 1613
ebdb334b
JR
1614 event_notifier_error_accounting_init(config.event_notifier_error_counter_bucket);
1615
6a4e4039
JG
1616 /*
1617 * Initialize agent app hash table. We allocate the hash table here
1618 * since cleanup() can get called after this point.
1619 */
1620 if (agent_app_ht_alloc()) {
1621 ERR("Failed to allocate Agent app hash table");
4a15001e 1622 retval = -1;
0d64e8fb 1623 goto stop_threads;
f20baf8e
DG
1624 }
1625
44760c20
JR
1626 if (agent_by_event_notifier_domain_ht_create()) {
1627 ERR("Failed to allocate per-event notifier domain agent hash table");
1628 retval = -1;
1629 goto stop_threads;
1630 }
a88df331
DG
1631 /*
1632 * These actions must be executed as root. We do that *after* setting up
1633 * the sockets path because we MUST make the check for another daemon using
1634 * those paths *before* trying to set the kernel consumer sockets and init
1635 * kernel tracer.
1636 */
1637 if (is_root) {
e6142f2e 1638 if (set_consumer_sockets(&kconsumer_data)) {
4a15001e 1639 retval = -1;
0d64e8fb 1640 goto stop_threads;
7753dea8
MD
1641 }
1642
a88df331 1643 /* Setup kernel tracer */
e6142f2e 1644 if (!config.no_kernel) {
4fba7219
DG
1645 init_kernel_tracer();
1646 }
a88df331
DG
1647
1648 /* Set ulimit for open files */
1649 set_ulimit();
fac6795d 1650 }
4063050c
MD
1651 /* init lttng_fd tracking must be done after set_ulimit. */
1652 lttng_fd_init();
fac6795d 1653
e6142f2e 1654 if (set_consumer_sockets(&ustconsumer64_data)) {
4a15001e 1655 retval = -1;
0d64e8fb 1656 goto stop_threads;
67e40797
DG
1657 }
1658
e6142f2e 1659 if (set_consumer_sockets(&ustconsumer32_data)) {
4a15001e 1660 retval = -1;
0d64e8fb 1661 goto stop_threads;
67e40797
DG
1662 }
1663
5b8719f5 1664 /* Get parent pid if -S, --sig-parent is specified. */
e6142f2e 1665 if (config.sig_parent) {
5b8719f5
DG
1666 ppid = getppid();
1667 }
1668
7a485870 1669 /* Setup the kernel pipe for waking up the kernel thread */
e6142f2e 1670 if (is_root && !config.no_kernel) {
4a15001e
MD
1671 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
1672 retval = -1;
0d64e8fb 1673 goto stop_threads;
6620da75 1674 }
7a485870
DG
1675 }
1676
099e26bd 1677 /* Setup the thread apps communication pipe. */
4a15001e
MD
1678 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
1679 retval = -1;
0d64e8fb 1680 goto stop_threads;
099e26bd
DG
1681 }
1682
d0b96690 1683 /* Setup the thread apps notify communication pipe. */
4a15001e
MD
1684 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
1685 retval = -1;
0d64e8fb 1686 goto stop_threads;
d0b96690
DG
1687 }
1688
7972aab2
DG
1689 /* Initialize global buffer per UID and PID registry. */
1690 buffer_reg_init_uid_registry();
1691 buffer_reg_init_pid_registry();
1692
099e26bd 1693 /* Init UST command queue. */
8bdee6e2 1694 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 1695
2f77fc4b 1696 cmd_init();
00e2e675 1697
ae9e45b3
DG
1698 /* Check for the application socket timeout env variable. */
1699 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
1700 if (env_app_timeout) {
e6142f2e 1701 config.app_socket_timeout = atoi(env_app_timeout);
ae9e45b3 1702 } else {
e6142f2e 1703 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
ae9e45b3
DG
1704 }
1705
4a15001e
MD
1706 ret = write_pidfile();
1707 if (ret) {
1708 ERR("Error in write_pidfile");
1709 retval = -1;
0d64e8fb 1710 goto stop_threads;
4a15001e 1711 }
35f90c40 1712
554831e7
MD
1713 /* Initialize communication library */
1714 lttcomm_init();
4a15001e 1715 /* Initialize TCP timeout values */
d831c249 1716 lttcomm_inet_init();
554831e7 1717
12b4fb37 1718 /* Create health-check thread. */
173fca4f 1719 if (!launch_health_management_thread()) {
4a15001e 1720 retval = -1;
0d64e8fb 1721 goto stop_threads;
44a5e5eb
DG
1722 }
1723
b3530820
JG
1724 /* notification_thread_data acquires the pipes' read side. */
1725 notification_thread_handle = notification_thread_handle_create(
1726 ust32_channel_monitor_pipe,
1727 ust64_channel_monitor_pipe,
c8a9de5a 1728 kernel_channel_monitor_pipe);
b3530820
JG
1729 if (!notification_thread_handle) {
1730 retval = -1;
1731 ERR("Failed to create notification thread shared data");
0d64e8fb 1732 goto stop_threads;
b3530820
JG
1733 }
1734
1735 /* Create notification thread. */
4a91420c
JG
1736 notification_thread = launch_notification_thread(
1737 notification_thread_handle);
1738 if (!notification_thread) {
b3530820 1739 retval = -1;
0d64e8fb 1740 goto stop_threads;
b3530820
JG
1741 }
1742
d086f507 1743 /* Create timer thread. */
bc26e826 1744 if (!launch_timer_thread(&timer_thread_parameters)) {
d086f507 1745 retval = -1;
0d64e8fb 1746 goto stop_threads;
d086f507 1747 }
d086f507 1748
db66e574
JD
1749 /* rotation_thread_data acquires the pipes' read side. */
1750 rotation_thread_handle = rotation_thread_handle_create(
90936dcf 1751 rotation_timer_queue,
c8a9de5a 1752 notification_thread_handle);
db66e574
JD
1753 if (!rotation_thread_handle) {
1754 retval = -1;
1755 ERR("Failed to create rotation thread shared data");
1756 stop_threads();
0d64e8fb 1757 goto stop_threads;
db66e574 1758 }
db66e574
JD
1759
1760 /* Create rotation thread. */
64d9b072 1761 if (!launch_rotation_thread(rotation_thread_handle)) {
db66e574 1762 retval = -1;
0d64e8fb 1763 goto stop_threads;
db66e574
JD
1764 }
1765
cf3af59e 1766 /* Create thread to manage the client socket */
917a718d
JG
1767 client_thread = launch_client_thread();
1768 if (!client_thread) {
4a15001e 1769 retval = -1;
0d64e8fb 1770 goto stop_threads;
cf3af59e 1771 }
fac6795d 1772
308df7bb
JG
1773 /* Set credentials of the client socket and rundir */
1774 if (is_root && set_permissions(config.rundir.value)) {
1775 retval = -1;
1776 goto stop_threads;
1777 }
1778
5d1b0219
JG
1779 if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1],
1780 apps_cmd_notify_pipe[1])) {
4a15001e 1781 retval = -1;
0d64e8fb 1782 goto stop_threads;
099e26bd
DG
1783 }
1784
1785 /* Create thread to manage application registration. */
bd9addf7
JG
1786 register_apps_thread = launch_application_registration_thread(
1787 &ust_cmd_queue);
1788 if (!register_apps_thread) {
4a15001e 1789 retval = -1;
0d64e8fb 1790 goto stop_threads;
099e26bd
DG
1791 }
1792
cf3af59e 1793 /* Create thread to manage application socket */
7649924e 1794 if (!launch_application_management_thread(apps_cmd_pipe[0])) {
4a15001e 1795 retval = -1;
0d64e8fb 1796 goto stop_threads;
d0b96690
DG
1797 }
1798
1799 /* Create thread to manage application notify socket */
971a61c6 1800 if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
4a15001e 1801 retval = -1;
0d64e8fb 1802 goto stop_threads;
cf3af59e 1803 }
fac6795d 1804
8a7e4590
JG
1805 /* Create agent management thread. */
1806 if (!launch_agent_management_thread()) {
4a15001e 1807 retval = -1;
0d64e8fb 1808 goto stop_threads;
4d076222
DG
1809 }
1810
6620da75 1811 /* Don't start this thread if kernel tracing is not requested nor root */
e6142f2e 1812 if (is_root && !config.no_kernel) {
6620da75 1813 /* Create kernel thread to manage kernel event */
5b093681 1814 if (!launch_kernel_management_thread(kernel_poll_pipe[0])) {
4a15001e 1815 retval = -1;
0d64e8fb 1816 goto stop_threads;
6620da75 1817 }
352b58f5
JR
1818
1819 if (kernel_get_notification_fd() >= 0) {
1820 ret = notification_thread_command_add_tracer_event_source(
1821 notification_thread_handle,
1822 kernel_get_notification_fd(),
1823 LTTNG_DOMAIN_KERNEL);
1824 if (ret != LTTNG_OK) {
1825 ERR("Failed to add kernel trigger event source to notification thread");
1826 retval = -1;
1827 goto stop_threads;
1828 }
1829 }
ef367a93 1830 }
7a485870 1831
3316b9d6
JG
1832 /* Load sessions. */
1833 ret = config_load_session(config.load_session_path.value,
1834 NULL, 1, 1, NULL);
4a15001e 1835 if (ret) {
3316b9d6 1836 ERR("Session load failed: %s", error_get_str(ret));
4a15001e 1837 retval = -1;
0d64e8fb 1838 goto stop_threads;
4a15001e
MD
1839 }
1840
3316b9d6
JG
1841 /* Initialization completed. */
1842 sessiond_signal_parents();
1843
4a15001e
MD
1844 /*
1845 * This is where we start awaiting program completion (e.g. through
1846 * signal that asks threads to teardown).
1847 */
1848
99d688f2 1849 /* Initiate teardown once activity occurs on the quit pipe. */
2d54bfb6 1850 sessiond_wait_for_quit_pipe(-1);
917a718d 1851
0d64e8fb 1852stop_threads:
ebdb334b 1853
917a718d
JG
1854 /*
1855 * Ensure that the client thread is no longer accepting new commands,
1856 * which could cause new sessions to be created.
1857 */
0d64e8fb
JG
1858 if (client_thread) {
1859 lttng_thread_shutdown(client_thread);
917a718d 1860 lttng_thread_put(client_thread);
917a718d
JG
1861 }
1862
99d688f2 1863 destroy_all_sessions_and_wait();
0d64e8fb 1864
b69a1b40
JG
1865 /*
1866 * At this point no new trigger can be registered (no sessions are
1867 * running/rotating) and clients can't connect to the session daemon
1868 * anymore. Unregister all triggers.
1869 */
1870 unregister_all_triggers();
1871
bd9addf7
JG
1872 if (register_apps_thread) {
1873 lttng_thread_shutdown(register_apps_thread);
1874 lttng_thread_put(register_apps_thread);
1875 }
a3707772 1876 lttng_thread_list_shutdown_orphans();
917a718d 1877
4d62fbf8
MD
1878 /*
1879 * Wait for all pending call_rcu work to complete before tearing
1880 * down data structures. call_rcu worker may be trying to
1881 * perform lookups in those structures.
1882 */
1883 rcu_barrier();
4a15001e
MD
1884 /*
1885 * sessiond_cleanup() is called when no other thread is running, except
1886 * the ht_cleanup thread, which is needed to destroy the hash tables.
1887 */
1888 rcu_thread_online();
1889 sessiond_cleanup();
06f525de 1890
3b8a092f 1891 /*
ebdb334b 1892 * Wait for all pending call_rcu work to complete before shutting down
3b8a092f
FD
1893 * the notification thread. This call_rcu work includes shutting down
1894 * UST apps and event notifier pipes.
1895 */
1896 rcu_barrier();
1897
4a91420c
JG
1898 if (notification_thread) {
1899 lttng_thread_shutdown(notification_thread);
1900 lttng_thread_put(notification_thread);
1901 }
1902
ebdb334b
JR
1903 /*
1904 * Error accounting teardown has to be done after the teardown of all
1905 * event notifier pipes to ensure that no tracer may try to use the
1906 * error accounting facilities.
1907 */
1908 event_notifier_error_accounting_fini();
1909
1910 /*
1911 * Unloading the kernel modules needs to be done after all kernel
1912 * ressources have been released. In our case, this includes the
1913 * notification fd, the event notifier group fd, error accounting fd,
1914 * all event and event notifier fds, etc.
1915 *
1916 * In short, at this point, we need to have called close() on all fds
1917 * received from the kernel tracer.
1918 */
1919 if (is_root && !config.no_kernel) {
1920 DBG("Unloading kernel modules");
1921 modprobe_remove_lttng_all();
1922 }
1923
c4d5de9d
JG
1924 /*
1925 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1926 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1927 * the queue is empty before shutting down the clean-up thread.
1928 */
1929 rcu_barrier();
1930
a3707772
JG
1931 if (ht_cleanup_thread) {
1932 lttng_thread_shutdown(ht_cleanup_thread);
1933 lttng_thread_put(ht_cleanup_thread);
1934 }
1935
64d9b072
JG
1936 rcu_thread_offline();
1937 rcu_unregister_thread();
1938
1939 if (rotation_thread_handle) {
1940 rotation_thread_handle_destroy(rotation_thread_handle);
1941 }
1942
d086f507
JD
1943 /*
1944 * After the rotation and timer thread have quit, we can safely destroy
1945 * the rotation_timer_queue.
1946 */
92816cc3 1947 rotation_thread_timer_queue_destroy(rotation_timer_queue);
c8a9de5a
JG
1948 /*
1949 * The teardown of the notification system is performed after the
1950 * session daemon's teardown in order to allow it to be notified
1951 * of the active session and channels at the moment of the teardown.
1952 */
1953 if (notification_thread_handle) {
1954 notification_thread_handle_destroy(notification_thread_handle);
1955 }
b3530820
JG
1956 lttng_pipe_destroy(ust32_channel_monitor_pipe);
1957 lttng_pipe_destroy(ust64_channel_monitor_pipe);
1958 lttng_pipe_destroy(kernel_channel_monitor_pipe);
4a15001e 1959
9bc2ec5f
JR
1960 if (health_sessiond) {
1961 health_app_destroy(health_sessiond);
1962 }
7567352f 1963exit_create_run_as_worker_cleanup:
4a15001e 1964exit_options:
f472090a 1965 sessiond_cleanup_lock_file();
4a15001e
MD
1966 sessiond_cleanup_options();
1967
1968exit_set_signal_handler:
1969 if (!retval) {
cf3af59e 1970 exit(EXIT_SUCCESS);
4a15001e
MD
1971 } else {
1972 exit(EXIT_FAILURE);
67e40797 1973 }
fac6795d 1974}
This page took 0.404705 seconds and 5 git commands to generate.