mirror of https://github.com/asterisk/asterisk
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-fbb531ad65f310-digiumphones
parent
c28c620936
commit
5eae9f44f7
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2010, Digium, Inc.
|
||||
*
|
||||
* Tilghman Lesher <tlesher AT digium DOT com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!\file
|
||||
* \brief Bitfield expansions for ast_select
|
||||
*/
|
||||
|
||||
#ifndef __AST_SELECT_H
|
||||
#define __AST_SELECT_H
|
||||
|
||||
#include <sys/select.h>
|
||||
#include <errno.h>
|
||||
#include "asterisk/utils.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern unsigned int ast_FD_SETSIZE;
|
||||
|
||||
#if !defined(HAVE_VARIABLE_FDSET) && defined(CONFIGURE_RAN_AS_ROOT)
|
||||
#define ast_fdset fd_set
|
||||
#else
|
||||
typedef struct {
|
||||
long fds_bits[4096 / sizeof(long)]; /* 32768 bits */
|
||||
} ast_fdset;
|
||||
|
||||
#undef FD_ZERO
|
||||
#define FD_ZERO(a) \
|
||||
do { \
|
||||
long *bytes = (long *) a; \
|
||||
int i; \
|
||||
for (i = 0; i < sizeof(*(a)) / sizeof(long); i++) { \
|
||||
bytes[i] = 0; \
|
||||
} \
|
||||
} while (0)
|
||||
#undef FD_SET
|
||||
#define FD_SET(fd, fds) \
|
||||
do { \
|
||||
long *bytes = (long *) fds; \
|
||||
if (fd / sizeof(*bytes) + ((fd + 1) % sizeof(*bytes) ? 1 : 0) < sizeof(*(fds))) { \
|
||||
bytes[fd / (sizeof(*bytes))] |= 1L << (fd % sizeof(*bytes)); \
|
||||
} else { \
|
||||
ast_log(LOG_ERROR, "FD %d exceeds the maximum size of ast_fdset!\n", fd); \
|
||||
} \
|
||||
} while (0)
|
||||
#endif /* HAVE_VARIABLE_FDSET */
|
||||
|
||||
/*! \brief Waits for activity on a group of channels
|
||||
* \param nfds the maximum number of file descriptors in the sets
|
||||
* \param rfds file descriptors to check for read availability
|
||||
* \param wfds file descriptors to check for write availability
|
||||
* \param efds file descriptors to check for exceptions (OOB data)
|
||||
* \param tvp timeout while waiting for events
|
||||
* This is the same as a standard select(), except it guarantees the
|
||||
* behaviour where the passed struct timeval is updated with how much
|
||||
* time was not slept while waiting for the specified events
|
||||
*/
|
||||
static inline int ast_select(int nfds, ast_fdset *rfds, ast_fdset *wfds, ast_fdset *efds, struct timeval *tvp)
|
||||
{
|
||||
#ifdef __linux__
|
||||
ast_assert((unsigned int) nfds <= ast_FD_SETSIZE);
|
||||
return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
|
||||
#else
|
||||
int save_errno = 0;
|
||||
|
||||
ast_assert((unsigned int) nfds <= ast_FD_SETSIZE);
|
||||
if (tvp) {
|
||||
struct timeval tv, tvstart, tvend, tvlen;
|
||||
int res;
|
||||
|
||||
tv = *tvp;
|
||||
gettimeofday(&tvstart, NULL);
|
||||
res = select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, tvp);
|
||||
save_errno = errno;
|
||||
gettimeofday(&tvend, NULL);
|
||||
timersub(&tvend, &tvstart, &tvlen);
|
||||
timersub(&tv, &tvlen, tvp);
|
||||
if (tvp->tv_sec < 0 || (tvp->tv_sec == 0 && tvp->tv_usec < 0)) {
|
||||
tvp->tv_sec = 0;
|
||||
tvp->tv_usec = 0;
|
||||
}
|
||||
errno = save_errno;
|
||||
return res;
|
||||
}
|
||||
else
|
||||
return select(nfds, (fd_set *) rfds, (fd_set *) wfds, (fd_set *) efds, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AST_SELECT_H */
|
@ -0,0 +1,253 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2010, Digium, Inc.
|
||||
*
|
||||
* Tilghman Lesher <tlesher AT digium DOT com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \brief Poll Tests
|
||||
*
|
||||
* \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
|
||||
*
|
||||
* Verify that the various poll implementations work as desired (ast_poll, ast_poll2)
|
||||
* \ingroup tests
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<depend>TEST_FRAMEWORK</depend>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include "asterisk/utils.h"
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/test.h"
|
||||
#include "asterisk/poll-compat.h"
|
||||
|
||||
#ifndef HAVE_SBIN_LAUNCHD
|
||||
static void *failsafe_cancel(void *vparent)
|
||||
{
|
||||
pthread_t parent = (pthread_t) (long) vparent;
|
||||
|
||||
sleep(1);
|
||||
pthread_testcancel();
|
||||
pthread_kill(parent, SIGURG);
|
||||
sleep(1);
|
||||
pthread_testcancel();
|
||||
pthread_kill(parent, SIGURG);
|
||||
sleep(1);
|
||||
pthread_testcancel();
|
||||
pthread_kill(parent, SIGURG);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
#define RESET for (i = 0; i < 4; i++) { pfd[i].revents = 0; }
|
||||
AST_TEST_DEFINE(poll_test)
|
||||
{
|
||||
#define FDNO 3
|
||||
int fd[2], res = AST_TEST_PASS, i, res2;
|
||||
int rdblocker[2];
|
||||
#if FDNO > 3
|
||||
int wrblocker[2], consec_interrupt = 0;
|
||||
#endif
|
||||
struct pollfd pfd[4] = { { .events = POLLOUT, }, { .events = POLLIN, }, { .events = POLLIN }, { .events = POLLOUT } };
|
||||
pthread_t failsafe_tid;
|
||||
struct timeval tv = { 0, 0 };
|
||||
#if FDNO > 3
|
||||
char garbage[256] =
|
||||
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
|
||||
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
|
||||
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
|
||||
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/";
|
||||
#endif
|
||||
|
||||
switch (cmd) {
|
||||
case TEST_INIT:
|
||||
info->name = "poll_test";
|
||||
info->category = "main/poll/";
|
||||
info->summary = "unit test for the ast_poll() API";
|
||||
info->description =
|
||||
"Verifies behavior for the ast_poll() API call\n";
|
||||
return AST_TEST_NOT_RUN;
|
||||
case TEST_EXECUTE:
|
||||
break;
|
||||
}
|
||||
|
||||
ast_test_status_update(test, "Creating handle that should NEVER block on write\n");
|
||||
if ((fd[0] = open("/dev/null", O_WRONLY)) < 0) {
|
||||
ast_test_status_update(test, "Unable to open a writable handle to /dev/null: %s\n", strerror(errno));
|
||||
return AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
ast_test_status_update(test, "Creating handle that should NEVER block on read\n");
|
||||
if ((fd[1] = open("/dev/zero", O_RDONLY)) < 0) {
|
||||
ast_test_status_update(test, "Unable to open a readable handle to /dev/zero: %s\n", strerror(errno));
|
||||
close(fd[0]);
|
||||
return AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
ast_test_status_update(test, "Creating handle that should block on read\n");
|
||||
if (pipe(rdblocker) < 0) {
|
||||
ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno));
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
return AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
#if FDNO > 3
|
||||
ast_test_status_update(test, "Creating handle that should block on write\n");
|
||||
if (pipe(wrblocker) < 0) {
|
||||
ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno));
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
close(rdblocker[0]);
|
||||
close(rdblocker[1]);
|
||||
return AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
ast_test_status_update(test, "Starting thread to ensure we don't block forever\n");
|
||||
if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) {
|
||||
ast_test_status_update(test, "Unable to start failsafe thread\n");
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
close(fd[2]);
|
||||
close(rdblocker[0]);
|
||||
close(rdblocker[1]);
|
||||
close(wrblocker[0]);
|
||||
close(wrblocker[1]);
|
||||
return AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
/* Fill the pipe full of data */
|
||||
ast_test_status_update(test, "Making pipe block on write\n");
|
||||
for (i = 0; i < 4096; i++) { /* 1MB of data should be more than enough for any pipe */
|
||||
errno = 0;
|
||||
if (write(wrblocker[1], garbage, sizeof(garbage)) < sizeof(garbage)) {
|
||||
ast_test_status_update(test, "Got %d\n", errno);
|
||||
if (errno == EINTR && ++consec_interrupt > 1) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
consec_interrupt = 0;
|
||||
}
|
||||
}
|
||||
|
||||
ast_test_status_update(test, "Cancelling failsafe thread.\n");
|
||||
pthread_cancel(failsafe_tid);
|
||||
pthread_kill(failsafe_tid, SIGURG);
|
||||
pthread_join(failsafe_tid, NULL);
|
||||
#endif
|
||||
|
||||
pfd[0].fd = fd[0];
|
||||
pfd[1].fd = fd[1];
|
||||
pfd[2].fd = rdblocker[0];
|
||||
#if FDNO > 3
|
||||
pfd[3].fd = wrblocker[1];
|
||||
#endif
|
||||
|
||||
/* Need to ensure the infinite timeout doesn't stall the process */
|
||||
ast_test_status_update(test, "Starting thread to ensure we don't block forever\n");
|
||||
if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) {
|
||||
ast_test_status_update(test, "Unable to start failsafe thread\n");
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
close(rdblocker[0]);
|
||||
close(rdblocker[1]);
|
||||
#if FDNO > 3
|
||||
close(wrblocker[0]);
|
||||
close(wrblocker[1]);
|
||||
#endif
|
||||
return AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
RESET;
|
||||
if ((res2 = ast_poll(pfd, FDNO, -1)) != 2) {
|
||||
ast_test_status_update(test, "ast_poll does not return that only two handles are available (inf timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
RESET;
|
||||
if ((res2 = ast_poll2(pfd, FDNO, NULL)) != 2) {
|
||||
ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (inf timeout): %d %s\n", res2, res2 == -1 ? strerror(errno) : "");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
ast_test_status_update(test, "Cancelling failsafe thread.\n");
|
||||
pthread_cancel(failsafe_tid);
|
||||
pthread_kill(failsafe_tid, SIGURG);
|
||||
pthread_join(failsafe_tid, NULL);
|
||||
|
||||
RESET;
|
||||
if (ast_poll(pfd, FDNO, 0) != 2) {
|
||||
ast_test_status_update(test, "ast_poll does not return that only two handles are available (0 timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
RESET;
|
||||
if (ast_poll2(pfd, FDNO, &tv) != 2) {
|
||||
ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (0 timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
RESET;
|
||||
if (ast_poll(pfd, FDNO, 1) != 2) {
|
||||
ast_test_status_update(test, "ast_poll does not return that only two handles are available (1ms timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
tv.tv_usec = 1000;
|
||||
if (ast_poll2(pfd, FDNO, &tv) != 2) {
|
||||
ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (1ms timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
|
||||
res = AST_TEST_FAIL;
|
||||
}
|
||||
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
close(rdblocker[0]);
|
||||
close(rdblocker[1]);
|
||||
#if FDNO > 3
|
||||
close(wrblocker[0]);
|
||||
close(wrblocker[1]);
|
||||
#endif
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
#ifndef HAVE_SBIN_LAUNCHD
|
||||
AST_TEST_UNREGISTER(poll_test);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
#ifndef HAVE_SBIN_LAUNCHD
|
||||
AST_TEST_REGISTER(poll_test);
|
||||
#endif
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Poll test");
|
Loading…
Reference in new issue