procfs-util: fix confusion wrt. quantity limit and maximum value

From packit/rawhide-arm64 logs:
Assertion 'limit >= INT_MAX || get_process_ppid(limit+1, NULL) == -ESRCH' failed at src/test/test-process-util.c:855, function test_get_process_ppid(). Aborting.
――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――

The kernel has a few different limits. In particular kernel.threads-max can be
set to some lower value, and kernel.pid_max can be set to a higher value. This
is nice because it reduces PID reuse, even if the number of threads that is
allowed is limited. But the tests assumed that we cannot have a thread with
PID above MIN(kernel.threads-max, kernel.pid_max-1), which is not valid.

So let's rework the whole thing: let's expose the helpers to read
kernel.threads-max and kernel.pid_max, and print what they return in tests.
procfs_tasks_get_limit() was something that is only used in tests, and wasn't
very well defined, so let's drop it.

Fixes #21193.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-11-03 09:36:08 +01:00
parent db2aef5a1d
commit c3dead53d5
5 changed files with 85 additions and 68 deletions
+8 -2
View File
@@ -851,8 +851,14 @@ static void test_get_process_ppid(void) {
assert_se(get_process_ppid(1, NULL) == -EADDRNOTAVAIL);
/* the process with the PID above the global limit definitely doesn't exist. Verify that */
assert_se(procfs_tasks_get_limit(&limit) >= 0);
assert_se(limit >= INT_MAX || get_process_ppid(limit+1, NULL) == -ESRCH);
assert_se(procfs_get_pid_max(&limit) >= 0);
log_debug("kernel.pid_max = %"PRIu64, limit);
if (limit < INT_MAX) {
r = get_process_ppid(limit + 1, NULL);
log_debug_errno(r, "get_process_limit(%"PRIu64") → %d/%m", limit + 1, r);
assert(r == -ESRCH);
}
for (pid_t pid = 0;;) {
_cleanup_free_ char *c1 = NULL, *c2 = NULL;
+25 -11
View File
@@ -6,11 +6,12 @@
#include "format-util.h"
#include "log.h"
#include "procfs-util.h"
#include "process-util.h"
#include "tests.h"
int main(int argc, char *argv[]) {
nsec_t nsec;
uint64_t v;
uint64_t v, w;
int r;
log_parse_environment();
@@ -25,26 +26,39 @@ int main(int argc, char *argv[]) {
assert_se(procfs_tasks_get_current(&v) >= 0);
log_info("Current number of tasks: %" PRIu64, v);
r = procfs_tasks_get_limit(&v);
if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r))
return log_tests_skipped("can't read /proc/sys/kernel/pid_max");
v = TASKS_MAX;
r = procfs_get_pid_max(&v);
assert(r >= 0 || r == -ENOENT || ERRNO_IS_PRIVILEGE(r));
log_info("kernel.pid_max: %"PRIu64, v);
w = TASKS_MAX;
r = procfs_get_threads_max(&w);
assert(r >= 0 || r == -ENOENT || ERRNO_IS_PRIVILEGE(r));
log_info("kernel.threads-max: %"PRIu64, w);
v = MIN(v - (v > 0), w);
assert_se(r >= 0);
log_info("Limit of tasks: %" PRIu64, v);
assert_se(v > 0);
assert_se(procfs_tasks_set_limit(v) >= 0);
r = procfs_tasks_set_limit(v);
if (r == -ENOENT || ERRNO_IS_PRIVILEGE(r))
return log_tests_skipped("can't set task limits");
assert(r >= 0);
if (v > 100) {
uint64_t w;
r = procfs_tasks_set_limit(v-1);
assert_se(IN_SET(r, 0, -EPERM, -EACCES, -EROFS));
log_info("Reducing limit by one to %"PRIu64"…", v-1);
assert_se(procfs_tasks_get_limit(&w) >= 0);
assert_se((r == 0 && w == v - 1) || (r < 0 && w == v));
r = procfs_tasks_set_limit(v-1);
log_info_errno(r, "procfs_tasks_set_limit: %m");
assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r));
assert_se(procfs_get_threads_max(&w) >= 0);
assert_se(r >= 0 ? w == v - 1 : w == v);
assert_se(procfs_tasks_set_limit(v) >= 0);
assert_se(procfs_tasks_get_limit(&w) >= 0);
assert_se(procfs_get_threads_max(&w) >= 0);
assert_se(v == w);
}