Reuse get_string_from_pipe() in log_from_pipe().

This commit is contained in:
George Kadianakis 2011-07-18 17:06:16 +02:00
parent 51cdd30c01
commit 69271b2a38
2 changed files with 38 additions and 55 deletions

View File

@ -3173,6 +3173,12 @@ get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
if (buf_out[len - 1] == '\n') {
/* Remove the trailing newline */
buf_out[len - 1] = '\0';
} else {
/* No newline; check whether we overflowed the buffer */
if (!feof(stream))
log_warn(LD_GENERAL,
"Line from stream was truncated: %s", buf_out);
/* TODO: What to do with this error? */
}
return IO_STREAM_OKAY;
@ -3192,65 +3198,44 @@ log_from_pipe(FILE *stream, int severity, const char *executable,
int *child_status)
{
char buf[256];
enum stream_status r;
for (;;) {
char *retval;
retval = fgets(buf, sizeof(buf), stream);
r = get_string_from_pipe(stream, buf, sizeof(buf) - 1);
if (NULL == retval) {
if (feof(stream)) {
/* Program has closed stream (probably it exited) */
/* TODO: check error */
fclose(stream);
return 1;
if (r == IO_STREAM_CLOSED) {
fclose(stream);
return 1;
} else if (r == IO_STREAM_EAGAIN) {
return 0;
} else if (r == IO_STREAM_TERM) {
fclose(stream);
return -1;
}
tor_assert(r == IO_STREAM_OKAY);
/* Check if buf starts with SPAWN_ERROR_MESSAGE */
if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
/* Parse error message */
int retval, child_state, saved_errno;
retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
&child_state, &saved_errno);
if (retval == 2) {
log_warn(LD_GENERAL,
"Failed to start child process \"%s\" in state %d: %s",
executable, child_state, strerror(saved_errno));
if (child_status)
*child_status = 1;
} else {
if (EAGAIN == errno) {
/* Nothing more to read, try again next time */
return 0;
} else {
/* There was a problem, abandon this child process */
fclose(stream);
return -1;
}
/* Failed to parse message from child process, log it as a
warning */
log_warn(LD_GENERAL,
"Unexpected message from port forwarding helper \"%s\": %s",
executable, buf);
}
} else {
/* We have some data, log it and keep asking for more */
size_t len;
len = strlen(buf);
if (buf[len - 1] == '\n') {
/* Remove the trailing newline */
buf[len - 1] = '\0';
} else {
/* No newline; check whether we overflowed the buffer */
if (!feof(stream))
log_warn(LD_GENERAL,
"Line from port forwarding helper was truncated: %s", buf);
/* TODO: What to do with this error? */
}
/* Check if buf starts with SPAWN_ERROR_MESSAGE */
if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
/* Parse error message */
int retval, child_state, saved_errno;
retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
&child_state, &saved_errno);
if (retval == 2) {
log_warn(LD_GENERAL,
"Failed to start child process \"%s\" in state %d: %s",
executable, child_state, strerror(saved_errno));
if (child_status)
*child_status = 1;
} else {
/* Failed to parse message from child process, log it as a
warning */
log_warn(LD_GENERAL,
"Unexpected message from port forwarding helper \"%s\": %s",
executable, buf);
}
} else {
log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
}
log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
}
}

View File

@ -157,8 +157,6 @@ configure_proxy(managed_proxy_t *mp)
char stdout_buf[200];
while (1) {
memset(stdout_buf, 0, sizeof(stdout_buf));
r = get_string_from_pipe(mp->stdout, stdout_buf,
sizeof(stdout_buf) - 1);