Skip to content

Commit

Permalink
toolchain/musl: add support for renameat2()
Browse files Browse the repository at this point in the history
Backport an upstream patch to support the renameat2 syscall, added in Linux
3.15 and supported by glibc since 2.28. It is commonly used in filesystem
or security contexts, and needed building upstream kernel bpf selftests.

Link:
https://inbox.vuxu.org/musl/[email protected]/

Signed-off-by: Tony Ambardar <[email protected]>
  • Loading branch information
guidosarducci committed Jun 12, 2024
1 parent f95e3e1 commit b010450
Showing 1 changed file with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
From dc651fe2e6b16087c519c0bd0bf943cb7c53c807 Mon Sep 17 00:00:00 2001
In-Reply-To: <[email protected]>
References: <[email protected]>
From: Tony Ambardar <[email protected]>
Date: Sat, 20 Apr 2024 21:30:13 -0700
Subject: [PATCH v3] add renameat2 linux syscall wrapper
To: [email protected]
Cc: Rich Felker <[email protected]>

This syscall is available since Linux 3.15 and also implemented in glibc
from version 2.28. It is commonly used in filesystem or security contexts.

Constants RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
_GNU_SOURCE as with glibc.

Signed-off-by: Tony Ambardar <[email protected]>
---
v2 -> v3:
* call SYS_renameat first if applicable
* drop unneeded error code handling

v1 -> v2:
* align related constants
* drop 'int' from 'unsigned int'
* add fallback to SYS_renameat where applicable
---
include/stdio.h | 7 +++++++
src/linux/renameat2.c | 11 +++++++++++
2 files changed, 18 insertions(+)
create mode 100644 src/linux/renameat2.c

diff --git a/include/stdio.h b/include/stdio.h
index cb858618..4ea4c170 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -158,6 +158,13 @@ char *ctermid(char *);
#define L_ctermid 20
#endif

+#if defined(_GNU_SOURCE)
+#define RENAME_NOREPLACE (1 << 0)
+#define RENAME_EXCHANGE (1 << 1)
+#define RENAME_WHITEOUT (1 << 2)
+
+int renameat2(int, const char *, int, const char *, unsigned);
+#endif

#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
|| defined(_BSD_SOURCE)
diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
new file mode 100644
index 00000000..b8060388
--- /dev/null
+++ b/src/linux/renameat2.c
@@ -0,0 +1,11 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include "syscall.h"
+
+int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned flags)
+{
+#ifdef SYS_renameat
+ if (!flags) return syscall(SYS_renameat, oldfd, old, newfd, new);
+#endif
+ return syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
+}
--
2.34.1

0 comments on commit b010450

Please sign in to comment.