* Use syscall(2) rather than {open,read,close}(2) when possible to avoid * reentry during bootstrapping if another library has interposed system call * wrappers. */
| 347 | * wrappers. |
| 348 | */ |
| 349 | static bool |
| 350 | os_overcommits_proc(void) { |
| 351 | int fd; |
| 352 | char buf[1]; |
| 353 | ssize_t nread; |
| 354 | |
| 355 | #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open) |
| 356 | fd = (int)syscall(SYS_open, "/proc/sys/vm/overcommit_memory", O_RDONLY | |
| 357 | O_CLOEXEC); |
| 358 | #elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat) |
| 359 | fd = (int)syscall(SYS_openat, |
| 360 | AT_FDCWD, "/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC); |
| 361 | #else |
| 362 | fd = open("/proc/sys/vm/overcommit_memory", O_RDONLY | O_CLOEXEC); |
| 363 | #endif |
| 364 | if (fd == -1) { |
| 365 | return false; /* Error. */ |
| 366 | } |
| 367 | |
| 368 | #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read) |
| 369 | nread = (ssize_t)syscall(SYS_read, fd, &buf, sizeof(buf)); |
| 370 | #else |
| 371 | nread = read(fd, &buf, sizeof(buf)); |
| 372 | #endif |
| 373 | |
| 374 | #if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close) |
| 375 | syscall(SYS_close, fd); |
| 376 | #else |
| 377 | close(fd); |
| 378 | #endif |
| 379 | |
| 380 | if (nread < 1) { |
| 381 | return false; /* Error. */ |
| 382 | } |
| 383 | /* |
| 384 | * /proc/sys/vm/overcommit_memory meanings: |
| 385 | * 0: Heuristic overcommit. |
| 386 | * 1: Always overcommit. |
| 387 | * 2: Never overcommit. |
| 388 | */ |
| 389 | return (buf[0] == '0' || buf[0] == '1'); |
| 390 | } |
| 391 | #endif |
| 392 | |
| 393 | bool |