From 5b7e6df00b4227d1a4485ab0c983f80715f63326 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 14 Sep 2014 17:43:19 -0400 Subject: [PATCH] Util - The execute() function needed to fill the STDIN pipe before the fork, not after the fork, so the data is ready before the child process reads. --- src/Hooks.cpp | 2 +- src/util.cpp | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 2a835c2b8..4515df3a5 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -160,7 +160,7 @@ void Hooks::onExit () std::vector changes; context.tdb2.get_changes (changes); - std::string input; + std::string input = ""; std::vector ::const_iterator t; for (t = changes.begin (); t != changes.end (); ++t) input += t->composeJSON () + "\n"; diff --git a/src/util.cpp b/src/util.cpp index 51ffff114..a3b439e7c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -271,6 +271,17 @@ int execute ( pipe (pin); pipe (pout); + // Write input to fp, before the fork. + if (input != "") + { + FILE* pinf = fdopen (pin[1], "w"); + if (pinf) + { + fputs (input.c_str (), pinf); + fclose (pinf); + } + } + pid_t pid = fork (); if (pid == 0) { @@ -278,7 +289,7 @@ int execute ( dup2 (pin[0], STDIN_FILENO); dup2 (pout[1], STDOUT_FILENO); - char** argv = new char*[args.size () + 1]; + char** argv = new char* [args.size () + 1]; for (unsigned int i = 0; i < args.size (); ++i) argv[i] = (char*) args[i].c_str (); @@ -287,32 +298,22 @@ int execute ( } // This is only reached in the parent - close (pin[0]); - close (pout[1]); - - // Write input to fp. - FILE* pinf = fdopen (pin[1], "w"); - if (input != "" && - input != "\n") - { - fputs (input.c_str (), pinf); - } - - fclose (pinf); - close (pin[1]); + close (pin[0]); // Close the read end of the input pipe. + close (pout[1]); // Close the write end if the output pipe. + close (pin[1]); // Close the write end of the input pipe. // Read output from fp. output = ""; char* line = NULL; size_t len = 0; - FILE* poutf = fdopen(pout[0], "r"); + FILE* poutf = fdopen (pout[0], "r"); while (getline (&line, &len, poutf) != -1) output += line; free (line); line = NULL; fclose (poutf); - close (pout[0]); + close (pout[0]); // Close the read-end of the output pipe. int status = -1; wait (&status);