util: add helper for raising the max files limit

Historically the max files limit for processes has always been 1024,
because going beyond this is incompatible with the select() function.
None the less most apps these days will use poll() so should not be
limited in this way.

Since systemd >= 240, the hard limit will be 500k, while the soft
limit remains at 1k. Applications which don't use select() should
raise their soft limit to match the hard limit during their startup.

This function provides a convenient helper to do this limit raising.

Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2023-06-21 13:44:39 +01:00
parent 3967174d03
commit c41cc852f6
3 changed files with 36 additions and 0 deletions

View File

@ -3171,6 +3171,7 @@ virPortAllocatorSetUsed;
# util/virprocess.h
virProcessAbort;
virProcessActivateMaxFiles;
virProcessExitWithStatus;
virProcessGetAffinity;
virProcessGetMaxMemLock;

View File

@ -1048,6 +1048,35 @@ virProcessSetMaxFiles(pid_t pid, unsigned int files)
return 0;
}
void
virProcessActivateMaxFiles(void)
{
struct rlimit maxfiles = {0};
/*
* Ignore errors since we might be inside a container with seccomp
* filters and limits preset to suitable values.
*/
if (getrlimit(RLIMIT_NOFILE, &maxfiles) < 0) {
VIR_DEBUG("Unable to fetch process max files limit: %s",
g_strerror(errno));
return;
}
VIR_DEBUG("Initial max files was %llu", (unsigned long long)maxfiles.rlim_cur);
maxfiles.rlim_cur = maxfiles.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &maxfiles) < 0) {
VIR_DEBUG("Unable to set process max files limit to %llu: %s",
(unsigned long long)maxfiles.rlim_cur, g_strerror(errno));
return;
}
VIR_DEBUG("Raised max files to %llu", (unsigned long long)maxfiles.rlim_cur);
}
#else /* ! (WITH_SETRLIMIT && defined(RLIMIT_NOFILE)) */
int
virProcessSetMaxFiles(pid_t pid G_GNUC_UNUSED,
@ -1056,6 +1085,11 @@ virProcessSetMaxFiles(pid_t pid G_GNUC_UNUSED,
virReportSystemError(ENOSYS, "%s", _("Not supported on this platform"));
return -1;
}
void
virProcessActivateMaxFiles(void)
{
}
#endif /* ! (WITH_SETRLIMIT && defined(RLIMIT_NOFILE)) */
#if WITH_SETRLIMIT && defined(RLIMIT_CORE)

View File

@ -81,6 +81,7 @@ int virProcessSetMaxMemLock(pid_t pid, unsigned long long bytes) G_NO_INLINE;
int virProcessSetMaxProcesses(pid_t pid, unsigned int procs);
int virProcessSetMaxFiles(pid_t pid, unsigned int files);
int virProcessSetMaxCoreSize(pid_t pid, unsigned long long bytes);
void virProcessActivateMaxFiles(void);
int virProcessGetMaxMemLock(pid_t pid, unsigned long long *bytes) G_NO_INLINE;