the_Foundation [release-1.2]
Process: Reading output
f0e23e03b51c4ef42f8220ed3d68b034bf3f86fb
[1mdiff --git a/src/platform/posix/process.c b/src/platform/posix/process.c[m
[1mindex 135c5d2..28f1678 100644[m
[1m--- a/src/platform/posix/process.c[m
[1m+++ b/src/platform/posix/process.c[m
[36m@@ -39,6 +39,7 @@[m [mSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</small>[m
#include <unistd.h>[m
#include <signal.h>[m
#include <sys/wait.h>[m
[32m+[m[32m#include <errno.h>[m
#if defined (iPlatformOther)[m
# include <sys/select.h>[m
#endif[m
[36m@@ -145,6 +146,9 @@[m [miBool start_Process(iProcess *d) {[m
}[m
/* Start the child process. */[m
rc = posix_spawn(&d->pid, argv[0], &facts, NULL, iConstCast(char **, argv), envs);[m
[32m+[m[32m if (rc != 0) {[m
[32m+[m[32m iWarning("[Process] spawn error: %d %s\n", errno, strerror(errno));[m
[32m+[m[32m }[m
free(argv);[m
setCwd_Path(oldCwd);[m
delete_String(oldCwd);[m
[36m@@ -178,6 +182,7 @@[m [mint exitStatus_Process(const iProcess *d) {[m
[m
void waitForFinished_Process(iProcess *d) {[m
if (!d->pid) return;[m
[32m+[m[32m close(input_Pipe(&d->pin)); /* no more input */[m
int ws = 0;[m
waitpid(d->pid, &ws, 0);[m
if (WIFEXITED(ws)) {[m
[36m@@ -197,14 +202,13 @@[m [msize_t writeInput_Process(iProcess *d, const iBlock *data) {[m
}[m
else break;[m
}[m
[31m- close(input_Pipe(&d->pin));[m
return size_Block(data) - remain;[m
}[m
[m
static iBlock *readFromPipe_(int fd, iBlock *readChars) {[m
char buf[4096];[m
[31m- struct pollfd pfd = {.fd = fd, .events = POLLIN};[m
[31m- while (poll(&pfd, 1, 0) == 1) { // non-blocking[m
[32m+[m[32m struct pollfd pfd = { .fd = fd, .events = POLLIN };[m
[32m+[m[32m while (poll(&pfd, 1, 0) == 1) { /* non-blocking */[m
if (pfd.revents & POLLIN) {[m
ssize_t num = read(fd, buf, sizeof(buf));[m
if (num > 0) {[m
[36m@@ -234,34 +238,18 @@[m [mvoid kill_Process(iProcess *d) {[m
iBlock *readOutputUntilClosed_Process(iProcess *d) {[m
iBlock *output = new_Block(0);[m
const int fd = output_Pipe(&d->pout);[m
[31m- int flags = fcntl(fd, F_GETFL, 0);[m
[31m- fcntl(fd, F_SETFL, flags | O_NONBLOCK);[m
[32m+[m[32m close(input_Pipe(&d->pin)); /* no more input */[m
for (;;) {[m
[31m- fd_set reads, errs;[m
[31m- FD_ZERO(&reads);[m
[31m- FD_ZERO(&errs);[m
[31m- FD_SET(fd, &reads);[m
[31m- FD_SET(fd, &errs);[m
[31m- const int rc = select(fd + 1, &reads, NULL, &errs, NULL);[m
[31m- if (rc > 0) {[m
[31m- if (FD_ISSET(fd, &errs)) {[m
[31m- break;[m
[31m- }[m
[31m- if (FD_ISSET(fd, &reads)) {[m
[31m- char buf[0x20000];[m
[31m- ssize_t len = 0;[m
[31m- do {[m
[31m- len = read(fd, buf, sizeof(buf));[m
[31m- if (len > 0) {[m
[31m- appendData_Block(output, buf, len);[m
[31m- }[m
[31m- else if (len == 0) {[m
[31m- return output;[m
[31m- }[m
[31m- } while (len > 0);[m
[31m- }[m
[32m+[m[32m char buf[0x20000];[m
[32m+[m[32m ssize_t len = read(fd, buf, sizeof(buf));[m
[32m+[m[32m if (len > 0) {[m
[32m+[m[32m appendData_Block(output, buf, len);[m
[32m+[m[32m continue;[m
}[m
[31m- else break;[m
[32m+[m[32m if (len < 0 && isEmpty_Block(output)) {[m
[32m+[m[32m iWarning("[Process] failed to read output: %s\n", strerror(errno));[m
[32m+[m[32m }[m
[32m+[m[32m break; /* EOF */[m
}[m
return output;[m
}[m