1 /* 2 * Copyright 2018, 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 "poller.h" 18 19 #include "log.h" 20 21 #include <errno.h> 22 #include <poll.h> 23 #include <signal.h> 24 #include <stdio.h> 25 #include <string.h> 26 27 #include <unordered_map> 28 #include <vector> 29 30 using std::chrono::duration_cast; 31 32 static struct timespec* calculateTimeout(Pollable::Timestamp deadline, 33 struct timespec* ts) { 34 Pollable::Timestamp now = Pollable::Clock::now(); 35 if (deadline < Pollable::Timestamp::max()) { 36 if (deadline <= now) { 37 LOGE("Poller found past due deadline, setting to zero"); 38 ts->tv_sec = 0; 39 ts->tv_nsec = 0; 40 return ts; 41 } 42 43 auto timeout = deadline - now; 44 // Convert and round down to seconds 45 auto seconds = duration_cast<std::chrono::seconds>(timeout); 46 // Then subtract the seconds from the timeout and convert the remainder 47 auto nanos = duration_cast<std::chrono::nanoseconds>(timeout - seconds); 48 49 ts->tv_sec = seconds.count(); 50 ts->tv_nsec = nanos.count(); 51 52 return ts; 53 } 54 return nullptr; 55 } 56 57 Poller::Poller() { 58 } 59 60 void Poller::addPollable(Pollable* pollable) { 61 mPollables.push_back(pollable); 62 } 63 64 int Poller::run() { 65 // Block all signals while we're running. This way we don't have to deal 66 // with things like EINTR. We then uses ppoll to set the original mask while 67 // polling. This way polling can be interrupted but socket writing, reading 68 // and ioctl remain interrupt free. If a signal arrives while we're blocking 69 // it it will be placed in the signal queue and handled once ppoll sets the 70 // original mask. This way no signals are lost. 71 sigset_t blockMask, mask; 72 int status = ::sigfillset(&blockMask); 73 if (status != 0) { 74 LOGE("Unable to fill signal set: %s", strerror(errno)); 75 return errno; 76 } 77 status = ::sigprocmask(SIG_SETMASK, &blockMask, &mask); 78 if (status != 0) { 79 LOGE("Unable to set signal mask: %s", strerror(errno)); 80 return errno; 81 } 82 83 std::vector<struct pollfd> fds; 84 std::unordered_map<int, Pollable*> pollables; 85 while (true) { 86 fds.clear(); 87 pollables.clear(); 88 Pollable::Timestamp deadline = Pollable::Timestamp::max(); 89 for (auto& pollable : mPollables) { 90 size_t start = fds.size(); 91 pollable->getPollData(&fds); 92 Pollable::Timestamp pollableDeadline = pollable->getTimeout(); 93 // Create a map from each fd to the pollable 94 for (size_t i = start; i < fds.size(); ++i) { 95 pollables[fds[i].fd] = pollable; 96 } 97 if (pollableDeadline < deadline) { 98 deadline = pollableDeadline; 99 } 100 } 101 102 struct timespec ts = { 0, 0 }; 103 struct timespec* tsPtr = calculateTimeout(deadline, &ts); 104 status = ::ppoll(fds.data(), fds.size(), tsPtr, &mask); 105 if (status < 0) { 106 if (errno == EINTR) { 107 // Interrupted, just keep going 108 continue; 109 } 110 // Actual error, time to quit 111 LOGE("Polling failed: %s", strerror(errno)); 112 return errno; 113 } else if (status > 0) { 114 // Check for read or close events 115 for (const auto& fd : fds) { 116 if ((fd.revents & (POLLIN | POLLHUP)) == 0) { 117 // Neither POLLIN nor POLLHUP, not interested 118 continue; 119 } 120 auto pollable = pollables.find(fd.fd); 121 if (pollable == pollables.end()) { 122 // No matching fd, weird and unexpected 123 LOGE("Poller could not find fd matching %d", fd.fd); 124 continue; 125 } 126 if (fd.revents & POLLIN) { 127 // This pollable has data available for reading 128 int status = 0; 129 if (!pollable->second->onReadAvailable(fd.fd, &status)) { 130 // The onReadAvailable handler signaled an exit 131 return status; 132 } 133 } 134 if (fd.revents & POLLHUP) { 135 // The fd was closed from the other end 136 int status = 0; 137 if (!pollable->second->onClose(fd.fd, &status)) { 138 // The onClose handler signaled an exit 139 return status; 140 } 141 } 142 } 143 } 144 // Check for timeouts 145 Pollable::Timestamp now = Pollable::Clock::now(); 146 for (const auto& pollable : mPollables) { 147 if (pollable->getTimeout() <= now) { 148 int status = 0; 149 if (!pollable->onTimeout(&status)) { 150 // The onTimeout handler signaled an exit 151 return status; 152 } 153 } 154 } 155 } 156 157 return 0; 158 } 159