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