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