]> git.sur5r.net Git - openldap/blob - libraries/liblutil/detach.c
Change LDAP.ld_lberoptions to `short', to realign with BerElement.ber_options
[openldap] / libraries / liblutil / 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
17 #include <ac/signal.h>
18 #include <ac/unistd.h>
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <sys/file.h>
24 #include <sys/ioctl.h>
25
26 #include "lutil.h"
27
28 /* I'd really like to make do_close an fd_set, but that isn't portable. */
29 void
30 lutil_detach( int debug, int do_close )
31 {
32         int             i, sd, nbits;
33
34 #ifdef HAVE_SYSCONF
35         nbits = sysconf( _SC_OPEN_MAX );
36 #elif HAVE_GETDTABLESIZE
37         nbits = getdtablesize();
38 #else
39         nbits = FD_SETSIZE;
40 #endif
41
42 #ifdef FD_SETSIZE
43         if ( nbits > FD_SETSIZE ) {
44                 nbits = FD_SETSIZE;
45         }
46 #endif /* FD_SETSIZE */
47
48         if ( debug == 0 ) {
49                 for ( i = 0; i < 5; i++ ) {
50 #if HAVE_THR
51                         switch ( fork1() )
52 #else
53                         switch ( fork() )
54 #endif
55                         {
56                         case -1:
57                                 sleep( 5 );
58                                 continue;
59
60                         case 0:
61                                 break;
62
63                         default:
64                                 _exit( 0 );
65                         }
66                         break;
67                 }
68
69                 if ( do_close )
70                         for ( i = 3; i < nbits; i++ )
71                                 close( i );
72
73                 (void) chdir( "/" );
74
75                 if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
76                         perror( "/dev/null" );
77                         exit( 1 );
78                 }
79                 for ( i = 0;  i < 3;  i++ )
80                         if ( sd != i && isatty( i ) )
81                                 (void) dup2( sd, i );
82                 if ( sd > 2 )
83                         close( sd );
84
85 #ifdef HAVE_SETSID
86                 (void) setsid();
87 #else /* HAVE_SETSID */
88                 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
89                         (void) ioctl( sd, TIOCNOTTY, NULL );
90                         (void) close( sd );
91                 }
92 #endif /* HAVE_SETSID */
93         } 
94
95         (void) SIGNAL( SIGPIPE, SIG_IGN );
96 }