| 1 | /* |
|---|
| 2 | * poller_poll.c: poll() polling mechanism |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2002 the Ithildin Project. |
|---|
| 5 | * See the COPYING file for more information on licensing and use. |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | IDSTRING(poller_rcsid, "$Id$"); |
|---|
| 9 | |
|---|
| 10 | int poll_sockets(time_t timeout) { |
|---|
| 11 | int msec; |
|---|
| 12 | int ret; |
|---|
| 13 | struct isocket *sp; |
|---|
| 14 | |
|---|
| 15 | if (timeout == 0) |
|---|
| 16 | msec = INFTIM; |
|---|
| 17 | else if ((msec = timeout * 1000) <= 0) |
|---|
| 18 | msec = INT_MAX; /* ... fuh */ |
|---|
| 19 | |
|---|
| 20 | if ((ret = poll(pollfds, maxsockets, msec)) == -1 && errno != EINTR) { |
|---|
| 21 | log_error("poll(%p, %d, %d) error: %s", pollfds, maxsockets, |
|---|
| 22 | msec, strerror(errno)); |
|---|
| 23 | return 0; |
|---|
| 24 | } else if (ret <= 0) |
|---|
| 25 | return 1; |
|---|
| 26 | |
|---|
| 27 | me.now = time(NULL); |
|---|
| 28 | LIST_FOREACH(sp, &allsockets, intlp) { |
|---|
| 29 | if (SOCKET_DEAD(sp) || sp->fd < 0) |
|---|
| 30 | continue; /* dead socket. don't touch. */ |
|---|
| 31 | if (pollfds[sp->fd].revents) { |
|---|
| 32 | if (pollfds[sp->fd].revents & POLLIN) |
|---|
| 33 | sp->state |= SOCKET_FL_READ_PENDING; |
|---|
| 34 | if (pollfds[sp->fd].revents & POLLOUT) { |
|---|
| 35 | sp->state |= SOCKET_FL_WRITE_PENDING; |
|---|
| 36 | socket_unmonitor(sp, SOCKET_FL_WRITE); |
|---|
| 37 | } |
|---|
| 38 | if (pollfds[sp->fd].revents & (POLLERR|POLLHUP|POLLNVAL)) { |
|---|
| 39 | sp->state |= SOCKET_FL_ERROR_PENDING; |
|---|
| 40 | /* attempt a bogus write to get errno. yech */ |
|---|
| 41 | if (write(sp->fd, &sp->state, sizeof(sp->state)) != -1) { |
|---|
| 42 | log_error("yipes! poll() lied to me!"); |
|---|
| 43 | me.shutdown = 1; |
|---|
| 44 | return 0; |
|---|
| 45 | } |
|---|
| 46 | sp->err = errno; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | } |
|---|
| 50 | if (sp->state & SOCKET_FL_PENDING) |
|---|
| 51 | socket_event(sp); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | return 1; |
|---|
| 55 | } |
|---|
| 56 | /* vi:set ts=8 sts=4 sw=4 tw=76 et: */ |
|---|