Skip to contentSkip to conversation

oslib: fix OSes support for qemu_close_all_open_fd()

PATCH In review Clément Léger 4 participants 15 messages Aug 30, 2024 – Sep 3, 2024

lore
Share & tools
lore
#0 lore raw

cover oslib: fix OSes support for qemu_close_all_open_fd()

Some OSes (MacOS, FreeBSD) were not correctly handled by the original close_fd() code. Handle them by fixing the path for self process open file descriptors location as well as sysconf(_SC_OPEN_MAX) potentially returning -1 on MacOS.

---

Clément Léger (2):
qemu/osdep: fix current process fds path for other OSes qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

 util/oslib-posix.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/2 qemu/osdep: fix current process fds path for other OSes

While Linux and Solaris uses /proc/self/fd, other OSes (MacOS, FreeBSD) uses /dev/fd. In order to support theses OSes, use /dev/fd as a fallback.

Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-posix")
---
 util/oslib-posix.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
util/oslib-posix.c +6 -1
index 11b35e48fb..901dcccd73 100644
@@ -814,8 +814,13 @@ static bool qemu_close_all_open_fd_proc(const int *skip, unsigned int nskip)
     int fd, dfd;
     DIR *dir;
     unsigned int skip_start = 0, skip_end = nskip;
+#if defined(CONFIG_LINUX) || defined(CONFIG_SOLARIS)
+    const char *self_fd = "/proc/self/fd";
+#else
+    const char *self_fd = "/dev/fd";
+#endif
 
-    dir = opendir("/proc/self/fd");
+    dir = opendir(self_fd);
     if (!dir) {
         /* If /proc is not mounted, there is nothing that can be done. */
         return false;
Signature · 1 line
2.45.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] qemu/osdep: fix current process fds path for other OSes

Quoted 10 lines · Clément Léger

While Linux and Solaris uses /proc/self/fd, other OSes (MacOS, FreeBSD) uses /dev/fd. In order to support theses OSes, use /dev/fd as a fallback.

Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-posix")
---
 util/oslib-posix.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] qemu/osdep: fix current process fds path for other OSes

In reply to Clément Léger

30.08.2024 14:14, Clément Léger:

Quoted 7 lines

While Linux and Solaris uses /proc/self/fd, other OSes (MacOS, FreeBSD) uses /dev/fd. In order to support theses OSes, use /dev/fd as a fallback.

Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-posix")

/mjt

#4 lore raw

PATCH 2/2 qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

In reply to Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return -1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors as a fallback.

Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-posix")
---
 util/oslib-posix.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
util/oslib-posix.c +12 -0
index 901dcccd73..abf3aa597d 100644
@@ -44,6 +44,11 @@
 #include "qemu/thread-context.h"
 #include "qemu/main-loop.h"
 
+#ifdef CONFIG_DARWIN
+#include <limits.h>
+#include <unistd.h>
+#endif
+
 #ifdef CONFIG_LINUX
 #include <sys/syscall.h>
 #endif
@@ -928,6 +933,13 @@ static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int nskip,
 void qemu_close_all_open_fd(const int *skip, unsigned int nskip)
 {
     int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;
+#else
+        open_max = 1024;
+#endif
+    }
 
     assert(skip != NULL || nskip == 0);
 
Signature · 1 line
2.45.2
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

Quoted 7 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return -1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors as a fallback.

Quoted 6 lines
Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-posix")
---
 util/oslib-posix.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

In reply to Clément Léger
Quoted 10 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return -1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors as a fallback.

Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-posix")

util/oslib-posix.c static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int nskip, · line 941

Quoted 10 lines
util/oslib-posix.c +6 -0
@@ -928,6 +933,13 @@ static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int nskip,
  void qemu_close_all_open_fd(const int *skip, unsigned int nskip)
  {
      int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;
+#else
+        open_max = 1024;
+#endif

BTW, Can we PLEASE cap this to 1024 in all cases? :) (unrelated to this change but still).

/mjt

#7 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

Quoted 15 lines · Michael Tokarev
Quoted 11 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return -1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors as a fallback.

Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-

posix")

Quoted 14 lines

