]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/poll.cc
* environ.cc (environ_init): Avoid a compiler warning.
[newlib-cygwin.git] / winsup / cygwin / poll.cc
1 /* poll.cc. Implements poll(2) via usage of select(2) call.
2
3 Copyright 2000, 2001, 2002 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 <sys/socket.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 #include "security.h"
18 #include "fhandler.h"
19 #include "path.h"
20 #include "dtable.h"
21 #include "cygerrno.h"
22 #include "cygheap.h"
23 #include "sigproc.h"
24
25 extern "C"
26 int
27 poll (struct pollfd *fds, unsigned int nfds, int timeout)
28 {
29 int max_fd = 0;
30 fd_set *read_fds, *write_fds, *except_fds;
31 struct timeval tv = { timeout / 1000, (timeout % 1000) * 1000 };
32 sigframe thisframe (mainthread);
33
34 for (unsigned int i = 0; i < nfds; ++i)
35 if (fds[i].fd > max_fd)
36 max_fd = fds[i].fd;
37
38 size_t fds_size = howmany (max_fd + 1, NFDBITS) * sizeof (fd_mask);
39
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 (!read_fds || !write_fds || !except_fds)
45 {
46 set_errno (ENOMEM);
47 return -1;
48 }
49
50 memset (read_fds, 0, fds_size);
51 memset (write_fds, 0, fds_size);
52 memset (except_fds, 0, fds_size);
53
54 int invalid_fds = 0;
55 for (unsigned int i = 0; i < nfds; ++i)
56 {
57 fds[i].revents = 0;
58 if (!cygheap->fdtab.not_open (fds[i].fd))
59 {
60 if (fds[i].events & POLLIN)
61 FD_SET(fds[i].fd, read_fds);
62 if (fds[i].events & POLLOUT)
63 FD_SET(fds[i].fd, write_fds);
64 if (fds[i].events & POLLPRI)
65 FD_SET(fds[i].fd, except_fds);
66 }
67 else if (fds[i].fd >= 0)
68 {
69 ++invalid_fds;
70 fds[i].revents = POLLNVAL;
71 }
72 }
73
74 if (invalid_fds)
75 return invalid_fds;
76
77 int ret = cygwin_select (max_fd + 1, read_fds, write_fds, except_fds, timeout < 0 ? NULL : &tv);
78
79 if (ret > 0)
80 for (unsigned int i = 0; i < nfds; ++i)
81 {
82 if (fds[i].fd >= 0)
83 {
84 if (cygheap->fdtab.not_open (fds[i].fd))
85 fds[i].revents = POLLHUP;
86 else
87 {
88 if (FD_ISSET(fds[i].fd, read_fds))
89 {
90 char peek[1];
91 fhandler_socket *sock =
92 cygheap->fdtab[fds[i].fd]->is_socket ();
93 if (!sock)
94 fds[i].revents |= POLLIN;
95 else
96 switch (sock->recvfrom (peek, sizeof (peek), MSG_PEEK,
97 NULL, NULL))
98 {
99 case -1: /* Something weird happened */
100 fds[i].revents |= POLLERR;
101 break;
102 case 0: /* Closed on the read side. */
103 fds[i].revents |= POLLHUP;
104 break;
105 default:
106 fds[i].revents |= POLLIN;
107 break;
108 }
109 }
110 if (FD_ISSET(fds[i].fd, write_fds))
111 fds[i].revents |= POLLOUT;
112 if (FD_ISSET(fds[i].fd, except_fds))
113 fds[i].revents |= POLLPRI;
114 }
115 }
116 }
117
118 return ret;
119 }
This page took 0.039033 seconds and 5 git commands to generate.