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