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