| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| //===---------- Linux implementation of the epoll_create 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_create.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. | ||
michaelrj-google marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| namespace LIBC_NAMESPACE { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, epoll_create, ([[maybe_unused]] int size)) { | ||
| #ifdef SYS_epoll_create | ||
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_epoll_create, size); | ||
| #elif defined(SYS_epoll_create1) | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_epoll_create1, 0); | ||
| #else | ||
| #error \ | ||
| "epoll_create and epoll_create1 are unavailable. Unable to build epoll_create." | ||
| #endif | ||
|
|
||
| // 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 ret; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //===---------- Linux implementation of the epoll_create1 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_create1.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_create1, (int flags)) { | ||
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_epoll_create1, flags); | ||
|
|
||
| // 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 ret; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| //===---------- Linux implementation of the epoll_ctl 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_ctl.h" | ||
|
|
||
| #include "hdr/types/struct_epoll_event.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_ctl, | ||
| (int epfd, int op, int fd, epoll_event *event)) { | ||
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_epoll_ctl, epfd, op, fd, | ||
| reinterpret_cast<long>(event)); | ||
|
|
||
| // 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 ret; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //===-- Linux implementation of pipe --------------------------------------===// | ||
| // | ||
| // 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/unistd/pipe.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, pipe, (int pipefd[2])) { | ||
| #ifdef SYS_pipe | ||
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_pipe, | ||
| reinterpret_cast<long>(pipefd)); | ||
| #elif defined(SYS_pipe2) | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| int ret = LIBC_NAMESPACE::syscall_impl<int>( | ||
| SYS_pipe2, reinterpret_cast<long>(pipefd), 0); | ||
| #endif | ||
| if (ret < 0) { | ||
| libc_errno = -ret; | ||
| return -1; | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //===-- Implementation header for pipe --------------------------*- 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_UNISTD_PIPE_H | ||
| #define LLVM_LIBC_SRC_UNISTD_PIPE_H | ||
|
|
||
| namespace LIBC_NAMESPACE { | ||
|
|
||
| int pipe(int pipefd[2]); | ||
|
|
||
| } // namespace LIBC_NAMESPACE | ||
|
|
||
| #endif // LLVM_LIBC_SRC_UNISTD_PIPE_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //===-- Unittests for epoll_create1 ---------------------------------------===// | ||
| // | ||
| // 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 "hdr/sys_epoll_macros.h" | ||
| #include "src/errno/libc_errno.h" | ||
| #include "src/sys/epoll/epoll_create1.h" | ||
| #include "src/unistd/close.h" | ||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; | ||
|
|
||
| TEST(LlvmLibcEpollCreate1Test, Basic) { | ||
| int fd = LIBC_NAMESPACE::epoll_create1(0); | ||
| ASSERT_GT(fd, 0); | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ASSERT_ERRNO_SUCCESS(); | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds()); | ||
| } | ||
|
|
||
| TEST(LlvmLibcEpollCreate1Test, CloseOnExecute) { | ||
| int fd = LIBC_NAMESPACE::epoll_create1(EPOLL_CLOEXEC); | ||
| ASSERT_GT(fd, 0); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds()); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //===-- Unittests for epoll_create ----------------------------------------===// | ||
| // | ||
| // 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_create.h" | ||
| #include "src/unistd/close.h" | ||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; | ||
|
|
||
| TEST(LlvmLibcEpollCreateTest, Basic) { | ||
| int fd = LIBC_NAMESPACE::epoll_create(1); | ||
michaelrj-google marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ASSERT_GT(fd, 0); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds()); | ||
| } | ||
|
|
||
| TEST(LlvmLibcEpollCreateTest, Fails) { | ||
| ASSERT_THAT(LIBC_NAMESPACE::epoll_create(0), Fails(EINVAL)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| //===-- Unittests for epoll_ctl -------------------------------------------===// | ||
| // | ||
| // 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 "hdr/sys_epoll_macros.h" | ||
| #include "hdr/types/struct_epoll_event.h" | ||
| #include "src/errno/libc_errno.h" | ||
| #include "src/sys/epoll/epoll_create1.h" | ||
| #include "src/sys/epoll/epoll_ctl.h" | ||
| #include "src/unistd/close.h" | ||
| #include "src/unistd/pipe.h" | ||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; | ||
|
|
||
| TEST(LlvmLibcEpollCtlTest, Basic) { | ||
| int epfd = LIBC_NAMESPACE::epoll_create1(0); | ||
| ASSERT_GT(epfd, 0); | ||
| ASSERT_ERRNO_SUCCESS(); | ||
|
|
||
| int pipefd[2]; | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds()); | ||
|
|
||
| epoll_event event; | ||
| event.events = EPOLLOUT; | ||
| event.data.fd = pipefd[0]; | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::epoll_ctl(epfd, EPOLL_CTL_ADD, pipefd[0], &event), | ||
| Succeeds()); | ||
|
|
||
| // adding the same file fail. | ||
| ASSERT_THAT(LIBC_NAMESPACE::epoll_ctl(epfd, EPOLL_CTL_ADD, pipefd[0], &event), | ||
| Fails(EEXIST)); | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::epoll_ctl(epfd, EPOLL_CTL_DEL, pipefd[0], &event), | ||
| Succeeds()); | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds()); | ||
| ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds()); | ||
| ASSERT_THAT(LIBC_NAMESPACE::close(epfd), Succeeds()); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| //===-- Unittests for pipe ------------------------------------------------===// | ||
| // | ||
| // 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/unistd/close.h" | ||
| #include "src/unistd/pipe.h" | ||
|
|
||
| #include "test/UnitTest/ErrnoSetterMatcher.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; | ||
|
|
||
| TEST(LlvmLibcPipeTest, SmokeTest) { | ||
|
|
||
| int pipefd[2]; | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds()); | ||
|
|
||
| ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds()); | ||
| ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds()); | ||
| } | ||
|
|
||
| // TODO: Functionality tests |