libreplace: poll based on select
[metze/samba/wip.git] / lib / replace / poll.c
1 /*
2    Unix SMB/CIFS implementation.
3    poll.c - poll wrapper
4
5    This file is based on code from libssh (LGPLv2.1+ at the time it
6    was downloaded), thus the following copyrights:
7
8    Copyright (c) 2009-2010 by Andreas Schneider <mail@cynapses.org>
9    Copyright (c) 2003-2009 by Aris Adamantiadis
10    Copyright (c) 2009 Aleksandar Kanchev
11    Copyright (C) Volker Lendecke 2011
12
13      ** NOTE! The following LGPL license applies to the replace
14      ** library. This does NOT imply that all of Samba is released
15      ** under the LGPL
16
17    This library is free software; you can redistribute it and/or
18    modify it under the terms of the GNU Lesser General Public
19    License as published by the Free Software Foundation; either
20    version 3 of the License, or (at your option) any later version.
21
22    This library is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25    Lesser General Public License for more details.
26
27    You should have received a copy of the GNU Lesser General Public
28    License along with this library; if not, see <http://www.gnu.org/licenses/>.
29  */
30
31 #include "replace.h"
32 #include "system/select.h"
33
34
35 int rep_poll(struct pollfd *fds, nfds_t nfds, int timeout)
36 {
37         fd_set rfds, wfds, efds;
38         struct timeval tv, *ptv;
39         int max_fd;
40         int rc;
41         nfds_t i;
42
43         if (fds == NULL) {
44                 errno = EFAULT;
45                 return -1;
46         }
47
48         FD_ZERO(&rfds);
49         FD_ZERO(&wfds);
50         FD_ZERO(&efds);
51
52         rc = 0;
53         max_fd = 0;
54
55         /* compute fd_sets and find largest descriptor */
56         for (i = 0; i < nfds; i++) {
57                 if ((fds[i].fd < 0) || (fds[i].fd >= FD_SETSIZE)) {
58                         fds[i].revents = POLLNVAL;
59                         continue;
60                 }
61
62                 if (fds[i].events & (POLLIN | POLLRDNORM)) {
63                         FD_SET(fds[i].fd, &rfds);
64                 }
65                 if (fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
66                         FD_SET(fds[i].fd, &wfds);
67                 }
68                 if (fds[i].events & (POLLPRI | POLLRDBAND)) {
69                         FD_SET(fds[i].fd, &efds);
70                 }
71                 if (fds[i].fd > max_fd &&
72                     (fds[i].events & (POLLIN | POLLOUT | POLLPRI |
73                                       POLLRDNORM | POLLRDBAND |
74                                       POLLWRNORM | POLLWRBAND))) {
75                         max_fd = fds[i].fd;
76                 }
77         }
78
79         if (timeout < 0) {
80                 ptv = NULL;
81         } else {
82                 ptv = &tv;
83                 if (timeout == 0) {
84                         tv.tv_sec = 0;
85                         tv.tv_usec = 0;
86                 } else {
87                         tv.tv_sec = timeout / 1000;
88                         tv.tv_usec = (timeout % 1000) * 1000;
89                 }
90         }
91
92         rc = select(max_fd + 1, &rfds, &wfds, &efds, ptv);
93         if (rc < 0) {
94                 return -1;
95         }
96
97         for (rc = 0, i = 0; i < nfds; i++) {
98                 if ((fds[i].fd < 0) || (fds[i].fd >= FD_SETSIZE)) {
99                         continue;
100                 }
101
102                 fds[i].revents = 0;
103
104                 if (FD_ISSET(fds[i].fd, &rfds)) {
105                         int err = errno;
106                         int available = 0;
107                         int ret;
108
109                         /* support for POLLHUP */
110                         ret = ioctl(fds[i].fd, FIONREAD, &available);
111                         if ((ret == -1) || (available == 0)) {
112                                 fds[i].revents |= POLLHUP;
113                         } else {
114                                 fds[i].revents |= fds[i].events
115                                         & (POLLIN | POLLRDNORM);
116                         }
117
118                         errno = err;
119                 }
120                 if (FD_ISSET(fds[i].fd, &wfds)) {
121                         fds[i].revents |= fds[i].events
122                                 & (POLLOUT | POLLWRNORM | POLLWRBAND);
123                 }
124                 if (FD_ISSET(fds[i].fd, &efds)) {
125                         fds[i].revents |= fds[i].events
126                                 & (POLLPRI | POLLRDBAND);
127                 }
128                 if (fds[i].revents & ~POLLHUP) {
129                         rc++;
130                 }
131         }
132         return rc;
133 }