21 changes: 21 additions & 0 deletions libc/src/sys/epoll/epoll_wait.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for epoll_wait 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_WAIT_H
#define LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_WAIT_H

#include <sys/epoll.h> // For epoll_event

namespace LIBC_NAMESPACE {

int epoll_wait(int epfd, struct epoll_event *events, int maxevents,
int timeout);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_SYS_EPOLL_EPOLL_WAIT_H
41 changes: 41 additions & 0 deletions libc/src/sys/epoll/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
add_entrypoint_object(
epoll_wait
SRCS
epoll_wait.cpp
HDRS
../epoll_wait.h
DEPENDS
libc.include.sys_epoll
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

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

add_entrypoint_object(
epoll_pwait2
SRCS
epoll_pwait2.cpp
HDRS
../epoll_pwait2.h
DEPENDS
libc.include.sys_epoll
libc.include.signal
libc.include.time
libc.include.sys_syscall
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
38 changes: 38 additions & 0 deletions libc/src/sys/epoll/linux/epoll_pwait.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===---------- Linux implementation of the epoll_pwait function ----------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/sys/epoll/epoll_pwait.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"

#include "src/errno/libc_errno.h"
#include <sys/syscall.h> // For syscall numbers.

#include <signal.h> // For sigset_t

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, epoll_pwait,
(int epfd, struct epoll_event *events, int maxevents,
int timeout, const sigset_t *sigmask)) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_epoll_pwait, epfd, reinterpret_cast<long>(events), maxevents, timeout,
reinterpret_cast<long>(sigmask), sizeof(sigset_t));

// A negative return value indicates an error with the magnitude of the
// value being the error code.
if (ret < 0) {
libc_errno = -ret;
return -1;
}

return 0;
}

} // namespace LIBC_NAMESPACE
40 changes: 40 additions & 0 deletions libc/src/sys/epoll/linux/epoll_pwait2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===---------- Linux implementation of the epoll_pwait2 function ---------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/sys/epoll/epoll_pwait2.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"

#include "src/errno/libc_errno.h"
#include <sys/syscall.h> // For syscall numbers.

#include <signal.h> // For sigset_t
#include <time.h> // For timespec

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, epoll_pwait2,
(int epfd, struct epoll_event *events, int maxevents,
const struct timespec *timeout, const sigset_t *sigmask)) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_epoll_pwait2, epfd, reinterpret_cast<long>(events), maxevents,
reinterpret_cast<long>(timeout), reinterpret_cast<long>(sigmask),
sizeof(sigset_t));

// A negative return value indicates an error with the magnitude of the
// value being the error code.
if (ret < 0) {
libc_errno = -ret;
return -1;
}

return 0;
}

} // namespace LIBC_NAMESPACE
35 changes: 35 additions & 0 deletions libc/src/sys/epoll/linux/epoll_wait.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===---------- Linux implementation of the epoll_wait function -----------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "src/sys/epoll/epoll_wait.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"

#include "src/errno/libc_errno.h"
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, epoll_wait,
(int epfd, struct epoll_event *events, int maxevents,
int timeout)) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(
SYS_epoll_wait, epfd, reinterpret_cast<long>(events), maxevents, timeout);

// A negative return value indicates an error with the magnitude of the
// value being the error code.
if (ret < 0) {
libc_errno = -ret;
return -1;
}

return 0;
}

} // namespace LIBC_NAMESPACE
1 change: 1 addition & 0 deletions libc/test/src/sys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ add_subdirectory(utsname)
add_subdirectory(wait)
add_subdirectory(prctl)
add_subdirectory(auxv)
add_subdirectory(epoll)
3 changes: 3 additions & 0 deletions libc/test/src/sys/epoll/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${LIBC_TARGET_OS})
endif()
39 changes: 39 additions & 0 deletions libc/test/src/sys/epoll/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
add_custom_target(libc_sys_epoll_unittests)
add_libc_unittest(
epoll_wait_test
SUITE
libc_sys_epoll_unittests
SRCS
epoll_wait_test.cpp
DEPENDS
libc.include.sys_epoll
libc.src.errno.errno
libc.src.sys.epoll.epoll_wait
libc.test.UnitTest.ErrnoSetterMatcher
)

