111952073af22568bba0b661f7a9d4402c443a888Elliott Hughes/*
211952073af22568bba0b661f7a9d4402c443a888Elliott Hughes * Copyright (C) 2013 The Android Open Source Project
311952073af22568bba0b661f7a9d4402c443a888Elliott Hughes *
411952073af22568bba0b661f7a9d4402c443a888Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
511952073af22568bba0b661f7a9d4402c443a888Elliott Hughes * you may not use this file except in compliance with the License.
611952073af22568bba0b661f7a9d4402c443a888Elliott Hughes * You may obtain a copy of the License at
711952073af22568bba0b661f7a9d4402c443a888Elliott Hughes *
811952073af22568bba0b661f7a9d4402c443a888Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
911952073af22568bba0b661f7a9d4402c443a888Elliott Hughes *
1011952073af22568bba0b661f7a9d4402c443a888Elliott Hughes * Unless required by applicable law or agreed to in writing, software
1111952073af22568bba0b661f7a9d4402c443a888Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
1211952073af22568bba0b661f7a9d4402c443a888Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1311952073af22568bba0b661f7a9d4402c443a888Elliott Hughes * See the License for the specific language governing permissions and
1411952073af22568bba0b661f7a9d4402c443a888Elliott Hughes * limitations under the License.
1511952073af22568bba0b661f7a9d4402c443a888Elliott Hughes */
1611952073af22568bba0b661f7a9d4402c443a888Elliott Hughes
1711952073af22568bba0b661f7a9d4402c443a888Elliott Hughes#include <gtest/gtest.h>
1811952073af22568bba0b661f7a9d4402c443a888Elliott Hughes
1911952073af22568bba0b661f7a9d4402c443a888Elliott Hughes#include <errno.h>
209ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher#include <fcntl.h>
21f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#include <signal.h>
2211952073af22568bba0b661f7a9d4402c443a888Elliott Hughes#include <sys/epoll.h>
239ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher#include <unistd.h>
2411952073af22568bba0b661f7a9d4402c443a888Elliott Hughes
2511952073af22568bba0b661f7a9d4402c443a888Elliott HughesTEST(sys_epoll, smoke) {
2611952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  int epoll_fd = epoll_create(1);
2711952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  ASSERT_NE(-1, epoll_fd) << strerror(errno);
2811952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  epoll_event events[1];
2911952073af22568bba0b661f7a9d4402c443a888Elliott Hughes
3011952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  // Regular epoll_wait.
3111952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  ASSERT_EQ(0, epoll_wait(epoll_fd, events, 1, 1));
3211952073af22568bba0b661f7a9d4402c443a888Elliott Hughes
3311952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  // epoll_pwait without a sigset (which is equivalent to epoll_wait).
3411952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, NULL));
3511952073af22568bba0b661f7a9d4402c443a888Elliott Hughes
3611952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  // epoll_pwait with a sigset.
3711952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  sigset_t ss;
3811952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  sigemptyset(&ss);
3911952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  sigaddset(&ss, SIGPIPE);
4011952073af22568bba0b661f7a9d4402c443a888Elliott Hughes  ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, &ss));
4111952073af22568bba0b661f7a9d4402c443a888Elliott Hughes}
429ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher
439ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip HatcherTEST(sys_epoll, epoll_event_data) {
449ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  int epoll_fd = epoll_create(1);
459ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  ASSERT_NE(-1, epoll_fd) << strerror(errno);
469ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher
479ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  int fds[2];
489ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  ASSERT_NE(-1, pipe(fds));
499ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher
509ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  const uint64_t expected = 0x123456789abcdef0;
519ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher
529ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  // Get ready to poll on read end of pipe.
539ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  epoll_event ev;
549ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  ev.events = EPOLLIN;
559ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  ev.data.u64 = expected;
569ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  ASSERT_NE(-1, epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fds[0], &ev));
579ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher
589ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  // Ensure there's something in the pipe.
599ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  ASSERT_EQ(1, write(fds[1], "\n", 1));
609ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher
619ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  // Poll.
629ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  epoll_event events[1];
639ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  ASSERT_EQ(1, epoll_wait(epoll_fd, events, 1, 1));
649ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  ASSERT_EQ(expected, events[0].data.u64);
659ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher
669ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  close(fds[0]);
679ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher  close(fds[1]);
689ded07cff6c73bd3ea1bbc874180139d3a5d6f0cPhilip Hatcher}
69