.events = POLLOUT,
};
int res;
+ struct timeval start = ast_tvnow();
+ int elapsed = 0;
/* 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");
/* 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) {
+ return -1;
+ }
/* This was an acceptable error, go back into poll() */
continue;
}
return -1;
}
+ elapsed = ast_tvdiff_ms(ast_tvnow(), start);
+ if (elapsed >= timeoutms) {
+ return -1;
+ }
}
return 0;
* have a need to wait. This way, we get better performance.
* If the descriptor is blocking, all assumptions on the guaranteed
* detail do not apply anymore.
- * Also note that in the current implementation, the delay is per-write,
- * so you still have no guarantees, anyways.
- * Fortunately the routine is only used in a few places (cli.c, manager.c,
- * res_agi.c) so it is reasonably easy to check how it behaves there.
- *
- * XXX We either need to fix the code, or fix the documentation.
*/
int ast_carefulwrite(int fd, char *s, int len, int timeoutms)
{
+ struct timeval start = ast_tvnow();
int res = 0;
+ int elapsed = 0;
while (len) {
- if (ast_wait_for_output(fd, timeoutms)) {
+ if (ast_wait_for_output(fd, timeoutms - elapsed)) {
return -1;
}
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;
{
struct timeval start = ast_tvnow();
int n = 0;
+ int elapsed = 0;
while (len) {
- int elapsed;
-
- if (ast_wait_for_output(fd, timeoutms)) {
+ if (ast_wait_for_output(fd, timeoutms - elapsed)) {
/* poll returned a fatal error, so bail out immediately. */
return -1;
}
src += n;
elapsed = ast_tvdiff_ms(ast_tvnow(), start);
- if (elapsed > timeoutms) {
+ if (elapsed >= timeoutms) {
/* We've taken too long to write
* This is only an error condition if we haven't finished writing. */
n = len ? -1 : 0;