Skip to content

Commit 7aeef13

Browse files
committed
Remove the use of system headers, address comments
1 parent fff05af commit 7aeef13

File tree

14 files changed

+199
-16
lines changed

14 files changed

+199
-16
lines changed

libc/hdr/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ add_proxy_header_library(
3232
libc.include.math
3333
)
3434

35+
add_proxy_header_library(
36+
fcntl_macros
37+
HDRS
38+
fcntl_macros.h
39+
FULL_BUILD_DEPENDS
40+
libc.include.llvm-libc-macros.fcntl_macros
41+
libc.include.fcntl
42+
)
43+
3544
add_proxy_header_library(
3645
fenv_macros
3746
HDRS

libc/hdr/fcntl_macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from fcntl/fcntl.h ---------------------------===//
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_FCNTL_MACROS_H
10+
#define LLVM_LIBC_HDR_FCNTL_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-macros/fcntl-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include <fcntl.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_FCNTL_MACROS_H

libc/hdr/types/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ add_proxy_header_library(
1414
libc.include.llvm-libc-types.struct_epoll_event
1515
)
1616

17+
add_proxy_header_library(
18+
struct_flock
19+
HDRS
20+
struct_flock.h
21+
FULL_BUILD_DEPENDS
22+
libc.include.llvm-libc-types.struct_flock
23+
)
24+
25+
add_proxy_header_library(
26+
struct_flock64
27+
HDRS
28+
struct_flock64.h
29+
FULL_BUILD_DEPENDS
30+
libc.include.llvm-libc-types.struct_flock64
31+
)
32+
1733
add_proxy_header_library(
1834
struct_timespec
1935
HDRS

libc/hdr/types/struct_flock.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Proxy for struct flock -------------------------------------------===//
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+
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK_H
9+
#define LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/struct_flock.h"
14+
15+
#else
16+
17+
#include <fcntl.h>
18+
19+
#endif // LIBC_FULL_BUILD
20+
21+
#endif // LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK_H

libc/hdr/types/struct_flock64.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Proxy for struct flock64 -----------------------------------------===//
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+
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK64_H
9+
#define LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK64_H
10+
11+
#ifdef LIBC_FULL_BUILD
12+
13+
#include "include/llvm-libc-types/struct_flock64.h"
14+
15+
#else
16+
17+
#include <fcntl.h>
18+
19+
#endif // LIBC_FULL_BUILD
20+
21+
#endif // LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK64_H

libc/include/llvm-libc-macros/linux/fcntl-macros.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,36 @@
6767
#define F_SETFD 2
6868
#define F_GETFL 3
6969
#define F_SETFL 4
70+
#define F_GETLK 5
71+
#define F_SETLK 6
72+
#define F_SETLKW 7
73+
#define F_SETOWN 8
74+
#define F_GETOWN 9
75+
#define F_SETSIG 10
76+
#define F_GETSIG 11
77+
#define F_GETLK64 12
78+
#define F_SETLK64 13
79+
#define F_SETLKW64 14
80+
#define F_SETOWN_EX 15
81+
#define F_GETOWN_EX 16
82+
83+
// Open File Description Locks.
84+
#define F_OFD_GETLK 36
85+
#define F_OFD_SETLK 37
86+
#define F_OFD_SETLKW 38
87+
88+
// Close on succesful
89+
#define F_CLOEXEC 1
90+
91+
#define F_RDLCK 0
92+
#define F_WRLOCK 1
93+
#define F_UNLCK 2
94+
95+
// For Large File Support
96+
#if defined(_LARGEFILE64_SOURCE)
97+
#define F_GETLK F_GETLK64
98+
#define F_SETLK F_SETLK64
99+
#define F_SETLKW F_SETLKW64
100+
#endif
70101

71102
#endif // LLVM_LIBC_MACROS_LINUX_FCNTL_MACROS_H

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ add_header(rlim_t HDR rlim_t.h)
6060
add_header(time_t HDR time_t.h)
6161
add_header(stack_t HDR stack_t.h)
6262
add_header(suseconds_t HDR suseconds_t.h)
63+
add_header(struct_flock HDR struct_flock.h DEPENDS .off_t .pid_t)
64+
add_header(struct_flock64 HDR struct_flock64.h DEPENDS .off64_t .pid_t)
6365
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
6466
add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
6567
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Definition of type struct flock64 ---------------------------------===//
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_TYPES_STRUCT_FLOCK_H
10+
#define LLVM_LIBC_TYPES_STRUCT_FLOCK_H
11+
12+
#include "llvm-libc-types/off_t.h"
13+
#include "llvm-libc-types/pid_t.h"
14+
15+
struct flock64 {
16+
int16_t l_type;
17+
int16_t l_whence;
18+
off_t l_start;
19+
off_t l_len;
20+
pid_t l_pid;
21+
};
22+
23+
#endif // LLVM_LIBC_TYPES_STRUCT_FLOCK_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Definition of type struct flock64 ---------------------------------===//
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_TYPES_STRUCT_FLOCK64_H
10+
#define LLVM_LIBC_TYPES_STRUCT_FLOCK64_H
11+
12+
#include "llvm-libc-types/off64_t.h"
13+
#include "llvm-libc-types/pid_t.h"
14+
15+
struct flock64 {
16+
int16_t l_type;
17+
int16_t l_whence;
18+
off64_t l_start;
19+
off64_t l_len;
20+
pid_t l_pid;
21+
};
22+
23+
#endif // LLVM_LIBC_TYPES_STRUCT_FLOCK64_H

libc/spec/posix.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ def POSIX : StandardSpec<"POSIX"> {
230230
RetValSpec<IntType>,
231231
[ArgSpec<ConstCharPtr>, ArgSpec<ModeTType>]
232232
>,
233+
FunctionSpec<
234+
"fcntl",
235+
RetValSpec<IntType>,
236+
[ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<VarArgType>]
237+
>,
233238
FunctionSpec<
234239
"open",
235240
RetValSpec<IntType>,

libc/src/fcntl/fcntl.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#ifndef LLVM_LIBC_SRC_FCNTL_FCNTL_H
1010
#define LLVM_LIBC_SRC_FCNTL_FCNTL_H
1111

12-
#include <fcntl.h>
13-
1412
namespace LIBC_NAMESPACE {
1513

1614
int fcntl(int fd, int cmd, ...);

libc/src/fcntl/linux/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ add_entrypoint_object(
1818
../fcntl.h
1919
DEPENDS
2020
libc.include.fcntl
21+
libc.include.llvm-libc-types.struct_flock
22+
libc.include.llvm-libc-types.struct_flock64
23+
libc.hdr.fcntl_macros
2124
libc.src.__support.OSUtil.osutil
2225
libc.src.errno.errno
2326
)

libc/src/fcntl/linux/fcntl.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,29 @@
88

99
#include "src/fcntl/fcntl.h"
1010

11+
#include "hdr/fcntl_macros.h"
12+
#include "hdr/types/struct_flock.h"
13+
#include "hdr/types/struct_flock64.h"
1114
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1215
#include "src/__support/common.h"
1316
#include "src/errno/libc_errno.h"
1417

1518
#include <stdarg.h>
1619
#include <sys/syscall.h> // For syscall numbers.
1720

18-
namespace {
21+
// To avoid conflict with system headers when building
22+
// in overlay mode.
23+
namespace helper {
24+
enum pid_type {
25+
F_OWNER_TID = 0,
26+
F_OWNER_PID,
27+
F_OWNER_PGRP,
28+
};
1929
struct fowner_ex {
20-
int type;
30+
enum pid_type type;
2131
pid_t pid;
2232
};
23-
} // namespace
33+
} // namespace helper
2434

2535
// The OFD file locks require special handling for LARGEFILES
2636
namespace LIBC_NAMESPACE {
@@ -35,7 +45,7 @@ LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
3545
case F_SETLKW:
3646
return syscall_impl<int>(SYS_fcntl, fd, cmd, arg);
3747
case F_OFD_SETLKW: {
38-
struct flock *flk = (struct flock *)arg;
48+
struct flock *flk = reinterpret_cast<struct flock *>(arg);
3949
// convert the struct to a flock64
4050
struct flock64 flk64;
4151
flk64.l_type = flk->l_type;
@@ -48,7 +58,7 @@ LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
4858
}
4959
case F_OFD_GETLK:
5060
case F_OFD_SETLK: {
51-
struct flock *flk = (struct flock *)arg;
61+
struct flock *flk = reinterpret_cast<struct flock *>(arg);
5262
// convert the struct to a flock64
5363
struct flock64 flk64;
5464
flk64.l_type = flk->l_type;
@@ -63,8 +73,8 @@ LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
6373
return -1;
6474
// Check for overflow, i.e. the offsets are not the same when cast
6575
// to off_t from off64_t.
66-
if ((off_t)flk64.l_len != flk64.l_len ||
67-
(off_t)flk64.l_start != flk64.l_start) {
76+
if (static_cast<off_t>(flk64.l_len) != flk64.l_len ||
77+
static_cast<off_t>(flk64.l_start) != flk64.l_start) {
6878
libc_errno = EOVERFLOW;
6979
return -1;
7080
}
@@ -79,17 +89,18 @@ LLVM_LIBC_FUNCTION(int, fcntl, (int fd, int cmd, ...)) {
7989
// The general case
8090
default: {
8191
if (cmd == F_GETOWN) {
82-
struct fowner_ex fex;
92+
struct helper::fowner_ex fex;
8393
int retVal = syscall_impl<int>(SYS_fcntl, fd, F_GETOWN_EX, &fex);
8494
if (retVal == -EINVAL)
85-
return syscall_impl<int>(SYS_fcntl, fd, cmd, (void *)arg);
86-
if ((uint64_t)retVal <= -4096UL)
87-
return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;
95+
return syscall_impl<int>(SYS_fcntl, fd, cmd,
96+
reinterpret_cast<void *>(arg));
97+
if (static_cast<uint64_t>(retVal) <= -4096UL)
98+
return fex.type == helper::F_OWNER_PGRP ? -fex.pid : fex.pid;
8899

89100
libc_errno = -retVal;
90101
return -1;
91102
}
92-
return syscall_impl<int>(SYS_fcntl, fd, cmd, (void *)arg);
103+
return syscall_impl<int>(SYS_fcntl, fd, cmd, reinterpret_cast<void *>(arg));
93104
}
94105
}
95106
}

libc/test/src/fcntl/fcntl_test.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#include "test/UnitTest/ErrnoSetterMatcher.h"
1414
#include "test/UnitTest/Test.h"
1515

16-
#include <sys/stat.h>
17-
1816
TEST(LlvmLibcFcntlTest, FcntlDupfd) {
1917
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
2018
constexpr const char *TEST_FILE = "testdata/fcntl.test";

0 commit comments

Comments
 (0)