2007-06-25 12:15:12 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2021-11-18 12:11:37 +00:00
|
|
|
#if !defined(HAVE_PPOLL) || !defined(HAVE_POLL)
|
2007-06-25 12:15:12 +00:00
|
|
|
|
2013-11-08 10:12:58 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
2007-06-25 12:15:12 +00:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
# include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
2021-11-18 12:11:37 +00:00
|
|
|
#include <signal.h>
|
2013-11-08 10:12:58 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2007-06-25 12:15:12 +00:00
|
|
|
#include "bsd-poll.h"
|
|
|
|
|
2021-11-18 12:11:37 +00:00
|
|
|
#ifndef HAVE_PPOLL
|
2007-06-25 12:15:12 +00:00
|
|
|
/*
|
2021-11-18 12:11:37 +00:00
|
|
|
* A minimal implementation of ppoll(2), built on top of pselect(2).
|
2007-06-25 12:15:12 +00:00
|
|
|
*
|
|
|
|
* 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
|
2021-11-18 12:11:37 +00:00
|
|
|
ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmoutp,
|
|
|
|
const sigset_t *sigmask)
|
2007-06-25 12:15:12 +00:00
|
|
|
{
|
|
|
|
nfds_t i;
|
|
|
|
int saved_errno, ret, fd, maxfd = 0;
|
|
|
|
fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL;
|
|
|
|
size_t nmemb;
|
|
|
|
|
|
|
|
for (i = 0; i < nfds; i++) {
|
2008-08-29 21:32:37 +00:00
|
|
|
fd = fds[i].fd;
|
2007-06-25 12:15:12 +00:00
|
|
|
if (fd >= FD_SETSIZE) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2008-08-29 21:32:37 +00:00
|
|
|
maxfd = MAX(maxfd, fd);
|
2007-06-25 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 12:11:37 +00:00
|
|
|
ret = pselect(maxfd + 1, readfds, writefds, exceptfds, tmoutp, sigmask);
|
2007-06-25 12:15:12 +00:00
|
|
|
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:
|
2014-02-05 23:44:13 +00:00
|
|
|
free(readfds);
|
|
|
|
free(writefds);
|
|
|
|
free(exceptfds);
|
2007-06-25 12:15:12 +00:00
|
|
|
if (ret == -1)
|
|
|
|
errno = saved_errno;
|
|
|
|
return ret;
|
|
|
|
}
|
2021-11-18 12:11:37 +00:00
|
|
|
#endif /* HAVE_PPOLL */
|
|
|
|
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
int
|
|
|
|
poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
|
|
|
{
|
|
|
|
struct timespec ts, *tsp = NULL;
|
|
|
|
|
|
|
|
/* poll timeout is msec, ppoll is timespec (sec + nsec) */
|
|
|
|
if (timeout >= 0) {
|
|
|
|
ts.tv_sec = timeout / 1000;
|
2021-11-18 12:44:07 +00:00
|
|
|
ts.tv_nsec = (timeout % 1000) * 1000000;
|
2021-11-18 12:11:37 +00:00
|
|
|
tsp = &ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ppoll(fds, nfds, tsp, NULL);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_POLL */
|
|
|
|
|
|
|
|
#endif /* HAVE_PPOLL || HAVE_POLL */
|