mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-21 20:56:39 +00:00
Introduce ast_careful_fwrite() and use in AMI to prevent partial writes.
This patch introduces a function to do careful writes on a file stream which will handle timeouts and partial writes. It is currently used in AMI to address the issue that has been reported. However, there are probably a few other places where this could be used. (closes issue #13546) Reported by: srt Tested by: russell http://reviewboard.digium.com/r/104/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@166282 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -899,30 +899,7 @@ struct ast_variable *astman_get_variables(const struct message *m)
|
||||
*/
|
||||
static int send_string(struct mansession *s, char *string)
|
||||
{
|
||||
int len = strlen(string); /* residual length */
|
||||
char *src = string;
|
||||
struct timeval start = ast_tvnow();
|
||||
int n = 0;
|
||||
|
||||
for (;;) {
|
||||
int elapsed;
|
||||
struct pollfd fd;
|
||||
n = fwrite(src, 1, len, s->f); /* try to write the string, non blocking */
|
||||
if (n == len /* ok */ || n < 0 /* error */)
|
||||
break;
|
||||
len -= n; /* skip already written data */
|
||||
src += n;
|
||||
fd.fd = s->fd;
|
||||
fd.events = POLLOUT;
|
||||
n = -1; /* error marker */
|
||||
elapsed = ast_tvdiff_ms(ast_tvnow(), start);
|
||||
if (elapsed > s->writetimeout)
|
||||
break;
|
||||
if (poll(&fd, 1, s->writetimeout - elapsed) < 1)
|
||||
break;
|
||||
}
|
||||
fflush(s->f);
|
||||
return n < 0 ? -1 : 0;
|
||||
return ast_careful_fwrite(s->f, s->fd, string, strlen(string), s->writetimeout);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
Reference in New Issue
Block a user