5 kx
5 kx #ifdef HAVE_CONFIG_H
5 kx #include <config.h>
5 kx #endif
5 kx
5 kx #include <fcntl.h>
5 kx #include <paths.h>
5 kx #include <unistd.h>
5 kx
5 kx #include <daemon.h>
5 kx
5 kx int daemon( int nochdir, int noclose )
5 kx {
5 kx int fd;
5 kx
5 kx switch( fork() )
5 kx {
5 kx case -1:
5 kx return( -1 );
5 kx case 0:
5 kx break;
5 kx default:
5 kx _exit( 0 ); /* direct use kernel exit */
5 kx }
5 kx
5 kx if( setsid() == -1 ) return( -1 );
5 kx if( !nochdir ) chdir( "/" );
5 kx if( noclose ) return( 0 );
5 kx
5 kx fd = open( _PATH_DEVNULL, O_RDWR, 0 );
5 kx if( fd != -1 )
5 kx {
5 kx dup2( fd, STDIN_FILENO );
5 kx dup2( fd, STDOUT_FILENO );
5 kx dup2( fd, STDERR_FILENO );
5 kx if( fd > 2 ) close( fd );
5 kx }
5 kx return( 0 );
5 kx }