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 ) {
79 /* close stdin, stdout, stderr */
80 close( STDIN_FILENO );
81 close( STDOUT_FILENO );
82 close( STDERR_FILENO );
84 /* redirect stdin, stdout, stderr to /dev/null */
85 dup2( sd, STDIN_FILENO );
86 dup2( sd, STDOUT_FILENO );
87 dup2( sd, STDERR_FILENO );
92 /* close everything else */
93 for ( i = 0; i < nbits; i++ ) {
94 if( i != STDIN_FILENO &&
110 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
111 (void) ioctl( sd, TIOCNOTTY, NULL );
118 (void) SIGNAL( SIGPIPE, SIG_IGN );