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