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