| 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 "modprobe.h" |
| 65 | #include "shm.h" |
| 66 | #include "ust-ctl.h" |
| 67 | #include "ust-consumer.h" |
| 68 | #include "utils.h" |
| 69 | #include "fd-limit.h" |
| 70 | #include "health-sessiond.h" |
| 71 | #include "testpoint.h" |
| 72 | #include "ust-thread.h" |
| 73 | #include "agent-thread.h" |
| 74 | #include "save.h" |
| 75 | #include "load-session-thread.h" |
| 76 | #include "notification-thread.h" |
| 77 | #include "notification-thread-commands.h" |
| 78 | #include "rotation-thread.h" |
| 79 | #include "lttng-syscall.h" |
| 80 | #include "agent.h" |
| 81 | #include "ht-cleanup.h" |
| 82 | #include "sessiond-config.h" |
| 83 | #include "timer.h" |
| 84 | #include "thread.h" |
| 85 | #include "client.h" |
| 86 | #include "dispatch.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 | |
| 99 | /* Set to 1 when a SIGUSR1 signal is received. */ |
| 100 | static int recv_child_signal; |
| 101 | |
| 102 | /* Command line options */ |
| 103 | static const struct option long_options[] = { |
| 104 | { "client-sock", required_argument, 0, 'c' }, |
| 105 | { "apps-sock", required_argument, 0, 'a' }, |
| 106 | { "kconsumerd-cmd-sock", required_argument, 0, '\0' }, |
| 107 | { "kconsumerd-err-sock", required_argument, 0, '\0' }, |
| 108 | { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' }, |
| 109 | { "ustconsumerd32-err-sock", required_argument, 0, '\0' }, |
| 110 | { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' }, |
| 111 | { "ustconsumerd64-err-sock", required_argument, 0, '\0' }, |
| 112 | { "consumerd32-path", required_argument, 0, '\0' }, |
| 113 | { "consumerd32-libdir", required_argument, 0, '\0' }, |
| 114 | { "consumerd64-path", required_argument, 0, '\0' }, |
| 115 | { "consumerd64-libdir", required_argument, 0, '\0' }, |
| 116 | { "daemonize", no_argument, 0, 'd' }, |
| 117 | { "background", no_argument, 0, 'b' }, |
| 118 | { "sig-parent", no_argument, 0, 'S' }, |
| 119 | { "help", no_argument, 0, 'h' }, |
| 120 | { "group", required_argument, 0, 'g' }, |
| 121 | { "version", no_argument, 0, 'V' }, |
| 122 | { "quiet", no_argument, 0, 'q' }, |
| 123 | { "verbose", no_argument, 0, 'v' }, |
| 124 | { "verbose-consumer", no_argument, 0, '\0' }, |
| 125 | { "no-kernel", no_argument, 0, '\0' }, |
| 126 | { "pidfile", required_argument, 0, 'p' }, |
| 127 | { "agent-tcp-port", required_argument, 0, '\0' }, |
| 128 | { "config", required_argument, 0, 'f' }, |
| 129 | { "load", required_argument, 0, 'l' }, |
| 130 | { "kmod-probes", required_argument, 0, '\0' }, |
| 131 | { "extra-kmod-probes", required_argument, 0, '\0' }, |
| 132 | { NULL, 0, 0, 0 } |
| 133 | }; |
| 134 | |
| 135 | /* Command line options to ignore from configuration file */ |
| 136 | static const char *config_ignore_options[] = { "help", "version", "config" }; |
| 137 | static int apps_sock = -1; |
| 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 | |
| 145 | /* Pthread, Mutexes and Semaphores */ |
| 146 | static pthread_t apps_thread; |
| 147 | static pthread_t apps_notify_thread; |
| 148 | static pthread_t reg_apps_thread; |
| 149 | static pthread_t kernel_thread; |
| 150 | static pthread_t agent_reg_thread; |
| 151 | static pthread_t load_session_thread; |
| 152 | |
| 153 | /* |
| 154 | * UST registration command queue. This queue is tied with a futex and uses a N |
| 155 | * wakers / 1 waiter implemented and detailed in futex.c/.h |
| 156 | * |
| 157 | * The thread_registration_apps and thread_dispatch_ust_registration uses this |
| 158 | * queue along with the wait/wake scheme. The thread_manage_apps receives down |
| 159 | * the line new application socket and monitors it for any I/O error or clean |
| 160 | * close that triggers an unregistration of the application. |
| 161 | */ |
| 162 | static struct ust_cmd_queue ust_cmd_queue; |
| 163 | |
| 164 | static const char *module_proc_lttng = "/proc/lttng"; |
| 165 | |
| 166 | /* Load session thread information to operate. */ |
| 167 | static struct load_session_thread_data *load_info; |
| 168 | |
| 169 | /* |
| 170 | * Section name to look for in the daemon configuration file. |
| 171 | */ |
| 172 | static const char * const config_section_name = "sessiond"; |
| 173 | |
| 174 | /* Am I root or not. Set to 1 if the daemon is running as root */ |
| 175 | static int is_root; |
| 176 | |
| 177 | /* |
| 178 | * Stop all threads by closing the thread quit pipe. |
| 179 | */ |
| 180 | static void stop_threads(void) |
| 181 | { |
| 182 | int ret; |
| 183 | |
| 184 | /* Stopping all threads */ |
| 185 | DBG("Terminating all threads"); |
| 186 | ret = sessiond_notify_quit_pipe(); |
| 187 | if (ret < 0) { |
| 188 | ERR("write error on thread quit pipe"); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Close every consumer sockets. |
| 194 | */ |
| 195 | static void close_consumer_sockets(void) |
| 196 | { |
| 197 | int ret; |
| 198 | |
| 199 | if (kconsumer_data.err_sock >= 0) { |
| 200 | ret = close(kconsumer_data.err_sock); |
| 201 | if (ret < 0) { |
| 202 | PERROR("kernel consumer err_sock close"); |
| 203 | } |
| 204 | } |
| 205 | if (ustconsumer32_data.err_sock >= 0) { |
| 206 | ret = close(ustconsumer32_data.err_sock); |
| 207 | if (ret < 0) { |
| 208 | PERROR("UST consumerd32 err_sock close"); |
| 209 | } |
| 210 | } |
| 211 | if (ustconsumer64_data.err_sock >= 0) { |
| 212 | ret = close(ustconsumer64_data.err_sock); |
| 213 | if (ret < 0) { |
| 214 | PERROR("UST consumerd64 err_sock close"); |
| 215 | } |
| 216 | } |
| 217 | if (kconsumer_data.cmd_sock >= 0) { |
| 218 | ret = close(kconsumer_data.cmd_sock); |
| 219 | if (ret < 0) { |
| 220 | PERROR("kernel consumer cmd_sock close"); |
| 221 | } |
| 222 | } |
| 223 | if (ustconsumer32_data.cmd_sock >= 0) { |
| 224 | ret = close(ustconsumer32_data.cmd_sock); |
| 225 | if (ret < 0) { |
| 226 | PERROR("UST consumerd32 cmd_sock close"); |
| 227 | } |
| 228 | } |
| 229 | if (ustconsumer64_data.cmd_sock >= 0) { |
| 230 | ret = close(ustconsumer64_data.cmd_sock); |
| 231 | if (ret < 0) { |
| 232 | PERROR("UST consumerd64 cmd_sock close"); |
| 233 | } |
| 234 | } |
| 235 | if (kconsumer_data.channel_monitor_pipe >= 0) { |
| 236 | ret = close(kconsumer_data.channel_monitor_pipe); |
| 237 | if (ret < 0) { |
| 238 | PERROR("kernel consumer channel monitor pipe close"); |
| 239 | } |
| 240 | } |
| 241 | if (ustconsumer32_data.channel_monitor_pipe >= 0) { |
| 242 | ret = close(ustconsumer32_data.channel_monitor_pipe); |
| 243 | if (ret < 0) { |
| 244 | PERROR("UST consumerd32 channel monitor pipe close"); |
| 245 | } |
| 246 | } |
| 247 | if (ustconsumer64_data.channel_monitor_pipe >= 0) { |
| 248 | ret = close(ustconsumer64_data.channel_monitor_pipe); |
| 249 | if (ret < 0) { |
| 250 | PERROR("UST consumerd64 channel monitor pipe close"); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Wait on consumer process termination. |
| 257 | * |
| 258 | * Need to be called with the consumer data lock held or from a context |
| 259 | * ensuring no concurrent access to data (e.g: cleanup). |
| 260 | */ |
| 261 | static void wait_consumer(struct consumer_data *consumer_data) |
| 262 | { |
| 263 | pid_t ret; |
| 264 | int status; |
| 265 | |
| 266 | if (consumer_data->pid <= 0) { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | DBG("Waiting for complete teardown of consumerd (PID: %d)", |
| 271 | consumer_data->pid); |
| 272 | ret = waitpid(consumer_data->pid, &status, 0); |
| 273 | if (ret == -1) { |
| 274 | PERROR("consumerd waitpid pid: %d", consumer_data->pid) |
| 275 | } else if (!WIFEXITED(status)) { |
| 276 | ERR("consumerd termination with error: %d", |
| 277 | WEXITSTATUS(ret)); |
| 278 | } |
| 279 | consumer_data->pid = 0; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Cleanup the session daemon's data structures. |
| 284 | */ |
| 285 | static void sessiond_cleanup(void) |
| 286 | { |
| 287 | int ret; |
| 288 | struct ltt_session_list *session_list = session_get_list(); |
| 289 | |
| 290 | DBG("Cleanup sessiond"); |
| 291 | |
| 292 | /* |
| 293 | * Close the thread quit pipe. It has already done its job, |
| 294 | * since we are now called. |
| 295 | */ |
| 296 | sessiond_close_quit_pipe(); |
| 297 | |
| 298 | ret = remove(config.pid_file_path.value); |
| 299 | if (ret < 0) { |
| 300 | PERROR("remove pidfile %s", config.pid_file_path.value); |
| 301 | } |
| 302 | |
| 303 | DBG("Removing sessiond and consumerd content of directory %s", |
| 304 | config.rundir.value); |
| 305 | |
| 306 | /* sessiond */ |
| 307 | DBG("Removing %s", config.pid_file_path.value); |
| 308 | (void) unlink(config.pid_file_path.value); |
| 309 | |
| 310 | DBG("Removing %s", config.agent_port_file_path.value); |
| 311 | (void) unlink(config.agent_port_file_path.value); |
| 312 | |
| 313 | /* kconsumerd */ |
| 314 | DBG("Removing %s", kconsumer_data.err_unix_sock_path); |
| 315 | (void) unlink(kconsumer_data.err_unix_sock_path); |
| 316 | |
| 317 | DBG("Removing directory %s", config.kconsumerd_path.value); |
| 318 | (void) rmdir(config.kconsumerd_path.value); |
| 319 | |
| 320 | /* ust consumerd 32 */ |
| 321 | DBG("Removing %s", config.consumerd32_err_unix_sock_path.value); |
| 322 | (void) unlink(config.consumerd32_err_unix_sock_path.value); |
| 323 | |
| 324 | DBG("Removing directory %s", config.consumerd32_path.value); |
| 325 | (void) rmdir(config.consumerd32_path.value); |
| 326 | |
| 327 | /* ust consumerd 64 */ |
| 328 | DBG("Removing %s", config.consumerd64_err_unix_sock_path.value); |
| 329 | (void) unlink(config.consumerd64_err_unix_sock_path.value); |
| 330 | |
| 331 | DBG("Removing directory %s", config.consumerd64_path.value); |
| 332 | (void) rmdir(config.consumerd64_path.value); |
| 333 | |
| 334 | pthread_mutex_destroy(&session_list->lock); |
| 335 | |
| 336 | wait_consumer(&kconsumer_data); |
| 337 | wait_consumer(&ustconsumer64_data); |
| 338 | wait_consumer(&ustconsumer32_data); |
| 339 | |
| 340 | DBG("Cleaning up all agent apps"); |
| 341 | agent_app_ht_clean(); |
| 342 | |
| 343 | DBG("Closing all UST sockets"); |
| 344 | ust_app_clean_list(); |
| 345 | buffer_reg_destroy_registries(); |
| 346 | |
| 347 | if (is_root && !config.no_kernel) { |
| 348 | DBG2("Closing kernel fd"); |
| 349 | if (kernel_tracer_fd >= 0) { |
| 350 | ret = close(kernel_tracer_fd); |
| 351 | if (ret) { |
| 352 | PERROR("close"); |
| 353 | } |
| 354 | } |
| 355 | DBG("Unloading kernel modules"); |
| 356 | modprobe_remove_lttng_all(); |
| 357 | free(syscall_table); |
| 358 | } |
| 359 | |
| 360 | close_consumer_sockets(); |
| 361 | |
| 362 | if (load_info) { |
| 363 | load_session_destroy_data(load_info); |
| 364 | free(load_info); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * We do NOT rmdir rundir because there are other processes |
| 369 | * using it, for instance lttng-relayd, which can start in |
| 370 | * parallel with this teardown. |
| 371 | */ |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * Cleanup the daemon's option data structures. |
| 376 | */ |
| 377 | static void sessiond_cleanup_options(void) |
| 378 | { |
| 379 | DBG("Cleaning up options"); |
| 380 | |
| 381 | sessiond_config_fini(&config); |
| 382 | |
| 383 | run_as_destroy_worker(); |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Notify UST applications using the shm mmap futex. |
| 388 | */ |
| 389 | static int notify_ust_apps(int active) |
| 390 | { |
| 391 | char *wait_shm_mmap; |
| 392 | |
| 393 | DBG("Notifying applications of session daemon state: %d", active); |
| 394 | |
| 395 | /* See shm.c for this call implying mmap, shm and futex calls */ |
| 396 | wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root); |
| 397 | if (wait_shm_mmap == NULL) { |
| 398 | goto error; |
| 399 | } |
| 400 | |
| 401 | /* Wake waiting process */ |
| 402 | futex_wait_update((int32_t *) wait_shm_mmap, active); |
| 403 | |
| 404 | /* Apps notified successfully */ |
| 405 | return 0; |
| 406 | |
| 407 | error: |
| 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Update the kernel poll set of all channel fd available over all tracing |
| 413 | * session. Add the wakeup pipe at the end of the set. |
| 414 | */ |
| 415 | static int update_kernel_poll(struct lttng_poll_event *events) |
| 416 | { |
| 417 | int ret; |
| 418 | struct ltt_kernel_channel *channel; |
| 419 | struct ltt_session *session; |
| 420 | const struct ltt_session_list *session_list = session_get_list(); |
| 421 | |
| 422 | DBG("Updating kernel poll set"); |
| 423 | |
| 424 | session_lock_list(); |
| 425 | cds_list_for_each_entry(session, &session_list->head, list) { |
| 426 | if (!session_get(session)) { |
| 427 | continue; |
| 428 | } |
| 429 | session_lock(session); |
| 430 | if (session->kernel_session == NULL) { |
| 431 | session_unlock(session); |
| 432 | session_put(session); |
| 433 | continue; |
| 434 | } |
| 435 | |
| 436 | cds_list_for_each_entry(channel, |
| 437 | &session->kernel_session->channel_list.head, list) { |
| 438 | /* Add channel fd to the kernel poll set */ |
| 439 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); |
| 440 | if (ret < 0) { |
| 441 | session_unlock(session); |
| 442 | session_put(session); |
| 443 | goto error; |
| 444 | } |
| 445 | DBG("Channel fd %d added to kernel set", channel->fd); |
| 446 | } |
| 447 | session_unlock(session); |
| 448 | } |
| 449 | session_unlock_list(); |
| 450 | |
| 451 | return 0; |
| 452 | |
| 453 | error: |
| 454 | session_unlock_list(); |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Find the channel fd from 'fd' over all tracing session. When found, check |
| 460 | * for new channel stream and send those stream fds to the kernel consumer. |
| 461 | * |
| 462 | * Useful for CPU hotplug feature. |
| 463 | */ |
| 464 | static int update_kernel_stream(int fd) |
| 465 | { |
| 466 | int ret = 0; |
| 467 | struct ltt_session *session; |
| 468 | struct ltt_kernel_session *ksess; |
| 469 | struct ltt_kernel_channel *channel; |
| 470 | const struct ltt_session_list *session_list = session_get_list(); |
| 471 | |
| 472 | DBG("Updating kernel streams for channel fd %d", fd); |
| 473 | |
| 474 | session_lock_list(); |
| 475 | cds_list_for_each_entry(session, &session_list->head, list) { |
| 476 | if (!session_get(session)) { |
| 477 | continue; |
| 478 | } |
| 479 | session_lock(session); |
| 480 | if (session->kernel_session == NULL) { |
| 481 | session_unlock(session); |
| 482 | session_put(session); |
| 483 | continue; |
| 484 | } |
| 485 | ksess = session->kernel_session; |
| 486 | |
| 487 | cds_list_for_each_entry(channel, |
| 488 | &ksess->channel_list.head, list) { |
| 489 | struct lttng_ht_iter iter; |
| 490 | struct consumer_socket *socket; |
| 491 | |
| 492 | if (channel->fd != fd) { |
| 493 | continue; |
| 494 | } |
| 495 | DBG("Channel found, updating kernel streams"); |
| 496 | ret = kernel_open_channel_stream(channel); |
| 497 | if (ret < 0) { |
| 498 | goto error; |
| 499 | } |
| 500 | /* Update the stream global counter */ |
| 501 | ksess->stream_count_global += ret; |
| 502 | |
| 503 | /* |
| 504 | * Have we already sent fds to the consumer? If yes, it |
| 505 | * means that tracing is started so it is safe to send |
| 506 | * our updated stream fds. |
| 507 | */ |
| 508 | if (ksess->consumer_fds_sent != 1 |
| 509 | || ksess->consumer == NULL) { |
| 510 | ret = -1; |
| 511 | goto error; |
| 512 | } |
| 513 | |
| 514 | rcu_read_lock(); |
| 515 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, |
| 516 | &iter.iter, socket, node.node) { |
| 517 | pthread_mutex_lock(socket->lock); |
| 518 | ret = kernel_consumer_send_channel_streams(socket, |
| 519 | channel, ksess, |
| 520 | session->output_traces ? 1 : 0); |
| 521 | pthread_mutex_unlock(socket->lock); |
| 522 | if (ret < 0) { |
| 523 | rcu_read_unlock(); |
| 524 | goto error; |
| 525 | } |
| 526 | } |
| 527 | rcu_read_unlock(); |
| 528 | } |
| 529 | session_unlock(session); |
| 530 | session_put(session); |
| 531 | } |
| 532 | session_unlock_list(); |
| 533 | return ret; |
| 534 | |
| 535 | error: |
| 536 | session_unlock(session); |
| 537 | session_put(session); |
| 538 | session_unlock_list(); |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * This thread manage event coming from the kernel. |
| 544 | * |
| 545 | * Features supported in this thread: |
| 546 | * -) CPU Hotplug |
| 547 | */ |
| 548 | static void *thread_manage_kernel(void *data) |
| 549 | { |
| 550 | int ret, i, pollfd, update_poll_flag = 1, err = -1; |
| 551 | uint32_t revents, nb_fd; |
| 552 | char tmp; |
| 553 | struct lttng_poll_event events; |
| 554 | |
| 555 | DBG("[thread] Thread manage kernel started"); |
| 556 | |
| 557 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL); |
| 558 | |
| 559 | /* |
| 560 | * This first step of the while is to clean this structure which could free |
| 561 | * non NULL pointers so initialize it before the loop. |
| 562 | */ |
| 563 | lttng_poll_init(&events); |
| 564 | |
| 565 | if (testpoint(sessiond_thread_manage_kernel)) { |
| 566 | goto error_testpoint; |
| 567 | } |
| 568 | |
| 569 | health_code_update(); |
| 570 | |
| 571 | if (testpoint(sessiond_thread_manage_kernel_before_loop)) { |
| 572 | goto error_testpoint; |
| 573 | } |
| 574 | |
| 575 | while (1) { |
| 576 | health_code_update(); |
| 577 | |
| 578 | if (update_poll_flag == 1) { |
| 579 | /* Clean events object. We are about to populate it again. */ |
| 580 | lttng_poll_clean(&events); |
| 581 | |
| 582 | ret = sessiond_set_thread_pollset(&events, 2); |
| 583 | if (ret < 0) { |
| 584 | goto error_poll_create; |
| 585 | } |
| 586 | |
| 587 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); |
| 588 | if (ret < 0) { |
| 589 | goto error; |
| 590 | } |
| 591 | |
| 592 | /* This will add the available kernel channel if any. */ |
| 593 | ret = update_kernel_poll(&events); |
| 594 | if (ret < 0) { |
| 595 | goto error; |
| 596 | } |
| 597 | update_poll_flag = 0; |
| 598 | } |
| 599 | |
| 600 | DBG("Thread kernel polling"); |
| 601 | |
| 602 | /* Poll infinite value of time */ |
| 603 | restart: |
| 604 | health_poll_entry(); |
| 605 | ret = lttng_poll_wait(&events, -1); |
| 606 | DBG("Thread kernel return from poll on %d fds", |
| 607 | LTTNG_POLL_GETNB(&events)); |
| 608 | health_poll_exit(); |
| 609 | if (ret < 0) { |
| 610 | /* |
| 611 | * Restart interrupted system call. |
| 612 | */ |
| 613 | if (errno == EINTR) { |
| 614 | goto restart; |
| 615 | } |
| 616 | goto error; |
| 617 | } else if (ret == 0) { |
| 618 | /* Should not happen since timeout is infinite */ |
| 619 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
| 620 | "This should not have happened! Continuing..."); |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | nb_fd = ret; |
| 625 | |
| 626 | for (i = 0; i < nb_fd; i++) { |
| 627 | /* Fetch once the poll data */ |
| 628 | revents = LTTNG_POLL_GETEV(&events, i); |
| 629 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 630 | |
| 631 | health_code_update(); |
| 632 | |
| 633 | if (!revents) { |
| 634 | /* No activity for this FD (poll implementation). */ |
| 635 | continue; |
| 636 | } |
| 637 | |
| 638 | /* Thread quit pipe has been closed. Killing thread. */ |
| 639 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 640 | if (ret) { |
| 641 | err = 0; |
| 642 | goto exit; |
| 643 | } |
| 644 | |
| 645 | /* Check for data on kernel pipe */ |
| 646 | if (revents & LPOLLIN) { |
| 647 | if (pollfd == kernel_poll_pipe[0]) { |
| 648 | (void) lttng_read(kernel_poll_pipe[0], |
| 649 | &tmp, 1); |
| 650 | /* |
| 651 | * Ret value is useless here, if this pipe gets any actions an |
| 652 | * update is required anyway. |
| 653 | */ |
| 654 | update_poll_flag = 1; |
| 655 | continue; |
| 656 | } else { |
| 657 | /* |
| 658 | * New CPU detected by the kernel. Adding kernel stream to |
| 659 | * kernel session and updating the kernel consumer |
| 660 | */ |
| 661 | ret = update_kernel_stream(pollfd); |
| 662 | if (ret < 0) { |
| 663 | continue; |
| 664 | } |
| 665 | break; |
| 666 | } |
| 667 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 668 | update_poll_flag = 1; |
| 669 | continue; |
| 670 | } else { |
| 671 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 672 | goto error; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | exit: |
| 678 | error: |
| 679 | lttng_poll_clean(&events); |
| 680 | error_poll_create: |
| 681 | error_testpoint: |
| 682 | utils_close_pipe(kernel_poll_pipe); |
| 683 | kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1; |
| 684 | if (err) { |
| 685 | health_error(); |
| 686 | ERR("Health error occurred in %s", __func__); |
| 687 | WARN("Kernel thread died unexpectedly. " |
| 688 | "Kernel tracing can continue but CPU hotplug is disabled."); |
| 689 | } |
| 690 | health_unregister(health_sessiond); |
| 691 | DBG("Kernel thread dying"); |
| 692 | return NULL; |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * Signal pthread condition of the consumer data that the thread. |
| 697 | */ |
| 698 | static void signal_consumer_condition(struct consumer_data *data, int state) |
| 699 | { |
| 700 | pthread_mutex_lock(&data->cond_mutex); |
| 701 | |
| 702 | /* |
| 703 | * The state is set before signaling. It can be any value, it's the waiter |
| 704 | * job to correctly interpret this condition variable associated to the |
| 705 | * consumer pthread_cond. |
| 706 | * |
| 707 | * A value of 0 means that the corresponding thread of the consumer data |
| 708 | * was not started. 1 indicates that the thread has started and is ready |
| 709 | * for action. A negative value means that there was an error during the |
| 710 | * thread bootstrap. |
| 711 | */ |
| 712 | data->consumer_thread_is_ready = state; |
| 713 | (void) pthread_cond_signal(&data->cond); |
| 714 | |
| 715 | pthread_mutex_unlock(&data->cond_mutex); |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * This thread manage the consumer error sent back to the session daemon. |
| 720 | */ |
| 721 | void *thread_manage_consumer(void *data) |
| 722 | { |
| 723 | int sock = -1, i, ret, pollfd, err = -1, should_quit = 0; |
| 724 | uint32_t revents, nb_fd; |
| 725 | enum lttcomm_return_code code; |
| 726 | struct lttng_poll_event events; |
| 727 | struct consumer_data *consumer_data = data; |
| 728 | struct consumer_socket *cmd_socket_wrapper = NULL; |
| 729 | |
| 730 | DBG("[thread] Manage consumer started"); |
| 731 | |
| 732 | rcu_register_thread(); |
| 733 | rcu_thread_online(); |
| 734 | |
| 735 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER); |
| 736 | |
| 737 | health_code_update(); |
| 738 | |
| 739 | /* |
| 740 | * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the |
| 741 | * metadata_sock. Nothing more will be added to this poll set. |
| 742 | */ |
| 743 | ret = sessiond_set_thread_pollset(&events, 3); |
| 744 | if (ret < 0) { |
| 745 | goto error_poll; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * The error socket here is already in a listening state which was done |
| 750 | * just before spawning this thread to avoid a race between the consumer |
| 751 | * daemon exec trying to connect and the listen() call. |
| 752 | */ |
| 753 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
| 754 | if (ret < 0) { |
| 755 | goto error; |
| 756 | } |
| 757 | |
| 758 | health_code_update(); |
| 759 | |
| 760 | /* Infinite blocking call, waiting for transmission */ |
| 761 | restart: |
| 762 | health_poll_entry(); |
| 763 | |
| 764 | if (testpoint(sessiond_thread_manage_consumer)) { |
| 765 | goto error; |
| 766 | } |
| 767 | |
| 768 | ret = lttng_poll_wait(&events, -1); |
| 769 | health_poll_exit(); |
| 770 | if (ret < 0) { |
| 771 | /* |
| 772 | * Restart interrupted system call. |
| 773 | */ |
| 774 | if (errno == EINTR) { |
| 775 | goto restart; |
| 776 | } |
| 777 | goto error; |
| 778 | } |
| 779 | |
| 780 | nb_fd = ret; |
| 781 | |
| 782 | for (i = 0; i < nb_fd; i++) { |
| 783 | /* Fetch once the poll data */ |
| 784 | revents = LTTNG_POLL_GETEV(&events, i); |
| 785 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 786 | |
| 787 | health_code_update(); |
| 788 | |
| 789 | if (!revents) { |
| 790 | /* No activity for this FD (poll implementation). */ |
| 791 | continue; |
| 792 | } |
| 793 | |
| 794 | /* Thread quit pipe has been closed. Killing thread. */ |
| 795 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 796 | if (ret) { |
| 797 | err = 0; |
| 798 | goto exit; |
| 799 | } |
| 800 | |
| 801 | /* Event on the registration socket */ |
| 802 | if (pollfd == consumer_data->err_sock) { |
| 803 | if (revents & LPOLLIN) { |
| 804 | continue; |
| 805 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 806 | ERR("consumer err socket poll error"); |
| 807 | goto error; |
| 808 | } else { |
| 809 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 810 | goto error; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
| 816 | if (sock < 0) { |
| 817 | goto error; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Set the CLOEXEC flag. Return code is useless because either way, the |
| 822 | * show must go on. |
| 823 | */ |
| 824 | (void) utils_set_fd_cloexec(sock); |
| 825 | |
| 826 | health_code_update(); |
| 827 | |
| 828 | DBG2("Receiving code from consumer err_sock"); |
| 829 | |
| 830 | /* Getting status code from kconsumerd */ |
| 831 | ret = lttcomm_recv_unix_sock(sock, &code, |
| 832 | sizeof(enum lttcomm_return_code)); |
| 833 | if (ret <= 0) { |
| 834 | goto error; |
| 835 | } |
| 836 | |
| 837 | health_code_update(); |
| 838 | if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) { |
| 839 | ERR("consumer error when waiting for SOCK_READY : %s", |
| 840 | lttcomm_get_readable_code(-code)); |
| 841 | goto error; |
| 842 | } |
| 843 | |
| 844 | /* Connect both command and metadata sockets. */ |
| 845 | consumer_data->cmd_sock = |
| 846 | lttcomm_connect_unix_sock( |
| 847 | consumer_data->cmd_unix_sock_path); |
| 848 | consumer_data->metadata_fd = |
| 849 | lttcomm_connect_unix_sock( |
| 850 | consumer_data->cmd_unix_sock_path); |
| 851 | if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) { |
| 852 | PERROR("consumer connect cmd socket"); |
| 853 | /* On error, signal condition and quit. */ |
| 854 | signal_consumer_condition(consumer_data, -1); |
| 855 | goto error; |
| 856 | } |
| 857 | |
| 858 | consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd; |
| 859 | |
| 860 | /* Create metadata socket lock. */ |
| 861 | consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t)); |
| 862 | if (consumer_data->metadata_sock.lock == NULL) { |
| 863 | PERROR("zmalloc pthread mutex"); |
| 864 | goto error; |
| 865 | } |
| 866 | pthread_mutex_init(consumer_data->metadata_sock.lock, NULL); |
| 867 | |
| 868 | DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock); |
| 869 | DBG("Consumer metadata socket ready (fd: %d)", |
| 870 | consumer_data->metadata_fd); |
| 871 | |
| 872 | /* |
| 873 | * Remove the consumerd error sock since we've established a connection. |
| 874 | */ |
| 875 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
| 876 | if (ret < 0) { |
| 877 | goto error; |
| 878 | } |
| 879 | |
| 880 | /* Add new accepted error socket. */ |
| 881 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
| 882 | if (ret < 0) { |
| 883 | goto error; |
| 884 | } |
| 885 | |
| 886 | /* Add metadata socket that is successfully connected. */ |
| 887 | ret = lttng_poll_add(&events, consumer_data->metadata_fd, |
| 888 | LPOLLIN | LPOLLRDHUP); |
| 889 | if (ret < 0) { |
| 890 | goto error; |
| 891 | } |
| 892 | |
| 893 | health_code_update(); |
| 894 | |
| 895 | /* |
| 896 | * Transfer the write-end of the channel monitoring and rotate pipe |
| 897 | * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command. |
| 898 | */ |
| 899 | cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock); |
| 900 | if (!cmd_socket_wrapper) { |
| 901 | goto error; |
| 902 | } |
| 903 | cmd_socket_wrapper->lock = &consumer_data->lock; |
| 904 | |
| 905 | ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper, |
| 906 | consumer_data->channel_monitor_pipe); |
| 907 | if (ret) { |
| 908 | goto error; |
| 909 | } |
| 910 | |
| 911 | /* Discard the socket wrapper as it is no longer needed. */ |
| 912 | consumer_destroy_socket(cmd_socket_wrapper); |
| 913 | cmd_socket_wrapper = NULL; |
| 914 | |
| 915 | /* The thread is completely initialized, signal that it is ready. */ |
| 916 | signal_consumer_condition(consumer_data, 1); |
| 917 | |
| 918 | /* Infinite blocking call, waiting for transmission */ |
| 919 | restart_poll: |
| 920 | while (1) { |
| 921 | health_code_update(); |
| 922 | |
| 923 | /* Exit the thread because the thread quit pipe has been triggered. */ |
| 924 | if (should_quit) { |
| 925 | /* Not a health error. */ |
| 926 | err = 0; |
| 927 | goto exit; |
| 928 | } |
| 929 | |
| 930 | health_poll_entry(); |
| 931 | ret = lttng_poll_wait(&events, -1); |
| 932 | health_poll_exit(); |
| 933 | if (ret < 0) { |
| 934 | /* |
| 935 | * Restart interrupted system call. |
| 936 | */ |
| 937 | if (errno == EINTR) { |
| 938 | goto restart_poll; |
| 939 | } |
| 940 | goto error; |
| 941 | } |
| 942 | |
| 943 | nb_fd = ret; |
| 944 | |
| 945 | for (i = 0; i < nb_fd; i++) { |
| 946 | /* Fetch once the poll data */ |
| 947 | revents = LTTNG_POLL_GETEV(&events, i); |
| 948 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 949 | |
| 950 | health_code_update(); |
| 951 | |
| 952 | if (!revents) { |
| 953 | /* No activity for this FD (poll implementation). */ |
| 954 | continue; |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * Thread quit pipe has been triggered, flag that we should stop |
| 959 | * but continue the current loop to handle potential data from |
| 960 | * consumer. |
| 961 | */ |
| 962 | should_quit = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 963 | |
| 964 | if (pollfd == sock) { |
| 965 | /* Event on the consumerd socket */ |
| 966 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
| 967 | && !(revents & LPOLLIN)) { |
| 968 | ERR("consumer err socket second poll error"); |
| 969 | goto error; |
| 970 | } |
| 971 | health_code_update(); |
| 972 | /* Wait for any kconsumerd error */ |
| 973 | ret = lttcomm_recv_unix_sock(sock, &code, |
| 974 | sizeof(enum lttcomm_return_code)); |
| 975 | if (ret <= 0) { |
| 976 | ERR("consumer closed the command socket"); |
| 977 | goto error; |
| 978 | } |
| 979 | |
| 980 | ERR("consumer return code : %s", |
| 981 | lttcomm_get_readable_code(-code)); |
| 982 | |
| 983 | goto exit; |
| 984 | } else if (pollfd == consumer_data->metadata_fd) { |
| 985 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP) |
| 986 | && !(revents & LPOLLIN)) { |
| 987 | ERR("consumer err metadata socket second poll error"); |
| 988 | goto error; |
| 989 | } |
| 990 | /* UST metadata requests */ |
| 991 | ret = ust_consumer_metadata_request( |
| 992 | &consumer_data->metadata_sock); |
| 993 | if (ret < 0) { |
| 994 | ERR("Handling metadata request"); |
| 995 | goto error; |
| 996 | } |
| 997 | } |
| 998 | /* No need for an else branch all FDs are tested prior. */ |
| 999 | } |
| 1000 | health_code_update(); |
| 1001 | } |
| 1002 | |
| 1003 | exit: |
| 1004 | error: |
| 1005 | /* |
| 1006 | * We lock here because we are about to close the sockets and some other |
| 1007 | * thread might be using them so get exclusive access which will abort all |
| 1008 | * other consumer command by other threads. |
| 1009 | */ |
| 1010 | pthread_mutex_lock(&consumer_data->lock); |
| 1011 | |
| 1012 | /* Immediately set the consumerd state to stopped */ |
| 1013 | if (consumer_data->type == LTTNG_CONSUMER_KERNEL) { |
| 1014 | uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR); |
| 1015 | } else if (consumer_data->type == LTTNG_CONSUMER64_UST || |
| 1016 | consumer_data->type == LTTNG_CONSUMER32_UST) { |
| 1017 | uatomic_set(&ust_consumerd_state, CONSUMER_ERROR); |
| 1018 | } else { |
| 1019 | /* Code flow error... */ |
| 1020 | assert(0); |
| 1021 | } |
| 1022 | |
| 1023 | if (consumer_data->err_sock >= 0) { |
| 1024 | ret = close(consumer_data->err_sock); |
| 1025 | if (ret) { |
| 1026 | PERROR("close"); |
| 1027 | } |
| 1028 | consumer_data->err_sock = -1; |
| 1029 | } |
| 1030 | if (consumer_data->cmd_sock >= 0) { |
| 1031 | ret = close(consumer_data->cmd_sock); |
| 1032 | if (ret) { |
| 1033 | PERROR("close"); |
| 1034 | } |
| 1035 | consumer_data->cmd_sock = -1; |
| 1036 | } |
| 1037 | if (consumer_data->metadata_sock.fd_ptr && |
| 1038 | *consumer_data->metadata_sock.fd_ptr >= 0) { |
| 1039 | ret = close(*consumer_data->metadata_sock.fd_ptr); |
| 1040 | if (ret) { |
| 1041 | PERROR("close"); |
| 1042 | } |
| 1043 | } |
| 1044 | if (sock >= 0) { |
| 1045 | ret = close(sock); |
| 1046 | if (ret) { |
| 1047 | PERROR("close"); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | unlink(consumer_data->err_unix_sock_path); |
| 1052 | unlink(consumer_data->cmd_unix_sock_path); |
| 1053 | pthread_mutex_unlock(&consumer_data->lock); |
| 1054 | |
| 1055 | /* Cleanup metadata socket mutex. */ |
| 1056 | if (consumer_data->metadata_sock.lock) { |
| 1057 | pthread_mutex_destroy(consumer_data->metadata_sock.lock); |
| 1058 | free(consumer_data->metadata_sock.lock); |
| 1059 | } |
| 1060 | lttng_poll_clean(&events); |
| 1061 | |
| 1062 | if (cmd_socket_wrapper) { |
| 1063 | consumer_destroy_socket(cmd_socket_wrapper); |
| 1064 | } |
| 1065 | error_poll: |
| 1066 | if (err) { |
| 1067 | health_error(); |
| 1068 | ERR("Health error occurred in %s", __func__); |
| 1069 | } |
| 1070 | health_unregister(health_sessiond); |
| 1071 | DBG("consumer thread cleanup completed"); |
| 1072 | |
| 1073 | rcu_thread_offline(); |
| 1074 | rcu_unregister_thread(); |
| 1075 | |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * This thread receives application command sockets (FDs) on the |
| 1081 | * apps_cmd_pipe and waits (polls) on them until they are closed |
| 1082 | * or an error occurs. |
| 1083 | * |
| 1084 | * At that point, it flushes the data (tracing and metadata) associated |
| 1085 | * with this application and tears down ust app sessions and other |
| 1086 | * associated data structures through ust_app_unregister(). |
| 1087 | * |
| 1088 | * Note that this thread never sends commands to the applications |
| 1089 | * through the command sockets; it merely listens for hang-ups |
| 1090 | * and errors on those sockets and cleans-up as they occur. |
| 1091 | */ |
| 1092 | static void *thread_manage_apps(void *data) |
| 1093 | { |
| 1094 | int i, ret, pollfd, err = -1; |
| 1095 | ssize_t size_ret; |
| 1096 | uint32_t revents, nb_fd; |
| 1097 | struct lttng_poll_event events; |
| 1098 | |
| 1099 | DBG("[thread] Manage application started"); |
| 1100 | |
| 1101 | rcu_register_thread(); |
| 1102 | rcu_thread_online(); |
| 1103 | |
| 1104 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE); |
| 1105 | |
| 1106 | if (testpoint(sessiond_thread_manage_apps)) { |
| 1107 | goto error_testpoint; |
| 1108 | } |
| 1109 | |
| 1110 | health_code_update(); |
| 1111 | |
| 1112 | ret = sessiond_set_thread_pollset(&events, 2); |
| 1113 | if (ret < 0) { |
| 1114 | goto error_poll_create; |
| 1115 | } |
| 1116 | |
| 1117 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
| 1118 | if (ret < 0) { |
| 1119 | goto error; |
| 1120 | } |
| 1121 | |
| 1122 | if (testpoint(sessiond_thread_manage_apps_before_loop)) { |
| 1123 | goto error; |
| 1124 | } |
| 1125 | |
| 1126 | health_code_update(); |
| 1127 | |
| 1128 | while (1) { |
| 1129 | DBG("Apps thread polling"); |
| 1130 | |
| 1131 | /* Inifinite blocking call, waiting for transmission */ |
| 1132 | restart: |
| 1133 | health_poll_entry(); |
| 1134 | ret = lttng_poll_wait(&events, -1); |
| 1135 | DBG("Apps thread return from poll on %d fds", |
| 1136 | LTTNG_POLL_GETNB(&events)); |
| 1137 | health_poll_exit(); |
| 1138 | if (ret < 0) { |
| 1139 | /* |
| 1140 | * Restart interrupted system call. |
| 1141 | */ |
| 1142 | if (errno == EINTR) { |
| 1143 | goto restart; |
| 1144 | } |
| 1145 | goto error; |
| 1146 | } |
| 1147 | |
| 1148 | nb_fd = ret; |
| 1149 | |
| 1150 | for (i = 0; i < nb_fd; i++) { |
| 1151 | /* Fetch once the poll data */ |
| 1152 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1153 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1154 | |
| 1155 | health_code_update(); |
| 1156 | |
| 1157 | if (!revents) { |
| 1158 | /* No activity for this FD (poll implementation). */ |
| 1159 | continue; |
| 1160 | } |
| 1161 | |
| 1162 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1163 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 1164 | if (ret) { |
| 1165 | err = 0; |
| 1166 | goto exit; |
| 1167 | } |
| 1168 | |
| 1169 | /* Inspect the apps cmd pipe */ |
| 1170 | if (pollfd == apps_cmd_pipe[0]) { |
| 1171 | if (revents & LPOLLIN) { |
| 1172 | int sock; |
| 1173 | |
| 1174 | /* Empty pipe */ |
| 1175 | size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock)); |
| 1176 | if (size_ret < sizeof(sock)) { |
| 1177 | PERROR("read apps cmd pipe"); |
| 1178 | goto error; |
| 1179 | } |
| 1180 | |
| 1181 | health_code_update(); |
| 1182 | |
| 1183 | /* |
| 1184 | * Since this is a command socket (write then read), |
| 1185 | * we only monitor the error events of the socket. |
| 1186 | */ |
| 1187 | ret = lttng_poll_add(&events, sock, |
| 1188 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); |
| 1189 | if (ret < 0) { |
| 1190 | goto error; |
| 1191 | } |
| 1192 | |
| 1193 | DBG("Apps with sock %d added to poll set", sock); |
| 1194 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1195 | ERR("Apps command pipe error"); |
| 1196 | goto error; |
| 1197 | } else { |
| 1198 | ERR("Unknown poll events %u for sock %d", revents, pollfd); |
| 1199 | goto error; |
| 1200 | } |
| 1201 | } else { |
| 1202 | /* |
| 1203 | * At this point, we know that a registered application made |
| 1204 | * the event at poll_wait. |
| 1205 | */ |
| 1206 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1207 | /* Removing from the poll set */ |
| 1208 | ret = lttng_poll_del(&events, pollfd); |
| 1209 | if (ret < 0) { |
| 1210 | goto error; |
| 1211 | } |
| 1212 | |
| 1213 | /* Socket closed on remote end. */ |
| 1214 | ust_app_unregister(pollfd); |
| 1215 | } else { |
| 1216 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 1217 | goto error; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | health_code_update(); |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | exit: |
| 1226 | error: |
| 1227 | lttng_poll_clean(&events); |
| 1228 | error_poll_create: |
| 1229 | error_testpoint: |
| 1230 | utils_close_pipe(apps_cmd_pipe); |
| 1231 | apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1; |
| 1232 | |
| 1233 | /* |
| 1234 | * We don't clean the UST app hash table here since already registered |
| 1235 | * applications can still be controlled so let them be until the session |
| 1236 | * daemon dies or the applications stop. |
| 1237 | */ |
| 1238 | |
| 1239 | if (err) { |
| 1240 | health_error(); |
| 1241 | ERR("Health error occurred in %s", __func__); |
| 1242 | } |
| 1243 | health_unregister(health_sessiond); |
| 1244 | DBG("Application communication apps thread cleanup complete"); |
| 1245 | rcu_thread_offline(); |
| 1246 | rcu_unregister_thread(); |
| 1247 | return NULL; |
| 1248 | } |
| 1249 | |
| 1250 | /* |
| 1251 | * This thread manage application registration. |
| 1252 | */ |
| 1253 | static void *thread_registration_apps(void *data) |
| 1254 | { |
| 1255 | int sock = -1, i, ret, pollfd, err = -1; |
| 1256 | uint32_t revents, nb_fd; |
| 1257 | struct lttng_poll_event events; |
| 1258 | /* |
| 1259 | * Get allocated in this thread, enqueued to a global queue, dequeued and |
| 1260 | * freed in the manage apps thread. |
| 1261 | */ |
| 1262 | struct ust_command *ust_cmd = NULL; |
| 1263 | |
| 1264 | DBG("[thread] Manage application registration started"); |
| 1265 | |
| 1266 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
| 1267 | |
| 1268 | if (testpoint(sessiond_thread_registration_apps)) { |
| 1269 | goto error_testpoint; |
| 1270 | } |
| 1271 | |
| 1272 | ret = lttcomm_listen_unix_sock(apps_sock); |
| 1273 | if (ret < 0) { |
| 1274 | goto error_listen; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing |
| 1279 | * more will be added to this poll set. |
| 1280 | */ |
| 1281 | ret = sessiond_set_thread_pollset(&events, 2); |
| 1282 | if (ret < 0) { |
| 1283 | goto error_create_poll; |
| 1284 | } |
| 1285 | |
| 1286 | /* Add the application registration socket */ |
| 1287 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); |
| 1288 | if (ret < 0) { |
| 1289 | goto error_poll_add; |
| 1290 | } |
| 1291 | |
| 1292 | /* Notify all applications to register */ |
| 1293 | ret = notify_ust_apps(1); |
| 1294 | if (ret < 0) { |
| 1295 | ERR("Failed to notify applications or create the wait shared memory.\n" |
| 1296 | "Execution continues but there might be problem for already\n" |
| 1297 | "running applications that wishes to register."); |
| 1298 | } |
| 1299 | |
| 1300 | while (1) { |
| 1301 | DBG("Accepting application registration"); |
| 1302 | |
| 1303 | /* Inifinite blocking call, waiting for transmission */ |
| 1304 | restart: |
| 1305 | health_poll_entry(); |
| 1306 | ret = lttng_poll_wait(&events, -1); |
| 1307 | health_poll_exit(); |
| 1308 | if (ret < 0) { |
| 1309 | /* |
| 1310 | * Restart interrupted system call. |
| 1311 | */ |
| 1312 | if (errno == EINTR) { |
| 1313 | goto restart; |
| 1314 | } |
| 1315 | goto error; |
| 1316 | } |
| 1317 | |
| 1318 | nb_fd = ret; |
| 1319 | |
| 1320 | for (i = 0; i < nb_fd; i++) { |
| 1321 | health_code_update(); |
| 1322 | |
| 1323 | /* Fetch once the poll data */ |
| 1324 | revents = LTTNG_POLL_GETEV(&events, i); |
| 1325 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1326 | |
| 1327 | if (!revents) { |
| 1328 | /* No activity for this FD (poll implementation). */ |
| 1329 | continue; |
| 1330 | } |
| 1331 | |
| 1332 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1333 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 1334 | if (ret) { |
| 1335 | err = 0; |
| 1336 | goto exit; |
| 1337 | } |
| 1338 | |
| 1339 | /* Event on the registration socket */ |
| 1340 | if (pollfd == apps_sock) { |
| 1341 | if (revents & LPOLLIN) { |
| 1342 | sock = lttcomm_accept_unix_sock(apps_sock); |
| 1343 | if (sock < 0) { |
| 1344 | goto error; |
| 1345 | } |
| 1346 | |
| 1347 | /* |
| 1348 | * Set socket timeout for both receiving and ending. |
| 1349 | * app_socket_timeout is in seconds, whereas |
| 1350 | * lttcomm_setsockopt_rcv_timeout and |
| 1351 | * lttcomm_setsockopt_snd_timeout expect msec as |
| 1352 | * parameter. |
| 1353 | */ |
| 1354 | if (config.app_socket_timeout >= 0) { |
| 1355 | (void) lttcomm_setsockopt_rcv_timeout(sock, |
| 1356 | config.app_socket_timeout * 1000); |
| 1357 | (void) lttcomm_setsockopt_snd_timeout(sock, |
| 1358 | config.app_socket_timeout * 1000); |
| 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * Set the CLOEXEC flag. Return code is useless because |
| 1363 | * either way, the show must go on. |
| 1364 | */ |
| 1365 | (void) utils_set_fd_cloexec(sock); |
| 1366 | |
| 1367 | /* Create UST registration command for enqueuing */ |
| 1368 | ust_cmd = zmalloc(sizeof(struct ust_command)); |
| 1369 | if (ust_cmd == NULL) { |
| 1370 | PERROR("ust command zmalloc"); |
| 1371 | ret = close(sock); |
| 1372 | if (ret) { |
| 1373 | PERROR("close"); |
| 1374 | } |
| 1375 | goto error; |
| 1376 | } |
| 1377 | |
| 1378 | /* |
| 1379 | * Using message-based transmissions to ensure we don't |
| 1380 | * have to deal with partially received messages. |
| 1381 | */ |
| 1382 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 1383 | if (ret < 0) { |
| 1384 | ERR("Exhausted file descriptors allowed for applications."); |
| 1385 | free(ust_cmd); |
| 1386 | ret = close(sock); |
| 1387 | if (ret) { |
| 1388 | PERROR("close"); |
| 1389 | } |
| 1390 | sock = -1; |
| 1391 | continue; |
| 1392 | } |
| 1393 | |
| 1394 | health_code_update(); |
| 1395 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
| 1396 | if (ret < 0) { |
| 1397 | free(ust_cmd); |
| 1398 | /* Close socket of the application. */ |
| 1399 | ret = close(sock); |
| 1400 | if (ret) { |
| 1401 | PERROR("close"); |
| 1402 | } |
| 1403 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1404 | sock = -1; |
| 1405 | continue; |
| 1406 | } |
| 1407 | health_code_update(); |
| 1408 | |
| 1409 | ust_cmd->sock = sock; |
| 1410 | sock = -1; |
| 1411 | |
| 1412 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
| 1413 | " gid:%d sock:%d name:%s (version %d.%d)", |
| 1414 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, |
| 1415 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, |
| 1416 | ust_cmd->sock, ust_cmd->reg_msg.name, |
| 1417 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); |
| 1418 | |
| 1419 | /* |
| 1420 | * Lock free enqueue the registration request. The red pill |
| 1421 | * has been taken! This apps will be part of the *system*. |
| 1422 | */ |
| 1423 | cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node); |
| 1424 | |
| 1425 | /* |
| 1426 | * Wake the registration queue futex. Implicit memory |
| 1427 | * barrier with the exchange in cds_wfcq_enqueue. |
| 1428 | */ |
| 1429 | futex_nto1_wake(&ust_cmd_queue.futex); |
| 1430 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 1431 | ERR("Register apps socket poll error"); |
| 1432 | goto error; |
| 1433 | } else { |
| 1434 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 1435 | goto error; |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | exit: |
| 1442 | error: |
| 1443 | /* Notify that the registration thread is gone */ |
| 1444 | notify_ust_apps(0); |
| 1445 | |
| 1446 | if (apps_sock >= 0) { |
| 1447 | ret = close(apps_sock); |
| 1448 | if (ret) { |
| 1449 | PERROR("close"); |
| 1450 | } |
| 1451 | } |
| 1452 | if (sock >= 0) { |
| 1453 | ret = close(sock); |
| 1454 | if (ret) { |
| 1455 | PERROR("close"); |
| 1456 | } |
| 1457 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 1458 | } |
| 1459 | unlink(config.apps_unix_sock_path.value); |
| 1460 | |
| 1461 | error_poll_add: |
| 1462 | lttng_poll_clean(&events); |
| 1463 | error_listen: |
| 1464 | error_create_poll: |
| 1465 | error_testpoint: |
| 1466 | DBG("UST Registration thread cleanup complete"); |
| 1467 | if (err) { |
| 1468 | health_error(); |
| 1469 | ERR("Health error occurred in %s", __func__); |
| 1470 | } |
| 1471 | health_unregister(health_sessiond); |
| 1472 | |
| 1473 | return NULL; |
| 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | * Setup necessary data for kernel tracer action. |
| 1478 | */ |
| 1479 | static int init_kernel_tracer(void) |
| 1480 | { |
| 1481 | int ret; |
| 1482 | |
| 1483 | /* Modprobe lttng kernel modules */ |
| 1484 | ret = modprobe_lttng_control(); |
| 1485 | if (ret < 0) { |
| 1486 | goto error; |
| 1487 | } |
| 1488 | |
| 1489 | /* Open debugfs lttng */ |
| 1490 | kernel_tracer_fd = open(module_proc_lttng, O_RDWR); |
| 1491 | if (kernel_tracer_fd < 0) { |
| 1492 | DBG("Failed to open %s", module_proc_lttng); |
| 1493 | goto error_open; |
| 1494 | } |
| 1495 | |
| 1496 | /* Validate kernel version */ |
| 1497 | ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version, |
| 1498 | &kernel_tracer_abi_version); |
| 1499 | if (ret < 0) { |
| 1500 | goto error_version; |
| 1501 | } |
| 1502 | |
| 1503 | ret = modprobe_lttng_data(); |
| 1504 | if (ret < 0) { |
| 1505 | goto error_modules; |
| 1506 | } |
| 1507 | |
| 1508 | ret = kernel_supports_ring_buffer_snapshot_sample_positions( |
| 1509 | kernel_tracer_fd); |
| 1510 | if (ret < 0) { |
| 1511 | goto error_modules; |
| 1512 | } |
| 1513 | |
| 1514 | if (ret < 1) { |
| 1515 | WARN("Kernel tracer does not support buffer monitoring. " |
| 1516 | "The monitoring timer of channels in the kernel domain " |
| 1517 | "will be set to 0 (disabled)."); |
| 1518 | } |
| 1519 | |
| 1520 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
| 1521 | return 0; |
| 1522 | |
| 1523 | error_version: |
| 1524 | modprobe_remove_lttng_control(); |
| 1525 | ret = close(kernel_tracer_fd); |
| 1526 | if (ret) { |
| 1527 | PERROR("close"); |
| 1528 | } |
| 1529 | kernel_tracer_fd = -1; |
| 1530 | return LTTNG_ERR_KERN_VERSION; |
| 1531 | |
| 1532 | error_modules: |
| 1533 | ret = close(kernel_tracer_fd); |
| 1534 | if (ret) { |
| 1535 | PERROR("close"); |
| 1536 | } |
| 1537 | |
| 1538 | error_open: |
| 1539 | modprobe_remove_lttng_control(); |
| 1540 | |
| 1541 | error: |
| 1542 | WARN("No kernel tracer available"); |
| 1543 | kernel_tracer_fd = -1; |
| 1544 | if (!is_root) { |
| 1545 | return LTTNG_ERR_NEED_ROOT_SESSIOND; |
| 1546 | } else { |
| 1547 | return LTTNG_ERR_KERN_NA; |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | static int string_match(const char *str1, const char *str2) |
| 1552 | { |
| 1553 | return (str1 && str2) && !strcmp(str1, str2); |
| 1554 | } |
| 1555 | |
| 1556 | /* |
| 1557 | * Take an option from the getopt output and set it in the right variable to be |
| 1558 | * used later. |
| 1559 | * |
| 1560 | * Return 0 on success else a negative value. |
| 1561 | */ |
| 1562 | static int set_option(int opt, const char *arg, const char *optname) |
| 1563 | { |
| 1564 | int ret = 0; |
| 1565 | |
| 1566 | if (string_match(optname, "client-sock") || opt == 'c') { |
| 1567 | if (!arg || *arg == '\0') { |
| 1568 | ret = -EINVAL; |
| 1569 | goto end; |
| 1570 | } |
| 1571 | if (lttng_is_setuid_setgid()) { |
| 1572 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1573 | "-c, --client-sock"); |
| 1574 | } else { |
| 1575 | config_string_set(&config.client_unix_sock_path, |
| 1576 | strdup(arg)); |
| 1577 | if (!config.client_unix_sock_path.value) { |
| 1578 | ret = -ENOMEM; |
| 1579 | PERROR("strdup"); |
| 1580 | } |
| 1581 | } |
| 1582 | } else if (string_match(optname, "apps-sock") || opt == 'a') { |
| 1583 | if (!arg || *arg == '\0') { |
| 1584 | ret = -EINVAL; |
| 1585 | goto end; |
| 1586 | } |
| 1587 | if (lttng_is_setuid_setgid()) { |
| 1588 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1589 | "-a, --apps-sock"); |
| 1590 | } else { |
| 1591 | config_string_set(&config.apps_unix_sock_path, |
| 1592 | strdup(arg)); |
| 1593 | if (!config.apps_unix_sock_path.value) { |
| 1594 | ret = -ENOMEM; |
| 1595 | PERROR("strdup"); |
| 1596 | } |
| 1597 | } |
| 1598 | } else if (string_match(optname, "daemonize") || opt == 'd') { |
| 1599 | config.daemonize = true; |
| 1600 | } else if (string_match(optname, "background") || opt == 'b') { |
| 1601 | config.background = true; |
| 1602 | } else if (string_match(optname, "group") || opt == 'g') { |
| 1603 | if (!arg || *arg == '\0') { |
| 1604 | ret = -EINVAL; |
| 1605 | goto end; |
| 1606 | } |
| 1607 | if (lttng_is_setuid_setgid()) { |
| 1608 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1609 | "-g, --group"); |
| 1610 | } else { |
| 1611 | config_string_set(&config.tracing_group_name, |
| 1612 | strdup(arg)); |
| 1613 | if (!config.tracing_group_name.value) { |
| 1614 | ret = -ENOMEM; |
| 1615 | PERROR("strdup"); |
| 1616 | } |
| 1617 | } |
| 1618 | } else if (string_match(optname, "help") || opt == 'h') { |
| 1619 | ret = utils_show_help(8, "lttng-sessiond", help_msg); |
| 1620 | if (ret) { |
| 1621 | ERR("Cannot show --help for `lttng-sessiond`"); |
| 1622 | perror("exec"); |
| 1623 | } |
| 1624 | exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); |
| 1625 | } else if (string_match(optname, "version") || opt == 'V') { |
| 1626 | fprintf(stdout, "%s\n", VERSION); |
| 1627 | exit(EXIT_SUCCESS); |
| 1628 | } else if (string_match(optname, "sig-parent") || opt == 'S') { |
| 1629 | config.sig_parent = true; |
| 1630 | } else if (string_match(optname, "kconsumerd-err-sock")) { |
| 1631 | if (!arg || *arg == '\0') { |
| 1632 | ret = -EINVAL; |
| 1633 | goto end; |
| 1634 | } |
| 1635 | if (lttng_is_setuid_setgid()) { |
| 1636 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1637 | "--kconsumerd-err-sock"); |
| 1638 | } else { |
| 1639 | config_string_set(&config.kconsumerd_err_unix_sock_path, |
| 1640 | strdup(arg)); |
| 1641 | if (!config.kconsumerd_err_unix_sock_path.value) { |
| 1642 | ret = -ENOMEM; |
| 1643 | PERROR("strdup"); |
| 1644 | } |
| 1645 | } |
| 1646 | } else if (string_match(optname, "kconsumerd-cmd-sock")) { |
| 1647 | if (!arg || *arg == '\0') { |
| 1648 | ret = -EINVAL; |
| 1649 | goto end; |
| 1650 | } |
| 1651 | if (lttng_is_setuid_setgid()) { |
| 1652 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1653 | "--kconsumerd-cmd-sock"); |
| 1654 | } else { |
| 1655 | config_string_set(&config.kconsumerd_cmd_unix_sock_path, |
| 1656 | strdup(arg)); |
| 1657 | if (!config.kconsumerd_cmd_unix_sock_path.value) { |
| 1658 | ret = -ENOMEM; |
| 1659 | PERROR("strdup"); |
| 1660 | } |
| 1661 | } |
| 1662 | } else if (string_match(optname, "ustconsumerd64-err-sock")) { |
| 1663 | if (!arg || *arg == '\0') { |
| 1664 | ret = -EINVAL; |
| 1665 | goto end; |
| 1666 | } |
| 1667 | if (lttng_is_setuid_setgid()) { |
| 1668 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1669 | "--ustconsumerd64-err-sock"); |
| 1670 | } else { |
| 1671 | config_string_set(&config.consumerd64_err_unix_sock_path, |
| 1672 | strdup(arg)); |
| 1673 | if (!config.consumerd64_err_unix_sock_path.value) { |
| 1674 | ret = -ENOMEM; |
| 1675 | PERROR("strdup"); |
| 1676 | } |
| 1677 | } |
| 1678 | } else if (string_match(optname, "ustconsumerd64-cmd-sock")) { |
| 1679 | if (!arg || *arg == '\0') { |
| 1680 | ret = -EINVAL; |
| 1681 | goto end; |
| 1682 | } |
| 1683 | if (lttng_is_setuid_setgid()) { |
| 1684 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1685 | "--ustconsumerd64-cmd-sock"); |
| 1686 | } else { |
| 1687 | config_string_set(&config.consumerd64_cmd_unix_sock_path, |
| 1688 | strdup(arg)); |
| 1689 | if (!config.consumerd64_cmd_unix_sock_path.value) { |
| 1690 | ret = -ENOMEM; |
| 1691 | PERROR("strdup"); |
| 1692 | } |
| 1693 | } |
| 1694 | } else if (string_match(optname, "ustconsumerd32-err-sock")) { |
| 1695 | if (!arg || *arg == '\0') { |
| 1696 | ret = -EINVAL; |
| 1697 | goto end; |
| 1698 | } |
| 1699 | if (lttng_is_setuid_setgid()) { |
| 1700 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1701 | "--ustconsumerd32-err-sock"); |
| 1702 | } else { |
| 1703 | config_string_set(&config.consumerd32_err_unix_sock_path, |
| 1704 | strdup(arg)); |
| 1705 | if (!config.consumerd32_err_unix_sock_path.value) { |
| 1706 | ret = -ENOMEM; |
| 1707 | PERROR("strdup"); |
| 1708 | } |
| 1709 | } |
| 1710 | } else if (string_match(optname, "ustconsumerd32-cmd-sock")) { |
| 1711 | if (!arg || *arg == '\0') { |
| 1712 | ret = -EINVAL; |
| 1713 | goto end; |
| 1714 | } |
| 1715 | if (lttng_is_setuid_setgid()) { |
| 1716 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1717 | "--ustconsumerd32-cmd-sock"); |
| 1718 | } else { |
| 1719 | config_string_set(&config.consumerd32_cmd_unix_sock_path, |
| 1720 | strdup(arg)); |
| 1721 | if (!config.consumerd32_cmd_unix_sock_path.value) { |
| 1722 | ret = -ENOMEM; |
| 1723 | PERROR("strdup"); |
| 1724 | } |
| 1725 | } |
| 1726 | } else if (string_match(optname, "no-kernel")) { |
| 1727 | config.no_kernel = true; |
| 1728 | } else if (string_match(optname, "quiet") || opt == 'q') { |
| 1729 | config.quiet = true; |
| 1730 | } else if (string_match(optname, "verbose") || opt == 'v') { |
| 1731 | /* Verbose level can increase using multiple -v */ |
| 1732 | if (arg) { |
| 1733 | /* Value obtained from config file */ |
| 1734 | config.verbose = config_parse_value(arg); |
| 1735 | } else { |
| 1736 | /* -v used on command line */ |
| 1737 | config.verbose++; |
| 1738 | } |
| 1739 | /* Clamp value to [0, 3] */ |
| 1740 | config.verbose = config.verbose < 0 ? 0 : |
| 1741 | (config.verbose <= 3 ? config.verbose : 3); |
| 1742 | } else if (string_match(optname, "verbose-consumer")) { |
| 1743 | if (arg) { |
| 1744 | config.verbose_consumer = config_parse_value(arg); |
| 1745 | } else { |
| 1746 | config.verbose_consumer++; |
| 1747 | } |
| 1748 | } else if (string_match(optname, "consumerd32-path")) { |
| 1749 | if (!arg || *arg == '\0') { |
| 1750 | ret = -EINVAL; |
| 1751 | goto end; |
| 1752 | } |
| 1753 | if (lttng_is_setuid_setgid()) { |
| 1754 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1755 | "--consumerd32-path"); |
| 1756 | } else { |
| 1757 | config_string_set(&config.consumerd32_bin_path, |
| 1758 | strdup(arg)); |
| 1759 | if (!config.consumerd32_bin_path.value) { |
| 1760 | PERROR("strdup"); |
| 1761 | ret = -ENOMEM; |
| 1762 | } |
| 1763 | } |
| 1764 | } else if (string_match(optname, "consumerd32-libdir")) { |
| 1765 | if (!arg || *arg == '\0') { |
| 1766 | ret = -EINVAL; |
| 1767 | goto end; |
| 1768 | } |
| 1769 | if (lttng_is_setuid_setgid()) { |
| 1770 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1771 | "--consumerd32-libdir"); |
| 1772 | } else { |
| 1773 | config_string_set(&config.consumerd32_lib_dir, |
| 1774 | strdup(arg)); |
| 1775 | if (!config.consumerd32_lib_dir.value) { |
| 1776 | PERROR("strdup"); |
| 1777 | ret = -ENOMEM; |
| 1778 | } |
| 1779 | } |
| 1780 | } else if (string_match(optname, "consumerd64-path")) { |
| 1781 | if (!arg || *arg == '\0') { |
| 1782 | ret = -EINVAL; |
| 1783 | goto end; |
| 1784 | } |
| 1785 | if (lttng_is_setuid_setgid()) { |
| 1786 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1787 | "--consumerd64-path"); |
| 1788 | } else { |
| 1789 | config_string_set(&config.consumerd64_bin_path, |
| 1790 | strdup(arg)); |
| 1791 | if (!config.consumerd64_bin_path.value) { |
| 1792 | PERROR("strdup"); |
| 1793 | ret = -ENOMEM; |
| 1794 | } |
| 1795 | } |
| 1796 | } else if (string_match(optname, "consumerd64-libdir")) { |
| 1797 | if (!arg || *arg == '\0') { |
| 1798 | ret = -EINVAL; |
| 1799 | goto end; |
| 1800 | } |
| 1801 | if (lttng_is_setuid_setgid()) { |
| 1802 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1803 | "--consumerd64-libdir"); |
| 1804 | } else { |
| 1805 | config_string_set(&config.consumerd64_lib_dir, |
| 1806 | strdup(arg)); |
| 1807 | if (!config.consumerd64_lib_dir.value) { |
| 1808 | PERROR("strdup"); |
| 1809 | ret = -ENOMEM; |
| 1810 | } |
| 1811 | } |
| 1812 | } else if (string_match(optname, "pidfile") || opt == 'p') { |
| 1813 | if (!arg || *arg == '\0') { |
| 1814 | ret = -EINVAL; |
| 1815 | goto end; |
| 1816 | } |
| 1817 | if (lttng_is_setuid_setgid()) { |
| 1818 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1819 | "-p, --pidfile"); |
| 1820 | } else { |
| 1821 | config_string_set(&config.pid_file_path, strdup(arg)); |
| 1822 | if (!config.pid_file_path.value) { |
| 1823 | PERROR("strdup"); |
| 1824 | ret = -ENOMEM; |
| 1825 | } |
| 1826 | } |
| 1827 | } else if (string_match(optname, "agent-tcp-port")) { |
| 1828 | if (!arg || *arg == '\0') { |
| 1829 | ret = -EINVAL; |
| 1830 | goto end; |
| 1831 | } |
| 1832 | if (lttng_is_setuid_setgid()) { |
| 1833 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1834 | "--agent-tcp-port"); |
| 1835 | } else { |
| 1836 | unsigned long v; |
| 1837 | |
| 1838 | errno = 0; |
| 1839 | v = strtoul(arg, NULL, 0); |
| 1840 | if (errno != 0 || !isdigit(arg[0])) { |
| 1841 | ERR("Wrong value in --agent-tcp-port parameter: %s", arg); |
| 1842 | return -1; |
| 1843 | } |
| 1844 | if (v == 0 || v >= 65535) { |
| 1845 | ERR("Port overflow in --agent-tcp-port parameter: %s", arg); |
| 1846 | return -1; |
| 1847 | } |
| 1848 | config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v; |
| 1849 | DBG3("Agent TCP port set to non default: %i", (int) v); |
| 1850 | } |
| 1851 | } else if (string_match(optname, "load") || opt == 'l') { |
| 1852 | if (!arg || *arg == '\0') { |
| 1853 | ret = -EINVAL; |
| 1854 | goto end; |
| 1855 | } |
| 1856 | if (lttng_is_setuid_setgid()) { |
| 1857 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1858 | "-l, --load"); |
| 1859 | } else { |
| 1860 | config_string_set(&config.load_session_path, strdup(arg)); |
| 1861 | if (!config.load_session_path.value) { |
| 1862 | PERROR("strdup"); |
| 1863 | ret = -ENOMEM; |
| 1864 | } |
| 1865 | } |
| 1866 | } else if (string_match(optname, "kmod-probes")) { |
| 1867 | if (!arg || *arg == '\0') { |
| 1868 | ret = -EINVAL; |
| 1869 | goto end; |
| 1870 | } |
| 1871 | if (lttng_is_setuid_setgid()) { |
| 1872 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1873 | "--kmod-probes"); |
| 1874 | } else { |
| 1875 | config_string_set(&config.kmod_probes_list, strdup(arg)); |
| 1876 | if (!config.kmod_probes_list.value) { |
| 1877 | PERROR("strdup"); |
| 1878 | ret = -ENOMEM; |
| 1879 | } |
| 1880 | } |
| 1881 | } else if (string_match(optname, "extra-kmod-probes")) { |
| 1882 | if (!arg || *arg == '\0') { |
| 1883 | ret = -EINVAL; |
| 1884 | goto end; |
| 1885 | } |
| 1886 | if (lttng_is_setuid_setgid()) { |
| 1887 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 1888 | "--extra-kmod-probes"); |
| 1889 | } else { |
| 1890 | config_string_set(&config.kmod_extra_probes_list, |
| 1891 | strdup(arg)); |
| 1892 | if (!config.kmod_extra_probes_list.value) { |
| 1893 | PERROR("strdup"); |
| 1894 | ret = -ENOMEM; |
| 1895 | } |
| 1896 | } |
| 1897 | } else if (string_match(optname, "config") || opt == 'f') { |
| 1898 | /* This is handled in set_options() thus silent skip. */ |
| 1899 | goto end; |
| 1900 | } else { |
| 1901 | /* Unknown option or other error. |
| 1902 | * Error is printed by getopt, just return */ |
| 1903 | ret = -1; |
| 1904 | } |
| 1905 | |
| 1906 | end: |
| 1907 | if (ret == -EINVAL) { |
| 1908 | const char *opt_name = "unknown"; |
| 1909 | int i; |
| 1910 | |
| 1911 | for (i = 0; i < sizeof(long_options) / sizeof(struct option); |
| 1912 | i++) { |
| 1913 | if (opt == long_options[i].val) { |
| 1914 | opt_name = long_options[i].name; |
| 1915 | break; |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | WARN("Invalid argument provided for option \"%s\", using default value.", |
| 1920 | opt_name); |
| 1921 | } |
| 1922 | |
| 1923 | return ret; |
| 1924 | } |
| 1925 | |
| 1926 | /* |
| 1927 | * config_entry_handler_cb used to handle options read from a config file. |
| 1928 | * See config_entry_handler_cb comment in common/config/session-config.h for the |
| 1929 | * return value conventions. |
| 1930 | */ |
| 1931 | static int config_entry_handler(const struct config_entry *entry, void *unused) |
| 1932 | { |
| 1933 | int ret = 0, i; |
| 1934 | |
| 1935 | if (!entry || !entry->name || !entry->value) { |
| 1936 | ret = -EINVAL; |
| 1937 | goto end; |
| 1938 | } |
| 1939 | |
| 1940 | /* Check if the option is to be ignored */ |
| 1941 | for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) { |
| 1942 | if (!strcmp(entry->name, config_ignore_options[i])) { |
| 1943 | goto end; |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1; |
| 1948 | i++) { |
| 1949 | |
| 1950 | /* Ignore if not fully matched. */ |
| 1951 | if (strcmp(entry->name, long_options[i].name)) { |
| 1952 | continue; |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * If the option takes no argument on the command line, we have to |
| 1957 | * check if the value is "true". We support non-zero numeric values, |
| 1958 | * true, on and yes. |
| 1959 | */ |
| 1960 | if (!long_options[i].has_arg) { |
| 1961 | ret = config_parse_value(entry->value); |
| 1962 | if (ret <= 0) { |
| 1963 | if (ret) { |
| 1964 | WARN("Invalid configuration value \"%s\" for option %s", |
| 1965 | entry->value, entry->name); |
| 1966 | } |
| 1967 | /* False, skip boolean config option. */ |
| 1968 | goto end; |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | ret = set_option(long_options[i].val, entry->value, entry->name); |
| 1973 | goto end; |
| 1974 | } |
| 1975 | |
| 1976 | WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name); |
| 1977 | |
| 1978 | end: |
| 1979 | return ret; |
| 1980 | } |
| 1981 | |
| 1982 | /* |
| 1983 | * daemon configuration loading and argument parsing |
| 1984 | */ |
| 1985 | static int set_options(int argc, char **argv) |
| 1986 | { |
| 1987 | int ret = 0, c = 0, option_index = 0; |
| 1988 | int orig_optopt = optopt, orig_optind = optind; |
| 1989 | char *optstring; |
| 1990 | const char *config_path = NULL; |
| 1991 | |
| 1992 | optstring = utils_generate_optstring(long_options, |
| 1993 | sizeof(long_options) / sizeof(struct option)); |
| 1994 | if (!optstring) { |
| 1995 | ret = -ENOMEM; |
| 1996 | goto end; |
| 1997 | } |
| 1998 | |
| 1999 | /* Check for the --config option */ |
| 2000 | while ((c = getopt_long(argc, argv, optstring, long_options, |
| 2001 | &option_index)) != -1) { |
| 2002 | if (c == '?') { |
| 2003 | ret = -EINVAL; |
| 2004 | goto end; |
| 2005 | } else if (c != 'f') { |
| 2006 | /* if not equal to --config option. */ |
| 2007 | continue; |
| 2008 | } |
| 2009 | |
| 2010 | if (lttng_is_setuid_setgid()) { |
| 2011 | WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.", |
| 2012 | "-f, --config"); |
| 2013 | } else { |
| 2014 | config_path = utils_expand_path(optarg); |
| 2015 | if (!config_path) { |
| 2016 | ERR("Failed to resolve path: %s", optarg); |
| 2017 | } |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | ret = config_get_section_entries(config_path, config_section_name, |
| 2022 | config_entry_handler, NULL); |
| 2023 | if (ret) { |
| 2024 | if (ret > 0) { |
| 2025 | ERR("Invalid configuration option at line %i", ret); |
| 2026 | ret = -1; |
| 2027 | } |
| 2028 | goto end; |
| 2029 | } |
| 2030 | |
| 2031 | /* Reset getopt's global state */ |
| 2032 | optopt = orig_optopt; |
| 2033 | optind = orig_optind; |
| 2034 | while (1) { |
| 2035 | option_index = -1; |
| 2036 | /* |
| 2037 | * getopt_long() will not set option_index if it encounters a |
| 2038 | * short option. |
| 2039 | */ |
| 2040 | c = getopt_long(argc, argv, optstring, long_options, |
| 2041 | &option_index); |
| 2042 | if (c == -1) { |
| 2043 | break; |
| 2044 | } |
| 2045 | |
| 2046 | /* |
| 2047 | * Pass NULL as the long option name if popt left the index |
| 2048 | * unset. |
| 2049 | */ |
| 2050 | ret = set_option(c, optarg, |
| 2051 | option_index < 0 ? NULL : |
| 2052 | long_options[option_index].name); |
| 2053 | if (ret < 0) { |
| 2054 | break; |
| 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | end: |
| 2059 | free(optstring); |
| 2060 | return ret; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * Creates the application socket. |
| 2065 | */ |
| 2066 | static int init_daemon_socket(void) |
| 2067 | { |
| 2068 | int ret = 0; |
| 2069 | mode_t old_umask; |
| 2070 | |
| 2071 | old_umask = umask(0); |
| 2072 | |
| 2073 | /* Create the application unix socket */ |
| 2074 | apps_sock = lttcomm_create_unix_sock(config.apps_unix_sock_path.value); |
| 2075 | if (apps_sock < 0) { |
| 2076 | ERR("Create unix sock failed: %s", config.apps_unix_sock_path.value); |
| 2077 | ret = -1; |
| 2078 | goto end; |
| 2079 | } |
| 2080 | |
| 2081 | /* Set the cloexec flag */ |
| 2082 | ret = utils_set_fd_cloexec(apps_sock); |
| 2083 | if (ret < 0) { |
| 2084 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " |
| 2085 | "Continuing but note that the consumer daemon will have a " |
| 2086 | "reference to this socket on exec()", apps_sock); |
| 2087 | } |
| 2088 | |
| 2089 | /* File permission MUST be 666 */ |
| 2090 | ret = chmod(config.apps_unix_sock_path.value, |
| 2091 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 2092 | if (ret < 0) { |
| 2093 | ERR("Set file permissions failed: %s", config.apps_unix_sock_path.value); |
| 2094 | PERROR("chmod"); |
| 2095 | goto end; |
| 2096 | } |
| 2097 | |
| 2098 | DBG3("Session daemon application socket %d created", |
| 2099 | apps_sock); |
| 2100 | |
| 2101 | end: |
| 2102 | umask(old_umask); |
| 2103 | return ret; |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * Create lockfile using the rundir and return its fd. |
| 2108 | */ |
| 2109 | static int create_lockfile(void) |
| 2110 | { |
| 2111 | return utils_create_lock_file(config.lock_file_path.value); |
| 2112 | } |
| 2113 | |
| 2114 | /* |
| 2115 | * Check if the global socket is available, and if a daemon is answering at the |
| 2116 | * other side. If yes, error is returned. |
| 2117 | * |
| 2118 | * Also attempts to create and hold the lock file. |
| 2119 | */ |
| 2120 | static int check_existing_daemon(void) |
| 2121 | { |
| 2122 | int ret = 0; |
| 2123 | |
| 2124 | /* Is there anybody out there ? */ |
| 2125 | if (lttng_session_daemon_alive()) { |
| 2126 | ret = -EEXIST; |
| 2127 | goto end; |
| 2128 | } |
| 2129 | |
| 2130 | lockfile_fd = create_lockfile(); |
| 2131 | if (lockfile_fd < 0) { |
| 2132 | ret = -EEXIST; |
| 2133 | goto end; |
| 2134 | } |
| 2135 | end: |
| 2136 | return ret; |
| 2137 | } |
| 2138 | |
| 2139 | static void sessiond_cleanup_lock_file(void) |
| 2140 | { |
| 2141 | int ret; |
| 2142 | |
| 2143 | /* |
| 2144 | * Cleanup lock file by deleting it and finaly closing it which will |
| 2145 | * release the file system lock. |
| 2146 | */ |
| 2147 | if (lockfile_fd >= 0) { |
| 2148 | ret = remove(config.lock_file_path.value); |
| 2149 | if (ret < 0) { |
| 2150 | PERROR("remove lock file"); |
| 2151 | } |
| 2152 | ret = close(lockfile_fd); |
| 2153 | if (ret < 0) { |
| 2154 | PERROR("close lock file"); |
| 2155 | } |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | * Set the tracing group gid onto the client socket. |
| 2161 | * |
| 2162 | * Race window between mkdir and chown is OK because we are going from more |
| 2163 | * permissive (root.root) to less permissive (root.tracing). |
| 2164 | */ |
| 2165 | static int set_permissions(char *rundir) |
| 2166 | { |
| 2167 | int ret; |
| 2168 | gid_t gid; |
| 2169 | |
| 2170 | gid = utils_get_group_id(config.tracing_group_name.value); |
| 2171 | |
| 2172 | /* Set lttng run dir */ |
| 2173 | ret = chown(rundir, 0, gid); |
| 2174 | if (ret < 0) { |
| 2175 | ERR("Unable to set group on %s", rundir); |
| 2176 | PERROR("chown"); |
| 2177 | } |
| 2178 | |
| 2179 | /* |
| 2180 | * Ensure all applications and tracing group can search the run |
| 2181 | * dir. Allow everyone to read the directory, since it does not |
| 2182 | * buy us anything to hide its content. |
| 2183 | */ |
| 2184 | ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); |
| 2185 | if (ret < 0) { |
| 2186 | ERR("Unable to set permissions on %s", rundir); |
| 2187 | PERROR("chmod"); |
| 2188 | } |
| 2189 | |
| 2190 | /* lttng client socket path */ |
| 2191 | ret = chown(config.client_unix_sock_path.value, 0, gid); |
| 2192 | if (ret < 0) { |
| 2193 | ERR("Unable to set group on %s", config.client_unix_sock_path.value); |
| 2194 | PERROR("chown"); |
| 2195 | } |
| 2196 | |
| 2197 | /* kconsumer error socket path */ |
| 2198 | ret = chown(kconsumer_data.err_unix_sock_path, 0, 0); |
| 2199 | if (ret < 0) { |
| 2200 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
| 2201 | PERROR("chown"); |
| 2202 | } |
| 2203 | |
| 2204 | /* 64-bit ustconsumer error socket path */ |
| 2205 | ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0); |
| 2206 | if (ret < 0) { |
| 2207 | ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path); |
| 2208 | PERROR("chown"); |
| 2209 | } |
| 2210 | |
| 2211 | /* 32-bit ustconsumer compat32 error socket path */ |
| 2212 | ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0); |
| 2213 | if (ret < 0) { |
| 2214 | ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path); |
| 2215 | PERROR("chown"); |
| 2216 | } |
| 2217 | |
| 2218 | DBG("All permissions are set"); |
| 2219 | |
| 2220 | return ret; |
| 2221 | } |
| 2222 | |
| 2223 | /* |
| 2224 | * Create the lttng run directory needed for all global sockets and pipe. |
| 2225 | */ |
| 2226 | static int create_lttng_rundir(void) |
| 2227 | { |
| 2228 | int ret; |
| 2229 | |
| 2230 | DBG3("Creating LTTng run directory: %s", config.rundir.value); |
| 2231 | |
| 2232 | ret = mkdir(config.rundir.value, S_IRWXU); |
| 2233 | if (ret < 0) { |
| 2234 | if (errno != EEXIST) { |
| 2235 | ERR("Unable to create %s", config.rundir.value); |
| 2236 | goto error; |
| 2237 | } else { |
| 2238 | ret = 0; |
| 2239 | } |
| 2240 | } |
| 2241 | |
| 2242 | error: |
| 2243 | return ret; |
| 2244 | } |
| 2245 | |
| 2246 | /* |
| 2247 | * Setup sockets and directory needed by the consumerds' communication with the |
| 2248 | * session daemon. |
| 2249 | */ |
| 2250 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
| 2251 | { |
| 2252 | int ret; |
| 2253 | char *path = NULL; |
| 2254 | |
| 2255 | switch (consumer_data->type) { |
| 2256 | case LTTNG_CONSUMER_KERNEL: |
| 2257 | path = config.kconsumerd_path.value; |
| 2258 | break; |
| 2259 | case LTTNG_CONSUMER64_UST: |
| 2260 | path = config.consumerd64_path.value; |
| 2261 | break; |
| 2262 | case LTTNG_CONSUMER32_UST: |
| 2263 | path = config.consumerd32_path.value; |
| 2264 | break; |
| 2265 | default: |
| 2266 | ERR("Consumer type unknown"); |
| 2267 | ret = -EINVAL; |
| 2268 | goto error; |
| 2269 | } |
| 2270 | assert(path); |
| 2271 | |
| 2272 | DBG2("Creating consumer directory: %s", path); |
| 2273 | |
| 2274 | ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); |
| 2275 | if (ret < 0 && errno != EEXIST) { |
| 2276 | PERROR("mkdir"); |
| 2277 | ERR("Failed to create %s", path); |
| 2278 | goto error; |
| 2279 | } |
| 2280 | if (is_root) { |
| 2281 | ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value)); |
| 2282 | if (ret < 0) { |
| 2283 | ERR("Unable to set group on %s", path); |
| 2284 | PERROR("chown"); |
| 2285 | goto error; |
| 2286 | } |
| 2287 | } |
| 2288 | |
| 2289 | /* Create the consumerd error unix socket */ |
| 2290 | consumer_data->err_sock = |
| 2291 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); |
| 2292 | if (consumer_data->err_sock < 0) { |
| 2293 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); |
| 2294 | ret = -1; |
| 2295 | goto error; |
| 2296 | } |
| 2297 | |
| 2298 | /* |
| 2299 | * Set the CLOEXEC flag. Return code is useless because either way, the |
| 2300 | * show must go on. |
| 2301 | */ |
| 2302 | ret = utils_set_fd_cloexec(consumer_data->err_sock); |
| 2303 | if (ret < 0) { |
| 2304 | PERROR("utils_set_fd_cloexec"); |
| 2305 | /* continue anyway */ |
| 2306 | } |
| 2307 | |
| 2308 | /* File permission MUST be 660 */ |
| 2309 | ret = chmod(consumer_data->err_unix_sock_path, |
| 2310 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
| 2311 | if (ret < 0) { |
| 2312 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
| 2313 | PERROR("chmod"); |
| 2314 | goto error; |
| 2315 | } |
| 2316 | |
| 2317 | error: |
| 2318 | return ret; |
| 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | * Signal handler for the daemon |
| 2323 | * |
| 2324 | * Simply stop all worker threads, leaving main() return gracefully after |
| 2325 | * joining all threads and calling cleanup(). |
| 2326 | */ |
| 2327 | static void sighandler(int sig) |
| 2328 | { |
| 2329 | switch (sig) { |
| 2330 | case SIGINT: |
| 2331 | DBG("SIGINT caught"); |
| 2332 | stop_threads(); |
| 2333 | break; |
| 2334 | case SIGTERM: |
| 2335 | DBG("SIGTERM caught"); |
| 2336 | stop_threads(); |
| 2337 | break; |
| 2338 | case SIGUSR1: |
| 2339 | CMM_STORE_SHARED(recv_child_signal, 1); |
| 2340 | break; |
| 2341 | default: |
| 2342 | break; |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | /* |
| 2347 | * Setup signal handler for : |
| 2348 | * SIGINT, SIGTERM, SIGPIPE |
| 2349 | */ |
| 2350 | static int set_signal_handler(void) |
| 2351 | { |
| 2352 | int ret = 0; |
| 2353 | struct sigaction sa; |
| 2354 | sigset_t sigset; |
| 2355 | |
| 2356 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 2357 | PERROR("sigemptyset"); |
| 2358 | return ret; |
| 2359 | } |
| 2360 | |
| 2361 | sa.sa_mask = sigset; |
| 2362 | sa.sa_flags = 0; |
| 2363 | |
| 2364 | sa.sa_handler = sighandler; |
| 2365 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 2366 | PERROR("sigaction"); |
| 2367 | return ret; |
| 2368 | } |
| 2369 | |
| 2370 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
| 2371 | PERROR("sigaction"); |
| 2372 | return ret; |
| 2373 | } |
| 2374 | |
| 2375 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
| 2376 | PERROR("sigaction"); |
| 2377 | return ret; |
| 2378 | } |
| 2379 | |
| 2380 | sa.sa_handler = SIG_IGN; |
| 2381 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
| 2382 | PERROR("sigaction"); |
| 2383 | return ret; |
| 2384 | } |
| 2385 | |
| 2386 | DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT"); |
| 2387 | |
| 2388 | return ret; |
| 2389 | } |
| 2390 | |
| 2391 | /* |
| 2392 | * Set open files limit to unlimited. This daemon can open a large number of |
| 2393 | * file descriptors in order to consume multiple kernel traces. |
| 2394 | */ |
| 2395 | static void set_ulimit(void) |
| 2396 | { |
| 2397 | int ret; |
| 2398 | struct rlimit lim; |
| 2399 | |
| 2400 | /* The kernel does not allow an infinite limit for open files */ |
| 2401 | lim.rlim_cur = 65535; |
| 2402 | lim.rlim_max = 65535; |
| 2403 | |
| 2404 | ret = setrlimit(RLIMIT_NOFILE, &lim); |
| 2405 | if (ret < 0) { |
| 2406 | PERROR("failed to set open files limit"); |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | static int write_pidfile(void) |
| 2411 | { |
| 2412 | return utils_create_pid_file(getpid(), config.pid_file_path.value); |
| 2413 | } |
| 2414 | |
| 2415 | static int set_clock_plugin_env(void) |
| 2416 | { |
| 2417 | int ret = 0; |
| 2418 | char *env_value = NULL; |
| 2419 | |
| 2420 | if (!config.lttng_ust_clock_plugin.value) { |
| 2421 | goto end; |
| 2422 | } |
| 2423 | |
| 2424 | ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s", |
| 2425 | config.lttng_ust_clock_plugin.value); |
| 2426 | if (ret < 0) { |
| 2427 | PERROR("asprintf"); |
| 2428 | goto end; |
| 2429 | } |
| 2430 | |
| 2431 | ret = putenv(env_value); |
| 2432 | if (ret) { |
| 2433 | free(env_value); |
| 2434 | PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN"); |
| 2435 | goto end; |
| 2436 | } |
| 2437 | |
| 2438 | DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"", |
| 2439 | config.lttng_ust_clock_plugin.value); |
| 2440 | end: |
| 2441 | return ret; |
| 2442 | } |
| 2443 | |
| 2444 | static void destroy_all_sessions_and_wait(void) |
| 2445 | { |
| 2446 | struct ltt_session *session, *tmp; |
| 2447 | struct ltt_session_list *session_list; |
| 2448 | |
| 2449 | session_list = session_get_list(); |
| 2450 | DBG("Initiating destruction of all sessions"); |
| 2451 | |
| 2452 | if (!session_list) { |
| 2453 | return; |
| 2454 | } |
| 2455 | |
| 2456 | session_lock_list(); |
| 2457 | /* Initiate the destruction of all sessions. */ |
| 2458 | cds_list_for_each_entry_safe(session, tmp, |
| 2459 | &session_list->head, list) { |
| 2460 | if (!session_get(session)) { |
| 2461 | continue; |
| 2462 | } |
| 2463 | |
| 2464 | session_lock(session); |
| 2465 | if (session->destroyed) { |
| 2466 | goto unlock_session; |
| 2467 | } |
| 2468 | (void) cmd_destroy_session(session, |
| 2469 | notification_thread_handle); |
| 2470 | unlock_session: |
| 2471 | session_unlock(session); |
| 2472 | session_put(session); |
| 2473 | } |
| 2474 | session_unlock_list(); |
| 2475 | |
| 2476 | /* Wait for the destruction of all sessions to complete. */ |
| 2477 | DBG("Waiting for the destruction of all sessions to complete"); |
| 2478 | session_list_wait_empty(); |
| 2479 | DBG("Destruction of all sessions completed"); |
| 2480 | } |
| 2481 | |
| 2482 | /* |
| 2483 | * main |
| 2484 | */ |
| 2485 | int main(int argc, char **argv) |
| 2486 | { |
| 2487 | int ret = 0, retval = 0; |
| 2488 | void *status; |
| 2489 | const char *env_app_timeout; |
| 2490 | struct lttng_pipe *ust32_channel_monitor_pipe = NULL, |
| 2491 | *ust64_channel_monitor_pipe = NULL, |
| 2492 | *kernel_channel_monitor_pipe = NULL; |
| 2493 | struct lttng_thread *ht_cleanup_thread = NULL; |
| 2494 | struct timer_thread_parameters timer_thread_parameters; |
| 2495 | /* Rotation thread handle. */ |
| 2496 | struct rotation_thread_handle *rotation_thread_handle = NULL; |
| 2497 | /* Queue of rotation jobs populated by the sessiond-timer. */ |
| 2498 | struct rotation_thread_timer_queue *rotation_timer_queue = NULL; |
| 2499 | struct lttng_thread *client_thread = NULL; |
| 2500 | |
| 2501 | init_kernel_workarounds(); |
| 2502 | |
| 2503 | rcu_register_thread(); |
| 2504 | |
| 2505 | if (set_signal_handler()) { |
| 2506 | retval = -1; |
| 2507 | goto exit_set_signal_handler; |
| 2508 | } |
| 2509 | |
| 2510 | if (timer_signal_init()) { |
| 2511 | retval = -1; |
| 2512 | goto exit_set_signal_handler; |
| 2513 | } |
| 2514 | |
| 2515 | page_size = sysconf(_SC_PAGESIZE); |
| 2516 | if (page_size < 0) { |
| 2517 | PERROR("sysconf _SC_PAGESIZE"); |
| 2518 | page_size = LONG_MAX; |
| 2519 | WARN("Fallback page size to %ld", page_size); |
| 2520 | } |
| 2521 | |
| 2522 | ret = sessiond_config_init(&config); |
| 2523 | if (ret) { |
| 2524 | retval = -1; |
| 2525 | goto exit_set_signal_handler; |
| 2526 | } |
| 2527 | |
| 2528 | /* |
| 2529 | * Init config from environment variables. |
| 2530 | * Command line option override env configuration per-doc. Do env first. |
| 2531 | */ |
| 2532 | sessiond_config_apply_env_config(&config); |
| 2533 | |
| 2534 | /* |
| 2535 | * Parse arguments and load the daemon configuration file. |
| 2536 | * |
| 2537 | * We have an exit_options exit path to free memory reserved by |
| 2538 | * set_options. This is needed because the rest of sessiond_cleanup() |
| 2539 | * depends on ht_cleanup_thread, which depends on lttng_daemonize, which |
| 2540 | * depends on set_options. |
| 2541 | */ |
| 2542 | progname = argv[0]; |
| 2543 | if (set_options(argc, argv)) { |
| 2544 | retval = -1; |
| 2545 | goto exit_options; |
| 2546 | } |
| 2547 | |
| 2548 | /* |
| 2549 | * Resolve all paths received as arguments, configuration option, or |
| 2550 | * through environment variable as absolute paths. This is necessary |
| 2551 | * since daemonizing causes the sessiond's current working directory |
| 2552 | * to '/'. |
| 2553 | */ |
| 2554 | ret = sessiond_config_resolve_paths(&config); |
| 2555 | if (ret) { |
| 2556 | goto exit_options; |
| 2557 | } |
| 2558 | |
| 2559 | /* Apply config. */ |
| 2560 | lttng_opt_verbose = config.verbose; |
| 2561 | lttng_opt_quiet = config.quiet; |
| 2562 | kconsumer_data.err_unix_sock_path = |
| 2563 | config.kconsumerd_err_unix_sock_path.value; |
| 2564 | kconsumer_data.cmd_unix_sock_path = |
| 2565 | config.kconsumerd_cmd_unix_sock_path.value; |
| 2566 | ustconsumer32_data.err_unix_sock_path = |
| 2567 | config.consumerd32_err_unix_sock_path.value; |
| 2568 | ustconsumer32_data.cmd_unix_sock_path = |
| 2569 | config.consumerd32_cmd_unix_sock_path.value; |
| 2570 | ustconsumer64_data.err_unix_sock_path = |
| 2571 | config.consumerd64_err_unix_sock_path.value; |
| 2572 | ustconsumer64_data.cmd_unix_sock_path = |
| 2573 | config.consumerd64_cmd_unix_sock_path.value; |
| 2574 | set_clock_plugin_env(); |
| 2575 | |
| 2576 | sessiond_config_log(&config); |
| 2577 | |
| 2578 | if (create_lttng_rundir()) { |
| 2579 | retval = -1; |
| 2580 | goto exit_options; |
| 2581 | } |
| 2582 | |
| 2583 | /* Abort launch if a session daemon is already running. */ |
| 2584 | if (check_existing_daemon()) { |
| 2585 | ERR("A session daemon is already running."); |
| 2586 | retval = -1; |
| 2587 | goto exit_options; |
| 2588 | } |
| 2589 | |
| 2590 | /* Daemonize */ |
| 2591 | if (config.daemonize || config.background) { |
| 2592 | int i; |
| 2593 | |
| 2594 | ret = lttng_daemonize(&child_ppid, &recv_child_signal, |
| 2595 | !config.background); |
| 2596 | if (ret < 0) { |
| 2597 | retval = -1; |
| 2598 | goto exit_options; |
| 2599 | } |
| 2600 | |
| 2601 | /* |
| 2602 | * We are in the child. Make sure all other file descriptors are |
| 2603 | * closed, in case we are called with more opened file |
| 2604 | * descriptors than the standard ones and the lock file. |
| 2605 | */ |
| 2606 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { |
| 2607 | if (i == lockfile_fd) { |
| 2608 | continue; |
| 2609 | } |
| 2610 | (void) close(i); |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | if (run_as_create_worker(argv[0]) < 0) { |
| 2615 | goto exit_create_run_as_worker_cleanup; |
| 2616 | } |
| 2617 | |
| 2618 | /* |
| 2619 | * Starting from here, we can create threads. This needs to be after |
| 2620 | * lttng_daemonize due to RCU. |
| 2621 | */ |
| 2622 | |
| 2623 | /* |
| 2624 | * Initialize the health check subsystem. This call should set the |
| 2625 | * appropriate time values. |
| 2626 | */ |
| 2627 | health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES); |
| 2628 | if (!health_sessiond) { |
| 2629 | PERROR("health_app_create error"); |
| 2630 | retval = -1; |
| 2631 | goto exit_health_sessiond_cleanup; |
| 2632 | } |
| 2633 | |
| 2634 | /* Create thread to clean up RCU hash tables */ |
| 2635 | ht_cleanup_thread = launch_ht_cleanup_thread(); |
| 2636 | if (!ht_cleanup_thread) { |
| 2637 | retval = -1; |
| 2638 | goto exit_ht_cleanup; |
| 2639 | } |
| 2640 | |
| 2641 | /* Create thread quit pipe */ |
| 2642 | if (sessiond_init_thread_quit_pipe()) { |
| 2643 | retval = -1; |
| 2644 | goto exit_init_data; |
| 2645 | } |
| 2646 | |
| 2647 | /* Check if daemon is UID = 0 */ |
| 2648 | is_root = !getuid(); |
| 2649 | if (is_root) { |
| 2650 | /* Create global run dir with root access */ |
| 2651 | |
| 2652 | kernel_channel_monitor_pipe = lttng_pipe_open(0); |
| 2653 | if (!kernel_channel_monitor_pipe) { |
| 2654 | ERR("Failed to create kernel consumer channel monitor pipe"); |
| 2655 | retval = -1; |
| 2656 | goto exit_init_data; |
| 2657 | } |
| 2658 | kconsumer_data.channel_monitor_pipe = |
| 2659 | lttng_pipe_release_writefd( |
| 2660 | kernel_channel_monitor_pipe); |
| 2661 | if (kconsumer_data.channel_monitor_pipe < 0) { |
| 2662 | retval = -1; |
| 2663 | goto exit_init_data; |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | /* Set consumer initial state */ |
| 2668 | kernel_consumerd_state = CONSUMER_STOPPED; |
| 2669 | ust_consumerd_state = CONSUMER_STOPPED; |
| 2670 | |
| 2671 | ust32_channel_monitor_pipe = lttng_pipe_open(0); |
| 2672 | if (!ust32_channel_monitor_pipe) { |
| 2673 | ERR("Failed to create 32-bit user space consumer channel monitor pipe"); |
| 2674 | retval = -1; |
| 2675 | goto exit_init_data; |
| 2676 | } |
| 2677 | ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd( |
| 2678 | ust32_channel_monitor_pipe); |
| 2679 | if (ustconsumer32_data.channel_monitor_pipe < 0) { |
| 2680 | retval = -1; |
| 2681 | goto exit_init_data; |
| 2682 | } |
| 2683 | |
| 2684 | /* |
| 2685 | * The rotation_thread_timer_queue structure is shared between the |
| 2686 | * sessiond timer thread and the rotation thread. The main thread keeps |
| 2687 | * its ownership and destroys it when both threads have been joined. |
| 2688 | */ |
| 2689 | rotation_timer_queue = rotation_thread_timer_queue_create(); |
| 2690 | if (!rotation_timer_queue) { |
| 2691 | retval = -1; |
| 2692 | goto exit_init_data; |
| 2693 | } |
| 2694 | timer_thread_parameters.rotation_thread_job_queue = |
| 2695 | rotation_timer_queue; |
| 2696 | |
| 2697 | ust64_channel_monitor_pipe = lttng_pipe_open(0); |
| 2698 | if (!ust64_channel_monitor_pipe) { |
| 2699 | ERR("Failed to create 64-bit user space consumer channel monitor pipe"); |
| 2700 | retval = -1; |
| 2701 | goto exit_init_data; |
| 2702 | } |
| 2703 | ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd( |
| 2704 | ust64_channel_monitor_pipe); |
| 2705 | if (ustconsumer64_data.channel_monitor_pipe < 0) { |
| 2706 | retval = -1; |
| 2707 | goto exit_init_data; |
| 2708 | } |
| 2709 | |
| 2710 | /* |
| 2711 | * Init UST app hash table. Alloc hash table before this point since |
| 2712 | * cleanup() can get called after that point. |
| 2713 | */ |
| 2714 | if (ust_app_ht_alloc()) { |
| 2715 | ERR("Failed to allocate UST app hash table"); |
| 2716 | retval = -1; |
| 2717 | goto exit_init_data; |
| 2718 | } |
| 2719 | |
| 2720 | /* |
| 2721 | * Initialize agent app hash table. We allocate the hash table here |
| 2722 | * since cleanup() can get called after this point. |
| 2723 | */ |
| 2724 | if (agent_app_ht_alloc()) { |
| 2725 | ERR("Failed to allocate Agent app hash table"); |
| 2726 | retval = -1; |
| 2727 | goto exit_init_data; |
| 2728 | } |
| 2729 | |
| 2730 | /* |
| 2731 | * These actions must be executed as root. We do that *after* setting up |
| 2732 | * the sockets path because we MUST make the check for another daemon using |
| 2733 | * those paths *before* trying to set the kernel consumer sockets and init |
| 2734 | * kernel tracer. |
| 2735 | */ |
| 2736 | if (is_root) { |
| 2737 | if (set_consumer_sockets(&kconsumer_data)) { |
| 2738 | retval = -1; |
| 2739 | goto exit_init_data; |
| 2740 | } |
| 2741 | |
| 2742 | /* Setup kernel tracer */ |
| 2743 | if (!config.no_kernel) { |
| 2744 | init_kernel_tracer(); |
| 2745 | if (kernel_tracer_fd >= 0) { |
| 2746 | ret = syscall_init_table(); |
| 2747 | if (ret < 0) { |
| 2748 | ERR("Unable to populate syscall table. " |
| 2749 | "Syscall tracing won't work " |
| 2750 | "for this session daemon."); |
| 2751 | } |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | /* Set ulimit for open files */ |
| 2756 | set_ulimit(); |
| 2757 | } |
| 2758 | /* init lttng_fd tracking must be done after set_ulimit. */ |
| 2759 | lttng_fd_init(); |
| 2760 | |
| 2761 | if (set_consumer_sockets(&ustconsumer64_data)) { |
| 2762 | retval = -1; |
| 2763 | goto exit_init_data; |
| 2764 | } |
| 2765 | |
| 2766 | if (set_consumer_sockets(&ustconsumer32_data)) { |
| 2767 | retval = -1; |
| 2768 | goto exit_init_data; |
| 2769 | } |
| 2770 | |
| 2771 | /* Setup the needed unix socket */ |
| 2772 | if (init_daemon_socket()) { |
| 2773 | retval = -1; |
| 2774 | goto exit_init_data; |
| 2775 | } |
| 2776 | |
| 2777 | /* Set credentials to socket */ |
| 2778 | if (is_root && set_permissions(config.rundir.value)) { |
| 2779 | retval = -1; |
| 2780 | goto exit_init_data; |
| 2781 | } |
| 2782 | |
| 2783 | /* Get parent pid if -S, --sig-parent is specified. */ |
| 2784 | if (config.sig_parent) { |
| 2785 | ppid = getppid(); |
| 2786 | } |
| 2787 | |
| 2788 | /* Setup the kernel pipe for waking up the kernel thread */ |
| 2789 | if (is_root && !config.no_kernel) { |
| 2790 | if (utils_create_pipe_cloexec(kernel_poll_pipe)) { |
| 2791 | retval = -1; |
| 2792 | goto exit_init_data; |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | /* Setup the thread apps communication pipe. */ |
| 2797 | if (utils_create_pipe_cloexec(apps_cmd_pipe)) { |
| 2798 | retval = -1; |
| 2799 | goto exit_init_data; |
| 2800 | } |
| 2801 | |
| 2802 | /* Setup the thread apps notify communication pipe. */ |
| 2803 | if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) { |
| 2804 | retval = -1; |
| 2805 | goto exit_init_data; |
| 2806 | } |
| 2807 | |
| 2808 | /* Initialize global buffer per UID and PID registry. */ |
| 2809 | buffer_reg_init_uid_registry(); |
| 2810 | buffer_reg_init_pid_registry(); |
| 2811 | |
| 2812 | /* Init UST command queue. */ |
| 2813 | cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail); |
| 2814 | |
| 2815 | cmd_init(); |
| 2816 | |
| 2817 | /* Check for the application socket timeout env variable. */ |
| 2818 | env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV); |
| 2819 | if (env_app_timeout) { |
| 2820 | config.app_socket_timeout = atoi(env_app_timeout); |
| 2821 | } else { |
| 2822 | config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT; |
| 2823 | } |
| 2824 | |
| 2825 | ret = write_pidfile(); |
| 2826 | if (ret) { |
| 2827 | ERR("Error in write_pidfile"); |
| 2828 | retval = -1; |
| 2829 | goto exit_init_data; |
| 2830 | } |
| 2831 | |
| 2832 | /* Initialize communication library */ |
| 2833 | lttcomm_init(); |
| 2834 | /* Initialize TCP timeout values */ |
| 2835 | lttcomm_inet_init(); |
| 2836 | |
| 2837 | if (load_session_init_data(&load_info) < 0) { |
| 2838 | retval = -1; |
| 2839 | goto exit_init_data; |
| 2840 | } |
| 2841 | load_info->path = config.load_session_path.value; |
| 2842 | |
| 2843 | /* Create health-check thread. */ |
| 2844 | if (!launch_health_management_thread()) { |
| 2845 | retval = -1; |
| 2846 | goto exit_health; |
| 2847 | } |
| 2848 | |
| 2849 | /* notification_thread_data acquires the pipes' read side. */ |
| 2850 | notification_thread_handle = notification_thread_handle_create( |
| 2851 | ust32_channel_monitor_pipe, |
| 2852 | ust64_channel_monitor_pipe, |
| 2853 | kernel_channel_monitor_pipe); |
| 2854 | if (!notification_thread_handle) { |
| 2855 | retval = -1; |
| 2856 | ERR("Failed to create notification thread shared data"); |
| 2857 | goto exit_notification; |
| 2858 | } |
| 2859 | |
| 2860 | /* Create notification thread. */ |
| 2861 | if (!launch_notification_thread(notification_thread_handle)) { |
| 2862 | retval = -1; |
| 2863 | goto exit_notification; |
| 2864 | } |
| 2865 | |
| 2866 | /* Create timer thread. */ |
| 2867 | if (!launch_timer_thread(&timer_thread_parameters)) { |
| 2868 | retval = -1; |
| 2869 | goto exit_notification; |
| 2870 | } |
| 2871 | |
| 2872 | /* rotation_thread_data acquires the pipes' read side. */ |
| 2873 | rotation_thread_handle = rotation_thread_handle_create( |
| 2874 | rotation_timer_queue, |
| 2875 | notification_thread_handle); |
| 2876 | if (!rotation_thread_handle) { |
| 2877 | retval = -1; |
| 2878 | ERR("Failed to create rotation thread shared data"); |
| 2879 | stop_threads(); |
| 2880 | goto exit_rotation; |
| 2881 | } |
| 2882 | |
| 2883 | /* Create rotation thread. */ |
| 2884 | if (!launch_rotation_thread(rotation_thread_handle)) { |
| 2885 | retval = -1; |
| 2886 | goto exit_rotation; |
| 2887 | } |
| 2888 | |
| 2889 | /* Create thread to manage the client socket */ |
| 2890 | client_thread = launch_client_thread(); |
| 2891 | if (!client_thread) { |
| 2892 | retval = -1; |
| 2893 | goto exit_client; |
| 2894 | } |
| 2895 | |
| 2896 | if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1], |
| 2897 | apps_cmd_notify_pipe[1])) { |
| 2898 | retval = -1; |
| 2899 | goto exit_dispatch; |
| 2900 | } |
| 2901 | |
| 2902 | /* Create thread to manage application registration. */ |
| 2903 | ret = pthread_create(®_apps_thread, default_pthread_attr(), |
| 2904 | thread_registration_apps, (void *) NULL); |
| 2905 | if (ret) { |
| 2906 | errno = ret; |
| 2907 | PERROR("pthread_create registration"); |
| 2908 | retval = -1; |
| 2909 | stop_threads(); |
| 2910 | goto exit_reg_apps; |
| 2911 | } |
| 2912 | |
| 2913 | /* Create thread to manage application socket */ |
| 2914 | ret = pthread_create(&apps_thread, default_pthread_attr(), |
| 2915 | thread_manage_apps, (void *) NULL); |
| 2916 | if (ret) { |
| 2917 | errno = ret; |
| 2918 | PERROR("pthread_create apps"); |
| 2919 | retval = -1; |
| 2920 | stop_threads(); |
| 2921 | goto exit_apps; |
| 2922 | } |
| 2923 | |
| 2924 | /* Create thread to manage application notify socket */ |
| 2925 | ret = pthread_create(&apps_notify_thread, default_pthread_attr(), |
| 2926 | ust_thread_manage_notify, (void *) NULL); |
| 2927 | if (ret) { |
| 2928 | errno = ret; |
| 2929 | PERROR("pthread_create notify"); |
| 2930 | retval = -1; |
| 2931 | stop_threads(); |
| 2932 | goto exit_apps_notify; |
| 2933 | } |
| 2934 | |
| 2935 | /* Create agent registration thread. */ |
| 2936 | ret = pthread_create(&agent_reg_thread, default_pthread_attr(), |
| 2937 | agent_thread_manage_registration, (void *) NULL); |
| 2938 | if (ret) { |
| 2939 | errno = ret; |
| 2940 | PERROR("pthread_create agent"); |
| 2941 | retval = -1; |
| 2942 | stop_threads(); |
| 2943 | goto exit_agent_reg; |
| 2944 | } |
| 2945 | |
| 2946 | /* Don't start this thread if kernel tracing is not requested nor root */ |
| 2947 | if (is_root && !config.no_kernel) { |
| 2948 | /* Create kernel thread to manage kernel event */ |
| 2949 | ret = pthread_create(&kernel_thread, default_pthread_attr(), |
| 2950 | thread_manage_kernel, (void *) NULL); |
| 2951 | if (ret) { |
| 2952 | errno = ret; |
| 2953 | PERROR("pthread_create kernel"); |
| 2954 | retval = -1; |
| 2955 | stop_threads(); |
| 2956 | goto exit_kernel; |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | /* Create session loading thread. */ |
| 2961 | ret = pthread_create(&load_session_thread, default_pthread_attr(), |
| 2962 | thread_load_session, load_info); |
| 2963 | if (ret) { |
| 2964 | errno = ret; |
| 2965 | PERROR("pthread_create load_session_thread"); |
| 2966 | retval = -1; |
| 2967 | stop_threads(); |
| 2968 | goto exit_load_session; |
| 2969 | } |
| 2970 | |
| 2971 | /* |
| 2972 | * This is where we start awaiting program completion (e.g. through |
| 2973 | * signal that asks threads to teardown). |
| 2974 | */ |
| 2975 | |
| 2976 | ret = pthread_join(load_session_thread, &status); |
| 2977 | if (ret) { |
| 2978 | errno = ret; |
| 2979 | PERROR("pthread_join load_session_thread"); |
| 2980 | retval = -1; |
| 2981 | } |
| 2982 | |
| 2983 | /* Initiate teardown once activity occurs on the quit pipe. */ |
| 2984 | sessiond_wait_for_quit_pipe(-1U); |
| 2985 | |
| 2986 | /* |
| 2987 | * Ensure that the client thread is no longer accepting new commands, |
| 2988 | * which could cause new sessions to be created. |
| 2989 | */ |
| 2990 | if (!lttng_thread_shutdown(client_thread)) { |
| 2991 | ERR("Failed to shutdown the client thread, continuing teardown"); |
| 2992 | lttng_thread_put(client_thread); |
| 2993 | client_thread = NULL; |
| 2994 | } |
| 2995 | |
| 2996 | destroy_all_sessions_and_wait(); |
| 2997 | exit_load_session: |
| 2998 | |
| 2999 | if (is_root && !config.no_kernel) { |
| 3000 | ret = pthread_join(kernel_thread, &status); |
| 3001 | if (ret) { |
| 3002 | errno = ret; |
| 3003 | PERROR("pthread_join"); |
| 3004 | retval = -1; |
| 3005 | } |
| 3006 | } |
| 3007 | exit_kernel: |
| 3008 | |
| 3009 | ret = pthread_join(agent_reg_thread, &status); |
| 3010 | if (ret) { |
| 3011 | errno = ret; |
| 3012 | PERROR("pthread_join agent"); |
| 3013 | retval = -1; |
| 3014 | } |
| 3015 | exit_agent_reg: |
| 3016 | |
| 3017 | ret = pthread_join(apps_notify_thread, &status); |
| 3018 | if (ret) { |
| 3019 | errno = ret; |
| 3020 | PERROR("pthread_join apps notify"); |
| 3021 | retval = -1; |
| 3022 | } |
| 3023 | exit_apps_notify: |
| 3024 | |
| 3025 | ret = pthread_join(apps_thread, &status); |
| 3026 | if (ret) { |
| 3027 | errno = ret; |
| 3028 | PERROR("pthread_join apps"); |
| 3029 | retval = -1; |
| 3030 | } |
| 3031 | exit_apps: |
| 3032 | |
| 3033 | ret = pthread_join(reg_apps_thread, &status); |
| 3034 | if (ret) { |
| 3035 | errno = ret; |
| 3036 | PERROR("pthread_join"); |
| 3037 | retval = -1; |
| 3038 | } |
| 3039 | exit_reg_apps: |
| 3040 | exit_dispatch: |
| 3041 | exit_client: |
| 3042 | exit_rotation: |
| 3043 | exit_notification: |
| 3044 | lttng_thread_list_shutdown_orphans(); |
| 3045 | exit_health: |
| 3046 | exit_init_data: |
| 3047 | if (client_thread) { |
| 3048 | lttng_thread_put(client_thread); |
| 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | * Wait for all pending call_rcu work to complete before tearing |
| 3053 | * down data structures. call_rcu worker may be trying to |
| 3054 | * perform lookups in those structures. |
| 3055 | */ |
| 3056 | rcu_barrier(); |
| 3057 | /* |
| 3058 | * sessiond_cleanup() is called when no other thread is running, except |
| 3059 | * the ht_cleanup thread, which is needed to destroy the hash tables. |
| 3060 | */ |
| 3061 | rcu_thread_online(); |
| 3062 | sessiond_cleanup(); |
| 3063 | |
| 3064 | /* |
| 3065 | * Ensure all prior call_rcu are done. call_rcu callbacks may push |
| 3066 | * hash tables to the ht_cleanup thread. Therefore, we ensure that |
| 3067 | * the queue is empty before shutting down the clean-up thread. |
| 3068 | */ |
| 3069 | rcu_barrier(); |
| 3070 | |
| 3071 | if (ht_cleanup_thread) { |
| 3072 | lttng_thread_shutdown(ht_cleanup_thread); |
| 3073 | lttng_thread_put(ht_cleanup_thread); |
| 3074 | } |
| 3075 | |
| 3076 | rcu_thread_offline(); |
| 3077 | rcu_unregister_thread(); |
| 3078 | |
| 3079 | if (rotation_thread_handle) { |
| 3080 | rotation_thread_handle_destroy(rotation_thread_handle); |
| 3081 | } |
| 3082 | |
| 3083 | /* |
| 3084 | * After the rotation and timer thread have quit, we can safely destroy |
| 3085 | * the rotation_timer_queue. |
| 3086 | */ |
| 3087 | rotation_thread_timer_queue_destroy(rotation_timer_queue); |
| 3088 | /* |
| 3089 | * The teardown of the notification system is performed after the |
| 3090 | * session daemon's teardown in order to allow it to be notified |
| 3091 | * of the active session and channels at the moment of the teardown. |
| 3092 | */ |
| 3093 | if (notification_thread_handle) { |
| 3094 | notification_thread_handle_destroy(notification_thread_handle); |
| 3095 | } |
| 3096 | lttng_pipe_destroy(ust32_channel_monitor_pipe); |
| 3097 | lttng_pipe_destroy(ust64_channel_monitor_pipe); |
| 3098 | lttng_pipe_destroy(kernel_channel_monitor_pipe); |
| 3099 | exit_ht_cleanup: |
| 3100 | |
| 3101 | health_app_destroy(health_sessiond); |
| 3102 | exit_health_sessiond_cleanup: |
| 3103 | exit_create_run_as_worker_cleanup: |
| 3104 | |
| 3105 | exit_options: |
| 3106 | sessiond_cleanup_lock_file(); |
| 3107 | sessiond_cleanup_options(); |
| 3108 | |
| 3109 | exit_set_signal_handler: |
| 3110 | if (!retval) { |
| 3111 | exit(EXIT_SUCCESS); |
| 3112 | } else { |
| 3113 | exit(EXIT_FAILURE); |
| 3114 | } |
| 3115 | } |