]> git.sur5r.net Git - openldap/blob - servers/slapd/detach.c
More header work toward draft-ietf-ldapext-ldap-c-api-01.
[openldap] / servers / slapd / detach.c
1 /*
2  * Copyright (c) 1990, 1994 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/signal.h>
18 #include <ac/unistd.h>
19
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <sys/file.h>
23 #include <sys/ioctl.h>
24
25 detach()
26 {
27         int             i, sd, nbits;
28 #ifdef LDAP_DEBUG
29         extern int      ldap_debug;
30 #endif
31
32 #ifdef HAVE_SYSCONF
33         nbits = sysconf( _SC_OPEN_MAX );
34 #elif HAVE_GETDTABLESIZE
35         nbits = getdtablesize();
36 #else
37         nbits = FD_SETSIZE
38 #endif 
39
40 #ifdef FD_SETSIZE
41         if ( nbits > FD_SETSIZE ) {
42                 nbits = FD_SETSIZE;
43         }
44 #endif /* FD_SETSIZE */
45
46 #ifdef LDAP_DEBUG
47         if ( ldap_debug == 0 ) {
48 #endif
49                 for ( i = 0; i < 5; i++ ) {
50 #if defined( HAVE_LWP_THR )
51                         switch ( fork1() ) {
52 #else
53                         switch ( fork() ) {
54 #endif
55                         case -1:
56                                 sleep( 5 );
57                                 continue;
58
59                         case 0:
60                                 break;
61
62                         default:
63                                 _exit( 0 );
64                         }
65                         break;
66                 }
67
68 /*
69                 for ( i = 3; i < nbits; i++ )
70                         close( i );
71 */
72
73                 (void) chdir( "/" );
74
75                 if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
76                         perror( "/dev/null" );
77                         exit( 1 );
78                 }
79                 if ( isatty( 0 ) )
80                         (void) dup2( sd, 0 );
81                 if ( isatty( 1 ) )
82                         (void) dup2( sd, 1 );
83                 if ( isatty(2) )
84                         (void) dup2( sd, 2 );
85                 close( sd );
86
87 #ifdef HAVE_SETSID
88                 setsid();
89 #else /* HAVE_SETSID */
90                 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
91                         (void) ioctl( sd, TIOCNOTTY, NULL );
92                         (void) close( sd );
93                 }
94 #endif /* HAVE_SETSID */
95 #ifdef LDAP_DEBUG
96         } 
97 #endif
98
99         (void) SIGNAL( SIGPIPE, SIG_IGN );
100 }