Skip to content

Commit aca5117

Browse files
authored
[libc] Implement fcntl() function (#89507)
Fixes #84968. Implements the `fcntl()` function defined in the `fcntl.h` header.
1 parent d1b3648 commit aca5117

File tree

22 files changed

+545
-0
lines changed

22 files changed

+545
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(TARGET_LIBC_ENTRYPOINTS
2222

2323
# fcntl.h entrypoints
2424
libc.src.fcntl.creat
25+
libc.src.fcntl.fcntl
2526
libc.src.fcntl.open
2627
libc.src.fcntl.openat
2728

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(TARGET_LIBC_ENTRYPOINTS
2222

2323
# fcntl.h entrypoints
2424
libc.src.fcntl.creat
25+
libc.src.fcntl.fcntl
2526
libc.src.fcntl.open
2627
libc.src.fcntl.openat
2728

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(TARGET_LIBC_ENTRYPOINTS
2222

2323
# fcntl.h entrypoints
2424
libc.src.fcntl.creat
25+
libc.src.fcntl.fcntl
2526
libc.src.fcntl.open
2627
libc.src.fcntl.openat
2728

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ 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+
33+
add_proxy_header_library(
34+
struct_f_owner_ex
35+
HDRS
36+
struct_f_owner_ex.h
37+
FULL_BUILD_DEPENDS
38+
libc.include.llvm-libc-types.struct_f_owner_ex
39+
)
40+
1741
add_proxy_header_library(
1842
struct_timespec
1943
HDRS

libc/hdr/types/struct_f_owner_ex.h

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

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/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ add_gen_header(
4343
DEPENDS
4444
.llvm-libc-macros.fcntl_macros
4545
.llvm-libc-types.mode_t
46+
.llvm-libc-types.struct_flock
47+
.llvm-libc-types.struct_flock64
48+
.llvm-libc-types.off64_t
49+
.llvm-libc-types.pid_t
4650
.llvm-libc-types.off_t
4751
.llvm_libc_common_h
4852
)

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_WRLCK 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ 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)
65+
add_header(struct_f_owner_ex HDR struct_f_owner_ex.h DEPENDS .pid_t)
6366
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
6467
add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
6568
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Definition of type struct f_owner_ex ------------------------------===//
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_F_OWNER_EX_H
10+
#define LLVM_LIBC_TYPES_STRUCT_F_OWNER_EX_H
11+
12+
#include "llvm-libc-types/pid_t.h"
13+
14+
enum pid_type {
15+
F_OWNER_TID = 0,
16+
F_OWNER_PID,
17+
F_OWNER_PGRP,
18+
};
19+
20+
struct f_owner_ex {
21+
enum pid_type type;
22+
pid_t pid;
23+
};
24+
25+
#endif // LLVM_LIBC_TYPES_STRUCT_F_OWNER_EX_H
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#include <stdint.h>
16+
17+
struct flock {
18+
int16_t l_type;
19+
int16_t l_whence;
20+
off_t l_start;
21+
off_t l_len;
22+
pid_t l_pid;
23+
};
24+
25+
#endif // LLVM_LIBC_TYPES_STRUCT_FLOCK_H
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#include <stdint.h>
16+
17+
struct flock64 {
18+
int16_t l_type;
19+
int16_t l_whence;
20+
off64_t l_start;
21+
off64_t l_len;
22+
pid_t l_pid;
23+
};
24+
25+
#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/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ add_entrypoint_object(
99
.${LIBC_TARGET_OS}.creat
1010
)
1111

12+
add_entrypoint_object(
13+
fcntl
14+
ALIAS
15+
DEPENDS
16+
.${LIBC_TARGET_OS}.fcntl
17+
)
18+
1219
add_entrypoint_object(
1320
open
1421
ALIAS

libc/src/fcntl/fcntl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header of fcntl --------------------------*- 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_FCNTL_FCNTL_H
10+
#define LLVM_LIBC_SRC_FCNTL_FCNTL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
int fcntl(int fd, int cmd, ...);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_FCNTL_FCNTL_H

libc/src/fcntl/linux/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ add_entrypoint_object(
1010
libc.src.errno.errno
1111
)
1212

13+
add_entrypoint_object(
14+
fcntl
15+
SRCS
16+
fcntl.cpp
17+
HDRS
18+
../fcntl.h
19+
DEPENDS
20+
libc.include.fcntl
21+
libc.hdr.types.struct_flock
22+
libc.hdr.types.struct_flock64
23+
libc.hdr.types.struct_f_owner_ex
24+
libc.hdr.fcntl_macros
25+
libc.src.__support.OSUtil.osutil
26+
libc.src.errno.errno
27+
)
28+
1329
add_entrypoint_object(
1430
open
1531
SRCS

0 commit comments

Comments
 (0)