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