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