mirror of
https://github.com/transmission/transmission
synced 2025-02-12 09:25:03 +00:00
(trunk daemon) simplify the code in the fallback implementation of daemon() for systems that don't have this function themselves.
This commit is contained in:
parent
e7fc697c90
commit
f8b739f0f9
1 changed files with 25 additions and 37 deletions
|
@ -173,48 +173,36 @@ static int
|
|||
tr_daemon( int nochdir, int noclose )
|
||||
{
|
||||
#if defined(USE_OS_DAEMON)
|
||||
return daemon( nochdir, noclose );
|
||||
#elif defined(USE_TR_DAEMON)
|
||||
pid_t pid = fork( );
|
||||
if( pid < 0 )
|
||||
return -1;
|
||||
else if( pid > 0 )
|
||||
_exit( 0 );
|
||||
else {
|
||||
pid = setsid( );
|
||||
if( pid < 0 )
|
||||
return -1;
|
||||
|
||||
pid = fork( );
|
||||
if( pid < 0 )
|
||||
return daemon( nochdir, noclose );
|
||||
|
||||
#elif defined(USE_TR_DAEMON)
|
||||
|
||||
/* this is loosely based off of glibc's daemon() implementation
|
||||
* http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=misc/daemon.c */
|
||||
|
||||
switch( fork( ) ) {
|
||||
case -1: return -1;
|
||||
case 0: break;
|
||||
default: _exit(0);
|
||||
}
|
||||
|
||||
if( setsid( ) == -1 )
|
||||
return -1;
|
||||
else if( pid > 0 )
|
||||
_exit( 0 );
|
||||
else {
|
||||
|
||||
if( !nochdir )
|
||||
if( chdir( "/" ) < 0 )
|
||||
return -1;
|
||||
|
||||
umask( (mode_t)0 );
|
||||
chdir( "/" );
|
||||
|
||||
if( !noclose ) {
|
||||
/* send stdin, stdout, and stderr to /dev/null */
|
||||
int i;
|
||||
int fd = open( "/dev/null", O_RDWR, 0 );
|
||||
if( fd < 0 )
|
||||
fprintf( stderr, "unable to open /dev/null: %s\n", tr_strerror(errno) );
|
||||
for( i=0; i<3; ++i ) {
|
||||
if( close( i ) )
|
||||
return -1;
|
||||
dup2( fd, i );
|
||||
}
|
||||
dup2( fd, STDIN_FILENO );
|
||||
dup2( fd, STDOUT_FILENO );
|
||||
dup2( fd, STDERR_FILENO );
|
||||
close( fd );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#else /* USE_NO_DAEMON */
|
||||
return 0;
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue