Park two non-CLOEXEC descriptors near the top of the table; returns the lower * of the two and hands back the original (low) descriptor in *keep_low. The * kernel may cap the table below _SC_OPEN_MAX (macOS: kern.maxfilesperproc), * so the start point halves until F_DUPFD accepts it. */
| 1348 | * kernel may cap the table below _SC_OPEN_MAX (macOS: kern.maxfilesperproc), |
| 1349 | * so the start point halves until F_DUPFD accepts it. */ |
| 1350 | static int subprocess_park_high_fds(int *keep_low) { |
| 1351 | char path[] = "/tmp/cbm-subprocess-highfd-XXXXXX"; |
| 1352 | int low = cbm_mkstemp(path); |
| 1353 | if (low < 0) { |
| 1354 | return -1; |
| 1355 | } |
| 1356 | (void)unlink(path); |
| 1357 | long top = sysconf(_SC_OPEN_MAX); |
| 1358 | if (top <= 0 || top > 1048576L) { |
| 1359 | top = 1048576L; |
| 1360 | } |
| 1361 | int high = -1; |
| 1362 | for (long base = top - 4; high < 0 && base > 64; base /= 2) { |
| 1363 | high = fcntl(low, F_DUPFD, (int)base); |
| 1364 | } |
| 1365 | int higher = high >= 0 ? fcntl(low, F_DUPFD, high + 1) : -1; |
| 1366 | if (higher != high + 1) { |
| 1367 | (void)close(low); |
| 1368 | if (high >= 0) { |
| 1369 | (void)close(high); |
| 1370 | } |
| 1371 | if (higher >= 0) { |
| 1372 | (void)close(higher); |
| 1373 | } |
| 1374 | return -1; |
| 1375 | } |
| 1376 | *keep_low = low; |
| 1377 | return high; |
| 1378 | } |
| 1379 | #endif |
| 1380 | |
| 1381 | TEST(subprocess_child_close_fds_uses_one_syscall_not_rlimit_loop) { |
no test coverage detected