source: trunk/server/lib/replace/poll.c@ 745

Last change on this file since 745 was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 3.4 KB
Line 
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#ifdef __OS2__
35#include <sys/time.h>
36#include <sys/ioctl.h>
37#endif
38
39int rep_poll(struct pollfd *fds, nfds_t nfds, int timeout)
40{
41 fd_set rfds, wfds, efds;
42 struct timeval tv, *ptv;
43 int max_fd;
44 int rc;
45 nfds_t i;
46
47 if (fds == NULL) {
48 errno = EFAULT;
49 return -1;
50 }
51
52 FD_ZERO(&rfds);
53 FD_ZERO(&wfds);
54 FD_ZERO(&efds);
55
56 rc = 0;
57 max_fd = 0;
58
59 /* compute fd_sets and find largest descriptor */
60 for (i = 0; i < nfds; i++) {
61 if ((fds[i].fd < 0) || (fds[i].fd >= FD_SETSIZE)) {
62 fds[i].revents = POLLNVAL;
63 continue;
64 }
65
66 if (fds[i].events & (POLLIN | POLLRDNORM)) {
67 FD_SET(fds[i].fd, &rfds);
68 }
69 if (fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
70 FD_SET(fds[i].fd, &wfds);
71 }
72 if (fds[i].events & (POLLPRI | POLLRDBAND)) {
73 FD_SET(fds[i].fd, &efds);
74 }
75 if (fds[i].fd > max_fd &&
76 (fds[i].events & (POLLIN | POLLOUT | POLLPRI |
77 POLLRDNORM | POLLRDBAND |
78 POLLWRNORM | POLLWRBAND))) {
79 max_fd = fds[i].fd;
80 }
81 }
82
83 if (timeout < 0) {
84 ptv = NULL;
85 } else {
86 ptv = &tv;
87 if (timeout == 0) {
88 tv.tv_sec = 0;
89 tv.tv_usec = 0;
90 } else {
91 tv.tv_sec = timeout / 1000;
92 tv.tv_usec = (timeout % 1000) * 1000;
93 }
94 }
95
96 rc = select(max_fd + 1, &rfds, &wfds, &efds, ptv);
97 if (rc < 0) {
98 return -1;
99 }
100
101 for (rc = 0, i = 0; i < nfds; i++) {
102 if ((fds[i].fd < 0) || (fds[i].fd >= FD_SETSIZE)) {
103 continue;
104 }
105
106 fds[i].revents = 0;
107
108 if (FD_ISSET(fds[i].fd, &rfds)) {
109 int err = errno;
110 int available = 0;
111 int ret;
112
113 /* support for POLLHUP */
114 ret = ioctl(fds[i].fd, FIONREAD, &available);
115 if ((ret == -1) || (available == 0)) {
116 fds[i].revents |= POLLHUP;
117 } else {
118 fds[i].revents |= fds[i].events
119 & (POLLIN | POLLRDNORM);
120 }
121
122 errno = err;
123 }
124 if (FD_ISSET(fds[i].fd, &wfds)) {
125 fds[i].revents |= fds[i].events
126 & (POLLOUT | POLLWRNORM | POLLWRBAND);
127 }
128 if (FD_ISSET(fds[i].fd, &efds)) {
129 fds[i].revents |= fds[i].events
130 & (POLLPRI | POLLRDBAND);
131 }
132#ifdef __OS2__ // if we only increment rc when no POLLHUB we loop
133 if (fds[i].revents != 0) {
134#else
135 if (fds[i].revents & ~POLLHUP) {
136#endif
137 rc++;
138 }
139 }
140 return rc;
141}
Note: See TracBrowser for help on using the repository browser.