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 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). 36 ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, NULL)); 37 38 #if defined(__BIONIC__) 39 // epoll_pwait64 without a sigset (which is equivalent to epoll_wait). 40 ASSERT_EQ(0, epoll_pwait64(epoll_fd, events, 1, 1, NULL)); 41 #endif 42 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 } 49 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 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 } 82 83 TEST(sys_epoll, epoll_create1) { 84 int fd; 85 fd = epoll_create(1); 86 AssertCloseOnExec(fd, false); 87 close(fd); 88 89 fd = epoll_create1(0); 90 AssertCloseOnExec(fd, false); 91 close(fd); 92 93 fd = epoll_create1(EPOLL_CLOEXEC); 94 AssertCloseOnExec(fd, true); 95 close(fd); 96 } 97