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