* Use syscall(2) rather than {open,read,close}(2) when possible to avoid * reentry during bootstrapping if another library has interposed system call * wrappers. */
| 433 | * wrappers. |
| 434 | */ |
| 435 | static bool |
| 436 | os_overcommits_proc(void) { |
| 437 | int fd; |
| 438 | char buf[1]; |
| 439 | |
| 440 | #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open) |
| 441 | #if defined(O_CLOEXEC) |
| 442 | fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY | |
| 443 | O_CLOEXEC); |
| 444 | #else |
| 445 | fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY); |
| 446 | if (fd != -1) { |
| 447 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); |
| 448 | } |
| 449 | #endif |
| 450 | #elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat) |
| 451 | #if defined(O_CLOEXEC) |
| 452 | fd = (int)syscall(SYS_openat, |
| 453 | AT_FDCWD, "/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC); |
| 454 | #else |
| 455 | fd = (int)syscall(SYS_openat, |
| 456 | AT_FDCWD, "/proc/sys/vm/overcommit_memory", O_RDONLY); |
| 457 | if (fd != -1) { |
| 458 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); |
| 459 | } |
| 460 | #endif |
| 461 | #else |
| 462 | #if defined(O_CLOEXEC) |
| 463 | fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC); |
| 464 | #else |
| 465 | fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY); |
| 466 | if (fd != -1) { |
| 467 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); |
| 468 | } |
| 469 | #endif |
| 470 | #endif |
| 471 | |
| 472 | if (fd == -1) { |
| 473 | return false; /* Error. */ |
| 474 | } |
| 475 | |
| 476 | ssize_t nread = malloc_read_fd(fd, &buf, sizeof(buf)); |
| 477 | #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close) |
| 478 | syscall(SYS_close, fd); |
| 479 | #else |
| 480 | close(fd); |
| 481 | #endif |
| 482 | |
| 483 | if (nread < 1) { |
| 484 | return false; /* Error. */ |
| 485 | } |
| 486 | /* |
| 487 | * /proc/sys/vm/overcommit_memory meanings: |
| 488 | * 0: Heuristic overcommit. |
| 489 | * 1: Always overcommit. |
| 490 | * 2: Never overcommit. |
| 491 | */ |
| 492 | return (buf[0] == '0' || buf[0] == '1'); |
no test coverage detected