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