2008-05-18 16:44:30 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.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)
|
2008-09-23 19:11:04 +00:00
|
|
|
* 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>
|
2008-09-23 19:11:04 +00:00
|
|
|
#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>
|
2008-10-13 22:26:02 +00:00
|
|
|
#include <unistd.h> /* daemon */
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/bencode.h>
|
2008-09-05 14:31:58 +00:00
|
|
|
#include <libtransmission/rpcimpl.h>
|
2008-07-08 16:50:34 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2008-05-22 19:24:11 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
#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-09-23 19:11:04 +00:00
|
|
|
static int closing = FALSE;
|
2008-05-22 19:24:11 +00:00
|
|
|
static tr_handle * mySession;
|
2008-10-14 03:03:29 +00:00
|
|
|
static char * myConfigFilename = NULL;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-10-01 22:59:29 +00:00
|
|
|
#define KEY_BLOCKLIST "blocklist-enabled"
|
|
|
|
#define KEY_DOWNLOAD_DIR "download-dir"
|
|
|
|
#define KEY_ENCRYPTION "encryption"
|
|
|
|
#define KEY_LAZY_BITFIELD "lazy-bitfield-enabled"
|
|
|
|
#define KEY_PEER_LIMIT "max-peers-global"
|
|
|
|
#define KEY_PEER_PORT "peer-port"
|
|
|
|
#define KEY_PORT_FORWARDING "port-forwarding-enabled"
|
|
|
|
#define KEY_PEX_ENABLED "pex-enabled"
|
|
|
|
#define KEY_AUTH_REQUIRED "rpc-authentication-required"
|
|
|
|
#define KEY_USERNAME "rpc-username"
|
|
|
|
#define KEY_PASSWORD "rpc-password"
|
|
|
|
#define KEY_WHITELIST "rpc-whitelist"
|
|
|
|
#define KEY_WHITELIST_ENABLED "rpc-whitelist-enabled"
|
|
|
|
#define KEY_RPC_PORT "rpc-port"
|
|
|
|
#define KEY_DSPEED "download-limit"
|
|
|
|
#define KEY_DSPEED_ENABLED "download-limit-enabled"
|
|
|
|
#define KEY_USPEED "upload-limit"
|
|
|
|
#define KEY_USPEED_ENABLED "upload-limit-enabled"
|
2008-06-11 20:09:36 +00:00
|
|
|
|
2008-10-01 22:59:29 +00:00
|
|
|
#define CONFIG_FILE "settings.json"
|
2008-06-11 20:09:36 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
/***
|
|
|
|
**** Config File
|
|
|
|
***/
|
2008-06-11 20:09:36 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
replaceInt( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
int64_t value )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
|
|
|
tr_bencDictRemove( dict, key );
|
|
|
|
tr_bencDictAddInt( dict, key, value );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
replaceStr( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
const char* value )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
|
|
|
tr_bencDictRemove( dict, key );
|
|
|
|
tr_bencDictAddStr( dict, key, value );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
2008-06-11 20:09:36 +00:00
|
|
|
saveState( tr_session * s )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i, n = 0;
|
|
|
|
char * strs[4];
|
2008-07-16 22:45:28 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_benc d;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
if( tr_bencLoadJSONFile( myConfigFilename, &d ) )
|
|
|
|
tr_bencInitDict( &d, 16 );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
replaceInt( &d, KEY_BLOCKLIST, tr_blocklistIsEnabled( s ) );
|
|
|
|
replaceStr( &d, KEY_DOWNLOAD_DIR, tr_sessionGetDownloadDir( s ) );
|
|
|
|
replaceInt( &d, KEY_PEER_LIMIT, tr_sessionGetPeerLimit( s ) );
|
|
|
|
replaceInt( &d, KEY_PEER_PORT, tr_sessionGetPeerPort( s ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
replaceInt( &d, KEY_PORT_FORWARDING,
|
|
|
|
tr_sessionIsPortForwardingEnabled( s ) );
|
2008-06-12 16:25:36 +00:00
|
|
|
replaceInt( &d, KEY_PEX_ENABLED, tr_sessionIsPexEnabled( s ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
replaceStr( &d, KEY_USERNAME, strs[n++] =
|
|
|
|
tr_sessionGetRPCUsername(
|
|
|
|
s ) );
|
|
|
|
replaceStr( &d, KEY_PASSWORD, strs[n++] =
|
|
|
|
tr_sessionGetRPCPassword(
|
|
|
|
s ) );
|
2008-10-01 20:23:57 +00:00
|
|
|
replaceStr( &d, KEY_WHITELIST, strs[n++] = tr_sessionGetRPCWhitelist( s ) );
|
2008-06-12 16:25:36 +00:00
|
|
|
replaceInt( &d, KEY_RPC_PORT, tr_sessionGetRPCPort( s ) );
|
|
|
|
replaceInt( &d, KEY_AUTH_REQUIRED, tr_sessionIsRPCPasswordEnabled( s ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
replaceInt( &d, KEY_DSPEED,
|
|
|
|
tr_sessionGetSpeedLimit( s, TR_DOWN ) );
|
|
|
|
replaceInt( &d, KEY_DSPEED_ENABLED,
|
|
|
|
tr_sessionIsSpeedLimitEnabled( s, TR_DOWN ) );
|
2008-06-12 16:25:36 +00:00
|
|
|
replaceInt( &d, KEY_USPEED, tr_sessionGetSpeedLimit( s, TR_UP ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
replaceInt( &d, KEY_USPEED_ENABLED,
|
|
|
|
tr_sessionIsSpeedLimitEnabled( s, TR_UP ) );
|
2008-06-12 16:25:36 +00:00
|
|
|
replaceInt( &d, KEY_ENCRYPTION, tr_sessionGetEncryption( s ) );
|
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
tr_bencSaveJSONFile( myConfigFilename, &d );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_bencFree( &d );
|
2008-06-11 20:09:36 +00:00
|
|
|
tr_ninf( MY_NAME, "saved \"%s\"", myConfigFilename );
|
2008-07-16 22:45:28 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < n; ++i )
|
2008-07-16 22:45:28 +00:00
|
|
|
tr_free( strs[i] );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
getConfigInt( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
int * setme,
|
|
|
|
int defaultVal )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( *setme < 0 )
|
|
|
|
{
|
2008-06-12 16:25:36 +00:00
|
|
|
int64_t i;
|
|
|
|
if( tr_bencDictFindInt( dict, key, &i ) )
|
|
|
|
*setme = i;
|
|
|
|
else
|
|
|
|
*setme = defaultVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
getConfigStr( tr_benc * dict,
|
|
|
|
const char * key,
|
|
|
|
const char ** setme,
|
|
|
|
const char * defaultVal )
|
2008-06-12 16:25:36 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !*setme )
|
|
|
|
{
|
2008-06-12 16:25:36 +00:00
|
|
|
const char * s;
|
|
|
|
if( tr_bencDictFindStr( dict, key, &s ) )
|
|
|
|
*setme = s;
|
|
|
|
else
|
|
|
|
*setme = defaultVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
/**
|
|
|
|
* @param port port number, or -1 if not set in the command line
|
|
|
|
* @param auth TRUE, FALSE, or -1 if not set on the command line
|
|
|
|
* @param blocklist TRUE, FALSE, or -1 if not set on the command line
|
|
|
|
*/
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
session_init( const char * configDir,
|
|
|
|
const char * downloadDir,
|
|
|
|
int rpcPort,
|
2008-10-01 20:23:57 +00:00
|
|
|
const char * whitelist,
|
2008-09-23 19:11:04 +00:00
|
|
|
int authRequired,
|
|
|
|
const char * username,
|
|
|
|
const char * password,
|
|
|
|
int blocklistEnabled )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char mycwd[MAX_PATH_LENGTH];
|
|
|
|
tr_benc state, *dict = NULL;
|
|
|
|
int peerPort = -1, peers = -1;
|
2008-10-01 22:59:29 +00:00
|
|
|
int whitelistEnabled = -1;
|
2008-09-23 19:11:04 +00:00
|
|
|
int pexEnabled = -1;
|
|
|
|
int fwdEnabled = -1;
|
|
|
|
int upLimit = -1, upLimited = -1, downLimit = -1,
|
|
|
|
downLimited = -1;
|
|
|
|
int encryption = -1;
|
|
|
|
int useLazyBitfield = -1;
|
|
|
|
tr_ctor * ctor;
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_torrent ** torrents;
|
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
if( !tr_bencLoadJSONFile( myConfigFilename, &state ) )
|
|
|
|
dict = &state;
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
/***
|
|
|
|
**** Decide on which values to pass into tr_sessionInitFull().
|
|
|
|
**** The command-line arguments are given precedence and
|
|
|
|
**** the state file from the previous session is used as a fallback.
|
|
|
|
**** If neither of those can be found, the TR_DEFAULT fields are used .
|
|
|
|
***/
|
|
|
|
|
2008-10-13 22:26:02 +00:00
|
|
|
tr_getcwd( mycwd, sizeof( mycwd ) );
|
2008-06-12 16:25:36 +00:00
|
|
|
getConfigStr( dict, KEY_DOWNLOAD_DIR, &downloadDir, mycwd );
|
2008-09-23 19:11:04 +00:00
|
|
|
getConfigInt( dict, KEY_PEX_ENABLED, &pexEnabled,
|
|
|
|
TR_DEFAULT_PEX_ENABLED );
|
|
|
|
getConfigInt( dict, KEY_PORT_FORWARDING, &fwdEnabled,
|
|
|
|
TR_DEFAULT_PORT_FORWARDING_ENABLED );
|
|
|
|
getConfigInt( dict, KEY_PEER_PORT, &peerPort,
|
|
|
|
TR_DEFAULT_PORT );
|
2008-06-12 16:25:36 +00:00
|
|
|
getConfigInt( dict, KEY_DSPEED, &downLimit, 100 );
|
|
|
|
getConfigInt( dict, KEY_DSPEED_ENABLED, &downLimited, FALSE );
|
|
|
|
getConfigInt( dict, KEY_USPEED, &upLimit, 100 );
|
|
|
|
getConfigInt( dict, KEY_USPEED_ENABLED, &upLimited, FALSE );
|
2008-09-23 19:11:04 +00:00
|
|
|
getConfigInt( dict, KEY_LAZY_BITFIELD, &useLazyBitfield,
|
|
|
|
TR_DEFAULT_LAZY_BITFIELD_ENABLED );
|
|
|
|
getConfigInt( dict, KEY_PEER_LIMIT, &peers,
|
|
|
|
TR_DEFAULT_GLOBAL_PEER_LIMIT );
|
|
|
|
getConfigInt( dict, KEY_BLOCKLIST, &blocklistEnabled,
|
|
|
|
TR_DEFAULT_BLOCKLIST_ENABLED );
|
|
|
|
getConfigInt( dict, KEY_RPC_PORT, &rpcPort,
|
|
|
|
TR_DEFAULT_RPC_PORT );
|
2008-10-01 22:59:29 +00:00
|
|
|
getConfigInt( dict, KEY_WHITELIST_ENABLED, &whitelistEnabled,
|
|
|
|
TR_DEFAULT_RPC_WHITELIST_ENABLED );
|
2008-10-01 20:23:57 +00:00
|
|
|
getConfigStr( dict, KEY_WHITELIST, &whitelist,
|
|
|
|
TR_DEFAULT_RPC_WHITELIST );
|
2008-06-12 16:25:36 +00:00
|
|
|
getConfigInt( dict, KEY_AUTH_REQUIRED, &authRequired, FALSE );
|
|
|
|
getConfigStr( dict, KEY_USERNAME, &username, NULL );
|
|
|
|
getConfigStr( dict, KEY_PASSWORD, &password, NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
getConfigInt( dict, KEY_ENCRYPTION, &encryption,
|
|
|
|
TR_ENCRYPTION_PREFERRED );
|
2008-06-05 18:16:59 +00:00
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
/* start the session */
|
2008-05-22 19:24:11 +00:00
|
|
|
mySession = tr_sessionInitFull( configDir, "daemon", downloadDir,
|
2008-06-12 16:25:36 +00:00
|
|
|
pexEnabled, fwdEnabled, peerPort,
|
2008-05-22 19:24:11 +00:00
|
|
|
encryption,
|
2008-08-16 21:06:57 +00:00
|
|
|
useLazyBitfield,
|
2008-06-12 16:25:36 +00:00
|
|
|
upLimited, upLimit,
|
|
|
|
downLimited, downLimit,
|
2008-05-22 19:24:11 +00:00
|
|
|
peers,
|
|
|
|
TR_MSG_INF, 0,
|
2008-06-11 20:09:36 +00:00
|
|
|
blocklistEnabled,
|
2008-05-22 19:24:11 +00:00
|
|
|
TR_DEFAULT_PEER_SOCKET_TOS,
|
2008-10-01 22:59:29 +00:00
|
|
|
TRUE, rpcPort,
|
|
|
|
whitelistEnabled, whitelist,
|
|
|
|
authRequired, username, password,
|
2008-06-10 16:16:31 +00:00
|
|
|
TR_DEFAULT_PROXY_ENABLED,
|
|
|
|
TR_DEFAULT_PROXY,
|
2008-07-15 01:03:03 +00:00
|
|
|
TR_DEFAULT_PROXY_PORT,
|
2008-06-11 20:45:53 +00:00
|
|
|
TR_DEFAULT_PROXY_TYPE,
|
2008-06-10 16:16:31 +00:00
|
|
|
TR_DEFAULT_PROXY_AUTH_ENABLED,
|
|
|
|
TR_DEFAULT_PROXY_USERNAME,
|
|
|
|
TR_DEFAULT_PROXY_PASSWORD );
|
|
|
|
|
2008-06-05 18:16:59 +00:00
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
if( authRequired )
|
2008-06-05 18:16:59 +00:00
|
|
|
tr_ninf( MY_NAME, "requiring authentication" );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
/* load the torrents */
|
2008-05-22 19:24:11 +00:00
|
|
|
ctor = tr_ctorNew( mySession );
|
|
|
|
torrents = tr_sessionLoadTorrents( mySession, ctor, NULL );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_free( torrents );
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
if( dict )
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_bencFree( &state );
|
|
|
|
}
|
|
|
|
|
2008-07-08 16:50:34 +00:00
|
|
|
static const char *
|
|
|
|
getUsage( void )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
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"
|
2008-09-23 19:11:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static const struct tr_option options[] =
|
|
|
|
{
|
2008-10-01 20:23:57 +00:00
|
|
|
{ 'a', "allowed",
|
|
|
|
"Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a",
|
2008-09-23 19:11:04 +00:00
|
|
|
1, "<list>" },
|
|
|
|
{ 'b', "blocklist", "Enable peer blocklists",
|
|
|
|
"b", 0, NULL },
|
|
|
|
{ 'B', "no-blocklist", "Disable peer blocklists",
|
|
|
|
"B", 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
|
|
|
{
|
2008-09-23 19:11:04 +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
|
2008-09-23 19:11:04 +00:00
|
|
|
readargs( int argc,
|
|
|
|
const char ** argv,
|
|
|
|
int * nofork,
|
|
|
|
const char ** configDir,
|
|
|
|
const char ** downloadDir,
|
|
|
|
int * rpcPort,
|
2008-10-01 20:23:57 +00:00
|
|
|
const char ** whitelist,
|
2008-09-23 19:11:04 +00:00
|
|
|
int * authRequired,
|
|
|
|
const char ** username,
|
|
|
|
const char ** password,
|
|
|
|
int * blocklistEnabled )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int c;
|
2008-07-08 16:50:34 +00:00
|
|
|
const char * optarg;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
while( ( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ) ) )
|
2008-07-08 16:50:34 +00:00
|
|
|
{
|
|
|
|
switch( c )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
case 'a':
|
2008-10-01 20:23:57 +00:00
|
|
|
*whitelist = optarg; break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
case 'b':
|
|
|
|
*blocklistEnabled = 1; break;
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
*blocklistEnabled = 0; break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
*nofork = 1; break;
|
|
|
|
|
|
|
|
case 'g':
|
|
|
|
*configDir = optarg; break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
*rpcPort = atoi( optarg ); break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
*authRequired = TRUE; break;
|
|
|
|
|
|
|
|
case 'T':
|
|
|
|
*authRequired = FALSE; break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
*username = optarg; break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
*password = optarg; break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
*downloadDir = optarg; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
showUsage( ); break;
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static void
|
|
|
|
gotsig( int sig UNUSED )
|
2008-02-28 19:06:23 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
closing = TRUE;
|
|
|
|
}
|
2008-02-28 19:06:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
#if !defined( HAVE_DAEMON )
|
2008-05-18 16:44:30 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
daemon( int nochdir,
|
|
|
|
int noclose )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( fork( ) )
|
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
case 0:
|
2008-02-28 19:06:23 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
case - 1:
|
|
|
|
tr_nerr( MY_NAME, "Error daemonizing (fork)! %d - %s", errno,
|
|
|
|
strerror(
|
|
|
|
errno ) );
|
2008-05-18 16:44:30 +00:00
|
|
|
return -1;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-28 19:06:23 +00:00
|
|
|
default:
|
2008-09-23 19:11:04 +00:00
|
|
|
_exit( 0 );
|
2008-02-28 19:06:23 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
switch( fork( ) )
|
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
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-09-23 19:11:04 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
default:
|
2008-09-23 19:11:04 +00:00
|
|
|
_exit( 0 );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +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;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !noclose )
|
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int fd;
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( ( fd = open( "/dev/null", O_RDONLY ) ) ) != 0 )
|
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
dup2( fd, 0 );
|
|
|
|
close( fd );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( ( fd = open( "/dev/null", O_WRONLY ) ) ) != 1 )
|
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
dup2( fd, 1 );
|
|
|
|
close( fd );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
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-09-23 19:11:04 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#endif
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
main( int argc,
|
|
|
|
char ** argv )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int nofork = 0;
|
|
|
|
int rpcPort = -1;
|
|
|
|
int authRequired = -1;
|
|
|
|
int blocklistEnabled = -1;
|
|
|
|
char * freeme = NULL;
|
2008-07-08 16:50:34 +00:00
|
|
|
const char * configDir = NULL;
|
|
|
|
const char * downloadDir = NULL;
|
2008-10-01 20:23:57 +00:00
|
|
|
const char * whitelist = NULL;
|
2008-07-08 16:50:34 +00:00
|
|
|
const char * username = NULL;
|
|
|
|
const char * password = NULL;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
signal( SIGINT, gotsig );
|
|
|
|
signal( SIGTERM, gotsig );
|
2008-10-13 22:26:02 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
signal( SIGQUIT, gotsig );
|
2007-04-18 16:39:10 +00:00
|
|
|
signal( SIGPIPE, SIG_IGN );
|
|
|
|
signal( SIGHUP, SIG_IGN );
|
2008-10-13 22:26:02 +00:00
|
|
|
#endif
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-07-08 17:44:33 +00:00
|
|
|
readargs( argc, (const char**)argv, &nofork, &configDir, &downloadDir,
|
2008-10-01 20:23:57 +00:00
|
|
|
&rpcPort, &whitelist, &authRequired, &username, &password,
|
2008-06-11 20:09:36 +00:00
|
|
|
&blocklistEnabled );
|
2008-10-14 03:03:29 +00:00
|
|
|
if( configDir )
|
2008-09-23 19:11:04 +00:00
|
|
|
configDir = freeme = tr_strdup_printf( "%s-daemon",
|
|
|
|
tr_getDefaultConfigDir( ) );
|
2008-10-14 03:03:29 +00:00
|
|
|
myConfigFilename = tr_buildPath( configDir, CONFIG_FILE, NULL );
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !nofork )
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2008-06-11 20:09:36 +00:00
|
|
|
session_init( configDir, downloadDir,
|
2008-10-01 20:23:57 +00:00
|
|
|
rpcPort, whitelist, authRequired, username, password,
|
2008-06-11 20:09:36 +00:00
|
|
|
blocklistEnabled );
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
while( !closing )
|
2008-10-13 22:26:02 +00:00
|
|
|
tr_wait( 1000 ); /* sleep one second */
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2008-05-22 19:24:11 +00:00
|
|
|
saveState( mySession );
|
2008-05-18 16:44:30 +00:00
|
|
|
printf( "Closing transmission session..." );
|
2008-05-22 19:24:11 +00:00
|
|
|
tr_sessionClose( mySession );
|
2008-05-18 16:44:30 +00:00
|
|
|
printf( " done.\n" );
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2008-07-16 22:45:28 +00:00
|
|
|
tr_free( freeme );
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( myConfigFilename );
|
2007-08-14 04:02:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|