]> git.sur5r.net Git - openldap/blob - libraries/liblutil/detach.c
More contrib cleanout
[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                         perror("/dev/null");
77                 }
78
79                 /* close stdin, stdout, stderr */
80                 close( STDIN_FILENO );
81                 close( STDOUT_FILENO );
82                 close( STDERR_FILENO );
83
84                 /* redirect stdin, stdout, stderr to /dev/null */
85                 dup2( sd, STDIN_FILENO );
86                 dup2( sd, STDOUT_FILENO );
87                 dup2( sd, STDERR_FILENO );
88
89                 close( sd );
90
91                 if ( do_close ) {
92                         /* close everything else */
93                         for ( i = 0; i < nbits; i++ ) {
94                                 if( i != STDIN_FILENO &&
95                                         i != STDOUT_FILENO && 
96                                         i != STDERR_FILENO )
97                                 {
98                                         close( i );
99                                 }
100                         }
101                 }
102
103 #ifdef CHDIR_TO_ROOT
104                 (void) chdir( "/" );
105 #endif
106
107 #ifdef HAVE_SETSID
108                 (void) setsid();
109 #elif TIOCNOTTY
110                 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
111                         (void) ioctl( sd, TIOCNOTTY, NULL );
112                         (void) close( sd );
113                 }
114 #endif
115         } 
116
117 #ifdef SIGPIPE
118         (void) SIGNAL( SIGPIPE, SIG_IGN );
119 #endif
120 }