3 * Copyright (c) 1990, 1994 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/stdlib.h>
19 #include <ac/signal.h>
20 #include <ac/socket.h>
21 #include <ac/unistd.h>
26 #ifdef HAVE_SYS_FILE_H
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
36 lutil_detach( int debug, int do_close )
41 nbits = sysconf( _SC_OPEN_MAX );
42 #elif HAVE_GETDTABLESIZE
43 nbits = getdtablesize();
49 if ( nbits > FD_SETSIZE ) {
52 #endif /* FD_SETSIZE */
55 for ( i = 0; i < 5; i++ ) {
70 _exit( EXIT_SUCCESS );
75 if ( (sd = open( "/dev/null", O_RDWR )) == -1 &&
76 (sd = open( "/dev/null", O_RDONLY )) == -1 &&
77 /* Panic -- open *something* */
78 (sd = open( "/", O_RDONLY )) == -1 ) {
81 /* redirect stdin, stdout, stderr to /dev/null */
82 dup2( sd, STDIN_FILENO );
83 dup2( sd, STDOUT_FILENO );
84 dup2( sd, STDERR_FILENO );
97 /* close everything else */
98 for ( i = 0; i < nbits; i++ ) {
99 if( i != STDIN_FILENO &&
100 i != STDOUT_FILENO &&
115 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
116 (void) ioctl( sd, TIOCNOTTY, NULL );
123 (void) SIGNAL( SIGPIPE, SIG_IGN );