Skip to content

Commit

Permalink
Better pipe handling (issue #563).
Browse files Browse the repository at this point in the history
  • Loading branch information
joewing committed May 13, 2022
1 parent 9e571f1 commit a0676a9
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,12 @@ char *ReadFromProcess(const char *command, unsigned timeout_ms)
pid = fork();
if(pid == 0) {
/* The child process. */
close(ConnectionNumber(display));
close(fds[0]);
if(display) {
close(ConnectionNumber(display));
}
dup2(fds[1], 1); /* stdout */
close(fds[0]);
close(fds[1]);
setsid();
execl(SHELL_NAME, SHELL_NAME, "-c", command, NULL);
Warning(_("exec failed: (%s) %s"), SHELL_NAME, command);
Expand All @@ -176,6 +179,7 @@ char *ReadFromProcess(const char *command, unsigned timeout_ms)
unsigned buffer_size, max_size;
TimeType start_time, current_time;

close(fds[1]);
max_size = BLOCK_SIZE;
buffer_size = 0;
buffer = Allocate(max_size);
Expand All @@ -185,13 +189,7 @@ char *ReadFromProcess(const char *command, unsigned timeout_ms)
struct timeval tv;
unsigned long diff_ms;
fd_set fs;
int rc;

/* Make sure we have room to read. */
if(buffer_size + BLOCK_SIZE > max_size) {
max_size *= 2;
buffer = Reallocate(buffer, max_size);
}
int rc, got_read;

FD_ZERO(&fs);
FD_SET(fds[0], &fs);
Expand All @@ -204,7 +202,7 @@ char *ReadFromProcess(const char *command, unsigned timeout_ms)
tv.tv_usec = (diff_ms % 1000) * 1000;

/* Wait for data (or a timeout). */
rc = select(fds[0] + 1, &fs, &fs, &fs, &tv);
rc = select(fds[0] + 1, &fs, NULL, &fs, &tv);
if(rc == 0) {
close(fds[0]);
/* Timeout */
Expand All @@ -215,19 +213,19 @@ char *ReadFromProcess(const char *command, unsigned timeout_ms)
break;
}

rc = read(fds[0], &buffer[buffer_size], BLOCK_SIZE);
if(rc > 0) {
buffer_size += rc;
} else {
/* Process exited, check for any leftovers and return. */
do {
if(buffer_size + BLOCK_SIZE > max_size) {
max_size *= 2;
buffer = Reallocate(buffer, max_size);
}
rc = read(fds[0], &buffer[buffer_size], BLOCK_SIZE);
buffer_size += (rc > 0) ? rc : 0;
} while(rc > 0);
got_read = 0;
do {
/* Make sure we have room to read. */
if(buffer_size + BLOCK_SIZE > max_size) {
max_size *= 2;
buffer = Reallocate(buffer, max_size);
}
rc = read(fds[0], &buffer[buffer_size], BLOCK_SIZE);
buffer_size += (rc > 0) ? rc : 0;
got_read = got_read || rc > 0;
} while(rc > 0);
if(!got_read) {
/* Process exited */
close(fds[0]);
break;
}
Expand Down

0 comments on commit a0676a9

Please sign in to comment.