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

Re: How to handle terminal detection in a daemon calling git?

From
TTravis <git@castle.fastmail.fm>
Date
Jun 1, 2012, 13:52 UTC
Message-ID
<827737B4-F5EE-4FD3-9B1A-710047836080@castle.fastmail.fm>
In-Reply-To
<20120601095333.GF32340@sigill.intra.peff.net>
On Jun 1, 2012, at 4:53 AM, Jeff King wrote:
Show 27 quoted lines
> On Thu, May 31, 2012 at 08:39:42AM -0500, Travis P wrote:
>
>> Here's what I learned this morning: it appears to work when I don't
>> close STDIN.
>>
>>    #close $_ for *STDIN, *STDOUT, *STDERR; # What I was doing. Fails.
>>    close $_ for *STDOUT, *STDERR;  # Tried this, it works.
>>    *STDOUT = $log_fh;
>>    *STDERR = $log_fh;
>
> Yeah, don't do that.  This can cause subtle bugs in subprocesses. For
> example:
>
>  1. You don't have a descriptor 0, because it is closed.
>
>  2. Some part of the program opens a new descriptor (e.g., to read a
>     file, making a pipe, etc). This becomes descriptor 0, because it  
> is
>     the lowest unused descriptor.
>
>  3. The program wants to redirect its stdin (e.g., because it is
>     forking and exec'ing a child). So it calls dup2(fd, 0), closing
>     what was at stdin previously, which might have been valuable.
>
> The right thing to do is to redirect stdin from /dev/null, not close  
> it
> entirely.

Ah, yes, that's a normal thing I would think about in C. In Perl, I imagined that those details were handled by Perl.

With that in mind, I'm still seeing strange behavior when I do this, where it looks to me like I'm closing and then immediately assigning STDIN:

  my $null_in_fh;
  open($null_in_fh, '<', '/dev/null') or die;
  close *STDIN;  # this appears to mess things up, even with the  
following assignment
  *STDIN = $null_in_fh;

But if I don't do the close STDIN (with or without the glob), then things work. Maybe that assignment doesn't work for some Perl-ish reason.

Because, if I just do this
   open(STDIN, '<', '/dev/null') or die;
even after the close and/or assignment, then all appears okay.

I'll stick with this last technique and just chalk it up to something to something about Perl I don't understand. (I had the idea that maybe I wanted to redirect /dev/null to STDIN at some point anyway.)

Thanks for your comments.
-Travis
Previous: Jeff KingNext: Jeff King
Message 6 of 9 in “How to handle terminal detection in a daemon calling git?”
  1. Travis PMay 30, 2012
  2. Jeff KingMay 31, 2012
  3. Junio C HamanoMay 31, 2012
  4. Travis PMay 31, 2012
  5. Jeff KingJun 1, 2012
  6. TravisJun 1, 2012
  7. Jeff KingJun 2, 2012
  8. Travis PMay 31, 2012
  9. Sitaram ChamartyMay 31, 2012

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.