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