diff options
author | Matthew Jordan <mjordan@digium.com> | 2012-11-05 23:10:14 +0000 |
---|---|---|
committer | Matthew Jordan <mjordan@digium.com> | 2012-11-05 23:10:14 +0000 |
commit | a0c363e227aa728ea0a1ddad048683a82a373cca (patch) | |
tree | 6c89b8c6a9399b38863e8203849a4f90530bde26 /res/res_timing_pthread.c | |
parent | 5336a97f25c67220384b2c602d23748dd31c7c29 (diff) |
Refactor ast_timer_ack to return an error and handle the error in timer users
Currently, if an acknowledgement of a timer fails Asterisk will not realize
that a serious error occurred and will continue attempting to use the timer's
file descriptor. This can lead to situations where errors stream to the
CLI/log file. This consumes significant resources, masks the actual problem
that occurred (whatever caused the timer to fail in the first place), and
can leave channels in odd states.
This patch propagates the errors in the timing resource modules up through
the timer core, and makes users of these timers handle acknowledgement
failures. It also adds some defensive coding around the use of timers
to prevent using bad file descriptors in off nominal code paths.
Note that the patch created by the issue reporter was modified slightly for
this commit and backported to 1.8, as it was originally written for
Asterisk 10.
Review: https://reviewboard.asterisk.org/r/2178/
(issue ASTERISK-20032)
Reported by: Jeremiah Gowdy
patches:
jgowdy-timerfd-6-22-2012.diff uploaded by Jeremiah Gowdy (license 6358)
........
Merged revisions 375893 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........
Merged revisions 375894 from http://svn.asterisk.org/svn/asterisk/branches/10
........
Merged revisions 375895 from http://svn.asterisk.org/svn/asterisk/branches/11
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@375896 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res/res_timing_pthread.c')
-rw-r--r-- | res/res_timing_pthread.c | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/res/res_timing_pthread.c b/res/res_timing_pthread.c index 654f1d082..aa37885e2 100644 --- a/res/res_timing_pthread.c +++ b/res/res_timing_pthread.c @@ -47,7 +47,7 @@ static void *timing_funcs_handle; static int pthread_timer_open(void); static void pthread_timer_close(int handle); static int pthread_timer_set_rate(int handle, unsigned int rate); -static void pthread_timer_ack(int handle, unsigned int quantity); +static int pthread_timer_ack(int handle, unsigned int quantity); static int pthread_timer_enable_continuous(int handle); static int pthread_timer_disable_continuous(int handle); static enum ast_timer_event pthread_timer_get_event(int handle); @@ -97,7 +97,7 @@ struct pthread_timer { static void pthread_timer_destructor(void *obj); static struct pthread_timer *find_timer(int handle, int unlinkobj); static void write_byte(struct pthread_timer *timer); -static void read_pipe(struct pthread_timer *timer, unsigned int num); +static int read_pipe(struct pthread_timer *timer, unsigned int num); /*! * \brief Data for the timing thread @@ -190,21 +190,24 @@ static int pthread_timer_set_rate(int handle, unsigned int rate) return 0; } -static void pthread_timer_ack(int handle, unsigned int quantity) +static int pthread_timer_ack(int handle, unsigned int quantity) { struct pthread_timer *timer; + int res; ast_assert(quantity > 0); if (!(timer = find_timer(handle, 0))) { - return; + return -1; } ao2_lock(timer); - read_pipe(timer, quantity); + res = read_pipe(timer, quantity); ao2_unlock(timer); ao2_ref(timer, -1); + + return res; } static int pthread_timer_enable_continuous(int handle) @@ -240,7 +243,12 @@ static int pthread_timer_disable_continuous(int handle) ao2_lock(timer); if (timer->continuous) { timer->continuous = 0; - read_pipe(timer, 1); + if (read_pipe(timer, 1) != 0) { + /* Let the errno from read_pipe propagate up */ + ao2_unlock(timer); + ao2_ref(timer, -1); + return -1; + } } ao2_unlock(timer); @@ -358,8 +366,10 @@ static int check_timer(struct pthread_timer *timer) /*! * \internal * \pre timer is locked + * \retval 0 if nothing to read or read success + * \retval -1 on error */ -static void read_pipe(struct pthread_timer *timer, unsigned int quantity) +static int read_pipe(struct pthread_timer *timer, unsigned int quantity) { int rd_fd = timer->pipe[PIPE_READ]; int pending_ticks = timer->pending_ticks; @@ -375,7 +385,7 @@ static void read_pipe(struct pthread_timer *timer, unsigned int quantity) } if (!quantity) { - return; + return 0; } do { @@ -389,7 +399,7 @@ static void read_pipe(struct pthread_timer *timer, unsigned int quantity) if (ast_poll(&pfd, 1, 0) != 1) { ast_debug(1, "Reading not available on timing pipe, " "quantity: %u\n", quantity); - break; + return -1; } res = read(rd_fd, buf, @@ -401,12 +411,14 @@ static void read_pipe(struct pthread_timer *timer, unsigned int quantity) } ast_log(LOG_ERROR, "read failed on timing pipe: %s\n", strerror(errno)); - break; + return -1; } quantity -= res; timer->pending_ticks -= res; } while (quantity); + + return 0; } /*! |