mirror of
https://github.com/transmission/transmission
synced 2025-02-12 17:34:40 +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 )
|
tr_daemon( int nochdir, int noclose )
|
||||||
{
|
{
|
||||||
#if defined(USE_OS_DAEMON)
|
#if defined(USE_OS_DAEMON)
|
||||||
|
|
||||||
return daemon( nochdir, noclose );
|
return daemon( nochdir, noclose );
|
||||||
|
|
||||||
#elif defined(USE_TR_DAEMON)
|
#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( );
|
/* this is loosely based off of glibc's daemon() implementation
|
||||||
if( pid < 0 )
|
* http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=misc/daemon.c */
|
||||||
return -1;
|
|
||||||
else if( pid > 0 )
|
|
||||||
_exit( 0 );
|
|
||||||
else {
|
|
||||||
|
|
||||||
if( !nochdir )
|
switch( fork( ) ) {
|
||||||
if( chdir( "/" ) < 0 )
|
case -1: return -1;
|
||||||
return -1;
|
case 0: break;
|
||||||
|
default: _exit(0);
|
||||||
umask( (mode_t)0 );
|
|
||||||
|
|
||||||
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 );
|
|
||||||
}
|
|
||||||
close( fd );
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( setsid( ) == -1 )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if( !nochdir )
|
||||||
|
chdir( "/" );
|
||||||
|
|
||||||
|
if( !noclose ) {
|
||||||
|
int fd = open( "/dev/null", O_RDWR, 0 );
|
||||||
|
dup2( fd, STDIN_FILENO );
|
||||||
|
dup2( fd, STDOUT_FILENO );
|
||||||
|
dup2( fd, STDERR_FILENO );
|
||||||
|
close( fd );
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
#else /* USE_NO_DAEMON */
|
#else /* USE_NO_DAEMON */
|
||||||
return 0;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue