| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include "fd-limit.hpp" |
| 11 | #include "health-sessiond.hpp" |
| 12 | #include "lttng-sessiond.hpp" |
| 13 | #include "register.hpp" |
| 14 | #include "testpoint.hpp" |
| 15 | #include "thread.hpp" |
| 16 | #include "utils.hpp" |
| 17 | |
| 18 | #include <common/futex.hpp> |
| 19 | #include <common/macros.hpp> |
| 20 | #include <common/shm.hpp> |
| 21 | #include <common/utils.hpp> |
| 22 | |
| 23 | #include <stddef.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <urcu.h> |
| 27 | |
| 28 | namespace { |
| 29 | struct thread_state { |
| 30 | struct lttng_pipe *quit_pipe; |
| 31 | struct ust_cmd_queue *ust_cmd_queue; |
| 32 | sem_t ready; |
| 33 | bool running; |
| 34 | int application_socket; |
| 35 | }; |
| 36 | } /* namespace */ |
| 37 | |
| 38 | /* |
| 39 | * Creates the application socket. |
| 40 | */ |
| 41 | static int create_application_socket() |
| 42 | { |
| 43 | int ret = 0; |
| 44 | int apps_sock; |
| 45 | |
| 46 | /* Create the application unix socket */ |
| 47 | apps_sock = lttcomm_create_unix_sock(the_config.apps_unix_sock_path.value); |
| 48 | if (apps_sock < 0) { |
| 49 | ERR("Create unix sock failed: %s", the_config.apps_unix_sock_path.value); |
| 50 | ret = -1; |
| 51 | goto end; |
| 52 | } |
| 53 | |
| 54 | /* Set the cloexec flag */ |
| 55 | ret = utils_set_fd_cloexec(apps_sock); |
| 56 | if (ret < 0) { |
| 57 | ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). " |
| 58 | "Continuing but note that the consumer daemon will have a " |
| 59 | "reference to this socket on exec()", |
| 60 | apps_sock); |
| 61 | } |
| 62 | |
| 63 | /* File permission MUST be 666 */ |
| 64 | ret = chmod(the_config.apps_unix_sock_path.value, |
| 65 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 66 | if (ret < 0) { |
| 67 | PERROR("Set file permissions failed on %s", the_config.apps_unix_sock_path.value); |
| 68 | goto error_close_socket; |
| 69 | } |
| 70 | |
| 71 | DBG3("Session daemon application socket created (fd = %d) ", apps_sock); |
| 72 | ret = apps_sock; |
| 73 | end: |
| 74 | return ret; |
| 75 | error_close_socket: |
| 76 | if (close(apps_sock)) { |
| 77 | PERROR("Failed to close application socket in error path"); |
| 78 | } |
| 79 | apps_sock = -1; |
| 80 | ret = -1; |
| 81 | goto end; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Notify UST applications using the shm mmap futex. |
| 86 | */ |
| 87 | static int notify_ust_apps(int active, bool is_root) |
| 88 | { |
| 89 | char *wait_shm_mmap; |
| 90 | |
| 91 | DBG("Notifying applications of session daemon state: %d", active); |
| 92 | |
| 93 | /* See shm.c for this call implying mmap, shm and futex calls */ |
| 94 | wait_shm_mmap = shm_ust_get_mmap(the_config.wait_shm_path.value, is_root); |
| 95 | if (wait_shm_mmap == nullptr) { |
| 96 | goto error; |
| 97 | } |
| 98 | |
| 99 | /* Wake waiting process */ |
| 100 | futex_wait_update((int32_t *) wait_shm_mmap, active); |
| 101 | |
| 102 | /* Apps notified successfully */ |
| 103 | return 0; |
| 104 | |
| 105 | error: |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | static void cleanup_application_registration_thread(void *data) |
| 110 | { |
| 111 | struct thread_state *thread_state = (struct thread_state *) data; |
| 112 | |
| 113 | if (!data) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | lttng_pipe_destroy(thread_state->quit_pipe); |
| 118 | free(thread_state); |
| 119 | } |
| 120 | |
| 121 | static void set_thread_status(struct thread_state *thread_state, bool running) |
| 122 | { |
| 123 | DBG("Marking application registration thread's state as %s", running ? "running" : "error"); |
| 124 | thread_state->running = running; |
| 125 | sem_post(&thread_state->ready); |
| 126 | } |
| 127 | |
| 128 | static bool wait_thread_status(struct thread_state *thread_state) |
| 129 | { |
| 130 | DBG("Waiting for application registration thread to be ready"); |
| 131 | sem_wait(&thread_state->ready); |
| 132 | if (thread_state->running) { |
| 133 | DBG("Application registration thread is ready"); |
| 134 | } else { |
| 135 | ERR("Initialization of application registration thread failed"); |
| 136 | } |
| 137 | |
| 138 | return thread_state->running; |
| 139 | } |
| 140 | |
| 141 | static void thread_init_cleanup(void *data) |
| 142 | { |
| 143 | struct thread_state *thread_state = (struct thread_state *) data; |
| 144 | |
| 145 | set_thread_status(thread_state, false); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * This thread manage application registration. |
| 150 | */ |
| 151 | static void *thread_application_registration(void *data) |
| 152 | { |
| 153 | int sock = -1, i, ret, err = -1; |
| 154 | uint32_t nb_fd; |
| 155 | struct lttng_poll_event events; |
| 156 | /* |
| 157 | * Gets allocated in this thread, enqueued to a global queue, dequeued |
| 158 | * and freed in the manage apps thread. |
| 159 | */ |
| 160 | struct ust_command *ust_cmd = nullptr; |
| 161 | const bool is_root = (getuid() == 0); |
| 162 | struct thread_state *thread_state = (struct thread_state *) data; |
| 163 | const int application_socket = thread_state->application_socket; |
| 164 | const auto thread_quit_pipe_fd = lttng_pipe_get_readfd(thread_state->quit_pipe); |
| 165 | |
| 166 | DBG("[thread] Manage application registration started"); |
| 167 | |
| 168 | pthread_cleanup_push(thread_init_cleanup, thread_state); |
| 169 | health_register(the_health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG); |
| 170 | |
| 171 | ret = lttcomm_listen_unix_sock(application_socket); |
| 172 | if (ret < 0) { |
| 173 | goto error_listen; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Pass 2 as size here for the thread quit pipe and apps_sock. Nothing |
| 178 | * more will be added to this poll set. |
| 179 | */ |
| 180 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); |
| 181 | if (ret < 0) { |
| 182 | goto error_create_poll; |
| 183 | } |
| 184 | |
| 185 | /* Add the application registration socket */ |
| 186 | ret = lttng_poll_add(&events, application_socket, LPOLLIN | LPOLLRDHUP); |
| 187 | if (ret < 0) { |
| 188 | goto error_poll_add; |
| 189 | } |
| 190 | |
| 191 | /* Add the application registration socket */ |
| 192 | ret = lttng_poll_add(&events, thread_quit_pipe_fd, LPOLLIN | LPOLLRDHUP); |
| 193 | if (ret < 0) { |
| 194 | goto error_poll_add; |
| 195 | } |
| 196 | |
| 197 | set_thread_status(thread_state, true); |
| 198 | pthread_cleanup_pop(0); |
| 199 | |
| 200 | if (testpoint(sessiond_thread_registration_apps)) { |
| 201 | goto error_poll_add; |
| 202 | } |
| 203 | |
| 204 | while (true) { |
| 205 | DBG("Accepting application registration"); |
| 206 | |
| 207 | /* Inifinite blocking call, waiting for transmission */ |
| 208 | restart: |
| 209 | health_poll_entry(); |
| 210 | ret = lttng_poll_wait(&events, -1); |
| 211 | health_poll_exit(); |
| 212 | if (ret < 0) { |
| 213 | /* |
| 214 | * Restart interrupted system call. |
| 215 | */ |
| 216 | if (errno == EINTR) { |
| 217 | goto restart; |
| 218 | } |
| 219 | goto error; |
| 220 | } |
| 221 | |
| 222 | nb_fd = ret; |
| 223 | |
| 224 | for (i = 0; i < nb_fd; i++) { |
| 225 | health_code_update(); |
| 226 | |
| 227 | /* Fetch once the poll data */ |
| 228 | const auto revents = LTTNG_POLL_GETEV(&events, i); |
| 229 | const auto pollfd = LTTNG_POLL_GETFD(&events, i); |
| 230 | |
| 231 | /* Activity on thread quit pipe, closing. */ |
| 232 | if (pollfd == thread_quit_pipe_fd) { |
| 233 | err = 0; |
| 234 | goto exit; |
| 235 | } |
| 236 | |
| 237 | /* Event on the registration socket. */ |
| 238 | if (revents & LPOLLIN) { |
| 239 | sock = lttcomm_accept_unix_sock(application_socket); |
| 240 | if (sock < 0) { |
| 241 | goto error; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Set socket timeout for both receiving and ending. |
| 246 | * app_socket_timeout is in seconds, whereas |
| 247 | * lttcomm_setsockopt_rcv_timeout and |
| 248 | * lttcomm_setsockopt_snd_timeout expect msec as |
| 249 | * parameter. |
| 250 | */ |
| 251 | if (the_config.app_socket_timeout >= 0) { |
| 252 | (void) lttcomm_setsockopt_rcv_timeout( |
| 253 | sock, the_config.app_socket_timeout * 1000); |
| 254 | (void) lttcomm_setsockopt_snd_timeout( |
| 255 | sock, the_config.app_socket_timeout * 1000); |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Set the CLOEXEC flag. Return code is useless because |
| 260 | * either way, the show must go on. |
| 261 | */ |
| 262 | (void) utils_set_fd_cloexec(sock); |
| 263 | |
| 264 | /* Create UST registration command for enqueuing */ |
| 265 | ust_cmd = zmalloc<ust_command>(); |
| 266 | if (ust_cmd == nullptr) { |
| 267 | PERROR("ust command zmalloc"); |
| 268 | ret = close(sock); |
| 269 | if (ret) { |
| 270 | PERROR("close"); |
| 271 | } |
| 272 | sock = -1; |
| 273 | goto error; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Using message-based transmissions to ensure we don't |
| 278 | * have to deal with partially received messages. |
| 279 | */ |
| 280 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
| 281 | if (ret < 0) { |
| 282 | ERR("Exhausted file descriptors allowed for applications."); |
| 283 | free(ust_cmd); |
| 284 | ret = close(sock); |
| 285 | if (ret) { |
| 286 | PERROR("close"); |
| 287 | } |
| 288 | sock = -1; |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | health_code_update(); |
| 293 | ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg); |
| 294 | if (ret < 0) { |
| 295 | free(ust_cmd); |
| 296 | /* Close socket of the application. */ |
| 297 | ret = close(sock); |
| 298 | if (ret) { |
| 299 | PERROR("close"); |
| 300 | } |
| 301 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 302 | sock = -1; |
| 303 | continue; |
| 304 | } |
| 305 | health_code_update(); |
| 306 | |
| 307 | ust_cmd->sock = sock; |
| 308 | sock = -1; |
| 309 | |
| 310 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
| 311 | " gid:%d sock:%d name:%s (version %d.%d)", |
| 312 | ust_cmd->reg_msg.pid, |
| 313 | ust_cmd->reg_msg.ppid, |
| 314 | ust_cmd->reg_msg.uid, |
| 315 | ust_cmd->reg_msg.gid, |
| 316 | ust_cmd->sock, |
| 317 | ust_cmd->reg_msg.name, |
| 318 | ust_cmd->reg_msg.major, |
| 319 | ust_cmd->reg_msg.minor); |
| 320 | |
| 321 | /* |
| 322 | * Lock free enqueue the registration request. The red pill |
| 323 | * has been taken! This apps will be part of the *system*. |
| 324 | */ |
| 325 | cds_wfcq_head_ptr_t head; |
| 326 | head.h = &thread_state->ust_cmd_queue->head; |
| 327 | cds_wfcq_enqueue( |
| 328 | head, &thread_state->ust_cmd_queue->tail, &ust_cmd->node); |
| 329 | |
| 330 | /* |
| 331 | * Wake the registration queue futex. Implicit memory |
| 332 | * barrier with the exchange in cds_wfcq_enqueue. |
| 333 | */ |
| 334 | futex_nto1_wake(&thread_state->ust_cmd_queue->futex); |
| 335 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 336 | ERR("Register apps socket poll error"); |
| 337 | goto error; |
| 338 | } else { |
| 339 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 340 | goto error; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | exit: |
| 346 | error: |
| 347 | /* Notify that the registration thread is gone */ |
| 348 | notify_ust_apps(0, is_root); |
| 349 | |
| 350 | ret = close(application_socket); |
| 351 | if (ret) { |
| 352 | PERROR("Failed to close application registration socket"); |
| 353 | } |
| 354 | if (sock >= 0) { |
| 355 | ret = close(sock); |
| 356 | if (ret) { |
| 357 | PERROR("Failed to close application socket"); |
| 358 | } |
| 359 | lttng_fd_put(LTTNG_FD_APPS, 1); |
| 360 | } |
| 361 | unlink(the_config.apps_unix_sock_path.value); |
| 362 | |
| 363 | error_poll_add: |
| 364 | lttng_poll_clean(&events); |
| 365 | error_listen: |
| 366 | error_create_poll: |
| 367 | DBG("UST Registration thread cleanup complete"); |
| 368 | if (err) { |
| 369 | health_error(); |
| 370 | ERR("Health error occurred in %s", __func__); |
| 371 | } |
| 372 | health_unregister(the_health_sessiond); |
| 373 | return nullptr; |
| 374 | } |
| 375 | |
| 376 | static bool shutdown_application_registration_thread(void *data) |
| 377 | { |
| 378 | struct thread_state *thread_state = (struct thread_state *) data; |
| 379 | const int write_fd = lttng_pipe_get_writefd(thread_state->quit_pipe); |
| 380 | |
| 381 | return notify_thread_pipe(write_fd) == 1; |
| 382 | } |
| 383 | |
| 384 | struct lttng_thread *launch_application_registration_thread(struct ust_cmd_queue *cmd_queue) |
| 385 | { |
| 386 | int ret; |
| 387 | struct lttng_pipe *quit_pipe; |
| 388 | struct thread_state *thread_state = nullptr; |
| 389 | struct lttng_thread *thread = nullptr; |
| 390 | const bool is_root = (getuid() == 0); |
| 391 | int application_socket = -1; |
| 392 | |
| 393 | thread_state = zmalloc<struct thread_state>(); |
| 394 | if (!thread_state) { |
| 395 | goto error_alloc; |
| 396 | } |
| 397 | quit_pipe = lttng_pipe_open(FD_CLOEXEC); |
| 398 | if (!quit_pipe) { |
| 399 | goto error; |
| 400 | } |
| 401 | thread_state->quit_pipe = quit_pipe; |
| 402 | thread_state->ust_cmd_queue = cmd_queue; |
| 403 | application_socket = create_application_socket(); |
| 404 | if (application_socket < 0) { |
| 405 | goto error; |
| 406 | } |
| 407 | thread_state->application_socket = application_socket; |
| 408 | sem_init(&thread_state->ready, 0, 0); |
| 409 | |
| 410 | thread = lttng_thread_create("UST application registration", |
| 411 | thread_application_registration, |
| 412 | shutdown_application_registration_thread, |
| 413 | cleanup_application_registration_thread, |
| 414 | thread_state); |
| 415 | if (!thread) { |
| 416 | goto error; |
| 417 | } |
| 418 | /* |
| 419 | * The application registration thread now owns the application socket |
| 420 | * and the global thread state. The thread state is used to wait for |
| 421 | * the thread's status, but its ownership now belongs to the thread. |
| 422 | */ |
| 423 | application_socket = -1; |
| 424 | if (!wait_thread_status(thread_state)) { |
| 425 | thread_state = nullptr; |
| 426 | goto error; |
| 427 | } |
| 428 | |
| 429 | /* Notify all applications to register. */ |
| 430 | ret = notify_ust_apps(1, is_root); |
| 431 | if (ret < 0) { |
| 432 | ERR("Failed to notify applications or create the wait shared memory.\n" |
| 433 | "Execution continues but there might be problems for already\n" |
| 434 | "running applications that wishes to register."); |
| 435 | } |
| 436 | |
| 437 | return thread; |
| 438 | error: |
| 439 | lttng_thread_put(thread); |
| 440 | cleanup_application_registration_thread(thread_state); |
| 441 | if (application_socket >= 0) { |
| 442 | if (close(application_socket)) { |
| 443 | PERROR("Failed to close application registration socket"); |
| 444 | } |
| 445 | } |
| 446 | error_alloc: |
| 447 | return nullptr; |
| 448 | } |