]> xenbits.xensource.com Git - libvirt.git/commitdiff
vircommand: Make sysconf(_SC_OPEN_MAX) failure non-fatal
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 29 Aug 2023 07:22:09 +0000 (09:22 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 13 Sep 2024 12:50:43 +0000 (14:50 +0200)
The point of calling sysconf(_SC_OPEN_MAX) is to allocate big
enough bitmap so that subsequent call to
virCommandMassCloseGetFDsDir() can just set the bit instead of
expanding memory (this code runs in a forked off child and thus
using async-signal-unsafe functions like malloc() is a bit
tricky).

But on some systems the limit for opened FDs is virtually
non-existent (typically macOS Ventura started reporting EINVAL).

But with both glibc and musl using malloc() after fork() is safe.
And with sufficiently new glib too, as it's using malloc() with
newer releases instead of their own allocator.

Therefore, pick a sufficiently large value (glibc falls back to
256, [1], Darwin to 10240 [2] so 10240 should be good enough) to
fall back to and make the error non-fatal.

1: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/getdtsz.c;h=4c5a6208067d2f9eaaac6dba652702fb4af9b7e3;hb=HEAD
2  https://github.com/apple/darwin-xnu/blob/main/bsd/sys/syslimits.h#L104

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
src/util/vircommand.c

index 5526499b4357dbb472907892d5d965be3140f69d..ea52acfbb8876ef51f5eea6207f282493e37b1c8 100644 (file)
@@ -494,7 +494,7 @@ virCommandMassCloseGetFDsDir(virBitmap *fds,
             return -1;
         }
 
-        ignore_value(virBitmapSetBit(fds, fd));
+        virBitmapSetBitExpand(fds, fd);
     }
 
     if (rc < 0)
@@ -539,9 +539,11 @@ virCommandMassCloseFrom(virCommand *cmd,
      * Therefore we can safely allocate memory here (and transitively call
      * opendir/readdir) without a deadlock. */
 
-    if (openmax < 0) {
-        virReportSystemError(errno, "%s", _("sysconf(_SC_OPEN_MAX) failed"));
-        return -1;
+    if (openmax <= 0) {
+        /* Darwin defaults to 10240. Start with a generous value.
+         * virCommandMassCloseGetFDsDir() uses virBitmapSetBitExpand() anyways.
+         */
+        openmax = 10240;
     }
 
     fds = virBitmapNew(openmax);