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
Jeff King <peff@peff.net>
Date
Jun 2, 2012, 16:51 UTC
Message-ID
<20120602165132.GE15017@sigill.intra.peff.net>
In-Reply-To
<827737B4-F5EE-4FD3-9B1A-710047836080@castle.fastmail.fm>
On Fri, Jun 01, 2012 at 08:52:04AM -0500, Travis wrote:
Show 9 quoted lines
> 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;

Keep in mind that STDIN is a perl filehandle, not a file descriptor. When you close it, you close the filehandle and its underlying descriptor. Then you assign another filehandle to STDIN, which has its own descriptor.

Try this:
  strace perl -e '
    open($null, "</dev/null");
    close *STDIN;
    *STDIN = $null;
    <STDIN>;
  '
you'll see that it is doing something like:
  open("/dev/null", O_RDONLY)             = 3
  close(0)                                = 0
  read(3, "", 8192)                       = 0

The final read comes from the new descriptor, and we never re-opened descriptor 0. You are basically just pointing the name STDIN to a new handle, not doing anything with the underlying descriptor.

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

Yeah, this is the right way to re-open a descriptor in perl, because open() does the magic to dup2 the newly opened descriptor onto STDIN's descriptor.

> Thanks for your comments.
No problem. Glad it is working now.
-Peff
Previous: TravisNext: Travis P
Message 7 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.