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