{"thread":{"id":"12510","subject":"[PATCH] run-command: Redirect stderr to a pipe before redirecting stdout to stderr","startedAt":"2008-03-05T07:35:16Z","lastAt":"2008-03-06T06:42:25Z","messageCount":5,"participants":["Christian Couder","Johannes Sixt","Xavier Maillard","Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"71039","messageId":"20080305083516.e1a2a139.chriscool@tuxfamily.org","threadId":"12510","inReplyTo":null,"subject":"[PATCH] run-command: Redirect stderr to a pipe before redirecting stdout to stderr","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2008-03-05T07:35:16Z","receivedAt":"2008-03-05T07:35:16Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"With this patch, in the 'start_command' function after forking\nwe now take care of stderr in the child process before stdout.\n\nThis way if 'start_command' is called with a 'child_process'\nargument like this:\n\n\t.err = -1;\n\t.stdout_to_stderr = 1;\n\nthen stderr will be redirected to a pipe before stdout is\nredirected to stderr. So we can now get the process' stdout\nfrom the pipe (as well as its stderr).\n\nEarlier such a call would have redirected stdout to stderr\nbefore stderr was itself redirected, and therefore stdout\nwould not have followed stderr, which would not have been\nvery useful anyway.\n\nUpdate documentation in 'api-run-command.txt' accordingly.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n Documentation/technical/api-run-command.txt |    7 ++++---\n run-command.c                               |   14 +++++++-------\n 2 files changed, 11 insertions(+), 10 deletions(-)\n\n\tChanges since previous version:\n\n\t- improved patch justification as requested by Junio,\n\t- s/before/after/ in 'api-run-command.txt' (ooops).\n\ndiff --git a/Documentation/technical/api-run-command.txt b/Documentation/technical/api-run-command.txt\nindex fde3b45..c364a22 100644\n--- a/Documentation/technical/api-run-command.txt\n+++ b/Documentation/technical/api-run-command.txt\n@@ -111,9 +111,10 @@ stderr as follows:\n \t.no_stdin, .no_stdout, .no_stderr: The respective channel is\n \t\tredirected to /dev/null.\n \n-\t.stdout_to_stderr: stdout of the child is redirected to the\n-\t\tparent's stderr (i.e. *not* to what .err or\n-\t\t.no_stderr specify).\n+\t.stdout_to_stderr: stdout of the child is redirected to its\n+\t\tstderr. This happens after stderr is itself redirected.\n+\t\tSo stdout will follow stderr to wherever it is\n+\t\tredirected.\n \n To modify the environment of the sub-process, specify an array of\n string pointers (NULL terminated) in .env:\ndiff --git a/run-command.c b/run-command.c\nindex 743757c..44100a7 100644\n--- a/run-command.c\n+++ b/run-command.c\n@@ -91,6 +91,13 @@ int start_command(struct child_process *cmd)\n \t\t\tclose(cmd->in);\n \t\t}\n \n+\t\tif (cmd->no_stderr)\n+\t\t\tdup_devnull(2);\n+\t\telse if (need_err) {\n+\t\t\tdup2(fderr[1], 2);\n+\t\t\tclose_pair(fderr);\n+\t\t}\n+\n \t\tif (cmd->no_stdout)\n \t\t\tdup_devnull(1);\n \t\telse if (cmd->stdout_to_stderr)\n@@ -103,13 +110,6 @@ int start_command(struct child_process *cmd)\n \t\t\tclose(cmd->out);\n \t\t}\n \n-\t\tif (cmd->no_stderr)\n-\t\t\tdup_devnull(2);\n-\t\telse if (need_err) {\n-\t\t\tdup2(fderr[1], 2);\n-\t\t\tclose_pair(fderr);\n-\t\t}\n"},{"id":"71040","messageId":"47CE4D73.4050607@viscovery.net","threadId":"12510","inReplyTo":"20080305083516.e1a2a139.chriscool@tuxfamily.org","subject":"Re: [PATCH] run-command: Redirect stderr to a pipe before redirecting stdout to stderr","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2008-03-05T07:36:19Z","receivedAt":"2008-03-05T07:36:19Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Christian Couder schrieb:\n> With this patch, in the 'start_command' function after forking\n> we now take care of stderr in the child process before stdout.\n> \n> This way if 'start_command' is called with a 'child_process'\n> argument like this:\n> \n> \t.err = -1;\n> \t.stdout_to_stderr = 1;\n> \n> then stderr will be redirected to a pipe before stdout is\n> redirected to stderr. So we can now get the process' stdout\n> from the pipe (as well as its stderr).\n> \n> Earlier such a call would have redirected stdout to stderr\n> before stderr was itself redirected, and therefore stdout\n> would not have followed stderr, which would not have been\n> very useful anyway.\n> \n> Update documentation in 'api-run-command.txt' accordingly.\n> \n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n\nFWIW:\n\nAcked-by: Johannes Sixt <johannes.sixt@telecom.at>\n\n-- Hannes\n\n"},{"id":"71161","messageId":"200803060100.m26104Vx004182@localhost.localdomain","threadId":"12510","inReplyTo":"20080305083516.e1a2a139.chriscool@tuxfamily.org","subject":"Re: [PATCH] run-command: Redirect stderr to a pipe before redirecting stdout to stderr","fromName":"Xavier Maillard","fromEmail":"xma@gnu.org","sentAt":"2008-03-06T01:00:05Z","receivedAt":"2008-03-06T01:00:05Z","isPatch":true,"sender":{"key":"xma@gnu.org","avatar":null},"body":"\n   Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n\nTested successfully.\n\nAcked-by: Xavier Maillard <xma@gnu.org>\n\n\tXavier\n-- \nhttp://www.gnu.org\nhttp://www.april.org\nhttp://www.lolica.org\n"},{"id":"71192","messageId":"20080306064415.c5ca762a.chriscool@tuxfamily.org","threadId":"12510","inReplyTo":"20080305083516.e1a2a139.chriscool@tuxfamily.org","subject":"Re: [PATCH] run-command: Redirect stderr to a pipe before redirecting stdout to stderr","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2008-03-06T05:44:15Z","receivedAt":"2008-03-06T05:44:15Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Hi Junio and Shawn,\n\nOn Wed, 5 Mar 2008, I wrote:\n> With this patch, in the 'start_command' function after forking\n> we now take care of stderr in the child process before stdout.\n> ...\n\nI am afraid I forgot the \n\nFrom: \"Shawn O. Pearce\" <spearce@spearce.org>\n\nat the top of the patch in my last resend, so the patch was attributed to me instead of Shawn.\n\nIs it possible to do something about it ? (I fear not since it is now in 'next' but maybe I am wrong.)\n\nThanks in advance,\nChristian.\n"},{"id":"71201","messageId":"20080306064225.GS8410@spearce.org","threadId":"12510","inReplyTo":"20080306064415.c5ca762a.chriscool@tuxfamily.org","subject":"Re: [PATCH] run-command: Redirect stderr to a pipe before redirecting stdout to stderr","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2008-03-06T06:42:25Z","receivedAt":"2008-03-06T06:42:25Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Christian Couder <chriscool@tuxfamily.org> wrote:\n> On Wed, 5 Mar 2008, I wrote:\n> > With this patch, in the 'start_command' function after forking\n> > we now take care of stderr in the child process before stdout.\n> > ...\n> \n> I am afraid I forgot the \n> \n> From: \"Shawn O. Pearce\" <spearce@spearce.org>\n> \n> at the top of the patch in my last resend, so the patch was attributed to me instead of Shawn.\n> \n> Is it possible to do something about it ? (I fear not since it is now in 'next' but maybe I am wrong.)\n\nNope, its done and over with.\n\nDon't sweat it.  It was a very short patch, you wrote it apparently\nindependently from me, so the author line from you is just as valid.\n\nI say its fine as is.  Maybe better.  Your version had an API\ndoc update.  My version is what triggered the \"maybe we should\ndocument the API before we modify it further\" discussion.  ;-)\n\n-- \nShawn.\n"}]}