[libc-commits] [libc] [libc] Implement fchown (PR #167286)
Anshul Nigham via libc-commits
libc-commits at lists.llvm.org
Sun Nov 9 23:48:47 PST 2025
https://github.com/nigham created https://github.com/llvm/llvm-project/pull/167286
- **[RISCV] Optimize (and (icmp x, 0, neq), (icmp y, 0, neq)) utilizing zicond extension**
- **[RISCV] Support outlining of CFI instructions in the machine outliner (#166149)**
- **[NFCI][lldb][test] Avoid unnecessary GNU extension for assembly call (#166769)**
- **[orc-rt] Add endian_read/write operations. (#166892)**
- **[libc++] Simplify most of the segmented iterator optimizations (#164797)**
- **[libcxxabi][ItaniumDemangle] Separate GtIsGt counter into more states (#166578)**
- **[VPlan] Inline WidenSelect::isInvariantCond (NFC) (#166742)**
- **[mlir][linalg-transform] dyn_cast DestinationStyleOpInterface and early return (#166299)**
- **[VPlan] Remove no-longer-needed EVL VPlan debug output tests. NFC (#166158)**
- **[Runtimes][CMake] Add CMake option to enable execute-only code generation on AArch64 (#143698)**
- **[compiler-rt] Add CMake option to enable execute-only code generation on AArch64 (#140555)**
- **[libunwind] Add CMake option to enable execute-only code generation on AArch64 (#140554)**
- **[libclang/python] Return actual C types from libclang functions instead of Python bool (#166446)**
- **[ValueTracking] Refine known bits for linear interpolation patterns (#166378)**
- **[flang] remove bogus header include from #164988 (#166906)**
- **Reapply "[lldb-dap] Use protocol types for exceptioninfo" (#166161) (#166836)**
- **[flang][OpenACC] lower acc loops with early exits (#164992)**
- **[libc][math] Enable math acos for baremetal Arm and AArch64 (#166749)**
- **[mlir][spirv] Enable validation of selection and phi tests (#166794)**
- **[mlir][spirv] Add validation for loop and struct tests (#166803)**
- **[clang][bytecode] Dummy variables can have pointers to them (#166908)**
- **Fix bazel build issue from #165615 (#166918)**
- **Fix bazel build issue I introduced in #166918 (#166921)**
- **[libc++] Optimize fstream::read (#165223)**
- **[DAG] visitBITCAST - fold (bitcast (freeze (load x))) -> (freeze (load (bitcast*)x)) (#164618)**
- **[NVGPU] Fix nvdsl examples (#156830)**
- **[bazel][mlir] Fix transform_xegpu_ext.py test for bazel (#166924)**
- **[X86][Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - add AVX512 KTEST/KORTEST intrinsics to be used in constexpr (#166103)**
- **[OpenMP] Add definitions for DECLARE_INDUCTION and related clauses (#166235)**
- **[X86] Add some initial add i64 test coverage for #142308 (#166929)**
- **[LoongArch] Add `isSafeToMove` hook to prevent unsafe instruction motion (#163725)**
- **[MLIR][Pygments] Refine the pygments MLIR lexer (#166406)**
- **[LLDB] Run MSVC STL string(-view) tests with PDB (#166833)**
- **[mlir] Use LDBG to replace LLVM_DEBUG in IntegerRelation.cpp (NFC) (#166772)**
- **[VPlan] Support multiple F(Max|Min)Num reductions. (#161735)**
- **[MLIR][Transform][Python] Sync derived classes and their wrappers (#166871)**
- **Revert "Ignore trailing NullStmts in StmtExprs for GCC compatibility." (#166036)**
- **Revert "[NVGPU] Fix nvdsl examples" (#166943)**
- **[Clang] Fix comment typo in BuiltinTargetFeatures.h**
- **[InstCombnine] Add test for sinking with dereferneceable assumes.**
- **[AArch64][llvm] Add support for new vcvt* intrinsics (#163572)**
- **[AArch64][CostModel] Replace undef with poison in sve-arith-fp.ll (NFC) (#166930)**
- **[clang-format] Add option to separate comment alignment between ... (#165033)**
- **[AArch64][llvm] Add support for Neon vmmlaq_{f16,f32}_mf8_fpm intrinsics (#165431)**
- **[AMDGPU] Delete redundant s_or_b32 (#165261)**
- **[PowerPC] Lowering support for EVL type VP_LOAD/VP_STORE (#165910)**
- **Add `llvm.vector.partial.reduce.fadd` intrinsic (#159776)**
- **[BOLT] Remove redundant declarations (NFC) (#166893)**
- **[clang-tools-extra] Remove redundant declarations (NFC) (#166894)**
- **[Demangle] Remove redundant declarations (NFC) (#166895)**
- **[mlir] Remove redundant declarations (NFC) (#166896)**
- **[Flang][OpenMP] Unify MapInfoFinalization's BoxChar handling with other Box types (#165954)**
- **[CAS] Fix wrong usage of `llvm::sort()` in UnifiedOnDiskCache (#166963)**
- **[flang] Explicit interface externals are constant expressions (#166181)**
- **[flang] Improve warning text (#166407)**
- **[flang][runtime] Allow some list-directed child output to advance (#166847)**
- **[libc++][test] Fix-up tests for `is_clock(_v)` (#166888)**
- **[bazel] Add mlir/utils/generate-test-checks.py to bazel overlay (#160693)**
- **[clang][CIR] Fix build. NFC**
- **[clang] Proofread *.rst (#166897)**
- **[X86] isGuaranteedNotToBeUndefOrPoison - add simple target shuffles with known test coverage (#161553)**
- **[flang][NFC] Strip trailing whitespace from tests (1 of N)**
- **Revert "[NFCI][lldb][test] Avoid unnecessary GNU extension for assembly call" (#166970)**
- **[mlir][arith] Fix `arith.select` lowering after #166513 (#166692)**
- **[lldb] Correctly detach (rather than kill) when connecting with gdb-remote (#166869)**
- **[DA] Properly pass outermost loop to monotonicity checker (#166928)**
- **[NFC][AMDGPU][GISel] Precommit GlobalISel specific tests for call instruction (#165898)**
- **AMDGPU: Preliminary documentation for named barriers (#165502)**
- **[CAS] Add llvm-cas tools to inspect on-disk LLVMCAS (#166481)**
- **[AMDGPU][NFC] Pre-commit shlN_add test results with sdag (#166636)**
- **[VectorCombine] support mismatching extract/insert indices for foldInsExtFNeg (#126408)**
- **[libc] Fix wrapper headers for `at_quick_exit` on GLIBC for C++11 (#166960)**
- **[mlir][NVVM] Add nvvm.membar operation (#166698)**
- **[Github][Bazel] Add Workflow to Run Bazel Build (#165071)**
- **[libc] add cpu feature flags for SVE/SVE2/MOPS (#166884)**
- **[MLIR][XeVM] Update XeVM prefetch ops. (#166445)**
- **[dsymutil] Fix parallel linker's self-recursive typedef DIE by including referred-to types into synthetic name (#166767)**
- **[CIR] Recognize constant aggregate initialization of auto vars (#166850)**
- **[clang][CIR] Port test from d9c7c76**
- **Fix a crash when a stop hook deletes itself in its callback. (#160416)**
- **[flang][cuda][NFC] Use NVVM operation for thread syncs (#166999)**
- **[AMDGPU] Stop optimising readfirstlane in pass AMDGPUUniformIntrinsicCombine (#166955)**
- **[VPlan] Convert redundant isSingleScalar check into assert (NFC).**
- **[clang-tidy] Rename and move 'cert-oop58-cpp' to 'bugprone-copy-constructor-mutates-argument' (#164566)**
- **[clang-tidy][NFC] Update header guards to match LLVM style (#166669)**
- **[compiler-rt][sanitizers] Mark three tests as unsupported on Android**
- **Revert "[CIR] Recognize constant aggregate initialization of auto vars (#166850)"**
- **[CI] Make premerge_advisor_explain write comments**
- **[clang][DependencyScanning] Implementation of `CompilerInstanceWithContext` to Improve By-Name Queries (#164345)**
- **[VPlan] Update more VPRecipeBuilder members to take VPInst directly (NFC)**
- **[Hexagon] Add missing build attributes (#166992)**
- **[flang][cuda][NFC] Simplify thread fence lowering (#167009)**
- **[clang-doc] remove indentation for preformatted text (#166672)**
- **[CIR] Fix failing lit test (statement-exprs.c) (#167000)**
- **[lldb][docs] Use section_iter() to iterate over sections (#167012)**
- **[OpenMP][Clang] Add parser/semantic support for dyn_groupprivate clause (#152651)**
- **Revert "[CI] Make premerge_advisor_explain write comments"**
- **[fix test] Move typedefs-with-same-name.test into ARM folder (#167005)**
- **[clang-doc] revert to a nested directory structure (#166978)**
- **[DebugInfo] Adjusting DWARF version to fix a test failure in aix-ppc64 pipeline (#167019)**
- **[llvm][RISCV] Do not assume V extension on seeing vector type. (#166994)**
- **[SystemZ] Mark RELOC_NONE as not having size for SystemZ (#167027)**
- **[lldb][ClangModulesDeclVendor] Revamp error handling when loading Clang modules (#166917)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (1/N) (#167030)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (2/N) (#167035)**
- **Reapply "[compiler-rt][HWAsan] Remove CHECK lines from test" (#167038)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (3/N) (#167040)**
- **[lldb][ClangModulesDeclVendor] Don't stop loading Clang modules if an individual import failed (#166940)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (4/N) (#167042)**
- **[CAS] Restrict the CAS size when running lit testing (#167024)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (5/N) (#167047)**
- **[CodeGen][NFC] Remove one meaningless `equal_to` specialization (#166115)**
- **[CAS] Fix msan error in ObjectStore::importObject() (#167051)**
- **[prepare-code-coverage-artifact.py] Change run line to use python3**
- **[lld][macho] Move unwind logic from equalsVariable to equalsConstant (#165325)**
- **[clang-tidy][NFC] Use llvm::DenseMap (#167050)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (6/N) (#167056)**
- **[DependencyScanning] Fix warnings**
- **[lldb][ClangModulesDeclVendor] Print Clang module loading errors to expression log instead of console (#166964)**
- **[clang-tidy][NFC] Temporary disable `llvm-header-filter` check (#167021)**
- **[lld][WebAssembly] Default to --stack-first (#166998)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (7/N) (#167058)**
- **[compiler-rt][HWAsan] Partially revert to fix test**
- **[GitHub][CI] Fix tooling container names (#167052)**
- **[NFC][CodeGen] Replace loop with "if !empty()" (#166515)**
- **[X86][clang-cl] Add CL option /vlen (#166375)**
- **[BOLT] Refactor undefined symbols handling. NFCI (#167075)**
- **Fix UB introduced by be0aa7b6c72bdb162f1f3fe251e469927118963e.**
- **[clang][bytecode] Avoid copies with elidable CXXConstructExprs (#166931)**
- **Fix issues and re-enable tests disabled in PR #165983 (from PR #165360) (#166147)**
- **AMDGPU: Minor SDWA pass cleanups (#166629)**
- **[AMDGPU][MachineVerifier] test failures in SIFoldOperands (#166600)**
- **[clang][bytecode] Fix a std::optional<bool> mishap (#167091)**
- **[CI] Drop use of install-ninja action (#167068)**
- **benchmarks: Fix sample_symbol_list.txt generation again (#167078)**
- **[BOLT] Refactor tracking internals of BinaryFunction. NFCI (#167074)**
- **[clang-tidy][NFC] Add missing "Check" suffix to filenames in clang-tidy checks (#166889)**
- **[clang-tidy] Fix `bugprone-exception-escape` not diagnosing throws in argument lists (#165955)**
- **[clang-tidy][NFC] Run clang-format-22 over clang-tidy (#167122)**
- **[clang-tidy][NFC] Remove trailing whitespaces in documentation (#167103)**
- **[Headers][X86] avx512cd - move constexpr to the end of the function attribute lists. NFC. (#166968)**
- **[libc] add various macros relate to *ADDR* (#164830)**
- **[SimplifyCFG] Simplify uncond br with icmp & select (#165580)**
- **fix: C++ empty record with align lead to va_list out of sync (#72197)**
- **[InstCombine] Fold operation into select, when one operand is zext of select's condition (#166816)**
- **Fix bazel build for #166719**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (11/N) (#167128)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (14/N) (#167131)**
- **Fix missing include from #166664**
- **[clang-doc] Remove an unused local variable (NFC) (#167104)**
- **[flang] Remove unused local variables (NFC) (#167105)**
- **[llvm] Remove unused local variables (NFC) (#167106)**
- **[mlir] Remove unused local variables (NFC) (#167107)**
- **[llvm] Proofread *.rst (#167108)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (10/N) (#167127)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (8/N) (#167123)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (12/N) (#167129)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (13/N) (#167130)**
- **[clang-tidy][NFC] Fix misc-const-correctness warnings (9/N) (#167124)**
- **[ADT] Add a missing std::move to StringSwitch::EndsWithLower (#167151)**
- **[ExecutionEngine] Use StringRef::starts_with (NFC) (#167152)**
- **[NVPTX] Remove redundant declarations (NFC) (#167153)**
- **[ObjectYAML] Remove redundant .str().c_str() (NFC) (#167154)**
- **[WebAssembly] Clean up CustomMappingTraits<BBNumberMap> (NFC) (#167155)**
- **[Driver] Remove extraneous c_str() (NFC) (#167156)**
- **[libc++] Run macOS buildbot under xcrun (#167072)**
- **[libc++abi][libunwind] Enable AT&T syntax explicitly (#166818)**
- **[llvm-rc] Don't interpret integer literals as octal numbers in rc.exe mode (#166915)**
- **Remove unused <vector> inclusion (#166997)**
- **[TableGen] Use "using" instead of "typedef" (NFC) (#167168)**
- **[llvm] Allow Rust personality name to contain arbitrary prefix (#166095)**
- **Add missing #include (fix for #166997)**
- **[LV] Add register pressure test for #164124.**
- **[llvm] Use llvm::find_if and llvm::is_contained (NFC) (#167166)**
- **[llvm] Use static_assert on getEmptyKey and getTombstoneKey (NFC) (#167167)**
- **[BOLT] Use DenseMap::contains (NFC) (#167169)**
- **[BinaryFromat] Simplify subscript operators (NFC) (#167170)**
- **[MLIR] Fix FileCheck annotations and add 32-bit coverage (#167149)**
- **[libc++] Treat P0513R0 as a defect report against C++11 (#166690)**
- **[mlir] Remove duplicate includes (NFC) (#167187)**
- **[ObjectYAML] Remove extraneous .c_str() (NFC) (#167189)**
- **[gn] port ebb61a5bea (llvm-cas)**
- **[libc++][numeric] Marked saturation artithmetic functions as `[[nodiscard]]` (#166898)**
- **Reapply "[compiler-rt] Default to Lit's Internal Shell"**
- **[Offload] Remove unused KernelArgsTy instantiation (#167197)**
- **[Vectorize] Remove a redundant declaration (NFC) (#167188)**
- **[LifetimeSafety] Use StringMap::contains (NFC) (#167186)**
- **[llvm] Remove unused local variables (NFC) (#167185)**
- **Revert "Reapply "[compiler-rt] Default to Lit's Internal Shell""**
- **[PostRASink] Add target hook shouldPostRASink (#167182)**
- **[tools][llc] Add `--save-stats` option (#163967)**
- **[tools][llc] Fix save-stats.ll require aarch64 target (#167218)**
- **[SelectionDAG] Fix assertion failure on inline asm register type mismatch (#166615)**
- **[gn] port c940bfd7e621 (BPF SDNodeInfo)**
- **[NFCI][lldb][test][Recognizer] Fix mismatched C/C++ frontend subtitutions (#167220)**
- **[gn build] Port 00eacc29f009**
- **[gn build] Port 8d950d27d686**
- **Remove unused <array> and <list> inclusion (#167116)**
- **Remove unused <set> and <map> inclusion (#167175)**
- **[clang-tidy] Fix `readability-container-data-pointer` check (#165636)**
- **[SPIRV] Add support for `bfloat16` atomics via the `SPV_INTEL_16bit_atomics` extension (#166257)**
- **[mlir] Use llvm::transform (NFC) (#167205)**
- **[Target] Fix misleading indentation (NFC) (#167206)**
- **[clang] Remove redundant typename (NFC) (#167207)**
- **[mlir] Consolidate two implementations of meet (NFC) (#167208)**
- **[AArch64][GlobalISel] Correct instructions for 64bit fneg constant vectors. (#166537)**
- **[SandboxIR] Remove tight-coupling with LLVM's SwitchInst::CaseHandle (#167093)**
- **[lldb] Disable TestLocationsAfterRebuild for remote targets (#167239)**
- **Fix typo in LangRef.md regarding regions (#167242)**
- **[BOLT] Support restartable sequences in tcmalloc (#167195)**
- **[gn build] Port f2c50f930540**
- **[InstCombine] Don't sink if it would require dropping deref assumptions. (#166945)**
- **[VPlan] Use VPInstructionWithType for casts in VPlan0. (NFC)**
- **[clang-format] Fix a crash in AlignArrayOfStructures (#167099)**
- **[LoongArch][NFC] Pre-commit tests for memcmp expansion (#166718)**
- **[mlir][tosa] Fix crash in `tosa.concat` verifier (#165966)**
- **[Github] Update PR labeller to v6.0.1 (#167246)**
- **[Github] Make Windows container use zstd (#167022)**
- **Revert "[Github] Update PR labeller to v6.0.1 (#167246)"**
- **[LoongArch] Initial implementation for `enableMemCmpExpansion` hook (#166526)**
- **[llvm][RISCV] Support Zvfbfa codegen for fneg, fabs and copysign (#166944)**
- **[Flang] Add parser support for prefetch directive (#139702)**
- **[Hexagon] Implement isMaskAndCmp0FoldingBeneficial (#166891)**
- **[clang-tidy][NFC] Enable `performance-unnecessary-value-param` in the codebase (#163686)**
- **[MLIR][BufferResultsToOutParamsPass] Add Option to Modify Public Function's Signature (#167248)**
- **[libc] fchown initial impl**
>From 9f7ee389c14eb49dd04f98646a3e04f30982d3f2 Mon Sep 17 00:00:00 2001
From: Anshul Nigham <nigham at google.com>
Date: Mon, 10 Nov 2025 07:48:04 +0000
Subject: [PATCH] [libc] fchown initial impl
---
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/include/unistd.yaml | 8 ++++
libc/src/unistd/CMakeLists.txt | 7 ++++
libc/src/unistd/fchown.h | 23 +++++++++++
libc/src/unistd/linux/CMakeLists.txt | 14 +++++++
libc/src/unistd/linux/fchown.cpp | 29 ++++++++++++++
libc/test/src/unistd/CMakeLists.txt | 20 ++++++++++
libc/test/src/unistd/fchown_test.cpp | 51 ++++++++++++++++++++++++
8 files changed, 153 insertions(+)
create mode 100644 libc/src/unistd/fchown.h
create mode 100644 libc/src/unistd/linux/fchown.cpp
create mode 100644 libc/test/src/unistd/fchown_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a44e2041e57f2..f4012514fe20e 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -334,6 +334,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.execve
libc.src.unistd.faccessat
libc.src.unistd.fchdir
+ libc.src.unistd.fchown
libc.src.unistd.fpathconf
libc.src.unistd.fsync
libc.src.unistd.ftruncate
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 0e5b22e627b67..3f5e957768533 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -120,6 +120,14 @@ functions:
return_type: int
arguments:
- type: int
+ - name: fchown
+ standards:
+ - POSIX
+ return_type: int
+ arguments:
+ - type: int
+ - type: uid_t
+ - type: gid_t
- name: fork
standards:
- POSIX
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 337480cbbf928..b7444a4722b0d 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -76,6 +76,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.fchdir
)
+add_entrypoint_object(
+ fchown
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.fchown
+)
+
add_entrypoint_object(
fork
ALIAS
diff --git a/libc/src/unistd/fchown.h b/libc/src/unistd/fchown.h
new file mode 100644
index 0000000000000..23c6295da1a43
--- /dev/null
+++ b/libc/src/unistd/fchown.h
@@ -0,0 +1,23 @@
+//===-- Implementation header for fchown ------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_FCHOWN_H_
+#define LLVM_LIBC_SRC_UNISTD_FCHOWN_H_
+
+#include "hdr/types/gid_t.h"
+#include "hdr/types/uid_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int fchown(int fildes, uid_t owner, gid_t group);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+
+#endif // LLVM_LIBC_SRC_UNISTD_FCHOWN_H_
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index c2dacc6456e27..c45b6ef1c5d80 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -120,6 +120,20 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ fchown
+ SRCS
+ fchown.cpp
+ HDRS
+ ../fchown.h
+ DEPENDS
+ libc.hdr.types.uid_t
+ libc.hdr.types.gid_t
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
fork
SRCS
diff --git a/libc/src/unistd/linux/fchown.cpp b/libc/src/unistd/linux/fchown.cpp
new file mode 100644
index 0000000000000..63cad81ea585b
--- /dev/null
+++ b/libc/src/unistd/linux/fchown.cpp
@@ -0,0 +1,29 @@
+//===-- Linux implementation of fchown ------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/fchown.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.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, fchown, (int fildes, uid_t owner, gid_t group)) {
+ int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fchown, fildes, owner, group);
+ if (ret < 0) {
+ libc_errno = -ret;
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 07070535459ec..3012ea9a466f4 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -146,6 +146,26 @@ add_libc_unittest(
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_unittest(
+ fchown_test
+ SUITE
+ libc_unistd_unittests
+ SRCS
+ fchown_test.cpp
+ DEPENDS
+ libc.hdr.fcntl_macros
+ libc.include.unistd
+ libc.src.errno.errno
+ libc.src.unistd.fchown
+ libc.src.unistd.close
+ libc.src.unistd.unlink
+ libc.src.fcntl.open
+ libc.src.unistd.getuid
+ libc.src.unistd.getgid
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
add_libc_unittest(
ftruncate_test
SUITE
diff --git a/libc/test/src/unistd/fchown_test.cpp b/libc/test/src/unistd/fchown_test.cpp
new file mode 100644
index 0000000000000..b44b728d3889e
--- /dev/null
+++ b/libc/test/src/unistd/fchown_test.cpp
@@ -0,0 +1,51 @@
+//===-- Unittests for fchown ----------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/fcntl/open.h"
+#include "src/unistd/fchown.h"
+#include "src/unistd/close.h"
+#include "src/unistd/getgid.h"
+#include "src/unistd/getuid.h"
+#include "src/unistd/unlink.h"
+
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "hdr/fcntl_macros.h"
+#include <sys/stat.h>
+
+using LlvmLibcFchownTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcFchownTest, FchownSuccess) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+ uid_t my_uid = LIBC_NAMESPACE::getuid();
+ gid_t my_gid = LIBC_NAMESPACE::getgid();
+ constexpr const char *FILENAME = "fchown.test";
+ auto TEST_FILE = libc_make_test_file_path(FILENAME);
+
+ // Create a test file.
+ int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(write_fd, 0);
+
+ // Change the ownership of the file.
+ ASSERT_THAT(LIBC_NAMESPACE::fchown(write_fd, my_uid, my_gid), Succeeds(0));
+
+ // Close the file descriptor.
+ ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
+
+ // Clean up the test file.
+ ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
+}
+
+TEST_F(LlvmLibcFchownTest, ChownInvalidFileDescriptor) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+ ASSERT_THAT(LIBC_NAMESPACE::chown(-1, 1000, 1000),
+ Fails(ENOENT));
+}
More information about the libc-commits
mailing list