[PATCH 2/9] tools/nolibc: make dup2() rely on dup3() when available

From: Willy Tarreau
Date: Thu Jan 21 2021 - 02:28:42 EST


A recent boot failure on 5.4-rc3 on arm64 revealed that sys_dup2()
is not available and that only sys_dup3() is implemented, thus let's
detect this and fall back to sys_dup3() when available.
[This is nolibc's upstream commit fd5272ec2c66]

Signed-off-by: Willy Tarreau <w@xxxxxx>
---
tools/include/nolibc/nolibc.h | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)

diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
index 3115c6467d10..5fda4d844054 100644
--- a/tools/include/nolibc/nolibc.h
+++ b/tools/include/nolibc/nolibc.h
@@ -1502,10 +1502,22 @@ int sys_dup(int fd)
return my_syscall1(__NR_dup, fd);
}

+#ifdef __NR_dup3
+static __attribute__((unused))
+int sys_dup3(int old, int new, int flags)
+{
+ return my_syscall3(__NR_dup3, old, new, flags);
+}
+#endif
+
static __attribute__((unused))
int sys_dup2(int old, int new)
{
+#ifdef __NR_dup3
+ return my_syscall3(__NR_dup3, old, new, 0);
+#else
return my_syscall2(__NR_dup2, old, new);
+#endif
}

static __attribute__((unused))
@@ -1876,6 +1888,20 @@ int dup2(int old, int new)
return ret;
}

+#ifdef __NR_dup3
+static __attribute__((unused))
+int dup3(int old, int new, int flags)
+{
+ int ret = sys_dup3(old, new, flags);
+
+ if (ret < 0) {
+ SET_ERRNO(-ret);
+ ret = -1;
+ }
+ return ret;
+}
+#endif
+
static __attribute__((unused))
int execve(const char *filename, char *const argv[], char *const envp[])
{
--
2.28.0