|
| 1 | +/* |
| 2 | + * lwan - web server |
| 3 | + * Copyright (c) 2012 L. A. F. Pereira <[email protected]> |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License |
| 7 | + * as published by the Free Software Foundation; either version 2 |
| 8 | + * of the License, or any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
| 18 | + * USA. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <errno.h> |
| 22 | +#include <fcntl.h> |
| 23 | +#include <limits.h> |
| 24 | +#include <stdint.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include <sys/epoll.h> |
| 27 | +#include <sys/types.h> |
| 28 | +#include <unistd.h> |
| 29 | + |
| 30 | +#include "lwan.h" |
| 31 | + |
| 32 | +#if !defined(LWAN_HAVE_EPOLL) && defined(LWAN_HAVE_KQUEUE) |
| 33 | +#include <sys/event.h> |
| 34 | +#include <sys/time.h> |
| 35 | +#include <sys/types.h> |
| 36 | + |
| 37 | +#include "hash.h" |
| 38 | + |
| 39 | +int epoll_create1(int flags) |
| 40 | +{ |
| 41 | +#if defined(LWAN_HAVE_KQUEUE1) |
| 42 | + return kqueue1(flags & EPOLL_CLOEXEC ? O_CLOEXEC : 0); |
| 43 | +#else |
| 44 | + int fd = kqueue(); |
| 45 | + |
| 46 | + if (flags & EPOLL_CLOEXEC) { |
| 47 | + int flags; |
| 48 | + |
| 49 | + flags = fcntl(fd, F_GETFD); |
| 50 | + if (flags < 0) |
| 51 | + return -1; |
| 52 | + |
| 53 | + if (fcntl(fd, F_SETFD, flags | O_CLOEXEC) < 0) |
| 54 | + return -1; |
| 55 | + } |
| 56 | + |
| 57 | + return fd; |
| 58 | +#endif |
| 59 | +} |
| 60 | + |
| 61 | +static int epoll_no_event_marker; |
| 62 | + |
| 63 | +int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) |
| 64 | +{ |
| 65 | + struct kevent ev; |
| 66 | + |
| 67 | + switch (op) { |
| 68 | + case EPOLL_CTL_ADD: |
| 69 | + case EPOLL_CTL_MOD: { |
| 70 | + int events = 0; |
| 71 | + void *udata = event->data.ptr; |
| 72 | + int flags = EV_ADD; |
| 73 | + |
| 74 | + if (event->events & EPOLLIN) { |
| 75 | + events = EVFILT_READ; |
| 76 | + } else if (event->events & EPOLLOUT) { |
| 77 | + events = EVFILT_WRITE; |
| 78 | + } else { |
| 79 | + events = EVFILT_WRITE; |
| 80 | + udata = &epoll_no_event_marker; |
| 81 | + } |
| 82 | + |
| 83 | + if (event->events & EPOLLONESHOT) |
| 84 | + flags |= EV_ONESHOT; |
| 85 | + if (event->events & EPOLLET) |
| 86 | + flags |= EV_CLEAR; |
| 87 | + |
| 88 | + flags |= EV_ERROR; /* EPOLLERR is always set. */ |
| 89 | + flags |= EV_EOF; /* EPOLLHUP is always set. */ |
| 90 | + |
| 91 | + EV_SET(&ev, fd, events, flags, 0, 0, udata); |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + case EPOLL_CTL_DEL: |
| 96 | + EV_SET(&ev, fd, 0, EV_DELETE, 0, 0, 0); |
| 97 | + break; |
| 98 | + |
| 99 | + default: |
| 100 | + errno = EINVAL; |
| 101 | + return -1; |
| 102 | + } |
| 103 | + |
| 104 | + return kevent(epfd, &ev, 1, NULL, 0, NULL); |
| 105 | +} |
| 106 | + |
| 107 | +static struct timespec *to_timespec(struct timespec *t, int ms) |
| 108 | +{ |
| 109 | + if (ms < 0) |
| 110 | + return NULL; |
| 111 | + |
| 112 | + t->tv_sec = ms / 1000; |
| 113 | + t->tv_nsec = (ms % 1000) * 1000000; |
| 114 | + |
| 115 | + return t; |
| 116 | +} |
| 117 | + |
| 118 | +int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) |
| 119 | +{ |
| 120 | + struct epoll_event *ev = events; |
| 121 | + struct kevent evs[maxevents]; |
| 122 | + struct timespec tmspec; |
| 123 | + struct hash *coalesce; |
| 124 | + int i, r; |
| 125 | + |
| 126 | + coalesce = hash_int_new(NULL, NULL); |
| 127 | + if (UNLIKELY(!coalesce)) |
| 128 | + return -1; |
| 129 | + |
| 130 | + r = kevent(epfd, NULL, 0, evs, maxevents, to_timespec(&tmspec, timeout)); |
| 131 | + if (UNLIKELY(r < 0)) { |
| 132 | + hash_free(coalesce); |
| 133 | + return -1; |
| 134 | + } |
| 135 | + |
| 136 | + for (i = 0; i < r; i++) { |
| 137 | + struct kevent *kev = &evs[i]; |
| 138 | + uint32_t mask = (uint32_t)(uintptr_t)hash_find( |
| 139 | + coalesce, (void *)(intptr_t)evs[i].ident); |
| 140 | + |
| 141 | + if (kev->flags & EV_ERROR) |
| 142 | + mask |= EPOLLERR; |
| 143 | + if (kev->flags & EV_EOF) |
| 144 | + mask |= EPOLLRDHUP; |
| 145 | + |
| 146 | + if (kev->filter == EVFILT_READ) |
| 147 | + mask |= EPOLLIN; |
| 148 | + else if (kev->filter == EVFILT_WRITE && evs[i].udata != &epoll_no_event_marker) |
| 149 | + mask |= EPOLLOUT; |
| 150 | + |
| 151 | + hash_add(coalesce, (void *)(intptr_t)evs[i].ident, |
| 152 | + (void *)(uintptr_t)mask); |
| 153 | + } |
| 154 | + |
| 155 | + for (i = 0; i < r; i++) { |
| 156 | + void *maskptr; |
| 157 | + |
| 158 | + maskptr = hash_find(coalesce, (void *)(intptr_t)evs[i].ident); |
| 159 | + if (maskptr) { |
| 160 | + struct kevent *kev = &evs[i]; |
| 161 | + |
| 162 | + if (kev->udata == &epoll_no_event_marker) |
| 163 | + continue; |
| 164 | + |
| 165 | + ev->data.ptr = kev->udata; |
| 166 | + ev->events = (uint32_t)(uintptr_t)maskptr; |
| 167 | + ev++; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + hash_free(coalesce); |
| 172 | + return (int)(intptr_t)(ev - events); |
| 173 | +} |
| 174 | +#elif !defined(LWAN_HAVE_EPOLL) |
| 175 | +#error epoll() not implemented for this platform |
| 176 | +#endif |
| 177 | + |
0 commit comments