From fbc6a7557120fbb97d781a5d2c640a3c55c3000f Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Fri, 25 Aug 2023 14:25:33 +0200 Subject: [PATCH 1/2] mkfs-util: Set FORK_REOPEN_LOG We set FORK_CLOSE_ALL_FDS, so we should also set FORK_REOPEN_LOG to reopen the log in the child process so we don't miss any logging messages. --- src/shared/mkfs-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/mkfs-util.c b/src/shared/mkfs-util.c index b936a4733f..6fb0e2a165 100644 --- a/src/shared/mkfs-util.c +++ b/src/shared/mkfs-util.c @@ -603,7 +603,7 @@ int make_filesystem( /*except_fds=*/ NULL, /*n_except_fds=*/ 0, FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG|FORK_LOG|FORK_WAIT| - FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_NEW_MOUNTNS, + FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_NEW_MOUNTNS|FORK_REOPEN_LOG, /*ret_pid=*/ NULL); if (r < 0) return r; From f470dafddcd688c3ea6031d4bbcbf934fd094711 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Fri, 25 Aug 2023 13:55:36 +0200 Subject: [PATCH 2/2] Limit rlim_max in rlimit_nofile_safe() to nr_open We might inherit a max rlim value that's larger than the kernel's maximum (nr_open). This will cause setrlimit() to fail as the given maximum is larger than the kernel's maximum. To get around this, let's limit the max rlim we pass to rlimit() to the value of nr_open. Should fix #28965 --- src/basic/rlimit-util.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c index 91424cd3cc..a0ffb24626 100644 --- a/src/basic/rlimit-util.c +++ b/src/basic/rlimit-util.c @@ -401,7 +401,11 @@ int rlimit_nofile_safe(void) { if (rl.rlim_cur <= FD_SETSIZE) return 0; - rl.rlim_cur = FD_SETSIZE; + /* So we might have inherited a hard limit that's larger than the kernel's maximum limit as stored in + * /proc/sys/fs/nr_open. If we pass this hard limit unmodified to setrlimit(), we'll get EPERM. To + * make sure that doesn't happen, let's limit our hard limit to the value from nr_open. */ + rl.rlim_max = MIN(rl.rlim_max, (rlim_t) read_nr_open()); + rl.rlim_cur = MIN((rlim_t) FD_SETSIZE, rl.rlim_max); if (setrlimit(RLIMIT_NOFILE, &rl) < 0) return log_debug_errno(errno, "Failed to lower RLIMIT_NOFILE's soft limit to " RLIM_FMT ": %m", rl.rlim_cur);