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