Skip to content

Commit 0fd0298

Browse files
shubhe25pshubh@DOE
andauthored
[libc] Add chown and getgid implementations (#166434)
Implements chown and getgid per the POSIX specification and adds corresponding unit tests. getgid is added as it is required by the chown unit tests. This PR will address #165785 Co-authored-by: shubh@DOE <[email protected]>
1 parent 2b4ac66 commit 0fd0298

File tree

13 files changed

+281
-0
lines changed

13 files changed

+281
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ set(TARGET_LIBC_ENTRYPOINTS
326326
# unistd.h entrypoints
327327
libc.src.unistd.access
328328
libc.src.unistd.chdir
329+
libc.src.unistd.chown
329330
libc.src.unistd.close
330331
libc.src.unistd.dup
331332
libc.src.unistd.dup2
@@ -344,6 +345,7 @@ set(TARGET_LIBC_ENTRYPOINTS
344345
libc.src.unistd.getppid
345346
libc.src.unistd.getsid
346347
libc.src.unistd.gettid
348+
libc.src.unistd.getgid
347349
libc.src.unistd.getuid
348350
libc.src.unistd.isatty
349351
libc.src.unistd.link

libc/hdr/types/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,11 @@ add_proxy_header_library(
479479
libc.include.llvm-libc-types.struct_rlimit
480480
libc.include.sys_resource
481481
)
482+
483+
add_proxy_header_library(
484+
gid_t
485+
HDRS
486+
gid_t.h
487+
FULL_BUILD_DEPENDS
488+
libc.include.llvm-libc-types.gid_t
489+
)

libc/hdr/types/gid_t.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Proxy for gid_t ---------------------------------------------------===//
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_HDR_TYPES_GID_T_H
10+
#define LLVM_LIBC_HDR_TYPES_GID_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/gid_t.h"
15+
16+
#else // Overlay mode
17+
18+
#include <sys/types.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_GID_T_H

libc/include/unistd.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ header_template: unistd.h.def
33
macros: []
44
types:
55
- type_name: uid_t
6+
- type_name: gid_t
67
- type_name: ssize_t
78
- type_name: size_t
89
- type_name: pid_t
@@ -54,6 +55,14 @@ functions:
5455
return_type: int
5556
arguments:
5657
- type: const char *
58+
- name: chown
59+
standards:
60+
- POSIX
61+
return_type: int
62+
arguments:
63+
- type: const char *
64+
- type: uid_t
65+
- type: gid_t
5766
- name: close
5867
standards:
5968
- POSIX
@@ -195,6 +204,12 @@ functions:
195204
return_type: uid_t
196205
arguments:
197206
- type: void
207+
- name: getgid
208+
standards:
209+
- POSIX
210+
return_type: gid_t
211+
arguments:
212+
- type: void
198213
- name: isatty
199214
standards:
200215
- POSIX

libc/src/unistd/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ add_entrypoint_object(
2727
.${LIBC_TARGET_OS}.chdir
2828
)
2929

30+
add_entrypoint_object(
31+
chown
32+
ALIAS
33+
DEPENDS
34+
.${LIBC_TARGET_OS}.chown
35+
)
36+
3037
add_entrypoint_object(
3138
close
3239
ALIAS
@@ -160,6 +167,13 @@ add_entrypoint_object(
160167
.${LIBC_TARGET_OS}.getuid
161168
)
162169

170+
add_entrypoint_object(
171+
getgid
172+
ALIAS
173+
DEPENDS
174+
.${LIBC_TARGET_OS}.getgid
175+
)
176+
163177
add_entrypoint_object(
164178
isatty
165179
ALIAS

libc/src/unistd/chown.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for chown -------------------------*- 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_CHOWN_H
10+
#define LLVM_LIBC_SRC_UNISTD_CHOWN_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 chown(const char *path, uid_t owner, gid_t group);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_UNISTD_CHOWN_H

libc/src/unistd/getgid.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for getgid ------------------------*- 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_GETGID_H
10+
#define LLVM_LIBC_SRC_UNISTD_GETGID_H
11+
12+
#include "hdr/types/gid_t.h"
13+
#include "hdr/unistd_macros.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
gid_t getgid();
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_UNISTD_GETGID_H

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ add_entrypoint_object(
2525
libc.src.errno.errno
2626
)
2727

28+
add_entrypoint_object(
29+
chown
30+
SRCS
31+
chown.cpp
32+
HDRS
33+
../chown.h
34+
DEPENDS
35+
libc.hdr.types.uid_t
36+
libc.hdr.types.gid_t
37+
libc.include.sys_syscall
38+
libc.src.__support.OSUtil.osutil
39+
libc.src.errno.errno
40+
)
41+
2842
add_entrypoint_object(
2943
close
3044
SRCS
@@ -276,6 +290,20 @@ add_entrypoint_object(
276290
libc.src.errno.errno
277291
)
278292

293+
add_entrypoint_object(
294+
getgid
295+
SRCS
296+
getgid.cpp
297+
HDRS
298+
../getgid.h
299+
DEPENDS
300+
libc.hdr.types.gid_t
301+
libc.hdr.fcntl_macros
302+
libc.include.unistd
303+
libc.include.sys_syscall
304+
libc.src.__support.OSUtil.osutil
305+
)
306+
279307
add_entrypoint_object(
280308
getuid
281309
SRCS

libc/src/unistd/linux/chown.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Linux implementation of chown -------------------------------------===//
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/chown.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
#include "src/__support/common.h"
13+
14+
#include "src/__support/libc_errno.h"
15+
#include "src/__support/macros/config.h"
16+
#include <sys/syscall.h> // For syscall numbers.
17+
18+
namespace LIBC_NAMESPACE_DECL {
19+
20+
LLVM_LIBC_FUNCTION(int, chown, (const char *path, uid_t owner, gid_t group)) {
21+
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_chown, path, owner, group);
22+
if (ret < 0) {
23+
libc_errno = -ret;
24+
return -1;
25+
}
26+
return 0;
27+
}
28+
29+
} // namespace LIBC_NAMESPACE_DECL

libc/src/unistd/linux/getgid.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Linux implementation of getgid ------------------------------------===//
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/getgid.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
15+
#include <sys/syscall.h> // For syscall numbers.
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
LLVM_LIBC_FUNCTION(gid_t, getgid, ()) {
20+
return LIBC_NAMESPACE::syscall_impl<gid_t>(SYS_getgid);
21+
}
22+
23+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)