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