| 1 | /* |
| 2 | * lttng-ust-comm.c |
| 3 | * |
| 4 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
| 5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; only |
| 10 | * version 2.1 of the License. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/prctl.h> |
| 25 | #include <sys/mman.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <pthread.h> |
| 31 | #include <semaphore.h> |
| 32 | #include <time.h> |
| 33 | #include <assert.h> |
| 34 | #include <signal.h> |
| 35 | #include <urcu/uatomic.h> |
| 36 | |
| 37 | #include <lttng-ust-comm.h> |
| 38 | #include <ust/usterr-signal-safe.h> |
| 39 | #include <ust/lttng-ust-abi.h> |
| 40 | #include <ust/tracepoint.h> |
| 41 | #include <ust/tracepoint-internal.h> |
| 42 | #include <ust/ust.h> |
| 43 | #include "ltt-tracer-core.h" |
| 44 | |
| 45 | /* |
| 46 | * Has lttng ust comm constructor been called ? |
| 47 | */ |
| 48 | static int initialized; |
| 49 | |
| 50 | /* |
| 51 | * The ust_lock/ust_unlock lock is used as a communication thread mutex. |
| 52 | * Held when handling a command, also held by fork() to deal with |
| 53 | * removal of threads, and by exit path. |
| 54 | */ |
| 55 | |
| 56 | /* Should the ust comm thread quit ? */ |
| 57 | static int lttng_ust_comm_should_quit; |
| 58 | |
| 59 | /* |
| 60 | * Wait for either of these before continuing to the main |
| 61 | * program: |
| 62 | * - the register_done message from sessiond daemon |
| 63 | * (will let the sessiond daemon enable sessions before main |
| 64 | * starts.) |
| 65 | * - sessiond daemon is not reachable. |
| 66 | * - timeout (ensuring applications are resilient to session |
| 67 | * daemon problems). |
| 68 | */ |
| 69 | static sem_t constructor_wait; |
| 70 | /* |
| 71 | * Doing this for both the global and local sessiond. |
| 72 | */ |
| 73 | static int sem_count = { 2 }; |
| 74 | |
| 75 | /* |
| 76 | * Info about socket and associated listener thread. |
| 77 | */ |
| 78 | struct sock_info { |
| 79 | const char *name; |
| 80 | pthread_t ust_listener; /* listener thread */ |
| 81 | int root_handle; |
| 82 | int constructor_sem_posted; |
| 83 | int allowed; |
| 84 | int global; |
| 85 | |
| 86 | char sock_path[PATH_MAX]; |
| 87 | int socket; |
| 88 | |
| 89 | char wait_shm_path[PATH_MAX]; |
| 90 | char *wait_shm_mmap; |
| 91 | }; |
| 92 | |
| 93 | /* Socket from app (connect) to session daemon (listen) for communication */ |
| 94 | struct sock_info global_apps = { |
| 95 | .name = "global", |
| 96 | .global = 1, |
| 97 | |
| 98 | .root_handle = -1, |
| 99 | .allowed = 1, |
| 100 | |
| 101 | .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK, |
| 102 | .socket = -1, |
| 103 | |
| 104 | .wait_shm_path = DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH, |
| 105 | }; |
| 106 | |
| 107 | /* TODO: allow global_apps_sock_path override */ |
| 108 | |
| 109 | struct sock_info local_apps = { |
| 110 | .name = "local", |
| 111 | .global = 0, |
| 112 | .root_handle = -1, |
| 113 | .allowed = 0, /* Check setuid bit first */ |
| 114 | |
| 115 | .socket = -1, |
| 116 | }; |
| 117 | |
| 118 | extern void ltt_ring_buffer_client_overwrite_init(void); |
| 119 | extern void ltt_ring_buffer_client_discard_init(void); |
| 120 | extern void ltt_ring_buffer_metadata_client_init(void); |
| 121 | extern void ltt_ring_buffer_client_overwrite_exit(void); |
| 122 | extern void ltt_ring_buffer_client_discard_exit(void); |
| 123 | extern void ltt_ring_buffer_metadata_client_exit(void); |
| 124 | |
| 125 | static |
| 126 | int setup_local_apps(void) |
| 127 | { |
| 128 | const char *home_dir; |
| 129 | uid_t uid; |
| 130 | |
| 131 | uid = getuid(); |
| 132 | /* |
| 133 | * Disallow per-user tracing for setuid binaries. |
| 134 | */ |
| 135 | if (uid != geteuid()) { |
| 136 | local_apps.allowed = 0; |
| 137 | return 0; |
| 138 | } else { |
| 139 | local_apps.allowed = 1; |
| 140 | } |
| 141 | home_dir = (const char *) getenv("HOME"); |
| 142 | if (!home_dir) |
| 143 | return -ENOENT; |
| 144 | snprintf(local_apps.sock_path, PATH_MAX, |
| 145 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); |
| 146 | snprintf(local_apps.wait_shm_path, PATH_MAX, |
| 147 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, uid); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static |
| 152 | int register_app_to_sessiond(int socket) |
| 153 | { |
| 154 | ssize_t ret; |
| 155 | int prctl_ret; |
| 156 | struct { |
| 157 | uint32_t major; |
| 158 | uint32_t minor; |
| 159 | pid_t pid; |
| 160 | pid_t ppid; |
| 161 | uid_t uid; |
| 162 | gid_t gid; |
| 163 | char name[16]; /* process name */ |
| 164 | } reg_msg; |
| 165 | |
| 166 | reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR; |
| 167 | reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR; |
| 168 | reg_msg.pid = getpid(); |
| 169 | reg_msg.ppid = getppid(); |
| 170 | reg_msg.uid = getuid(); |
| 171 | reg_msg.gid = getgid(); |
| 172 | prctl_ret = prctl(PR_GET_NAME, (unsigned long) reg_msg.name, 0, 0, 0); |
| 173 | if (prctl_ret) { |
| 174 | ERR("Error executing prctl"); |
| 175 | return -errno; |
| 176 | } |
| 177 | |
| 178 | ret = lttcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); |
| 179 | if (ret >= 0 && ret != sizeof(reg_msg)) |
| 180 | return -EIO; |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static |
| 185 | int send_reply(int sock, struct lttcomm_ust_reply *lur) |
| 186 | { |
| 187 | ssize_t len; |
| 188 | |
| 189 | len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
| 190 | switch (len) { |
| 191 | case sizeof(*lur): |
| 192 | DBG("message successfully sent"); |
| 193 | return 0; |
| 194 | case -1: |
| 195 | if (errno == ECONNRESET) { |
| 196 | printf("remote end closed connection\n"); |
| 197 | return 0; |
| 198 | } |
| 199 | return -1; |
| 200 | default: |
| 201 | printf("incorrect message size: %zd\n", len); |
| 202 | return -1; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static |
| 207 | int handle_register_done(struct sock_info *sock_info) |
| 208 | { |
| 209 | int ret; |
| 210 | |
| 211 | if (sock_info->constructor_sem_posted) |
| 212 | return 0; |
| 213 | sock_info->constructor_sem_posted = 1; |
| 214 | ret = uatomic_add_return(&sem_count, -1); |
| 215 | if (ret == 0) { |
| 216 | ret = sem_post(&constructor_wait); |
| 217 | assert(!ret); |
| 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static |
| 223 | int handle_message(struct sock_info *sock_info, |
| 224 | int sock, struct lttcomm_ust_msg *lum) |
| 225 | { |
| 226 | int ret = 0; |
| 227 | const struct objd_ops *ops; |
| 228 | struct lttcomm_ust_reply lur; |
| 229 | |
| 230 | ust_lock(); |
| 231 | |
| 232 | memset(&lur, 0, sizeof(lur)); |
| 233 | |
| 234 | if (lttng_ust_comm_should_quit) { |
| 235 | ret = -EPERM; |
| 236 | goto end; |
| 237 | } |
| 238 | |
| 239 | ops = objd_ops(lum->handle); |
| 240 | if (!ops) { |
| 241 | ret = -ENOENT; |
| 242 | goto end; |
| 243 | } |
| 244 | |
| 245 | switch (lum->cmd) { |
| 246 | case LTTNG_UST_REGISTER_DONE: |
| 247 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) |
| 248 | ret = handle_register_done(sock_info); |
| 249 | else |
| 250 | ret = -EINVAL; |
| 251 | break; |
| 252 | case LTTNG_UST_RELEASE: |
| 253 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) |
| 254 | ret = -EPERM; |
| 255 | else |
| 256 | ret = objd_unref(lum->handle); |
| 257 | break; |
| 258 | default: |
| 259 | if (ops->cmd) |
| 260 | ret = ops->cmd(lum->handle, lum->cmd, |
| 261 | (unsigned long) &lum->u); |
| 262 | else |
| 263 | ret = -ENOSYS; |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | end: |
| 268 | lur.handle = lum->handle; |
| 269 | lur.cmd = lum->cmd; |
| 270 | lur.ret_val = ret; |
| 271 | if (ret >= 0) { |
| 272 | lur.ret_code = LTTCOMM_OK; |
| 273 | } else { |
| 274 | lur.ret_code = LTTCOMM_SESSION_FAIL; |
| 275 | } |
| 276 | ret = send_reply(sock, &lur); |
| 277 | |
| 278 | ust_unlock(); |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | static |
| 283 | void cleanup_sock_info(struct sock_info *sock_info) |
| 284 | { |
| 285 | int ret; |
| 286 | |
| 287 | if (sock_info->socket != -1) { |
| 288 | ret = close(sock_info->socket); |
| 289 | if (ret) { |
| 290 | ERR("Error closing apps socket"); |
| 291 | } |
| 292 | sock_info->socket = -1; |
| 293 | } |
| 294 | if (sock_info->root_handle != -1) { |
| 295 | ret = objd_unref(sock_info->root_handle); |
| 296 | if (ret) { |
| 297 | ERR("Error unref root handle"); |
| 298 | } |
| 299 | sock_info->root_handle = -1; |
| 300 | } |
| 301 | sock_info->constructor_sem_posted = 0; |
| 302 | if (sock_info->wait_shm_mmap) { |
| 303 | ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE)); |
| 304 | if (ret) { |
| 305 | ERR("Error unmapping wait shm"); |
| 306 | } |
| 307 | sock_info->wait_shm_mmap = NULL; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | static |
| 312 | char *get_map_shm(struct sock_info *sock_info) |
| 313 | { |
| 314 | size_t mmap_size = sysconf(_SC_PAGE_SIZE); |
| 315 | int wait_shm_fd, ret; |
| 316 | char *wait_shm_mmap; |
| 317 | int mode; |
| 318 | |
| 319 | mode = S_IRUSR | S_IRGRP; |
| 320 | if (sock_info->global) |
| 321 | mode |= S_IROTH; |
| 322 | |
| 323 | /* |
| 324 | * Get existing (read-only) shm, or open new shm. |
| 325 | * First try to open read-only. |
| 326 | */ |
| 327 | wait_shm_fd = shm_open(sock_info->wait_shm_path, |
| 328 | O_RDONLY, mode); |
| 329 | if (wait_shm_fd >= 0) |
| 330 | goto got_shm; |
| 331 | /* |
| 332 | * Real-only open did not work. If it is because it did |
| 333 | * not exist, try creating it. Else it's a failure that |
| 334 | * prohibits using shm. |
| 335 | */ |
| 336 | if (errno != ENOENT) { |
| 337 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
| 338 | goto error; |
| 339 | } |
| 340 | wait_shm_fd = shm_open(sock_info->wait_shm_path, |
| 341 | O_RDWR | O_CREAT | O_EXCL, mode | S_IWUSR); |
| 342 | if (wait_shm_fd >= 0) |
| 343 | goto created_shm; |
| 344 | if (errno != EEXIST) { |
| 345 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
| 346 | goto error; |
| 347 | } |
| 348 | /* |
| 349 | * If another process beat us to create the shm, we are |
| 350 | * pretty certain the shm is available for us in |
| 351 | * read-only mode. |
| 352 | */ |
| 353 | wait_shm_fd = shm_open(sock_info->wait_shm_path, |
| 354 | O_RDWR | O_CREAT | O_EXCL, mode); |
| 355 | if (wait_shm_fd >= 0) |
| 356 | goto got_shm; |
| 357 | else |
| 358 | goto error; |
| 359 | |
| 360 | created_shm: |
| 361 | ret = ftruncate(wait_shm_fd, mmap_size); |
| 362 | if (ret) { |
| 363 | PERROR("ftruncate"); |
| 364 | ret = close(wait_shm_fd); |
| 365 | if (ret) { |
| 366 | ERR("Error closing fd"); |
| 367 | } |
| 368 | wait_shm_fd = -1; |
| 369 | goto error; |
| 370 | } |
| 371 | /* Drop write access ASAP */ |
| 372 | ret = chmod(sock_info->wait_shm_path, mode); |
| 373 | if (ret) { |
| 374 | PERROR("chmod"); |
| 375 | } |
| 376 | got_shm: |
| 377 | wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ, |
| 378 | MAP_SHARED, wait_shm_fd, 0); |
| 379 | if (wait_shm_mmap == MAP_FAILED) { |
| 380 | PERROR("mmap"); |
| 381 | goto error; |
| 382 | } |
| 383 | /* close shm fd immediately after taking the mmap reference */ |
| 384 | ret = close(wait_shm_fd); |
| 385 | if (ret) { |
| 386 | ERR("Error closing fd"); |
| 387 | } |
| 388 | return wait_shm_mmap; |
| 389 | |
| 390 | error: |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | static |
| 395 | void wait_for_sessiond(struct sock_info *sock_info) |
| 396 | { |
| 397 | ust_lock(); |
| 398 | if (lttng_ust_comm_should_quit) { |
| 399 | goto quit; |
| 400 | } |
| 401 | if (!sock_info->wait_shm_mmap) { |
| 402 | sock_info->wait_shm_mmap = get_map_shm(sock_info); |
| 403 | if (!sock_info->wait_shm_mmap) |
| 404 | goto error; |
| 405 | } |
| 406 | ust_unlock(); |
| 407 | |
| 408 | DBG("Waiting for %s apps sessiond", sock_info->name); |
| 409 | /* Wait for futex wakeup TODO */ |
| 410 | sleep(5); |
| 411 | |
| 412 | return; |
| 413 | |
| 414 | quit: |
| 415 | ust_unlock(); |
| 416 | return; |
| 417 | |
| 418 | error: |
| 419 | ust_unlock(); |
| 420 | /* Error handling: fallback on a 5 seconds sleep. */ |
| 421 | sleep(5); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * This thread does not allocate any resource, except within |
| 427 | * handle_message, within mutex protection. This mutex protects against |
| 428 | * fork and exit. |
| 429 | * The other moment it allocates resources is at socket connexion, which |
| 430 | * is also protected by the mutex. |
| 431 | */ |
| 432 | static |
| 433 | void *ust_listener_thread(void *arg) |
| 434 | { |
| 435 | struct sock_info *sock_info = arg; |
| 436 | int sock, ret; |
| 437 | |
| 438 | /* Restart trying to connect to the session daemon */ |
| 439 | restart: |
| 440 | ust_lock(); |
| 441 | |
| 442 | if (lttng_ust_comm_should_quit) { |
| 443 | ust_unlock(); |
| 444 | goto quit; |
| 445 | } |
| 446 | |
| 447 | if (sock_info->socket != -1) { |
| 448 | ret = close(sock_info->socket); |
| 449 | if (ret) { |
| 450 | ERR("Error closing %s apps socket", sock_info->name); |
| 451 | } |
| 452 | sock_info->socket = -1; |
| 453 | } |
| 454 | |
| 455 | /* Register */ |
| 456 | ret = lttcomm_connect_unix_sock(sock_info->sock_path); |
| 457 | if (ret < 0) { |
| 458 | ERR("Error connecting to %s apps socket", sock_info->name); |
| 459 | /* |
| 460 | * If we cannot find the sessiond daemon, don't delay |
| 461 | * constructor execution. |
| 462 | */ |
| 463 | ret = handle_register_done(sock_info); |
| 464 | assert(!ret); |
| 465 | ust_unlock(); |
| 466 | |
| 467 | /* Wait for sessiond availability with pipe */ |
| 468 | wait_for_sessiond(sock_info); |
| 469 | goto restart; |
| 470 | } |
| 471 | |
| 472 | sock_info->socket = sock = ret; |
| 473 | |
| 474 | /* |
| 475 | * Create only one root handle per listener thread for the whole |
| 476 | * process lifetime. |
| 477 | */ |
| 478 | if (sock_info->root_handle == -1) { |
| 479 | ret = lttng_abi_create_root_handle(); |
| 480 | if (ret) { |
| 481 | ERR("Error creating root handle"); |
| 482 | ust_unlock(); |
| 483 | goto quit; |
| 484 | } |
| 485 | sock_info->root_handle = ret; |
| 486 | } |
| 487 | |
| 488 | ret = register_app_to_sessiond(sock); |
| 489 | if (ret < 0) { |
| 490 | ERR("Error registering to %s apps socket", sock_info->name); |
| 491 | /* |
| 492 | * If we cannot register to the sessiond daemon, don't |
| 493 | * delay constructor execution. |
| 494 | */ |
| 495 | ret = handle_register_done(sock_info); |
| 496 | assert(!ret); |
| 497 | ust_unlock(); |
| 498 | wait_for_sessiond(sock_info); |
| 499 | goto restart; |
| 500 | } |
| 501 | ust_unlock(); |
| 502 | |
| 503 | for (;;) { |
| 504 | ssize_t len; |
| 505 | struct lttcomm_ust_msg lum; |
| 506 | |
| 507 | len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
| 508 | switch (len) { |
| 509 | case 0: /* orderly shutdown */ |
| 510 | DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name); |
| 511 | goto end; |
| 512 | case sizeof(lum): |
| 513 | DBG("message received\n"); |
| 514 | ret = handle_message(sock_info, sock, &lum); |
| 515 | if (ret < 0) { |
| 516 | ERR("Error handling message for %s socket", sock_info->name); |
| 517 | } |
| 518 | continue; |
| 519 | case -1: |
| 520 | if (errno == ECONNRESET) { |
| 521 | ERR("%s remote end closed connection\n", sock_info->name); |
| 522 | goto end; |
| 523 | } |
| 524 | goto end; |
| 525 | default: |
| 526 | ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len); |
| 527 | continue; |
| 528 | } |
| 529 | |
| 530 | } |
| 531 | end: |
| 532 | goto restart; /* try to reconnect */ |
| 533 | quit: |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Return values: -1: don't wait. 0: wait forever. 1: timeout wait. |
| 539 | */ |
| 540 | static |
| 541 | int get_timeout(struct timespec *constructor_timeout) |
| 542 | { |
| 543 | long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS; |
| 544 | char *str_delay; |
| 545 | int ret; |
| 546 | |
| 547 | str_delay = getenv("UST_REGISTER_TIMEOUT"); |
| 548 | if (str_delay) { |
| 549 | constructor_delay_ms = strtol(str_delay, NULL, 10); |
| 550 | } |
| 551 | |
| 552 | switch (constructor_delay_ms) { |
| 553 | case -1:/* fall-through */ |
| 554 | case 0: |
| 555 | return constructor_delay_ms; |
| 556 | default: |
| 557 | break; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * If we are unable to find the current time, don't wait. |
| 562 | */ |
| 563 | ret = clock_gettime(CLOCK_REALTIME, constructor_timeout); |
| 564 | if (ret) { |
| 565 | return -1; |
| 566 | } |
| 567 | constructor_timeout->tv_sec += constructor_delay_ms / 1000UL; |
| 568 | constructor_timeout->tv_nsec += |
| 569 | (constructor_delay_ms % 1000UL) * 1000000UL; |
| 570 | if (constructor_timeout->tv_nsec >= 1000000000UL) { |
| 571 | constructor_timeout->tv_sec++; |
| 572 | constructor_timeout->tv_nsec -= 1000000000UL; |
| 573 | } |
| 574 | return 1; |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * sessiond monitoring thread: monitor presence of global and per-user |
| 579 | * sessiond by polling the application common named pipe. |
| 580 | */ |
| 581 | /* TODO */ |
| 582 | |
| 583 | void __attribute__((constructor)) lttng_ust_init(void) |
| 584 | { |
| 585 | struct timespec constructor_timeout; |
| 586 | int timeout_mode; |
| 587 | int ret; |
| 588 | |
| 589 | if (uatomic_xchg(&initialized, 1) == 1) |
| 590 | return; |
| 591 | |
| 592 | /* |
| 593 | * We want precise control over the order in which we construct |
| 594 | * our sub-libraries vs starting to receive commands from |
| 595 | * sessiond (otherwise leading to errors when trying to create |
| 596 | * sessiond before the init functions are completed). |
| 597 | */ |
| 598 | init_usterr(); |
| 599 | init_tracepoint(); |
| 600 | ltt_ring_buffer_metadata_client_init(); |
| 601 | ltt_ring_buffer_client_overwrite_init(); |
| 602 | ltt_ring_buffer_client_discard_init(); |
| 603 | |
| 604 | timeout_mode = get_timeout(&constructor_timeout); |
| 605 | |
| 606 | ret = sem_init(&constructor_wait, 0, 0); |
| 607 | assert(!ret); |
| 608 | |
| 609 | ret = setup_local_apps(); |
| 610 | if (ret) { |
| 611 | ERR("Error setting up to local apps"); |
| 612 | } |
| 613 | ret = pthread_create(&local_apps.ust_listener, NULL, |
| 614 | ust_listener_thread, &local_apps); |
| 615 | |
| 616 | if (local_apps.allowed) { |
| 617 | ret = pthread_create(&global_apps.ust_listener, NULL, |
| 618 | ust_listener_thread, &global_apps); |
| 619 | } else { |
| 620 | handle_register_done(&local_apps); |
| 621 | } |
| 622 | |
| 623 | switch (timeout_mode) { |
| 624 | case 1: /* timeout wait */ |
| 625 | do { |
| 626 | ret = sem_timedwait(&constructor_wait, |
| 627 | &constructor_timeout); |
| 628 | } while (ret < 0 && errno == EINTR); |
| 629 | if (ret < 0 && errno == ETIMEDOUT) { |
| 630 | ERR("Timed out waiting for ltt-sessiond"); |
| 631 | } else { |
| 632 | assert(!ret); |
| 633 | } |
| 634 | break; |
| 635 | case -1:/* wait forever */ |
| 636 | do { |
| 637 | ret = sem_wait(&constructor_wait); |
| 638 | } while (ret < 0 && errno == EINTR); |
| 639 | assert(!ret); |
| 640 | break; |
| 641 | case 0: /* no timeout */ |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | static |
| 647 | void lttng_ust_cleanup(int exiting) |
| 648 | { |
| 649 | cleanup_sock_info(&global_apps); |
| 650 | if (local_apps.allowed) { |
| 651 | cleanup_sock_info(&local_apps); |
| 652 | } |
| 653 | lttng_ust_abi_exit(); |
| 654 | ltt_events_exit(); |
| 655 | ltt_ring_buffer_client_discard_exit(); |
| 656 | ltt_ring_buffer_client_overwrite_exit(); |
| 657 | ltt_ring_buffer_metadata_client_exit(); |
| 658 | exit_tracepoint(); |
| 659 | if (!exiting) { |
| 660 | /* Reinitialize values for fork */ |
| 661 | sem_count = 2; |
| 662 | lttng_ust_comm_should_quit = 0; |
| 663 | initialized = 0; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | void __attribute__((destructor)) lttng_ust_exit(void) |
| 668 | { |
| 669 | int ret; |
| 670 | |
| 671 | /* |
| 672 | * Using pthread_cancel here because: |
| 673 | * A) we don't want to hang application teardown. |
| 674 | * B) the thread is not allocating any resource. |
| 675 | */ |
| 676 | |
| 677 | /* |
| 678 | * Require the communication thread to quit. Synchronize with |
| 679 | * mutexes to ensure it is not in a mutex critical section when |
| 680 | * pthread_cancel is later called. |
| 681 | */ |
| 682 | ust_lock(); |
| 683 | lttng_ust_comm_should_quit = 1; |
| 684 | ust_unlock(); |
| 685 | |
| 686 | ret = pthread_cancel(global_apps.ust_listener); |
| 687 | if (ret) { |
| 688 | ERR("Error cancelling global ust listener thread"); |
| 689 | } |
| 690 | if (local_apps.allowed) { |
| 691 | ret = pthread_cancel(local_apps.ust_listener); |
| 692 | if (ret) { |
| 693 | ERR("Error cancelling local ust listener thread"); |
| 694 | } |
| 695 | } |
| 696 | lttng_ust_cleanup(1); |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * We exclude the worker threads across fork and clone (except |
| 701 | * CLONE_VM), because these system calls only keep the forking thread |
| 702 | * running in the child. Therefore, we don't want to call fork or clone |
| 703 | * in the middle of an tracepoint or ust tracing state modification. |
| 704 | * Holding this mutex protects these structures across fork and clone. |
| 705 | */ |
| 706 | void ust_before_fork(ust_fork_info_t *fork_info) |
| 707 | { |
| 708 | /* |
| 709 | * Disable signals. This is to avoid that the child intervenes |
| 710 | * before it is properly setup for tracing. It is safer to |
| 711 | * disable all signals, because then we know we are not breaking |
| 712 | * anything by restoring the original mask. |
| 713 | */ |
| 714 | sigset_t all_sigs; |
| 715 | int ret; |
| 716 | |
| 717 | /* Disable signals */ |
| 718 | sigfillset(&all_sigs); |
| 719 | ret = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs); |
| 720 | if (ret == -1) { |
| 721 | PERROR("sigprocmask"); |
| 722 | } |
| 723 | ust_lock(); |
| 724 | rcu_bp_before_fork(); |
| 725 | } |
| 726 | |
| 727 | static void ust_after_fork_common(ust_fork_info_t *fork_info) |
| 728 | { |
| 729 | int ret; |
| 730 | |
| 731 | DBG("process %d", getpid()); |
| 732 | ust_unlock(); |
| 733 | /* Restore signals */ |
| 734 | ret = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL); |
| 735 | if (ret == -1) { |
| 736 | PERROR("sigprocmask"); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void ust_after_fork_parent(ust_fork_info_t *fork_info) |
| 741 | { |
| 742 | DBG("process %d", getpid()); |
| 743 | rcu_bp_after_fork_parent(); |
| 744 | /* Release mutexes and reenable signals */ |
| 745 | ust_after_fork_common(fork_info); |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * After fork, in the child, we need to cleanup all the leftover state, |
| 750 | * except the worker thread which already magically disappeared thanks |
| 751 | * to the weird Linux fork semantics. After tyding up, we call |
| 752 | * lttng_ust_init() again to start over as a new PID. |
| 753 | * |
| 754 | * This is meant for forks() that have tracing in the child between the |
| 755 | * fork and following exec call (if there is any). |
| 756 | */ |
| 757 | void ust_after_fork_child(ust_fork_info_t *fork_info) |
| 758 | { |
| 759 | DBG("process %d", getpid()); |
| 760 | /* Release urcu mutexes */ |
| 761 | rcu_bp_after_fork_child(); |
| 762 | lttng_ust_cleanup(0); |
| 763 | /* Release mutexes and reenable signals */ |
| 764 | ust_after_fork_common(fork_info); |
| 765 | lttng_ust_init(); |
| 766 | } |