2008-05-18 16:44:30 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2007-04-18 16:39:10 +00:00
|
|
|
*
|
2010-06-16 14:27:24 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
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 <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 <fcntl.h> /* open */
|
2007-04-18 16:39:10 +00:00
|
|
|
#include <signal.h>
|
2009-10-23 05:00:16 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
2008-10-13 22:26:02 +00:00
|
|
|
#include <unistd.h> /* daemon */
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2010-12-24 09:11:23 +00:00
|
|
|
#include <event2/buffer.h>
|
2008-12-30 22:07:39 +00:00
|
|
|
|
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>
|
2008-05-22 19:24:11 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/version.h>
|
|
|
|
|
2009-03-01 15:38:58 +00:00
|
|
|
#include "watch.h"
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#define MY_NAME "transmission-daemon"
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2009-02-27 21:49:31 +00:00
|
|
|
#define PREF_KEY_DIR_WATCH "watch-dir"
|
|
|
|
#define PREF_KEY_DIR_WATCH_ENABLED "watch-dir-enabled"
|
2010-07-07 18:49:06 +00:00
|
|
|
#define PREF_KEY_PIDFILE "pidfile"
|
2009-02-27 21:49:31 +00:00
|
|
|
|
2010-07-05 23:36:05 +00:00
|
|
|
#define MEM_K 1024
|
|
|
|
#define MEM_K_STR "KiB"
|
|
|
|
#define MEM_M_STR "MiB"
|
|
|
|
#define MEM_G_STR "GiB"
|
2010-07-06 20:25:54 +00:00
|
|
|
#define MEM_T_STR "TiB"
|
2010-07-05 23:36:05 +00:00
|
|
|
|
2010-07-28 00:31:11 +00:00
|
|
|
#define DISK_K 1024
|
|
|
|
#define DISK_B_STR "B"
|
|
|
|
#define DISK_K_STR "KiB"
|
|
|
|
#define DISK_M_STR "MiB"
|
|
|
|
#define DISK_G_STR "GiB"
|
|
|
|
#define DISK_T_STR "TiB"
|
2010-07-05 23:36:05 +00:00
|
|
|
|
2010-07-28 00:31:11 +00:00
|
|
|
#define SPEED_K 1024
|
|
|
|
#define SPEED_B_STR "B/s"
|
|
|
|
#define SPEED_K_STR "KiB/s"
|
|
|
|
#define SPEED_M_STR "MiB/s"
|
|
|
|
#define SPEED_G_STR "GiB/s"
|
|
|
|
#define SPEED_T_STR "TiB/s"
|
2010-07-05 23:36:05 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool paused = false;
|
|
|
|
static bool closing = false;
|
2009-03-01 15:38:58 +00:00
|
|
|
static tr_session * mySession = NULL;
|
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-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"
|
2010-02-23 00:27:59 +00:00
|
|
|
"that can be controlled via transmission-remote\n"
|
|
|
|
"or the web interface.\n"
|
2008-12-16 15:27:07 +00:00
|
|
|
"\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[] =
|
|
|
|
{
|
2010-09-22 15:16:42 +00:00
|
|
|
|
2010-12-27 19:18:17 +00:00
|
|
|
{ 'a', "allowed", "Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a", 1, "<list>" },
|
2008-12-13 23:17:36 +00:00
|
|
|
{ 'b', "blocklist", "Enable peer blocklists", "b", 0, NULL },
|
|
|
|
{ 'B', "no-blocklist", "Disable peer blocklists", "B", 0, NULL },
|
2010-06-16 14:05:18 +00:00
|
|
|
{ 'c', "watch-dir", "Where to watch for new .torrent files", "c", 1, "<directory>" },
|
2009-02-27 21:49:31 +00:00
|
|
|
{ 'C', "no-watch-dir", "Disable the watch-dir", "C", 0, NULL },
|
2010-02-07 19:20:00 +00:00
|
|
|
{ 941, "incomplete-dir", "Where to store new torrents until they're complete", NULL, 1, "<directory>" },
|
|
|
|
{ 942, "no-incomplete-dir", "Don't store incomplete torrents in a different location", NULL, 0, NULL },
|
2008-12-14 01:22:06 +00:00
|
|
|
{ 'd', "dump-settings", "Dump the settings and exit", "d", 0, NULL },
|
2010-02-11 01:37:59 +00:00
|
|
|
{ 'e', "logfile", "Dump the log messages to this filename", "e", 1, "<filename>" },
|
2008-12-13 23:17:36 +00:00
|
|
|
{ '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>" },
|
2009-01-23 19:36:34 +00:00
|
|
|
{ 'V', "version", "Show version number and exit", "V", 0, NULL },
|
2010-09-22 22:19:21 +00:00
|
|
|
{ 810, "log-error", "Show error messages", NULL, 0, NULL },
|
2010-09-22 15:16:42 +00:00
|
|
|
{ 811, "log-info", "Show error and info messages", NULL, 0, NULL },
|
|
|
|
{ 812, "log-debug", "Show error, info, and debug messages", NULL, 0, NULL },
|
2008-12-13 23:17:36 +00:00
|
|
|
{ 'w', "download-dir", "Where to save downloaded data", "w", 1, "<path>" },
|
2009-05-13 19:58:04 +00:00
|
|
|
{ 800, "paused", "Pause all torrents on startup", NULL, 0, NULL },
|
2010-02-07 19:20:00 +00:00
|
|
|
{ 'o', "dht", "Enable distributed hash tables (DHT)", "o", 0, NULL },
|
|
|
|
{ 'O', "no-dht", "Disable distributed hash tables (DHT)", "O", 0, NULL },
|
2010-05-15 13:46:46 +00:00
|
|
|
{ 'y', "lpd", "Enable local peer discovery (LPD)", "y", 0, NULL },
|
|
|
|
{ 'Y', "no-lpd", "Disable local peer discovery (LPD)", "Y", 0, NULL },
|
2011-04-20 23:17:10 +00:00
|
|
|
{ 830, "utp", "Enable uTP for peer connections", NULL, 0, NULL },
|
|
|
|
{ 831, "no-utp", "Disable uTP for peer connections", NULL, 0, NULL },
|
2008-12-30 18:18:34 +00:00
|
|
|
{ 'P', "peerport", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "P", 1, "<port>" },
|
|
|
|
{ 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", 0, NULL },
|
|
|
|
{ 'M', "no-portmap", "Disable portmapping", "M", 0, NULL },
|
|
|
|
{ 'L', "peerlimit-global", "Maximum overall number of peers (Default: " TR_DEFAULT_PEER_LIMIT_GLOBAL_STR ")", "L", 1, "<limit>" },
|
|
|
|
{ 'l', "peerlimit-torrent", "Maximum number of peers per torrent (Default: " TR_DEFAULT_PEER_LIMIT_TORRENT_STR ")", "l", 1, "<limit>" },
|
|
|
|
{ 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL },
|
|
|
|
{ 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", 0, NULL },
|
|
|
|
{ 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", 0, NULL },
|
2010-06-16 14:05:18 +00:00
|
|
|
{ 'i', "bind-address-ipv4", "Where to listen for peer connections", "i", 1, "<ipv4 addr>" },
|
|
|
|
{ 'I', "bind-address-ipv6", "Where to listen for peer connections", "I", 1, "<ipv6 addr>" },
|
|
|
|
{ 'r', "rpc-bind-address", "Where to listen for RPC connections", "r", 1, "<ipv4 addr>" },
|
2010-02-07 19:20:00 +00:00
|
|
|
{ 953, "global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed until a specific ratio", "gsr", 1, "ratio" },
|
|
|
|
{ 954, "no-global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed regardless of ratio", "GSR", 0, NULL },
|
2010-05-18 20:37:49 +00:00
|
|
|
{ 'x', "pid-file", "Enable PID file", "x", 1, "<pid-file>" },
|
2008-12-13 23:17:36 +00:00
|
|
|
{ 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
|
2009-10-24 16:18:40 +00:00
|
|
|
gotsig( int sig )
|
2008-02-28 19:06:23 +00:00
|
|
|
{
|
2009-10-24 16:18:40 +00:00
|
|
|
switch( sig )
|
|
|
|
{
|
|
|
|
case SIGHUP:
|
|
|
|
{
|
|
|
|
tr_benc settings;
|
|
|
|
const char * configDir = tr_sessionGetConfigDir( mySession );
|
|
|
|
tr_inf( "Reloading settings from \"%s\"", configDir );
|
|
|
|
tr_bencInitDict( &settings, 0 );
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, true );
|
2009-10-24 16:18:40 +00:00
|
|
|
tr_sessionLoadSettings( &settings, configDir, MY_NAME );
|
|
|
|
tr_sessionSet( mySession, &settings );
|
|
|
|
tr_bencFree( &settings );
|
2009-12-01 00:07:18 +00:00
|
|
|
tr_sessionReloadBlocklists( mySession );
|
2009-10-27 16:46:06 +00:00
|
|
|
break;
|
2009-10-24 16:18:40 +00:00
|
|
|
}
|
2009-10-23 03:41:36 +00:00
|
|
|
|
2009-10-24 16:18:40 +00:00
|
|
|
default:
|
2011-03-22 15:19:54 +00:00
|
|
|
closing = true;
|
2009-10-27 16:46:06 +00:00
|
|
|
break;
|
2009-10-24 16:18:40 +00:00
|
|
|
}
|
2009-10-23 03:41:36 +00:00
|
|
|
}
|
|
|
|
|
2008-12-26 01:36:32 +00:00
|
|
|
#if defined(WIN32)
|
|
|
|
#define USE_NO_DAEMON
|
2009-01-12 20:01:37 +00:00
|
|
|
#elif !defined(HAVE_DAEMON) || defined(__UCLIBC__)
|
2008-12-26 01:36:32 +00:00
|
|
|
#define USE_TR_DAEMON
|
|
|
|
#else
|
|
|
|
#define USE_OS_DAEMON
|
|
|
|
#endif
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
static int
|
2008-12-25 22:06:48 +00:00
|
|
|
tr_daemon( int nochdir, int noclose )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-12-26 01:36:32 +00:00
|
|
|
#if defined(USE_OS_DAEMON)
|
2011-02-18 17:05:17 +00:00
|
|
|
|
2008-12-25 22:06:48 +00:00
|
|
|
return daemon( nochdir, noclose );
|
2011-02-18 17:05:17 +00:00
|
|
|
|
2008-12-26 01:36:32 +00:00
|
|
|
#elif defined(USE_TR_DAEMON)
|
2011-02-18 17:05:17 +00:00
|
|
|
|
|
|
|
/* 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 )
|
2007-04-18 16:39:10 +00:00
|
|
|
return -1;
|
2011-02-18 17:05:17 +00:00
|
|
|
|
|
|
|
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 );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2011-02-18 17:05:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2008-12-26 01:36:32 +00:00
|
|
|
#else /* USE_NO_DAEMON */
|
|
|
|
return 0;
|
2008-05-18 16:44:30 +00:00
|
|
|
#endif
|
2008-12-25 22:06:48 +00:00
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-12-13 23:17:36 +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;
|
|
|
|
|
2009-01-09 19:16:52 +00:00
|
|
|
while(( c = tr_getopt( getUsage( ), argc, argv, options, &optarg ))) {
|
|
|
|
if( c == 'g' ) {
|
2008-12-13 23:17:36 +00:00
|
|
|
configDir = optarg;
|
2009-01-09 19:16:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
tr_optind = ind;
|
|
|
|
|
|
|
|
if( configDir == NULL )
|
|
|
|
configDir = tr_getDefaultConfigDir( MY_NAME );
|
|
|
|
|
|
|
|
return configDir;
|
|
|
|
}
|
|
|
|
|
2009-02-27 21:49:31 +00:00
|
|
|
static void
|
2009-03-01 15:38:58 +00:00
|
|
|
onFileAdded( tr_session * session, const char * dir, const char * file )
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2010-03-17 19:23:03 +00:00
|
|
|
char * filename = tr_buildPath( dir, file, NULL );
|
|
|
|
tr_ctor * ctor = tr_ctorNew( session );
|
|
|
|
int err = tr_ctorSetMetainfoFromFile( ctor, filename );
|
|
|
|
|
|
|
|
if( !err )
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2010-03-17 19:23:03 +00:00
|
|
|
tr_torrentNew( ctor, &err );
|
|
|
|
|
|
|
|
if( err == TR_PARSE_ERR )
|
|
|
|
tr_err( "Error parsing .torrent file \"%s\"", file );
|
|
|
|
else
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool trash = false;
|
2010-03-17 19:23:03 +00:00
|
|
|
int test = tr_ctorGetDeleteSource( ctor, &trash );
|
2009-02-28 15:27:39 +00:00
|
|
|
|
2010-03-17 19:23:03 +00:00
|
|
|
tr_inf( "Parsing .torrent file successful \"%s\"", file );
|
2009-02-28 14:43:37 +00:00
|
|
|
|
2010-03-17 19:23:03 +00:00
|
|
|
if( !test && trash )
|
|
|
|
{
|
|
|
|
tr_inf( "Deleting input .torrent file \"%s\"", file );
|
|
|
|
if( remove( filename ) )
|
|
|
|
tr_err( "Error deleting .torrent file: %s", tr_strerror( errno ) );
|
|
|
|
}
|
2010-07-05 23:36:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char * new_filename = tr_strdup_printf( "%s.added", filename );
|
|
|
|
rename( filename, new_filename );
|
|
|
|
tr_free( new_filename );
|
|
|
|
}
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
2010-03-17 19:23:03 +00:00
|
|
|
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
tr_free( filename );
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2009-10-23 05:00:16 +00:00
|
|
|
static void
|
2010-02-11 01:37:59 +00:00
|
|
|
printMessage( FILE * logfile, int level, const char * name, const char * message, const char * file, int line )
|
2009-10-23 05:00:16 +00:00
|
|
|
{
|
2010-02-11 01:37:59 +00:00
|
|
|
if( logfile != NULL )
|
2010-02-07 22:17:42 +00:00
|
|
|
{
|
|
|
|
char timestr[64];
|
|
|
|
tr_getLogTimeStr( timestr, sizeof( timestr ) );
|
|
|
|
if( name )
|
2010-02-11 01:37:59 +00:00
|
|
|
fprintf( logfile, "[%s] %s %s (%s:%d)\n", timestr, name, message, file, line );
|
2010-02-07 22:17:42 +00:00
|
|
|
else
|
2010-02-11 01:37:59 +00:00
|
|
|
fprintf( logfile, "[%s] %s (%s:%d)\n", timestr, message, file, line );
|
2010-02-07 22:17:42 +00:00
|
|
|
}
|
2010-02-11 01:37:59 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
2010-02-07 22:17:42 +00:00
|
|
|
else /* daemon... write to syslog */
|
|
|
|
{
|
|
|
|
int priority;
|
2009-10-23 05:00:16 +00:00
|
|
|
|
2010-02-07 22:17:42 +00:00
|
|
|
/* figure out the syslog priority */
|
|
|
|
switch( level ) {
|
|
|
|
case TR_MSG_ERR: priority = LOG_ERR; break;
|
|
|
|
case TR_MSG_DBG: priority = LOG_DEBUG; break;
|
|
|
|
default: priority = LOG_INFO; break;
|
2009-10-23 05:00:16 +00:00
|
|
|
}
|
2010-02-07 22:17:42 +00:00
|
|
|
|
|
|
|
if( name )
|
|
|
|
syslog( priority, "%s %s (%s:%d)", name, message, file, line );
|
|
|
|
else
|
|
|
|
syslog( priority, "%s (%s:%d)", message, file, line );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-02-11 01:37:59 +00:00
|
|
|
pumpLogMessages( FILE * logfile )
|
2010-02-07 22:17:42 +00:00
|
|
|
{
|
|
|
|
const tr_msg_list * l;
|
|
|
|
tr_msg_list * list = tr_getQueuedMessages( );
|
|
|
|
|
|
|
|
for( l=list; l!=NULL; l=l->next )
|
2010-02-11 01:37:59 +00:00
|
|
|
printMessage( logfile, l->level, l->name, l->message, l->file, l->line );
|
2009-10-23 05:00:16 +00:00
|
|
|
|
2011-01-15 17:14:15 +00:00
|
|
|
if( logfile != NULL )
|
|
|
|
fflush( logfile );
|
|
|
|
|
2009-10-23 05:00:16 +00:00
|
|
|
tr_freeMessageList( list );
|
|
|
|
}
|
|
|
|
|
2011-01-15 18:12:45 +00:00
|
|
|
static tr_rpc_callback_status
|
|
|
|
on_rpc_callback( tr_session * session UNUSED,
|
|
|
|
tr_rpc_callback_type type,
|
|
|
|
struct tr_torrent * tor UNUSED,
|
|
|
|
void * user_data UNUSED )
|
|
|
|
{
|
|
|
|
if( type == TR_RPC_SESSION_CLOSE )
|
2011-03-22 15:19:54 +00:00
|
|
|
closing = true;
|
2011-01-15 18:12:45 +00:00
|
|
|
return TR_RPC_OK;
|
|
|
|
}
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
int
|
2009-02-27 21:49:31 +00:00
|
|
|
main( int argc, char ** argv )
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
int c;
|
|
|
|
const char * optarg;
|
|
|
|
tr_benc settings;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool boolVal;
|
|
|
|
bool loaded;
|
|
|
|
bool foreground = false;
|
|
|
|
bool dumpSettings = false;
|
2008-07-08 16:50:34 +00:00
|
|
|
const char * configDir = NULL;
|
2010-07-07 18:49:06 +00:00
|
|
|
const char * pid_filename;
|
2009-03-01 15:38:58 +00:00
|
|
|
dtr_watchdir * watchdir = NULL;
|
2010-02-11 01:37:59 +00:00
|
|
|
FILE * logfile = NULL;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool pidfile_created = false;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2009-10-24 16:18:40 +00:00
|
|
|
signal( SIGINT, gotsig );
|
2009-10-29 23:18:53 +00:00
|
|
|
signal( SIGTERM, gotsig );
|
2009-08-12 14:40:32 +00:00
|
|
|
#ifndef WIN32
|
2009-10-24 16:18:40 +00:00
|
|
|
signal( SIGHUP, gotsig );
|
2008-10-13 22:26:02 +00:00
|
|
|
#endif
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* load settings from defaults + config file */
|
|
|
|
tr_bencInitDict( &settings, 0 );
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, true );
|
2008-12-13 23:17:36 +00:00
|
|
|
configDir = getConfigDir( argc, (const char**)argv );
|
2010-01-12 16:43:37 +00:00
|
|
|
loaded = tr_sessionLoadSettings( &settings, configDir, MY_NAME );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
/* 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 );
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, true );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'b': tr_bencDictAddBool( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, true );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'B': tr_bencDictAddBool( &settings, TR_PREFS_KEY_BLOCKLIST_ENABLED, false );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2009-02-27 21:49:31 +00:00
|
|
|
case 'c': tr_bencDictAddStr( &settings, PREF_KEY_DIR_WATCH, optarg );
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_bencDictAddBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, true );
|
2009-02-27 21:49:31 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'C': tr_bencDictAddBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, false );
|
2009-02-27 21:49:31 +00:00
|
|
|
break;
|
2010-05-18 20:45:08 +00:00
|
|
|
case 941: tr_bencDictAddStr( &settings, TR_PREFS_KEY_INCOMPLETE_DIR, optarg );
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_bencDictAddBool( &settings, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, true );
|
2010-05-18 20:45:08 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 942: tr_bencDictAddBool( &settings, TR_PREFS_KEY_INCOMPLETE_DIR_ENABLED, false );
|
2010-05-18 20:45:08 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'd': dumpSettings = true;
|
2008-12-14 01:22:06 +00:00
|
|
|
break;
|
2010-02-11 01:37:59 +00:00
|
|
|
case 'e': logfile = fopen( optarg, "a+" );
|
|
|
|
if( logfile == NULL )
|
|
|
|
fprintf( stderr, "Couldn't open \"%s\": %s\n", optarg, tr_strerror( errno ) );
|
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'f': foreground = true;
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
|
|
|
case 'g': /* handled above */
|
|
|
|
break;
|
2010-05-18 20:45:08 +00:00
|
|
|
case 'V': /* version */
|
2010-08-09 00:18:26 +00:00
|
|
|
fprintf(stderr, "%s %s\n", MY_NAME, LONG_VERSION_STRING);
|
2010-05-18 20:45:08 +00:00
|
|
|
exit( 0 );
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'o': tr_bencDictAddBool( &settings, TR_PREFS_KEY_DHT_ENABLED, true );
|
2010-05-18 20:45:08 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'O': tr_bencDictAddBool( &settings, TR_PREFS_KEY_DHT_ENABLED, false );
|
2010-05-18 20:45:08 +00:00
|
|
|
break;
|
2008-12-13 23:17:36 +00:00
|
|
|
case 'p': tr_bencDictAddInt( &settings, TR_PREFS_KEY_RPC_PORT, atoi( optarg ) );
|
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 't': tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, true );
|
2008-12-13 23:17:36 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'T': tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, false );
|
2008-12-13 23:17:36 +00:00
|
|
|
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;
|
2008-12-30 18:18:34 +00:00
|
|
|
case 'P': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_PORT, atoi( optarg ) );
|
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'm': tr_bencDictAddBool( &settings, TR_PREFS_KEY_PORT_FORWARDING, true );
|
2008-12-30 18:18:34 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'M': tr_bencDictAddBool( &settings, TR_PREFS_KEY_PORT_FORWARDING, false );
|
2008-12-30 18:18:34 +00:00
|
|
|
break;
|
|
|
|
case 'L': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, atoi( optarg ) );
|
|
|
|
break;
|
|
|
|
case 'l': tr_bencDictAddInt( &settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, atoi( optarg ) );
|
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 800: paused = true;
|
2009-05-13 19:58:04 +00:00
|
|
|
break;
|
2008-12-30 18:18:34 +00:00
|
|
|
case 910: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_REQUIRED );
|
|
|
|
break;
|
2008-12-30 22:26:05 +00:00
|
|
|
case 911: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_ENCRYPTION_PREFERRED );
|
2008-12-30 18:18:34 +00:00
|
|
|
break;
|
2008-12-30 22:26:05 +00:00
|
|
|
case 912: tr_bencDictAddInt( &settings, TR_PREFS_KEY_ENCRYPTION, TR_CLEAR_PREFERRED );
|
2008-12-30 18:18:34 +00:00
|
|
|
break;
|
2010-05-18 20:45:08 +00:00
|
|
|
case 'i': tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV4, optarg );
|
2009-04-15 21:05:58 +00:00
|
|
|
break;
|
2010-05-18 20:45:08 +00:00
|
|
|
case 'I': tr_bencDictAddStr( &settings, TR_PREFS_KEY_BIND_ADDRESS_IPV6, optarg );
|
2009-04-15 21:05:58 +00:00
|
|
|
break;
|
2010-05-18 20:45:08 +00:00
|
|
|
case 'r': tr_bencDictAddStr( &settings, TR_PREFS_KEY_RPC_BIND_ADDRESS, optarg );
|
2009-04-15 21:05:58 +00:00
|
|
|
break;
|
2010-05-18 20:45:08 +00:00
|
|
|
case 953: tr_bencDictAddReal( &settings, TR_PREFS_KEY_RATIO, atof(optarg) );
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_bencDictAddBool( &settings, TR_PREFS_KEY_RATIO_ENABLED, true );
|
2010-05-18 20:37:49 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 954: tr_bencDictAddBool( &settings, TR_PREFS_KEY_RATIO_ENABLED, false );
|
2010-05-18 20:37:49 +00:00
|
|
|
break;
|
2010-07-07 18:49:06 +00:00
|
|
|
case 'x': tr_bencDictAddStr( &settings, PREF_KEY_PIDFILE, optarg );
|
2010-05-18 20:37:49 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'y': tr_bencDictAddBool( &settings, TR_PREFS_KEY_LPD_ENABLED, true );
|
2010-05-18 20:37:49 +00:00
|
|
|
break;
|
2011-03-22 15:19:54 +00:00
|
|
|
case 'Y': tr_bencDictAddBool( &settings, TR_PREFS_KEY_LPD_ENABLED, false );
|
2010-05-18 20:37:49 +00:00
|
|
|
break;
|
2010-09-22 15:16:42 +00:00
|
|
|
case 810: tr_bencDictAddInt( &settings, TR_PREFS_KEY_MSGLEVEL, TR_MSG_ERR );
|
|
|
|
break;
|
|
|
|
case 811: tr_bencDictAddInt( &settings, TR_PREFS_KEY_MSGLEVEL, TR_MSG_INF );
|
|
|
|
break;
|
|
|
|
case 812: tr_bencDictAddInt( &settings, TR_PREFS_KEY_MSGLEVEL, TR_MSG_DBG );
|
|
|
|
break;
|
2011-04-20 23:17:10 +00:00
|
|
|
case 830: tr_bencDictAddBool( &settings, TR_PREFS_KEY_UTP_ENABLED, true );
|
|
|
|
break;
|
|
|
|
case 831: tr_bencDictAddBool( &settings, TR_PREFS_KEY_UTP_ENABLED, false );
|
|
|
|
break;
|
2008-12-13 23:17:36 +00:00
|
|
|
default: showUsage( );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2010-02-11 01:37:59 +00:00
|
|
|
if( foreground && !logfile )
|
|
|
|
logfile = stderr;
|
|
|
|
|
2010-02-07 22:17:42 +00:00
|
|
|
if( !loaded )
|
|
|
|
{
|
2010-02-11 01:37:59 +00:00
|
|
|
printMessage( logfile, TR_MSG_ERR, MY_NAME, "Error loading config file -- exiting.", __FILE__, __LINE__ );
|
2010-02-07 22:17:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-14 01:22:06 +00:00
|
|
|
if( dumpSettings )
|
|
|
|
{
|
2009-06-02 01:48:48 +00:00
|
|
|
char * str = tr_bencToStr( &settings, TR_FMT_JSON, NULL );
|
|
|
|
fprintf( stderr, "%s", str );
|
|
|
|
tr_free( str );
|
2008-12-14 01:22:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
if( !foreground && tr_daemon( true, false ) < 0 )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2010-02-07 22:17:42 +00:00
|
|
|
char buf[256];
|
2010-02-19 05:40:19 +00:00
|
|
|
tr_snprintf( buf, sizeof( buf ), "Failed to daemonize: %s", tr_strerror( errno ) );
|
2010-02-11 01:37:59 +00:00
|
|
|
printMessage( logfile, TR_MSG_ERR, MY_NAME, buf, __FILE__, __LINE__ );
|
2008-12-26 01:36:32 +00:00
|
|
|
exit( 1 );
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* start the session */
|
2010-07-06 20:25:54 +00:00
|
|
|
tr_formatter_mem_init( MEM_K, MEM_K_STR, MEM_M_STR, MEM_G_STR, MEM_T_STR );
|
|
|
|
tr_formatter_size_init( DISK_K, DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR );
|
|
|
|
tr_formatter_speed_init( SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR );
|
2011-03-22 15:19:54 +00:00
|
|
|
mySession = tr_sessionInit( "daemon", configDir, true, &settings );
|
2011-01-15 18:12:45 +00:00
|
|
|
tr_sessionSetRPCCallback( mySession, on_rpc_callback, NULL );
|
2010-02-07 22:17:42 +00:00
|
|
|
tr_ninf( NULL, "Using settings from \"%s\"", configDir );
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_sessionSaveSettings( mySession, configDir, &settings );
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2010-07-07 18:49:06 +00:00
|
|
|
pid_filename = NULL;
|
|
|
|
tr_bencDictFindStr( &settings, PREF_KEY_PIDFILE, &pid_filename );
|
2010-07-07 18:51:23 +00:00
|
|
|
if( pid_filename && *pid_filename )
|
2010-05-18 20:58:12 +00:00
|
|
|
{
|
2010-05-18 20:37:49 +00:00
|
|
|
FILE * fp = fopen( pid_filename, "w+" );
|
2010-05-18 20:58:12 +00:00
|
|
|
if( fp != NULL )
|
|
|
|
{
|
2010-05-18 20:37:49 +00:00
|
|
|
fprintf( fp, "%d", (int)getpid() );
|
|
|
|
fclose( fp );
|
2010-05-18 22:25:14 +00:00
|
|
|
tr_inf( "Saved pidfile \"%s\"", pid_filename );
|
2011-03-22 15:19:54 +00:00
|
|
|
pidfile_created = true;
|
2010-05-18 20:37:49 +00:00
|
|
|
}
|
2010-05-18 22:25:14 +00:00
|
|
|
else
|
2011-05-30 15:28:55 +00:00
|
|
|
tr_err( "Unable to save pidfile \"%s\": %s", pid_filename, tr_strerror( errno ) );
|
2010-05-18 20:37:49 +00:00
|
|
|
}
|
|
|
|
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &boolVal ) && boolVal )
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_ninf( MY_NAME, "requiring authentication" );
|
|
|
|
|
2009-02-27 21:49:31 +00:00
|
|
|
/* maybe add a watchdir */
|
|
|
|
{
|
2009-03-01 15:38:58 +00:00
|
|
|
const char * dir;
|
|
|
|
|
2009-03-29 23:05:32 +00:00
|
|
|
if( tr_bencDictFindBool( &settings, PREF_KEY_DIR_WATCH_ENABLED, &boolVal )
|
|
|
|
&& boolVal
|
2009-03-01 15:38:58 +00:00
|
|
|
&& tr_bencDictFindStr( &settings, PREF_KEY_DIR_WATCH, &dir )
|
|
|
|
&& dir
|
|
|
|
&& *dir )
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2009-03-01 15:38:58 +00:00
|
|
|
tr_inf( "Watching \"%s\" for new .torrent files", dir );
|
|
|
|
watchdir = dtr_watchdir_new( mySession, dir, onFileAdded );
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* load the torrents */
|
|
|
|
{
|
2009-05-13 19:58:04 +00:00
|
|
|
tr_torrent ** torrents;
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_ctor * ctor = tr_ctorNew( mySession );
|
2009-05-13 19:58:04 +00:00
|
|
|
if( paused )
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, true );
|
2009-05-13 19:58:04 +00:00
|
|
|
torrents = tr_sessionLoadTorrents( mySession, ctor, NULL );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_free( torrents );
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
}
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2009-10-23 05:00:16 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
if( !foreground )
|
2010-05-18 20:37:49 +00:00
|
|
|
openlog( MY_NAME, LOG_CONS|LOG_PID, LOG_DAEMON );
|
2009-10-23 05:00:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
while( !closing ) {
|
2010-01-01 22:40:54 +00:00
|
|
|
tr_wait_msec( 1000 ); /* sleep one second */
|
2009-03-01 15:38:58 +00:00
|
|
|
dtr_watchdir_update( watchdir );
|
2010-02-11 01:37:59 +00:00
|
|
|
pumpLogMessages( logfile );
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2011-02-20 19:12:45 +00:00
|
|
|
printf( "Closing transmission session..." );
|
|
|
|
tr_sessionSaveSettings( mySession, configDir, &settings );
|
|
|
|
dtr_watchdir_free( watchdir );
|
|
|
|
tr_sessionClose( mySession );
|
|
|
|
pumpLogMessages( logfile );
|
|
|
|
printf( " done.\n" );
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* shutdown */
|
2009-10-23 05:00:16 +00:00
|
|
|
#if HAVE_SYSLOG
|
|
|
|
if( !foreground )
|
2010-02-24 18:14:40 +00:00
|
|
|
{
|
2009-10-23 05:00:16 +00:00
|
|
|
syslog( LOG_INFO, "%s", "Closing session" );
|
2010-02-24 18:14:40 +00:00
|
|
|
closelog( );
|
|
|
|
}
|
2009-10-23 05:00:16 +00:00
|
|
|
#endif
|
2010-02-24 18:14:40 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* cleanup */
|
2010-05-18 20:58:12 +00:00
|
|
|
if( pidfile_created )
|
|
|
|
remove( pid_filename );
|
2008-12-13 23:17:36 +00:00
|
|
|
tr_bencFree( &settings );
|
2007-08-14 04:02:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|