Fix: sessiond: leak of config_path on duplicate --config option
[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 "lttng-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 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 free(config_path);
825 config_path = utils_expand_path(optarg);
826 if (!config_path) {
827 ERR("Failed to resolve path: %s", optarg);
828 }
829 }
830 }
831
832 ret = config_get_section_entries(config_path, config_section_name,
833 config_entry_handler, NULL);
834 if (ret) {
835 if (ret > 0) {
836 ERR("Invalid configuration option at line %i", ret);
837 ret = -1;
838 }
839 goto end;
840 }
841
842 /* Reset getopt's global state */
843 optopt = orig_optopt;
844 optind = orig_optind;
845 while (1) {
846 option_index = -1;
847 /*
848 * getopt_long() will not set option_index if it encounters a
849 * short option.
850 */
851 c = getopt_long(argc, argv, optstring, long_options,
852 &option_index);
853 if (c == -1) {
854 break;
855 }
856
857 /*
858 * Pass NULL as the long option name if popt left the index
859 * unset.
860 */
861 ret = set_option(c, optarg,
862 option_index < 0 ? NULL :
863 long_options[option_index].name);
864 if (ret < 0) {
865 break;
866 }
867 }
868
869 end:
870 free(config_path);
871 free(optstring);
872 return ret;
873 }
874
875 /*
876 * Create lockfile using the rundir and return its fd.
877 */
878 static int create_lockfile(void)
879 {
880 return utils_create_lock_file(config.lock_file_path.value);
881 }
882
883 /*
884 * Check if the global socket is available, and if a daemon is answering at the
885 * other side. If yes, error is returned.
886 *
887 * Also attempts to create and hold the lock file.
888 */
889 static int check_existing_daemon(void)
890 {
891 int ret = 0;
892
893 /* Is there anybody out there ? */
894 if (lttng_session_daemon_alive()) {
895 ret = -EEXIST;
896 goto end;
897 }
898
899 lockfile_fd = create_lockfile();
900 if (lockfile_fd < 0) {
901 ret = -EEXIST;
902 goto end;
903 }
904 end:
905 return ret;
906 }
907
908 static void sessiond_cleanup_lock_file(void)
909 {
910 int ret;
911
912 /*
913 * Cleanup lock file by deleting it and finaly closing it which will
914 * release the file system lock.
915 */
916 if (lockfile_fd >= 0) {
917 ret = remove(config.lock_file_path.value);
918 if (ret < 0) {
919 PERROR("remove lock file");
920 }
921 ret = close(lockfile_fd);
922 if (ret < 0) {
923 PERROR("close lock file");
924 }
925 }
926 }
927
928 /*
929 * Set the tracing group gid onto the client socket.
930 *
931 * Race window between mkdir and chown is OK because we are going from more
932 * permissive (root.root) to less permissive (root.tracing).
933 */
934 static int set_permissions(char *rundir)
935 {
936 int ret;
937 gid_t gid;
938
939 ret = utils_get_group_id(config.tracing_group_name.value, true, &gid);
940 if (ret) {
941 /* Default to root group. */
942 gid = 0;
943 }
944
945 /* Set lttng run dir */
946 ret = chown(rundir, 0, gid);
947 if (ret < 0) {
948 ERR("Unable to set group on %s", rundir);
949 PERROR("chown");
950 }
951
952 /*
953 * Ensure all applications and tracing group can search the run
954 * dir. Allow everyone to read the directory, since it does not
955 * buy us anything to hide its content.
956 */
957 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
958 if (ret < 0) {
959 ERR("Unable to set permissions on %s", rundir);
960 PERROR("chmod");
961 }
962
963 /* lttng client socket path */
964 ret = chown(config.client_unix_sock_path.value, 0, gid);
965 if (ret < 0) {
966 ERR("Unable to set group on %s", config.client_unix_sock_path.value);
967 PERROR("chown");
968 }
969
970 /* kconsumer error socket path */
971 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
972 if (ret < 0) {
973 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
974 PERROR("chown");
975 }
976
977 /* 64-bit ustconsumer error socket path */
978 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
979 if (ret < 0) {
980 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
981 PERROR("chown");
982 }
983
984 /* 32-bit ustconsumer compat32 error socket path */
985 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
986 if (ret < 0) {
987 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
988 PERROR("chown");
989 }
990
991 DBG("All permissions are set");
992
993 return ret;
994 }
995
996 /*
997 * Create the lttng run directory needed for all global sockets and pipe.
998 */
999 static int create_lttng_rundir(void)
1000 {
1001 int ret;
1002
1003 DBG3("Creating LTTng run directory: %s", config.rundir.value);
1004
1005 ret = mkdir(config.rundir.value, S_IRWXU);
1006 if (ret < 0) {
1007 if (errno != EEXIST) {
1008 ERR("Unable to create %s", config.rundir.value);
1009 goto error;
1010 } else {
1011 ret = 0;
1012 }
1013 }
1014
1015 error:
1016 return ret;
1017 }
1018
1019 /*
1020 * Setup sockets and directory needed by the consumerds' communication with the
1021 * session daemon.
1022 */
1023 static int set_consumer_sockets(struct consumer_data *consumer_data)
1024 {
1025 int ret;
1026 char *path = NULL;
1027
1028 switch (consumer_data->type) {
1029 case LTTNG_CONSUMER_KERNEL:
1030 path = config.kconsumerd_path.value;
1031 break;
1032 case LTTNG_CONSUMER64_UST:
1033 path = config.consumerd64_path.value;
1034 break;
1035 case LTTNG_CONSUMER32_UST:
1036 path = config.consumerd32_path.value;
1037 break;
1038 default:
1039 ERR("Consumer type unknown");
1040 ret = -EINVAL;
1041 goto error;
1042 }
1043 assert(path);
1044
1045 DBG2("Creating consumer directory: %s", path);
1046
1047 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
1048 if (ret < 0 && errno != EEXIST) {
1049 PERROR("mkdir");
1050 ERR("Failed to create %s", path);
1051 goto error;
1052 }
1053 if (is_root) {
1054 gid_t gid;
1055
1056 ret = utils_get_group_id(config.tracing_group_name.value, true,
1057 &gid);
1058 if (ret) {
1059 /* Default to root group. */
1060 gid = 0;
1061 }
1062
1063 ret = chown(path, 0, gid);
1064 if (ret < 0) {
1065 ERR("Unable to set group on %s", path);
1066 PERROR("chown");
1067 goto error;
1068 }
1069 }
1070
1071 /* Create the consumerd error unix socket */
1072 consumer_data->err_sock =
1073 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
1074 if (consumer_data->err_sock < 0) {
1075 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
1076 ret = -1;
1077 goto error;
1078 }
1079
1080 /*
1081 * Set the CLOEXEC flag. Return code is useless because either way, the
1082 * show must go on.
1083 */
1084 ret = utils_set_fd_cloexec(consumer_data->err_sock);
1085 if (ret < 0) {
1086 PERROR("utils_set_fd_cloexec");
1087 /* continue anyway */
1088 }
1089
1090 /* File permission MUST be 660 */
1091 ret = chmod(consumer_data->err_unix_sock_path,
1092 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1093 if (ret < 0) {
1094 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
1095 PERROR("chmod");
1096 goto error;
1097 }
1098
1099 error:
1100 return ret;
1101 }
1102
1103 /*
1104 * Signal handler for the daemon
1105 *
1106 * Simply stop all worker threads, leaving main() return gracefully after
1107 * joining all threads and calling cleanup().
1108 */
1109 static void sighandler(int sig)
1110 {
1111 switch (sig) {
1112 case SIGINT:
1113 DBG("SIGINT caught");
1114 stop_threads();
1115 break;
1116 case SIGTERM:
1117 DBG("SIGTERM caught");
1118 stop_threads();
1119 break;
1120 case SIGUSR1:
1121 CMM_STORE_SHARED(recv_child_signal, 1);
1122 break;
1123 default:
1124 break;
1125 }
1126 }
1127
1128 /*
1129 * Setup signal handler for :
1130 * SIGINT, SIGTERM, SIGPIPE
1131 */
1132 static int set_signal_handler(void)
1133 {
1134 int ret = 0;
1135 struct sigaction sa;
1136 sigset_t sigset;
1137
1138 if ((ret = sigemptyset(&sigset)) < 0) {
1139 PERROR("sigemptyset");
1140 return ret;
1141 }
1142
1143 sa.sa_mask = sigset;
1144 sa.sa_flags = 0;
1145
1146 sa.sa_handler = sighandler;
1147 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1148 PERROR("sigaction");
1149 return ret;
1150 }
1151
1152 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1153 PERROR("sigaction");
1154 return ret;
1155 }
1156
1157 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
1158 PERROR("sigaction");
1159 return ret;
1160 }
1161
1162 sa.sa_handler = SIG_IGN;
1163 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
1164 PERROR("sigaction");
1165 return ret;
1166 }
1167
1168 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1169
1170 return ret;
1171 }
1172
1173 /*
1174 * Set open files limit to unlimited. This daemon can open a large number of
1175 * file descriptors in order to consume multiple kernel traces.
1176 */
1177 static void set_ulimit(void)
1178 {
1179 int ret;
1180 struct rlimit lim;
1181
1182 /* The kernel does not allow an infinite limit for open files */
1183 lim.rlim_cur = 65535;
1184 lim.rlim_max = 65535;
1185
1186 ret = setrlimit(RLIMIT_NOFILE, &lim);
1187 if (ret < 0) {
1188 PERROR("failed to set open files limit");
1189 }
1190 }
1191
1192 static int write_pidfile(void)
1193 {
1194 return utils_create_pid_file(getpid(), config.pid_file_path.value);
1195 }
1196
1197 static int set_clock_plugin_env(void)
1198 {
1199 int ret = 0;
1200 char *env_value = NULL;
1201
1202 if (!config.lttng_ust_clock_plugin.value) {
1203 goto end;
1204 }
1205
1206 ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s",
1207 config.lttng_ust_clock_plugin.value);
1208 if (ret < 0) {
1209 PERROR("asprintf");
1210 goto end;
1211 }
1212
1213 ret = putenv(env_value);
1214 if (ret) {
1215 free(env_value);
1216 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1217 goto end;
1218 }
1219
1220 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1221 config.lttng_ust_clock_plugin.value);
1222 end:
1223 return ret;
1224 }
1225
1226 static void destroy_all_sessions_and_wait(void)
1227 {
1228 struct ltt_session *session, *tmp;
1229 struct ltt_session_list *session_list;
1230
1231 session_list = session_get_list();
1232 DBG("Initiating destruction of all sessions");
1233
1234 if (!session_list) {
1235 return;
1236 }
1237
1238 session_lock_list();
1239 /* Initiate the destruction of all sessions. */
1240 cds_list_for_each_entry_safe(session, tmp,
1241 &session_list->head, list) {
1242 if (!session_get(session)) {
1243 continue;
1244 }
1245
1246 session_lock(session);
1247 if (session->destroyed) {
1248 goto unlock_session;
1249 }
1250 (void) cmd_stop_trace(session);
1251 (void) cmd_destroy_session(session, notification_thread_handle,
1252 NULL);
1253 unlock_session:
1254 session_unlock(session);
1255 session_put(session);
1256 }
1257 session_unlock_list();
1258
1259 /* Wait for the destruction of all sessions to complete. */
1260 DBG("Waiting for the destruction of all sessions to complete");
1261 session_list_wait_empty();
1262 DBG("Destruction of all sessions completed");
1263 }
1264
1265 static int run_as_worker_post_fork_cleanup(void *data)
1266 {
1267 struct sessiond_config *sessiond_config = data;
1268
1269 sessiond_config_fini(sessiond_config);
1270 return 0;
1271 }
1272
1273 static int launch_run_as_worker(const char *procname)
1274 {
1275 /*
1276 * Clean-up before forking the run-as worker. Any dynamically
1277 * allocated memory of which the worker is not aware will
1278 * be leaked as the process forks a run-as worker (and performs
1279 * no exec*()). The same would apply to any opened fd.
1280 */
1281 return run_as_create_worker(procname, run_as_worker_post_fork_cleanup,
1282 &config);
1283 }
1284
1285 static void sessiond_uuid_log(void)
1286 {
1287 char uuid_str[UUID_STR_LEN];
1288
1289 lttng_uuid_to_str(sessiond_uuid, uuid_str);
1290 DBG("Starting lttng-sessiond {%s}", uuid_str);
1291 }
1292
1293 /*
1294 * main
1295 */
1296 int main(int argc, char **argv)
1297 {
1298 int ret = 0, retval = 0;
1299 const char *env_app_timeout;
1300 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
1301 *ust64_channel_monitor_pipe = NULL,
1302 *kernel_channel_monitor_pipe = NULL;
1303 struct lttng_thread *ht_cleanup_thread = NULL;
1304 struct timer_thread_parameters timer_thread_parameters;
1305 /* Rotation thread handle. */
1306 struct rotation_thread_handle *rotation_thread_handle = NULL;
1307 /* Queue of rotation jobs populated by the sessiond-timer. */
1308 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
1309 struct lttng_thread *client_thread = NULL;
1310 struct lttng_thread *notification_thread = NULL;
1311 struct lttng_thread *register_apps_thread = NULL;
1312
1313 init_kernel_workarounds();
1314
1315 rcu_register_thread();
1316
1317 if (set_signal_handler()) {
1318 retval = -1;
1319 goto exit_set_signal_handler;
1320 }
1321
1322 if (timer_signal_init()) {
1323 retval = -1;
1324 goto exit_set_signal_handler;
1325 }
1326
1327 page_size = sysconf(_SC_PAGESIZE);
1328 if (page_size < 0) {
1329 PERROR("sysconf _SC_PAGESIZE");
1330 page_size = LONG_MAX;
1331 WARN("Fallback page size to %ld", page_size);
1332 }
1333
1334 ret = sessiond_config_init(&config);
1335 if (ret) {
1336 retval = -1;
1337 goto exit_set_signal_handler;
1338 }
1339
1340 /*
1341 * Init config from environment variables.
1342 * Command line option override env configuration per-doc. Do env first.
1343 */
1344 sessiond_config_apply_env_config(&config);
1345
1346 /*
1347 * Parse arguments and load the daemon configuration file.
1348 *
1349 * We have an exit_options exit path to free memory reserved by
1350 * set_options. This is needed because the rest of sessiond_cleanup()
1351 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1352 * depends on set_options.
1353 */
1354 progname = argv[0];
1355 if (set_options(argc, argv)) {
1356 retval = -1;
1357 goto exit_options;
1358 }
1359
1360 /*
1361 * Resolve all paths received as arguments, configuration option, or
1362 * through environment variable as absolute paths. This is necessary
1363 * since daemonizing causes the sessiond's current working directory
1364 * to '/'.
1365 */
1366 ret = sessiond_config_resolve_paths(&config);
1367 if (ret) {
1368 goto exit_options;
1369 }
1370
1371 /* Apply config. */
1372 lttng_opt_verbose = config.verbose;
1373 lttng_opt_quiet = config.quiet;
1374 kconsumer_data.err_unix_sock_path =
1375 config.kconsumerd_err_unix_sock_path.value;
1376 kconsumer_data.cmd_unix_sock_path =
1377 config.kconsumerd_cmd_unix_sock_path.value;
1378 ustconsumer32_data.err_unix_sock_path =
1379 config.consumerd32_err_unix_sock_path.value;
1380 ustconsumer32_data.cmd_unix_sock_path =
1381 config.consumerd32_cmd_unix_sock_path.value;
1382 ustconsumer64_data.err_unix_sock_path =
1383 config.consumerd64_err_unix_sock_path.value;
1384 ustconsumer64_data.cmd_unix_sock_path =
1385 config.consumerd64_cmd_unix_sock_path.value;
1386 set_clock_plugin_env();
1387
1388 sessiond_config_log(&config);
1389 sessiond_uuid_log();
1390
1391 if (create_lttng_rundir()) {
1392 retval = -1;
1393 goto exit_options;
1394 }
1395
1396 /* Abort launch if a session daemon is already running. */
1397 if (check_existing_daemon()) {
1398 ERR("A session daemon is already running.");
1399 retval = -1;
1400 goto exit_options;
1401 }
1402
1403 /* Daemonize */
1404 if (config.daemonize || config.background) {
1405 int i;
1406
1407 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
1408 !config.background);
1409 if (ret < 0) {
1410 retval = -1;
1411 goto exit_options;
1412 }
1413
1414 /*
1415 * We are in the child. Make sure all other file descriptors are
1416 * closed, in case we are called with more opened file
1417 * descriptors than the standard ones and the lock file.
1418 */
1419 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1420 if (i == lockfile_fd) {
1421 continue;
1422 }
1423 (void) close(i);
1424 }
1425 }
1426
1427 if (launch_run_as_worker(argv[0]) < 0) {
1428 goto exit_create_run_as_worker_cleanup;
1429 }
1430
1431 /*
1432 * Starting from here, we can create threads. This needs to be after
1433 * lttng_daemonize due to RCU.
1434 */
1435
1436 /*
1437 * Initialize the health check subsystem. This call should set the
1438 * appropriate time values.
1439 */
1440 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
1441 if (!health_sessiond) {
1442 PERROR("health_app_create error");
1443 retval = -1;
1444 goto stop_threads;
1445 }
1446
1447 /* Create thread to clean up RCU hash tables */
1448 ht_cleanup_thread = launch_ht_cleanup_thread();
1449 if (!ht_cleanup_thread) {
1450 retval = -1;
1451 goto stop_threads;
1452 }
1453
1454 /* Create thread quit pipe */
1455 if (sessiond_init_thread_quit_pipe()) {
1456 retval = -1;
1457 goto stop_threads;
1458 }
1459
1460 /* Check if daemon is UID = 0 */
1461 is_root = !getuid();
1462 if (is_root) {
1463 /* Create global run dir with root access */
1464
1465 kernel_channel_monitor_pipe = lttng_pipe_open(0);
1466 if (!kernel_channel_monitor_pipe) {
1467 ERR("Failed to create kernel consumer channel monitor pipe");
1468 retval = -1;
1469 goto stop_threads;
1470 }
1471 kconsumer_data.channel_monitor_pipe =
1472 lttng_pipe_release_writefd(
1473 kernel_channel_monitor_pipe);
1474 if (kconsumer_data.channel_monitor_pipe < 0) {
1475 retval = -1;
1476 goto stop_threads;
1477 }
1478 }
1479
1480 /* Set consumer initial state */
1481 kernel_consumerd_state = CONSUMER_STOPPED;
1482 ust_consumerd_state = CONSUMER_STOPPED;
1483
1484 ust32_channel_monitor_pipe = lttng_pipe_open(0);
1485 if (!ust32_channel_monitor_pipe) {
1486 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1487 retval = -1;
1488 goto stop_threads;
1489 }
1490 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1491 ust32_channel_monitor_pipe);
1492 if (ustconsumer32_data.channel_monitor_pipe < 0) {
1493 retval = -1;
1494 goto stop_threads;
1495 }
1496
1497 /*
1498 * The rotation_thread_timer_queue structure is shared between the
1499 * sessiond timer thread and the rotation thread. The main thread keeps
1500 * its ownership and destroys it when both threads have been joined.
1501 */
1502 rotation_timer_queue = rotation_thread_timer_queue_create();
1503 if (!rotation_timer_queue) {
1504 retval = -1;
1505 goto stop_threads;
1506 }
1507 timer_thread_parameters.rotation_thread_job_queue =
1508 rotation_timer_queue;
1509
1510 ust64_channel_monitor_pipe = lttng_pipe_open(0);
1511 if (!ust64_channel_monitor_pipe) {
1512 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1513 retval = -1;
1514 goto stop_threads;
1515 }
1516 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1517 ust64_channel_monitor_pipe);
1518 if (ustconsumer64_data.channel_monitor_pipe < 0) {
1519 retval = -1;
1520 goto stop_threads;
1521 }
1522
1523 /*
1524 * Init UST app hash table. Alloc hash table before this point since
1525 * cleanup() can get called after that point.
1526 */
1527 if (ust_app_ht_alloc()) {
1528 ERR("Failed to allocate UST app hash table");
1529 retval = -1;
1530 goto stop_threads;
1531 }
1532
1533 /*
1534 * Initialize agent app hash table. We allocate the hash table here
1535 * since cleanup() can get called after this point.
1536 */
1537 if (agent_app_ht_alloc()) {
1538 ERR("Failed to allocate Agent app hash table");
1539 retval = -1;
1540 goto stop_threads;
1541 }
1542
1543 /*
1544 * These actions must be executed as root. We do that *after* setting up
1545 * the sockets path because we MUST make the check for another daemon using
1546 * those paths *before* trying to set the kernel consumer sockets and init
1547 * kernel tracer.
1548 */
1549 if (is_root) {
1550 if (set_consumer_sockets(&kconsumer_data)) {
1551 retval = -1;
1552 goto stop_threads;
1553 }
1554
1555 /* Setup kernel tracer */
1556 if (!config.no_kernel) {
1557 init_kernel_tracer();
1558 }
1559
1560 /* Set ulimit for open files */
1561 set_ulimit();
1562 }
1563 /* init lttng_fd tracking must be done after set_ulimit. */
1564 lttng_fd_init();
1565
1566 if (set_consumer_sockets(&ustconsumer64_data)) {
1567 retval = -1;
1568 goto stop_threads;
1569 }
1570
1571 if (set_consumer_sockets(&ustconsumer32_data)) {
1572 retval = -1;
1573 goto stop_threads;
1574 }
1575
1576 /* Get parent pid if -S, --sig-parent is specified. */
1577 if (config.sig_parent) {
1578 ppid = getppid();
1579 }
1580
1581 /* Setup the kernel pipe for waking up the kernel thread */
1582 if (is_root && !config.no_kernel) {
1583 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
1584 retval = -1;
1585 goto stop_threads;
1586 }
1587 }
1588
1589 /* Setup the thread apps communication pipe. */
1590 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
1591 retval = -1;
1592 goto stop_threads;
1593 }
1594
1595 /* Setup the thread apps notify communication pipe. */
1596 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
1597 retval = -1;
1598 goto stop_threads;
1599 }
1600
1601 /* Initialize global buffer per UID and PID registry. */
1602 buffer_reg_init_uid_registry();
1603 buffer_reg_init_pid_registry();
1604
1605 /* Init UST command queue. */
1606 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1607
1608 cmd_init();
1609
1610 /* Check for the application socket timeout env variable. */
1611 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
1612 if (env_app_timeout) {
1613 config.app_socket_timeout = atoi(env_app_timeout);
1614 } else {
1615 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
1616 }
1617
1618 ret = write_pidfile();
1619 if (ret) {
1620 ERR("Error in write_pidfile");
1621 retval = -1;
1622 goto stop_threads;
1623 }
1624
1625 /* Initialize communication library */
1626 lttcomm_init();
1627 /* Initialize TCP timeout values */
1628 lttcomm_inet_init();
1629
1630 /* Create health-check thread. */
1631 if (!launch_health_management_thread()) {
1632 retval = -1;
1633 goto stop_threads;
1634 }
1635
1636 /* notification_thread_data acquires the pipes' read side. */
1637 notification_thread_handle = notification_thread_handle_create(
1638 ust32_channel_monitor_pipe,
1639 ust64_channel_monitor_pipe,
1640 kernel_channel_monitor_pipe);
1641 if (!notification_thread_handle) {
1642 retval = -1;
1643 ERR("Failed to create notification thread shared data");
1644 goto stop_threads;
1645 }
1646
1647 /* Create notification thread. */
1648 notification_thread = launch_notification_thread(
1649 notification_thread_handle);
1650 if (!notification_thread) {
1651 retval = -1;
1652 goto stop_threads;
1653 }
1654
1655 /* Create timer thread. */
1656 if (!launch_timer_thread(&timer_thread_parameters)) {
1657 retval = -1;
1658 goto stop_threads;
1659 }
1660
1661 /* rotation_thread_data acquires the pipes' read side. */
1662 rotation_thread_handle = rotation_thread_handle_create(
1663 rotation_timer_queue,
1664 notification_thread_handle);
1665 if (!rotation_thread_handle) {
1666 retval = -1;
1667 ERR("Failed to create rotation thread shared data");
1668 stop_threads();
1669 goto stop_threads;
1670 }
1671
1672 /* Create rotation thread. */
1673 if (!launch_rotation_thread(rotation_thread_handle)) {
1674 retval = -1;
1675 goto stop_threads;
1676 }
1677
1678 /* Create thread to manage the client socket */
1679 client_thread = launch_client_thread();
1680 if (!client_thread) {
1681 retval = -1;
1682 goto stop_threads;
1683 }
1684
1685 /* Set credentials of the client socket and rundir */
1686 if (is_root && set_permissions(config.rundir.value)) {
1687 retval = -1;
1688 goto stop_threads;
1689 }
1690
1691 if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1],
1692 apps_cmd_notify_pipe[1])) {
1693 retval = -1;
1694 goto stop_threads;
1695 }
1696
1697 /* Create thread to manage application registration. */
1698 register_apps_thread = launch_application_registration_thread(
1699 &ust_cmd_queue);
1700 if (!register_apps_thread) {
1701 retval = -1;
1702 goto stop_threads;
1703 }
1704
1705 /* Create thread to manage application socket */
1706 if (!launch_application_management_thread(apps_cmd_pipe[0])) {
1707 retval = -1;
1708 goto stop_threads;
1709 }
1710
1711 /* Create thread to manage application notify socket */
1712 if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
1713 retval = -1;
1714 goto stop_threads;
1715 }
1716
1717 /* Create agent management thread. */
1718 if (!launch_agent_management_thread()) {
1719 retval = -1;
1720 goto stop_threads;
1721 }
1722
1723 /* Don't start this thread if kernel tracing is not requested nor root */
1724 if (is_root && !config.no_kernel) {
1725 /* Create kernel thread to manage kernel event */
1726 if (!launch_kernel_management_thread(kernel_poll_pipe[0])) {
1727 retval = -1;
1728 goto stop_threads;
1729 }
1730 }
1731
1732 /* Load sessions. */
1733 ret = config_load_session(config.load_session_path.value,
1734 NULL, 1, 1, NULL);
1735 if (ret) {
1736 ERR("Session load failed: %s", error_get_str(ret));
1737 retval = -1;
1738 goto stop_threads;
1739 }
1740
1741 /* Initialization completed. */
1742 sessiond_signal_parents();
1743
1744 /*
1745 * This is where we start awaiting program completion (e.g. through
1746 * signal that asks threads to teardown).
1747 */
1748
1749 /* Initiate teardown once activity occurs on the quit pipe. */
1750 sessiond_wait_for_quit_pipe(-1);
1751
1752 stop_threads:
1753 /*
1754 * Ensure that the client thread is no longer accepting new commands,
1755 * which could cause new sessions to be created.
1756 */
1757 if (client_thread) {
1758 lttng_thread_shutdown(client_thread);
1759 lttng_thread_put(client_thread);
1760 }
1761
1762 destroy_all_sessions_and_wait();
1763
1764 if (register_apps_thread) {
1765 lttng_thread_shutdown(register_apps_thread);
1766 lttng_thread_put(register_apps_thread);
1767 }
1768 lttng_thread_list_shutdown_orphans();
1769
1770 /*
1771 * Wait for all pending call_rcu work to complete before tearing
1772 * down data structures. call_rcu worker may be trying to
1773 * perform lookups in those structures.
1774 */
1775 rcu_barrier();
1776 /*
1777 * sessiond_cleanup() is called when no other thread is running, except
1778 * the ht_cleanup thread, which is needed to destroy the hash tables.
1779 */
1780 rcu_thread_online();
1781 sessiond_cleanup();
1782
1783 if (notification_thread) {
1784 lttng_thread_shutdown(notification_thread);
1785 lttng_thread_put(notification_thread);
1786 }
1787
1788 /*
1789 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1790 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1791 * the queue is empty before shutting down the clean-up thread.
1792 */
1793 rcu_barrier();
1794
1795 if (ht_cleanup_thread) {
1796 lttng_thread_shutdown(ht_cleanup_thread);
1797 lttng_thread_put(ht_cleanup_thread);
1798 }
1799
1800 rcu_thread_offline();
1801 rcu_unregister_thread();
1802
1803 if (rotation_thread_handle) {
1804 rotation_thread_handle_destroy(rotation_thread_handle);
1805 }
1806
1807 /*
1808 * After the rotation and timer thread have quit, we can safely destroy
1809 * the rotation_timer_queue.
1810 */
1811 rotation_thread_timer_queue_destroy(rotation_timer_queue);
1812 /*
1813 * The teardown of the notification system is performed after the
1814 * session daemon's teardown in order to allow it to be notified
1815 * of the active session and channels at the moment of the teardown.
1816 */
1817 if (notification_thread_handle) {
1818 notification_thread_handle_destroy(notification_thread_handle);
1819 }
1820 lttng_pipe_destroy(ust32_channel_monitor_pipe);
1821 lttng_pipe_destroy(ust64_channel_monitor_pipe);
1822 lttng_pipe_destroy(kernel_channel_monitor_pipe);
1823
1824 if (health_sessiond) {
1825 health_app_destroy(health_sessiond);
1826 }
1827 exit_create_run_as_worker_cleanup:
1828 exit_options:
1829 sessiond_cleanup_lock_file();
1830 sessiond_cleanup_options();
1831
1832 exit_set_signal_handler:
1833 if (!retval) {
1834 exit(EXIT_SUCCESS);
1835 } else {
1836 exit(EXIT_FAILURE);
1837 }
1838 }
This page took 0.108954 seconds and 4 git commands to generate.