[libc-commits] [libc] [libc] Add support for chown on platforms that don't define SYS_chown (PR #186167)

via libc-commits libc-commits at lists.llvm.org
Thu Mar 12 09:22:59 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Mikhail R. Gadelha (mikhailramalho)

<details>
<summary>Changes</summary>

Some platforms don't define SYS_chown (like risc-v), so this PR adds a fallback to calling SYS_fchownat.

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


3 Files Affected:

- (modified) libc/config/linux/riscv/entrypoints.txt (+1) 
- (modified) libc/src/unistd/linux/CMakeLists.txt (+1) 
- (modified) libc/src/unistd/linux/chown.cpp (+9) 


``````````diff
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index a0a2a486c761f..7230b5e44ab73 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -324,6 +324,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     # unistd.h entrypoints
     libc.src.unistd.access
     libc.src.unistd.chdir
+    libc.src.unistd.chown
     libc.src.unistd.close
     libc.src.unistd.dup
     libc.src.unistd.dup2
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 560c78552fec8..9e530b634e566 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -32,6 +32,7 @@ add_entrypoint_object(
   HDRS
     ../chown.h
   DEPENDS
+    libc.hdr.fcntl_macros
     libc.hdr.types.uid_t
     libc.hdr.types.gid_t
     libc.include.sys_syscall
diff --git a/libc/src/unistd/linux/chown.cpp b/libc/src/unistd/linux/chown.cpp
index c7bf1703ffe57..1e7817a1e91a9 100644
--- a/libc/src/unistd/linux/chown.cpp
+++ b/libc/src/unistd/linux/chown.cpp
@@ -11,6 +11,7 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 #include <sys/syscall.h> // For syscall numbers.
@@ -18,7 +19,15 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, chown, (const char *path, uid_t owner, gid_t group)) {
+#ifdef SYS_chown
   int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_chown, path, owner, group);
+#elif defined(SYS_fchownat)
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fchownat, AT_FDCWD, path,
+                                              owner, group, 0);
+#else
+#error "chown and fchownat syscalls not available."
+#endif
+
   if (ret < 0) {
     libc_errno = -ret;
     return -1;

``````````

</details>


https://github.com/llvm/llvm-project/pull/186167


More information about the libc-commits mailing list