]> git.sur5r.net Git - openldap/blob - libraries/liblutil/detach.c
72ec99741f2dccdcc0d0c25147af0889be47a4fc
[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 "portable.h"
27
28
29 /* I'd really like to make do_close an fd_set, but that isn't portable. */
30 void
31 lutil_detach( int debug, int do_close )
32 {
33         int             i, sd, nbits;
34
35 #ifdef HAVE_SYSCONF
36         nbits = sysconf( _SC_OPEN_MAX );
37 #elif HAVE_GETDTABLESIZE
38         nbits = getdtablesize();
39 #else
40         nbits = FD_SETSIZE;
41 #endif
42
43 #ifdef FD_SETSIZE
44         if ( nbits > FD_SETSIZE ) {
45                 nbits = FD_SETSIZE;
46         }
47 #endif /* FD_SETSIZE */
48
49         if ( debug == 0 ) {
50                 for ( i = 0; i < 5; i++ ) {
51 #if HAVE_THR
52                         switch ( fork1() )
53 #else
54                         switch ( fork() )
55 #endif
56                         {
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                 if ( do_close )
71                         for ( i = 3; i < nbits; i++ )
72                                 close( i );
73
74                 (void) chdir( "/" );
75
76                 if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
77                         perror( "/dev/null" );
78                         exit( 1 );
79                 }
80                 for ( i = 0;  i < 3;  i++ )
81                         if ( sd != i && isatty( i ) )
82                                 (void) dup2( sd, i );
83                 if ( sd > 2 )
84                         close( sd );
85
86 #ifdef HAVE_SETSID
87                 (void) setsid();
88 #else /* HAVE_SETSID */
89                 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
90                         (void) ioctl( sd, TIOCNOTTY, NULL );
91                         (void) close( sd );
92                 }
93 #endif /* HAVE_SETSID */
94         } 
95
96         (void) SIGNAL( SIGPIPE, SIG_IGN );
97 }