2008-05-18 16:44:30 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2007-04-18 16:39:10 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2007-04-18 16:39:10 +00:00
|
|
|
*
|
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 */
|
2015-04-04 17:43:56 +00:00
|
|
|
#include <stdlib.h> /* atoi */
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2009-10-23 05:00:16 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
#include <syslog.h>
|
|
|
|
#endif
|
2014-12-13 15:22:39 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <process.h> /* getpid */
|
2014-12-13 15:22:39 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <unistd.h> /* getpid */
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
#include <event2/event.h>
|
2008-12-30 22:07:39 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2014-07-08 00:08:43 +00:00
|
|
|
#include <libtransmission/error.h>
|
|
|
|
#include <libtransmission/file.h>
|
2008-07-08 16:50:34 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2013-01-25 23:34:20 +00:00
|
|
|
#include <libtransmission/log.h>
|
2008-05-22 19:24:11 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/version.h>
|
2016-01-02 14:28:59 +00:00
|
|
|
#include <libtransmission/watchdir.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
|
2016-04-23 16:13:25 +00:00
|
|
|
#ifdef USE_SYSTEMD
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
#include <systemd/sd-daemon.h>
|
|
|
|
|
2013-06-07 23:31:26 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void sd_notify(int status UNUSED, char const* str UNUSED)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void sd_notifyf(int status UNUSED, char const* fmt UNUSED, ...)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-07 23:31:26 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-04 17:43:56 +00:00
|
|
|
#include "daemon.h"
|
2009-03-01 15:38:58 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#define MY_NAME "transmission-daemon"
|
2007-04-18 16:39:10 +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
|
|
|
|
2012-02-03 21:21:52 +00:00
|
|
|
#define DISK_K 1000
|
2017-04-21 07:40:57 +00:00
|
|
|
#define DISK_B_STR "B"
|
2012-02-03 21:21:52 +00:00
|
|
|
#define DISK_K_STR "kB"
|
|
|
|
#define DISK_M_STR "MB"
|
|
|
|
#define DISK_G_STR "GB"
|
|
|
|
#define DISK_T_STR "TB"
|
2010-07-05 23:36:05 +00:00
|
|
|
|
2012-02-03 21:21:52 +00:00
|
|
|
#define SPEED_K 1000
|
2017-04-21 07:40:57 +00:00
|
|
|
#define SPEED_B_STR "B/s"
|
2012-02-03 21:21:52 +00:00
|
|
|
#define SPEED_K_STR "kB/s"
|
|
|
|
#define SPEED_M_STR "MB/s"
|
|
|
|
#define SPEED_G_STR "GB/s"
|
|
|
|
#define SPEED_T_STR "TB/s"
|
2010-07-05 23:36:05 +00:00
|
|
|
|
2012-05-21 02:07:46 +00:00
|
|
|
static bool seenHUP = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* logfileName = NULL;
|
2014-09-21 18:03:13 +00:00
|
|
|
static tr_sys_file_t logfile = TR_BAD_SYS_FILE;
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_session* mySession = NULL;
|
2012-12-22 20:35:19 +00:00
|
|
|
static tr_quark key_pidfile = 0;
|
2016-01-25 22:09:49 +00:00
|
|
|
static tr_quark key_watch_dir_force_generic = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
static struct event_base* ev_base = 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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* getUsage(void)
|
2008-07-08 16:50:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return "Transmission " LONG_VERSION_STRING " https://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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static struct tr_option const options[] =
|
2008-09-23 19:11:04 +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>" },
|
2017-04-19 12:04:45 +00:00
|
|
|
{ 910, "encryption-required", "Encrypt all peer connections", "er", 0, NULL },
|
2008-12-30 18:18:34 +00:00
|
|
|
{ 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>" },
|
2017-01-28 20:18:38 +00:00
|
|
|
{ 'r', "rpc-bind-address", "Where to listen for RPC connections", "r", 1, "<ip 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
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool reopen_log_file(char const* filename)
|
2014-09-21 18:03:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error* error = NULL;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_sys_file_t const old_log_file = logfile;
|
|
|
|
tr_sys_file_t const new_log_file = tr_sys_file_open(filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_APPEND, 0666,
|
2017-04-19 12:04:45 +00:00
|
|
|
&error);
|
2014-09-21 18:03:13 +00:00
|
|
|
|
|
|
|
if (new_log_file == TR_BAD_SYS_FILE)
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "Couldn't (re)open log file \"%s\": %s\n", filename, error->message);
|
|
|
|
tr_error_free(error);
|
2014-09-21 18:03:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
logfile = new_log_file;
|
|
|
|
|
|
|
|
if (old_log_file != TR_BAD_SYS_FILE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_sys_file_close(old_log_file, NULL);
|
|
|
|
}
|
2014-09-21 18:03:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* getConfigDir(int argc, char const* const* argv)
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
|
|
|
int c;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* configDir = NULL;
|
|
|
|
char const* optarg;
|
|
|
|
int const ind = tr_optind;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((c = tr_getopt(getUsage(), argc, argv, options, &optarg)) != TR_OPT_DONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (configDir == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
configDir = tr_getDefaultConfigDir(MY_NAME);
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
return configDir;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static tr_watchdir_status onFileAdded(tr_watchdir_t dir, char const* name, void* context)
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = context;
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_str_has_suffix(name, ".torrent"))
|
|
|
|
{
|
2016-01-02 14:28:59 +00:00
|
|
|
return TR_WATCHDIR_IGNORE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* filename = tr_buildPath(tr_watchdir_get_path(dir), name, NULL);
|
|
|
|
tr_ctor* ctor = tr_ctorNew(session);
|
|
|
|
int err = tr_ctorSetMetainfoFromFile(ctor, filename);
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (err == 0)
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrentNew(ctor, &err, NULL);
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (err == TR_PARSE_ERR)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_logAddError("Error parsing .torrent file \"%s\"", name);
|
|
|
|
}
|
2010-03-17 19:23:03 +00:00
|
|
|
else
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool trash = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const test = tr_ctorGetDeleteSource(ctor, &trash);
|
2009-02-28 15:27:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddInfo("Parsing .torrent file successful \"%s\"", name);
|
2009-02-28 14:43:37 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
if (test && trash)
|
2010-03-17 19:23:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error* error = NULL;
|
2014-07-08 00:08:43 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddInfo("Deleting input .torrent file \"%s\"", name);
|
|
|
|
|
|
|
|
if (!tr_sys_path_remove(filename, &error))
|
2014-07-08 00:08:43 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError("Error deleting .torrent file: %s", error->message);
|
|
|
|
tr_error_free(error);
|
2014-07-08 00:08:43 +00:00
|
|
|
}
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
2010-07-05 23:36:05 +00:00
|
|
|
else
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* new_filename = tr_strdup_printf("%s.added", filename);
|
|
|
|
tr_sys_path_rename(filename, new_filename, NULL);
|
|
|
|
tr_free(new_filename);
|
2010-07-05 23:36:05 +00:00
|
|
|
}
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
err = TR_PARSE_ERR;
|
|
|
|
}
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorFree(ctor);
|
|
|
|
tr_free(filename);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
|
|
|
return err == TR_PARSE_ERR ? TR_WATCHDIR_RETRY : TR_WATCHDIR_ACCEPT;
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void printMessage(tr_sys_file_t logfile, int level, char const* name, char const* message, char const* file, int line)
|
2009-10-23 05:00:16 +00:00
|
|
|
{
|
2014-09-21 18:03:13 +00:00
|
|
|
if (logfile != TR_BAD_SYS_FILE)
|
2010-02-07 22:17:42 +00:00
|
|
|
{
|
|
|
|
char timestr[64];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logGetTimeStr(timestr, sizeof(timestr));
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (name != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_sys_file_write_fmt(logfile, "[%s] %s %s (%s:%d)" TR_NATIVE_EOL_STR, NULL, timestr, name, message, file, line);
|
|
|
|
}
|
2010-02-07 22:17:42 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_sys_file_write_fmt(logfile, "[%s] %s (%s:%d)" TR_NATIVE_EOL_STR, NULL, timestr, message, file, line);
|
|
|
|
}
|
2010-02-07 22:17:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-02-11 01:37:59 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
2017-04-30 16:25:26 +00:00
|
|
|
|
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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case TR_LOG_ERROR:
|
|
|
|
priority = LOG_ERR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_LOG_DEBUG:
|
|
|
|
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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (name != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
syslog(priority, "%s %s (%s:%d)", name, message, file, line);
|
|
|
|
}
|
2010-02-07 22:17:42 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
syslog(priority, "%s (%s:%d)", message, file, line);
|
|
|
|
}
|
2010-02-07 22:17:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2015-03-19 06:08:06 +00:00
|
|
|
#else
|
2017-04-30 16:25:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)level;
|
2017-04-30 16:25:26 +00:00
|
|
|
|
2010-02-07 22:17:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void pumpLogMessages(tr_sys_file_t logfile)
|
2010-02-07 22:17:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_log_message* list = tr_logGetQueue();
|
2010-02-07 22:17:42 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_log_message const* l = list; l != NULL; l = l->next)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
printMessage(logfile, l->level, l->name, l->message, l->file, l->line);
|
|
|
|
}
|
2009-10-23 05:00:16 +00:00
|
|
|
|
2014-09-21 18:03:13 +00:00
|
|
|
if (logfile != TR_BAD_SYS_FILE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_sys_file_flush(logfile, NULL);
|
|
|
|
}
|
2011-01-15 17:14:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logFreeQueue(list);
|
2009-10-23 05:00:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void reportStatus(void)
|
2014-04-27 22:01:51 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
double const up = tr_sessionGetRawSpeed_KBps(mySession, TR_UP);
|
|
|
|
double const dn = tr_sessionGetRawSpeed_KBps(mySession, TR_DOWN);
|
2014-04-27 22:01:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (up > 0 || dn > 0)
|
|
|
|
{
|
|
|
|
sd_notifyf(0, "STATUS=Uploading %.2f KBps, Downloading %.2f KBps.\n", up, dn);
|
|
|
|
}
|
2014-04-27 22:01:51 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sd_notify(0, "STATUS=Idle.\n");
|
|
|
|
}
|
2014-04-27 22:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void periodicUpdate(evutil_socket_t fd UNUSED, short what UNUSED, void* context UNUSED)
|
2014-04-27 22:01:51 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
pumpLogMessages(logfile);
|
|
|
|
reportStatus();
|
2014-04-27 22:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04: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)
|
2011-01-15 18:12:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (type == TR_RPC_SESSION_CLOSE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2014-04-27 22:01:51 +00:00
|
|
|
event_base_loopexit(ev_base, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-15 18:12:45 +00:00
|
|
|
return TR_RPC_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool parse_args(int argc, char const** argv, tr_variant* settings, bool* paused, bool* dump_settings, bool* foreground,
|
2017-04-19 12:04:45 +00:00
|
|
|
int* exit_code)
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
int c;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* optarg;
|
2014-09-21 18:06:28 +00:00
|
|
|
|
2015-04-04 17:43:56 +00:00
|
|
|
*paused = false;
|
|
|
|
*dump_settings = false;
|
|
|
|
*foreground = false;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
tr_optind = 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((c = tr_getopt(getUsage(), argc, argv, options, &optarg)) != TR_OPT_DONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
tr_variantDictAddStr(settings, TR_KEY_rpc_whitelist, optarg);
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_rpc_whitelist_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_blocklist_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_blocklist_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
tr_variantDictAddStr(settings, TR_KEY_watch_dir, optarg);
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_watch_dir_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_watch_dir_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 941:
|
|
|
|
tr_variantDictAddStr(settings, TR_KEY_incomplete_dir, optarg);
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_incomplete_dir_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 942:
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_incomplete_dir_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
*dump_settings = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
|
|
|
if (reopen_log_file(optarg))
|
|
|
|
{
|
|
|
|
logfileName = optarg;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
*foreground = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g': /* handled above */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V': /* version */
|
|
|
|
fprintf(stderr, "%s %s\n", MY_NAME, LONG_VERSION_STRING);
|
|
|
|
*exit_code = 0;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_dht_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_dht_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_rpc_port, atoi(optarg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_rpc_authentication_required, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_rpc_authentication_required, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
tr_variantDictAddStr(settings, TR_KEY_rpc_username, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
tr_variantDictAddStr(settings, TR_KEY_rpc_password, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
tr_variantDictAddStr(settings, TR_KEY_download_dir, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_peer_port, atoi(optarg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_port_forwarding_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_port_forwarding_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'L':
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_peer_limit_global, atoi(optarg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_peer_limit_per_torrent, atoi(optarg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 800:
|
|
|
|
*paused = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 910:
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_encryption, TR_ENCRYPTION_REQUIRED);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 911:
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_encryption, TR_ENCRYPTION_PREFERRED);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 912:
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_encryption, TR_CLEAR_PREFERRED);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
tr_variantDictAddStr(settings, TR_KEY_bind_address_ipv4, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I':
|
|
|
|
tr_variantDictAddStr(settings, TR_KEY_bind_address_ipv6, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
tr_variantDictAddStr(settings, TR_KEY_rpc_bind_address, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 953:
|
|
|
|
tr_variantDictAddReal(settings, TR_KEY_ratio_limit, atof(optarg));
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_ratio_limit_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 954:
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_ratio_limit_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
tr_variantDictAddStr(settings, key_pidfile, optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'y':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_lpd_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Y':
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_lpd_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 810:
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_message_level, TR_LOG_ERROR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 811:
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_message_level, TR_LOG_INFO);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 812:
|
|
|
|
tr_variantDictAddInt(settings, TR_KEY_message_level, TR_LOG_DEBUG);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 830:
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_utp_enabled, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 831:
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_utp_enabled, false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
tr_getopt_usage(MY_NAME, getUsage(), options);
|
|
|
|
*exit_code = 0;
|
|
|
|
return false;
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2015-04-04 17:43:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-02-11 01:37:59 +00:00
|
|
|
|
2015-04-04 17:43:56 +00:00
|
|
|
struct daemon_data
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant settings;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* configDir;
|
2017-04-19 12:04:45 +00:00
|
|
|
bool paused;
|
2015-04-04 17:43:56 +00:00
|
|
|
};
|
2010-02-07 22:17:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void daemon_reconfigure(void* arg UNUSED)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (mySession == NULL)
|
2008-12-14 01:22:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddInfo("Deferring reload until session is fully started.");
|
2015-04-04 17:43:56 +00:00
|
|
|
seenHUP = true;
|
2008-12-14 01:22:06 +00:00
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
else
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2015-04-04 17:43:56 +00:00
|
|
|
tr_variant settings;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* configDir;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
/* reopen the logfile to allow for log rotation */
|
|
|
|
if (logfileName != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
reopen_log_file(logfileName);
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
configDir = tr_sessionGetConfigDir(mySession);
|
|
|
|
tr_logAddInfo("Reloading settings from \"%s\"", configDir);
|
|
|
|
tr_variantInitDict(&settings, 0);
|
|
|
|
tr_variantDictAddBool(&settings, TR_KEY_rpc_enabled, true);
|
|
|
|
tr_sessionLoadSettings(&settings, configDir, MY_NAME);
|
|
|
|
tr_sessionSet(mySession, &settings);
|
|
|
|
tr_variantFree(&settings);
|
|
|
|
tr_sessionReloadBlocklists(mySession);
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void daemon_stop(void* arg UNUSED)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
event_base_loopexit(ev_base, NULL);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int daemon_start(void* raw_arg, bool foreground)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
|
|
|
bool boolVal;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* pid_filename;
|
2015-04-04 17:43:56 +00:00
|
|
|
bool pidfile_created = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = NULL;
|
|
|
|
struct event* status_ev = NULL;
|
2016-01-02 14:28:59 +00:00
|
|
|
tr_watchdir_t watchdir = NULL;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct daemon_data* const arg = raw_arg;
|
|
|
|
tr_variant* const settings = &arg->settings;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* const configDir = arg->configDir;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
#ifndef HAVE_SYSLOG
|
2017-04-19 12:04:45 +00:00
|
|
|
(void)foreground;
|
2015-04-04 17:43:56 +00:00
|
|
|
#endif
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
sd_notifyf(0, "MAINPID=%d\n", (int)getpid());
|
2013-06-07 23:31:26 +00:00
|
|
|
|
2015-10-11 16:35:51 +00:00
|
|
|
/* should go before libevent calls */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_net_init();
|
2015-10-11 16:35:51 +00:00
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
/* setup event state */
|
2017-04-19 12:04:45 +00:00
|
|
|
ev_base = event_base_new();
|
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
if (ev_base == NULL)
|
|
|
|
{
|
|
|
|
char buf[256];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, sizeof(buf), "Failed to init daemon event state: %s", tr_strerror(errno));
|
|
|
|
printMessage(logfile, TR_LOG_ERROR, MY_NAME, buf, __FILE__, __LINE__);
|
2015-04-04 17:43:56 +00:00
|
|
|
return 1;
|
2014-04-27 22:01:51 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* start the session */
|
2017-04-19 12:04:45 +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);
|
|
|
|
session = tr_sessionInit(configDir, true, settings);
|
|
|
|
tr_sessionSetRPCCallback(session, on_rpc_callback, NULL);
|
|
|
|
tr_logAddNamedInfo(NULL, "Using settings from \"%s\"", configDir);
|
|
|
|
tr_sessionSaveSettings(session, configDir, settings);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2010-07-07 18:49:06 +00:00
|
|
|
pid_filename = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantDictFindStr(settings, key_pidfile, &pid_filename, NULL);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (pid_filename != NULL && *pid_filename != '\0')
|
2010-05-18 20:58:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error* error = NULL;
|
|
|
|
tr_sys_file_t fp = tr_sys_file_open(pid_filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0666, &error);
|
|
|
|
|
2014-09-21 18:03:13 +00:00
|
|
|
if (fp != TR_BAD_SYS_FILE)
|
2010-05-18 20:58:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sys_file_write_fmt(fp, "%d", NULL, (int)getpid());
|
|
|
|
tr_sys_file_close(fp, NULL);
|
|
|
|
tr_logAddInfo("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
|
2014-09-21 18:03:13 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError("Unable to save pidfile \"%s\": %s", pid_filename, error->message);
|
|
|
|
tr_error_free(error);
|
2014-09-21 18:03:13 +00:00
|
|
|
}
|
2010-05-18 20:37:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_rpc_authentication_required, &boolVal) && boolVal)
|
|
|
|
{
|
|
|
|
tr_logAddNamedInfo(MY_NAME, "requiring authentication");
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2012-05-21 02:07:46 +00:00
|
|
|
mySession = session;
|
|
|
|
|
|
|
|
/* If we got a SIGHUP during startup, process that now. */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (seenHUP)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
daemon_reconfigure(arg);
|
|
|
|
}
|
2012-05-21 02:07:46 +00:00
|
|
|
|
2009-02-27 21:49:31 +00:00
|
|
|
/* maybe add a watchdir */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindBool(settings, TR_KEY_watch_dir_enabled, &boolVal) && boolVal)
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* dir;
|
2016-01-25 22:09:49 +00:00
|
|
|
bool force_generic;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantDictFindBool(settings, key_watch_dir_force_generic, &force_generic))
|
|
|
|
{
|
|
|
|
force_generic = false;
|
|
|
|
}
|
2009-03-01 15:38:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(settings, TR_KEY_watch_dir, &dir, NULL) && dir != NULL && *dir != '\0')
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddInfo("Watching \"%s\" for new .torrent files", dir);
|
|
|
|
|
|
|
|
if ((watchdir = tr_watchdir_new(dir, &onFileAdded, mySession, ev_base, force_generic)) == NULL)
|
|
|
|
{
|
2016-01-02 14:28:59 +00:00
|
|
|
goto cleanup;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* load the torrents */
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent** torrents;
|
|
|
|
tr_ctor* ctor = tr_ctorNew(mySession);
|
|
|
|
|
2015-04-04 17:43:56 +00:00
|
|
|
if (arg->paused)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_ctorSetPaused(ctor, TR_FORCE, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
torrents = tr_sessionLoadTorrents(mySession, ctor, NULL);
|
|
|
|
tr_free(torrents);
|
|
|
|
tr_ctorFree(ctor);
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2009-10-23 05:00:16 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!foreground)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
openlog(MY_NAME, LOG_CONS | LOG_PID, LOG_DAEMON);
|
|
|
|
}
|
|
|
|
|
2009-10-23 05:00:16 +00:00
|
|
|
#endif
|
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
/* Create new timer event to report daemon status */
|
|
|
|
{
|
2017-06-24 10:17:04 +00:00
|
|
|
struct timeval one_sec = { .tv_sec = 1, .tv_usec = 0 };
|
2016-01-02 14:28:59 +00:00
|
|
|
status_ev = event_new(ev_base, -1, EV_PERSIST, &periodicUpdate, NULL);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
if (status_ev == NULL)
|
|
|
|
{
|
|
|
|
tr_logAddError("Failed to create status event %s", tr_strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
if (event_add(status_ev, &one_sec) == -1)
|
|
|
|
{
|
|
|
|
tr_logAddError("Failed to add status event %s", tr_strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2013-06-07 23:31:26 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
sd_notify(0, "READY=1\n");
|
2013-06-07 23:31:26 +00:00
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
/* Run daemon event loop */
|
|
|
|
if (event_base_dispatch(ev_base) == -1)
|
|
|
|
{
|
|
|
|
tr_logAddError("Failed to launch daemon event loop: %s", tr_strerror(errno));
|
2015-01-02 11:15:31 +00:00
|
|
|
goto cleanup;
|
2014-04-27 22:01:51 +00:00
|
|
|
}
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
cleanup:
|
2017-04-19 12:04:45 +00:00
|
|
|
sd_notify(0, "STATUS=Closing transmission session...\n");
|
|
|
|
printf("Closing transmission session...");
|
2014-04-27 22:01:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_watchdir_free(watchdir);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (status_ev != NULL)
|
2014-04-27 22:01:51 +00:00
|
|
|
{
|
|
|
|
event_del(status_ev);
|
|
|
|
event_free(status_ev);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
event_base_free(ev_base);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionSaveSettings(mySession, configDir, settings);
|
|
|
|
tr_sessionClose(mySession);
|
|
|
|
pumpLogMessages(logfile);
|
|
|
|
printf(" done.\n");
|
2011-02-20 19:12:45 +00:00
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* shutdown */
|
2014-12-13 15:22:39 +00:00
|
|
|
#ifdef HAVE_SYSLOG
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!foreground)
|
2010-02-24 18:14:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
syslog(LOG_INFO, "%s", "Closing session");
|
|
|
|
closelog();
|
2010-02-24 18:14:40 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
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 */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (pidfile_created)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_sys_path_remove(pid_filename, NULL);
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
sd_notify(0, "STATUS=\n");
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2007-08-14 04:02:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_main(int argc, char* argv[])
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
dtr_callbacks const cb =
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
.on_start = &daemon_start,
|
|
|
|
.on_stop = &daemon_stop,
|
2015-04-04 17:43:56 +00:00
|
|
|
.on_reconfigure = &daemon_reconfigure,
|
|
|
|
};
|
|
|
|
|
|
|
|
int ret;
|
2017-05-01 15:46:41 +00:00
|
|
|
bool loaded;
|
|
|
|
bool dumpSettings;
|
|
|
|
bool foreground;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error* error = NULL;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
struct daemon_data arg;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* const settings = &arg.settings;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const** const configDir = &arg.configDir;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
key_pidfile = tr_quark_new("pidfile", 7);
|
|
|
|
key_watch_dir_force_generic = tr_quark_new("watch-dir-force-generic", 23);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
/* load settings from defaults + config file */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(settings, 0);
|
|
|
|
tr_variantDictAddBool(settings, TR_KEY_rpc_enabled, true);
|
2017-04-20 16:02:19 +00:00
|
|
|
*configDir = getConfigDir(argc, (char const* const*)argv);
|
2017-04-19 12:04:45 +00:00
|
|
|
loaded = tr_sessionLoadSettings(settings, *configDir, MY_NAME);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
/* overwrite settings from the comamndline */
|
2017-04-20 16:02:19 +00:00
|
|
|
if (!parse_args(argc, (char const**)argv, settings, &arg.paused, &dumpSettings, &foreground, &ret))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2015-04-04 17:43:56 +00:00
|
|
|
goto cleanup;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
if (foreground && logfile == TR_BAD_SYS_FILE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
logfile = tr_sys_file_get_std(TR_STD_SYS_FILE_ERR, NULL);
|
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
if (!loaded)
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printMessage(logfile, TR_LOG_ERROR, MY_NAME, "Error loading config file -- exiting.", __FILE__, __LINE__);
|
2015-04-04 17:43:56 +00:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dumpSettings)
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* str = tr_variantToStr(settings, TR_VARIANT_FMT_JSON, NULL);
|
|
|
|
fprintf(stderr, "%s", str);
|
|
|
|
tr_free(str);
|
2015-04-04 17:43:56 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!dtr_daemon(&cb, &arg, foreground, &ret, &error))
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
|
|
|
char buf[256];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_snprintf(buf, sizeof(buf), "Failed to daemonize: %s", error->message);
|
|
|
|
printMessage(logfile, TR_LOG_ERROR, MY_NAME, buf, __FILE__, __LINE__);
|
|
|
|
tr_error_free(error);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(settings);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|