2000-07-04 09:58:49 -07:00
|
|
|
/* poll.cc. Implements poll(2) via usage of select(2) call.
|
|
|
|
|
2001-09-11 13:01:02 -07:00
|
|
|
Copyright 2000, 2001 Red Hat, Inc.
|
2000-07-04 09:58:49 -07:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-11 22:39:41 -07:00
|
|
|
#include "winsup.h"
|
2000-09-07 19:56:55 -07:00
|
|
|
#include <sys/time.h>
|
2000-08-02 09:28:18 -07:00
|
|
|
#include <sys/poll.h>
|
2000-08-11 13:34:24 -07:00
|
|
|
#include <errno.h>
|
2001-07-26 12:22:24 -07:00
|
|
|
#include "security.h"
|
2000-08-21 22:10:20 -07:00
|
|
|
#include "fhandler.h"
|
2001-09-30 21:10:07 -07:00
|
|
|
#include "path.h"
|
2000-08-11 22:35:42 -07:00
|
|
|
#include "dtable.h"
|
2001-04-18 14:10:15 -07:00
|
|
|
#include "cygheap.h"
|
2000-08-21 20:58:47 -07:00
|
|
|
#include "cygerrno.h"
|
2001-03-19 14:48:26 -08:00
|
|
|
#include "sigproc.h"
|
2000-07-04 09:58:49 -07:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
int
|
|
|
|
poll (struct pollfd *fds, unsigned int nfds, int timeout)
|
|
|
|
{
|
|
|
|
int max_fd = 0;
|
2000-08-11 13:34:24 -07:00
|
|
|
fd_set *open_fds, *read_fds, *write_fds, *except_fds;
|
2000-07-04 09:58:49 -07:00
|
|
|
struct timeval tv = { timeout / 1000, (timeout % 1000) * 1000 };
|
2001-03-19 14:48:26 -08:00
|
|
|
sigframe thisframe (mainthread);
|
2000-07-04 09:58:49 -07:00
|
|
|
|
2000-08-11 13:34:24 -07:00
|
|
|
for (unsigned int i = 0; i < nfds; ++i)
|
|
|
|
if (fds[i].fd > max_fd)
|
|
|
|
max_fd = fds[i].fd;
|
|
|
|
|
|
|
|
size_t fds_size = howmany(max_fd + 1, NFDBITS) * sizeof (fd_mask);
|
|
|
|
|
|
|
|
open_fds = (fd_set *) alloca (fds_size);
|
|
|
|
read_fds = (fd_set *) alloca (fds_size);
|
|
|
|
write_fds = (fd_set *) alloca (fds_size);
|
|
|
|
except_fds = (fd_set *) alloca (fds_size);
|
|
|
|
|
|
|
|
if (!open_fds || !read_fds || !write_fds || !except_fds)
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (open_fds, 0, fds_size);
|
|
|
|
memset (read_fds, 0, fds_size);
|
|
|
|
memset (write_fds, 0, fds_size);
|
|
|
|
memset (except_fds, 0, fds_size);
|
2000-07-04 09:58:49 -07:00
|
|
|
|
2001-09-07 10:17:54 -07:00
|
|
|
bool invalid_fds = false;
|
2000-07-04 09:58:49 -07:00
|
|
|
for (unsigned int i = 0; i < nfds; ++i)
|
2001-04-18 14:10:15 -07:00
|
|
|
if (!cygheap->fdtab.not_open (fds[i].fd))
|
2000-07-04 09:58:49 -07:00
|
|
|
{
|
2000-09-02 21:16:35 -07:00
|
|
|
FD_SET (fds[i].fd, open_fds);
|
|
|
|
if (fds[i].events & POLLIN)
|
|
|
|
FD_SET (fds[i].fd, read_fds);
|
|
|
|
if (fds[i].events & POLLOUT)
|
|
|
|
FD_SET (fds[i].fd, write_fds);
|
|
|
|
if (fds[i].events & POLLPRI)
|
|
|
|
FD_SET (fds[i].fd, except_fds);
|
2000-07-04 09:58:49 -07:00
|
|
|
}
|
2001-09-07 10:17:54 -07:00
|
|
|
else
|
|
|
|
invalid_fds = true;
|
2000-07-04 09:58:49 -07:00
|
|
|
|
2001-09-07 10:17:54 -07:00
|
|
|
int ret = 0;
|
|
|
|
if (!invalid_fds)
|
|
|
|
ret = cygwin_select (max_fd + 1, read_fds, write_fds, except_fds,
|
|
|
|
timeout < 0 ? NULL : &tv);
|
2000-07-04 09:58:49 -07:00
|
|
|
|
2000-08-11 05:51:47 -07:00
|
|
|
for (unsigned int i = 0; i < nfds; ++i)
|
|
|
|
{
|
2000-08-11 13:34:24 -07:00
|
|
|
if (!FD_ISSET (fds[i].fd, open_fds))
|
2001-09-07 10:17:54 -07:00
|
|
|
{
|
|
|
|
fds[i].revents = POLLNVAL;
|
|
|
|
ret++;
|
|
|
|
}
|
2001-04-18 14:10:15 -07:00
|
|
|
else if (cygheap->fdtab.not_open(fds[i].fd))
|
2000-09-02 21:16:35 -07:00
|
|
|
fds[i].revents = POLLHUP;
|
2000-08-11 05:51:47 -07:00
|
|
|
else if (ret < 0)
|
2000-09-02 21:16:35 -07:00
|
|
|
fds[i].revents = POLLERR;
|
2000-08-11 05:51:47 -07:00
|
|
|
else
|
2000-09-02 21:16:35 -07:00
|
|
|
{
|
|
|
|
fds[i].revents = 0;
|
|
|
|
if (FD_ISSET (fds[i].fd, read_fds))
|
|
|
|
fds[i].revents |= POLLIN;
|
|
|
|
if (FD_ISSET (fds[i].fd, write_fds))
|
|
|
|
fds[i].revents |= POLLOUT;
|
|
|
|
if (FD_ISSET (fds[i].fd, except_fds))
|
|
|
|
fds[i].revents |= POLLPRI;
|
|
|
|
}
|
2000-08-11 05:51:47 -07:00
|
|
|
}
|
2000-07-04 09:58:49 -07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|