/* 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);
#include <unistd.h>
#include <sys/wait.h>
+static uint NumChildren = 0;
+
#define PIPE_READ 0
#define PIPE_WRITE 1
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) {
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;
}
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;
}
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);
}
}
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;
}
draw_status(CLR_BASE1, (width - 4) / x11_font_width(Font));
draw_region(TAGS);
draw_region(EDIT);
+ cmdreap(); // cleanup any zombie child processes
}
#endif