|
16 | 16 | #include <unistd.h> |
17 | 17 | #ifndef _WIN32 |
18 | 18 | #include <sys/stat.h> /* chmod for the symlink-policy mode legs */ |
| 19 | +#include <errno.h> |
| 20 | +#include <sys/resource.h> /* getpriority for the worker-priority leg */ |
| 21 | +#include <sys/wait.h> |
19 | 22 | #endif |
20 | 23 |
|
21 | 24 | #ifdef __linux__ |
@@ -1442,6 +1445,49 @@ TEST(regex_estimate_counts_brackets_and_escapes_literally) { |
1442 | 1445 | PASS(); |
1443 | 1446 | } |
1444 | 1447 |
|
| 1448 | +/* An index worker lowers its own priority (#2834). A non-root process cannot |
| 1449 | + * raise it back, so the helper runs in a forked child that reports its nice. */ |
| 1450 | +TEST(platform_lower_cpu_priority_nices_the_process) { |
| 1451 | +#ifdef _WIN32 |
| 1452 | + SKIP_PLATFORM("POSIX nice contract"); |
| 1453 | +#else |
| 1454 | + int fds[2]; |
| 1455 | + ASSERT_EQ(pipe(fds), 0); |
| 1456 | + pid_t child = fork(); |
| 1457 | + if (child == 0) { |
| 1458 | + (void)close(fds[0]); |
| 1459 | + /* report: lowered ok, nice after; then, from a nicer start, ok, target, nice after */ |
| 1460 | + int report[5] = {cbm_lower_cpu_priority() ? 1 : 0, 0, 0, 0, 0}; |
| 1461 | + errno = 0; |
| 1462 | + report[1] = getpriority(PRIO_PROCESS, 0); |
| 1463 | + if (errno != 0) { |
| 1464 | + report[0] = 0; |
| 1465 | + } |
| 1466 | + /* Never-raise leg: start nicer than the helper's level; it must leave it. */ |
| 1467 | + report[3] = report[1] >= 15 ? 19 : 15; |
| 1468 | + if (setpriority(PRIO_PROCESS, 0, report[3]) == 0) { |
| 1469 | + report[2] = cbm_lower_cpu_priority() ? 1 : 0; |
| 1470 | + errno = 0; |
| 1471 | + report[4] = getpriority(PRIO_PROCESS, 0); |
| 1472 | + } |
| 1473 | + _exit(write(fds[1], report, sizeof(report)) == (ssize_t)sizeof(report) ? 0 : 1); |
| 1474 | + } |
| 1475 | + ASSERT_TRUE(child > 0); |
| 1476 | + (void)close(fds[1]); |
| 1477 | + int report[5] = {0, 0, 0, 0, 0}; |
| 1478 | + ssize_t got = read(fds[0], report, sizeof(report)); |
| 1479 | + (void)close(fds[0]); |
| 1480 | + int status = 0; |
| 1481 | + (void)waitpid(child, &status, 0); |
| 1482 | + ASSERT_EQ(got, (ssize_t)sizeof(report)); |
| 1483 | + ASSERT_EQ(report[0], 1); |
| 1484 | + ASSERT_TRUE(report[1] >= CBM_LOW_CPU_PRIORITY_NICE); |
| 1485 | + ASSERT_EQ(report[2], 1); |
| 1486 | + ASSERT_EQ(report[4], report[3]); |
| 1487 | + PASS(); |
| 1488 | +#endif |
| 1489 | +} |
| 1490 | + |
1445 | 1491 | SUITE(platform) { |
1446 | 1492 | RUN_TEST(platform_file_apis_survive_max_path_overflow); |
1447 | 1493 | RUN_TEST(platform_mkdir_p_follows_own_symlink_only_when_opted_in); |
@@ -1477,6 +1523,7 @@ SUITE(platform) { |
1477 | 1523 | RUN_TEST(platform_env_long_refuses_what_it_cannot_read); |
1478 | 1524 | RUN_TEST(platform_env_dup_honours_the_settings_resolver); |
1479 | 1525 | RUN_TEST(platform_system_info); |
| 1526 | + RUN_TEST(platform_lower_cpu_priority_nices_the_process); |
1480 | 1527 | RUN_TEST(regex_compile_refuses_oversized_expansion); |
1481 | 1528 | RUN_TEST(regex_compile_accepts_expansion_under_budget); |
1482 | 1529 | RUN_TEST(regex_compile_refuses_overlong_pattern); |
|
0 commit comments