aboutsummaryrefslogtreecommitdiffstats
path: root/main/utils.c
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2008-12-22 17:22:56 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2008-12-22 17:22:56 +0000
commit825e65fc8b94ad4f7e4df89d2b0747d71cd57267 (patch)
tree69e09348be23cefe386f8d958b6e5e39ff9144f5 /main/utils.c
parent91ecd609c1e7e2f9ff5f8c7ea3f44aabc24c139c (diff)
Fix up timeout handling in ast_carefulwrite().
git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.4@166297 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main/utils.c')
-rw-r--r--main/utils.c19
1 files changed, 18 insertions, 1 deletions
diff --git a/main/utils.c b/main/utils.c
index 7bc854a95..d7863198b 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -921,7 +921,9 @@ int ast_wait_for_input(int fd, int ms)
int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
{
+ struct timeval start = ast_tvnow();
int res = 0;
+ int elapsed = 0;
while (len) {
struct pollfd pfd = {
@@ -930,7 +932,7 @@ int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
};
/* poll() until the fd is writable without blocking */
- while ((res = poll(&pfd, 1, timeoutms)) <= 0) {
+ while ((res = poll(&pfd, 1, timeoutms - elapsed)) <= 0) {
if (res == 0) {
/* timed out. */
ast_log(LOG_NOTICE, "Timed out trying to write\n");
@@ -939,6 +941,13 @@ int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
/* poll() returned an error, check to see if it was fatal */
if (errno == EINTR || errno == EAGAIN) {
+ elapsed = ast_tvdiff_ms(ast_tvnow(), start);
+ if (elapsed > timeoutms) {
+ /* We've taken too long to write
+ * This is only an error condition if we haven't finished writing. */
+ res = len ? -1 : 0;
+ break;
+ }
/* This was an acceptable error, go back into poll() */
continue;
}
@@ -967,6 +976,14 @@ int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
len -= res;
s += res;
res = 0;
+
+ elapsed = ast_tvdiff_ms(ast_tvnow(), start);
+ if (elapsed > timeoutms) {
+ /* We've taken too long to write
+ * This is only an error condition if we haven't finished writing. */
+ res = len ? -1 : 0;
+ break;
+ }
}
return res;