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