/* ******************************************************************************* * Copyright (c) 1995 Martin Poole * ******************************************************************************* ** ** WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! ** ** Any changes to be made to this file should first be checked with ** mplib1 source control for library integrity. ** ** mplib1 source control can be reached at mplib1@quatermass.co.uk ** * * System : * Subsystem : * Module : * $Source$ * $Author$ * $Date$ * $Revision$ * Purpose : * ******************************************************************************* * * Change History * * $Log$ * ******************************************************************************* */ #ident "$Header$" /* ------------------------------------------------------------------ Include files ------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include /* ------------------------------------------------------------------ structures / defines / variables ------------------------------------------------------------------ */ static char stdin_name[]="STDIN"; static char stdout_name[]="STDOUT"; static char stderr_name[]="STDERR"; static char def_name[]="/dev/null"; /* ------------------------------------------------------------------ Code starts here ------------------------------------------------------------------ */ static char * get_one_name( const char *from ) { char *cp; cp = eval_config_default( from, (char *)NULL ); if (cp==NULL || *cp=='\0') cp = def_name; return(cp); } int restart_files( void ) { freopen( get_one_name( stdin_name ), "r", stdin ); freopen( get_one_name( stdout_name ), "a", stdout ); freopen( get_one_name( stderr_name ), "a", stderr ); setbuf( stderr, NULL ); return(1); } static const char * get_some_name( const char *from ) { if (from==NULL || *from=='\0') from = def_name; return(from); } int restart_these_files( const char *in_name, const char *out_name, const char *err_name ) { freopen( get_some_name( in_name ), "r", stdin ); freopen( get_some_name( out_name ), "a", stdout ); freopen( get_some_name( err_name ), "a", stderr ); setbuf( stderr, NULL ); return(1); } pid_t leavehome( void ) { pid_t t; /* In this function we will attempt to disconnect ourselves from the outside world. 1. We close all standard files, and open supplied ones instead. 2. We fork. The parent dies, and the child continues. 3. We setsid() to make ourselves moderately bullet-proof. */ restart_files( ); /* Now fork */ t = fork(); if (t > (pid_t)0) { /* We are the parent, so time to die */ exit(0); }else if (t == (pid_t)0) { /* Time to setsid */ setsid(); }else { /* oh dear */ } return(t); } /* -- End of File -- */