blob: 744f1c90b992941e52e3ab6c314b5279a3be22e1 [file] [log] [blame]
Elliott Hughes11952072013-10-24 15:15:14 -07001/*
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 Hatcher9ded07c2014-02-25 14:16:37 +010020#include <fcntl.h>
Christopher Ferrisf04935c2013-12-20 18:43:21 -080021#include <signal.h>
Elliott Hughes11952072013-10-24 15:15:14 -070022#include <sys/epoll.h>
Philip Hatcher9ded07c2014-02-25 14:16:37 +010023#include <unistd.h>
Elliott Hughes11952072013-10-24 15:15:14 -070024
Elliott Hughesa7f12942017-12-15 13:55:53 -080025#include "utils.h"
26
Elliott Hughes4c62e592023-07-13 15:45:33 -070027TEST(sys_epoll, epoll_wait) {
Elliott Hughes11952072013-10-24 15:15:14 -070028 int epoll_fd = epoll_create(1);
Elliott Hughes4c62e592023-07-13 15:45:33 -070029 ASSERT_NE(-1, epoll_fd);
Elliott Hughes11952072013-10-24 15:15:14 -070030
31 // Regular epoll_wait.
Elliott Hughes4c62e592023-07-13 15:45:33 -070032 epoll_event events[1] = {};
Elliott Hughes11952072013-10-24 15:15:14 -070033 ASSERT_EQ(0, epoll_wait(epoll_fd, events, 1, 1));
Elliott Hughes4c62e592023-07-13 15:45:33 -070034}
35
36TEST(sys_epoll, epoll_pwait_no_sigset) {
37 int epoll_fd = epoll_create(1);
38 ASSERT_NE(-1, epoll_fd);
Elliott Hughes11952072013-10-24 15:15:14 -070039
40 // epoll_pwait without a sigset (which is equivalent to epoll_wait).
Elliott Hughes4c62e592023-07-13 15:45:33 -070041 epoll_event events[1] = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -070042 ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, nullptr));
Elliott Hughes4c62e592023-07-13 15:45:33 -070043}
Elliott Hughes11952072013-10-24 15:15:14 -070044
Elliott Hughes4c62e592023-07-13 15:45:33 -070045TEST(sys_epoll, epoll_pwait64_no_sigset) {
Elliott Hughes5905d6f2018-01-30 15:09:51 -080046#if defined(__BIONIC__)
Elliott Hughes4c62e592023-07-13 15:45:33 -070047 int epoll_fd = epoll_create(1);
48 ASSERT_NE(-1, epoll_fd);
49
Elliott Hughes5905d6f2018-01-30 15:09:51 -080050 // epoll_pwait64 without a sigset (which is equivalent to epoll_wait).
Elliott Hughes4c62e592023-07-13 15:45:33 -070051 epoll_event events[1] = {};
Yi Kong32bc0fc2018-08-02 17:31:13 -070052 ASSERT_EQ(0, epoll_pwait64(epoll_fd, events, 1, 1, nullptr));
Elliott Hughes4c62e592023-07-13 15:45:33 -070053#else
54 GTEST_SKIP() << "epoll_pwait64 is bionic-only";
Elliott Hughes5905d6f2018-01-30 15:09:51 -080055#endif
Elliott Hughes4c62e592023-07-13 15:45:33 -070056}
57
58TEST(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);
Elliott Hughes4ae4be92023-09-22 17:15:25 -070067 if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no epoll_pwait2() in this kernel";
Elliott Hughes4c62e592023-07-13 15:45:33 -070068 ASSERT_EQ(0, rc) << strerror(errno);
69#else
70 GTEST_SKIP() << "epoll_pwait2 is only in glibc 2.35+";
71#endif
72}
73
74TEST(sys_epoll, epoll_pwait_with_sigset) {
75 int epoll_fd = epoll_create(1);
76 ASSERT_NE(-1, epoll_fd);
Elliott Hughes5905d6f2018-01-30 15:09:51 -080077
Elliott Hughes11952072013-10-24 15:15:14 -070078 // epoll_pwait with a sigset.
Elliott Hughes4c62e592023-07-13 15:45:33 -070079 epoll_event events[1] = {};
Elliott Hughes11952072013-10-24 15:15:14 -070080 sigset_t ss;
81 sigemptyset(&ss);
82 sigaddset(&ss, SIGPIPE);
83 ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, &ss));
84}
Philip Hatcher9ded07c2014-02-25 14:16:37 +010085
Elliott Hughes4c62e592023-07-13 15:45:33 -070086TEST(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);
Elliott Hughes4ae4be92023-09-22 17:15:25 -070097 if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no epoll_pwait2() in this kernel";
Elliott Hughes4c62e592023-07-13 15:45:33 -070098 ASSERT_EQ(0, rc) << strerror(errno);
99#else
100 GTEST_SKIP() << "epoll_pwait2 is only in glibc 2.35+";
101#endif
102}
103
104TEST(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);
Elliott Hughes4ae4be92023-09-22 17:15:25 -0700115 if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no epoll_pwait2() in this kernel";
Elliott Hughes4c62e592023-07-13 15:45:33 -0700116 ASSERT_EQ(0, rc) << strerror(errno);
117#else
118 GTEST_SKIP() << "epoll_pwait2_64 is bionic-only";
119#endif
120}
121
Greg Hackmanna03c62b2016-03-24 13:41:17 -0700122TEST(sys_epoll, epoll_create_invalid_size) {
123 errno = 0;
124 ASSERT_EQ(-1, epoll_create(0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700125 ASSERT_ERRNO(EINVAL);
Greg Hackmanna03c62b2016-03-24 13:41:17 -0700126}
127
Philip Hatcher9ded07c2014-02-25 14:16:37 +0100128TEST(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 Hughesa7f12942017-12-15 13:55:53 -0800154
155TEST(sys_epoll, epoll_create1) {
156 int fd;
157 fd = epoll_create(1);
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800158 ASSERT_FALSE(CloseOnExec(fd));
Elliott Hughesa7f12942017-12-15 13:55:53 -0800159 close(fd);
160
161 fd = epoll_create1(0);
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800162 ASSERT_FALSE(CloseOnExec(fd));
Elliott Hughesa7f12942017-12-15 13:55:53 -0800163 close(fd);
164
165 fd = epoll_create1(EPOLL_CLOEXEC);
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800166 ASSERT_TRUE(CloseOnExec(fd));
Elliott Hughesa7f12942017-12-15 13:55:53 -0800167 close(fd);
168}