]> git.sur5r.net Git - openldap/blob - libraries/liblutil/detach.c
Fixed liblber ber_get_next trickle bug (ITS#2490)
[openldap] / libraries / liblutil / detach.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1990, 1994 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14 #include "portable.h"
15
16 #include <stdio.h>
17
18 #include <ac/stdlib.h>
19 #include <ac/signal.h>
20 #include <ac/socket.h>
21 #include <ac/unistd.h>
22
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #ifdef HAVE_SYS_FILE_H
27 #include <sys/file.h>
28 #endif
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
31 #endif
32
33 #include "lutil.h"
34
35 void
36 lutil_detach( int debug, int do_close )
37 {
38         int             i, sd, nbits;
39
40 #ifdef HAVE_SYSCONF
41         nbits = sysconf( _SC_OPEN_MAX );
42 #elif HAVE_GETDTABLESIZE
43         nbits = getdtablesize();
44 #else
45         nbits = FD_SETSIZE;
46 #endif
47
48 #ifdef FD_SETSIZE
49         if ( nbits > FD_SETSIZE ) {
50                 nbits = FD_SETSIZE;
51         }
52 #endif /* FD_SETSIZE */
53
54         if ( debug == 0 ) {
55                 for ( i = 0; i < 5; i++ ) {
56 #if HAVE_THR
57                         switch ( fork1() )
58 #else
59                         switch ( fork() )
60 #endif
61                         {
62                         case -1:
63                                 sleep( 5 );
64                                 continue;
65
66                         case 0:
67                                 break;
68
69                         default:
70                                 _exit( EXIT_SUCCESS );
71                         }
72                         break;
73                 }
74
75                 if ( (sd = open( "/dev/null", O_RDWR   )) == -1 &&
76                          (sd = open( "/dev/null", O_RDONLY )) == -1 &&
77                          /* Panic -- open *something* */
78                          (sd = open( "/",         O_RDONLY )) == -1    ) {
79                         perror("/dev/null");
80                 } else {
81                         /* redirect stdin, stdout, stderr to /dev/null */
82                         dup2( sd, STDIN_FILENO );
83                         dup2( sd, STDOUT_FILENO );
84                         dup2( sd, STDERR_FILENO );
85
86                         switch( sd ) {
87                         default:
88                                 close( sd );
89                         case STDIN_FILENO:
90                         case STDOUT_FILENO:
91                         case STDERR_FILENO:
92                                 break;
93                         }
94                 }
95
96                 if ( do_close ) {
97                         /* close everything else */
98                         for ( i = 0; i < nbits; i++ ) {
99                                 if( i != STDIN_FILENO &&
100                                         i != STDOUT_FILENO && 
101                                         i != STDERR_FILENO )
102                                 {
103                                         close( i );
104                                 }
105                         }
106                 }
107
108 #ifdef CHDIR_TO_ROOT
109                 (void) chdir( "/" );
110 #endif
111
112 #ifdef HAVE_SETSID
113                 (void) setsid();
114 #elif TIOCNOTTY
115                 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
116                         (void) ioctl( sd, TIOCNOTTY, NULL );
117                         (void) close( sd );
118                 }
119 #endif
120         } 
121
122 #ifdef SIGPIPE
123         (void) SIGNAL( SIGPIPE, SIG_IGN );
124 #endif
125 }