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