From: Jeff King <peff@peff.net>
To: Travis <git@castle.fastmail.fm>
Cc: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: Re: How to handle terminal detection in a daemon calling git?
Date: Sat, 2 Jun 2012 12:51:32 -0400 [thread overview]
Message-ID: <20120602165132.GE15017@sigill.intra.peff.net> (raw)
In-Reply-To: <827737B4-F5EE-4FD3-9B1A-710047836080@castle.fastmail.fm>
On Fri, Jun 01, 2012 at 08:52:04AM -0500, Travis wrote:
> 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
next prev parent reply other threads:[~2012-06-02 16:52 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-30 21:16 How to handle terminal detection in a daemon calling git? Travis P
2012-05-31 1:29 ` Jeff King
2012-05-31 6:22 ` Junio C Hamano
2012-05-31 13:39 ` Travis P
2012-06-01 9:53 ` Jeff King
2012-06-01 13:52 ` Travis
2012-06-02 16:51 ` Jeff King [this message]
2012-05-31 13:43 ` Travis P
2012-05-31 3:14 ` Sitaram Chamarty
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120602165132.GE15017@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@castle.fastmail.fm \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).