[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for chown (PR #241767)
via libc-commits
libc-commits at lists.llvm.org
Thu Nov 12 02:27:17 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Connector Switch (c8ef)
<details>
<summary>Changes</summary>
This PR move chown behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
---
Full diff: https://github.com/llvm/llvm-project/pull/241767.diff
5 Files Affected:
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+15)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/chown.h (+42)
- (modified) libc/src/unistd/linux/CMakeLists.txt (+5-4)
- (modified) libc/src/unistd/linux/chown.cpp (+10-16)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+37)
``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index f16bd7fa7c329..7fd9991be0e16 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 0000000000000..d80078c698c68
--- /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 bb8086513f515..5a44bbd3d9caa 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 1e7817a1e91a9..edec7d3512258 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 997e85b2fa20b..a1cc803e9b35e 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"],
``````````
</details>
https://github.com/llvm/llvm-project/pull/241767
More information about the libc-commits
mailing list