Skip to content

Commit

Permalink
[libc] add remaining epoll functions, pipe (llvm#84587)
Browse files Browse the repository at this point in the history
The epoll_wait functions need the rest of the epoll functions (create,
ctl) to be available to actually test them, as well as pipe to create a
usable file descriptor. This patch adds epoll_create, epoll_create1,
epoll_ctl, and pipe. These have tests, and the tests for epoll_wait,
epoll_pwait, and epoll_pwait2 (currently disabled) are updated to use
these newly available functions.
  • Loading branch information
michaelrj-google authored Apr 11, 2024
1 parent 2f55056 commit 5fb8215
Show file tree
Hide file tree
Showing 40 changed files with 933 additions and 38 deletions.
4 changes: 4 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.fileno

# sys/epoll.h entrypoints
libc.src.sys.epoll.epoll_create
libc.src.sys.epoll.epoll_create1
libc.src.sys.epoll.epoll_ctl
libc.src.sys.epoll.epoll_wait
libc.src.sys.epoll.epoll_pwait
# TODO: Need to check if pwait2 is available before providing.
Expand Down Expand Up @@ -308,6 +311,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.link
libc.src.unistd.linkat
libc.src.unistd.lseek
libc.src.unistd.pipe
libc.src.unistd.pread
libc.src.unistd.pwrite
libc.src.unistd.read
Expand Down
18 changes: 18 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,22 @@ add_proxy_header_library(
libc.include.fenv
)

add_proxy_header_library(
signal_macros
HDRS
signal_macros.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-macros.signal_macros
libc.include.signal
)

add_proxy_header_library(
sys_epoll_macros
HDRS
sys_epoll_macros.h
FULL_BUILD_DEPENDS
libc.include.sys_epoll
libc.include.llvm-libc-macros.sys_epoll_macros
)

add_subdirectory(types)
22 changes: 22 additions & 0 deletions libc/hdr/signal_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Definition of macros from signal.h --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_SIGNAL_MACROS_H
#define LLVM_LIBC_HDR_SIGNAL_MACROS_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-macros/signal-macros.h"

#else // Overlay mode

#include <signal.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_SIGNAL_MACROS_H
22 changes: 22 additions & 0 deletions libc/hdr/sys_epoll_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Definition of macros from sys/epoll.h -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_SYS_EPOLL_MACROS_H
#define LLVM_LIBC_HDR_SYS_EPOLL_MACROS_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-macros/sys-epoll-macros.h"

#else // Overlay mode

#include <sys/epoll.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_SYS/EPOLL_MACROS_H
1 change: 1 addition & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ add_gen_header(
.llvm-libc-types.struct_epoll_event
.llvm-libc-types.struct_epoll_data
.llvm-libc-types.sigset_t
.llvm-libc-macros.sys_epoll_macros
)

add_gen_header(
Expand Down
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ add_macro_header(
sys-auxv-macros.h
)

add_macro_header(
sys_epoll_macros
HDR
sys-epoll-macros.h
)

add_macro_header(
sys_ioctl_macros
HDR
Expand Down
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ add_header(
sched-macros.h
)

add_header(
sys_epoll_macros
HDR
sys-epoll-macros.h
)

add_header(
sys_ioctl_macros
HDR
Expand Down
40 changes: 40 additions & 0 deletions libc/include/llvm-libc-macros/linux/sys-epoll-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===-- Macros defined in sys/epoll.h header file -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_MACROS_LINUX_SYS_EPOLL_MACROS_H
#define LLVM_LIBC_MACROS_LINUX_SYS_EPOLL_MACROS_H

#include "fcntl-macros.h"

// These are also defined in <linux/eventpoll.h> but that also contains a
// different definition of the epoll_event struct that is different from the
// userspace version.

#define EPOLL_CLOEXEC O_CLOEXEC

#define EPOLL_CTL_ADD 1
#define EPOLL_CTL_DEL 2
#define EPOLL_CTL_MOD 3

#define EPOLLIN 0x1
#define EPOLLPRI 0x2
#define EPOLLOUT 0x4
#define EPOLLERR 0x8
#define EPOLLHUP 0x10
#define EPOLLRDNORM 0x40
#define EPOLLRDBAND 0x80
#define EPOLLWRNORM 0x100
#define EPOLLWRBAND 0x200
#define EPOLLMSG 0x400
#define EPOLLRDHUP 0x2000
#define EPOLLEXCLUSIVE 0x10000000
#define EPOLLWAKEUP 0x20000000
#define EPOLLONESHOT 0x40000000
#define EPOLLET 0x80000000

#endif // LLVM_LIBC_MACROS_LINUX_SYS_EPOLL_MACROS_H
16 changes: 16 additions & 0 deletions libc/include/llvm-libc-macros/sys-epoll-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===-- Macros defined in sys/epoll.h header file -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_MACROS_SYS_EPOLL_MACROS_H
#define LLVM_LIBC_MACROS_SYS_EPOLL_MACROS_H

#ifdef __linux__
#include "linux/sys-epoll-macros.h"
#endif

#endif // LLVM_LIBC_MACROS_SYS_EPOLL_MACROS_H
4 changes: 2 additions & 2 deletions libc/include/llvm-libc-types/sigset_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

// This definition can be adjusted/specialized for different targets and
// platforms as necessary. This definition works for Linux on most targets.
typedef struct {
struct sigset_t {
unsigned long __signals[__NSIGSET_WORDS];
} sigset_t;
};

#endif // LLVM_LIBC_TYPES_SIGSET_T_H
6 changes: 5 additions & 1 deletion libc/include/llvm-libc-types/struct_epoll_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

#include "llvm-libc-types/struct_epoll_data.h"

typedef struct epoll_event {
typedef struct
#ifdef __x86_64__
[[gnu::packed]] // Necessary for compatibility.
#endif
epoll_event {
__UINT32_TYPE__ events;
epoll_data_t data;
} epoll_event;
Expand Down
2 changes: 2 additions & 0 deletions libc/include/sys/epoll.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "__llvm-libc-common.h"

#include "llvm-libc-macros/sys-epoll-macros.h"

%%public_api()

#endif // LLVM_LIBC_SYS_EPOLL_H
5 changes: 5 additions & 0 deletions libc/spec/posix.td
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,11 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<OffTType>,
[ArgSpec<IntType>, ArgSpec<OffTType>, ArgSpec<IntType>]
>,
FunctionSpec<
"pipe",
RetValSpec<IntType>,
[ArgSpec<IntPtr>] //TODO: make this int[2]
>,
FunctionSpec<
"pread",
RetValSpec<SSizeTType>,
Expand Down
21 changes: 21 additions & 0 deletions libc/src/sys/epoll/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()

add_entrypoint_object(
epoll_create
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.epoll_create
)

add_entrypoint_object(
epoll_create1
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.epoll_create1
)

add_entrypoint_object(
epoll_ctl
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.epoll_ctl
)

add_entrypoint_object(
epoll_wait
ALIAS
Expand Down
18 changes: 18 additions & 0 deletions libc/src/sys/epoll/epoll_create.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for epoll_create function ---------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_CREATE_H
#define LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_CREATE_H

namespace LIBC_NAMESPACE {

int epoll_create(int size);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_CREATE_H
18 changes: 18 additions & 0 deletions libc/src/sys/epoll/epoll_create1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for epoll_create1 function --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_CREATE1_H
#define LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_CREATE1_H

namespace LIBC_NAMESPACE {

int epoll_create1(int flags);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_CREATE1_H
21 changes: 21 additions & 0 deletions libc/src/sys/epoll/epoll_ctl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for epoll_ctl function ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_CTL_H
#define LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_CTL_H

#include "hdr/types/struct_epoll_event.h"

namespace LIBC_NAMESPACE {

// TODO: event should be nullable
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_CTL_H
4 changes: 2 additions & 2 deletions libc/src/sys/epoll/epoll_pwait.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
namespace LIBC_NAMESPACE {

// TODO: sigmask should be nullable
int epoll_pwait(int epfd, epoll_event *events, int maxevents, int timeout,
const sigset_t *sigmask);
int epoll_pwait(int epfd, struct epoll_event *events, int maxevents,
int timeout, const sigset_t *sigmask);

} // namespace LIBC_NAMESPACE

Expand Down
45 changes: 42 additions & 3 deletions libc/src/sys/epoll/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
add_entrypoint_object(
epoll_create
SRCS
epoll_create.cpp
HDRS
../epoll_create.h
DEPENDS
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
epoll_create1
SRCS
epoll_create1.cpp
HDRS
../epoll_create1.h
DEPENDS
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
epoll_ctl
SRCS
epoll_ctl.cpp
HDRS
../epoll_ctl.h
DEPENDS
libc.hdr.types.struct_epoll_event
libc.hdr.sys_epoll_macros
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
epoll_wait
SRCS
Expand All @@ -23,7 +61,8 @@ add_entrypoint_object(
libc.hdr.types.sigset_t
libc.hdr.types.struct_epoll_event
libc.hdr.types.struct_timespec
libc.include.signal
libc.hdr.sys_epoll_macros
libc.hdr.signal_macros
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
Expand All @@ -39,9 +78,9 @@ add_entrypoint_object(
libc.hdr.types.sigset_t
libc.hdr.types.struct_epoll_event
libc.hdr.types.struct_timespec
libc.include.signal
libc.hdr.sys_epoll_macros
libc.hdr.signal_macros
libc.include.sys_syscall
libc.include.time
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
Loading

0 comments on commit 5fb8215

Please sign in to comment.