util/oslib-posix.c static void qemu_close_all_open_fd_fallback(const · line 942

Quoted 11 lines
util/oslib-posix.c +6 -0
@@ -928,6 +933,13 @@ static void qemu_close_all_open_fd_fallback(const
 int *skip, unsigned int nskip,
   void qemu_close_all_open_fd(const int *skip, unsigned int nskip)
   {
       int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;
+#else
+        open_max = 1024;
+#endif

BTW, Can we PLEASE cap this to 1024 in all cases? :) (unrelated to this change but still).

Hi Michael,

Do you mean for all OSes or always using 1024 rather than using the sysconf returned value ?

In any case, the code now uses close_range() or /proc/self/fd and is handling that efficiently.

Thanks,

Clément

Quoted 2 lines

/mjt

#8 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

Quoted 16 lines · Clément Léger
Quoted 15 lines · Michael Tokarev
Quoted 11 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return -1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors as a fallback.

Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-

posix")

Quoted 8 lines
Quoted 8 lines

util/oslib-posix.c static void qemu_close_all_open_fd_fallback(const · line 939

Quoted 8 lines
util/oslib-posix.c +3 -0
@@ -928,6 +933,13 @@ static void qemu_close_all_open_fd_fallback(const
 int *skip, unsigned int nskip,
   void qemu_close_all_open_fd(const int *skip, unsigned int nskip)
   {
       int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;

Missing errno check.

Quoted 11 lines
Quoted 6 lines

util/oslib-posix.c static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int nskip, · line 941

Quoted 3 lines
util/oslib-posix.c +3 -0
+#else
+        open_max = 1024;
+#endif

BTW, Can we PLEASE cap this to 1024 in all cases? :) (unrelated to this change but still).

Hi Michael,

Do you mean for all OSes or always using 1024 rather than using the sysconf returned value ?

Alternatively add:

  long qemu_sysconf(int name, long unsupported_default);

which returns value, unsupported_default if not supported, or -1.

Quoted 12 lines

In any case, the code now uses close_range() or /proc/self/fd and is handling that efficiently.

Thanks,

Clément

Quoted 2 lines

/mjt

#9 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

Quoted 18 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Clément Léger
Quoted 16 lines · Michael Tokarev
Quoted 8 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return -1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors
as a fallback.

Quoted 4 lines
Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-

posix")

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

util/oslib-posix.c static void qemu_close_all_open_fd_fallback(const · line 939

Quoted 8 lines
util/oslib-posix.c +3 -0
@@ -928,6 +933,13 @@ static void qemu_close_all_open_fd_fallback(const
 int *skip, unsigned int nskip,
    void qemu_close_all_open_fd(const int *skip, unsigned int nskip)
    {
        int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;

Missing errno check.

man sysconf states that:

"On error, -1 is returned and errno is set to indicate the error (for example, EINVAL, indicating that name is invalid)."

So it seems checking for -1 is enough no ? Or were you thinking about something else ?

Quoted 10 lines
Quoted 10 lines
Quoted 7 lines

util/oslib-posix.c static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int nskip, · line 941

Quoted 4 lines
util/oslib-posix.c +3 -0
+#else
+        open_max = 1024;
+#endif

BTW, Can we PLEASE cap this to 1024 in all cases? :) (unrelated to this change but still).

Hi Michael,

Quoted 8 lines
Quoted 2 lines

Do you mean for all OSes or always using 1024 rather than using the sysconf returned value ?

Alternatively add:

  long qemu_sysconf(int name, long unsupported_default);

which returns value, unsupported_default if not supported, or -1.

Acked, should this be a global function even if only used in the qemu_close_all_open_fd() helper yet ?

Thanks,

Clément

Quoted 14 lines
Quoted 13 lines

In any case, the code now uses close_range() or /proc/self/fd and is handling that efficiently.

Thanks,

Clément

Quoted 2 lines

/mjt

#10 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

Quoted 30 lines · Clément Léger
Quoted 18 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Clément Léger
Quoted 16 lines · Michael Tokarev
Quoted 8 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return -1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors
as a fallback.

Quoted 4 lines
Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-

posix")

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

util/oslib-posix.c static void qemu_close_all_open_fd_fallback(const · line 939

Quoted 8 lines
util/oslib-posix.c +3 -0
@@ -928,6 +933,13 @@ static void qemu_close_all_open_fd_fallback(const
 int *skip, unsigned int nskip,
    void qemu_close_all_open_fd(const int *skip, unsigned int nskip)
    {
        int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;

Missing errno check.

Quoted 7 lines

man sysconf states that:

"On error, -1 is returned and errno is set to indicate the error (for example, EINVAL, indicating that name is invalid)."

So it seems checking for -1 is enough no ? Or were you thinking about something else ?

Mine (macOS 14.6) is:

RETURN VALUES
If the call to sysconf() is not successful, -1 is returned and errno is set appropriately. Otherwise, if the variable is associated with functionality that is not supported, -1 is returned and errno is not modified. Otherwise, the current variable value is returned.

STANDARDS
Except for the fact that values returned by sysconf() may change over the lifetime of the calling process, this function conforms to IEEE Std 1003.1-1988 (“POSIX.1”).

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 8 lines

util/oslib-posix.c static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int nskip, · line 941

Quoted 5 lines
util/oslib-posix.c +3 -0
+#else
+        open_max = 1024;
+#endif

BTW, Can we PLEASE cap this to 1024 in all cases? :) (unrelated to this change but still).

Hi Michael,

Do you mean for all OSes or always using 1024 rather than using the sysconf returned value ?

Quoted 8 lines
Quoted 5 lines

Alternatively add:

  long qemu_sysconf(int name, long unsupported_default);

which returns value, unsupported_default if not supported, or -1.

Acked, should this be a global function even if only used in the qemu_close_all_open_fd() helper yet ?

I'm seeing a few more:

$ git grep -w sysconf | wc -l
      35

From this list a dozen could use qemu_sysconf().

Quoted 20 lines

Thanks,

Clément

Quoted 14 lines
Quoted 13 lines

In any case, the code now uses close_range() or /proc/self/fd and is handling that efficiently.

Thanks,

Clément

Quoted 2 lines

/mjt

#11 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

Quoted 41 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Clément Léger
Quoted 19 lines · Philippe Mathieu-Daudé
Quoted 18 lines · Clément Léger
Quoted 17 lines · Michael Tokarev
Quoted 9 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return
-1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors
as a fallback.

Quoted 4 lines
Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-

posix")

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +0 -0
@@ -928,6 +933,13 @@ static void

qemu_close_all_open_fd_fallback(const
int *skip, unsigned int nskip,
    void qemu_close_all_open_fd(const int *skip, unsigned int nskip) {
        int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;

Missing errno check.

Quoted 7 lines

man sysconf states that:

"On error, -1 is returned and errno is set to indicate the error (for example, EINVAL, indicating that name is invalid)."

So it seems checking for -1 is enough no ? Or were you thinking about something else ?

Quoted 8 lines

Mine (macOS 14.6) is:

 RETURN VALUES
     If the call to sysconf() is not successful, -1 is returned and errno is set appropriately.  Otherwise, if the variable is associated with functionality that is not supported, -1 is returned and errno is not modified.  Otherwise, the current variable value is returned.

Which seems to imply the same than mine right ? -1 is always returned in case of error and errno might or not be set. So checking for -1 seems enough to check an error return.

Quoted 29 lines

 STANDARDS
     Except for the fact that values returned by sysconf() may change over the lifetime of the calling process, this function conforms to IEEE Std 1003.1-1988 (“POSIX.1”).

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 8 lines

util/oslib-posix.c static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int nskip, · line 941

Quoted 5 lines
util/oslib-posix.c +3 -0
+#else
+        open_max = 1024;
+#endif

BTW, Can we PLEASE cap this to 1024 in all cases? :) (unrelated to this change but still).

Hi Michael,

Do you mean for all OSes or always using 1024 rather than using the sysconf returned value ?

Quoted 8 lines
Quoted 5 lines

Alternatively add:

   long qemu_sysconf(int name, long unsupported_default);

which returns value, unsupported_default if not supported, or -1.

Acked, should this be a global function even if only used in the qemu_close_all_open_fd() helper yet ?

Quoted 6 lines

I'm seeing a few more:

$ git grep -w sysconf | wc -l
      35

From this list a dozen could use qemu_sysconf().

Acked.

Quoted 22 lines
Quoted 21 lines

Thanks,

Clément

Quoted 14 lines
Quoted 13 lines

In any case, the code now uses close_range() or /proc/self/fd and is handling that efficiently.

Thanks,

Clément

Quoted 2 lines

/mjt

#12 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

Quoted 51 lines · Clément Léger
Quoted 41 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Clément Léger
Quoted 19 lines · Philippe Mathieu-Daudé
Quoted 18 lines · Clément Léger
Quoted 17 lines · Michael Tokarev
Quoted 9 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return
-1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors
as a fallback.

Quoted 4 lines
Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-

posix")

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +0 -0
@@ -928,6 +933,13 @@ static void

qemu_close_all_open_fd_fallback(const
int *skip, unsigned int nskip,
    void qemu_close_all_open_fd(const int *skip, unsigned int nskip) {
        int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;

Missing errno check.

Quoted 7 lines

man sysconf states that:

"On error, -1 is returned and errno is set to indicate the error (for example, EINVAL, indicating that name is invalid)."

So it seems checking for -1 is enough no ? Or were you thinking about something else ?

Quoted 8 lines

Mine (macOS 14.6) is:

 RETURN VALUES
     If the call to sysconf() is not successful, -1 is returned and errno is set appropriately.  Otherwise, if the variable is associated with functionality that is not supported, -1 is returned and errno is not modified.  Otherwise, the current variable value is returned.

Quoted 3 lines

Which seems to imply the same than mine right ? -1 is always returned in case of error and errno might or not be set. So checking for -1 seems enough to check an error return.

Yes but we can check for the unsupported case. Something like:

    long qemu_sysconf(int name, long unsupported_default)
    {
        int current_errno = errno;
        long retval;

        retval = sysconf(name);
        if (retval == -1) {
            if (errno == current_errno) {
                return unsupported_default;
            }
            perror("sysconf");
            return -1;
        }
        return retval;
    }

(untested)

Quoted 44 lines
Quoted 30 lines

 STANDARDS
     Except for the fact that values returned by sysconf() may change over the lifetime of the calling process, this function conforms to IEEE Std 1003.1-1988 (“POSIX.1”).

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +3 -0
+#else
+        open_max = 1024;
+#endif

BTW, Can we PLEASE cap this to 1024 in all cases? :) (unrelated to this change but still).

Hi Michael,

Do you mean for all OSes or always using 1024 rather than using the sysconf returned value ?

Quoted 8 lines
Quoted 5 lines

Alternatively add:

   long qemu_sysconf(int name, long unsupported_default);

which returns value, unsupported_default if not supported, or -1.

Acked, should this be a global function even if only used in the qemu_close_all_open_fd() helper yet ?

Quoted 6 lines

I'm seeing a few more:

$ git grep -w sysconf | wc -l
       35

From this list a dozen could use qemu_sysconf().

Acked.

Quoted 5 lines
Quoted 5 lines

Thanks,

Clément

#13 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

Quoted 58 lines · Philippe Mathieu-Daudé
Quoted 51 lines · Clément Léger
Quoted 41 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Clément Léger
Quoted 19 lines · Philippe Mathieu-Daudé
Quoted 18 lines · Clément Léger
Quoted 17 lines · Michael Tokarev
Quoted 9 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return
-1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors
as a fallback.

Quoted 4 lines
Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-

posix")

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +0 -0
@@ -928,6 +933,13 @@ static void

qemu_close_all_open_fd_fallback(const
int *skip, unsigned int nskip,
    void qemu_close_all_open_fd(const int *skip, unsigned int nskip) {
        int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;

Missing errno check.

Quoted 7 lines

man sysconf states that:

"On error, -1 is returned and errno is set to indicate the error (for example, EINVAL, indicating that name is invalid)."

So it seems checking for -1 is enough no ? Or were you thinking about something else ?

Quoted 8 lines

Mine (macOS 14.6) is:

 RETURN VALUES
     If the call to sysconf() is not successful, -1 is returned and errno is set appropriately.  Otherwise, if the variable is associated with functionality that is not supported, -1 is returned and errno is not modified.  Otherwise, the current variable value is returned.

Quoted 3 lines

Which seems to imply the same than mine right ? -1 is always returned in case of error and errno might or not be set. So checking for -1 seems enough to check an error return.

Yes but we can check for the unsupported case. Something like:

Quoted 15 lines
    long qemu_sysconf(int name, long unsupported_default)
    {
        int current_errno = errno;
        long retval;

        retval = sysconf(name);
        if (retval == -1) {
            if (errno == current_errno) {
                return unsupported_default;
            }
            perror("sysconf");
            return -1;
        }
        return retval;
    }

That looks uncessarily complicated, and IMHO makes it less portable. eg consider macOS behaviour:

"if the variable is associated with functionality that is
not supported, -1 is returned and errno is not modified"

vs Linux documented behaviour:

"If name corresponds to a maximum or minimum limit, and
that limit is indeterminate, -1 is returned and errno is not changed."

IMHO we should do what Clément already suggested and set a default anytime retval == -1, and ignore errno. There is no user benefit from turning errnos into a fatal error via perror()

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#14 lore raw

Re: [PATCH 2/2] qemu/osdep: handle sysconf(_SC_OPEN_MAX) return value == -1

Quoted 83 lines · Daniel P. Berrangé
Quoted 58 lines · Philippe Mathieu-Daudé
Quoted 51 lines · Clément Léger
Quoted 41 lines · Philippe Mathieu-Daudé
Quoted 32 lines · Clément Léger
Quoted 19 lines · Philippe Mathieu-Daudé
Quoted 18 lines · Clément Léger
Quoted 17 lines · Michael Tokarev
Quoted 9 lines · Clément Léger

On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return
-1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors
as a fallback.

Quoted 4 lines
Fixes 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-

posix")

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +0 -0
@@ -928,6 +933,13 @@ static void

qemu_close_all_open_fd_fallback(const
int *skip, unsigned int nskip,
    void qemu_close_all_open_fd(const int *skip, unsigned int nskip) {
        int open_max = sysconf(_SC_OPEN_MAX);
+    if (open_max == -1) {
+#ifdef CONFIG_DARWIN
+        open_max = OPEN_MAX;

Missing errno check.

Quoted 7 lines

man sysconf states that:

"On error, -1 is returned and errno is set to indicate the error (for example, EINVAL, indicating that name is invalid)."

So it seems checking for -1 is enough no ? Or were you thinking about something else ?

Quoted 8 lines

Mine (macOS 14.6) is:

 RETURN VALUES
     If the call to sysconf() is not successful, -1 is returned and errno is set appropriately.  Otherwise, if the variable is associated with functionality that is not supported, -1 is returned and errno is not modified.  Otherwise, the current variable value is returned.

Quoted 3 lines

Which seems to imply the same than mine right ? -1 is always returned in case of error and errno might or not be set. So checking for -1 seems enough to check an error return.

Yes but we can check for the unsupported case. Something like:

Quoted 15 lines
     long qemu_sysconf(int name, long unsupported_default)
     {
         int current_errno = errno;
         long retval;

         retval = sysconf(name);
         if (retval == -1) {
             if (errno == current_errno) {
                 return unsupported_default;
             }
             perror("sysconf");
             return -1;
         }
         return retval;
     }

That looks uncessarily complicated, and IMHO makes it less portable. eg consider macOS behaviour:

"if the variable is associated with functionality that is
not supported, -1 is returned and errno is not modified"

vs Linux documented behaviour:

Quoted 8 lines

"If name corresponds to a maximum or minimum limit, and
that limit is indeterminate, -1 is returned and errno is not changed."

IMHO we should do what Clément already suggested and set a default anytime retval == -1, and ignore errno. There is no user benefit from turning errnos into a fatal error via perror()

Fine by me.