Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <errno.h> |
Philip Hatcher | 9ded07c | 2014-02-25 14:16:37 +0100 | [diff] [blame] | 20 | #include <fcntl.h> |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 21 | #include <signal.h> |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 22 | #include <sys/epoll.h> |
Philip Hatcher | 9ded07c | 2014-02-25 14:16:37 +0100 | [diff] [blame] | 23 | #include <unistd.h> |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 25 | #include "utils.h" |
| 26 | |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 27 | TEST(sys_epoll, epoll_wait) { |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 28 | int epoll_fd = epoll_create(1); |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 29 | ASSERT_NE(-1, epoll_fd); |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 30 | |
| 31 | // Regular epoll_wait. |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 32 | epoll_event events[1] = {}; |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 33 | ASSERT_EQ(0, epoll_wait(epoll_fd, events, 1, 1)); |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | TEST(sys_epoll, epoll_pwait_no_sigset) { |
| 37 | int epoll_fd = epoll_create(1); |
| 38 | ASSERT_NE(-1, epoll_fd); |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 39 | |
| 40 | // epoll_pwait without a sigset (which is equivalent to epoll_wait). |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 41 | epoll_event events[1] = {}; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 42 | ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, nullptr)); |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 43 | } |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 45 | TEST(sys_epoll, epoll_pwait64_no_sigset) { |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 46 | #if defined(__BIONIC__) |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 47 | int epoll_fd = epoll_create(1); |
| 48 | ASSERT_NE(-1, epoll_fd); |
| 49 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 50 | // epoll_pwait64 without a sigset (which is equivalent to epoll_wait). |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 51 | epoll_event events[1] = {}; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 52 | ASSERT_EQ(0, epoll_pwait64(epoll_fd, events, 1, 1, nullptr)); |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 53 | #else |
| 54 | GTEST_SKIP() << "epoll_pwait64 is bionic-only"; |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 55 | #endif |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | TEST(sys_epoll, epoll_pwait2_no_sigset) { |
| 59 | #if defined(__BIONIC__) |
| 60 | int epoll_fd = epoll_create(1); |
| 61 | ASSERT_NE(-1, epoll_fd); |
| 62 | |
| 63 | // epoll_pwait2 without a sigset (which is equivalent to epoll_wait). |
| 64 | epoll_event events[1] = {}; |
| 65 | timespec ts = {.tv_nsec = 500}; |
| 66 | int rc = epoll_pwait2(epoll_fd, events, 1, &ts, nullptr); |
| 67 | if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no epoll_pwait2 in this kernel"; |
| 68 | ASSERT_EQ(0, rc) << strerror(errno); |
| 69 | #else |
| 70 | GTEST_SKIP() << "epoll_pwait2 is only in glibc 2.35+"; |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | TEST(sys_epoll, epoll_pwait_with_sigset) { |
| 75 | int epoll_fd = epoll_create(1); |
| 76 | ASSERT_NE(-1, epoll_fd); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 77 | |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 78 | // epoll_pwait with a sigset. |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 79 | epoll_event events[1] = {}; |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 80 | sigset_t ss; |
| 81 | sigemptyset(&ss); |
| 82 | sigaddset(&ss, SIGPIPE); |
| 83 | ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, &ss)); |
| 84 | } |
Philip Hatcher | 9ded07c | 2014-02-25 14:16:37 +0100 | [diff] [blame] | 85 | |
Elliott Hughes | 4c62e59 | 2023-07-13 15:45:33 -0700 | [diff] [blame] | 86 | TEST(sys_epoll, epoll_pwait2_with_sigset) { |
| 87 | int epoll_fd = epoll_create(1); |
| 88 | ASSERT_NE(-1, epoll_fd); |
| 89 | |
| 90 | #if defined(__BIONIC__) |
| 91 | epoll_event events[1] = {}; |
| 92 | timespec ts = {.tv_nsec = 500}; |
| 93 | sigset_t ss2; |
| 94 | sigemptyset(&ss2); |
| 95 | sigaddset(&ss2, SIGPIPE); |
| 96 | int rc = epoll_pwait2(epoll_fd, events, 1, &ts, &ss2); |
| 97 | if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no epoll_pwait2 in this kernel"; |
| 98 | ASSERT_EQ(0, rc) << strerror(errno); |
| 99 | #else |
| 100 | GTEST_SKIP() << "epoll_pwait2 is only in glibc 2.35+"; |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | TEST(sys_epoll, epoll_pwait2_64_with_sigset) { |
| 105 | int epoll_fd = epoll_create(1); |
| 106 | ASSERT_NE(-1, epoll_fd); |
| 107 | |
| 108 | #if defined(__BIONIC__) |
| 109 | epoll_event events[1] = {}; |
| 110 | timespec ts = {.tv_nsec = 500}; |
| 111 | sigset64_t ss2; |
| 112 | sigemptyset64(&ss2); |
| 113 | sigaddset64(&ss2, SIGPIPE); |
| 114 | int rc = epoll_pwait2_64(epoll_fd, events, 1, &ts, &ss2); |
| 115 | if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no epoll_pwait2 in this kernel"; |
| 116 | ASSERT_EQ(0, rc) << strerror(errno); |
| 117 | #else |
| 118 | GTEST_SKIP() << "epoll_pwait2_64 is bionic-only"; |
| 119 | #endif |
| 120 | } |
| 121 | |
Greg Hackmann | a03c62b | 2016-03-24 13:41:17 -0700 | [diff] [blame] | 122 | TEST(sys_epoll, epoll_create_invalid_size) { |
| 123 | errno = 0; |
| 124 | ASSERT_EQ(-1, epoll_create(0)); |
| 125 | ASSERT_EQ(EINVAL, errno); |
| 126 | } |
| 127 | |
Philip Hatcher | 9ded07c | 2014-02-25 14:16:37 +0100 | [diff] [blame] | 128 | TEST(sys_epoll, epoll_event_data) { |
| 129 | int epoll_fd = epoll_create(1); |
| 130 | ASSERT_NE(-1, epoll_fd) << strerror(errno); |
| 131 | |
| 132 | int fds[2]; |
| 133 | ASSERT_NE(-1, pipe(fds)); |
| 134 | |
| 135 | const uint64_t expected = 0x123456789abcdef0; |
| 136 | |
| 137 | // Get ready to poll on read end of pipe. |
| 138 | epoll_event ev; |
| 139 | ev.events = EPOLLIN; |
| 140 | ev.data.u64 = expected; |
| 141 | ASSERT_NE(-1, epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fds[0], &ev)); |
| 142 | |
| 143 | // Ensure there's something in the pipe. |
| 144 | ASSERT_EQ(1, write(fds[1], "\n", 1)); |
| 145 | |
| 146 | // Poll. |
| 147 | epoll_event events[1]; |
| 148 | ASSERT_EQ(1, epoll_wait(epoll_fd, events, 1, 1)); |
| 149 | ASSERT_EQ(expected, events[0].data.u64); |
| 150 | |
| 151 | close(fds[0]); |
| 152 | close(fds[1]); |
| 153 | } |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 154 | |
| 155 | TEST(sys_epoll, epoll_create1) { |
| 156 | int fd; |
| 157 | fd = epoll_create(1); |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 158 | ASSERT_FALSE(CloseOnExec(fd)); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 159 | close(fd); |
| 160 | |
| 161 | fd = epoll_create1(0); |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 162 | ASSERT_FALSE(CloseOnExec(fd)); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 163 | close(fd); |
| 164 | |
| 165 | fd = epoll_create1(EPOLL_CLOEXEC); |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 166 | ASSERT_TRUE(CloseOnExec(fd)); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 167 | close(fd); |
| 168 | } |