[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for chown (PR #241767)

Connector Switch via libc-commits libc-commits at lists.llvm.org
Thu Nov 12 02:26:35 PST 2026


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/241767

This PR move chown behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.


>From f52dc657ed1f60815eaaab98030d3b1c026460de Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Thu, 12 Nov 2026 18:25:36 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for chown

---
 .../linux/syscall_wrappers/CMakeLists.txt     | 15 +++++++
 .../OSUtil/linux/syscall_wrappers/chown.h     | 42 +++++++++++++++++++
 libc/src/unistd/linux/CMakeLists.txt          |  9 ++--
 libc/src/unistd/linux/chown.cpp               | 26 +++++-------
 .../llvm-project-overlay/libc/BUILD.bazel     | 37 ++++++++++++++++
 5 files changed, 109 insertions(+), 20 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/chown.h

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index f16bd7fa7c3291..7fd9991be0e16f 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1179,6 +1179,21 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  chown
+  HDRS
+    chown.h
+  DEPENDS
+    libc.hdr.fcntl_macros
+    libc.hdr.types.gid_t
+    libc.hdr.types.uid_t
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   fchmod
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/chown.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/chown.h
new file mode 100644
index 00000000000000..d80078c698c682
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/chown.h
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for chown.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CHOWN_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CHOWN_H
+
+#include "hdr/fcntl_macros.h"
+#include "hdr/types/gid_t.h"
+#include "hdr/types/uid_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> chown(const char *path, uid_t owner, gid_t group) {
+#ifdef SYS_chown
+  return syscall_checked<int>(SYS_chown, path, owner, group);
+#elif defined(SYS_fchownat)
+  return syscall_checked<int>(SYS_fchownat, AT_FDCWD, path, owner, group, 0);
+#else
+#error "chown and fchownat syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CHOWN_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index bb8086513f5159..5a44bbd3d9caac 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -39,11 +39,12 @@ 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
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.types.uid_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.chown
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
     libc.src.errno.errno
 )
 
diff --git a/libc/src/unistd/linux/chown.cpp b/libc/src/unistd/linux/chown.cpp
index 1e7817a1e91a9a..edec7d35122582 100644
--- a/libc/src/unistd/linux/chown.cpp
+++ b/libc/src/unistd/linux/chown.cpp
@@ -1,35 +1,29 @@
-//===-- Linux implementation of chown -------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation for chown.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/unistd/chown.h"
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/chown.h"
 #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.
 
 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;
+  auto result = linux_syscalls::chown(path, owner, group);
+  if (!result) {
+    libc_errno = result.error();
     return -1;
   }
   return 0;
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 997e85b2fa20b4..a1cc803e9b35e8 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2667,6 +2667,24 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_chown",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/chown.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+        ":hdr_fcntl_macros",
+        ":types_gid_t",
+        ":types_uid_t",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_dup",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/dup.h"],
@@ -18564,6 +18582,25 @@ libc_function(
     ],
 )
 
+libc_function(
+    name = "chown",
+    srcs = ["src/unistd/linux/chown.cpp"],
+    hdrs = ["src/unistd/chown.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_libc_errno",
+        ":__support_macros_config",
+        ":__support_osutil_linux_syscall_wrappers_chown",
+        ":errno",
+        ":types_gid_t",
+        ":types_uid_t",
+    ],
+)
+
 libc_function(
     name = "close",
     srcs = ["src/unistd/linux/close.cpp"],



More information about the libc-commits mailing list