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 */