From: Jeff King Date: Sat, 02 Jun 2012 16:51:32 GMT Subject: Re: How to handle terminal detection in a daemon calling git? 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: > 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, "; ' 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