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