b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | From dc651fe2e6b16087c519c0bd0bf943cb7c53c807 Mon Sep 17 00:00:00 2001 |
| 2 | In-Reply-To: <20240423234355.2414567-1-Tony.Ambardar@gmail.com> |
| 3 | References: <20240423234355.2414567-1-Tony.Ambardar@gmail.com> |
| 4 | From: Tony Ambardar <Tony.Ambardar@gmail.com> |
| 5 | Date: Sat, 20 Apr 2024 21:30:13 -0700 |
| 6 | Subject: [PATCH v3] add renameat2 linux syscall wrapper |
| 7 | To: musl@lists.openwall.com |
| 8 | Cc: Rich Felker <dalias@libc.org> |
| 9 | |
| 10 | This syscall is available since Linux 3.15 and also implemented in glibc |
| 11 | from version 2.28. It is commonly used in filesystem or security contexts. |
| 12 | |
| 13 | Constants RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by |
| 14 | _GNU_SOURCE as with glibc. |
| 15 | |
| 16 | Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com> |
| 17 | --- |
| 18 | v2 -> v3: |
| 19 | * call SYS_renameat first if applicable |
| 20 | * drop unneeded error code handling |
| 21 | |
| 22 | v1 -> v2: |
| 23 | * align related constants |
| 24 | * drop 'int' from 'unsigned int' |
| 25 | * add fallback to SYS_renameat where applicable |
| 26 | --- |
| 27 | include/stdio.h | 7 +++++++ |
| 28 | src/linux/renameat2.c | 11 +++++++++++ |
| 29 | 2 files changed, 18 insertions(+) |
| 30 | create mode 100644 src/linux/renameat2.c |
| 31 | |
| 32 | --- a/include/stdio.h |
| 33 | +++ b/include/stdio.h |
| 34 | @@ -158,6 +158,13 @@ char *ctermid(char *); |
| 35 | #define L_ctermid 20 |
| 36 | #endif |
| 37 | |
| 38 | +#if defined(_GNU_SOURCE) |
| 39 | +#define RENAME_NOREPLACE (1 << 0) |
| 40 | +#define RENAME_EXCHANGE (1 << 1) |
| 41 | +#define RENAME_WHITEOUT (1 << 2) |
| 42 | + |
| 43 | +int renameat2(int, const char *, int, const char *, unsigned); |
| 44 | +#endif |
| 45 | |
| 46 | #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \ |
| 47 | || defined(_BSD_SOURCE) |
| 48 | --- /dev/null |
| 49 | +++ b/src/linux/renameat2.c |
| 50 | @@ -0,0 +1,11 @@ |
| 51 | +#define _GNU_SOURCE |
| 52 | +#include <stdio.h> |
| 53 | +#include "syscall.h" |
| 54 | + |
| 55 | +int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned flags) |
| 56 | +{ |
| 57 | +#ifdef SYS_renameat |
| 58 | + if (!flags) return syscall(SYS_renameat, oldfd, old, newfd, new); |
| 59 | +#endif |
| 60 | + return syscall(SYS_renameat2, oldfd, old, newfd, new, flags); |
| 61 | +} |