Home | History | Annotate | Line # | Download | only in liblutil
      1  1.8  christos /*	$NetBSD: detach.c,v 1.9 2025/09/05 21:16:23 christos Exp $	*/
      2  1.2       mrg 
      3  1.1     lukem /* detach.c -- routines to daemonize a process */
      4  1.3      tron /* $OpenLDAP$ */
      5  1.1     lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  1.1     lukem  *
      7  1.9  christos  * Copyright 1998-2024 The OpenLDAP Foundation.
      8  1.1     lukem  * All rights reserved.
      9  1.1     lukem  *
     10  1.1     lukem  * Redistribution and use in source and binary forms, with or without
     11  1.1     lukem  * modification, are permitted only as authorized by the OpenLDAP
     12  1.1     lukem  * Public License.
     13  1.1     lukem  *
     14  1.1     lukem  * A copy of this license is available in the file LICENSE in the
     15  1.1     lukem  * top-level directory of the distribution or, alternatively, at
     16  1.1     lukem  * <http://www.OpenLDAP.org/license.html>.
     17  1.1     lukem  */
     18  1.1     lukem /*
     19  1.1     lukem  * Copyright (c) 1990, 1994 Regents of the University of Michigan.
     20  1.1     lukem  * All rights reserved.
     21  1.1     lukem  *
     22  1.1     lukem  * Redistribution and use in source and binary forms are permitted
     23  1.1     lukem  * provided that this notice is preserved and that due credit is given
     24  1.1     lukem  * to the University of Michigan at Ann Arbor. The name of the University
     25  1.1     lukem  * may not be used to endorse or promote products derived from this
     26  1.1     lukem  * software without specific prior written permission. This software
     27  1.1     lukem  * is provided ``as is'' without express or implied warranty.
     28  1.1     lukem  */
     29  1.1     lukem /* This work was originally developed by the University of Michigan
     30  1.1     lukem  * and distributed as part of U-MICH LDAP.
     31  1.1     lukem  */
     32  1.1     lukem 
     33  1.4  christos #include <sys/cdefs.h>
     34  1.8  christos __RCSID("$NetBSD: detach.c,v 1.9 2025/09/05 21:16:23 christos Exp $");
     35  1.4  christos 
     36  1.1     lukem #include "portable.h"
     37  1.1     lukem 
     38  1.1     lukem #include <stdio.h>
     39  1.1     lukem 
     40  1.1     lukem #include <ac/stdlib.h>
     41  1.1     lukem #include <ac/signal.h>
     42  1.1     lukem #include <ac/socket.h>
     43  1.1     lukem #include <ac/unistd.h>
     44  1.1     lukem 
     45  1.1     lukem #include <sys/stat.h>
     46  1.1     lukem #include <fcntl.h>
     47  1.1     lukem 
     48  1.1     lukem #ifdef HAVE_SYS_FILE_H
     49  1.1     lukem #include <sys/file.h>
     50  1.1     lukem #endif
     51  1.1     lukem #ifdef HAVE_SYS_IOCTL_H
     52  1.1     lukem #include <sys/ioctl.h>
     53  1.1     lukem #endif
     54  1.1     lukem 
     55  1.1     lukem #include "lutil.h"
     56  1.1     lukem 
     57  1.3      tron int
     58  1.1     lukem lutil_detach( int debug, int do_close )
     59  1.1     lukem {
     60  1.3      tron 	int		i, sd, nbits, pid;
     61  1.1     lukem 
     62  1.1     lukem #ifdef HAVE_SYSCONF
     63  1.1     lukem 	nbits = sysconf( _SC_OPEN_MAX );
     64  1.1     lukem #elif defined(HAVE_GETDTABLESIZE)
     65  1.1     lukem 	nbits = getdtablesize();
     66  1.1     lukem #else
     67  1.1     lukem 	nbits = FD_SETSIZE;
     68  1.1     lukem #endif
     69  1.1     lukem 
     70  1.1     lukem #ifdef FD_SETSIZE
     71  1.1     lukem 	if ( nbits > FD_SETSIZE ) {
     72  1.1     lukem 		nbits = FD_SETSIZE;
     73  1.1     lukem 	}
     74  1.1     lukem #endif /* FD_SETSIZE */
     75  1.1     lukem 
     76  1.1     lukem 	if ( debug == 0 ) {
     77  1.1     lukem 		for ( i = 0; i < 5; i++ ) {
     78  1.1     lukem #ifdef HAVE_THR
     79  1.3      tron 			pid = fork1();
     80  1.1     lukem #else
     81  1.3      tron 			pid = fork();
     82  1.1     lukem #endif
     83  1.3      tron 			switch ( pid )
     84  1.1     lukem 			{
     85  1.1     lukem 			case -1:
     86  1.1     lukem 				sleep( 5 );
     87  1.1     lukem 				continue;
     88  1.1     lukem 
     89  1.1     lukem 			case 0:
     90  1.1     lukem 				break;
     91  1.1     lukem 
     92  1.1     lukem 			default:
     93  1.3      tron 				return pid;
     94  1.1     lukem 			}
     95  1.1     lukem 			break;
     96  1.1     lukem 		}
     97  1.1     lukem 
     98  1.1     lukem 		if ( (sd = open( "/dev/null", O_RDWR   )) == -1 &&
     99  1.1     lukem 			 (sd = open( "/dev/null", O_RDONLY )) == -1 &&
    100  1.1     lukem 			 /* Panic -- open *something* */
    101  1.1     lukem 			 (sd = open( "/",         O_RDONLY )) == -1    ) {
    102  1.1     lukem 			perror("/dev/null");
    103  1.1     lukem 		} else {
    104  1.1     lukem 			/* redirect stdin, stdout, stderr to /dev/null */
    105  1.1     lukem 			dup2( sd, STDIN_FILENO );
    106  1.1     lukem 			dup2( sd, STDOUT_FILENO );
    107  1.1     lukem 			dup2( sd, STDERR_FILENO );
    108  1.1     lukem 
    109  1.1     lukem 			switch( sd ) {
    110  1.1     lukem 			default:
    111  1.1     lukem 				close( sd );
    112  1.1     lukem 			case STDIN_FILENO:
    113  1.1     lukem 			case STDOUT_FILENO:
    114  1.1     lukem 			case STDERR_FILENO:
    115  1.1     lukem 				break;
    116  1.1     lukem 			}
    117  1.1     lukem 		}
    118  1.1     lukem 
    119  1.1     lukem 		if ( do_close ) {
    120  1.1     lukem 			/* close everything else */
    121  1.1     lukem 			for ( i = 0; i < nbits; i++ ) {
    122  1.1     lukem 				if( i != STDIN_FILENO &&
    123  1.1     lukem 					i != STDOUT_FILENO &&
    124  1.1     lukem 					i != STDERR_FILENO )
    125  1.1     lukem 				{
    126  1.1     lukem 					close( i );
    127  1.1     lukem 				}
    128  1.1     lukem 			}
    129  1.1     lukem 		}
    130  1.1     lukem 
    131  1.1     lukem #ifdef CHDIR_TO_ROOT
    132  1.1     lukem 		(void) chdir( "/" );
    133  1.1     lukem #endif
    134  1.1     lukem 
    135  1.1     lukem #ifdef HAVE_SETSID
    136  1.1     lukem 		(void) setsid();
    137  1.2       mrg #elif defined(TIOCNOTTY)
    138  1.1     lukem 		if ( (sd = open( "/dev/tty", O_RDWR )) != -1 ) {
    139  1.1     lukem 			(void) ioctl( sd, TIOCNOTTY, NULL );
    140  1.1     lukem 			(void) close( sd );
    141  1.1     lukem 		}
    142  1.1     lukem #endif
    143  1.1     lukem 	}
    144  1.1     lukem 
    145  1.1     lukem #ifdef SIGPIPE
    146  1.1     lukem 	(void) SIGNAL( SIGPIPE, SIG_IGN );
    147  1.1     lukem #endif
    148  1.3      tron 	return 0;
    149  1.1     lukem }
    150