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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
|
/*
* OpenVPN -- An application to securely tunnel IP networks
* over a single TCP/UDP port, with support for SSL/TLS-based
* session authentication and key exchange,
* packet encryption, packet authentication, and
* packet compression.
*
* Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef WIN32
#include "config-win32.h"
#else
#include "config.h"
#endif
#include "syshead.h"
#include "buffer.h"
#include "error.h"
#include "integer.h"
#include "event.h"
#include "memdbg.h"
/*
* Some OSes will prefer select() over poll()
* when both are available.
*/
#if defined(TARGET_DARWIN)
#define SELECT_PREFERRED_OVER_POLL
#endif
/*
* All non-windows OSes are assumed to have select()
*/
#ifdef WIN32
#define SELECT 0
#else
#define SELECT 1
#endif
/*
* This should be set to the highest file descriptor
* which can be used in one of the FD_ macros.
*/
#ifdef FD_SETSIZE
#define SELECT_MAX_FDS FD_SETSIZE
#else
#define SELECT_MAX_FDS 256
#endif
static inline int
tv_to_ms_timeout (const struct timeval *tv)
{
if (tv->tv_sec == 0 && tv->tv_usec == 0)
return 0;
else
return max_int (tv->tv_sec * 1000 + (tv->tv_usec + 500) / 1000, 1);
}
#ifdef WIN32
struct we_set
{
struct event_set_functions func;
bool fast;
HANDLE *events;
struct event_set_return *esr;
int n_events;
int capacity;
};
static inline void
we_set_event (struct we_set *wes, int i, event_t event, unsigned int rwflags, void *arg)
{
ASSERT (i >= 0 && i < wes->capacity);
if (rwflags == EVENT_READ)
{
ASSERT (event->read != NULL);
wes->events[i] = event->read;
}
else if (rwflags == EVENT_WRITE)
{
ASSERT (event->write != NULL);
wes->events[i] = event->write;
}
else
msg (M_FATAL, "fatal error in we_set_events: rwflags=%d", rwflags);
wes->esr[i].rwflags = rwflags;
wes->esr[i].arg = arg;
}
static inline bool
we_append_event (struct we_set *wes, event_t event, unsigned int rwflags, void *arg)
{
if (rwflags & EVENT_WRITE)
{
if (wes->n_events < wes->capacity)
{
we_set_event (wes, wes->n_events, event, EVENT_WRITE, arg);
++wes->n_events;
}
else
return false;
}
if (rwflags & EVENT_READ)
{
if (wes->n_events < wes->capacity)
{
we_set_event (wes, wes->n_events, event, EVENT_READ, arg);
++wes->n_events;
}
else
return false;
}
return true;
}
static void
we_del_event (struct we_set *wes, event_t event)
{
int i, j = 0;
const int len = wes->n_events;
for (i = 0; i < len; ++i)
{
const HANDLE h = wes->events[i];
if (h == event->read || h == event->write)
--wes->n_events;
else
{
if (i != j)
{
wes->events[j] = wes->events[i];
wes->esr[j] = wes->esr[i];
}
++j;
}
}
}
static void
we_del_index (struct we_set *wes, int index)
{
int i;
ASSERT (index >= 0 && index < wes->n_events);
for (i = index; i < wes->n_events - 1; ++i)
{
wes->events[i] = wes->events[i+1];
wes->esr[i] = wes->esr[i+1];
}
--wes->n_events;
}
static void
we_get_rw_indices (struct we_set *wes, event_t event, int *ri, int *wi)
{
int i;
*ri = *wi = -1;
for (i = 0; i < wes->n_events; ++i)
{
const HANDLE h = wes->events[i];
if (h == event->read)
{
ASSERT (*ri == -1);
*ri = i;
}
else if (h == event->write)
{
ASSERT (*wi == -1);
*wi = i;
}
}
}
static void
we_free (struct event_set *es)
{
struct we_set *wes = (struct we_set *) es;
free (wes->events);
free (wes->esr);
free (wes);
}
static void
we_reset (struct event_set *es)
{
struct we_set *wes = (struct we_set *) es;
ASSERT (wes->fast);
wes->n_events = 0;
}
static void
we_del (struct event_set *es, event_t event)
{
struct we_set *wes = (struct we_set *) es;
ASSERT (!wes->fast);
we_del_event (wes, event);
}
static void
we_ctl (struct event_set *es, event_t event, unsigned int rwflags, void *arg)
{
struct we_set *wes = (struct we_set *) es;
dmsg (D_EVENT_WAIT, "WE_CTL n=%d ev=0x%08x rwflags=0x%04x arg=" ptr_format,
wes->n_events,
(unsigned int)event,
rwflags,
(ptr_type)arg);
if (wes->fast)
{
if (!we_append_event (wes, event, rwflags, arg))
goto err;
}
else
{
int ri, wi;
int one = -1;
int n = 0;
we_get_rw_indices (wes, event, &ri, &wi);
if (wi >= 0)
{
one = wi;
++n;
}
if (ri >= 0)
{
one = ri;
++n;
}
switch (rwflags)
{
case 0:
switch (n)
{
case 0:
break;
case 1:
we_del_index (wes, one);
break;
case 2:
we_del_event (wes, event);
break;
default:
ASSERT (0);
}
break;
case EVENT_READ:
switch (n)
{
case 0:
if (!we_append_event (wes, event, EVENT_READ, arg))
goto err;
break;
case 1:
we_set_event (wes, one, event, EVENT_READ, arg);
break;
case 2:
we_del_index (wes, wi);
break;
default:
ASSERT (0);
}
break;
case EVENT_WRITE:
switch (n)
{
case 0:
if (!we_append_event (wes, event, EVENT_WRITE, arg))
goto err;
break;
case 1:
we_set_event (wes, one, event, EVENT_WRITE, arg);
break;
case 2:
we_del_index (wes, ri);
break;
default:
ASSERT (0);
}
break;
case EVENT_READ|EVENT_WRITE:
switch (n)
{
case 0:
if (!we_append_event (wes, event, EVENT_READ|EVENT_WRITE, arg))
goto err;
break;
case 1:
if (ri == -1)
{
ASSERT (wi != -1);
if (!we_append_event (wes, event, EVENT_READ, arg))
goto err;
}
else if (wi == -1)
{
if (!we_append_event (wes, event, EVENT_WRITE, arg))
goto err;
}
else
ASSERT (0);
break;
case 2:
break;
default:
ASSERT (0);
}
break;
default:
msg (M_FATAL, "fatal error in we_ctl: rwflags=%d", rwflags);
}
}
return;
err:
msg (D_EVENT_ERRORS, "Error: Windows resource limit WSA_MAXIMUM_WAIT_EVENTS (%d) has been exceeded", WSA_MAXIMUM_WAIT_EVENTS);
}
static int
we_wait (struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
{
struct we_set *wes = (struct we_set *) es;
const int timeout = tv_to_ms_timeout (tv);
DWORD status;
dmsg (D_EVENT_WAIT, "WE_WAIT enter n=%d to=%d", wes->n_events, timeout);
#ifdef ENABLE_DEBUG
if (check_debug_level (D_EVENT_WAIT)) {
int i;
for (i = 0; i < wes->n_events; ++i)
dmsg (D_EVENT_WAIT, "[%d] ev=0x%08x rwflags=0x%04x arg=" ptr_format,
i,
(unsigned int)wes->events[i],
wes->esr[i].rwflags,
(ptr_type)wes->esr[i].arg);
}
#endif
/*
* First poll our event list with 0 timeout
*/
status = WSAWaitForMultipleEvents(
(DWORD) wes->n_events,
wes->events,
FALSE,
(DWORD) 0,
FALSE);
/*
* If at least one event is already set, we must
* individually poll the whole list.
*/
if (status >= WSA_WAIT_EVENT_0 && status < WSA_WAIT_EVENT_0 + (DWORD) wes->n_events)
{
int i;
int j = 0;
for (i = 0; i < wes->n_events; ++i)
{
if (j >= outlen)
break;
if (WaitForSingleObject (wes->events[i], 0) == WAIT_OBJECT_0)
{
*out = wes->esr[i];
dmsg (D_EVENT_WAIT, "WE_WAIT leave [%d,%d] rwflags=0x%04x arg=" ptr_format,
i, j, out->rwflags, (ptr_type)out->arg);
++j;
++out;
}
}
return j;
}
else
{
/*
* If caller specified timeout > 0, we know at this point
* that no events are set, so wait only for the first event
* (or timeout) and return at most one event_set_return object.
*
* If caller specified timeout == 0, the second call to
* WSAWaitForMultipleEvents would be redundant -- just
* return 0 indicating timeout.
*/
if (timeout > 0)
status = WSAWaitForMultipleEvents(
(DWORD) wes->n_events,
wes->events,
FALSE,
(DWORD) timeout,
FALSE);
if (outlen >= 1 && status >= WSA_WAIT_EVENT_0 && status < WSA_WAIT_EVENT_0 + (DWORD) wes->n_events)
{
*out = wes->esr[status - WSA_WAIT_EVENT_0];
dmsg (D_EVENT_WAIT, "WE_WAIT leave rwflags=0x%04x arg=" ptr_format,
out->rwflags, (ptr_type)out->arg);
return 1;
}
else if (status == WSA_WAIT_TIMEOUT)
return 0;
else
return -1;
}
}
static struct event_set *
we_init (int *maxevents, unsigned int flags)
{
struct we_set *wes;
dmsg (D_EVENT_WAIT, "WE_INIT maxevents=%d flags=0x%08x", *maxevents, flags);
ALLOC_OBJ_CLEAR (wes, struct we_set);
/* set dispatch functions */
wes->func.free = we_free;
wes->func.reset = we_reset;
wes->func.del = we_del;
wes->func.ctl = we_ctl;
wes->func.wait = we_wait;
if (flags & EVENT_METHOD_FAST)
wes->fast = true;
wes->n_events = 0;
/* Figure our event capacity */
ASSERT (*maxevents > 0);
wes->capacity = min_int (*maxevents * 2, WSA_MAXIMUM_WAIT_EVENTS);
*maxevents = min_int (*maxevents, WSA_MAXIMUM_WAIT_EVENTS);
/* Allocate space for Win32 event handles */
ALLOC_ARRAY_CLEAR (wes->events, HANDLE, wes->capacity);
/* Allocate space for event_set_return objects */
ALLOC_ARRAY_CLEAR (wes->esr, struct event_set_return, wes->capacity);
dmsg (D_EVENT_WAIT, "WE_INIT maxevents=%d capacity=%d",
*maxevents, wes->capacity);
return (struct event_set *) wes;
}
#endif /* WIN32 */
#if EPOLL
struct ep_set
{
struct event_set_functions func;
bool fast;
int epfd;
int maxevents;
struct epoll_event *events;
};
static void
ep_free (struct event_set *es)
{
struct ep_set *eps = (struct ep_set *) es;
close (eps->epfd);
free (eps->events);
free (eps);
}
static void
ep_reset (struct event_set *es)
{
const struct ep_set *eps = (struct ep_set *) es;
ASSERT (eps->fast);
}
static void
ep_del (struct event_set *es, event_t event)
{
struct epoll_event ev;
struct ep_set *eps = (struct ep_set *) es;
dmsg (D_EVENT_WAIT, "EP_DEL ev=%d", (int)event);
ASSERT (!eps->fast);
CLEAR (ev);
epoll_ctl (eps->epfd, EPOLL_CTL_DEL, event, &ev);
}
static void
ep_ctl (struct event_set *es, event_t event, unsigned int rwflags, void *arg)
{
struct ep_set *eps = (struct ep_set *) es;
struct epoll_event ev;
CLEAR (ev);
ev.data.ptr = arg;
if (rwflags & EVENT_READ)
ev.events |= EPOLLIN;
if (rwflags & EVENT_WRITE)
ev.events |= EPOLLOUT;
dmsg (D_EVENT_WAIT, "EP_CTL fd=%d rwflags=0x%04x ev=0x%08x arg=" ptr_format,
(int)event,
rwflags,
(unsigned int)ev.events,
(ptr_type)ev.data.ptr);
if (epoll_ctl (eps->epfd, EPOLL_CTL_MOD, event, &ev) < 0)
{
if (errno == ENOENT)
{
if (epoll_ctl (eps->epfd, EPOLL_CTL_ADD, event, &ev) < 0)
msg (M_ERR, "EVENT: epoll_ctl EPOLL_CTL_ADD failed");
}
else
msg (M_ERR, "EVENT: epoll_ctl EPOLL_CTL_MOD failed");
}
}
static int
ep_wait (struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
{
struct ep_set *eps = (struct ep_set *) es;
int stat;
if (outlen > eps->maxevents)
outlen = eps->maxevents;
stat = epoll_wait (eps->epfd, eps->events, outlen, tv_to_ms_timeout (tv));
ASSERT (stat <= outlen);
if (stat > 0)
{
int i;
const struct epoll_event *ev = eps->events;
struct event_set_return *esr = out;
for (i = 0; i < stat; ++i)
{
esr->rwflags = 0;
if (ev->events & (EPOLLIN|EPOLLPRI|EPOLLERR))
esr->rwflags |= EVENT_READ;
if (ev->events & EPOLLOUT)
esr->rwflags |= EVENT_WRITE;
esr->arg = ev->data.ptr;
dmsg (D_EVENT_WAIT, "EP_WAIT[%d] rwflags=0x%04x ev=0x%08x arg=" ptr_format,
i, esr->rwflags, ev->events, (ptr_type)ev->data.ptr);
++ev;
++esr;
}
}
return stat;
}
static struct event_set *
ep_init (int *maxevents, unsigned int flags)
{
struct ep_set *eps;
int fd;
dmsg (D_EVENT_WAIT, "EP_INIT maxevents=%d flags=0x%08x", *maxevents, flags);
/* open epoll file descriptor */
fd = epoll_create (*maxevents);
if (fd < 0)
return NULL;
ALLOC_OBJ_CLEAR (eps, struct ep_set);
/* set dispatch functions */
eps->func.free = ep_free;
eps->func.reset = ep_reset;
eps->func.del = ep_del;
eps->func.ctl = ep_ctl;
eps->func.wait = ep_wait;
/* fast method ("sort of") corresponds to epoll one-shot */
if (flags & EVENT_METHOD_FAST)
eps->fast = true;
/* allocate space for epoll_wait return */
ASSERT (*maxevents > 0);
eps->maxevents = *maxevents;
ALLOC_ARRAY_CLEAR (eps->events, struct epoll_event, eps->maxevents);
/* set epoll control fd */
eps->epfd = fd;
return (struct event_set *) eps;
}
#endif /* EPOLL */
#if POLL
struct po_set
{
struct event_set_functions func;
bool fast;
struct pollfd *events;
void **args;
int n_events;
int capacity;
};
static void
po_free (struct event_set *es)
{
struct po_set *pos = (struct po_set *) es;
free (pos->events);
free (pos->args);
free (pos);
}
static void
po_reset (struct event_set *es)
{
struct po_set *pos = (struct po_set *) es;
ASSERT (pos->fast);
pos->n_events = 0;
}
static void
po_del (struct event_set *es, event_t event)
{
struct po_set *pos = (struct po_set *) es;
int i;
dmsg (D_EVENT_WAIT, "PO_DEL ev=%d", (int)event);
ASSERT (!pos->fast);
for (i = 0; i < pos->n_events; ++i)
{
if (pos->events[i].fd == event)
{
int j;
for (j = i; j < pos->n_events - 1; ++j)
{
pos->events[j] = pos->events[j+1];
pos->args[j] = pos->args[j+1];
}
--pos->n_events;
break;
}
}
}
static inline void
po_set_pollfd_events (struct pollfd *pfdp, unsigned int rwflags)
{
pfdp->events = 0;
if (rwflags & EVENT_WRITE)
pfdp->events |= POLLOUT;
if (rwflags & EVENT_READ)
pfdp->events |= (POLLIN|POLLPRI);
}
static inline bool
po_append_event (struct po_set *pos, event_t event, unsigned int rwflags, void *arg)
{
if (pos->n_events < pos->capacity)
{
struct pollfd *pfdp = &pos->events[pos->n_events];
pfdp->fd = event;
pos->args[pos->n_events] = arg;
po_set_pollfd_events (pfdp, rwflags);
++pos->n_events;
return true;
}
else
return false;
}
static void
po_ctl (struct event_set *es, event_t event, unsigned int rwflags, void *arg)
{
struct po_set *pos = (struct po_set *) es;
dmsg (D_EVENT_WAIT, "PO_CTL rwflags=0x%04x ev=%d arg=" ptr_format,
rwflags, (int)event, (ptr_type)arg);
if (pos->fast)
{
if (!po_append_event (pos, event, rwflags, arg))
goto err;
}
else
{
int i;
for (i = 0; i < pos->n_events; ++i)
{
struct pollfd *pfdp = &pos->events[i];
if (pfdp->fd == event)
{
pos->args[i] = arg;
po_set_pollfd_events (pfdp, rwflags);
goto done;
}
}
if (!po_append_event (pos, event, rwflags, arg))
goto err;
}
done:
return;
err:
msg (D_EVENT_ERRORS, "Error: poll: too many I/O wait events");
}
static int
po_wait (struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
{
struct po_set *pos = (struct po_set *) es;
int stat;
stat = poll (pos->events, pos->n_events, tv_to_ms_timeout (tv));
ASSERT (stat <= pos->n_events);
if (stat > 0)
{
int i, j=0;
const struct pollfd *pfdp = pos->events;
for (i = 0; i < pos->n_events && j < outlen; ++i)
{
if (pfdp->revents & (POLLIN|POLLPRI|POLLERR|POLLHUP|POLLOUT))
{
out->rwflags = 0;
if (pfdp->revents & (POLLIN|POLLPRI|POLLERR|POLLHUP))
out->rwflags |= EVENT_READ;
if (pfdp->revents & POLLOUT)
out->rwflags |= EVENT_WRITE;
out->arg = pos->args[i];
dmsg (D_EVENT_WAIT, "PO_WAIT[%d,%d] fd=%d rev=0x%08x rwflags=0x%04x arg=" ptr_format " %s",
i, j, pfdp->fd, pfdp->revents, out->rwflags, (ptr_type)out->arg, pos->fast ? "" : "[scalable]");
++out;
++j;
}
else if (pfdp->revents)
{
msg (D_EVENT_ERRORS, "Error: poll: unknown revents=0x%04x", (unsigned int)pfdp->revents);
}
++pfdp;
}
return j;
}
return stat;
}
static struct event_set *
po_init (int *maxevents, unsigned int flags)
{
struct po_set *pos;
dmsg (D_EVENT_WAIT, "PO_INIT maxevents=%d flags=0x%08x", *maxevents, flags);
ALLOC_OBJ_CLEAR (pos, struct po_set);
/* set dispatch functions */
pos->func.free = po_free;
pos->func.reset = po_reset;
pos->func.del = po_del;
pos->func.ctl = po_ctl;
pos->func.wait = po_wait;
if (flags & EVENT_METHOD_FAST)
pos->fast = true;
pos->n_events = 0;
/* Figure our event capacity */
ASSERT (*maxevents > 0);
pos->capacity = *maxevents;
/* Allocate space for pollfd structures to be passed to poll() */
ALLOC_ARRAY_CLEAR (pos->events, struct pollfd, pos->capacity);
/* Allocate space for event_set_return objects */
ALLOC_ARRAY_CLEAR (pos->args, void *, pos->capacity);
return (struct event_set *) pos;
}
#endif /* POLL */
#if SELECT
struct se_set
{
struct event_set_functions func;
bool fast;
fd_set readfds;
fd_set writefds;
void **args; /* allocated to capacity size */
int maxfd; /* largest fd seen so far, always < capacity */
int capacity; /* fixed largest fd + 1 */
};
static void
se_free (struct event_set *es)
{
struct se_set *ses = (struct se_set *) es;
free (ses->args);
free (ses);
}
static void
se_reset (struct event_set *es)
{
struct se_set *ses = (struct se_set *) es;
int i;
ASSERT (ses->fast);
dmsg (D_EVENT_WAIT, "SE_RESET");
FD_ZERO (&ses->readfds);
FD_ZERO (&ses->writefds);
for (i = 0; i <= ses->maxfd; ++i)
ses->args[i] = NULL;
ses->maxfd = -1;
}
static void
se_del (struct event_set *es, event_t event)
{
struct se_set *ses = (struct se_set *) es;
ASSERT (!ses->fast);
dmsg (D_EVENT_WAIT, "SE_DEL ev=%d", (int)event);
if (event >= 0 && event < ses->capacity)
{
FD_CLR (event, &ses->readfds);
FD_CLR (event, &ses->writefds);
ses->args[event] = NULL;
}
else
msg (D_EVENT_ERRORS, "Error: select/se_del: too many I/O wait events");
return;
}
static void
se_ctl (struct event_set *es, event_t event, unsigned int rwflags, void *arg)
{
struct se_set *ses = (struct se_set *) es;
dmsg (D_EVENT_WAIT, "SE_CTL rwflags=0x%04x ev=%d fast=%d cap=%d maxfd=%d arg=" ptr_format,
rwflags, (int)event, (int)ses->fast, ses->capacity, ses->maxfd, (ptr_type)arg);
if (event >= 0 && event < ses->capacity)
{
ses->maxfd = max_int (event, ses->maxfd);
ses->args[event] = arg;
if (ses->fast)
{
if (rwflags & EVENT_READ)
FD_SET (event, &ses->readfds);
if (rwflags & EVENT_WRITE)
FD_SET (event, &ses->writefds);
}
else
{
if (rwflags & EVENT_READ)
FD_SET (event, &ses->readfds);
else
FD_CLR (event, &ses->readfds);
if (rwflags & EVENT_WRITE)
FD_SET (event, &ses->writefds);
else
FD_CLR (event, &ses->writefds);
}
}
else
{
msg (D_EVENT_ERRORS, "Error: select: too many I/O wait events, fd=%d cap=%d",
(int) event,
ses->capacity);
}
}
static int
se_wait_return (struct se_set *ses,
fd_set *read,
fd_set *write,
struct event_set_return *out,
int outlen)
{
int i, j = 0;
for (i = 0; i <= ses->maxfd && j < outlen; ++i)
{
const bool r = FD_ISSET (i, read);
const bool w = FD_ISSET (i, write);
if (r || w)
{
out->rwflags = 0;
if (r)
out->rwflags |= EVENT_READ;
if (w)
out->rwflags |= EVENT_WRITE;
out->arg = ses->args[i];
dmsg (D_EVENT_WAIT, "SE_WAIT[%d,%d] rwflags=0x%04x arg=" ptr_format,
i, j, out->rwflags, (ptr_type)out->arg);
++out;
++j;
}
}
return j;
}
static int
se_wait_fast (struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
{
struct se_set *ses = (struct se_set *) es;
struct timeval tv_tmp = *tv;
int stat;
dmsg (D_EVENT_WAIT, "SE_WAIT_FAST maxfd=%d tv=%d/%d",
ses->maxfd,
(int)tv_tmp.tv_sec,
(int)tv_tmp.tv_usec);
stat = select (ses->maxfd + 1, &ses->readfds, &ses->writefds, NULL, &tv_tmp);
if (stat > 0)
stat = se_wait_return (ses, &ses->readfds, &ses->writefds, out, outlen);
return stat;
}
static int
se_wait_scalable (struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
{
struct se_set *ses = (struct se_set *) es;
struct timeval tv_tmp = *tv;
fd_set read = ses->readfds;
fd_set write = ses->writefds;
int stat;
dmsg (D_EVENT_WAIT, "SE_WAIT_SCALEABLE maxfd=%d tv=%d/%d",
ses->maxfd, (int)tv_tmp.tv_sec, (int)tv_tmp.tv_usec);
stat = select (ses->maxfd + 1, &read, &write, NULL, &tv_tmp);
if (stat > 0)
stat = se_wait_return (ses, &read, &write, out, outlen);
return stat;
}
static struct event_set *
se_init (int *maxevents, unsigned int flags)
{
struct se_set *ses;
dmsg (D_EVENT_WAIT, "SE_INIT maxevents=%d flags=0x%08x", *maxevents, flags);
ALLOC_OBJ_CLEAR (ses, struct se_set);
/* set dispatch functions */
ses->func.free = se_free;
ses->func.reset = se_reset;
ses->func.del = se_del;
ses->func.ctl = se_ctl;
ses->func.wait = se_wait_scalable;
if (flags & EVENT_METHOD_FAST)
{
ses->fast = true;
ses->func.wait = se_wait_fast;
}
/* Select needs to be passed this value + 1 */
ses->maxfd = -1;
/* Set our event capacity */
ASSERT (*maxevents > 0);
*maxevents = min_int (*maxevents, SELECT_MAX_FDS);
ses->capacity = SELECT_MAX_FDS;
/* Allocate space for event_set_return void * args */
ALLOC_ARRAY_CLEAR (ses->args, void *, ses->capacity);
return (struct event_set *) ses;
}
#endif /* SELECT */
static struct event_set *
event_set_init_simple (int *maxevents, unsigned int flags)
{
struct event_set *ret = NULL;
#ifdef WIN32
ret = we_init (maxevents, flags);
#elif POLL && SELECT
#if 0 /* Define to 1 if EVENT_METHOD_US_TIMEOUT should cause select to be favored over poll */
if (flags & EVENT_METHOD_US_TIMEOUT)
ret = se_init (maxevents, flags);
#endif
# ifdef SELECT_PREFERRED_OVER_POLL
if (!ret)
ret = se_init (maxevents, flags);
if (!ret)
ret = po_init (maxevents, flags);
# else
if (!ret)
ret = po_init (maxevents, flags);
if (!ret)
ret = se_init (maxevents, flags);
# endif
#elif POLL
ret = po_init (maxevents, flags);
#elif SELECT
ret = se_init (maxevents, flags);
#else
#error At least one of poll, select, or WSAWaitForMultipleEvents must be supported by the kernel
#endif
ASSERT (ret);
return ret;
}
static struct event_set *
event_set_init_scalable (int *maxevents, unsigned int flags)
{
struct event_set *ret = NULL;
#if EPOLL
ret = ep_init (maxevents, flags);
if (!ret)
{
msg (M_WARN, "Note: sys_epoll API is unavailable, falling back to poll/select API");
ret = event_set_init_simple (maxevents, flags);
}
#else
ret = event_set_init_simple (maxevents, flags);
#endif
ASSERT (ret);
return ret;
}
struct event_set *
event_set_init (int *maxevents, unsigned int flags)
{
if (flags & EVENT_METHOD_FAST)
return event_set_init_simple (maxevents, flags);
else
return event_set_init_scalable (maxevents, flags);
}
|