Change lutil_detach() to not close the descriptors before dup2(), try to

open /dev/null and then / in read-only mode if opening /dev/null failed,
and skip the dup2()s as well if open() failed.
This commit is contained in:
Hallvard Furuseth 2003-01-30 22:44:53 +00:00
parent 23c575805a
commit 65d76d7db4

View File

@ -72,22 +72,27 @@ lutil_detach( int debug, int do_close )
break;
}
if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
if ( (sd = open( "/dev/null", O_RDWR )) == -1 &&
(sd = open( "/dev/null", O_RDONLY )) == -1 &&
/* Panic -- open *something* */
(sd = open( "/", O_RDONLY )) == -1 ) {
perror("/dev/null");
} else {
/* redirect stdin, stdout, stderr to /dev/null */
dup2( sd, STDIN_FILENO );
dup2( sd, STDOUT_FILENO );
dup2( sd, STDERR_FILENO );
switch( sd ) {
default:
close( sd );
case STDIN_FILENO:
case STDOUT_FILENO:
case STDERR_FILENO:
break;
}
}
/* close stdin, stdout, stderr */
close( STDIN_FILENO );
close( STDOUT_FILENO );
close( STDERR_FILENO );
/* redirect stdin, stdout, stderr to /dev/null */
dup2( sd, STDIN_FILENO );
dup2( sd, STDOUT_FILENO );
dup2( sd, STDERR_FILENO );
close( sd );
if ( do_close ) {
/* close everything else */
for ( i = 0; i < nbits; i++ ) {