Skip to content

[libc][stdio] implement rename via SYS_renameat2 #86140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2024

Conversation

nickdesaulniers
Copy link
Member

@nickdesaulniers nickdesaulniers commented Mar 21, 2024

SYS_rename may be unavailable on architectures such as aarch64 and riscv.
rename can be implemented in terms of SYS_rename, SYS_renameat, or
SYS_renameat2. I don't have a full picture of the history here, but it seems
that SYS_renameat might also be unavailable on some platforms.

man 2 rename mentions that SYS_renameat2 was added in Linux 3.15. We don't
need to support such ancient kernel versions prior.

Link: #84980
Link: #85068

SYS_rename may be unavailable on architectures such as aarch64 and riscv.
rename can be implemented in terms of SYS_rename, SYS_renameat, or
SYS_renameat2. I don't have a full picture of the history here, but it seems
that SYS_renameat might also be unavailable on some platforms.

`man 2 rename` mentions that SYS_renameat2 was added in Linux 3.15.  We don't
need to support such ancient kernel versions.

Link: llvm#84980
Link: llvm#85068
@nickdesaulniers
Copy link
Member Author

cc @aniplcc

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

SYS_rename may be unavailable on architectures such as aarch64 and riscv.
rename can be implemented in terms of SYS_rename, SYS_renameat, or
SYS_renameat2. I don't have a full picture of the history here, but it seems
that SYS_renameat might also be unavailable on some platforms.

man 2 rename mentions that SYS_renameat2 was added in Linux 3.15. We don't
need to support such ancient kernel versions.

Link: #84980
Link: #85068


Full diff: https://github.com/llvm/llvm-project/pull/86140.diff

1 Files Affected:

  • (modified) libc/src/stdio/linux/rename.cpp (+2-1)
diff --git a/libc/src/stdio/linux/rename.cpp b/libc/src/stdio/linux/rename.cpp
index f3d684249ad28e..afa025864fb1c5 100644
--- a/libc/src/stdio/linux/rename.cpp
+++ b/libc/src/stdio/linux/rename.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "include/llvm-libc-macros/linux/fcntl-macros.h"
 #include "src/stdio/rename.h"
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
@@ -15,7 +16,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(int, rename, (const char *oldpath, const char *newpath)) {
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_rename, oldpath, newpath);
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_renameat2, AT_FDCWD, oldpath, AT_FDCWD, newpath, 0);
 
   if (ret >= 0)
     return 0;

Copy link

github-actions bot commented Mar 21, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@nickdesaulniers
Copy link
Member Author

https://www.kernel.org/doc/html/next/process/adding-syscalls.html#designing-the-api-planning-for-extension mentions:

A new system call forms part of the API of the kernel, and has to be supported indefinitely. As such, it's a very good idea to explicitly discuss the interface on the kernel mailing list, and it's important to plan for future extensions of the interface.

(The syscall table is littered with historical examples where this wasn't done, together with the corresponding follow-up system calls -- eventfd/eventfd2, dup2/dup3, inotify_init/inotify_init1, pipe/pipe2, renameat/renameat2 -- so learn from the history of the kernel and plan for extensions from the start.)

(emphasis added)

@nickdesaulniers
Copy link
Member Author

Merging this to unbreak the build. @michaelrj-google when you're online let's chat about what to do for cases like this in the future. I'm happy to write up public docs for whatever policy we decide on.

@nickdesaulniers nickdesaulniers merged commit 6eff53b into llvm:main Mar 21, 2024
@nickdesaulniers nickdesaulniers deleted the renameat2 branch March 21, 2024 16:35
gchatelet added a commit to gchatelet/llvm-project that referenced this pull request Mar 21, 2024
nickdesaulniers pushed a commit that referenced this pull request Mar 21, 2024
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
SYS_rename may be unavailable on architectures such as aarch64 and
riscv.
rename can be implemented in terms of SYS_rename, SYS_renameat, or
SYS_renameat2. I don't have a full picture of the history here, but it
seems
that SYS_renameat might also be unavailable on some platforms.

`man 2 rename` mentions that SYS_renameat2 was added in Linux 3.15. We
don't
need to support such ancient kernel versions prior.

Link: llvm#84980
Link: llvm#85068
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants