]> git.sur5r.net Git - openldap/blob - libraries/liblutil/detach.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[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 ( do_close )
76                         for ( i = 3; i < nbits; i++ )
77                                 close( i );
78
79                 (void) chdir( "/" );
80
81                 if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
82                         perror( "/dev/null" );
83                         exit( EXIT_FAILURE );
84                 }
85                 for ( i = 0;  i < 3;  i++ )
86                         if ( sd != i && isatty( i ) )
87                                 (void) dup2( sd, i );
88                 if ( sd > 2 )
89                         close( sd );
90
91 #ifdef HAVE_SETSID
92                 (void) setsid();
93 #elif TIOCNOTTY
94                 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
95                         (void) ioctl( sd, TIOCNOTTY, NULL );
96                         (void) close( sd );
97                 }
98 #endif
99         } 
100
101 #ifdef SIGPIPE
102         (void) SIGNAL( SIGPIPE, SIG_IGN );
103 #endif
104 }