]> git.sur5r.net Git - openldap/blob - servers/slurpd/detach.c
Fixup bugs created by merge.
[openldap] / servers / slurpd / 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/unistd.h>
18 #include <ac/signal.h>
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <sys/file.h>
24 #include <sys/ioctl.h>
25
26 #include "portable.h"
27
28 detach()
29 {
30         int             i, sd, nbits;
31 #ifdef LDAP_DEBUG
32         extern int      ldap_debug;
33 #endif
34
35 #ifdef HAVE_SYSCONF
36         nbits = sysconf( _SC_OPEN_MAX );
37 #elif HAVE_GETDTABLESIZE
38         nbits = getdtablesize();
39 #else
40         nbits = FD_SETSIZE;
41 #endif
42
43 #ifdef FD_SETSIZE
44         if ( nbits > FD_SETSIZE ) {
45                 nbits = FD_SETSIZE;
46         }
47 #endif /* FD_SETSIZE */
48
49 #ifdef LDAP_DEBUG
50         if ( ldap_debug == 0 ) {
51 #endif
52                 for ( i = 0; i < 5; i++ ) {
53 #if HAVE_THR
54                         switch ( fork1() ) {
55 #else
56                         switch ( fork() ) {
57 #endif
58                         case -1:
59                                 sleep( 5 );
60                                 continue;
61
62                         case 0:
63                                 break;
64
65                         default:
66                                 _exit( 0 );
67                         }
68                         break;
69                 }
70
71 /*
72                 for ( i = 3; i < nbits; i++ )
73                         close( i );
74 */
75
76                 (void) chdir( "/" );
77
78                 if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
79                         perror( "/dev/null" );
80                         exit( 1 );
81                 }
82                 if ( isatty( 0 ) )
83                         (void) dup2( sd, 0 );
84                 if ( isatty( 1 ) )
85                         (void) dup2( sd, 1 );
86                 if ( isatty(2) )
87                         (void) dup2( sd, 2 );
88                 close( sd );
89
90 #ifdef HAVE_SETSID
91                 setsid();
92 #else /* HAVE_SETSID */
93                 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
94                         (void) ioctl( sd, TIOCNOTTY, NULL );
95                         (void) close( sd );
96                 }
97 #endif /* HAVE_SETSID */
98 #ifdef LDAP_DEBUG
99         } 
100 #endif
101
102         (void) SIGNAL( SIGPIPE, SIG_IGN );
103 }