transmission/daemon/daemon.c

285 lines
7.9 KiB
C
Raw Normal View History

2008-05-18 16:44:30 +00:00
/*
2008-12-16 00:20:44 +00:00
* This file Copyright (C) 2008 Charles Kerr <charles@transmissionbt.com>
2007-04-18 16:39:10 +00:00
*
2008-05-18 16:44:30 +00:00
* This file is licensed by the GPL version 2. Works owned by the
* Transmission project are granted a special exemption to clause 2(b)
* so that the bulk of its code can remain under the MIT license.
2008-05-18 16:44:30 +00:00
* This exemption does not extend to derived works not owned by
* the Transmission project.
2007-04-18 16:39:10 +00:00
*
2008-05-19 16:16:38 +00:00
* $Id$
2008-05-18 16:44:30 +00:00
*/
2007-04-18 16:39:10 +00:00
#include <assert.h>
#include <errno.h>
#include <stdio.h> /* printf */
2008-05-18 16:44:30 +00:00
#include <stdlib.h> /* exit, atoi */
#include <string.h> /* strcmp */
#include <fcntl.h> /* open */
2007-04-18 16:39:10 +00:00
#include <signal.h>
#include <unistd.h> /* daemon */
2008-05-18 16:44:30 +00:00
#include <libtransmission/transmission.h>
#include <libtransmission/bencode.h>
2008-07-08 16:50:34 +00:00
#include <libtransmission/tr-getopt.h>
#include <libtransmission/utils.h>
#include <libtransmission/version.h>
2008-05-18 16:44:30 +00:00
#define MY_NAME "transmission-daemon"
2007-04-18 16:39:10 +00:00
2008-10-28 19:49:33 +00:00
static int closing = FALSE;
static tr_session * mySession = NULL;
/***
**** Config File
***/
2008-07-08 16:50:34 +00:00
static const char *
getUsage( void )
{
return "Transmission " LONG_VERSION_STRING
" http://www.transmissionbt.com/\n"
2008-07-08 16:50:34 +00:00
"A fast and easy BitTorrent client\n"
"\n"
MY_NAME " is a headless Transmission session\n"
"that can be controlled via transmission-remote or Clutch.\n"
"\n"
"Usage: " MY_NAME " [options]";
2008-07-08 16:50:34 +00:00
}
static const struct tr_option options[] =
{
{ 'a', "allowed", "Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a", 1, "<list>" },
{ 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL },
{ 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL },
{ 'd', "dump-settings", "Dump the settings and exit", "d", 0, NULL },
{ 'f', "foreground", "Run in the foreground instead of daemonizing", "f", 0, NULL },
{ 'g', "config-dir", "Where to look for configuration files", "g", 1, "<path>" },
{ 'p', "port", "RPC port (Default: " TR_DEFAULT_RPC_PORT_STR ")", "p", 1, "<port>" },
{ 't', "auth", "Require authentication", "t", 0, NULL },
{ 'T', "no-auth", "Don't require authentication", "T", 0, NULL },
{ 'u', "username", "Set username for authentication", "u", 1, "<username>" },
{ 'v', "password", "Set password for authentication", "v", 1, "<password>" },
{ 'w', "download-dir", "Where to save downloaded data", "w", 1, "<path>" },
{ 0, NULL, NULL, NULL, 0, NULL }
2008-07-08 16:50:34 +00:00
};
2008-05-18 16:44:30 +00:00
static void
2008-07-08 16:50:34 +00:00
showUsage( void )
2008-05-18 16:44:30 +00:00
{
tr_getopt_usage( MY_NAME, getUsage( ), options );
2007-04-18 16:39:10 +00:00
exit( 0 );
}
2008-05-18 16:44:30 +00:00
static void
gotsig( int sig UNUSED )
{
2008-05-18 16:44:30 +00:00
closing = TRUE;
}
2008-10-14 20:31:16 +00:00
#if !defined( WIN32 )
#if !defined( HAVE_DAEMON )
2008-05-18 16:44:30 +00:00
static int
daemon( int nochdir,
int noclose )
2008-05-18 16:44:30 +00:00
{
switch( fork( ) )
{
2008-05-18 16:44:30 +00:00
case 0:
break;
case - 1:
tr_nerr( MY_NAME, "Error daemonizing (fork)! %d - %s", errno,
strerror(
errno ) );
2008-05-18 16:44:30 +00:00
return -1;
default:
_exit( 0 );
}
if( setsid( ) < 0 )
{
tr_nerr( MY_NAME, "Error daemonizing (setsid)! %d - %s", errno,
strerror(
errno ) );
2007-04-18 16:39:10 +00:00
return -1;
}
switch( fork( ) )
{
2008-05-18 16:44:30 +00:00
case 0:
break;
case - 1:
tr_nerr( MY_NAME, "Error daemonizing (fork2)! %d - %s", errno,
strerror(
errno ) );
2008-05-18 16:44:30 +00:00
return -1;
2008-05-18 16:44:30 +00:00
default:
_exit( 0 );
2007-04-18 16:39:10 +00:00
}
if( !nochdir && 0 > chdir( "/" ) )
{
tr_nerr( MY_NAME, "Error daemonizing (chdir)! %d - %s", errno,
strerror(
errno ) );
2007-04-18 16:39:10 +00:00
return -1;
}
if( !noclose )
{
2008-05-18 16:44:30 +00:00
int fd;
if( ( ( fd = open( "/dev/null", O_RDONLY ) ) ) != 0 )
{
2008-05-18 16:44:30 +00:00
dup2( fd, 0 );
close( fd );
}
if( ( ( fd = open( "/dev/null", O_WRONLY ) ) ) != 1 )
{
2008-05-18 16:44:30 +00:00
dup2( fd, 1 );
close( fd );
}
if( ( ( fd = open( "/dev/null", O_WRONLY ) ) ) != 2 )
{
2008-05-18 16:44:30 +00:00
dup2( fd, 2 );
2007-04-18 16:39:10 +00:00
close( fd );
}
}
2008-05-18 16:44:30 +00:00
return 0;
2007-04-18 16:39:10 +00:00
}
2008-10-14 20:31:16 +00:00
#endif
2008-05-18 16:44:30 +00:00
#endif
2007-04-18 16:39:10 +00:00
static const char*
getConfigDir( int argc, const char ** argv )
{
int c;
const char * configDir = NULL;
const char * optarg;
const int ind = tr_optind;
while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg )))
if( c == 'g' )
configDir = optarg;
tr_optind = ind;
if( configDir == NULL )
configDir = tr_getDefaultConfigDir( MY_NAME );
return configDir;
}
2008-05-18 16:44:30 +00:00
int
main( int argc,
char ** argv )
2007-04-18 16:39:10 +00:00
{
int c;
int64_t i;
const char * optarg;
tr_benc settings;
tr_bool foreground = FALSE;
tr_bool dumpSettings = FALSE;
2008-07-08 16:50:34 +00:00
const char * configDir = NULL;
2008-05-18 16:44:30 +00:00
signal( SIGINT, gotsig );
signal( SIGTERM, gotsig );
#ifndef WIN32
signal( SIGQUIT, gotsig );
2007-04-18 16:39:10 +00:00
signal( SIGPIPE, SIG_IGN );
signal( SIGHUP, SIG_IGN );
#endif
2007-04-18 16:39:10 +00:00
/* load settings from defaults + config file */
tr_bencInitDict( &settings, 0 );
configDir = getConfigDir( argc, (const char**)argv );
tr_sessionLoadSettings( &settings, configDir, MY_NAME );
tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_ENABLED, 1 );
/* overwrite settings from the comamndline */
tr_optind = 1;
while(( c = tr_getopt( getUsage(), argc, (const char**)argv, options, &optarg ))) {
switch( c ) {
case 'a': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_WHITELIST, optarg );
tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, 1 );
break;
case 'b': tr_bencDictAddInt( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, 1 );
break;
case 'B': tr_bencDictAddInt( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, 0 );
break;
case 'd': dumpSettings = TRUE;
break;
case 'f': foreground = TRUE;
break;
case 'g': /* handled above */
break;
case 'p': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_PORT, atoi( optarg ) );
break;
case 't': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, 0 );
break;
case 'T': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, 1 );
break;
case 'u': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_USERNAME, optarg );
break;
case 'v': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_PASSWORD, optarg );
break;
case 'w': tr_bencDictAddStr( &settings, TR_PREFS_KEY_DOWNLOAD_DIR, optarg );
break;
default: showUsage( );
break;
}
}
2007-04-18 16:39:10 +00:00
if( dumpSettings )
{
char * str = tr_bencSaveAsJSON( &settings, NULL );
fprintf( stderr, "%s", str );
tr_free( str );
return 0;
}
2008-10-14 20:31:16 +00:00
#ifndef WIN32
if( !foreground )
{
if( 0 > daemon( 1, 0 ) )
{
2008-05-18 16:44:30 +00:00
fprintf( stderr, "failed to daemonize: %s\n", strerror( errno ) );
exit( 1 );
}
2007-04-18 16:39:10 +00:00
}
2008-10-14 20:31:16 +00:00
#endif
2007-08-14 04:02:50 +00:00
/* start the session */
mySession = tr_sessionInit( "daemon", configDir, FALSE, &settings );
if( tr_bencDictFindInt( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &i ) && i!=0 )
tr_ninf( MY_NAME, "requiring authentication" );
/* load the torrents */
{
tr_ctor * ctor = tr_ctorNew( mySession );
tr_torrent ** torrents = tr_sessionLoadTorrents( mySession, ctor, NULL );
tr_free( torrents );
tr_ctorFree( ctor );
}
2007-08-14 04:02:50 +00:00
2008-05-18 16:44:30 +00:00
while( !closing )
tr_wait( 1000 ); /* sleep one second */
2007-08-14 04:02:50 +00:00
/* shutdown */
2008-05-18 16:44:30 +00:00
printf( "Closing transmission session..." );
tr_sessionSaveSettings( mySession, configDir, &settings );
tr_sessionClose( mySession );
2008-05-18 16:44:30 +00:00
printf( " done.\n" );
2007-08-14 04:02:50 +00:00
/* cleanup */
tr_bencFree( &settings );
2007-08-14 04:02:50 +00:00
return 0;
}