Skip to content

Commit edb7206

Browse files
[libc] add epoll_wait functions (#79515)
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 32334b9 commit edb7206

34 files changed

+604
-5
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ set(TARGET_LIBC_ENTRYPOINTS
172172
# sys/auxv.h entrypoints
173173
libc.src.sys.auxv.getauxval
174174

175+
# sys/epoll.h entrypoints
176+
libc.src.sys.epoll.epoll_wait
177+
libc.src.sys.epoll.epoll_pwait
178+
libc.src.sys.epoll.epoll_pwait2
179+
175180
# termios.h entrypoints
176181
libc.src.termios.cfgetispeed
177182
libc.src.termios.cfgetospeed

libc/config/linux/aarch64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ set(TARGET_PUBLIC_HEADERS
2424
libc.include.unistd
2525

2626
libc.include.sys_ioctl
27+
libc.include.sys_epoll
2728
)

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ set(TARGET_LIBC_ENTRYPOINTS
9595

9696
# sys/prctl.h entrypoints
9797
libc.src.sys.prctl.prctl
98+
99+
# sys/epoll.h entrypoints
100+
libc.src.sys.epoll.epoll_wait
101+
libc.src.sys.epoll.epoll_pwait
102+
libc.src.sys.epoll.epoll_pwait2
103+
98104
)
99105

100106
set(TARGET_LIBM_ENTRYPOINTS

libc/config/linux/arm/headers.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ set(TARGET_PUBLIC_HEADERS
99
libc.include.string
1010
libc.include.strings
1111
libc.include.search
12+
13+
libc.include.sys_epoll
1214
)

libc/config/linux/riscv/entrypoints.txt

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

181+
# sys/epoll.h entrypoints
182+
libc.src.sys.epoll.epoll_wait
183+
libc.src.sys.epoll.epoll_pwait
184+
libc.src.sys.epoll.epoll_pwait2
185+
181186
# termios.h entrypoints
182187
libc.src.termios.cfgetispeed
183188
libc.src.termios.cfgetospeed

libc/config/linux/riscv/headers.txt

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

3131
libc.include.sys_auxv
32+
libc.include.sys_epoll
3233
libc.include.sys_ioctl
3334
libc.include.sys_mman
3435
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
@@ -136,6 +136,11 @@ set(TARGET_LIBC_ENTRYPOINTS
136136
libc.src.stdio.scanf
137137
libc.src.stdio.fscanf
138138

139+
# sys/epoll.h entrypoints
140+
libc.src.sys.epoll.epoll_wait
141+
libc.src.sys.epoll.epoll_pwait
142+
libc.src.sys.epoll.epoll_pwait2
143+
139144
# sys/mman.h entrypoints
140145
libc.src.sys.mman.madvise
141146
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
@@ -29,6 +29,7 @@ set(TARGET_PUBLIC_HEADERS
2929
libc.include.arpa_inet
3030

3131
libc.include.sys_auxv
32+
libc.include.sys_epoll
3233
libc.include.sys_ioctl
3334
libc.include.sys_mman
3435
libc.include.sys_prctl

libc/include/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,17 @@ add_gen_header(
332332
.llvm-libc-macros.sys_auxv_macros
333333
)
334334

335+
add_gen_header(
336+
sys_epoll
337+
DEF_FILE sys/epoll.h.def
338+
GEN_HDR sys/epoll.h
339+
DEPENDS
340+
.llvm_libc_common_h
341+
.llvm-libc-types.struct_epoll_event
342+
.llvm-libc-types.struct_epoll_data
343+
.llvm-libc-types.sigset_t
344+
)
345+
335346
add_gen_header(
336347
sys_ioctl
337348
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/epoll_data.h>
13+
14+
typedef struct epoll_event {
15+
__UINT32_TYPE__ events;
16+
epoll_data_t data;
17+
};
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#include <signal.h> // For sigset_t
13+
#include <sys/epoll.h> // For epoll_event
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
// TODO: sigmask should be nullable
18+
int epoll_pwait(int epfd, struct epoll_event *events, int maxevents,
19+
int timeout, const sigset_t *sigmask);
20+
21+
} // namespace LIBC_NAMESPACE
22+
23+
#endif // LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_PWAIT_H

libc/src/sys/epoll/epoll_pwait2.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
#include <signal.h> // For sigset_t
13+
#include <sys/epoll.h> // For epoll_event
14+
#include <time.h> // For timespec
15+
16+
namespace LIBC_NAMESPACE {
17+
18+
// TODO: sigmask and timeout should be nullable
19+
int epoll_pwait2(int epfd, struct epoll_event *events, int maxevents,
20+
const struct timespec *timeout, const sigset_t *sigmask);
21+
22+
} // namespace LIBC_NAMESPACE
23+
24+
#endif // LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_PWAIT2_H

0 commit comments

Comments
 (0)