3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1990, 1994 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
22 #include <ac/stdlib.h>
23 #include <ac/signal.h>
24 #include <ac/socket.h>
25 #include <ac/unistd.h>
30 #ifdef HAVE_SYS_FILE_H
33 #ifdef HAVE_SYS_IOCTL_H
34 #include <sys/ioctl.h>
40 lutil_detach( int debug, int do_close )
45 nbits = sysconf( _SC_OPEN_MAX );
46 #elif HAVE_GETDTABLESIZE
47 nbits = getdtablesize();
53 if ( nbits > FD_SETSIZE ) {
56 #endif /* FD_SETSIZE */
59 for ( i = 0; i < 5; i++ ) {
74 _exit( EXIT_SUCCESS );
79 if ( (sd = open( "/dev/null", O_RDWR )) == -1 &&
80 (sd = open( "/dev/null", O_RDONLY )) == -1 &&
81 /* Panic -- open *something* */
82 (sd = open( "/", O_RDONLY )) == -1 ) {
85 /* redirect stdin, stdout, stderr to /dev/null */
86 dup2( sd, STDIN_FILENO );
87 dup2( sd, STDOUT_FILENO );
88 dup2( sd, STDERR_FILENO );
101 /* close everything else */
102 for ( i = 0; i < nbits; i++ ) {
103 if( i != STDIN_FILENO &&
104 i != STDOUT_FILENO &&
119 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
120 (void) ioctl( sd, TIOCNOTTY, NULL );
127 (void) SIGNAL( SIGPIPE, SIG_IGN );