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

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Thu Mar 12 09:22:29 PDT 2026


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

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

>From 0efd3e9cff4a426eb37e814249fc124665815494 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Thu, 12 Mar 2026 13:19:10 -0300
Subject: [PATCH] [libc] Add support for chown on platforms that don't define
 SYS_chown

Some platforms don't define SYS_chown (like risc-v), so this PR adds a
fallback to calling SYS_fchownat.
---
 libc/config/linux/riscv/entrypoints.txt | 1 +
 libc/src/unistd/linux/CMakeLists.txt    | 1 +
 libc/src/unistd/linux/chown.cpp         | 9 +++++++++
 3 files changed, 11 insertions(+)

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;



More information about the libc-commits mailing list