1 /* 2 * Copyright 2000-2004 Niels Provos <[email protected]> 3 * All rights reserved. 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 #ifdef HAVE_CONFIG_H 28 #include "config.h" 29 #endif 30 31 #include <sys/types.h> 32 #include <sys/resource.h> 33 #ifdef HAVE_SYS_TIME_H 34 #include <sys/time.h> 35 #else 36 #include <sys/_time.h> 37 #endif 38 #include <sys/queue.h> 39 #include <sys/devpoll.h> 40 #include <signal.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <fcntl.h> 46 #include <errno.h> 47 #include <assert.h> 48 49 #include "event2/event.h" 50 #include "event2/event_struct.h" 51 #include "event-internal.h" 52 #include "evsignal-internal.h" 53 #include "log-internal.h" 54 #include "evmap-internal.h" 55 56 struct devpollop { 57 struct pollfd *events; 58 int nevents; 59 int dpfd; 60 struct pollfd *changes; 61 int nchanges; 62 }; 63 64 static void *devpoll_init (struct event_base *); 65 static int devpoll_add(struct event_base *, int fd, short old, short events, void *); 66 static int devpoll_del(struct event_base *, int fd, short old, short events, void *); 67 static int devpoll_dispatch (struct event_base *, struct timeval *); 68 static void devpoll_dealloc (struct event_base *); 69 70 const struct eventop devpollops = { 71 "devpoll", 72 devpoll_init, 73 devpoll_add, 74 devpoll_del, 75 devpoll_dispatch, 76 devpoll_dealloc, 77 1, /* need reinit */ 78 EV_FEATURE_FDS|EV_FEATURE_O1, 79 0 80 }; 81 82 #define NEVENT 32000 83 84 static int 85 devpoll_commit(struct devpollop *devpollop) 86 { 87 /* 88 * Due to a bug in Solaris, we have to use pwrite with an offset of 0. 89 * Write is limited to 2GB of data, until it will fail. 90 */ 91 if (pwrite(devpollop->dpfd, devpollop->changes, 92 sizeof(struct pollfd) * devpollop->nchanges, 0) == -1) 93 return(-1); 94 95 devpollop->nchanges = 0; 96 return(0); 97 } 98 99 static int 100 devpoll_queue(struct devpollop *devpollop, int fd, int events) { 101 struct pollfd *pfd; 102 103 if (devpollop->nchanges >= devpollop->nevents) { 104 /* 105 * Change buffer is full, must commit it to /dev/poll before 106 * adding more 107 */ 108 if (devpoll_commit(devpollop) != 0) 109 return(-1); 110 } 111 112 pfd = &devpollop->changes[devpollop->nchanges++]; 113 pfd->fd = fd; 114 pfd->events = events; 115 pfd->revents = 0; 116 117 return(0); 118 } 119 120 static void * 121 devpoll_init(struct event_base *base) 122 { 123 int dpfd, nfiles = NEVENT; 124 struct rlimit rl; 125 struct devpollop *devpollop; 126 127 if (!(devpollop = mm_calloc(1, sizeof(struct devpollop)))) 128 return (NULL); 129 130 if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && 131 rl.rlim_cur != RLIM_INFINITY) 132 nfiles = rl.rlim_cur; 133 134 /* Initialize the kernel queue */ 135 if ((dpfd = open("/dev/poll", O_RDWR)) == -1) { 136 event_warn("open: /dev/poll"); 137 mm_free(devpollop); 138 return (NULL); 139 } 140 141 devpollop->dpfd = dpfd; 142 143 /* Initialize fields */ 144 devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd)); 145 if (devpollop->events == NULL) { 146 mm_free(devpollop); 147 close(dpfd); 148 return (NULL); 149 } 150 devpollop->nevents = nfiles; 151 152 devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd)); 153 if (devpollop->changes == NULL) { 154 mm_free(devpollop->events); 155 mm_free(devpollop); 156 close(dpfd); 157 return (NULL); 158 } 159 160 evsig_init(base); 161 162 return (devpollop); 163 } 164 165 static int 166 devpoll_dispatch(struct event_base *base, struct timeval *tv) 167 { 168 struct devpollop *devpollop = base->evbase; 169 struct pollfd *events = devpollop->events; 170 struct dvpoll dvp; 171 struct evdevpoll *evdp; 172 int i, res, timeout = -1; 173 174 if (devpollop->nchanges) 175 devpoll_commit(devpollop); 176 177 if (tv != NULL) 178 timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; 179 180 dvp.dp_fds = devpollop->events; 181 dvp.dp_nfds = devpollop->nevents; 182 dvp.dp_timeout = timeout; 183 184 res = ioctl(devpollop->dpfd, DP_POLL, &dvp); 185 186 if (res == -1) { 187 if (errno != EINTR) { 188 event_warn("ioctl: DP_POLL"); 189 return (-1); 190 } 191 192 evsig_process(base); 193 return (0); 194 } else if (base->sig.evsig_caught) { 195 evsig_process(base); 196 } 197 198 event_debug(("%s: devpoll_wait reports %d", __func__, res)); 199 200 for (i = 0; i < res; i++) { 201 int which = 0; 202 int what = events[i].revents; 203 struct event *evread = NULL, *evwrite = NULL; 204 205 if (what & POLLHUP) 206 what |= POLLIN | POLLOUT; 207 else if (what & POLLERR) 208 what |= POLLIN | POLLOUT; 209 210 if (what & POLLIN) { 211 evread = evdp->evread; 212 which |= EV_READ; 213 } 214 215 if (what & POLLOUT) { 216 evwrite = evdp->evwrite; 217 which |= EV_WRITE; 218 } 219 220 if (!which) 221 continue; 222 223 /* XXX(niels): not sure if this works for devpoll */ 224 evmap_io_active(base, events[i].fd, which); 225 } 226 227 return (0); 228 } 229 230 231 static int 232 devpoll_add(struct event_base *base, int fd, short old, short events, void *p) 233 { 234 struct devpollop *devpollop = base->evbase; 235 struct evdevpoll *evdp; 236 int events; 237 (void)p; 238 239 /* 240 * It's not necessary to OR the existing read/write events that we 241 * are currently interested in with the new event we are adding. 242 * The /dev/poll driver ORs any new events with the existing events 243 * that it has cached for the fd. 244 */ 245 246 events = 0; 247 if (events & EV_READ) 248 events |= POLLIN; 249 if (events & EV_WRITE) 250 events |= POLLOUT; 251 252 if (devpoll_queue(devpollop, fd, events) != 0) 253 return(-1); 254 255 return (0); 256 } 257 258 static int 259 devpoll_del(struct event_base *base, int fd, short old, short events, void *p) 260 { 261 struct devpollop *devpollop = base->evbase; 262 struct evdevpoll *evdp; 263 int events; 264 int needwritedelete = 1, needreaddelete = 1; 265 (void)p; 266 267 events = 0; 268 if (events & EV_READ) 269 events |= POLLIN; 270 if (events & EV_WRITE) 271 events |= POLLOUT; 272 273 /* 274 * The only way to remove an fd from the /dev/poll monitored set is 275 * to use POLLREMOVE by itself. This removes ALL events for the fd 276 * provided so if we care about two events and are only removing one 277 * we must re-add the other event after POLLREMOVE. 278 */ 279 280 if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0) 281 return(-1); 282 283 if ((events & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) { 284 /* 285 * We're not deleting all events, so we must resubmit the 286 * event that we are still interested in if one exists. 287 */ 288 289 if ((events & POLLIN) && (old & EV_WRITE)) { 290 /* Deleting read, still care about write */ 291 devpoll_queue(devpollop, fd, POLLOUT); 292 needwritedelete = 0; 293 } else if ((events & POLLOUT) && (old & EV_READ)) { 294 /* Deleting write, still care about read */ 295 devpoll_queue(devpollop, fd, POLLIN); 296 needreaddelete = 0; 297 } 298 } 299 300 return (0); 301 } 302 303 static void 304 devpoll_dealloc(struct event_base *base) 305 { 306 struct devpollop *devpollop = base->evbase; 307 308 evsig_dealloc(base); 309 if (devpollop->events) 310 mm_free(devpollop->events); 311 if (devpollop->changes) 312 mm_free(devpollop->changes); 313 if (devpollop->dpfd >= 0) 314 close(devpollop->dpfd); 315 316 memset(devpollop, 0, sizeof(struct devpollop)); 317 mm_free(devpollop); 318 } 319