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