]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: add helper for raising the max files limit
authorDaniel P. Berrangé <berrange@redhat.com>
Wed, 21 Jun 2023 12:44:39 +0000 (13:44 +0100)
committerDaniel P. Berrangé <berrange@redhat.com>
Thu, 20 Jul 2023 09:58:01 +0000 (10:58 +0100)
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>
src/libvirt_private.syms
src/util/virprocess.c
src/util/virprocess.h

index fb7ad9c85511acfc129489856f669e93ce659eee..3071dec13a77520b9df6e8c6f68f09840b5e92c2 100644 (file)
@@ -3171,6 +3171,7 @@ virPortAllocatorSetUsed;
 
 # util/virprocess.h
 virProcessAbort;
+virProcessActivateMaxFiles;
 virProcessExitWithStatus;
 virProcessGetAffinity;
 virProcessGetMaxMemLock;
index 0462ae84657e9bf059742f3e6b0df0112d32ec3c..a26683f333ba21185f4722be266f4ca578f7aa8c 100644 (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)
index c18f87e80ec917466447d6deff55cc83232daf35..6008cca4af6a0f628484db290807677d308de44f 100644 (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;