Skip to content

Commit 497dc10

Browse files
authored
[libc] Implement fchown (#167286)
Implements fchown fixes: #166856
1 parent a4a6b5f commit 497dc10

File tree

9 files changed

+154
-0
lines changed

9 files changed

+154
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ set(TARGET_LIBC_ENTRYPOINTS
327327
libc.src.unistd.execve
328328
libc.src.unistd.faccessat
329329
libc.src.unistd.fchdir
330+
libc.src.unistd.fchown
330331
libc.src.unistd.fpathconf
331332
libc.src.unistd.fsync
332333
libc.src.unistd.ftruncate

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ set(TARGET_LIBC_ENTRYPOINTS
334334
libc.src.unistd.execve
335335
libc.src.unistd.faccessat
336336
libc.src.unistd.fchdir
337+
libc.src.unistd.fchown
337338
libc.src.unistd.fpathconf
338339
libc.src.unistd.fsync
339340
libc.src.unistd.ftruncate

libc/include/unistd.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ functions:
120120
return_type: int
121121
arguments:
122122
- type: int
123+
- name: fchown
124+
standards:
125+
- POSIX
126+
return_type: int
127+
arguments:
128+
- type: int
129+
- type: uid_t
130+
- type: gid_t
123131
- name: fork
124132
standards:
125133
- POSIX

libc/src/unistd/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ add_entrypoint_object(
7676
.${LIBC_TARGET_OS}.fchdir
7777
)
7878

79+
add_entrypoint_object(
80+
fchown
81+
ALIAS
82+
DEPENDS
83+
.${LIBC_TARGET_OS}.fchown
84+
)
85+
7986
add_entrypoint_object(
8087
fork
8188
ALIAS

libc/src/unistd/fchown.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for fchown ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_UNISTD_FCHOWN_H
10+
#define LLVM_LIBC_SRC_UNISTD_FCHOWN_H
11+
12+
#include "hdr/types/gid_t.h"
13+
#include "hdr/types/uid_t.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
int fchown(int fildes, uid_t owner, gid_t group);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_UNISTD_FCHOWN_H

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ add_entrypoint_object(
120120
libc.src.errno.errno
121121
)
122122

123+
add_entrypoint_object(
124+
fchown
125+
SRCS
126+
fchown.cpp
127+
HDRS
128+
../fchown.h
129+
DEPENDS
130+
libc.hdr.types.uid_t
131+
libc.hdr.types.gid_t
132+
libc.include.sys_syscall
133+
libc.src.__support.OSUtil.osutil
134+
libc.src.errno.errno
135+
)
136+
123137
add_entrypoint_object(
124138
fork
125139
SRCS

libc/src/unistd/linux/fchown.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- Linux implementation of fchown ------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/fchown.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
#include "src/__support/common.h"
13+
14+
#include "hdr/types/gid_t.h"
15+
#include "hdr/types/uid_t.h"
16+
#include "src/__support/libc_errno.h"
17+
#include "src/__support/macros/config.h"
18+
#include <sys/syscall.h> // For syscall numbers.
19+
20+
namespace LIBC_NAMESPACE_DECL {
21+
22+
LLVM_LIBC_FUNCTION(int, fchown, (int fildes, uid_t owner, gid_t group)) {
23+
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fchown, fildes, owner, group);
24+
if (ret < 0) {
25+
libc_errno = -ret;
26+
return -1;
27+
}
28+
return 0;
29+
}
30+
31+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/unistd/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ add_libc_unittest(
146146
libc.test.UnitTest.ErrnoSetterMatcher
147147
)
148148

149+
add_libc_unittest(
150+
fchown_test
151+
SUITE
152+
libc_unistd_unittests
153+
SRCS
154+
fchown_test.cpp
155+
DEPENDS
156+
libc.hdr.fcntl_macros
157+
libc.include.unistd
158+
libc.src.errno.errno
159+
libc.src.unistd.fchown
160+
libc.src.unistd.close
161+
libc.src.unistd.unlink
162+
libc.src.fcntl.open
163+
libc.src.unistd.getuid
164+
libc.src.unistd.getgid
165+
libc.test.UnitTest.ErrnoCheckingTest
166+
libc.test.UnitTest.ErrnoSetterMatcher
167+
)
168+
149169
add_libc_unittest(
150170
ftruncate_test
151171
SUITE
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- Unittests for fchown ----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/fcntl/open.h"
10+
#include "src/unistd/close.h"
11+
#include "src/unistd/fchown.h"
12+
#include "src/unistd/getgid.h"
13+
#include "src/unistd/getuid.h"
14+
#include "src/unistd/unlink.h"
15+
16+
#include "test/UnitTest/ErrnoCheckingTest.h"
17+
#include "test/UnitTest/ErrnoSetterMatcher.h"
18+
#include "test/UnitTest/Test.h"
19+
20+
#include "hdr/fcntl_macros.h"
21+
#include <sys/stat.h>
22+
23+
using LlvmLibcFchownTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
24+
25+
TEST_F(LlvmLibcFchownTest, FchownSuccess) {
26+
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
27+
uid_t my_uid = LIBC_NAMESPACE::getuid();
28+
gid_t my_gid = LIBC_NAMESPACE::getgid();
29+
constexpr const char *FILENAME = "fchown.test";
30+
auto TEST_FILE = libc_make_test_file_path(FILENAME);
31+
32+
// Create a test file.
33+
int write_fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
34+
ASSERT_ERRNO_SUCCESS();
35+
ASSERT_GT(write_fd, 0);
36+
37+
// Change the ownership of the file.
38+
ASSERT_THAT(LIBC_NAMESPACE::fchown(write_fd, my_uid, my_gid), Succeeds(0));
39+
40+
// Close the file descriptor.
41+
ASSERT_THAT(LIBC_NAMESPACE::close(write_fd), Succeeds(0));
42+
43+
// Clean up the test file.
44+
ASSERT_THAT(LIBC_NAMESPACE::unlink(TEST_FILE), Succeeds(0));
45+
}
46+
47+
TEST_F(LlvmLibcFchownTest, FchownInvalidFileDescriptor) {
48+
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
49+
ASSERT_THAT(LIBC_NAMESPACE::fchown(-1, 1000, 1000), Fails(EBADF));
50+
}

0 commit comments

Comments
 (0)