diff options
author | Darren Tucker <dtucker@zip.com.au> | 2007-06-25 22:15:12 +1000 |
---|---|---|
committer | Darren Tucker <dtucker@zip.com.au> | 2007-06-25 22:15:12 +1000 |
commit | febf0f5668f997c63210b3dbd50ce5443b0f6aea (patch) | |
tree | df7829b7c652ba79cd3d2a357dbf5a4b083de6da /openbsd-compat | |
parent | dc4a779fbbefd662e1b0b4dd2417329826ff264f (diff) |
- (dtucker) [atomicio.c configure.ac openbsd-compat/Makefile.in
openbsd-compat/bsd-poll.{c,h} openbsd-compat/openbsd-compat.h]
Add an implementation of poll() built on top of select(2). Code from
OpenNTPD with changes suggested by djm. ok djm@
Diffstat (limited to 'openbsd-compat')
-rw-r--r-- | openbsd-compat/Makefile.in | 4 | ||||
-rw-r--r-- | openbsd-compat/bsd-poll.c | 117 | ||||
-rw-r--r-- | openbsd-compat/bsd-poll.h | 61 | ||||
-rw-r--r-- | openbsd-compat/openbsd-compat.h | 3 |
4 files changed, 182 insertions, 3 deletions
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in index 9f06605d7..b44a7851e 100644 --- a/openbsd-compat/Makefile.in +++ b/openbsd-compat/Makefile.in | |||
@@ -1,4 +1,4 @@ | |||
1 | # $Id: Makefile.in,v 1.40 2006/08/30 17:24:41 djm Exp $ | 1 | # $Id: Makefile.in,v 1.41 2007/06/25 12:15:13 dtucker Exp $ |
2 | 2 | ||
3 | sysconfdir=@sysconfdir@ | 3 | sysconfdir=@sysconfdir@ |
4 | piddir=@piddir@ | 4 | piddir=@piddir@ |
@@ -18,7 +18,7 @@ LDFLAGS=-L. @LDFLAGS@ | |||
18 | 18 | ||
19 | OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sha2.o sigact.o strlcat.o strlcpy.o strmode.o strsep.o strtonum.o strtoll.o strtoul.o vis.o | 19 | OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sha2.o sigact.o strlcat.o strlcpy.o strmode.o strsep.o strtonum.o strtoll.o strtoul.o vis.o |
20 | 20 | ||
21 | COMPAT=bsd-arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-snprintf.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o | 21 | COMPAT=bsd-arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-snprintf.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o |
22 | 22 | ||
23 | PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o | 23 | PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o |
24 | 24 | ||
diff --git a/openbsd-compat/bsd-poll.c b/openbsd-compat/bsd-poll.c new file mode 100644 index 000000000..836882eea --- /dev/null +++ b/openbsd-compat/bsd-poll.c | |||
@@ -0,0 +1,117 @@ | |||
1 | /* $Id: bsd-poll.c,v 1.1 2007/06/25 12:15:13 dtucker Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au). | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include "includes.h" | ||
20 | #if !defined(HAVE_POLL) && defined(HAVE_SELECT) | ||
21 | |||
22 | #ifdef HAVE_SYS_SELECT_H | ||
23 | # include <sys/select.h> | ||
24 | #endif | ||
25 | |||
26 | #include <errno.h> | ||
27 | #include "bsd-poll.h" | ||
28 | |||
29 | /* | ||
30 | * A minimal implementation of poll(2), built on top of select(2). | ||
31 | * | ||
32 | * Only supports POLLIN and POLLOUT flags in pfd.events, and POLLIN, POLLOUT | ||
33 | * and POLLERR flags in revents. | ||
34 | * | ||
35 | * Supports pfd.fd = -1 meaning "unused" although it's not standard. | ||
36 | */ | ||
37 | |||
38 | int | ||
39 | poll(struct pollfd *fds, nfds_t nfds, int timeout) | ||
40 | { | ||
41 | nfds_t i; | ||
42 | int saved_errno, ret, fd, maxfd = 0; | ||
43 | fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL; | ||
44 | size_t nmemb; | ||
45 | struct timeval tv, *tvp = NULL; | ||
46 | |||
47 | for (i = 0; i < nfds; i++) { | ||
48 | if (fd >= FD_SETSIZE) { | ||
49 | errno = EINVAL; | ||
50 | return -1; | ||
51 | } | ||
52 | maxfd = MAX(maxfd, fds[i].fd); | ||
53 | } | ||
54 | |||
55 | nmemb = howmany(maxfd + 1 , NFDBITS); | ||
56 | if ((readfds = calloc(nmemb, sizeof(fd_mask))) == NULL || | ||
57 | (writefds = calloc(nmemb, sizeof(fd_mask))) == NULL || | ||
58 | (exceptfds = calloc(nmemb, sizeof(fd_mask))) == NULL) { | ||
59 | saved_errno = ENOMEM; | ||
60 | ret = -1; | ||
61 | goto out; | ||
62 | } | ||
63 | |||
64 | /* populate event bit vectors for the events we're interested in */ | ||
65 | for (i = 0; i < nfds; i++) { | ||
66 | fd = fds[i].fd; | ||
67 | if (fd == -1) | ||
68 | continue; | ||
69 | if (fds[i].events & POLLIN) { | ||
70 | FD_SET(fd, readfds); | ||
71 | FD_SET(fd, exceptfds); | ||
72 | } | ||
73 | if (fds[i].events & POLLOUT) { | ||
74 | FD_SET(fd, writefds); | ||
75 | FD_SET(fd, exceptfds); | ||
76 | } | ||
77 | } | ||
78 | |||
79 | /* poll timeout is msec, select is timeval (sec + usec) */ | ||
80 | if (timeout >= 0) { | ||
81 | tv.tv_sec = timeout / 1000; | ||
82 | tv.tv_usec = (timeout % 1000) * 1000; | ||
83 | tvp = &tv; | ||
84 | } | ||
85 | |||
86 | ret = select(maxfd + 1, readfds, writefds, exceptfds, tvp); | ||
87 | saved_errno = errno; | ||
88 | |||
89 | /* scan through select results and set poll() flags */ | ||
90 | for (i = 0; i < nfds; i++) { | ||
91 | fd = fds[i].fd; | ||
92 | fds[i].revents = 0; | ||
93 | if (fd == -1) | ||
94 | continue; | ||
95 | if (FD_ISSET(fd, readfds)) { | ||
96 | fds[i].revents |= POLLIN; | ||
97 | } | ||
98 | if (FD_ISSET(fd, writefds)) { | ||
99 | fds[i].revents |= POLLOUT; | ||
100 | } | ||
101 | if (FD_ISSET(fd, exceptfds)) { | ||
102 | fds[i].revents |= POLLERR; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | out: | ||
107 | if (readfds != NULL) | ||
108 | free(readfds); | ||
109 | if (writefds != NULL) | ||
110 | free(writefds); | ||
111 | if (exceptfds != NULL) | ||
112 | free(exceptfds); | ||
113 | if (ret == -1) | ||
114 | errno = saved_errno; | ||
115 | return ret; | ||
116 | } | ||
117 | #endif | ||
diff --git a/openbsd-compat/bsd-poll.h b/openbsd-compat/bsd-poll.h new file mode 100644 index 000000000..dcbb9ca40 --- /dev/null +++ b/openbsd-compat/bsd-poll.h | |||
@@ -0,0 +1,61 @@ | |||
1 | /* $OpenBSD: poll.h,v 1.11 2003/12/10 23:10:08 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 Theo de Raadt | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
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 | |||
28 | /* OPENBSD ORIGINAL: sys/sys/poll.h */ | ||
29 | |||
30 | #if !defined(HAVE_POLL) && !defined(HAVE_POLL_H) | ||
31 | #ifndef _COMPAT_POLL_H_ | ||
32 | #define _COMPAT_POLL_H_ | ||
33 | |||
34 | typedef struct pollfd { | ||
35 | int fd; | ||
36 | short events; | ||
37 | short revents; | ||
38 | } pollfd_t; | ||
39 | |||
40 | typedef unsigned int nfds_t; | ||
41 | |||
42 | #define POLLIN 0x0001 | ||
43 | #define POLLOUT 0x0004 | ||
44 | #define POLLERR 0x0008 | ||
45 | #if 0 | ||
46 | /* the following are currently not implemented */ | ||
47 | #define POLLPRI 0x0002 | ||
48 | #define POLLHUP 0x0010 | ||
49 | #define POLLNVAL 0x0020 | ||
50 | #define POLLRDNORM 0x0040 | ||
51 | #define POLLNORM POLLRDNORM | ||
52 | #define POLLWRNORM POLLOUT | ||
53 | #define POLLRDBAND 0x0080 | ||
54 | #define POLLWRBAND 0x0100 | ||
55 | #endif | ||
56 | |||
57 | #define INFTIM (-1) /* not standard */ | ||
58 | |||
59 | int poll(struct pollfd *, nfds_t, int); | ||
60 | #endif /* !_COMPAT_POLL_H_ */ | ||
61 | #endif /* !HAVE_POLL_H */ | ||
diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h index aac2e6cbc..6406af19d 100644 --- a/openbsd-compat/openbsd-compat.h +++ b/openbsd-compat/openbsd-compat.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $Id: openbsd-compat.h,v 1.42 2006/09/03 12:44:50 dtucker Exp $ */ | 1 | /* $Id: openbsd-compat.h,v 1.43 2007/06/25 12:15:13 dtucker Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 1999-2003 Damien Miller. All rights reserved. | 4 | * Copyright (c) 1999-2003 Damien Miller. All rights reserved. |
@@ -140,6 +140,7 @@ int writev(int, struct iovec *, int); | |||
140 | /* Home grown routines */ | 140 | /* Home grown routines */ |
141 | #include "bsd-misc.h" | 141 | #include "bsd-misc.h" |
142 | #include "bsd-waitpid.h" | 142 | #include "bsd-waitpid.h" |
143 | #include "bsd-poll.h" | ||
143 | 144 | ||
144 | #ifndef HAVE_GETPEEREID | 145 | #ifndef HAVE_GETPEEREID |
145 | int getpeereid(int , uid_t *, gid_t *); | 146 | int getpeereid(int , uid_t *, gid_t *); |