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