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