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