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

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

Samba Server: updated trunk to 3.6.9 2nd part

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