Avoid running into nonexistent system calls repeatedly.
[gnulib.git] / lib / dup3.c
index 62cdb53..906594e 100644 (file)
@@ -53,9 +53,18 @@ dup3 (int oldfd, int newfd, int flags)
   /* Try the system call first, if it exists.  (We may be running with a glibc
      that has the function but with an older kernel that lacks it.)  */
   {
-    int result = dup3 (oldfd, newfd, flags);
-    if (!(result < 0 && errno == ENOSYS))
-      return result;
+    /* Cache the information whether the system call really exists.  */
+    static int have_dup3_really; /* 0 = unknown, 1 = yes, -1 = no */
+    if (have_dup3_really >= 0)
+      {
+       int result = dup3 (oldfd, newfd, flags);
+       if (!(result < 0 && errno == ENOSYS))
+         {
+           have_dup3_really = 1;
+           return result;
+         }
+       have_dup3_really = -1;
+      }
   }
 #endif