{"thread":{"id":"7180","subject":"[PATCH 4/8] Split run_command into two halves (start/finish)","startedAt":"2007-03-10T08:28:05Z","lastAt":"2007-03-10T08:28:05Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":8},"messages":[{"id":"36738","messageId":"20070310082805.GD4133@spearce.org","threadId":"7180","inReplyTo":null,"subject":"[PATCH 4/8] Split run_command into two halves (start/finish)","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-10T08:28:05Z","receivedAt":"2007-03-10T08:28:05Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"If the calling process wants to send data to stdin of a\nchild process it will need to arrange for a pipe and get\nthe child process running, feed data to it, then wait\nfor the child process to finish.  So we split the run\nfunction into two halves, allowing callers to first\nstart the child then later finish it.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n run-command.c |   26 +++++++++++++++++++-------\n run-command.h |    3 +++\n 2 files changed, 22 insertions(+), 7 deletions(-)\n\ndiff --git a/run-command.c b/run-command.c\nindex 1fb7fa5..a866a06 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -2,13 +2,12 @@\n #include \"run-command.h\"\n #include \"exec_cmd.h\"\n \n-int run_command(struct child_process *cmd)\n+int start_command(struct child_process *cmd)\n {\n-\tpid_t pid = fork();\n-\n-\tif (pid < 0)\n+\tcmd->pid = fork();\n+\tif (cmd->pid < 0)\n \t\treturn -ERR_RUN_COMMAND_FORK;\n-\tif (!pid) {\n+\tif (!cmd->pid) {\n \t\tif (cmd->no_stdin) {\n \t\t\tint fd = open(\"/dev/null\", O_RDWR);\n \t\t\tdup2(fd, 0);\n@@ -23,9 +22,14 @@ int run_command(struct child_process *cmd)\n \t\t}\n \t\tdie(\"exec %s failed.\", cmd->argv[0]);\n \t}\n+\treturn 0;\n+}\n+\n+int finish_command(struct child_process *cmd)\n+{\n \tfor (;;) {\n \t\tint status, code;\n-\t\tpid_t waiting = waitpid(pid, &status, 0);\n+\t\tpid_t waiting = waitpid(cmd->pid, &status, 0);\n \n \t\tif (waiting < 0) {\n \t\t\tif (errno == EINTR)\n@@ -33,7 +37,7 @@ int run_command(struct child_process *cmd)\n \t\t\terror(\"waitpid failed (%s)\", strerror(errno));\n \t\t\treturn -ERR_RUN_COMMAND_WAITPID;\n \t\t}\n-\t\tif (waiting != pid)\n+\t\tif (waiting != cmd->pid)\n \t\t\treturn -ERR_RUN_COMMAND_WAITPID_WRONG_PID;\n \t\tif (WIFSIGNALED(status))\n \t\t\treturn -ERR_RUN_COMMAND_WAITPID_SIGNAL;\n@@ -47,6 +51,14 @@ int run_command(struct child_process *cmd)\n \t}\n }\n \n+int run_command(struct child_process *cmd)\n+{\n+\tint code = start_command(cmd);\n+\tif (code)\n+\t\treturn code;\n+\treturn finish_command(cmd);\n+}\n+\n int run_command_v_opt(const char **argv, int opt)\n {\n \tstruct child_process cmd;\ndiff --git a/run-command.h b/run-command.h\nindex f9db2a7..24cdb4e 100644\n--- a/run-command.h\n+++ b/run-command.h\n@@ -12,11 +12,14 @@ enum {\n \n struct child_process {\n \tconst char **argv;\n+\tpid_t pid;\n \tunsigned no_stdin:1;\n \tunsigned git_cmd:1; /* if this is to be git sub-command */\n \tunsigned stdout_to_stderr:1;\n };\n \n+int start_command(struct child_process *);\n+int finish_command(struct child_process *);\n int run_command(struct child_process *);\n \n #define RUN_COMMAND_NO_STDIN 1\n-- \n1.5.0.3.942.g299f\n"}]}