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
|
/*
* $Id: io_wait.c,v 1.1 2006/01/09 20:31:54 bogdan_iancu Exp $
*
* Copyright (C) 2005 iptelorg GmbH
*
* This file is part of openser, a free SIP server.
*
* openser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* openser 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; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* tcp io wait common stuff used by tcp_main.c & tcp_read.c
* (see io_wait.h)
*/
/*
* History:
* --------
* 2005-06-15 created by andrei
* 2005-06-26 added kqueue (andrei)
* 2005-07-04 added /dev/poll (andrei)
*/
#ifdef USE_TCP /* for now it make sense only with tcp */
#ifdef HAVE_EPOLL
#include <unistd.h> /* close() */
#endif
#ifdef HAVE_DEVPOLL
#include <sys/types.h> /* open */
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> /* close, ioctl */
#endif
#include <sys/utsname.h> /* uname() */
#include <stdlib.h> /* strtol() */
#include "io_wait.h"
#include "mem/mem.h"
#ifndef local_malloc
#define local_malloc pkg_malloc
#endif
#ifndef local_free
#define local_free pkg_free
#endif
char* poll_support="poll"
#ifdef HAVE_EPOLL
", epoll_lt, epoll_et"
#endif
#ifdef HAVE_SIGIO_RT
", sigio_rt"
#endif
#ifdef HAVE_SELECT
", select"
#endif
#ifdef HAVE_KQUEUE
", kqueue"
#endif
#ifdef HAVE_DEVPOLL
", /dev/poll"
#endif
;
char* poll_method_str[POLL_END]={ "none", "poll", "epoll_lt", "epoll_et",
"sigio_rt", "select", "kqueue", "/dev/poll"
};
#ifdef HAVE_SIGIO_RT
static int _sigio_init=0;
static int _sigio_crt_rtsig;
static sigset_t _sigio_rtsig_used;
#endif
#ifdef HAVE_SIGIO_RT
/* sigio specific init
* returns -1 on error, 0 on success */
static int init_sigio(io_wait_h* h, int rsig)
{
int r;
int n;
int signo;
int start_sig;
sigset_t oldset;
if (!_sigio_init){
_sigio_init=1;
_sigio_crt_rtsig=SIGRTMIN;
sigemptyset(&_sigio_rtsig_used);
}
h->signo=0;
if (rsig==0){
start_sig=_sigio_crt_rtsig;
n=SIGRTMAX-SIGRTMIN;
}else{
if ((rsig < SIGRTMIN) || (rsig >SIGRTMAX)){
LOG(L_CRIT, "ERROR: init_sigio: real time signal %d out of"
" range [%d, %d]\n", rsig, SIGRTMIN, SIGRTMAX);
goto error;
}
start_sig=rsig;
n=0;
}
sigemptyset(&h->sset);
sigemptyset(&oldset);
retry1:
/* get current block mask */
if (sigprocmask(SIG_BLOCK, &h->sset, &oldset )==-1){
if (errno==EINTR) goto retry1;
LOG(L_ERR, "ERROR: init_sigio: 1st sigprocmask failed: %s [%d]\n",
strerror(errno), errno);
/* try to continue */
}
for (r=start_sig; r<=(n+start_sig); r++){
signo=(r>SIGRTMAX)?r-SIGRTMAX+SIGRTMIN:r;
if (! sigismember(&_sigio_rtsig_used, signo) &&
! sigismember(&oldset, signo)){
sigaddset(&_sigio_rtsig_used, signo);
h->signo=signo;
_sigio_crt_rtsig=(signo<SIGRTMAX)?signo+1:SIGRTMIN;
break;
}
}
if (h->signo==0){
LOG(L_CRIT, "ERROR: init_sigio: %s\n",
rsig?"could not assign requested real-time signal":
"out of real-time signals");
goto error;
}
DBG("init_sigio: trying signal %d... \n", h->signo);
if (sigaddset(&h->sset, h->signo)==-1){
LOG(L_ERR, "ERROR: init_sigio: sigaddset failed for %d: %s [%d]\n",
h->signo, strerror(errno), errno);
goto error;
}
if (sigaddset(&h->sset, SIGIO)==-1){
LOG(L_ERR, "ERROR: init_sigio: sigaddset failed for %d: %s [%d]\n",
SIGIO, strerror(errno), errno);
goto error;
}
retry:
if (sigprocmask(SIG_BLOCK, &h->sset, 0)==-1){
if (errno==EINTR) goto retry;
LOG(L_ERR, "ERROR: init_sigio: sigprocmask failed: %s [%d]\n",
strerror(errno), errno);
goto error;
}
return 0;
error:
h->signo=0;
sigemptyset(&h->sset);
return -1;
}
/* sigio specific destroy */
static void destroy_sigio(io_wait_h* h)
{
if (h->signo){
sigprocmask(SIG_UNBLOCK, &h->sset, 0);
sigemptyset(&h->sset);
sigdelset(&_sigio_rtsig_used, h->signo);
h->signo=0;
}
}
#endif
#ifdef HAVE_EPOLL
/* epoll specific init
* returns -1 on error, 0 on success */
static int init_epoll(io_wait_h* h)
{
again:
h->epfd=epoll_create(h->max_fd_no);
if (h->epfd==-1){
if (errno==EINTR) goto again;
LOG(L_ERR, "ERROR: init_epoll: epoll_create: %s [%d]\n",
strerror(errno), errno);
return -1;
}
return 0;
}
static void destroy_epoll(io_wait_h* h)
{
if (h->epfd!=-1){
close(h->epfd);
h->epfd=-1;
}
}
#endif
#ifdef HAVE_KQUEUE
/* kqueue specific init
* returns -1 on error, 0 on success */
static int init_kqueue(io_wait_h* h)
{
again:
h->kq_fd=kqueue();
if (h->kq_fd==-1){
if (errno==EINTR) goto again;
LOG(L_ERR, "ERROR: init_kqueue: kqueue: %s [%d]\n",
strerror(errno), errno);
return -1;
}
return 0;
}
static void destroy_kqueue(io_wait_h* h)
{
if (h->kq_fd!=-1){
close(h->kq_fd);
h->kq_fd=-1;
}
}
#endif
#ifdef HAVE_DEVPOLL
/* /dev/poll specific init
* returns -1 on error, 0 on success */
static int init_devpoll(io_wait_h* h)
{
again:
h->dpoll_fd=open("/dev/poll", O_RDWR);
if (h->dpoll_fd==-1){
if (errno==EINTR) goto again;
LOG(L_ERR, "ERROR: init_/dev/poll: open: %s [%d]\n",
strerror(errno), errno);
return -1;
}
return 0;
}
static void destroy_devpoll(io_wait_h* h)
{
if (h->dpoll_fd!=-1){
close(h->dpoll_fd);
h->dpoll_fd=-1;
}
}
#endif
#ifdef HAVE_SELECT
static int init_select(io_wait_h* h)
{
FD_ZERO(&h->master_set);
return 0;
}
#endif
/* return system version (major.minor.minor2) as
* (major<<16)|(minor)<<8|(minor2)
* (if some of them are missing, they are set to 0)
* if the parameters are not null they are set to the coresp. part
*/
static unsigned int get_sys_version(int* major, int* minor, int* minor2)
{
struct utsname un;
int m1;
int m2;
int m3;
char* p;
memset (&un, 0, sizeof(un));
m1=m2=m3=0;
/* get sys version */
uname(&un);
m1=strtol(un.release, &p, 10);
if (*p=='.'){
p++;
m2=strtol(p, &p, 10);
if (*p=='.'){
p++;
m3=strtol(p, &p, 10);
}
}
if (major) *major=m1;
if (minor) *minor=m2;
if (minor2) *minor2=m3;
return ((m1<<16)|(m2<<8)|(m3));
}
/*
* returns 0 on success, and an error message on error
*/
char* check_poll_method(enum poll_types poll_method)
{
char* ret;
unsigned int os_ver;
ret=0;
os_ver=get_sys_version(0,0,0);
switch(poll_method){
case POLL_NONE:
break;
case POLL_POLL:
/* always supported */
break;
case POLL_SELECT:
/* should be always supported */
#ifndef HAVE_SELECT
ret="select not supported, try re-compiling with -DHAVE_SELECT";
#endif
break;
case POLL_EPOLL_LT:
case POLL_EPOLL_ET:
#ifndef HAVE_EPOLL
ret="epoll not supported, try re-compiling with -DHAVE_EPOLL";
#else
/* only on 2.6 + */
if (os_ver<0x020542) /* if ver < 2.5.66 */
ret="epoll not supported on kernels < 2.6";
#endif
break;
case POLL_SIGIO_RT:
#ifndef HAVE_SIGIO_RT
ret="sigio_rt not supported, try re-compiling with"
" -DHAVE_SIGIO_RT";
#else
/* only on 2.2 + ?? */
if (os_ver<0x020200) /* if ver < 2.2.0 */
ret="epoll not supported on kernels < 2.2 (?)";
#endif
break;
case POLL_KQUEUE:
#ifndef HAVE_KQUEUE
ret="kqueue not supported, try re-compiling with -DHAVE_KQUEUE";
#else
/* only in FreeBSD 4.1, NETBSD 2.0, OpenBSD 2.9, Darwin */
#ifdef __OS_freebsd
if (os_ver<0x0401) /* if ver < 4.1 */
ret="kqueue not supported on FreeBSD < 4.1";
#elif defined (__OS_netbsd)
if (os_ver<0x020000) /* if ver < 2.0 */
ret="kqueue not supported on NetBSD < 2.0";
#elif defined (__OS_openbsd)
if (os_ver<0x0209) /* if ver < 2.9 ? */
ret="kqueue not supported on OpenBSD < 2.9 (?)";
#endif /* assume that the rest support kqueue ifdef HAVE_KQUEUE */
#endif
break;
case POLL_DEVPOLL:
#ifndef HAVE_DEVPOLL
ret="/dev/poll not supported, try re-compiling with"
" -DHAVE_DEVPOLL";
#else
/* only in Solaris >= 7.0 (?) */
#ifdef __OS_solaris
if (os_ver<0x0507) /* ver < 5.7 */
ret="/dev/poll not supported on Solaris < 7.0 (SunOS 5.7)";
#endif
#endif
break;
default:
ret="unknown not supported method";
}
return ret;
}
enum poll_types choose_poll_method()
{
enum poll_types poll_method;
unsigned int os_ver;
os_ver=get_sys_version(0,0,0);
poll_method=0;
#ifdef HAVE_EPOLL
if (os_ver>=0x020542) /* if ver >= 2.5.66 */
poll_method=POLL_EPOLL_LT; /* or POLL_EPOLL_ET */
#endif
#ifdef HAVE_KQUEUE
if (poll_method==0)
/* only in FreeBSD 4.1, NETBSD 2.0, OpenBSD 2.9, Darwin */
#ifdef __OS_freebsd
if (os_ver>=0x0401) /* if ver >= 4.1 */
#elif defined (__OS_netbsd)
if (os_ver>=0x020000) /* if ver >= 2.0 */
#elif defined (__OS_openbsd)
if (os_ver>=0x0209) /* if ver >= 2.9 (?) */
#endif /* assume that the rest support kqueue ifdef HAVE_KQUEUE */
poll_method=POLL_KQUEUE;
#endif
#ifdef HAVE_DEVPOLL
#ifdef __OS_solaris
if (poll_method==0)
/* only in Solaris >= 7.0 (?) */
if (os_ver>=0x0507) /* if ver >=SunOS 5.7 */
poll_method=POLL_DEVPOLL;
#endif
#endif
#ifdef HAVE_SIGIO_RT
if (poll_method==0)
if (os_ver>=0x020200) /* if ver >= 2.2.0 */
poll_method=POLL_SIGIO_RT;
#endif
if (poll_method==0) poll_method=POLL_POLL;
return poll_method;
}
char* poll_method_name(enum poll_types poll_method)
{
if ((poll_method>=POLL_NONE) && (poll_method<POLL_END))
return poll_method_str[poll_method];
else
return "invalid poll method";
}
/* converts a string into a poll_method
* returns POLL_NONE (0) on error, else the corresponding poll type */
enum poll_types get_poll_type(char* s)
{
int r;
int l;
l=strlen(s);
for (r=POLL_END-1; r>POLL_NONE; r--)
if ((strlen(poll_method_str[r])==l) &&
(strncasecmp(poll_method_str[r], s, l)==0))
break;
return r;
}
/* initializes the static vars/arrays
* params: h - pointer to the io_wait_h that will be initialized
* max_fd - maximum allowed fd number
* poll_m - poll method (0 for automatic best fit)
*/
int init_io_wait(io_wait_h* h, int max_fd, enum poll_types poll_method)
{
char * poll_err;
memset(h, 0, sizeof(*h));
h->max_fd_no=max_fd;
#ifdef HAVE_EPOLL
h->epfd=-1;
#endif
#ifdef HAVE_KQUEUE
h->kq_fd=-1;
#endif
#ifdef HAVE_DEVPOLL
h->dpoll_fd=-1;
#endif
poll_err=check_poll_method(poll_method);
/* set an appropiate poll method */
if (poll_err || (poll_method==0)){
poll_method=choose_poll_method();
if (poll_err){
LOG(L_ERR, "ERROR: init_io_wait: %s, using %s instead\n",
poll_err, poll_method_str[poll_method]);
}else{
LOG(L_INFO, "init_io_wait: using %s as the io watch method"
" (auto detected)\n", poll_method_str[poll_method]);
}
}
h->poll_method=poll_method;
/* common stuff, everybody has fd_hash */
h->fd_hash=local_malloc(sizeof(*(h->fd_hash))*h->max_fd_no);
if (h->fd_hash==0){
LOG(L_CRIT, "ERROR: init_io_wait: could not alloc"
" fd hashtable (%ld bytes)\n",
(long)sizeof(*(h->fd_hash))*h->max_fd_no );
goto error;
}
memset((void*)h->fd_hash, 0, sizeof(*(h->fd_hash))*h->max_fd_no);
switch(poll_method){
case POLL_POLL:
#ifdef HAVE_SELECT
case POLL_SELECT:
#endif
#ifdef HAVE_SIGIO_RT
case POLL_SIGIO_RT:
#endif
#ifdef HAVE_DEVPOLL
case POLL_DEVPOLL:
#endif
h->fd_array=local_malloc(sizeof(*(h->fd_array))*h->max_fd_no);
if (h->fd_array==0){
LOG(L_CRIT, "ERROR: init_io_wait: could not"
" alloc fd array (%ld bytes)\n",
(long)sizeof(*(h->fd_hash))*h->max_fd_no);
goto error;
}
memset((void*)h->fd_array, 0, sizeof(*(h->fd_array))*h->max_fd_no);
#ifdef HAVE_SIGIO_RT
if ((poll_method==POLL_SIGIO_RT) && (init_sigio(h, 0)<0)){
LOG(L_CRIT, "ERROR: init_io_wait: sigio init failed\n");
goto error;
}
#endif
#ifdef HAVE_DEVPOLL
if ((poll_method==POLL_DEVPOLL) && (init_devpoll(h)<0)){
LOG(L_CRIT, "ERROR: init_io_wait: /dev/poll init failed\n");
goto error;
}
#endif
#ifdef HAVE_SELECT
if ((poll_method==POLL_SELECT) && (init_select(h)<0)){
LOG(L_CRIT, "ERROR: init_io_wait: select init failed\n");
goto error;
}
#endif
break;
#ifdef HAVE_EPOLL
case POLL_EPOLL_LT:
case POLL_EPOLL_ET:
h->ep_array=local_malloc(sizeof(*(h->ep_array))*h->max_fd_no);
if (h->ep_array==0){
LOG(L_CRIT, "ERROR: init_io_wait: could not alloc"
" epoll array\n");
goto error;
}
memset((void*)h->ep_array, 0, sizeof(*(h->ep_array))*h->max_fd_no);
if (init_epoll(h)<0){
LOG(L_CRIT, "ERROR: init_io_wait: epoll init failed\n");
goto error;
}
break;
#endif
#ifdef HAVE_KQUEUE
case POLL_KQUEUE:
h->kq_array=local_malloc(sizeof(*(h->kq_array))*h->max_fd_no);
if (h->kq_array==0){
LOG(L_CRIT, "ERROR: init_io_wait: could not alloc"
" kqueue event array\n");
goto error;
}
h->kq_changes_size=KQ_CHANGES_ARRAY_SIZE;
h->kq_changes=local_malloc(sizeof(*(h->kq_changes))*
h->kq_changes_size);
if (h->kq_changes==0){
LOG(L_CRIT, "ERROR: init_io_wait: could not alloc"
" kqueue changes array\n");
goto error;
}
h->kq_nchanges=0;
memset((void*)h->kq_array, 0, sizeof(*(h->kq_array))*h->max_fd_no);
memset((void*)h->kq_changes, 0,
sizeof(*(h->kq_changes))* h->kq_changes_size);
if (init_kqueue(h)<0){
LOG(L_CRIT, "ERROR: init_io_wait: kqueue init failed\n");
goto error;
}
break;
#endif
default:
LOG(L_CRIT, "BUG: init_io_wait: unknown/unsupported poll"
" method %s (%d)\n",
poll_method_str[poll_method], poll_method);
goto error;
}
return 0;
error:
return -1;
}
/* destroys everything init_io_wait allocated */
void destroy_io_wait(io_wait_h* h)
{
switch(h->poll_method){
#ifdef HAVE_EPOLL
case POLL_EPOLL_LT:
case POLL_EPOLL_ET:
destroy_epoll(h);
if (h->ep_array){
local_free(h->ep_array);
h->ep_array=0;
}
break;
#endif
#ifdef HAVE_KQUEUE
case POLL_KQUEUE:
destroy_kqueue(h);
if (h->kq_array){
local_free(h->kq_array);
h->kq_array=0;
}
if (h->kq_changes){
local_free(h->kq_changes);
h->kq_changes=0;
}
break;
#endif
#ifdef HAVE_SIGIO_RT
case POLL_SIGIO_RT:
destroy_sigio(h);
break;
#endif
#ifdef HAVE_DEVPOLL
case POLL_DEVPOLL:
destroy_devpoll(h);
break;
#endif
default: /*do nothing*/
;
}
if (h->fd_array){
local_free(h->fd_array);
h->fd_array=0;
}
if (h->fd_hash){
local_free(h->fd_hash);
h->fd_hash=0;
}
}
#endif
|