From dc7f900626b53a955a4da0bfa765e8abcd61887a Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 15 Dec 2016 15:41:50 -0500 Subject: [PATCH] Tweaked command execution routines to eliminate commands hanging the editor waiting on input or output. Also added a function to cleanup zombie processes --- inc/edit.h | 1 + libedit/exec.c | 25 +++++++++++++++++-------- xedit.c | 1 + 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index cef36be..cbe5230 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -166,6 +166,7 @@ void view_indent(View* view, int dir); /* Command Executions *****************************************************************************/ +void cmdreap(void); int cmdrun(char** cmd, char** err); char* cmdread(char** cmd, char** err); void cmdwrite(char** cmd, char* text, char** err); diff --git a/libedit/exec.c b/libedit/exec.c index be0ee21..0996078 100644 --- a/libedit/exec.c +++ b/libedit/exec.c @@ -5,6 +5,8 @@ #include #include +static uint NumChildren = 0; + #define PIPE_READ 0 #define PIPE_WRITE 1 @@ -51,10 +53,9 @@ static int execute(char** cmd, Process* proc) { return proc->pid; } -static void detach(Process* proc) { - close(proc->in); - close(proc->out); - close(proc->err); +void cmdreap(void) { + while(NumChildren && (waitpid(-1, NULL, WNOHANG) > 0)) + NumChildren--; } int cmdrun(char** cmd, char** err) { @@ -63,8 +64,11 @@ int cmdrun(char** cmd, char** err) { perror("failed to execute"); return -1; } + NumChildren++; if (err) *err = fdgets(proc.err); - detach(&proc); + close(proc.in); + close(proc.out); + close(proc.err); return proc.pid; } @@ -74,9 +78,11 @@ char* cmdread(char** cmd, char** err) { perror("failed to execute"); return NULL; } + close(proc.in); char* str = fdgets(proc.out); + close(proc.out); if (err) *err = fdgets(proc.err); - detach(&proc); + close(proc.err); waitpid(proc.pid, NULL, 0); return str; } @@ -91,8 +97,10 @@ void cmdwrite(char** cmd, char* text, char** err) { perror("failed to write"); return; } + close(proc.in); if (err) *err = fdgets(proc.err); - detach(&proc); + close(proc.err); + close(proc.out); waitpid(proc.pid, NULL, 0); } @@ -108,8 +116,9 @@ char* cmdwriteread(char** cmd, char* text, char** err) { } close(proc.in); char* str = fdgets(proc.out); + close(proc.out); if (err) *err = fdgets(proc.err); - detach(&proc); + close(proc.err); waitpid(proc.pid, NULL, 0); return str; } diff --git a/xedit.c b/xedit.c index d81ebc3..f8ca2f0 100644 --- a/xedit.c +++ b/xedit.c @@ -376,6 +376,7 @@ static void redraw(int width, int height) { draw_status(CLR_BASE1, (width - 4) / x11_font_width(Font)); draw_region(TAGS); draw_region(EDIT); + cmdreap(); // cleanup any zombie child processes } #endif -- 2.49.0