git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] start_command(), if .in/.out > 0, closes file descriptors, not the callers

From
Johannes Sixt <johannes.sixt@telecom.at>
Date
Feb 17, 2008, 09:29 UTC
Message-ID
<200802171029.17850.johannes.sixt@telecom.at>
In-Reply-To
<7vmyq07bqe.fsf@gitster.siamese.dyndns.org>
On Saturday 16 February 2008 23:55, Junio C Hamano wrote:
Show 9 quoted lines
> Johannes Sixt <johannes.sixt@telecom.at> writes:
> >  	need_in = !cmd->no_stdin && cmd->in < 0;
> >  	if (need_in) {
> > -		if (pipe(fdin) < 0)
> > +		if (pipe(fdin) < 0) {
> > +			if (cmd->out > 1)
> > +				close(cmd->out);
>
> Why check for "2 or more"?
Later in the code, where we set up the redirections in the child process, we 
have this:
		...
		} else if (cmd->out > 1) {
			dup2(cmd->out, 1);
			close(cmd->out);
		}

and I thought it was good to also compare cmd->out > 1 in other situations. But now that I think about it, this > 1 is to be understood like this:

		if (cmd->out > 0)
			dup2(cmd->out, 1) unless cmd->out == 1;

so it's an optimization - an unnecessary one since dup2(1, 1) is supposed to succeed and be a noop.

Show 11 quoted lines
> > +	 * - Specify > 0 to give away a FD as follows:
> > +	 *     .in: a readable FD, becomes child's stdin
> > +	 *     .out: a writable FD, becomes child's stdout/stderr
> > +	 *     .err > 0 not supported
> > +	 *   The specified FD is closed by start_command(), even in case
> > +	 *   of errors!
>
> Perhaps you would need to spell out the semantic differences you
> are assigning to "inherit" vs "give away".  I presume the former
> is something run_command() would not touch vs the latter is
> closed by run_command()?
I'll clearify it.
-- Hannes
Previous: Shawn O. PearceNext: Johannes Sixt
Message 12 of 15 in “Automatically close stderr pipes created by run_command”
  1. 2/6 Automatically close stderr pipes created by run_commandShawn O. Pearce, Feb 14, 2008
  2. Johannes SixtFeb 14, 2008
  3. Junio C HamanoFeb 15, 2008
  4. Johannes SixtFeb 15, 2008
  5. Junio C HamanoFeb 15, 2008
  6. start_command(), .in/.out/.err = -1: Callers must close the file descriptorJohannes Sixt, Feb 16, 2008
  7. start_command(), if .in/.out > 0, closes file descriptors, not the callersJohannes Sixt, Feb 16, 2008
  8. Junio C HamanoFeb 16, 2008
  9. Shawn O. PearceFeb 17, 2008
  10. Junio C HamanoFeb 17, 2008
  11. Shawn O. PearceFeb 17, 2008
  12. Johannes SixtFeb 17, 2008
  13. start_command(), if .in/.out > 0, closes file descriptors, not the callersJohannes Sixt, Feb 21, 2008
  14. Junio C HamanoFeb 22, 2008
  15. Junio C HamanoFeb 16, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.