]> git.sur5r.net Git - openldap/commitdiff
Change lutil_detach() to not close the descriptors before dup2(), try to
authorHallvard Furuseth <hallvard@openldap.org>
Thu, 30 Jan 2003 22:44:53 +0000 (22:44 +0000)
committerHallvard Furuseth <hallvard@openldap.org>
Thu, 30 Jan 2003 22:44:53 +0000 (22:44 +0000)
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

index 022c2dbd787e825e58a2de47ba124e095ec0233e..3692a46de087cb08b169d5b9573db8b81bcd1176 100644 (file)
@@ -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 */