blob: c8e6222c0e345eff9e977c94dc4105b8d8ad212a [file] [log] [blame]
Darren Tuckerfebf0f52007-06-25 22:15:12 +10001/*
2 * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au).
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "includes.h"
Darren Tucker7ec87332008-02-29 13:57:47 +110018#if !defined(HAVE_POLL)
Darren Tuckerfebf0f52007-06-25 22:15:12 +100019
Darren Tucker8c333ec2013-11-08 21:12:58 +110020#include <sys/types.h>
21#include <sys/time.h>
Darren Tuckerfebf0f52007-06-25 22:15:12 +100022#ifdef HAVE_SYS_SELECT_H
23# include <sys/select.h>
24#endif
25
Darren Tuckerfebf0f52007-06-25 22:15:12 +100026#include <errno.h>
Darren Tucker8c333ec2013-11-08 21:12:58 +110027#include <stdlib.h>
28#include <unistd.h>
Darren Tuckerfebf0f52007-06-25 22:15:12 +100029#include "bsd-poll.h"
30
31/*
32 * A minimal implementation of poll(2), built on top of select(2).
33 *
34 * Only supports POLLIN and POLLOUT flags in pfd.events, and POLLIN, POLLOUT
35 * and POLLERR flags in revents.
36 *
37 * Supports pfd.fd = -1 meaning "unused" although it's not standard.
38 */
39
40int
41poll(struct pollfd *fds, nfds_t nfds, int timeout)
42{
43 nfds_t i;
44 int saved_errno, ret, fd, maxfd = 0;
45 fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL;
46 size_t nmemb;
47 struct timeval tv, *tvp = NULL;
48
49 for (i = 0; i < nfds; i++) {
Darren Tucker661f63b2008-08-30 07:32:37 +100050 fd = fds[i].fd;
Darren Tuckerfebf0f52007-06-25 22:15:12 +100051 if (fd >= FD_SETSIZE) {
52 errno = EINVAL;
53 return -1;
54 }
Darren Tucker661f63b2008-08-30 07:32:37 +100055 maxfd = MAX(maxfd, fd);
Darren Tuckerfebf0f52007-06-25 22:15:12 +100056 }
57
58 nmemb = howmany(maxfd + 1 , NFDBITS);
59 if ((readfds = calloc(nmemb, sizeof(fd_mask))) == NULL ||
60 (writefds = calloc(nmemb, sizeof(fd_mask))) == NULL ||
61 (exceptfds = calloc(nmemb, sizeof(fd_mask))) == NULL) {
62 saved_errno = ENOMEM;
63 ret = -1;
64 goto out;
65 }
66
67 /* populate event bit vectors for the events we're interested in */
68 for (i = 0; i < nfds; i++) {
69 fd = fds[i].fd;
70 if (fd == -1)
71 continue;
72 if (fds[i].events & POLLIN) {
73 FD_SET(fd, readfds);
74 FD_SET(fd, exceptfds);
75 }
76 if (fds[i].events & POLLOUT) {
77 FD_SET(fd, writefds);
78 FD_SET(fd, exceptfds);
79 }
80 }
81
82 /* poll timeout is msec, select is timeval (sec + usec) */
83 if (timeout >= 0) {
84 tv.tv_sec = timeout / 1000;
85 tv.tv_usec = (timeout % 1000) * 1000;
86 tvp = &tv;
87 }
88
89 ret = select(maxfd + 1, readfds, writefds, exceptfds, tvp);
90 saved_errno = errno;
91
92 /* scan through select results and set poll() flags */
93 for (i = 0; i < nfds; i++) {
94 fd = fds[i].fd;
95 fds[i].revents = 0;
96 if (fd == -1)
97 continue;
98 if (FD_ISSET(fd, readfds)) {
99 fds[i].revents |= POLLIN;
100 }
101 if (FD_ISSET(fd, writefds)) {
102 fds[i].revents |= POLLOUT;
103 }
104 if (FD_ISSET(fd, exceptfds)) {
105 fds[i].revents |= POLLERR;
106 }
107 }
108
109out:
Darren Tucker8d36f9a2014-02-06 10:44:13 +1100110 free(readfds);
111 free(writefds);
112 free(exceptfds);
Darren Tuckerfebf0f52007-06-25 22:15:12 +1000113 if (ret == -1)
114 errno = saved_errno;
115 return ret;
116}
117#endif