From a358c4ac366d82befa3b0b6d3f93bce8606bc9a4 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 6 Jan 2019 14:46:32 -0500 Subject: [PATCH] switched to non blocking I/O and fixed a bug in the poll event configuration --- src/lib/job.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/lib/job.c b/src/lib/job.c index df078c2..ef8df23 100644 --- a/src/lib/job.c +++ b/src/lib/job.c @@ -23,7 +23,8 @@ static Job* JobList = NULL; 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; @@ -42,12 +43,13 @@ static void pipe_read(Job* job) { 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; @@ -75,7 +77,7 @@ static void job_process(int fd, int events) { 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)) @@ -83,6 +85,7 @@ static void job_process(int fd, int events) { if (!job->readfn && !job->writefn) job_finish(job); } +#include static int job_exec(char** cmd, int* p_pid) { int pid, fds[2]; @@ -103,6 +106,7 @@ static int job_exec(char** cmd, int* p_pid) { 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]; @@ -115,8 +119,8 @@ bool job_poll(int ms) { 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 */ -- 2.51.0