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