1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/*
* Emulation of select() system call using poll() system call.
*
* Assumptions:
* polling for input only is most common.
* polling for exceptional conditions is very rare.
*
* Note that is it not feasible to emulate all error conditions,
* in particular conditions that would return EFAULT are far too
* difficult to check for in a library routine.
*/
#pragma weak _select = select
#include "lint.h"
#include <values.h>
#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/poll.h>
#include <alloca.h>
#include "libc.h"
/*
* STACK_PFD_LIM
*
* The limit at which pselect allocates pollfd structures in the heap,
* rather than on the stack. These limits match the historical behaviour
* with the * _large_fdset implementations.
*
* BULK_ALLOC_LIM
*
* The limit below which we'll just allocate nfds pollfds, rather than
* counting how many we actually need.
*/
#if defined(_LP64)
#define STACK_PFD_LIM FD_SETSIZE
#define BULK_ALLOC_LIM 8192
#else
#define STACK_PFD_LIM 1024
#define BULK_ALLOC_LIM 1024
#endif
/*
* The previous _large_fdset implementations are, unfortunately, baked into
* the ABI.
*/
#pragma weak select_large_fdset = select
#pragma weak pselect_large_fdset = pselect
#define fd_set_size(nfds) (((nfds) + (NFDBITS - 1)) / NFDBITS)
static nfds_t
fd_sets_count(int limit, fd_set *in, fd_set *out, fd_set *ex)
{
nfds_t total = 0;
if (limit <= 0)
return (0);
for (int i = 0; i < fd_set_size(limit); i++) {
long v = (in->fds_bits[i] | out->fds_bits[i] | ex->fds_bits[i]);
while (v != 0) {
v &= v - 1;
total++;
}
}
return (total);
}
int
pselect(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0,
const timespec_t *tsp, const sigset_t *sigmask)
{
long *in, *out, *ex;
ulong_t m; /* bit mask */
int j; /* loop counter */
ulong_t b; /* bits to test */
int n, rv;
struct pollfd *pfd;
struct pollfd *p;
int lastj = -1;
nfds_t npfds = 0;
boolean_t heap_pfds = B_FALSE;
/* "zero" is read-only, it could go in the text segment */
static fd_set zero = { 0 };
/*
* Check for invalid conditions at outset.
* Required for spec1170.
* SUSV3: We must behave as a cancellation point even if we fail early.
*/
if (nfds < 0 || nfds > FD_SETSIZE) {
pthread_testcancel();
errno = EINVAL;
return (-1);
}
if (tsp != NULL) {
/* check timespec validity */
if (tsp->tv_nsec < 0 || tsp->tv_nsec >= NANOSEC ||
tsp->tv_sec < 0) {
pthread_testcancel();
errno = EINVAL;
return (-1);
}
}
/*
* If any input args are null, point them at the null array.
*/
if (in0 == NULL)
in0 = &zero;
if (out0 == NULL)
out0 = &zero;
if (ex0 == NULL)
ex0 = &zero;
if (nfds <= BULK_ALLOC_LIM) {
p = pfd = alloca(nfds * sizeof (struct pollfd));
} else {
npfds = fd_sets_count(nfds, in0, out0, ex0);
if (npfds > STACK_PFD_LIM) {
p = pfd = malloc(npfds * sizeof (struct pollfd));
if (p == NULL)
return (-1);
heap_pfds = B_TRUE;
} else {
p = pfd = alloca(npfds * sizeof (struct pollfd));
}
}
/*
* For each fd, if any bits are set convert them into
* the appropriate pollfd struct.
*/
in = (long *)in0->fds_bits;
out = (long *)out0->fds_bits;
ex = (long *)ex0->fds_bits;
for (n = 0; n < nfds; n += NFDBITS) {
b = (ulong_t)(*in | *out | *ex);
for (j = 0, m = 1; b != 0; j++, b >>= 1, m <<= 1) {
if (b & 1) {
p->fd = n + j;
if (p->fd >= nfds)
goto done;
p->events = 0;
if (*in & m)
p->events |= POLLRDNORM;
if (*out & m)
p->events |= POLLWRNORM;
if (*ex & m)
p->events |= POLLRDBAND;
p++;
}
}
in++;
out++;
ex++;
}
done:
/*
* Now do the poll.
*/
npfds = (int)(p - pfd);
do {
rv = _pollsys(pfd, npfds, tsp, sigmask);
} while (rv < 0 && errno == EAGAIN);
if (rv < 0) /* no need to set bit masks */
goto out;
if (rv == 0) {
/*
* Clear out bit masks, just in case.
* On the assumption that usually only
* one bit mask is set, use three loops.
*/
if (in0 != &zero) {
in = (long *)in0->fds_bits;
for (n = 0; n < nfds; n += NFDBITS)
*in++ = 0;
}
if (out0 != &zero) {
out = (long *)out0->fds_bits;
for (n = 0; n < nfds; n += NFDBITS)
*out++ = 0;
}
if (ex0 != &zero) {
ex = (long *)ex0->fds_bits;
for (n = 0; n < nfds; n += NFDBITS)
*ex++ = 0;
}
rv = 0;
goto out;
}
/*
* Check for EINVAL error case first to avoid changing any bits
* if we're going to return an error.
*/
for (p = pfd, n = npfds; n-- > 0; p++) {
/*
* select will return EBADF immediately if any fd's
* are bad. poll will complete the poll on the
* rest of the fd's and include the error indication
* in the returned bits. This is a rare case so we
* accept this difference and return the error after
* doing more work than select would've done.
*/
if (p->revents & POLLNVAL) {
errno = EBADF;
rv = -1;
goto out;
}
/*
* We would like to make POLLHUP available to select,
* checking to see if we have pending data to be read.
* BUT until we figure out how not to break Xsun's
* dependencies on select's existing features...
* This is what we _thought_ would work ... sigh!
*/
/*
* if ((p->revents & POLLHUP) &&
* !(p->revents & (POLLRDNORM|POLLRDBAND))) {
* errno = EINTR;
* rv = -1;
* goto out;
* }
*/
}
/*
* Convert results of poll back into bits
* in the argument arrays.
*
* We assume POLLRDNORM, POLLWRNORM, and POLLRDBAND will only be set
* on return from poll if they were set on input, thus we don't
* worry about accidentally setting the corresponding bits in the
* zero array if the input bit masks were null.
*
* Must return number of bits set, not number of ready descriptors
* (as the man page says, and as poll() does).
*/
rv = 0;
for (p = pfd, n = npfds; n-- > 0; p++) {
j = (int)(p->fd / NFDBITS);
/* have we moved into another word of the bit mask yet? */
if (j != lastj) {
/* clear all output bits to start with */
in = (long *)&in0->fds_bits[j];
out = (long *)&out0->fds_bits[j];
ex = (long *)&ex0->fds_bits[j];
/*
* In case we made "zero" read-only (e.g., with
* cc -R), avoid actually storing into it.
*/
if (in0 != &zero)
*in = 0;
if (out0 != &zero)
*out = 0;
if (ex0 != &zero)
*ex = 0;
lastj = j;
}
if (p->revents) {
m = 1L << (p->fd % NFDBITS);
if (p->revents & POLLRDNORM) {
*in |= m;
rv++;
}
if (p->revents & POLLWRNORM) {
*out |= m;
rv++;
}
if (p->revents & POLLRDBAND) {
*ex |= m;
rv++;
}
/*
* Only set this bit on return if we asked about
* input conditions.
*/
if ((p->revents & (POLLHUP|POLLERR)) &&
(p->events & POLLRDNORM)) {
if ((*in & m) == 0)
rv++; /* wasn't already set */
*in |= m;
}
/*
* Only set this bit on return if we asked about
* output conditions.
*/
if ((p->revents & (POLLHUP|POLLERR)) &&
(p->events & POLLWRNORM)) {
if ((*out & m) == 0)
rv++; /* wasn't already set */
*out |= m;
}
/*
* Only set this bit on return if we asked about
* output conditions.
*/
if ((p->revents & (POLLHUP|POLLERR)) &&
(p->events & POLLRDBAND)) {
if ((*ex & m) == 0)
rv++; /* wasn't already set */
*ex |= m;
}
}
}
out:
if (heap_pfds)
free(pfd);
return (rv);
}
int
select(int nfds, fd_set *in0, fd_set *out0, fd_set *ex0, struct timeval *tv)
{
timespec_t ts;
timespec_t *tsp;
if (tv == NULL)
tsp = NULL;
else {
/* check timeval validity */
if (tv->tv_usec < 0 || tv->tv_usec >= MICROSEC) {
errno = EINVAL;
return (-1);
}
/*
* Convert timeval to timespec.
* To preserve compatibility with past behavior,
* when select was built upon poll(2), which has a
* minimum non-zero timeout of 1 millisecond, force
* a minimum non-zero timeout of 500 microseconds.
*/
ts.tv_sec = tv->tv_sec;
ts.tv_nsec = tv->tv_usec * 1000;
if (ts.tv_nsec != 0 && ts.tv_nsec < 500000)
ts.tv_nsec = 500000;
tsp = &ts;
}
return (pselect(nfds, in0, out0, ex0, tsp, NULL));
}
|