Skip to content

Commit 9f3854a

Browse files
[reland][libc] add epoll_wait functions (#79635)
The epoll_wait functions are syscall wrappers that were requested by upstream users. This patch adds them, as well as their header and types. The tests are currently incomplete since they require epoll_create to properly test epoll_wait. That will be added in a followup patch since this one is already very large.
1 parent 69cb99f commit 9f3854a

34 files changed

+631
-5
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ set(TARGET_LIBC_ENTRYPOINTS
179179
# sys/auxv.h entrypoints
180180
libc.src.sys.auxv.getauxval
181181

182+
# sys/epoll.h entrypoints
183+
# Disabled due to epoll_wait syscalls not being available on this platform.
184+
# libc.src.sys.epoll.epoll_wait
185+
# libc.src.sys.epoll.epoll_pwait
186+
# libc.src.sys.epoll.epoll_pwait2
187+
182188
# termios.h entrypoints
183189
libc.src.termios.cfgetispeed
184190
libc.src.termios.cfgetospeed

libc/config/linux/aarch64/headers.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ set(TARGET_PUBLIC_HEADERS
2525
libc.include.unistd
2626

2727
libc.include.sys_ioctl
28+
# Disabled due to epoll_wait syscalls not being available on this platform.
29+
# libc.include.sys_epoll
2830
)

libc/config/linux/api.td

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
include "config/public_api.td"
22

3-
include "spec/bsd_ext.td"
4-
include "spec/gnu_ext.td"
3+
include "spec/stdc.td"
4+
include "spec/posix.td"
55
include "spec/linux.td"
6+
include "spec/gnu_ext.td"
7+
include "spec/bsd_ext.td"
68
include "spec/llvm_libc_ext.td"
7-
include "spec/posix.td"
8-
include "spec/stdc.td"
99

1010
def AssertMacro : MacroDef<"assert"> {
1111
let Defn = [{
@@ -242,6 +242,10 @@ def SysUtsNameAPI : PublicAPI<"sys/utsname.h"> {
242242
let Types = ["struct utsname"];
243243
}
244244

245+
def SysEpollAPI : PublicAPI<"sys/epoll.h"> {
246+
let Types = ["struct epoll_event", "struct epoll_data", "sigset_t", "struct timespec"];
247+
}
248+
245249
def SpawnAPI : PublicAPI<"spawn.h"> {
246250
let Types = ["mode_t", "pid_t", "posix_spawnattr_t", "posix_spawn_file_actions_t"];
247251
}

libc/config/linux/arm/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ set(TARGET_LIBC_ENTRYPOINTS
102102

103103
# sys/prctl.h entrypoints
104104
libc.src.sys.prctl.prctl
105+
106+
# sys/epoll.h entrypoints
107+
# Disabled due to epoll_wait syscalls not being available on this platform.
108+
# libc.src.sys.epoll.epoll_wait
109+
# libc.src.sys.epoll.epoll_pwait
110+
# libc.src.sys.epoll.epoll_pwait2
111+
105112
)
106113

107114
set(TARGET_LIBM_ENTRYPOINTS

libc/config/linux/arm/headers.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ set(TARGET_PUBLIC_HEADERS
1010
libc.include.string
1111
libc.include.strings
1212
libc.include.search
13+
14+
# Disabled due to epoll_wait syscalls not being available on this platform.
15+
# libc.include.sys_epoll
1316
)

libc/config/linux/riscv/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ set(TARGET_LIBC_ENTRYPOINTS
185185
# sys/auxv.h entrypoints
186186
libc.src.sys.auxv.getauxval
187187

188+
# sys/epoll.h entrypoints
189+
# Disabled due to epoll_wait syscalls not being available on this platform.
190+
# libc.src.sys.epoll.epoll_wait
191+
# libc.src.sys.epoll.epoll_pwait
192+
# libc.src.sys.epoll.epoll_pwait2
193+
188194
# termios.h entrypoints
189195
libc.src.termios.cfgetispeed
190196
libc.src.termios.cfgetospeed

libc/config/linux/riscv/headers.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ set(TARGET_PUBLIC_HEADERS
3030
libc.include.arpa_inet
3131

3232
libc.include.sys_auxv
33+
# Disabled due to epoll_wait syscalls not being available on this platform.
34+
# libc.include.sys_epoll
3335
libc.include.sys_ioctl
3436
libc.include.sys_mman
3537
libc.include.sys_prctl

libc/config/linux/syscall_numbers.h.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@
258258
#define SYS_epoll_pwait __NR_epoll_pwait
259259
#endif
260260

261+
#ifdef __NR_epoll_pwait2
262+
#define SYS_epoll_pwait2 __NR_epoll_pwait2
263+
#endif
264+
261265
#ifdef __NR_epoll_wait
262266
#define SYS_epoll_wait __NR_epoll_wait
263267
#endif

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ set(TARGET_LIBC_ENTRYPOINTS
143143
libc.src.stdio.scanf
144144
libc.src.stdio.fscanf
145145

146+
# sys/epoll.h entrypoints
147+
libc.src.sys.epoll.epoll_wait
148+
libc.src.sys.epoll.epoll_pwait
149+
libc.src.sys.epoll.epoll_pwait2
150+
146151
# sys/mman.h entrypoints
147152
libc.src.sys.mman.madvise
148153
libc.src.sys.mman.mmap

libc/config/linux/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(TARGET_PUBLIC_HEADERS
3030
libc.include.arpa_inet
3131

3232
libc.include.sys_auxv
33+
libc.include.sys_epoll
3334
libc.include.sys_ioctl
3435
libc.include.sys_mman
3536
libc.include.sys_prctl

libc/include/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ add_gen_header(
341341
.llvm-libc-macros.sys_auxv_macros
342342
)
343343

344+
add_gen_header(
345+
sys_epoll
346+
DEF_FILE sys/epoll.h.def
347+
GEN_HDR sys/epoll.h
348+
DEPENDS
349+
.llvm_libc_common_h
350+
.llvm-libc-types.struct_epoll_event
351+
.llvm-libc-types.struct_epoll_data
352+
.llvm-libc-types.sigset_t
353+
)
354+
344355
add_gen_header(
345356
sys_ioctl
346357
DEF_FILE sys/ioctl.h.def

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,5 @@ add_header(rpc_opcodes_t HDR rpc_opcodes_t.h)
9696
add_header(ACTION HDR ACTION.h)
9797
add_header(ENTRY HDR ENTRY.h)
9898
add_header(struct_hsearch_data HDR struct_hsearch_data.h)
99+
add_header(struct_epoll_event HDR struct_epoll_event.h)
100+
add_header(struct_epoll_data HDR struct_epoll_data.h)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Definition of epoll_data type -------------------------------------===//
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_EPOLL_DATA_H__
10+
#define __LLVM_LIBC_TYPES_EPOLL_DATA_H__
11+
12+
union epoll_data {
13+
void *ptr;
14+
int fd;
15+
__UINT32_TYPE__ u32;
16+
__UINT64_TYPE__ u64;
17+
};
18+
19+
typedef union epoll_data epoll_data_t;
20+
21+
#endif // __LLVM_LIBC_TYPES_EPOLL_DATA_H__
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Definition of epoll_event type ------------------------------------===//
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_EPOLL_EVENT_H__
10+
#define __LLVM_LIBC_TYPES_EPOLL_EVENT_H__
11+
12+
#include <llvm-libc-types/struct_epoll_data.h>
13+
14+
typedef struct epoll_event {
15+
__UINT32_TYPE__ events;
16+
epoll_data_t data;
17+
} epoll_event;
18+
19+
#endif // __LLVM_LIBC_TYPES_EPOLL_EVENT_H__

libc/include/sys/epoll.h.def

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- Linux header epoll.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_SYS_EPOLL_H
10+
#define LLVM_LIBC_SYS_EPOLL_H
11+
12+
#include <__llvm-libc-common.h>
13+
14+
%%public_api()
15+
16+
#endif // LLVM_LIBC_SYS_EPOLL_H

libc/spec/gnu_ext.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
238238
[], // Macros
239239
[], // Types
240240
[], // Enumerations
241-
[] // Functions
241+
[
242+
//TODO: Add getauxval here
243+
] // Functions
242244
>;
243245

244246
HeaderSpec SendFile = HeaderSpec<

libc/spec/linux.td

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
def StructEpollEvent : NamedType<"struct epoll_event">;
2+
def StructEpollEventPtr : PtrType<StructEpollEvent>;
3+
4+
def StructEpollData : NamedType<"struct epoll_data">;
5+
16
def Linux : StandardSpec<"Linux"> {
27
HeaderSpec Errno = HeaderSpec<
38
"errno.h",
@@ -137,6 +142,77 @@ def Linux : StandardSpec<"Linux"> {
137142
[] // Functions
138143
>;
139144

145+
146+
HeaderSpec SysEpoll = HeaderSpec<
147+
"sys/epoll.h",
148+
[], // Macros
149+
[
150+
StructEpollEvent,
151+
StructEpollData,
152+
SigSetType,
153+
StructTimeSpec,
154+
], // Types
155+
[], // Enumerations
156+
[
157+
FunctionSpec<
158+
"epoll_create",
159+
RetValSpec<IntType>,
160+
[
161+
ArgSpec<IntType>
162+
]
163+
>,
164+
FunctionSpec<
165+
"epoll_create1",
166+
RetValSpec<IntType>,
167+
[
168+
ArgSpec<IntType>
169+
]
170+
>,
171+
FunctionSpec<
172+
"epoll_ctl",
173+
RetValSpec<IntType>,
174+
[
175+
ArgSpec<IntType>,
176+
ArgSpec<IntType>,
177+
ArgSpec<IntType>,
178+
ArgSpec<StructEpollEventPtr>
179+
]
180+
>,
181+
FunctionSpec<
182+
"epoll_wait",
183+
RetValSpec<IntType>,
184+
[
185+
ArgSpec<IntType>,
186+
ArgSpec<StructEpollEventPtr>,
187+
ArgSpec<IntType>,
188+
ArgSpec<IntType>
189+
]
190+
>,
191+
FunctionSpec<
192+
"epoll_pwait",
193+
RetValSpec<IntType>,
194+
[
195+
ArgSpec<IntType>,
196+
ArgSpec<StructEpollEventPtr>,
197+
ArgSpec<IntType>,
198+
ArgSpec<IntType>,
199+
ArgSpec<SigSetPtrType>
200+
]
201+
>,
202+
FunctionSpec<
203+
"epoll_pwait2",
204+
RetValSpec<IntType>,
205+
[
206+
ArgSpec<IntType>,
207+
ArgSpec<StructEpollEventPtr>,
208+
ArgSpec<IntType>,
209+
ArgSpec<ConstStructTimeSpecPtr>,
210+
ArgSpec<SigSetPtrType>
211+
]
212+
>,
213+
] // Functions
214+
>;
215+
140216
HeaderSpec Signal = HeaderSpec<
141217
"signal.h",
142218
[
@@ -181,6 +257,7 @@ def Linux : StandardSpec<"Linux"> {
181257

182258
let Headers = [
183259
Errno,
260+
SysEpoll,
184261
SysMMan,
185262
SysPrctl,
186263
SysRandom,

libc/src/sys/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_subdirectory(auxv)
2+
add_subdirectory(epoll)
23
add_subdirectory(mman)
34
add_subdirectory(random)
45
add_subdirectory(resource)

libc/src/sys/epoll/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
3+
endif()
4+
5+
add_entrypoint_object(
6+
epoll_wait
7+
ALIAS
8+
DEPENDS
9+
.${LIBC_TARGET_OS}.epoll_wait
10+
)
11+
12+
add_entrypoint_object(
13+
epoll_pwait
14+
ALIAS
15+
DEPENDS
16+
.${LIBC_TARGET_OS}.epoll_pwait
17+
)
18+
19+
add_entrypoint_object(
20+
epoll_pwait2
21+
ALIAS
22+
DEPENDS
23+
.${LIBC_TARGET_OS}.epoll_pwait2
24+
)

libc/src/sys/epoll/epoll_pwait.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Implementation header for epoll_pwait function ----------*- 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_SYS_EPOLL_EPOLL_PWAIT_H
10+
#define LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_PWAIT_H
11+
12+
// TODO: Use this include once the include headers are also using quotes.
13+
// #include "include/llvm-libc-types/sigset_t.h"
14+
// #include "include/llvm-libc-types/struct_epoll_event.h"
15+
16+
#include <sys/epoll.h>
17+
18+
namespace LIBC_NAMESPACE {
19+
20+
// TODO: sigmask should be nullable
21+
int epoll_pwait(int epfd, epoll_event *events, int maxevents, int timeout,
22+
const sigset_t *sigmask);
23+
24+
} // namespace LIBC_NAMESPACE
25+
26+
#endif // LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_PWAIT_H

libc/src/sys/epoll/epoll_pwait2.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation header for epoll_pwait2 function ---------*- 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_SYS_EPOLL_EPOLL_PWAIT2_H
10+
#define LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_PWAIT2_H
11+
12+
// TODO: Use this include once the include headers are also using quotes.
13+
// #include "include/llvm-libc-types/sigset_t.h"
14+
// #include "include/llvm-libc-types/struct_epoll_event.h"
15+
// #include "include/llvm-libc-types/struct_timespec.h"
16+
17+
#include <sys/epoll.h>
18+
19+
namespace LIBC_NAMESPACE {
20+
21+
// TODO: sigmask and timeout should be nullable
22+
int epoll_pwait2(int epfd, epoll_event *events, int maxevents,
23+
const timespec *timeout, const sigset_t *sigmask);
24+
25+
} // namespace LIBC_NAMESPACE
26+
27+
#endif // LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_PWAIT2_H

0 commit comments

Comments
 (0)