Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #14 from kblees/kb/redirect-output-to-null
Browse files Browse the repository at this point in the history
Fix menu commands blocking the file manager process
  • Loading branch information
dscho committed Mar 14, 2014
2 parents a6c3759 + c9eca1d commit da97444
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions common/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,48 @@ int exec_program_v(const char *working_directory,
{
int fdout[2], fderr[2];
int s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
int devnull = -1;

pid_t pid;
int status = 0;
int ret;

struct strbuf strout = STRBUF_INIT, strerr = STRBUF_INIT;
if (!output)
output = &strout;
if (!error_output)
error_output = &strerr;

reporter *debug = QUIETMODE & flags ? _debug_git : _debug_git_mbox;

if (!git_path()) {
debug("[ERROR] Could not find git path");
return -1;
}

if (pipe(fdout) < 0) {
return -ERR_RUN_COMMAND_PIPE;
}
s1 = dup(1);
dup2(fdout[1], 1);
if (!output || !error_output)
devnull = open("/dev/null", O_WRONLY);

flags |= WAITMODE;
s1 = dup(1);
if (output) {
if (pipe(fdout) < 0) {
return -ERR_RUN_COMMAND_PIPE;
}
dup2(fdout[1], 1);

if (pipe(fderr) < 0) {
if (output)
close_pair(fdout);
return -ERR_RUN_COMMAND_PIPE;
flags |= WAITMODE;
} else {
dup2(devnull, 1);
}

s2 = dup(2);
dup2(fderr[1], 2);
if (error_output) {
if (pipe(fderr) < 0) {
if (output)
close_pair(fdout);
return -ERR_RUN_COMMAND_PIPE;
}
dup2(fderr[1], 2);

flags |= WAITMODE;
flags |= WAITMODE;
} else {
dup2(devnull, 2);
}
close(devnull);

pid = fork_process(argv[0], argv, working_directory);

Expand All @@ -85,13 +92,17 @@ int exec_program_v(const char *working_directory,
dup2(s2, 2), close(s2);

if (pid < 0) {
close_pair(fdout);
close_pair(fderr);
if (output)
close_pair(fdout);
if (error_output)
close_pair(fderr);
return -ERR_RUN_COMMAND_FORK;
}

close(fdout[1]);
close(fderr[1]);
if (output)
close(fdout[1]);
if (error_output)
close(fderr[1]);

if (WAITMODE & flags) {
ret = wait_for_process(pid, MAX_PROCESSING_TIME,
Expand All @@ -107,11 +118,15 @@ int exec_program_v(const char *working_directory,
status = -1;
}

strbuf_read(output, fdout[0], 0);
debug_git("STDOUT:\r\n%s\r\n*** end of STDOUT ***\r\n", output->buf);
if (output) {
strbuf_read(output, fdout[0], 0);
debug_git("STDOUT:\r\n%s\r\n*** end of STDOUT ***\r\n", output->buf);
}

strbuf_read(error_output, fderr[0], 0);
debug_git("STDERR:\r\n%s\r\n*** end of STDERR ***\r\n", error_output->buf);
if (error_output) {
strbuf_read(error_output, fderr[0], 0);
debug_git("STDERR:\r\n%s\r\n*** end of STDERR ***\r\n", error_output->buf);
}
} else {
status = -ERR_RUN_COMMAND_WAITPID_NOEXIT;
debug_git("[ERROR] process timed out; "
Expand All @@ -121,11 +136,10 @@ int exec_program_v(const char *working_directory,
}
close_process(pid);

close(fdout[0]);
close(fderr[0]);

strbuf_release(&strerr);
strbuf_release(&strout);
if (output)
close(fdout[0]);
if (error_output)
close(fderr[0]);

return status;
}

0 comments on commit da97444

Please sign in to comment.