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