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