Avoid running into nonexistent system calls repeatedly.
authorBruno Haible <bruno@clisp.org>
Mon, 24 Aug 2009 00:18:15 +0000 (02:18 +0200)
committerBruno Haible <bruno@clisp.org>
Mon, 24 Aug 2009 00:18:15 +0000 (02:18 +0200)
ChangeLog
lib/dup3.c
lib/pipe2.c

index a24ed561937d36a11f1b19a3f5057215ab38a39a..efac0df2fd96723ddecc80c1a5b7211b696adeb3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2009-08-23  Bruno Haible  <bruno@clisp.org>
+
+       * lib/dup3.c (dup3): Test only once whether the system actually exists.
+       * lib/pipe2.c (pipe2): Likewise.
+       Suggested by Eric Blake.
+
 2009-08-23  Bruno Haible  <bruno@clisp.org>
 
        Tolerate declared but missing dup3 syscall.
index 62cdb53d317a73611339ed649d8a3f9fbe4fe3b0..906594e46978bc2b5e3e29f6fd5833c7d3273eba 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
 
index c18860de3074ad98d7e6ebc75cac9f471dba1a0d..d3b612d440b2f6397d684e92b5036f122ea6a9db 100644 (file)
@@ -45,9 +45,18 @@ pipe2 (int fd[2], 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 = pipe2 (fd, flags);
-    if (!(result < 0 && errno == ENOSYS))
-      return result;
+    /* Cache the information whether the system call really exists.  */
+    static int have_pipe2_really; /* 0 = unknown, 1 = yes, -1 = no */
+    if (have_pipe2_really >= 0)
+      {
+       int result = pipe2 (fd, flags);
+       if (!(result < 0 && errno == ENOSYS))
+         {
+           have_pipe2_really = 1;
+           return result;
+         }
+       have_pipe2_really = -1;
+      }
   }
 #endif