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>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <sys/epoll.h>
23 #include <unistd.h>
24 
25 #include "utils.h"
26 
TEST(sys_epoll,epoll_wait)27 TEST(sys_epoll, epoll_wait) {
28   int epoll_fd = epoll_create(1);
29   ASSERT_NE(-1, epoll_fd);
30 
31   // Regular epoll_wait.
32   epoll_event events[1] = {};
33   ASSERT_EQ(0, epoll_wait(epoll_fd, events, 1, 1));
34 }
35 
TEST(sys_epoll,epoll_pwait_no_sigset)36 TEST(sys_epoll, epoll_pwait_no_sigset) {
37   int epoll_fd = epoll_create(1);
38   ASSERT_NE(-1, epoll_fd);
39 
40   // epoll_pwait without a sigset (which is equivalent to epoll_wait).
41   epoll_event events[1] = {};
42   ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, nullptr));
43 }
44 
TEST(sys_epoll,epoll_pwait64_no_sigset)45 TEST(sys_epoll, epoll_pwait64_no_sigset) {
46 #if defined(__BIONIC__)
47   int epoll_fd = epoll_create(1);
48   ASSERT_NE(-1, epoll_fd);
49 
50   // epoll_pwait64 without a sigset (which is equivalent to epoll_wait).
51   epoll_event events[1] = {};
52   ASSERT_EQ(0, epoll_pwait64(epoll_fd, events, 1, 1, nullptr));
53 #else
54   GTEST_SKIP() << "epoll_pwait64 is bionic-only";
55 #endif
56 }
57 
TEST(sys_epoll,epoll_pwait2_no_sigset)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 
TEST(sys_epoll,epoll_pwait_with_sigset)74 TEST(sys_epoll, epoll_pwait_with_sigset) {
75   int epoll_fd = epoll_create(1);
76   ASSERT_NE(-1, epoll_fd);
77 
78   // epoll_pwait with a sigset.
79   epoll_event events[1] = {};
80   sigset_t ss;
81   sigemptyset(&ss);
82   sigaddset(&ss, SIGPIPE);
83   ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, &ss));
84 }
85 
TEST(sys_epoll,epoll_pwait2_with_sigset)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 
TEST(sys_epoll,epoll_pwait2_64_with_sigset)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 
TEST(sys_epoll,epoll_create_invalid_size)122 TEST(sys_epoll, epoll_create_invalid_size) {
123   errno = 0;
124   ASSERT_EQ(-1, epoll_create(0));
125   ASSERT_ERRNO(EINVAL);
126 }
127 
TEST(sys_epoll,epoll_event_data)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 }
154 
TEST(sys_epoll,epoll_create1)155 TEST(sys_epoll, epoll_create1) {
156   int fd;
157   fd = epoll_create(1);
158   ASSERT_FALSE(CloseOnExec(fd));
159   close(fd);
160 
161   fd = epoll_create1(0);
162   ASSERT_FALSE(CloseOnExec(fd));
163   close(fd);
164 
165   fd = epoll_create1(EPOLL_CLOEXEC);
166   ASSERT_TRUE(CloseOnExec(fd));
167   close(fd);
168 }
169