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 | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 27 | TEST(sys_epoll, smoke) { |
| 28 | int epoll_fd = epoll_create(1); |
| 29 | ASSERT_NE(-1, epoll_fd) << strerror(errno); |
| 30 | epoll_event events[1]; |
| 31 | |
| 32 | // Regular epoll_wait. |
| 33 | ASSERT_EQ(0, epoll_wait(epoll_fd, events, 1, 1)); |
| 34 | |
| 35 | // epoll_pwait without a sigset (which is equivalent to epoll_wait). |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 36 | ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, nullptr)); |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 38 | #if defined(__BIONIC__) |
| 39 | // epoll_pwait64 without a sigset (which is equivalent to epoll_wait). |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 40 | ASSERT_EQ(0, epoll_pwait64(epoll_fd, events, 1, 1, nullptr)); |
Elliott Hughes | 5905d6f | 2018-01-30 15:09:51 -0800 | [diff] [blame] | 41 | #endif |
| 42 | |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 43 | // epoll_pwait with a sigset. |
| 44 | sigset_t ss; |
| 45 | sigemptyset(&ss); |
| 46 | sigaddset(&ss, SIGPIPE); |
| 47 | ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, &ss)); |
| 48 | } |
Philip Hatcher | 9ded07c | 2014-02-25 14:16:37 +0100 | [diff] [blame] | 49 | |
Greg Hackmann | a03c62b | 2016-03-24 13:41:17 -0700 | [diff] [blame] | 50 | TEST(sys_epoll, epoll_create_invalid_size) { |
| 51 | errno = 0; |
| 52 | ASSERT_EQ(-1, epoll_create(0)); |
| 53 | ASSERT_EQ(EINVAL, errno); |
| 54 | } |
| 55 | |
Philip Hatcher | 9ded07c | 2014-02-25 14:16:37 +0100 | [diff] [blame] | 56 | TEST(sys_epoll, epoll_event_data) { |
| 57 | int epoll_fd = epoll_create(1); |
| 58 | ASSERT_NE(-1, epoll_fd) << strerror(errno); |
| 59 | |
| 60 | int fds[2]; |
| 61 | ASSERT_NE(-1, pipe(fds)); |
| 62 | |
| 63 | const uint64_t expected = 0x123456789abcdef0; |
| 64 | |
| 65 | // Get ready to poll on read end of pipe. |
| 66 | epoll_event ev; |
| 67 | ev.events = EPOLLIN; |
| 68 | ev.data.u64 = expected; |
| 69 | ASSERT_NE(-1, epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fds[0], &ev)); |
| 70 | |
| 71 | // Ensure there's something in the pipe. |
| 72 | ASSERT_EQ(1, write(fds[1], "\n", 1)); |
| 73 | |
| 74 | // Poll. |
| 75 | epoll_event events[1]; |
| 76 | ASSERT_EQ(1, epoll_wait(epoll_fd, events, 1, 1)); |
| 77 | ASSERT_EQ(expected, events[0].data.u64); |
| 78 | |
| 79 | close(fds[0]); |
| 80 | close(fds[1]); |
| 81 | } |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 82 | |
| 83 | TEST(sys_epoll, epoll_create1) { |
| 84 | int fd; |
| 85 | fd = epoll_create(1); |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame^] | 86 | ASSERT_FALSE(CloseOnExec(fd)); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 87 | close(fd); |
| 88 | |
| 89 | fd = epoll_create1(0); |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame^] | 90 | ASSERT_FALSE(CloseOnExec(fd)); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 91 | close(fd); |
| 92 | |
| 93 | fd = epoll_create1(EPOLL_CLOEXEC); |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame^] | 94 | ASSERT_TRUE(CloseOnExec(fd)); |
Elliott Hughes | a7f1294 | 2017-12-15 13:55:53 -0800 | [diff] [blame] | 95 | close(fd); |
| 96 | } |