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