From 049bfdaa5dfee0ab9b45c089a078db7db595c28c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 24 Aug 2009 02:18:15 +0200 Subject: [PATCH] Avoid running into nonexistent system calls repeatedly. --- ChangeLog | 6 ++++++ lib/dup3.c | 15 ++++++++++++--- lib/pipe2.c | 15 ++++++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index a24ed56193..efac0df2fd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2009-08-23 Bruno Haible + + * 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 Tolerate declared but missing dup3 syscall. diff --git a/lib/dup3.c b/lib/dup3.c index 62cdb53d31..906594e469 100644 --- a/lib/dup3.c +++ b/lib/dup3.c @@ -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 diff --git a/lib/pipe2.c b/lib/pipe2.c index c18860de30..d3b612d440 100644 --- a/lib/pipe2.c +++ b/lib/pipe2.c @@ -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 -- 2.30.2