Subject: Re: bug#32796: please consider using renameat2 from glibc if available

From: Paul Eggert <[email-masked]>
Date: Sun, 7 Oct 2018 23:27:34 -0700

Thanks for checking. I installed the attached into Gnulib master. The "(tiny 
change)" is because the patch is small enough that we don't need to worry about 
copyright papers.
>From c50cf67bd7ff70525f3cb4074f0d9cc1f5c6cf9c Mon Sep 17 00:00:00 2001
From: Andreas Henriksson <[email-masked]>
Date: Sun, 7 Oct 2018 15:01:07 -0700
Subject: [PATCH] renameatu: prefer renameat2 to syscall

* lib/renameatu.c (renameatu) [HAVE_RENAMEAT2]:
Use renameat2 instead of syscall (Bug#32796).
* m4/renameat.m4 (gl_FUNC_RENAMEAT): Check for renameat2.
---
 ChangeLog       | 7 +++++++
 lib/renameatu.c | 5 ++++-
 m4/renameat.m4  | 2 +-
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9726fdfe1..a91bbd139 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2018-10-07  Andreas Henriksson  <[email-masked]>  (tiny change)
+
+	renameatu: prefer renameat2 to syscall
+	* lib/renameatu.c (renameatu) [HAVE_RENAMEAT2]:
+	Use renameat2 instead of syscall (Bug#32796).
+	* m4/renameat.m4 (gl_FUNC_RENAMEAT): Check for renameat2.
+
 2018-10-07  Benno Schulenberg  <[email-masked]>
 
 	bootstrap, gnulib-tool: use https instead of insecure rsync
diff --git a/lib/renameatu.c b/lib/renameatu.c
index b013ad697..58eee92ec 100644
--- a/lib/renameatu.c
+++ b/lib/renameatu.c
@@ -80,7 +80,10 @@ renameatu (int fd1, char const *src, int fd2, char const *dst,
   int ret_val = -1;
   int err = EINVAL;
 
-#ifdef SYS_renameat2
+#ifdef HAVE_RENAMEAT2
+  ret_val = renameat2 (fd1, src, fd2, dst, flags);
+  err = errno;
+#elif defined SYS_renameat2
   ret_val = syscall (SYS_renameat2, fd1, src, fd2, dst, flags);
   err = errno;
 #elif defined RENAME_EXCL
diff --git a/m4/renameat.m4 b/m4/renameat.m4
index 116750785..9101844c4 100644
--- a/m4/renameat.m4
+++ b/m4/renameat.m4
@@ -15,7 +15,7 @@ AC_DEFUN([gl_FUNC_RENAMEAT],
   AC_REQUIRE([gl_STDIO_H_DEFAULTS])
   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
   AC_CHECK_HEADERS([linux/fs.h])
-  AC_CHECK_FUNCS_ONCE([renameat])
+  AC_CHECK_FUNCS_ONCE([renameat renameat2])
   if test $ac_cv_func_renameat = no; then
     HAVE_RENAMEAT=0
   elif test $REPLACE_RENAME = 1; then
-- 
2.17.1