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