From 65d76d7db43bc059288f6d603574685bceeea044 Mon Sep 17 00:00:00 2001 From: Hallvard Furuseth Date: Thu, 30 Jan 2003 22:44:53 +0000 Subject: [PATCH] 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. --- libraries/liblutil/detach.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/libraries/liblutil/detach.c b/libraries/liblutil/detach.c index 022c2dbd78..3692a46de0 100644 --- a/libraries/liblutil/detach.c +++ b/libraries/liblutil/detach.c @@ -72,21 +72,26 @@ 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"); - } - - /* close stdin, stdout, stderr */ - close( STDIN_FILENO ); - close( STDOUT_FILENO ); - close( STDERR_FILENO ); + } else { + /* redirect stdin, stdout, stderr to /dev/null */ + dup2( sd, STDIN_FILENO ); + dup2( sd, STDOUT_FILENO ); + dup2( sd, STDERR_FILENO ); - /* redirect stdin, stdout, stderr to /dev/null */ - dup2( sd, STDIN_FILENO ); - dup2( sd, STDOUT_FILENO ); - dup2( sd, STDERR_FILENO ); - - close( sd ); + switch( sd ) { + default: + close( sd ); + case STDIN_FILENO: + case STDOUT_FILENO: + case STDERR_FILENO: + break; + } + } if ( do_close ) { /* close everything else */ -- 2.39.5