OSDN Git Service

2002-03-01 David O'Brien <obrien@FreeBSD.org>
[pf3gnuchains/pf3gnuchains3x.git] / winsup / cygwin / poll.cc
1 /* poll.cc. Implements poll(2) via usage of select(2) call.
2
3    Copyright 2000, 2001 Red Hat, Inc.
4
5    This file is part of Cygwin.
6
7    This software is a copyrighted work licensed under the terms of the
8    Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9    details. */
10
11 #include "winsup.h"
12 #include <sys/time.h>
13 #include <sys/poll.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include "security.h"
17 #include "fhandler.h"
18 #include "path.h"
19 #include "dtable.h"
20 #include "cygerrno.h"
21 #include "cygheap.h"
22 #include "sigproc.h"
23
24 extern "C"
25 int
26 poll (struct pollfd *fds, unsigned int nfds, int timeout)
27 {
28   int max_fd = 0;
29   fd_set *open_fds, *read_fds, *write_fds, *except_fds;
30   struct timeval tv = { timeout / 1000, (timeout % 1000) * 1000 };
31   sigframe thisframe (mainthread);
32
33   for (unsigned int i = 0; i < nfds; ++i)
34     if (fds[i].fd > max_fd)
35       max_fd = fds[i].fd;
36
37   size_t fds_size = howmany(max_fd + 1, NFDBITS) * sizeof (fd_mask);
38
39   open_fds = (fd_set *) alloca (fds_size);
40   read_fds = (fd_set *) alloca (fds_size);
41   write_fds = (fd_set *) alloca (fds_size);
42   except_fds = (fd_set *) alloca (fds_size);
43
44   if (!open_fds || !read_fds || !write_fds || !except_fds)
45     {
46       set_errno (ENOMEM);
47       return -1;
48     }
49
50   memset (open_fds, 0, fds_size);
51   memset (read_fds, 0, fds_size);
52   memset (write_fds, 0, fds_size);
53   memset (except_fds, 0, fds_size);
54
55   bool valid_fds = false;
56   for (unsigned int i = 0; i < nfds; ++i)
57     if (!cygheap->fdtab.not_open (fds[i].fd))
58       {
59         valid_fds = true;
60         fds[i].revents = 0;
61         FD_SET (fds[i].fd, open_fds);
62         if (fds[i].events & POLLIN)
63           FD_SET (fds[i].fd, read_fds);
64         if (fds[i].events & POLLOUT)
65           FD_SET (fds[i].fd, write_fds);
66         if (fds[i].events & POLLPRI)
67           FD_SET (fds[i].fd, except_fds);
68       }
69       else
70         fds[i].revents = POLLNVAL;
71
72   int ret = 0;
73   if (valid_fds)
74     ret = cygwin_select (max_fd + 1, read_fds, write_fds, except_fds,
75                          timeout < 0 ? NULL : &tv);
76
77   for (unsigned int i = 0; i < nfds; ++i)
78     {
79       if (fds[i].revents == POLLNVAL && ret >= 0)
80         ret++;
81       else if (cygheap->fdtab.not_open(fds[i].fd))
82         fds[i].revents = POLLHUP;
83       else if (ret < 0)
84         fds[i].revents = POLLERR;
85       else
86         {
87           fds[i].revents = 0;
88           if (FD_ISSET (fds[i].fd, read_fds))
89             fds[i].revents |= POLLIN;
90           if (FD_ISSET (fds[i].fd, write_fds))
91             fds[i].revents |= POLLOUT;
92           if (FD_ISSET (fds[i].fd, except_fds))
93             fds[i].revents |= POLLPRI;
94         }
95     }
96
97   return ret;
98 }