1 /* 2 * Copyright 2000-2009 Niels Provos <[email protected]> 3 * Copyright 2009-2010 Niels Provos and Nick Mathewson 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #include "event2/event-config.h" 28 29 #include <sys/types.h> 30 #include <sys/resource.h> 31 #ifdef _EVENT_HAVE_SYS_TIME_H 32 #include <sys/time.h> 33 #endif 34 #include <sys/queue.h> 35 #include <sys/devpoll.h> 36 #include <signal.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include <fcntl.h> 42 #include <errno.h> 43 44 #include "event2/event.h" 45 #include "event2/event_struct.h" 46 #include "event2/thread.h" 47 #include "event-internal.h" 48 #include "evsignal-internal.h" 49 #include "log-internal.h" 50 #include "evmap-internal.h" 51 #include "evthread-internal.h" 52 53 struct devpollop { 54 struct pollfd *events; 55 int nevents; 56 int dpfd; 57 struct pollfd *changes; 58 int nchanges; 59 }; 60 61 static void *devpoll_init(struct event_base *); 62 static int devpoll_add(struct event_base *, int fd, short old, short events, void *); 63 static int devpoll_del(struct event_base *, int fd, short old, short events, void *); 64 static int devpoll_dispatch(struct event_base *, struct timeval *); 65 static void devpoll_dealloc(struct event_base *); 66 67 const struct eventop devpollops = { 68 "devpoll", 69 devpoll_init, 70 devpoll_add, 71 devpoll_del, 72 devpoll_dispatch, 73 devpoll_dealloc, 74 1, /* need reinit */ 75 EV_FEATURE_FDS|EV_FEATURE_O1, 76 0 77 }; 78 79 #define NEVENT 32000 80 81 static int 82 devpoll_commit(struct devpollop *devpollop) 83 { 84 /* 85 * Due to a bug in Solaris, we have to use pwrite with an offset of 0. 86 * Write is limited to 2GB of data, until it will fail. 87 */ 88 if (pwrite(devpollop->dpfd, devpollop->changes, 89 sizeof(struct pollfd) * devpollop->nchanges, 0) == -1) 90 return (-1); 91 92 devpollop->nchanges = 0; 93 return (0); 94 } 95 96 static int 97 devpoll_queue(struct devpollop *devpollop, int fd, int events) { 98 struct pollfd *pfd; 99 100 if (devpollop->nchanges >= devpollop->nevents) { 101 /* 102 * Change buffer is full, must commit it to /dev/poll before 103 * adding more 104 */ 105 if (devpoll_commit(devpollop) != 0) 106 return (-1); 107 } 108 109 pfd = &devpollop->changes[devpollop->nchanges++]; 110 pfd->fd = fd; 111 pfd->events = events; 112 pfd->revents = 0; 113 114 return (0); 115 } 116 117 static void * 118 devpoll_init(struct event_base *base) 119 { 120 int dpfd, nfiles = NEVENT; 121 struct rlimit rl; 122 struct devpollop *devpollop; 123 124 if (!(devpollop = mm_calloc(1, sizeof(struct devpollop)))) 125 return (NULL); 126 127 if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && 128 rl.rlim_cur != RLIM_INFINITY) 129 nfiles = rl.rlim_cur; 130 131 /* Initialize the kernel queue */ 132 if ((dpfd = open("/dev/poll", O_RDWR)) == -1) { 133 event_warn("open: /dev/poll"); 134 mm_free(devpollop); 135 return (NULL); 136 } 137 138 devpollop->dpfd = dpfd; 139 140 /* Initialize fields */ 141 /* FIXME: allocating 'nfiles' worth of space here can be 142 * expensive and unnecessary. See how epoll.c does it instead. */ 143 devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd)); 144 if (devpollop->events == NULL) { 145 mm_free(devpollop); 146 close(dpfd); 147 return (NULL); 148 } 149 devpollop->nevents = nfiles; 150 151 devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd)); 152 if (devpollop->changes == NULL) { 153 mm_free(devpollop->events); 154 mm_free(devpollop); 155 close(dpfd); 156 return (NULL); 157 } 158 159 evsig_init(base); 160 161 return (devpollop); 162 } 163 164 static int 165 devpoll_dispatch(struct event_base *base, struct timeval *tv) 166 { 167 struct devpollop *devpollop = base->evbase; 168 struct pollfd *events = devpollop->events; 169 struct dvpoll dvp; 170 int i, res, timeout = -1; 171 172 if (devpollop->nchanges) 173 devpoll_commit(devpollop); 174 175 if (tv != NULL) 176 timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; 177 178 dvp.dp_fds = devpollop->events; 179 dvp.dp_nfds = devpollop->nevents; 180 dvp.dp_timeout = timeout; 181 182 EVBASE_RELEASE_LOCK(base, th_base_lock); 183 184 res = ioctl(devpollop->dpfd, DP_POLL, &dvp); 185 186 EVBASE_ACQUIRE_LOCK(base, th_base_lock); 187 188 if (res == -1) { 189 if (errno != EINTR) { 190 event_warn("ioctl: DP_POLL"); 191 return (-1); 192 } 193 194 evsig_process(base); 195 return (0); 196 } else if (base->sig.evsig_caught) { 197 evsig_process(base); 198 } 199 200 event_debug(("%s: devpoll_wait reports %d", __func__, res)); 201 202 for (i = 0; i < res; i++) { 203 int which = 0; 204 int what = events[i].revents; 205 206 if (what & POLLHUP) 207 what |= POLLIN | POLLOUT; 208 else if (what & POLLERR) 209 what |= POLLIN | POLLOUT; 210 211 if (what & POLLIN) 212 which |= EV_READ; 213 if (what & POLLOUT) 214 which |= EV_WRITE; 215 216 if (!which) 217 continue; 218 219 /* XXX(niels): not sure if this works for devpoll */ 220 evmap_io_active(base, events[i].fd, which); 221 } 222 223 return (0); 224 } 225 226 227 static int 228 devpoll_add(struct event_base *base, int fd, short old, short events, void *p) 229 { 230 struct devpollop *devpollop = base->evbase; 231 int res; 232 (void)p; 233 234 /* 235 * It's not necessary to OR the existing read/write events that we 236 * are currently interested in with the new event we are adding. 237 * The /dev/poll driver ORs any new events with the existing events 238 * that it has cached for the fd. 239 */ 240 241 res = 0; 242 if (events & EV_READ) 243 res |= POLLIN; 244 if (events & EV_WRITE) 245 res |= POLLOUT; 246 247 if (devpoll_queue(devpollop, fd, res) != 0) 248 return (-1); 249 250 return (0); 251 } 252 253 static int 254 devpoll_del(struct event_base *base, int fd, short old, short events, void *p) 255 { 256 struct devpollop *devpollop = base->evbase; 257 int res; 258 (void)p; 259 260 res = 0; 261 if (events & EV_READ) 262 res |= POLLIN; 263 if (events & EV_WRITE) 264 res |= POLLOUT; 265 266 /* 267 * The only way to remove an fd from the /dev/poll monitored set is 268 * to use POLLREMOVE by itself. This removes ALL events for the fd 269 * provided so if we care about two events and are only removing one 270 * we must re-add the other event after POLLREMOVE. 271 */ 272 273 if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0) 274 return (-1); 275 276 if ((res & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) { 277 /* 278 * We're not deleting all events, so we must resubmit the 279 * event that we are still interested in if one exists. 280 */ 281 282 if ((res & POLLIN) && (old & EV_WRITE)) { 283 /* Deleting read, still care about write */ 284 devpoll_queue(devpollop, fd, POLLOUT); 285 } else if ((res & POLLOUT) && (old & EV_READ)) { 286 /* Deleting write, still care about read */ 287 devpoll_queue(devpollop, fd, POLLIN); 288 } 289 } 290 291 return (0); 292 } 293 294 static void 295 devpoll_dealloc(struct event_base *base) 296 { 297 struct devpollop *devpollop = base->evbase; 298 299 evsig_dealloc(base); 300 if (devpollop->events) 301 mm_free(devpollop->events); 302 if (devpollop->changes) 303 mm_free(devpollop->changes); 304 if (devpollop->dpfd >= 0) 305 close(devpollop->dpfd); 306 307 memset(devpollop, 0, sizeof(struct devpollop)); 308 mm_free(devpollop); 309 } 310