Clean-up: sessiond: prepend `the_` to global variable names
[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++) {
1325 enum lttng_error_code ret_code;
1326 uid_t trigger_owner;
1327 const char *trigger_name;
1328 const struct lttng_trigger *trigger =
1329 lttng_triggers_get_at_index(triggers, i);
1330
1331 assert(trigger);
1332
1333 trigger_status = lttng_trigger_get_owner_uid(
1334 trigger, &trigger_owner);
1335 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1336
1337 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
1338 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
1339
1340 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1341 (int) trigger_owner, trigger_name);
1342
412d7227
SM
1343 ret_code = cmd_unregister_trigger(&creds, trigger,
1344 the_notification_thread_handle);
b69a1b40
JG
1345 if (ret_code != LTTNG_OK) {
1346 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1347 (int) trigger_owner, trigger_name,
1348 lttng_strerror(-ret_code));
1349 /* Continue to unregister the remaining triggers. */
1350 }
1351 }
1352end:
1353 lttng_triggers_destroy(triggers);
1354}
1355
929f71ec
JG
1356static int run_as_worker_post_fork_cleanup(void *data)
1357{
1358 struct sessiond_config *sessiond_config = data;
1359
1360 sessiond_config_fini(sessiond_config);
1361 return 0;
1362}
1363
1364static int launch_run_as_worker(const char *procname)
1365{
1366 /*
1367 * Clean-up before forking the run-as worker. Any dynamically
1368 * allocated memory of which the worker is not aware will
1369 * be leaked as the process forks a run-as worker (and performs
1370 * no exec*()). The same would apply to any opened fd.
1371 */
412d7227
SM
1372 return run_as_create_worker(
1373 procname, run_as_worker_post_fork_cleanup, &the_config);
929f71ec
JG
1374}
1375
52a0e931
JG
1376static void sessiond_uuid_log(void)
1377{
c70636a7 1378 char uuid_str[LTTNG_UUID_STR_LEN];
52a0e931 1379
412d7227 1380 lttng_uuid_to_str(the_sessiond_uuid, uuid_str);
52a0e931
JG
1381 DBG("Starting lttng-sessiond {%s}", uuid_str);
1382}
1383
fac6795d
DG
1384/*
1385 * main
1386 */
1387int main(int argc, char **argv)
1388{
4a15001e 1389 int ret = 0, retval = 0;
e6142f2e 1390 const char *env_app_timeout;
b3530820
JG
1391 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
1392 *ust64_channel_monitor_pipe = NULL,
1393 *kernel_channel_monitor_pipe = NULL;
a3707772 1394 struct lttng_thread *ht_cleanup_thread = NULL;
bc26e826 1395 struct timer_thread_parameters timer_thread_parameters;
64d9b072
JG
1396 /* Rotation thread handle. */
1397 struct rotation_thread_handle *rotation_thread_handle = NULL;
d086f507
JD
1398 /* Queue of rotation jobs populated by the sessiond-timer. */
1399 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
917a718d 1400 struct lttng_thread *client_thread = NULL;
4a91420c 1401 struct lttng_thread *notification_thread = NULL;
bd9addf7 1402 struct lttng_thread *register_apps_thread = NULL;
fac6795d 1403
f5fb86c1 1404 logger_set_thread_name("Main", false);
335a95b7
MD
1405 init_kernel_workarounds();
1406
f6a9efaa
DG
1407 rcu_register_thread();
1408
4a15001e
MD
1409 if (set_signal_handler()) {
1410 retval = -1;
1411 goto exit_set_signal_handler;
0bb7724a
DG
1412 }
1413
92816cc3 1414 if (timer_signal_init()) {
d086f507
JD
1415 retval = -1;
1416 goto exit_set_signal_handler;
1417 }
1418
412d7227
SM
1419 the_page_size = sysconf(_SC_PAGESIZE);
1420 if (the_page_size < 0) {
12744796 1421 PERROR("sysconf _SC_PAGESIZE");
412d7227
SM
1422 the_page_size = LONG_MAX;
1423 WARN("Fallback page size to %ld", the_page_size);
12744796
DG
1424 }
1425
412d7227 1426 ret = sessiond_config_init(&the_config);
e6142f2e
JG
1427 if (ret) {
1428 retval = -1;
1429 goto exit_set_signal_handler;
1430 }
1431
2788b494
JR
1432 /*
1433 * Init config from environment variables.
1434 * Command line option override env configuration per-doc. Do env first.
1435 */
412d7227 1436 sessiond_config_apply_env_config(&the_config);
2788b494 1437
4a15001e
MD
1438 /*
1439 * Parse arguments and load the daemon configuration file.
1440 *
1441 * We have an exit_options exit path to free memory reserved by
1442 * set_options. This is needed because the rest of sessiond_cleanup()
1443 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1444 * depends on set_options.
1445 */
fac6795d 1446 progname = argv[0];
4a15001e
MD
1447 if (set_options(argc, argv)) {
1448 retval = -1;
1449 goto exit_options;
fac6795d
DG
1450 }
1451
e6142f2e
JG
1452 /*
1453 * Resolve all paths received as arguments, configuration option, or
1454 * through environment variable as absolute paths. This is necessary
1455 * since daemonizing causes the sessiond's current working directory
1456 * to '/'.
1457 */
412d7227 1458 ret = sessiond_config_resolve_paths(&the_config);
e6142f2e
JG
1459 if (ret) {
1460 goto exit_options;
1461 }
1462
1463 /* Apply config. */
412d7227
SM
1464 lttng_opt_verbose = the_config.verbose;
1465 lttng_opt_quiet = the_config.quiet;
1466 the_kconsumer_data.err_unix_sock_path =
1467 the_config.kconsumerd_err_unix_sock_path.value;
1468 the_kconsumer_data.cmd_unix_sock_path =
1469 the_config.kconsumerd_cmd_unix_sock_path.value;
1470 the_ustconsumer32_data.err_unix_sock_path =
1471 the_config.consumerd32_err_unix_sock_path.value;
1472 the_ustconsumer32_data.cmd_unix_sock_path =
1473 the_config.consumerd32_cmd_unix_sock_path.value;
1474 the_ustconsumer64_data.err_unix_sock_path =
1475 the_config.consumerd64_err_unix_sock_path.value;
1476 the_ustconsumer64_data.cmd_unix_sock_path =
1477 the_config.consumerd64_cmd_unix_sock_path.value;
e6142f2e
JG
1478 set_clock_plugin_env();
1479
412d7227 1480 sessiond_config_log(&the_config);
52a0e931 1481 sessiond_uuid_log();
e6142f2e 1482
a3bc3918
JR
1483 if (opt_print_version) {
1484 print_version();
1485 retval = 0;
1486 goto exit_options;
1487 }
1488
f472090a
JG
1489 if (create_lttng_rundir()) {
1490 retval = -1;
1491 goto exit_options;
1492 }
1493
1494 /* Abort launch if a session daemon is already running. */
1495 if (check_existing_daemon()) {
1496 ERR("A session daemon is already running.");
1497 retval = -1;
1498 goto exit_options;
1499 }
1500
fac6795d 1501 /* Daemonize */
412d7227 1502 if (the_config.daemonize || the_config.background) {
ceed52b5
MD
1503 int i;
1504
412d7227
SM
1505 ret = lttng_daemonize(&the_child_ppid, &recv_child_signal,
1506 !the_config.background);
53094c05 1507 if (ret < 0) {
4a15001e
MD
1508 retval = -1;
1509 goto exit_options;
53094c05 1510 }
0bb7724a 1511
ceed52b5 1512 /*
0bb7724a 1513 * We are in the child. Make sure all other file descriptors are
4a15001e 1514 * closed, in case we are called with more opened file
f472090a 1515 * descriptors than the standard ones and the lock file.
ceed52b5
MD
1516 */
1517 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
f472090a
JG
1518 if (i == lockfile_fd) {
1519 continue;
1520 }
ceed52b5
MD
1521 (void) close(i);
1522 }
1523 }
1524
929f71ec 1525 if (launch_run_as_worker(argv[0]) < 0) {
7567352f
MD
1526 goto exit_create_run_as_worker_cleanup;
1527 }
1528
4a15001e
MD
1529 /*
1530 * Starting from here, we can create threads. This needs to be after
1531 * lttng_daemonize due to RCU.
1532 */
1533
1534 /*
1535 * Initialize the health check subsystem. This call should set the
1536 * appropriate time values.
1537 */
412d7227
SM
1538 the_health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
1539 if (!the_health_sessiond) {
4a15001e
MD
1540 PERROR("health_app_create error");
1541 retval = -1;
0d64e8fb 1542 goto stop_threads;
4a15001e
MD
1543 }
1544
4a15001e 1545 /* Create thread to clean up RCU hash tables */
a3707772
JG
1546 ht_cleanup_thread = launch_ht_cleanup_thread();
1547 if (!ht_cleanup_thread) {
4a15001e 1548 retval = -1;
0d64e8fb 1549 goto stop_threads;
4a15001e
MD
1550 }
1551
ceed52b5 1552 /* Create thread quit pipe */
a7333da7 1553 if (sessiond_init_thread_quit_pipe()) {
4a15001e 1554 retval = -1;
0d64e8fb 1555 goto stop_threads;
fac6795d
DG
1556 }
1557
1558 /* Check if daemon is UID = 0 */
1559 is_root = !getuid();
fac6795d 1560 if (is_root) {
67e40797 1561 /* Create global run dir with root access */
67e40797 1562
b3530820
JG
1563 kernel_channel_monitor_pipe = lttng_pipe_open(0);
1564 if (!kernel_channel_monitor_pipe) {
1565 ERR("Failed to create kernel consumer channel monitor pipe");
1566 retval = -1;
0d64e8fb 1567 goto stop_threads;
b3530820 1568 }
412d7227 1569 the_kconsumer_data.channel_monitor_pipe =
b3530820 1570 lttng_pipe_release_writefd(
412d7227
SM
1571 kernel_channel_monitor_pipe);
1572 if (the_kconsumer_data.channel_monitor_pipe < 0) {
b3530820 1573 retval = -1;
0d64e8fb 1574 goto stop_threads;
b3530820 1575 }
fac6795d
DG
1576 }
1577
5c827ce0 1578 /* Set consumer initial state */
412d7227
SM
1579 the_kernel_consumerd_state = CONSUMER_STOPPED;
1580 the_ust_consumerd_state = CONSUMER_STOPPED;
5c827ce0 1581
b3530820
JG
1582 ust32_channel_monitor_pipe = lttng_pipe_open(0);
1583 if (!ust32_channel_monitor_pipe) {
1584 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1585 retval = -1;
0d64e8fb 1586 goto stop_threads;
b3530820 1587 }
412d7227
SM
1588 the_ustconsumer32_data.channel_monitor_pipe =
1589 lttng_pipe_release_writefd(ust32_channel_monitor_pipe);
1590 if (the_ustconsumer32_data.channel_monitor_pipe < 0) {
b3530820 1591 retval = -1;
0d64e8fb 1592 goto stop_threads;
b3530820 1593 }
62c43103 1594
d086f507 1595 /*
92816cc3
JG
1596 * The rotation_thread_timer_queue structure is shared between the
1597 * sessiond timer thread and the rotation thread. The main thread keeps
1598 * its ownership and destroys it when both threads have been joined.
d086f507 1599 */
92816cc3 1600 rotation_timer_queue = rotation_thread_timer_queue_create();
d086f507
JD
1601 if (!rotation_timer_queue) {
1602 retval = -1;
0d64e8fb 1603 goto stop_threads;
d086f507 1604 }
bc26e826
JG
1605 timer_thread_parameters.rotation_thread_job_queue =
1606 rotation_timer_queue;
67e40797 1607
b3530820
JG
1608 ust64_channel_monitor_pipe = lttng_pipe_open(0);
1609 if (!ust64_channel_monitor_pipe) {
1610 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1611 retval = -1;
0d64e8fb 1612 goto stop_threads;
b3530820 1613 }
412d7227
SM
1614 the_ustconsumer64_data.channel_monitor_pipe =
1615 lttng_pipe_release_writefd(ust64_channel_monitor_pipe);
1616 if (the_ustconsumer64_data.channel_monitor_pipe < 0) {
b3530820 1617 retval = -1;
0d64e8fb 1618 goto stop_threads;
b3530820 1619 }
847177cd 1620
1427f9b2
DG
1621 /*
1622 * Init UST app hash table. Alloc hash table before this point since
1623 * cleanup() can get called after that point.
1624 */
4a15001e 1625 if (ust_app_ht_alloc()) {
ddbeb0f6 1626 ERR("Failed to allocate UST app hash table");
4a15001e 1627 retval = -1;
0d64e8fb 1628 goto stop_threads;
4a15001e 1629 }
1427f9b2 1630
412d7227 1631 event_notifier_error_accounting_init(the_config.event_notifier_error_counter_bucket);
90aa04a1 1632
6a4e4039
JG
1633 /*
1634 * Initialize agent app hash table. We allocate the hash table here
1635 * since cleanup() can get called after this point.
1636 */
1637 if (agent_app_ht_alloc()) {
1638 ERR("Failed to allocate Agent app hash table");
4a15001e 1639 retval = -1;
0d64e8fb 1640 goto stop_threads;
f20baf8e
DG
1641 }
1642
44760c20
JR
1643 if (agent_by_event_notifier_domain_ht_create()) {
1644 ERR("Failed to allocate per-event notifier domain agent hash table");
1645 retval = -1;
1646 goto stop_threads;
1647 }
a88df331
DG
1648 /*
1649 * These actions must be executed as root. We do that *after* setting up
1650 * the sockets path because we MUST make the check for another daemon using
1651 * those paths *before* trying to set the kernel consumer sockets and init
1652 * kernel tracer.
1653 */
1654 if (is_root) {
412d7227 1655 if (set_consumer_sockets(&the_kconsumer_data)) {
4a15001e 1656 retval = -1;
0d64e8fb 1657 goto stop_threads;
7753dea8
MD
1658 }
1659
a88df331 1660 /* Setup kernel tracer */
412d7227 1661 if (!the_config.no_kernel) {
4fba7219
DG
1662 init_kernel_tracer();
1663 }
a88df331
DG
1664
1665 /* Set ulimit for open files */
1666 set_ulimit();
fac6795d 1667 }
4063050c
MD
1668 /* init lttng_fd tracking must be done after set_ulimit. */
1669 lttng_fd_init();
fac6795d 1670
412d7227 1671 if (set_consumer_sockets(&the_ustconsumer64_data)) {
4a15001e 1672 retval = -1;
0d64e8fb 1673 goto stop_threads;
67e40797
DG
1674 }
1675
412d7227 1676 if (set_consumer_sockets(&the_ustconsumer32_data)) {
4a15001e 1677 retval = -1;
0d64e8fb 1678 goto stop_threads;
67e40797
DG
1679 }
1680
5b8719f5 1681 /* Get parent pid if -S, --sig-parent is specified. */
412d7227
SM
1682 if (the_config.sig_parent) {
1683 the_ppid = getppid();
5b8719f5
DG
1684 }
1685
7a485870 1686 /* Setup the kernel pipe for waking up the kernel thread */
412d7227
SM
1687 if (is_root && !the_config.no_kernel) {
1688 if (utils_create_pipe_cloexec(the_kernel_poll_pipe)) {
4a15001e 1689 retval = -1;
0d64e8fb 1690 goto stop_threads;
6620da75 1691 }
7a485870
DG
1692 }
1693
099e26bd 1694 /* Setup the thread apps communication pipe. */
4a15001e
MD
1695 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
1696 retval = -1;
0d64e8fb 1697 goto stop_threads;
099e26bd
DG
1698 }
1699
d0b96690 1700 /* Setup the thread apps notify communication pipe. */
4a15001e
MD
1701 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
1702 retval = -1;
0d64e8fb 1703 goto stop_threads;
d0b96690
DG
1704 }
1705
7972aab2
DG
1706 /* Initialize global buffer per UID and PID registry. */
1707 buffer_reg_init_uid_registry();
1708 buffer_reg_init_pid_registry();
1709
099e26bd 1710 /* Init UST command queue. */
8bdee6e2 1711 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 1712
2f77fc4b 1713 cmd_init();
00e2e675 1714
ae9e45b3
DG
1715 /* Check for the application socket timeout env variable. */
1716 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
1717 if (env_app_timeout) {
412d7227 1718 the_config.app_socket_timeout = atoi(env_app_timeout);
ae9e45b3 1719 } else {
412d7227 1720 the_config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
ae9e45b3
DG
1721 }
1722
4a15001e
MD
1723 ret = write_pidfile();
1724 if (ret) {
1725 ERR("Error in write_pidfile");
1726 retval = -1;
0d64e8fb 1727 goto stop_threads;
4a15001e 1728 }
35f90c40 1729
554831e7
MD
1730 /* Initialize communication library */
1731 lttcomm_init();
4a15001e 1732 /* Initialize TCP timeout values */
d831c249 1733 lttcomm_inet_init();
554831e7 1734
12b4fb37 1735 /* Create health-check thread. */
173fca4f 1736 if (!launch_health_management_thread()) {
4a15001e 1737 retval = -1;
0d64e8fb 1738 goto stop_threads;
44a5e5eb
DG
1739 }
1740
b3530820 1741 /* notification_thread_data acquires the pipes' read side. */
412d7227
SM
1742 the_notification_thread_handle = notification_thread_handle_create(
1743 ust32_channel_monitor_pipe, ust64_channel_monitor_pipe,
c8a9de5a 1744 kernel_channel_monitor_pipe);
412d7227 1745 if (!the_notification_thread_handle) {
b3530820
JG
1746 retval = -1;
1747 ERR("Failed to create notification thread shared data");
0d64e8fb 1748 goto stop_threads;
b3530820
JG
1749 }
1750
1751 /* Create notification thread. */
4a91420c 1752 notification_thread = launch_notification_thread(
412d7227 1753 the_notification_thread_handle);
4a91420c 1754 if (!notification_thread) {
b3530820 1755 retval = -1;
0d64e8fb 1756 goto stop_threads;
b3530820
JG
1757 }
1758
d086f507 1759 /* Create timer thread. */
bc26e826 1760 if (!launch_timer_thread(&timer_thread_parameters)) {
d086f507 1761 retval = -1;
0d64e8fb 1762 goto stop_threads;
d086f507 1763 }
d086f507 1764
db66e574
JD
1765 /* rotation_thread_data acquires the pipes' read side. */
1766 rotation_thread_handle = rotation_thread_handle_create(
412d7227 1767 rotation_timer_queue, the_notification_thread_handle);
db66e574
JD
1768 if (!rotation_thread_handle) {
1769 retval = -1;
1770 ERR("Failed to create rotation thread shared data");
1771 stop_threads();
0d64e8fb 1772 goto stop_threads;
db66e574 1773 }
db66e574
JD
1774
1775 /* Create rotation thread. */
64d9b072 1776 if (!launch_rotation_thread(rotation_thread_handle)) {
db66e574 1777 retval = -1;
0d64e8fb 1778 goto stop_threads;
db66e574
JD
1779 }
1780
cf3af59e 1781 /* Create thread to manage the client socket */
917a718d
JG
1782 client_thread = launch_client_thread();
1783 if (!client_thread) {
4a15001e 1784 retval = -1;
0d64e8fb 1785 goto stop_threads;
cf3af59e 1786 }
fac6795d 1787
308df7bb 1788 /* Set credentials of the client socket and rundir */
412d7227 1789 if (is_root && set_permissions(the_config.rundir.value)) {
308df7bb
JG
1790 retval = -1;
1791 goto stop_threads;
1792 }
1793
5d1b0219
JG
1794 if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1],
1795 apps_cmd_notify_pipe[1])) {
4a15001e 1796 retval = -1;
0d64e8fb 1797 goto stop_threads;
099e26bd
DG
1798 }
1799
1800 /* Create thread to manage application registration. */
bd9addf7
JG
1801 register_apps_thread = launch_application_registration_thread(
1802 &ust_cmd_queue);
1803 if (!register_apps_thread) {
4a15001e 1804 retval = -1;
0d64e8fb 1805 goto stop_threads;
099e26bd
DG
1806 }
1807
cf3af59e 1808 /* Create thread to manage application socket */
7649924e 1809 if (!launch_application_management_thread(apps_cmd_pipe[0])) {
4a15001e 1810 retval = -1;
0d64e8fb 1811 goto stop_threads;
d0b96690
DG
1812 }
1813
1814 /* Create thread to manage application notify socket */
971a61c6 1815 if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
4a15001e 1816 retval = -1;
0d64e8fb 1817 goto stop_threads;
cf3af59e 1818 }
fac6795d 1819
8a7e4590
JG
1820 /* Create agent management thread. */
1821 if (!launch_agent_management_thread()) {
4a15001e 1822 retval = -1;
0d64e8fb 1823 goto stop_threads;
4d076222
DG
1824 }
1825
6620da75 1826 /* Don't start this thread if kernel tracing is not requested nor root */
412d7227 1827 if (is_root && !the_config.no_kernel) {
6620da75 1828 /* Create kernel thread to manage kernel event */
412d7227 1829 if (!launch_kernel_management_thread(the_kernel_poll_pipe[0])) {
4a15001e 1830 retval = -1;
0d64e8fb 1831 goto stop_threads;
6620da75 1832 }
352b58f5
JR
1833
1834 if (kernel_get_notification_fd() >= 0) {
1835 ret = notification_thread_command_add_tracer_event_source(
412d7227 1836 the_notification_thread_handle,
352b58f5
JR
1837 kernel_get_notification_fd(),
1838 LTTNG_DOMAIN_KERNEL);
1839 if (ret != LTTNG_OK) {
1840 ERR("Failed to add kernel trigger event source to notification thread");
1841 retval = -1;
1842 goto stop_threads;
1843 }
1844 }
ef367a93 1845 }
7a485870 1846
3316b9d6 1847 /* Load sessions. */
412d7227
SM
1848 ret = config_load_session(
1849 the_config.load_session_path.value, NULL, 1, 1, NULL);
4a15001e 1850 if (ret) {
3316b9d6 1851 ERR("Session load failed: %s", error_get_str(ret));
4a15001e 1852 retval = -1;
0d64e8fb 1853 goto stop_threads;
4a15001e
MD
1854 }
1855
3316b9d6
JG
1856 /* Initialization completed. */
1857 sessiond_signal_parents();
1858
4a15001e
MD
1859 /*
1860 * This is where we start awaiting program completion (e.g. through
1861 * signal that asks threads to teardown).
1862 */
1863
99d688f2 1864 /* Initiate teardown once activity occurs on the quit pipe. */
2d54bfb6 1865 sessiond_wait_for_quit_pipe(-1);
917a718d 1866
0d64e8fb 1867stop_threads:
90aa04a1 1868
917a718d
JG
1869 /*
1870 * Ensure that the client thread is no longer accepting new commands,
1871 * which could cause new sessions to be created.
1872 */
0d64e8fb
JG
1873 if (client_thread) {
1874 lttng_thread_shutdown(client_thread);
917a718d 1875 lttng_thread_put(client_thread);
917a718d
JG
1876 }
1877
99d688f2 1878 destroy_all_sessions_and_wait();
0d64e8fb 1879
b69a1b40
JG
1880 /*
1881 * At this point no new trigger can be registered (no sessions are
1882 * running/rotating) and clients can't connect to the session daemon
1883 * anymore. Unregister all triggers.
1884 */
1885 unregister_all_triggers();
1886
bd9addf7
JG
1887 if (register_apps_thread) {
1888 lttng_thread_shutdown(register_apps_thread);
1889 lttng_thread_put(register_apps_thread);
1890 }
a3707772 1891 lttng_thread_list_shutdown_orphans();
917a718d 1892
4d62fbf8
MD
1893 /*
1894 * Wait for all pending call_rcu work to complete before tearing
1895 * down data structures. call_rcu worker may be trying to
1896 * perform lookups in those structures.
1897 */
1898 rcu_barrier();
4a15001e
MD
1899 /*
1900 * sessiond_cleanup() is called when no other thread is running, except
1901 * the ht_cleanup thread, which is needed to destroy the hash tables.
1902 */
1903 rcu_thread_online();
1904 sessiond_cleanup();
06f525de 1905
3b8a092f 1906 /*
1d25334c 1907 * Wait for all pending call_rcu work to complete before shutting down
3b8a092f
FD
1908 * the notification thread. This call_rcu work includes shutting down
1909 * UST apps and event notifier pipes.
1910 */
1911 rcu_barrier();
1912
4a91420c
JG
1913 if (notification_thread) {
1914 lttng_thread_shutdown(notification_thread);
1915 lttng_thread_put(notification_thread);
1916 }
1917
90aa04a1 1918 /*
1d25334c
FD
1919 * Error accounting teardown has to be done after the teardown of all
1920 * event notifier pipes to ensure that no tracer may try to use the
1921 * error accounting facilities.
90aa04a1
FD
1922 */
1923 event_notifier_error_accounting_fini();
1924
1d25334c
FD
1925 /*
1926 * Unloading the kernel modules needs to be done after all kernel
1927 * ressources have been released. In our case, this includes the
1928 * notification fd, the event notifier group fd, error accounting fd,
1929 * all event and event notifier fds, etc.
1930 *
1931 * In short, at this point, we need to have called close() on all fds
1932 * received from the kernel tracer.
1933 */
412d7227 1934 if (is_root && !the_config.no_kernel) {
1d25334c
FD
1935 DBG("Unloading kernel modules");
1936 modprobe_remove_lttng_all();
1937 }
1938
c4d5de9d
JG
1939 /*
1940 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1941 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1942 * the queue is empty before shutting down the clean-up thread.
1943 */
1944 rcu_barrier();
1945
a3707772
JG
1946 if (ht_cleanup_thread) {
1947 lttng_thread_shutdown(ht_cleanup_thread);
1948 lttng_thread_put(ht_cleanup_thread);
1949 }
1950
64d9b072
JG
1951 rcu_thread_offline();
1952 rcu_unregister_thread();
1953
1954 if (rotation_thread_handle) {
1955 rotation_thread_handle_destroy(rotation_thread_handle);
1956 }
1957
d086f507
JD
1958 /*
1959 * After the rotation and timer thread have quit, we can safely destroy
1960 * the rotation_timer_queue.
1961 */
92816cc3 1962 rotation_thread_timer_queue_destroy(rotation_timer_queue);
c8a9de5a
JG
1963 /*
1964 * The teardown of the notification system is performed after the
1965 * session daemon's teardown in order to allow it to be notified
1966 * of the active session and channels at the moment of the teardown.
1967 */
412d7227
SM
1968 if (the_notification_thread_handle) {
1969 notification_thread_handle_destroy(
1970 the_notification_thread_handle);
c8a9de5a 1971 }
b3530820
JG
1972 lttng_pipe_destroy(ust32_channel_monitor_pipe);
1973 lttng_pipe_destroy(ust64_channel_monitor_pipe);
1974 lttng_pipe_destroy(kernel_channel_monitor_pipe);
4a15001e 1975
412d7227
SM
1976 if (the_health_sessiond) {
1977 health_app_destroy(the_health_sessiond);
9bc2ec5f 1978 }
7567352f 1979exit_create_run_as_worker_cleanup:
4a15001e 1980exit_options:
f472090a 1981 sessiond_cleanup_lock_file();
4a15001e
MD
1982 sessiond_cleanup_options();
1983
1984exit_set_signal_handler:
1985 if (!retval) {
cf3af59e 1986 exit(EXIT_SUCCESS);
4a15001e
MD
1987 } else {
1988 exit(EXIT_FAILURE);
67e40797 1989 }
fac6795d 1990}
This page took 0.371531 seconds and 4 git commands to generate.