summaryrefslogtreecommitdiffstats
path: root/compat
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2008-08-28 17:45:30 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2008-08-28 17:45:30 +0000
commit33aa9315414dca1218b3c88b0c5ffc89d4379974 (patch)
tree1594150f11001adb185809b5f593ed4be14f3dbe /compat
parent0abb4ca413d3df152a969d5141622771ddc1f9fa (diff)
Support OS X by moving to gettimeofday(2) and adding poll compat from OpenSSH.
Diffstat (limited to 'compat')
-rw-r--r--compat/bsd-poll.c123
-rw-r--r--compat/bsd-poll.h61
2 files changed, 184 insertions, 0 deletions
diff --git a/compat/bsd-poll.c b/compat/bsd-poll.c
new file mode 100644
index 00000000..39da9d9a
--- /dev/null
+++ b/compat/bsd-poll.c
@@ -0,0 +1,123 @@
+/* $Id: bsd-poll.c,v 1.1 2008-08-28 17:45:30 nicm Exp $ */
+
+/*
+ * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au).
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/* #include "includes.h" */
+#define HAVE_SYS_SELECT_H
+#define MAX(a,b) (((a) > (b)) ? (a) : (b))
+#include <sys/types.h>
+
+#if !defined(HAVE_POLL)
+
+#ifdef HAVE_SYS_SELECT_H
+# include <sys/select.h>
+#endif
+
+#include <stdlib.h>
+#include <errno.h>
+#include "bsd-poll.h"
+
+/*
+ * A minimal implementation of poll(2), built on top of select(2).
+ *
+ * Only supports POLLIN and POLLOUT flags in pfd.events, and POLLIN, POLLOUT
+ * and POLLERR flags in revents.
+ *
+ * Supports pfd.fd = -1 meaning "unused" although it's not standard.
+ */
+
+int
+poll(struct pollfd *fds, nfds_t nfds, int timeout)
+{
+ nfds_t i;
+ int saved_errno, ret, fd, maxfd = 0;
+ fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL;
+ size_t nmemb;
+ struct timeval tv, *tvp = NULL;
+
+ for (i = 0; i < nfds; i++) {
+ fd = fds[i].fd;
+ if (fd >= FD_SETSIZE) {
+ errno = EINVAL;
+ return -1;
+ }
+ maxfd = MAX(maxfd, fd);
+ }
+
+ nmemb = howmany(maxfd + 1 , NFDBITS);
+ if ((readfds = calloc(nmemb, sizeof(fd_mask))) == NULL ||
+ (writefds = calloc(nmemb, sizeof(fd_mask))) == NULL ||
+ (exceptfds = calloc(nmemb, sizeof(fd_mask))) == NULL) {
+ saved_errno = ENOMEM;
+ ret = -1;
+ goto out;
+ }
+
+ /* populate event bit vectors for the events we're interested in */
+ for (i = 0; i < nfds; i++) {
+ fd = fds[i].fd;
+ if (fd == -1)
+ continue;
+ if (fds[i].events & POLLIN) {
+ FD_SET(fd, readfds);
+ FD_SET(fd, exceptfds);
+ }
+ if (fds[i].events & POLLOUT) {
+ FD_SET(fd, writefds);
+ FD_SET(fd, exceptfds);
+ }
+ }
+
+ /* poll timeout is msec, select is timeval (sec + usec) */
+ if (timeout >= 0) {
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = (timeout % 1000) * 1000;
+ tvp = &tv;
+ }
+
+ ret = select(maxfd + 1, readfds, writefds, exceptfds, tvp);
+ saved_errno = errno;
+
+ /* scan through select results and set poll() flags */
+ for (i = 0; i < nfds; i++) {
+ fd = fds[i].fd;
+ fds[i].revents = 0;
+ if (fd == -1)
+ continue;
+ if (FD_ISSET(fd, readfds)) {
+ fds[i].revents |= POLLIN;
+ }
+ if (FD_ISSET(fd, writefds)) {
+ fds[i].revents |= POLLOUT;
+ }
+ if (FD_ISSET(fd, exceptfds)) {
+ fds[i].revents |= POLLERR;
+ }
+ }
+
+out:
+ if (readfds != NULL)
+ free(readfds);
+ if (writefds != NULL)
+ free(writefds);
+ if (exceptfds != NULL)
+ free(exceptfds);
+ if (ret == -1)
+ errno = saved_errno;
+ return ret;
+}
+#endif
diff --git a/compat/bsd-poll.h b/compat/bsd-poll.h
new file mode 100644
index 00000000..dcbb9ca4
--- /dev/null
+++ b/compat/bsd-poll.h
@@ -0,0 +1,61 @@
+/* $OpenBSD: poll.h,v 1.11 2003/12/10 23:10:08 millert Exp $ */
+
+/*
+ * Copyright (c) 1996 Theo de Raadt
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* OPENBSD ORIGINAL: sys/sys/poll.h */
+
+#if !defined(HAVE_POLL) && !defined(HAVE_POLL_H)
+#ifndef _COMPAT_POLL_H_
+#define _COMPAT_POLL_H_
+
+typedef struct pollfd {
+ int fd;
+ short events;
+ short revents;
+} pollfd_t;
+
+typedef unsigned int nfds_t;
+
+#define POLLIN 0x0001
+#define POLLOUT 0x0004
+#define POLLERR 0x0008
+#if 0
+/* the following are currently not implemented */
+#define POLLPRI 0x0002
+#define POLLHUP 0x0010
+#define POLLNVAL 0x0020
+#define POLLRDNORM 0x0040
+#define POLLNORM POLLRDNORM
+#define POLLWRNORM POLLOUT
+#define POLLRDBAND 0x0080
+#define POLLWRBAND 0x0100
+#endif
+
+#define INFTIM (-1) /* not standard */
+
+int poll(struct pollfd *, nfds_t, int);
+#endif /* !_COMPAT_POLL_H_ */
+#endif /* !HAVE_POLL_H */