diff options
author | Tilghman Lesher <tilghman@meg.abyt.es> | 2010-09-02 05:02:54 +0000 |
---|---|---|
committer | Tilghman Lesher <tilghman@meg.abyt.es> | 2010-09-02 05:02:54 +0000 |
commit | 5eae9f44f74f9869e46e416a2d5d092834043483 (patch) | |
tree | afc6eb9b08ef4ba9f7e0ad1bb4a59b13d5269d90 /main | |
parent | c28c620936e31ec5a63147ae20d82d621541ae90 (diff) |
Merged revisions 284597 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.8
................
r284597 | tilghman | 2010-09-02 00:00:34 -0500 (Thu, 02 Sep 2010) | 29 lines
Merged revisions 284593,284595 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.6.2
................
r284593 | tilghman | 2010-09-01 17:59:50 -0500 (Wed, 01 Sep 2010) | 18 lines
Merged revisions 284478 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r284478 | tilghman | 2010-09-01 13:49:11 -0500 (Wed, 01 Sep 2010) | 11 lines
Ensure that all areas that previously used select(2) now use poll(2), with implementations that need poll(2) implemented with select(2) safe against 1024-bit overflows.
This is a followup to the fix for the pthread timer in 1.6.2 and beyond, fixing
a potential crash bug in all supported releases.
(closes issue #17678)
Reported by: russell
Branch: https://origsvn.digium.com/svn/asterisk/team/tilghman/ast_select
Review: https://reviewboard.asterisk.org/r/824/
........
................
r284595 | tilghman | 2010-09-01 22:57:43 -0500 (Wed, 01 Sep 2010) | 2 lines
Failed to rerun bootstrap.sh after last commit
................
................
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@284598 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main')
-rw-r--r-- | main/asterisk.c | 46 | ||||
-rw-r--r-- | main/features.c | 87 | ||||
-rw-r--r-- | main/poll.c | 192 | ||||
-rw-r--r-- | main/stun.c | 7 |
4 files changed, 202 insertions, 130 deletions
diff --git a/main/asterisk.c b/main/asterisk.c index d8d3aebdf..f8d619a79 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -1,7 +1,7 @@ /* * Asterisk -- An open source telephony toolkit. * - * Copyright (C) 1999 - 2008, Digium, Inc. + * Copyright (C) 1999 - 2010, Digium, Inc. * * Mark Spencer <markster@digium.com> * @@ -272,6 +272,8 @@ static char ast_config_AST_CTL_OWNER[PATH_MAX] = "\0"; static char ast_config_AST_CTL_GROUP[PATH_MAX] = "\0"; static char ast_config_AST_CTL[PATH_MAX] = "asterisk.ctl"; +extern unsigned int ast_FD_SETSIZE; + static char *_argv[256]; static int shuttingdown; static int restartnow; @@ -3167,7 +3169,8 @@ int main(int argc, char *argv[]) char *buf; const char *runuser = NULL, *rungroup = NULL; char *remotesock = NULL; - int moduleresult; /*!< Result from the module load subsystem */ + int moduleresult; /*!< Result from the module load subsystem */ + struct rlimit l; /* Remember original args for restart */ if (argc > ARRAY_LEN(_argv) - 1) { @@ -3352,7 +3355,6 @@ int main(int argc, char *argv[]) } if (ast_opt_dump_core) { - struct rlimit l; memset(&l, 0, sizeof(l)); l.rlim_cur = RLIM_INFINITY; l.rlim_max = RLIM_INFINITY; @@ -3361,6 +3363,44 @@ int main(int argc, char *argv[]) } } + if (getrlimit(RLIMIT_NOFILE, &l)) { + ast_log(LOG_WARNING, "Unable to check file descriptor limit: %s\n", strerror(errno)); + } + +#if !defined(CONFIGURE_RAN_AS_ROOT) + /* Check if select(2) will run with more file descriptors */ + do { + int fd, fd2; + ast_fdset readers; + struct timeval tv = { 0, }; + + if (l.rlim_cur <= FD_SETSIZE) { + /* The limit of select()able FDs is irrelevant, because we'll never + * open one that high. */ + break; + } + + if (!(fd = open("/dev/null", O_RDONLY))) { + ast_log(LOG_ERROR, "Cannot open a file descriptor at boot? %s\n", strerror(errno)); + break; /* XXX Should we exit() here? XXX */ + } + + fd2 = (l.rlim_cur > sizeof(readers) * 8 ? sizeof(readers) * 8 : l.rlim_cur) - 1; + if (dup2(fd, fd2)) { + ast_log(LOG_WARNING, "Cannot open maximum file descriptor %d at boot? %s\n", fd2, strerror(errno)); + break; + } + + FD_ZERO(&readers); + FD_SET(fd2, &readers); + if (ast_select(fd2 + 1, &readers, NULL, NULL, &tv) < 0) { + ast_log(LOG_WARNING, "Maximum select()able file descriptor is %d\n", FD_SETSIZE); + } + } while (0); +#elif defined(HAVE_VARIABLE_FDSET) + ast_FD_SETSIZE = l.rlim_cur; +#endif /* !defined(CONFIGURE_RAN_AS_ROOT) */ + if ((!rungroup) && !ast_strlen_zero(ast_config_AST_RUN_GROUP)) rungroup = ast_config_AST_RUN_GROUP; if ((!runuser) && !ast_strlen_zero(ast_config_AST_RUN_USER)) diff --git a/main/features.c b/main/features.c index ac0bc7cbf..02ae76348 100644 --- a/main/features.c +++ b/main/features.c @@ -438,7 +438,7 @@ static const struct ast_datastore_info dial_features_info = { static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot); static void parkinglot_unref(struct ast_parkinglot *parkinglot); static void parkinglot_destroy(void *obj); -int manage_parkinglot(struct ast_parkinglot *curlot, fd_set *rfds, fd_set *efds, fd_set *nrfds, fd_set *nefds, int *fs, int *max); +int manage_parkinglot(struct ast_parkinglot *curlot, struct pollfd **pfds, int *nfds, int *fs); struct ast_parkinglot *find_parkinglot(const char *name); static struct ast_parkinglot *create_parkinglot(const char *name); static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot); @@ -3603,9 +3603,10 @@ static char *callback_dialoptions(struct ast_flags *features_callee, struct ast_ } /*! \brief Run management on parkinglots, called once per parkinglot */ -int manage_parkinglot(struct ast_parkinglot *curlot, fd_set *rfds, fd_set *efds, fd_set *nrfds, fd_set *nefds, int *ms, int *max) +int manage_parkinglot(struct ast_parkinglot *curlot, struct pollfd **pfds, int *nfds, int *ms) { - + struct pollfd *new_fds = NULL; + int new_nfds = 0; struct parkeduser *pu; int res = 0; char parkingslot[AST_MAX_EXTENSION]; @@ -3628,12 +3629,12 @@ int manage_parkinglot(struct ast_parkinglot *curlot, fd_set *rfds, fd_set *efds, /* Get chan, exten from derived kludge */ if (pu->peername[0]) { char *peername = ast_strdupa(pu->peername); - char *cp = strrchr(peername, '-'); + char *dash = strrchr(peername, '-'); char *peername_flat; /* using something like DAHDI/52 for an extension name is NOT a good idea */ int i; - if (cp) { - *cp = 0; + if (dash) { + *dash = '\0'; } peername_flat = ast_strdupa(peername); @@ -3712,14 +3713,33 @@ int manage_parkinglot(struct ast_parkinglot *curlot, fd_set *rfds, fd_set *efds, } else { /* still within parking time, process descriptors */ for (x = 0; x < AST_MAX_FDS; x++) { struct ast_frame *f; + int y; + + if (chan->fds[x] == -1) { + continue; /* nothing on this descriptor */ + } + + for (y = 0; y < *nfds; y++) { + if ((*pfds[y]).fd == chan->fds[x]) { + /* Found poll record! */ + break; + } + } + if (y == *nfds) { + /* Not found */ + continue; + } - if ((chan->fds[x] == -1) || (!FD_ISSET(chan->fds[x], rfds) && !FD_ISSET(pu->chan->fds[x], efds))) + if (!((*pfds[y]).revents & (POLLIN | POLLERR))) { + /* Next x */ continue; - - if (FD_ISSET(chan->fds[x], efds)) + } + + if ((*pfds[y]).revents & POLLERR) { ast_set_flag(chan, AST_FLAG_EXCEPTION); - else + } else { ast_clear_flag(chan, AST_FLAG_EXCEPTION); + } chan->fdno = x; /* See if they need servicing */ @@ -3760,22 +3780,32 @@ int manage_parkinglot(struct ast_parkinglot *curlot, fd_set *rfds, fd_set *efds, } } /* End for */ if (x >= AST_MAX_FDS) { -std: for (x=0; x<AST_MAX_FDS; x++) { /* mark fds for next round */ +std: for (x = 0; x < AST_MAX_FDS; x++) { /* mark fds for next round */ if (chan->fds[x] > -1) { - FD_SET(chan->fds[x], nrfds); - FD_SET(chan->fds[x], nefds); - if (chan->fds[x] > *max) - *max = chan->fds[x]; + void *tmp = ast_realloc(new_fds, (new_nfds + 1) * sizeof(*new_fds)); + if (!tmp) { + continue; + } + new_fds = tmp; + new_fds[new_nfds].fd = chan->fds[x]; + new_fds[new_nfds].events = POLLIN | POLLERR; + new_fds[new_nfds].revents = 0; + new_nfds++; } } /* Keep track of our shortest wait */ - if (tms < *ms || *ms < 0) + if (tms < *ms || *ms < 0) { *ms = tms; + } } } } AST_LIST_TRAVERSE_SAFE_END; AST_LIST_UNLOCK(&curlot->parkings); + + ast_free(*pfds); + *pfds = new_fds; + *nfds = new_nfds; return res; } @@ -3789,35 +3819,26 @@ std: for (x=0; x<AST_MAX_FDS; x++) { /* mark fds for next round */ */ static void *do_parking_thread(void *ignore) { - fd_set rfds, efds; /* results from previous select, to be preserved across loops. */ - fd_set nrfds, nefds; /* args for the next select */ - FD_ZERO(&rfds); - FD_ZERO(&efds); + struct pollfd *pfds = NULL; + int nfds = 0; for (;;) { - int res = 0; - int ms = -1; /* select timeout, uninitialized */ - int max = -1; /* max fd, none there yet */ struct ao2_iterator iter; struct ast_parkinglot *curlot; - FD_ZERO(&nrfds); - FD_ZERO(&nefds); + int ms = -1; /* poll2 timeout, uninitialized */ iter = ao2_iterator_init(parkinglots, 0); while ((curlot = ao2_iterator_next(&iter))) { - res = manage_parkinglot(curlot, &rfds, &efds, &nrfds, &nefds, &ms, &max); + manage_parkinglot(curlot, &pfds, &nfds, &ms); ao2_ref(curlot, -1); } + ao2_iterator_destroy(&iter); - rfds = nrfds; - efds = nefds; - { - struct timeval wait = ast_samp2tv(ms, 1000); - /* Wait for something to happen */ - ast_select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &wait : NULL); - } + /* Wait for something to happen */ + ast_poll(pfds, nfds, ms); pthread_testcancel(); } + /* If this WERE reached, we'd need to free(pfds) */ return NULL; /* Never reached */ } diff --git a/main/poll.c b/main/poll.c index d10639077..0b2106d13 100644 --- a/main/poll.c +++ b/main/poll.c @@ -10,9 +10,9 @@ struct pollfd { - int fd; - short events; - short revents; + int fd; + short events; + short revents; } int poll (struct pollfd *pArray, unsigned long n_fds, int timeout) @@ -73,54 +73,44 @@ #include "asterisk.h" -#include <unistd.h> /* standard Unix definitions */ -#include <sys/types.h> /* system types */ -#include <sys/time.h> /* time definitions */ -#include <assert.h> /* assertion macros */ -#include <string.h> /* string functions */ +#include <unistd.h> /* standard Unix definitions */ +#include <sys/types.h> /* system types */ +#include <sys/time.h> /* time definitions */ +#include <assert.h> /* assertion macros */ +#include <string.h> /* string functions */ +#include <errno.h> #include "asterisk/utils.h" /* this package */ #include "asterisk/poll-compat.h" /* this package */ -#ifdef AST_POLL_COMPAT +unsigned int ast_FD_SETSIZE = FD_SETSIZE; #ifndef MAX #define MAX(a,b) a > b ? a : b #endif /*---------------------------------------------------------------------------*\ - Private Functions + Private Functions \*---------------------------------------------------------------------------*/ -static int map_poll_spec -#if __STDC__ > 0 - (struct pollfd *pArray, - unsigned long n_fds, - fd_set *pReadSet, - fd_set *pWriteSet, - fd_set *pExceptSet) -#else - (pArray, n_fds, pReadSet, pWriteSet, pExceptSet) - struct pollfd *pArray; - unsigned long n_fds; - fd_set *pReadSet; - fd_set *pWriteSet; - fd_set *pExceptSet; -#endif +#if defined(AST_POLL_COMPAT) || !defined(HAVE_PPOLL) +static int map_poll_spec(struct pollfd *pArray, unsigned long n_fds, + ast_fdset *pReadSet, ast_fdset *pWriteSet, ast_fdset *pExceptSet) { - register unsigned long i; /* loop control */ - register struct pollfd *pCur; /* current array element */ - register int max_fd = -1; /* return value */ + register unsigned long i; /* loop control */ + register struct pollfd *pCur; /* current array element */ + register int max_fd = -1; /* return value */ - /*!\note + /* * Map the poll() structures into the file descriptor sets required * by select(). */ for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) { /* Skip any bad FDs in the array. */ - if (pCur->fd < 0) + if (pCur->fd < 0) { continue; + } if (pCur->events & POLLIN) { /* "Input Ready" notification desired. */ @@ -145,34 +135,28 @@ static int map_poll_spec return max_fd; } - -static struct timeval *map_timeout -#if __STDC__ > 0 - (int poll_timeout, struct timeval *pSelTimeout) -#else - (poll_timeout, pSelTimeout) - int poll_timeout; - struct timeval *pSelTimeout; -#endif + +#ifdef AST_POLL_COMPAT +static struct timeval *map_timeout(int poll_timeout, struct timeval *pSelTimeout) { struct timeval *pResult; - /*!\note - * Map the poll() timeout value into a select() timeout. The possible - * values of the poll() timeout value, and their meanings, are: - * - * VALUE MEANING - * - * -1 wait indefinitely (until signal occurs) - * 0 return immediately, don't block - * >0 wait specified number of milliseconds - * - * select() uses a "struct timeval", which specifies the timeout in - * seconds and microseconds, so the milliseconds value has to be mapped - * accordingly. - */ + /* + Map the poll() timeout value into a select() timeout. The possible + values of the poll() timeout value, and their meanings, are: + + VALUE MEANING - assert(pSelTimeout != (struct timeval *) NULL); + -1 wait indefinitely (until signal occurs) + 0 return immediately, don't block + >0 wait specified number of milliseconds + + select() uses a "struct timeval", which specifies the timeout in + seconds and microseconds, so the milliseconds value has to be mapped + accordingly. + */ + + assert(pSelTimeout != NULL); switch (poll_timeout) { case -1: @@ -203,25 +187,13 @@ static struct timeval *map_timeout return pResult; } +#endif /* AST_POLL_COMPAT */ -static void map_select_results -#if __STDC__ > 0 - (struct pollfd *pArray, - unsigned long n_fds, - fd_set *pReadSet, - fd_set *pWriteSet, - fd_set *pExceptSet) -#else - (pArray, n_fds, pReadSet, pWriteSet, pExceptSet) - struct pollfd *pArray; - unsigned long n_fds; - fd_set *pReadSet; - fd_set *pWriteSet; - fd_set *pExceptSet; -#endif +static void map_select_results(struct pollfd *pArray, unsigned long n_fds, + ast_fdset *pReadSet, ast_fdset *pWriteSet, ast_fdset *pExceptSet) { - register unsigned long i; /* loop control */ - register struct pollfd *pCur; /* current array element */ + register unsigned long i; /* loop control */ + register struct pollfd *pCur; /* current array element */ for (i = 0, pCur = pArray; i < n_fds; i++, pCur++) { /* Skip any bad FDs in the array. */ @@ -231,59 +203,101 @@ static void map_select_results } /* Exception events take priority over input events. */ - pCur->revents = 0; - if (FD_ISSET (pCur->fd, pExceptSet)) { + if (FD_ISSET(pCur->fd, (fd_set *) pExceptSet)) { pCur->revents |= POLLPRI; - } else if (FD_ISSET (pCur->fd, pReadSet)) { + } else if (FD_ISSET(pCur->fd, (fd_set *) pReadSet)) { pCur->revents |= POLLIN; } - if (FD_ISSET (pCur->fd, pWriteSet)) { + if (FD_ISSET(pCur->fd, (fd_set *) pWriteSet)) { pCur->revents |= POLLOUT; } } return; } +#endif /* defined(AST_POLL_COMPAT) || !defined(HAVE_PPOLL) */ /*---------------------------------------------------------------------------*\ - Public Functions + Public Functions \*---------------------------------------------------------------------------*/ - +#ifdef AST_POLL_COMPAT int ast_internal_poll(struct pollfd *pArray, unsigned long n_fds, int timeout) { - fd_set read_descs; /* input file descs */ - fd_set write_descs; /* output file descs */ - fd_set except_descs; /* exception descs */ + ast_fdset read_descs; /* input file descs */ + ast_fdset write_descs; /* output file descs */ + ast_fdset except_descs; /* exception descs */ struct timeval stime; /* select() timeout value */ - int ready_descriptors; /* function result */ - int max_fd = 0; /* maximum fd value */ + int ready_descriptors; /* function result */ + int max_fd = 0; /* maximum fd value */ struct timeval *pTimeout; /* actually passed */ + int save_errno; - FD_ZERO (&read_descs); - FD_ZERO (&write_descs); - FD_ZERO (&except_descs); + FD_ZERO(&read_descs); + FD_ZERO(&write_descs); + FD_ZERO(&except_descs); /* Map the poll() file descriptor list in the select() data structures. */ if (pArray) { - max_fd = map_poll_spec (pArray, n_fds, + max_fd = map_poll_spec (pArray, n_fds, &read_descs, &write_descs, &except_descs); } /* Map the poll() timeout value in the select() timeout structure. */ - pTimeout = map_timeout(timeout, &stime); + + pTimeout = map_timeout (timeout, &stime); /* Make the select() call. */ - ready_descriptors = select(max_fd + 1, &read_descs, &write_descs, + + ready_descriptors = ast_select(max_fd + 1, &read_descs, &write_descs, &except_descs, pTimeout); + save_errno = errno; + + if (ready_descriptors >= 0) { + map_select_results (pArray, n_fds, + &read_descs, &write_descs, &except_descs); + } + + errno = save_errno; + return ready_descriptors; +} +#endif /* AST_POLL_COMPAT */ + +int ast_poll2(struct pollfd *pArray, unsigned long n_fds, struct timeval *tv) +{ +#ifdef HAVE_PPOLL + struct timeval start = ast_tvnow(); + struct timespec ts = { tv ? tv->tv_sec : 0, tv ? tv->tv_usec * 1000 : 0 }; + int res = ppoll(pArray, n_fds, tv ? &ts : NULL, NULL); + struct timeval after = ast_tvnow(); + if (res > 0 && tv && ast_tvdiff_ms(ast_tvadd(*tv, start), after) > 0) { + *tv = ast_tvsub(*tv, ast_tvsub(after, start)); + } else if (res > 0 && tv) { + *tv = ast_tv(0, 0); + } + return res; +#else + ast_fdset read_descs, write_descs, except_descs; + int ready_descriptors, max_fd = 0; + + FD_ZERO(&read_descs); + FD_ZERO(&write_descs); + FD_ZERO(&except_descs); + + if (pArray) { + max_fd = map_poll_spec(pArray, n_fds, &read_descs, &write_descs, &except_descs); + } + + ready_descriptors = ast_select(max_fd + 1, &read_descs, &write_descs, &except_descs, tv); if (ready_descriptors >= 0) { map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs); } return ready_descriptors; +#endif } -#endif /* AST_POLL_COMPAT */ + diff --git a/main/stun.c b/main/stun.c index 74652f90b..3ad326ee8 100644 --- a/main/stun.c +++ b/main/stun.c @@ -397,8 +397,7 @@ int ast_stun_request(int s, struct sockaddr_in *dst, for (retry = 0; retry < 3; retry++) { /* XXX make retries configurable */ /* send request, possibly wait for reply */ unsigned char reply_buf[1024]; - fd_set rfds; - struct timeval to = { 3, 0 }; /* timeout, make it configurable */ + struct pollfd pfds = { .fd = s, .events = POLLIN }; struct sockaddr_in src; socklen_t srclen; @@ -410,9 +409,7 @@ int ast_stun_request(int s, struct sockaddr_in *dst, } if (answer == NULL) break; - FD_ZERO(&rfds); - FD_SET(s, &rfds); - res = ast_select(s + 1, &rfds, NULL, NULL, &to); + res = ast_poll(&pfds, 1, 3000); if (res <= 0) /* timeout or error */ continue; memset(&src, 0, sizeof(src)); |