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