|
Added
Link Here
|
| 1 |
/* |
| 2 |
* Copyright 2013 Pierre Ossman for Cendio AB |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "includes.h" |
| 18 |
|
| 19 |
#include "log.h" |
| 20 |
|
| 21 |
#undef read |
| 22 |
#undef write |
| 23 |
|
| 24 |
#undef close |
| 25 |
|
| 26 |
#undef accept |
| 27 |
#undef bind |
| 28 |
#undef connect |
| 29 |
#undef gethostbyaddr |
| 30 |
#undef gethostbyname |
| 31 |
#undef gethostname |
| 32 |
#undef getpeername |
| 33 |
#undef getprotobyname |
| 34 |
#undef getservbyname |
| 35 |
#undef getsockopt |
| 36 |
#undef getsockname |
| 37 |
#undef listen |
| 38 |
#undef setsockopt |
| 39 |
#undef socket |
| 40 |
|
| 41 |
#undef select |
| 42 |
|
| 43 |
#ifdef WIN32 |
| 44 |
|
| 45 |
extern ssize_t win32_read_console(int fildes, void *buf, size_t nbyte); |
| 46 |
extern int win32_filter_console_events(HANDLE console); |
| 47 |
|
| 48 |
ssize_t |
| 49 |
win32_read(int fildes, void *buf, size_t nbyte) |
| 50 |
{ |
| 51 |
int ret; |
| 52 |
|
| 53 |
if (isatty(fildes)) |
| 54 |
return win32_read_console(fildes, buf, nbyte); |
| 55 |
|
| 56 |
ret = recv(fildes, buf, nbyte, 0); |
| 57 |
if (ret != SOCKET_ERROR) |
| 58 |
return ret; |
| 59 |
|
| 60 |
errno = WSAGetLastError(); |
| 61 |
|
| 62 |
if ((errno != WSAENOTSOCK) && (errno != WSAEBADF)) { |
| 63 |
errno |= WIN32_ERRNO; |
| 64 |
return -1; |
| 65 |
} |
| 66 |
|
| 67 |
return read(fildes, buf, nbyte); |
| 68 |
} |
| 69 |
|
| 70 |
ssize_t |
| 71 |
win32_write(int fildes, const void *buf, size_t nbyte) |
| 72 |
{ |
| 73 |
int ret; |
| 74 |
|
| 75 |
ret = send(fildes, buf, nbyte, 0); |
| 76 |
if (ret != SOCKET_ERROR) |
| 77 |
return ret; |
| 78 |
|
| 79 |
errno = WSAGetLastError(); |
| 80 |
|
| 81 |
if ((errno != WSAENOTSOCK) && (errno != WSAEBADF)) { |
| 82 |
errno |= WIN32_ERRNO; |
| 83 |
return -1; |
| 84 |
} |
| 85 |
|
| 86 |
return write(fildes, buf, nbyte); |
| 87 |
} |
| 88 |
|
| 89 |
int |
| 90 |
win32_close(int fd) |
| 91 |
{ |
| 92 |
int ret; |
| 93 |
|
| 94 |
if (win32_is_afunix(fd)) |
| 95 |
return win32_afunix_close(fd); |
| 96 |
|
| 97 |
ret = closesocket(fd); |
| 98 |
if (ret != SOCKET_ERROR) |
| 99 |
return ret; |
| 100 |
|
| 101 |
errno = WSAGetLastError(); |
| 102 |
|
| 103 |
if ((errno != WSAENOTSOCK) && (errno != WSAEBADF)) { |
| 104 |
errno |= WIN32_ERRNO; |
| 105 |
return -1; |
| 106 |
} |
| 107 |
|
| 108 |
return close(fd); |
| 109 |
} |
| 110 |
|
| 111 |
int |
| 112 |
win32_accept(int socket, struct sockaddr *address, socklen_t *address_len) |
| 113 |
{ |
| 114 |
SOCKET ret; |
| 115 |
|
| 116 |
if (win32_is_afunix(socket)) |
| 117 |
return win32_afunix_accept(socket, address, address_len); |
| 118 |
|
| 119 |
ret = accept(socket, address, address_len); |
| 120 |
if (ret == INVALID_SOCKET) { |
| 121 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
return ret; |
| 126 |
} |
| 127 |
|
| 128 |
int |
| 129 |
win32_bind(int socket, const struct sockaddr *address, |
| 130 |
socklen_t address_len) |
| 131 |
{ |
| 132 |
int ret; |
| 133 |
|
| 134 |
if (win32_is_afunix(socket)) |
| 135 |
return win32_afunix_bind(socket, address, address_len); |
| 136 |
|
| 137 |
ret = bind(socket, address, address_len); |
| 138 |
if (ret == SOCKET_ERROR) { |
| 139 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
int |
| 147 |
win32_connect(int socket, const struct sockaddr *address, |
| 148 |
socklen_t address_len) |
| 149 |
{ |
| 150 |
int ret; |
| 151 |
|
| 152 |
if (win32_is_afunix(socket)) |
| 153 |
return win32_afunix_connect(socket, address, address_len); |
| 154 |
|
| 155 |
ret = connect(socket, address, address_len); |
| 156 |
if (ret == SOCKET_ERROR) { |
| 157 |
/* |
| 158 |
* POSIX says that the proper errno for a connect() |
| 159 |
* that cannot complete immediately is EINPROGRESS. |
| 160 |
* Windows decides to send the standard EWOULDBLOCK. |
| 161 |
*/ |
| 162 |
if (WSAGetLastError() == WSAEWOULDBLOCK) |
| 163 |
errno = WSAEINPROGRESS | WIN32_ERRNO; |
| 164 |
else |
| 165 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 166 |
|
| 167 |
return -1; |
| 168 |
} |
| 169 |
|
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
struct hostent* |
| 174 |
win32_gethostbyaddr(const void *addr, socklen_t len, int type) |
| 175 |
{ |
| 176 |
struct hostent *ret; |
| 177 |
|
| 178 |
ret = gethostbyaddr(addr, len, type); |
| 179 |
if (ret == NULL) { |
| 180 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
return ret; |
| 185 |
} |
| 186 |
|
| 187 |
struct hostent* |
| 188 |
win32_gethostbyname(const char *name) |
| 189 |
{ |
| 190 |
struct hostent *ret; |
| 191 |
|
| 192 |
ret = gethostbyname(name); |
| 193 |
if (ret == NULL) { |
| 194 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
return ret; |
| 199 |
} |
| 200 |
|
| 201 |
int |
| 202 |
win32_gethostname(char *name, size_t len) |
| 203 |
{ |
| 204 |
int ret; |
| 205 |
|
| 206 |
ret = gethostname(name, len); |
| 207 |
if (ret == SOCKET_ERROR) { |
| 208 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 209 |
return -1; |
| 210 |
} |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
int |
| 216 |
win32_getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) |
| 217 |
{ |
| 218 |
int ret; |
| 219 |
|
| 220 |
ret = getpeername(sockfd, addr, addrlen); |
| 221 |
if (ret == SOCKET_ERROR) { |
| 222 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 223 |
return -1; |
| 224 |
} |
| 225 |
|
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
struct protoent* |
| 230 |
win32_getprotobyname(const char *name) |
| 231 |
{ |
| 232 |
struct protoent *ret; |
| 233 |
|
| 234 |
ret = getprotobyname(name); |
| 235 |
if (ret == NULL) { |
| 236 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 237 |
return NULL; |
| 238 |
} |
| 239 |
|
| 240 |
return ret; |
| 241 |
} |
| 242 |
|
| 243 |
struct servent* |
| 244 |
win32_getservbyname(const char *name, const char *proto) |
| 245 |
{ |
| 246 |
struct servent *ret; |
| 247 |
|
| 248 |
ret = getservbyname(name, proto); |
| 249 |
if (ret == NULL) { |
| 250 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 251 |
return NULL; |
| 252 |
} |
| 253 |
|
| 254 |
return ret; |
| 255 |
} |
| 256 |
|
| 257 |
int |
| 258 |
win32_getsockopt(int sockfd, int level, int optname, |
| 259 |
void *optval, socklen_t *optlen) |
| 260 |
{ |
| 261 |
int ret; |
| 262 |
|
| 263 |
ret = getsockopt(sockfd, level, optname, optval, optlen); |
| 264 |
if (ret == SOCKET_ERROR) { |
| 265 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 266 |
return -1; |
| 267 |
} |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
int |
| 273 |
win32_getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) |
| 274 |
{ |
| 275 |
int ret; |
| 276 |
|
| 277 |
ret = getsockname(sockfd, addr, addrlen); |
| 278 |
if (ret == SOCKET_ERROR) { |
| 279 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 280 |
return -1; |
| 281 |
} |
| 282 |
|
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
int |
| 287 |
win32_listen(int sockfd, int backlog) |
| 288 |
{ |
| 289 |
int ret; |
| 290 |
|
| 291 |
if (win32_is_afunix(sockfd)) |
| 292 |
return win32_afunix_listen(sockfd, backlog); |
| 293 |
|
| 294 |
ret = listen(sockfd, backlog); |
| 295 |
if (ret == SOCKET_ERROR) { |
| 296 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 297 |
return -1; |
| 298 |
} |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
int |
| 304 |
win32_setsockopt(int sockfd, int level, int optname, |
| 305 |
const void *optval, socklen_t optlen) |
| 306 |
{ |
| 307 |
int ret; |
| 308 |
|
| 309 |
ret = setsockopt(sockfd, level, optname, optval, optlen); |
| 310 |
if (ret == SOCKET_ERROR) { |
| 311 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 312 |
return -1; |
| 313 |
} |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
int |
| 319 |
win32_socket(int domain, int type, int protocol) |
| 320 |
{ |
| 321 |
SOCKET ret; |
| 322 |
|
| 323 |
if (domain == AF_UNIX) |
| 324 |
return win32_afunix_socket(domain, type, protocol); |
| 325 |
|
| 326 |
ret = socket(domain, type, protocol); |
| 327 |
if (ret == INVALID_SOCKET) { |
| 328 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 329 |
return -1; |
| 330 |
} |
| 331 |
|
| 332 |
return ret; |
| 333 |
} |
| 334 |
|
| 335 |
typedef struct fd_events { |
| 336 |
SOCKET fd; |
| 337 |
long events; |
| 338 |
} fd_events; |
| 339 |
|
| 340 |
typedef struct fd_event_set { |
| 341 |
u_int fd_count; |
| 342 |
fd_events fd_array[128]; |
| 343 |
} fd_event_set; |
| 344 |
|
| 345 |
static void |
| 346 |
build_fd_event(fd_event_set *events, fd_set *readfds, fd_set *writefds, |
| 347 |
fd_set *exceptfds) |
| 348 |
{ |
| 349 |
u_int i, j; |
| 350 |
fd_events *ev; |
| 351 |
|
| 352 |
events->fd_count = 0; |
| 353 |
|
| 354 |
if (readfds != NULL) { |
| 355 |
for (i = 0;i < readfds->fd_count;i++) { |
| 356 |
ev = &events->fd_array[events->fd_count++]; |
| 357 |
ev->fd = readfds->fd_array[i]; |
| 358 |
ev->events = FD_READ | FD_ACCEPT | FD_CLOSE; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
if (writefds != NULL) { |
| 363 |
for (i = 0;i < writefds->fd_count;i++) { |
| 364 |
for (j = 0;j < events->fd_count;j++) { |
| 365 |
ev = &events->fd_array[j]; |
| 366 |
if (ev->fd == writefds->fd_array[i]) |
| 367 |
break; |
| 368 |
} |
| 369 |
|
| 370 |
if (j == events->fd_count) { |
| 371 |
ev = &events->fd_array[events->fd_count++]; |
| 372 |
ev->fd = writefds->fd_array[i]; |
| 373 |
ev->events = 0; |
| 374 |
} |
| 375 |
|
| 376 |
ev->events |= FD_WRITE | FD_CONNECT; |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
static int |
| 382 |
win32_wait(fd_set *readfds, fd_set *writefds, |
| 383 |
fd_set *exceptfds, struct timeval *timeout) |
| 384 |
{ |
| 385 |
int ret; |
| 386 |
u_int i; |
| 387 |
|
| 388 |
struct timeval no_timeout = {0, 0}; |
| 389 |
|
| 390 |
fd_event_set events; |
| 391 |
WSAEVENT socket_event; |
| 392 |
|
| 393 |
int has_console, has_fifo; |
| 394 |
|
| 395 |
HANDLE handles[2]; |
| 396 |
DWORD dwtimeout, pipe_timeout; |
| 397 |
|
| 398 |
has_console = 0; |
| 399 |
has_fifo = 0; |
| 400 |
|
| 401 |
/* Start by assembling the three lists into one */ |
| 402 |
build_fd_event(&events, readfds, writefds, exceptfds); |
| 403 |
|
| 404 |
/* |
| 405 |
* Now we need to check for pre-existing states, which only |
| 406 |
* the good ol' select() can do. |
| 407 |
*/ |
| 408 |
if (readfds != NULL) { |
| 409 |
for (i = 0;i < readfds->fd_count;i++) { |
| 410 |
if (isatty(readfds->fd_array[i])) |
| 411 |
has_console = 1; |
| 412 |
else if (isafifo(readfds->fd_array[i])) |
| 413 |
has_fifo = 1; |
| 414 |
else |
| 415 |
continue; |
| 416 |
FD_CLR(readfds->fd_array[i], readfds); |
| 417 |
i--; |
| 418 |
} |
| 419 |
} |
| 420 |
if (writefds != NULL) { |
| 421 |
for (i = 0;i < writefds->fd_count;i++) { |
| 422 |
if (isatty(writefds->fd_array[i]) || |
| 423 |
isafifo(writefds->fd_array[i])) { |
| 424 |
FD_CLR(writefds->fd_array[i], writefds); |
| 425 |
i--; |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
if (exceptfds != NULL) { |
| 430 |
for (i = 0;i < exceptfds->fd_count;i++) { |
| 431 |
if (isatty(exceptfds->fd_array[i]) || |
| 432 |
isafifo(exceptfds->fd_array[i])) { |
| 433 |
FD_CLR(exceptfds->fd_array[i], exceptfds); |
| 434 |
i--; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
ret = select(0, readfds, writefds, exceptfds, &no_timeout); |
| 440 |
if (ret > 0) |
| 441 |
return ret; |
| 442 |
if (ret == SOCKET_ERROR) { |
| 443 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 444 |
return -1; |
| 445 |
} |
| 446 |
|
| 447 |
/* Don't need these anymore */ |
| 448 |
if (readfds != NULL) |
| 449 |
FD_ZERO(readfds); |
| 450 |
if (writefds != NULL) |
| 451 |
FD_ZERO(writefds); |
| 452 |
if (exceptfds != NULL) |
| 453 |
FD_ZERO(exceptfds); |
| 454 |
|
| 455 |
/* Connect an event to all sockets */ |
| 456 |
socket_event = WSACreateEvent(); |
| 457 |
|
| 458 |
for (i = 0;i < events.fd_count;i++) { |
| 459 |
if (isatty(events.fd_array[i].fd) || |
| 460 |
isafifo(events.fd_array[i].fd)) |
| 461 |
continue; |
| 462 |
|
| 463 |
ret = WSAEventSelect(events.fd_array[i].fd, socket_event, |
| 464 |
events.fd_array[i].events); |
| 465 |
if (ret == SOCKET_ERROR) { |
| 466 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 467 |
ret = -1; |
| 468 |
goto end; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
handles[0] = socket_event; |
| 473 |
handles[1] = GetStdHandle(STD_INPUT_HANDLE); |
| 474 |
|
| 475 |
if (timeout == NULL) |
| 476 |
dwtimeout = INFINITE; |
| 477 |
else |
| 478 |
dwtimeout = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; |
| 479 |
|
| 480 |
pipe_timeout = 100; |
| 481 |
|
| 482 |
/* |
| 483 |
* It is impossible to get events for pipes, so we have to resort |
| 484 |
* to polling if we have one in the list. We start by polling |
| 485 |
* rapidly and gradually back off to at least a 1 second delay. |
| 486 |
* This is done to quickly respond to interactive periods, yet |
| 487 |
* still sleep properly during idle times. |
| 488 |
*/ |
| 489 |
do { |
| 490 |
DWORD iter_timeout; |
| 491 |
|
| 492 |
iter_timeout = dwtimeout; |
| 493 |
|
| 494 |
if (has_fifo) { |
| 495 |
for (i = 0;i < events.fd_count;i++) { |
| 496 |
HANDLE handle; |
| 497 |
DWORD avail; |
| 498 |
|
| 499 |
if (!isafifo(events.fd_array[i].fd)) |
| 500 |
continue; |
| 501 |
|
| 502 |
if (!(events.fd_array[i].events & FD_READ)) |
| 503 |
continue; |
| 504 |
|
| 505 |
handle = (HANDLE)_get_osfhandle(events.fd_array[i].fd); |
| 506 |
if (!PeekNamedPipe(handle, NULL, 0, NULL, &avail, NULL)) { |
| 507 |
errno = GetLastError() | WIN32_ERRNO; |
| 508 |
ret = -1; |
| 509 |
goto end; |
| 510 |
} |
| 511 |
|
| 512 |
if (avail == 0) |
| 513 |
continue; |
| 514 |
|
| 515 |
FD_SET(events.fd_array[i].fd, readfds); |
| 516 |
} |
| 517 |
|
| 518 |
if (readfds->fd_count > 0) { |
| 519 |
ret = readfds->fd_count; |
| 520 |
goto end; |
| 521 |
} |
| 522 |
|
| 523 |
if (iter_timeout > pipe_timeout) |
| 524 |
iter_timeout = pipe_timeout; |
| 525 |
if (dwtimeout != INFINITE) |
| 526 |
dwtimeout -= iter_timeout; |
| 527 |
if (pipe_timeout < 1000) |
| 528 |
pipe_timeout *= 2; |
| 529 |
} |
| 530 |
|
| 531 |
/* Now do the actual wait...*/ |
| 532 |
ret = WaitForMultipleObjects(has_console ? 2 : 1, handles, |
| 533 |
FALSE, iter_timeout); |
| 534 |
} while (has_fifo && (ret == WAIT_TIMEOUT) && (dwtimeout > 0)); |
| 535 |
|
| 536 |
if (ret == WAIT_FAILED) { |
| 537 |
errno = GetLastError() | WIN32_ERRNO; |
| 538 |
ret = -1; |
| 539 |
goto end; |
| 540 |
} |
| 541 |
|
| 542 |
if (ret == WAIT_TIMEOUT) { |
| 543 |
ret = 0; |
| 544 |
goto end; |
| 545 |
} |
| 546 |
|
| 547 |
/* We got something on stdin... */ |
| 548 |
if (ret == (WAIT_OBJECT_0 + 1)) { |
| 549 |
/* |
| 550 |
* stdin will signal on a lot of non-data events, so |
| 551 |
* we need to filter out that crud. |
| 552 |
*/ |
| 553 |
ret = win32_filter_console_events(GetStdHandle(STD_INPUT_HANDLE)); |
| 554 |
if (ret <= 0) |
| 555 |
goto end; |
| 556 |
|
| 557 |
for (i = 0;i < events.fd_count;i++) { |
| 558 |
if (!isatty(events.fd_array[i].fd)) |
| 559 |
continue; |
| 560 |
|
| 561 |
if (!(events.fd_array[i].events & FD_READ)) |
| 562 |
continue; |
| 563 |
|
| 564 |
FD_SET(events.fd_array[i].fd, readfds); |
| 565 |
} |
| 566 |
|
| 567 |
ret = readfds->fd_count; |
| 568 |
goto end; |
| 569 |
} |
| 570 |
|
| 571 |
/* We got something on a socket. Figure out what... */ |
| 572 |
for (i = 0;i < events.fd_count;i++) { |
| 573 |
WSANETWORKEVENTS net_events; |
| 574 |
|
| 575 |
if (isatty(events.fd_array[i].fd) || |
| 576 |
isafifo(events.fd_array[i].fd)) |
| 577 |
continue; |
| 578 |
|
| 579 |
ret = WSAEnumNetworkEvents(events.fd_array[i].fd, |
| 580 |
socket_event, &net_events); |
| 581 |
if (ret == SOCKET_ERROR) { |
| 582 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 583 |
ret = -1; |
| 584 |
goto end; |
| 585 |
} |
| 586 |
|
| 587 |
if (readfds != NULL) { |
| 588 |
if (net_events.lNetworkEvents & events.fd_array[i].events & |
| 589 |
(FD_READ | FD_ACCEPT | FD_CLOSE)) |
| 590 |
FD_SET(events.fd_array[i].fd, readfds); |
| 591 |
} |
| 592 |
|
| 593 |
if (writefds != NULL) { |
| 594 |
if (net_events.lNetworkEvents & events.fd_array[i].events & |
| 595 |
(FD_WRITE | FD_CONNECT)) |
| 596 |
FD_SET(events.fd_array[i].fd, writefds); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
/* Sum up the number of signalled sockets */ |
| 601 |
ret = 0; |
| 602 |
if (readfds != NULL) |
| 603 |
ret += readfds->fd_count; |
| 604 |
if (writefds != NULL) |
| 605 |
ret += writefds->fd_count; |
| 606 |
if (exceptfds != NULL) |
| 607 |
ret += exceptfds->fd_count; |
| 608 |
|
| 609 |
end: |
| 610 |
/* |
| 611 |
* Undocumented misfeature of the week: having an event |
| 612 |
* connected to a socket makes accept() return WSEANOTSOCK. |
| 613 |
* Make sure we clear the event from all sockets before |
| 614 |
* returning. |
| 615 |
*/ |
| 616 |
for (i = 0;i < events.fd_count;i++) { |
| 617 |
int local_ret; |
| 618 |
|
| 619 |
if (isatty(events.fd_array[i].fd) || |
| 620 |
isafifo(events.fd_array[i].fd)) |
| 621 |
continue; |
| 622 |
|
| 623 |
local_ret = WSAEventSelect(events.fd_array[i].fd, NULL, 0); |
| 624 |
if (local_ret == SOCKET_ERROR) { |
| 625 |
error("WSAEventSelect(%d, NULL, 0): %s", |
| 626 |
events.fd_array[i].fd, |
| 627 |
win32_strerror(WSAGetLastError() | WIN32_ERRNO)); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
CloseHandle(socket_event); |
| 632 |
|
| 633 |
return ret; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
int |
| 638 |
win32_select(int nfds, fd_set *readfds, fd_set *writefds, |
| 639 |
fd_set *exceptfds, struct timeval *timeout) |
| 640 |
{ |
| 641 |
int ret; |
| 642 |
u_int i; |
| 643 |
|
| 644 |
/* |
| 645 |
* There is no select() equivalent for writing non-sockets so we need |
| 646 |
* to check for that first and assume they are always writable. |
| 647 |
*/ |
| 648 |
if (writefds != NULL) { |
| 649 |
for (i = 0;i < writefds->fd_count;i++) { |
| 650 |
if (isatty(writefds->fd_array[i]) || |
| 651 |
isafifo(writefds->fd_array[i])) |
| 652 |
break; |
| 653 |
} |
| 654 |
|
| 655 |
if (i != writefds->fd_count) { |
| 656 |
fd_set newwr; |
| 657 |
|
| 658 |
FD_ZERO(&newwr); |
| 659 |
|
| 660 |
for (;i < writefds->fd_count;i++) { |
| 661 |
if (isatty(writefds->fd_array[i]) || |
| 662 |
isafifo(writefds->fd_array[i])) |
| 663 |
FD_SET(writefds->fd_array[i], &newwr); |
| 664 |
} |
| 665 |
|
| 666 |
if (readfds != NULL) |
| 667 |
FD_ZERO(readfds); |
| 668 |
if (writefds != NULL) |
| 669 |
FD_ZERO(writefds); |
| 670 |
if (exceptfds != NULL) |
| 671 |
FD_ZERO(exceptfds); |
| 672 |
|
| 673 |
memcpy(writefds, &newwr, sizeof(fd_set)); |
| 674 |
|
| 675 |
return writefds->fd_count; |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/* Need to use more Windows specific APIs when dealing with non-sockets */ |
| 680 |
if (readfds != NULL) { |
| 681 |
for (i = 0;i < readfds->fd_count;i++) { |
| 682 |
if (isatty(readfds->fd_array[i]) || |
| 683 |
isafifo(readfds->fd_array[i])) |
| 684 |
return win32_wait(readfds, writefds, exceptfds, timeout); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
ret = select(nfds, readfds, writefds, exceptfds, timeout); |
| 689 |
|
| 690 |
if (ret == SOCKET_ERROR) |
| 691 |
errno = WSAGetLastError() | WIN32_ERRNO; |
| 692 |
|
| 693 |
return ret; |
| 694 |
} |
| 695 |
|
| 696 |
#endif /* WIN32 */ |