- 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.
This commit is contained in:
Paul Beckingham 2014-09-14 17:43:19 -04:00
parent bfc6e38851
commit 5b7e6df00b
2 changed files with 18 additions and 17 deletions

View file

@ -160,7 +160,7 @@ void Hooks::onExit ()
std::vector <Task> changes; std::vector <Task> changes;
context.tdb2.get_changes (changes); context.tdb2.get_changes (changes);
std::string input; std::string input = "";
std::vector <Task>::const_iterator t; std::vector <Task>::const_iterator t;
for (t = changes.begin (); t != changes.end (); ++t) for (t = changes.begin (); t != changes.end (); ++t)
input += t->composeJSON () + "\n"; input += t->composeJSON () + "\n";

View file

@ -271,6 +271,17 @@ int execute (
pipe (pin); pipe (pin);
pipe (pout); 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 (); pid_t pid = fork ();
if (pid == 0) if (pid == 0)
{ {
@ -278,7 +289,7 @@ int execute (
dup2 (pin[0], STDIN_FILENO); dup2 (pin[0], STDIN_FILENO);
dup2 (pout[1], STDOUT_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) for (unsigned int i = 0; i < args.size (); ++i)
argv[i] = (char*) args[i].c_str (); argv[i] = (char*) args[i].c_str ();
@ -287,32 +298,22 @@ int execute (
} }
// This is only reached in the parent // This is only reached in the parent
close (pin[0]); close (pin[0]); // Close the read end of the input pipe.
close (pout[1]); close (pout[1]); // Close the write end if the output pipe.
close (pin[1]); // Close the write end of the input pipe.
// Write input to fp.
FILE* pinf = fdopen (pin[1], "w");
if (input != "" &&
input != "\n")
{
fputs (input.c_str (), pinf);
}
fclose (pinf);
close (pin[1]);
// Read output from fp. // Read output from fp.
output = ""; output = "";
char* line = NULL; char* line = NULL;
size_t len = 0; size_t len = 0;
FILE* poutf = fdopen(pout[0], "r"); FILE* poutf = fdopen (pout[0], "r");
while (getline (&line, &len, poutf) != -1) while (getline (&line, &len, poutf) != -1)
output += line; output += line;
free (line); free (line);
line = NULL; line = NULL;
fclose (poutf); fclose (poutf);
close (pout[0]); close (pout[0]); // Close the read-end of the output pipe.
int status = -1; int status = -1;
wait (&status); wait (&status);