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