add_libc_unittest(
epoll_pwait_test
SUITE
libc_sys_epoll_unittests
SRCS
epoll_pwait_test.cpp
DEPENDS
libc.include.sys_epoll
libc.src.errno.errno
libc.src.sys.epoll.epoll_pwait
libc.test.UnitTest.ErrnoSetterMatcher
)

add_libc_unittest(
epoll_pwait2_test
SUITE
libc_sys_epoll_unittests
SRCS
epoll_pwait2_test.cpp
DEPENDS
libc.include.sys_epoll
libc.src.errno.errno
libc.src.sys.epoll.epoll_pwait2
libc.test.UnitTest.ErrnoSetterMatcher
)
20 changes: 20 additions & 0 deletions libc/test/src/sys/epoll/linux/epoll_pwait2_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Unittests for epoll_pwait2 ----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "src/errno/libc_errno.h"
#include "src/sys/epoll/epoll_pwait2.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;

TEST(LlvmLibcEpollWaitTest, Basic) {
EXPECT_THAT(LIBC_NAMESPACE::epoll_pwait2(-1, nullptr, 0, nullptr, nullptr),
returns(EQ(-1ul)).with_errno(EQ(EINVAL)));
}

// TODO: Complete these tests when epoll_create is implemented.
20 changes: 20 additions & 0 deletions libc/test/src/sys/epoll/linux/epoll_pwait_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Unittests for epoll_pwait -----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "src/errno/libc_errno.h"
#include "src/sys/epoll/epoll_pwait.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;

TEST(LlvmLibcEpollWaitTest, Basic) {
EXPECT_THAT(LIBC_NAMESPACE::epoll_pwait(-1, nullptr, 0, 0, nullptr),
returns(EQ(-1ul)).with_errno(EQ(EINVAL)));
}

// TODO: Complete these tests when epoll_create is implemented.
20 changes: 20 additions & 0 deletions libc/test/src/sys/epoll/linux/epoll_wait_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Unittests for epoll_wait ------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "src/errno/libc_errno.h"
#include "src/sys/epoll/epoll_wait.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;

TEST(LlvmLibcEpollWaitTest, Basic) {
EXPECT_THAT(LIBC_NAMESPACE::epoll_wait(-1, nullptr, 0, 0),
returns(EQ(-1ul)).with_errno(EQ(EINVAL)));
}

// TODO: Complete these tests when epoll_create is implemented.
32 changes: 32 additions & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2942,3 +2942,35 @@ libc_function(
":vfprintf_internal",
],
)

############################## sys/epoll targets ###############################

libc_function(
name = "epoll_wait",
srcs = ["src/sys/epoll/linux/epoll_wait.cpp"],
hdrs = ["src/sys/epoll/epoll_wait.h"],
deps = [
":__support_osutil_syscall",
":errno",
],
)

libc_function(
name = "epoll_pwait",
srcs = ["src/sys/epoll/linux/epoll_pwait.cpp"],
hdrs = ["src/sys/epoll/epoll_pwait.h"],
deps = [
":__support_osutil_syscall",
":errno",
],
)

libc_function(
name = "epoll_pwait2",
srcs = ["src/sys/epoll/linux/epoll_pwait2.cpp"],
hdrs = ["src/sys/epoll/epoll_pwait2.h"],
deps = [
":__support_osutil_syscall",
":errno",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This file is licensed 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

# Tests for LLVM libc string.h functions.

load("//libc/test:libc_test_rules.bzl", "libc_test")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

libc_test(
name = "epoll_wait_test",
srcs = ["linux/epoll_wait_test.cpp"],
libc_function_deps = [
"//libc:epoll_wait",
],
)

libc_test(
name = "epoll_pwait_test",
srcs = ["linux/epoll_pwait_test.cpp"],
libc_function_deps = [
"//libc:epoll_pwait",
],
)

libc_test(
name = "epoll_pwait2_test",
srcs = ["linux/epoll_pwait2_test.cpp"],
libc_function_deps = [
"//libc:epoll_pwait2",
],
)