]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Uefi/select.c
StdLib/LibC: Fix transcription error from submitted patch.
[mirror_edk2.git] / StdLib / LibC / Uefi / select.c
1 /*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Portions copyright (c) 1999, 2000
11 * Intel Corporation.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 *
28 * This product includes software developed by the University of
29 * California, Berkeley, Intel Corporation, and its contributors.
30 *
31 * 4. Neither the name of University, Intel Corporation, or their respective
32 * contributors may be used to endorse or promote products derived from
33 * this software without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
36 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
37 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
38 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS,
39 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
45 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 *
47 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
48 * $Id: select.c,v 1.1.1.1 2003/11/19 01:50:30 kyu3 Exp $
49 */
50 #include <Library/UefiBootServicesTableLib.h>
51
52 #include <LibConfig.h>
53
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <string.h>
57 #include <sys/poll.h>
58 #include <sys/param.h>
59 #include <sys/time.h>
60 #include <extern.h> /* For ffs() */
61 #ifndef KERNEL
62 #define KERNEL
63 #include <errno.h>
64 #undef KERNEL
65 #else
66 #include <errno.h>
67 #endif
68
69 #ifdef EFI_NT_EMULATOR
70 #define _SELECT_DELAY_ 10000
71 #else
72 #define _SELECT_DELAY_ 1000
73 #endif
74
75 #define MAX_SLEEP_DELAY 0xfffffffe
76
77 /** Sleep for the specified number of Microseconds.
78
79 Implements the usleep(3) function.
80
81 @param[in] Microseconds Number of microseconds to sleep.
82
83 @retval 0 Always returns zero.
84 **/
85 int
86 usleep( useconds_t Microseconds )
87 {
88 while ( MAX_SLEEP_DELAY < Microseconds ) {
89 gBS->Stall ( MAX_SLEEP_DELAY );
90 Microseconds -= MAX_SLEEP_DELAY;
91 }
92 gBS->Stall((UINTN)Microseconds );
93 return (0);
94 }
95
96 unsigned int
97 sleep( unsigned int Seconds )
98 {
99 return (usleep( (useconds_t)(Seconds * 1000000) ));
100 }
101
102 static int
103 selscan(
104 fd_mask **ibits,
105 fd_mask **obits,
106 int nfd,
107 int *nselected
108 )
109 {
110 int msk;
111 int i;
112 int j;
113 int fd;
114 int n;
115 struct pollfd pfd;
116 int FdCount;
117 fd_mask bits;
118 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
119 static int16_t flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
120
121 for (msk = 0, n = 0; msk < 3; msk++) {
122 if (ibits[msk] == NULL)
123 continue;
124 for (i = 0; i < nfd; i += NFDBITS) {
125 bits = ibits[ msk ][ i / NFDBITS ];
126 while (( 0 != (j = ffs(bits))) && ((fd = i + --j) < nfd)) {
127 bits &= ~(1 << j);
128
129 pfd.fd = fd;
130 pfd.events = flag[msk];
131 pfd.revents = 0;
132 FdCount = poll ( &pfd, 1, 0 );
133 if ( -1 == FdCount ) {
134 return errno;
135 }
136 if ( 0 != FdCount ) {
137 obits[msk][(fd)/NFDBITS] |=
138 (1 << ((fd) % NFDBITS));
139 n++;
140 break;
141 }
142 }
143 }
144 }
145 *nselected = n;
146 return (0);
147 }
148
149 int
150 select(
151 int nd,
152 fd_set *in,
153 fd_set *ou,
154 fd_set *ex,
155 struct timeval *tv
156 )
157 {
158 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
159 int error, forever, nselected;
160 u_int nbufbytes, ncpbytes, nfdbits;
161 int64_t timo;
162
163 if (nd < 0)
164 return (EINVAL);
165
166 /*
167 * Allocate just enough bits for the non-null fd_sets. Use the
168 * preallocated auto buffer if possible.
169 */
170 nfdbits = roundup(nd, NFDBITS);
171 ncpbytes = nfdbits / NBBY;
172 nbufbytes = 0;
173 if (in != NULL)
174 nbufbytes += 2 * ncpbytes;
175 if (ou != NULL)
176 nbufbytes += 2 * ncpbytes;
177 if (ex != NULL)
178 nbufbytes += 2 * ncpbytes;
179 selbits = malloc(nbufbytes);
180
181 /*
182 * Assign pointers into the bit buffers and fetch the input bits.
183 * Put the output buffers together so that they can be bzeroed
184 * together.
185 */
186 sbp = selbits;
187 #define getbits(name, x) \
188 do { \
189 if (name == NULL) \
190 ibits[x] = NULL; \
191 else { \
192 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
193 obits[x] = sbp; \
194 sbp += ncpbytes / sizeof *sbp; \
195 bcopy(name, ibits[x], ncpbytes); \
196 } \
197 } while (0)
198 getbits(in, 0);
199 getbits(ou, 1);
200 getbits(ex, 2);
201 #undef getbits
202 if (nbufbytes != 0)
203 memset(selbits, 0, nbufbytes / 2);
204
205 if (tv) {
206 timo = tv->tv_usec + (tv->tv_sec * 1000000);
207 forever = 0;
208 } else {
209 timo = 0;
210 forever = 1;
211 }
212
213 /*
214 * Poll for I/O events
215 */
216 nselected = 0;
217 do {
218 /*
219 * Scan for pending I/O
220 */
221 error = selscan(ibits, obits, nd, &nselected);
222 if (error || nselected)
223 break;
224
225 /*
226 * Adjust timeout is needed
227 */
228 if (timo) {
229 /*
230 * Give it a rest
231 */
232 usleep( _SELECT_DELAY_ );
233 timo -= _SELECT_DELAY_;
234 }
235
236 } while (timo > 0 || forever);
237
238 /* select is not restarted after signals... */
239 if (error == ERESTART)
240 error = EINTR;
241 else if (error == EWOULDBLOCK)
242 error = 0;
243
244 #define putbits(name, x) if (name) bcopy(obits[x], name, ncpbytes)
245 if (error == 0) {
246 putbits(in, 0);
247 putbits(ou, 1);
248 putbits(ex, 2);
249 #undef putbits
250 } else {
251 errno = error;
252 nselected = -1;
253 }
254
255 free( selbits );
256 return ( nselected );
257 }