Re: [PATCH v2 1/2] perl: redirect stderr to /dev/null instead of closing

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH v2 1/2] perl: redirect stderr to /dev/null instead of closing

From: Thomas Rast <hidden>
Date: 2016-06-15 22:56:42

Petr Baudis [off-list ref] writes:
On Fri, Apr 05, 2013 at 11:57:19AM -0700, Junio C Hamano wrote:
  The thing is, I was confused about dup2() all along as my old UNIX
masters taught me that I must close() the original descriptor first
and since that's what's commonly done anyway, I never thought to
double-check. Now I did and I learned something new, thanks!
Indeed, that's the crucial point here.  dup2() is defined to close the
original FD first if needed.

It's much saner this way for the case of stderr, as there is no time
when we have no stderr available to report errors: the FD is replace
atomically from the POV of the program.

The manpage for dup2 does, however, say

   If newfd was open, any errors  that  would  have  been  reported  at
   close(2) time are lost.  A careful programmer will not use dup2() or
   dup3() without closing newfd first.

which is probably what you were referring to.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

Re: [PATCH v2 1/2] perl: redirect stderr to /dev/null instead of closing

From: Petr Baudis <hidden>
Date: 2016-06-15 22:56:42

On Sat, Apr 06, 2013 at 10:07:40AM +0200, Thomas Rast wrote:
The manpage for dup2 does, however, say

   If newfd was open, any errors  that  would  have  been  reported  at
   close(2) time are lost.  A careful programmer will not use dup2() or
   dup3() without closing newfd first.

which is probably what you were referring to.
Yes, that's probably one reason why I had this stuck in my mind (though,
how often does anyone bother to detect errors on close()...? ;-).

Funnily enough, POSIX.2008 specifies that if closing newfd would fail,
dup2() reports EIO and newfd is not closed, eliminating this problem.

The manpage does not cover this; well, that's fair enough as Linux just
doesn't care and never does that if I didn't miss anything in the code.

-- 
			Petr "Pasky who might even send
				a patch, but the matter is
				oh so obscure" Baudis
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help