610-add-renameat2-linux-syscall-wrapper.patch 1.8 KB

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