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