static void pipe_read(Job* job) {
struct PipeData* pipedata = job->data;
- char buffer[4096];
+ char buffer[16385];
+ errno = 0;
long nread = read(job->fd, buffer, sizeof(buffer)-1);
if (nread <= 0) {
job->readfn = NULL;
static void pipe_write(Job* job) {
struct PipeData* pipedata = job->data;
char* chunk = pipedata->data + pipedata->nwrite;
+ errno = 0;
long nwrite = write(job->fd, chunk, pipedata->ndata);
if (nwrite >= 0) {
pipedata->ndata -= nwrite;
pipedata->nwrite += nwrite;
}
- if (nwrite < 0 || pipedata->ndata <= 0) {
+ if ((nwrite < 0 && errno != EWOULDBLOCK) || pipedata->ndata <= 0) {
free(pipedata->data);
pipedata->data = NULL;
job->writefn = NULL;
Job* job = JobList; // Get job by fd
while (job && job->fd != fd)
job = job->next;
- if (!job) return;
+ if (!job || !events) return;
if (job->readfn && (events & POLLIN))
job->readfn(job);
if (job->writefn && (events & POLLOUT))
if (!job->readfn && !job->writefn)
job_finish(job);
}
+#include <fcntl.h>
static int job_exec(char** cmd, int* p_pid) {
int pid, fds[2];
exit(execvp(cmd[0], cmd));
} else {
close(fds[1]);
+ fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL, 0) | O_NONBLOCK);
}
if (p_pid) *p_pid = pid;
return fds[0];
JobFds[njobs].fd = job->fd;
JobFds[njobs].events = 0;
JobFds[njobs].revents = 0;
- if (job->readfn) JobFds[njobs].events = POLLIN;
- if (job->writefn) JobFds[njobs].events = POLLOUT;
+ if (job->readfn) JobFds[njobs].events |= POLLIN;
+ if (job->writefn) JobFds[njobs].events |= POLLOUT;
if (JobFds[njobs].events) njobs++;
}
/* Poll until a job is ready, call the functions based on events */