]> git.sur5r.net Git - openldap/blob - libraries/liblutil/detach.c
Merge in all -devel changes made since branch was created.
[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/stdlib.h>
18 #include <ac/signal.h>
19 #include <ac/socket.h>
20 #include <ac/unistd.h>
21
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25 #ifdef HAVE_SYS_FILE_H
26 #include <sys/file.h>
27 #endif
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
30 #endif
31
32 #include "lutil.h"
33
34 void
35 lutil_detach( int debug, int do_close )
36 {
37         int             i, sd, nbits;
38
39 #ifdef HAVE_SYSCONF
40         nbits = sysconf( _SC_OPEN_MAX );
41 #elif HAVE_GETDTABLESIZE
42         nbits = getdtablesize();
43 #else
44         nbits = FD_SETSIZE;
45 #endif
46
47 #ifdef FD_SETSIZE
48         if ( nbits > FD_SETSIZE ) {
49                 nbits = FD_SETSIZE;
50         }
51 #endif /* FD_SETSIZE */
52
53         if ( debug == 0 ) {
54                 for ( i = 0; i < 5; i++ ) {
55 #if HAVE_THR
56                         switch ( fork1() )
57 #else
58                         switch ( fork() )
59 #endif
60                         {
61                         case -1:
62                                 sleep( 5 );
63                                 continue;
64
65                         case 0:
66                                 break;
67
68                         default:
69                                 _exit( EXIT_SUCCESS );
70                         }
71                         break;
72                 }
73
74                 if ( do_close )
75                         for ( i = 3; i < nbits; i++ )
76                                 close( i );
77
78                 (void) chdir( "/" );
79
80                 if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
81                         perror( "/dev/null" );
82                         exit( EXIT_FAILURE );
83                 }
84                 for ( i = 0;  i < 3;  i++ )
85                         if ( sd != i && isatty( i ) )
86                                 (void) dup2( sd, i );
87                 if ( sd > 2 )
88                         close( sd );
89
90 #ifdef HAVE_SETSID
91                 (void) setsid();
92 #elif TIOCNOTTY
93                 if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
94                         (void) ioctl( sd, TIOCNOTTY, NULL );
95                         (void) close( sd );
96                 }
97 #endif
98         } 
99
100 #ifdef SIGPIPE
101         (void) SIGNAL( SIGPIPE, SIG_IGN );
102 #endif
103 }