changeset 11895:3ef16b8c79cd

Avoid running into nonexistent system calls repeatedly.
author Bruno Haible <bruno@clisp.org>
date Mon, 24 Aug 2009 02:18:15 +0200
parents 6b94c8deb683
children 5982a4bdef97
files ChangeLog lib/dup3.c lib/pipe2.c
diffstat 3 files changed, 30 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- a/lib/dup3.c
+++ b/lib/dup3.c
@@ -53,9 +53,18 @@
   /* 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
 
--- a/lib/pipe2.c
+++ b/lib/pipe2.c
@@ -45,9 +45,18 @@
   /* 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