2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
#include <array>
|
2022-03-18 19:15:43 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio> /* printf */
|
|
|
|
#include <cstdlib> /* atoi */
|
|
|
|
#include <iostream>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <iterator> /* std::back_inserter */
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
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
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <sys/time.h> /* timeval */
|
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
|
|
|
|
2022-03-14 04:43:35 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-03-18 19:15:43 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <libtransmission/error.h>
|
|
|
|
#include <libtransmission/file.h>
|
|
|
|
#include <libtransmission/log.h>
|
2022-08-10 13:34:51 +00:00
|
|
|
#include <libtransmission/timer-ev.h>
|
2008-07-08 16:50:34 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2022-04-04 02:14:44 +00:00
|
|
|
#include <libtransmission/tr-strbuf.h>
|
2023-07-06 03:05:03 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2023-07-08 15:24:03 +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
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "daemon.h"
|
|
|
|
|
|
|
|
struct tr_ctor;
|
|
|
|
struct tr_session;
|
|
|
|
struct tr_torrent;
|
|
|
|
|
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
|
|
|
|
2023-06-22 22:58:16 +00:00
|
|
|
// no-op
|
|
|
|
#define sd_notify(status, str)
|
|
|
|
#define sd_notifyf(status, fmt, ...)
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2013-06-07 23:31:26 +00:00
|
|
|
#endif
|
|
|
|
|
2022-11-15 16:25:12 +00:00
|
|
|
using namespace std::literals;
|
2022-08-10 13:34:51 +00:00
|
|
|
using libtransmission::Watchdir;
|
2009-03-01 15:38:58 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
static char constexpr MyName[] = "transmission-daemon";
|
|
|
|
static char constexpr Usage[] = "Transmission " LONG_VERSION_STRING
|
|
|
|
" https://transmissionbt.com/\n"
|
|
|
|
"A fast and easy BitTorrent client\n"
|
|
|
|
"\n"
|
|
|
|
"transmission-daemon is a headless Transmission session that can be\n"
|
|
|
|
"controlled via transmission-qt, transmission-remote, or its web interface.\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: transmission-daemon [options]";
|
|
|
|
|
2021-12-28 17:23:47 +00:00
|
|
|
static auto constexpr MemK = size_t{ 1024 };
|
|
|
|
static char constexpr MemKStr[] = "KiB";
|
|
|
|
static char constexpr MemMStr[] = "MiB";
|
|
|
|
static char constexpr MemGStr[] = "GiB";
|
|
|
|
static char constexpr MemTStr[] = "TiB";
|
2007-04-18 16:39:10 +00:00
|
|
|
|
2021-12-28 17:23:47 +00:00
|
|
|
static auto constexpr DiskK = size_t{ 1000 };
|
|
|
|
static char constexpr DiskKStr[] = "kB";
|
|
|
|
static char constexpr DiskMStr[] = "MB";
|
|
|
|
static char constexpr DiskGStr[] = "GB";
|
|
|
|
static char constexpr DiskTStr[] = "TB";
|
2010-07-05 23:36:05 +00:00
|
|
|
|
2021-12-28 17:23:47 +00:00
|
|
|
static auto constexpr SpeedK = size_t{ 1000 };
|
|
|
|
static char constexpr SpeedKStr[] = "kB/s";
|
|
|
|
static char constexpr SpeedMStr[] = "MB/s";
|
|
|
|
static char constexpr SpeedGStr[] = "GB/s";
|
|
|
|
static char constexpr SpeedTStr[] = "TB/s";
|
2010-07-05 23:36:05 +00:00
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
/***
|
|
|
|
**** Config File
|
|
|
|
***/
|
2008-06-11 20:09:36 +00:00
|
|
|
|
2022-03-18 19:15:43 +00:00
|
|
|
static auto constexpr Options = std::array<tr_option, 45>{
|
2021-12-17 20:48:02 +00:00
|
|
|
{ { 'a', "allowed", "Allowed IP addresses. (Default: " TR_DEFAULT_RPC_WHITELIST ")", "a", true, "<list>" },
|
|
|
|
{ 'b', "blocklist", "Enable peer blocklists", "b", false, nullptr },
|
|
|
|
{ 'B', "no-blocklist", "Disable peer blocklists", "B", false, nullptr },
|
2022-03-21 14:15:48 +00:00
|
|
|
{ 'c', "watch-dir", "Where to watch for new torrent files", "c", true, "<directory>" },
|
2021-12-17 20:48:02 +00:00
|
|
|
{ 'C', "no-watch-dir", "Disable the watch-dir", "C", false, nullptr },
|
|
|
|
{ 941, "incomplete-dir", "Where to store new torrents until they're complete", nullptr, true, "<directory>" },
|
|
|
|
{ 942, "no-incomplete-dir", "Don't store incomplete torrents in a different location", nullptr, false, nullptr },
|
|
|
|
{ 'd', "dump-settings", "Dump the settings and exit", "d", false, nullptr },
|
2022-02-20 17:54:20 +00:00
|
|
|
{ 943, "default-trackers", "Trackers for public torrents to use automatically", nullptr, true, "<list>" },
|
2021-12-17 20:48:02 +00:00
|
|
|
{ 'e', "logfile", "Dump the log messages to this filename", "e", true, "<filename>" },
|
|
|
|
{ 'f', "foreground", "Run in the foreground instead of daemonizing", "f", false, nullptr },
|
|
|
|
{ 'g', "config-dir", "Where to look for configuration files", "g", true, "<path>" },
|
|
|
|
{ 'p', "port", "RPC port (Default: " TR_DEFAULT_RPC_PORT_STR ")", "p", true, "<port>" },
|
|
|
|
{ 't', "auth", "Require authentication", "t", false, nullptr },
|
|
|
|
{ 'T', "no-auth", "Don't require authentication", "T", false, nullptr },
|
|
|
|
{ 'u', "username", "Set username for authentication", "u", true, "<username>" },
|
|
|
|
{ 'v', "password", "Set password for authentication", "v", true, "<password>" },
|
|
|
|
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
2022-03-18 19:15:43 +00:00
|
|
|
{ 810, "log-level", "Must be 'critical', 'error', 'warn', 'info', 'debug', or 'trace'.", nullptr, true, "<level>" },
|
|
|
|
{ 811, "log-error", "Deprecated. Use --log-level=error", nullptr, false, nullptr },
|
|
|
|
{ 812, "log-info", "Deprecated. Use --log-level=info", nullptr, false, nullptr },
|
|
|
|
{ 813, "log-debug", "Deprecated. Use --log-level=debug", nullptr, false, nullptr },
|
2021-12-17 20:48:02 +00:00
|
|
|
{ 'w', "download-dir", "Where to save downloaded data", "w", true, "<path>" },
|
|
|
|
{ 800, "paused", "Pause all torrents on startup", nullptr, false, nullptr },
|
|
|
|
{ 'o', "dht", "Enable distributed hash tables (DHT)", "o", false, nullptr },
|
|
|
|
{ 'O', "no-dht", "Disable distributed hash tables (DHT)", "O", false, nullptr },
|
|
|
|
{ 'y', "lpd", "Enable local peer discovery (LPD)", "y", false, nullptr },
|
|
|
|
{ 'Y', "no-lpd", "Disable local peer discovery (LPD)", "Y", false, nullptr },
|
2022-10-11 15:39:41 +00:00
|
|
|
{ 830, "utp", "Enable µTP for peer connections", nullptr, false, nullptr },
|
|
|
|
{ 831, "no-utp", "Disable µTP for peer connections", nullptr, false, nullptr },
|
2021-12-17 20:48:02 +00:00
|
|
|
{ 'P', "peerport", "Port for incoming peers (Default: " TR_DEFAULT_PEER_PORT_STR ")", "P", true, "<port>" },
|
|
|
|
{ 'm', "portmap", "Enable portmapping via NAT-PMP or UPnP", "m", false, nullptr },
|
|
|
|
{ 'M', "no-portmap", "Disable portmapping", "M", false, nullptr },
|
|
|
|
{ 'L',
|
|
|
|
"peerlimit-global",
|
|
|
|
"Maximum overall number of peers (Default: " TR_DEFAULT_PEER_LIMIT_GLOBAL_STR ")",
|
|
|
|
"L",
|
|
|
|
true,
|
|
|
|
"<limit>" },
|
|
|
|
{ 'l',
|
|
|
|
"peerlimit-torrent",
|
|
|
|
"Maximum number of peers per torrent (Default: " TR_DEFAULT_PEER_LIMIT_TORRENT_STR ")",
|
|
|
|
"l",
|
|
|
|
true,
|
|
|
|
"<limit>" },
|
|
|
|
{ 910, "encryption-required", "Encrypt all peer connections", "er", false, nullptr },
|
|
|
|
{ 911, "encryption-preferred", "Prefer encrypted peer connections", "ep", false, nullptr },
|
|
|
|
{ 912, "encryption-tolerated", "Prefer unencrypted peer connections", "et", false, nullptr },
|
|
|
|
{ 'i', "bind-address-ipv4", "Where to listen for peer connections", "i", true, "<ipv4 addr>" },
|
|
|
|
{ 'I', "bind-address-ipv6", "Where to listen for peer connections", "I", true, "<ipv6 addr>" },
|
|
|
|
{ 'r', "rpc-bind-address", "Where to listen for RPC connections", "r", true, "<ip addr>" },
|
|
|
|
{ 953,
|
|
|
|
"global-seedratio",
|
|
|
|
"All torrents, unless overridden by a per-torrent setting, should seed until a specific ratio",
|
|
|
|
"gsr",
|
|
|
|
true,
|
|
|
|
"ratio" },
|
|
|
|
{ 954,
|
|
|
|
"no-global-seedratio",
|
|
|
|
"All torrents, unless overridden by a per-torrent setting, should seed regardless of ratio",
|
|
|
|
"GSR",
|
|
|
|
false,
|
|
|
|
nullptr },
|
|
|
|
{ 'x', "pid-file", "Enable PID file", "x", true, "<pid-file>" },
|
|
|
|
{ 0, nullptr, nullptr, nullptr, false, nullptr } }
|
2008-07-08 16:50:34 +00:00
|
|
|
};
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
bool tr_daemon::reopen_log_file(char const* filename)
|
2014-09-21 18:03:13 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_error* error = nullptr;
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_sys_file_t const old_log_file = logfile_;
|
2021-08-15 09:41:48 +00:00
|
|
|
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,
|
|
|
|
&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;
|
|
|
|
}
|
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
logfile_ = new_log_file;
|
2023-01-18 18:46:57 +00:00
|
|
|
logfile_flush_ = tr_sys_file_flush_possible(logfile_);
|
2014-09-21 18:03:13 +00:00
|
|
|
|
|
|
|
if (old_log_file != TR_BAD_SYS_FILE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(old_log_file);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-09-21 18:03:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-17 00:28:57 +00:00
|
|
|
static std::string getConfigDir(int argc, char const* const* argv)
|
2008-12-13 23:17:36 +00:00
|
|
|
{
|
|
|
|
int c;
|
2021-09-21 23:03:39 +00:00
|
|
|
char const* optstr;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const ind = tr_optind;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
while ((c = tr_getopt(Usage, argc, argv, std::data(Options), &optstr)) != TR_OPT_DONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (c == 'g')
|
|
|
|
{
|
2022-08-17 00:28:57 +00:00
|
|
|
return optstr;
|
2009-01-09 19:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
tr_optind = ind;
|
|
|
|
|
2022-08-17 00:28:57 +00:00
|
|
|
return tr_getDefaultConfigDir(MyName);
|
2008-12-13 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 16:25:12 +00:00
|
|
|
static auto onFileAdded(tr_session const* session, std::string_view dirname, std::string_view basename)
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
auto const lowercase = tr_strlower(basename);
|
2023-06-30 14:49:58 +00:00
|
|
|
auto const is_torrent = tr_strv_ends_with(lowercase, ".torrent"sv);
|
|
|
|
auto const is_magnet = tr_strv_ends_with(lowercase, ".magnet"sv);
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
if (!is_torrent && !is_magnet)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
return Watchdir::Action::Done;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
auto const filename = tr_pathbuf{ dirname, '/', basename };
|
2022-04-04 02:14:44 +00:00
|
|
|
tr_ctor* const ctor = tr_ctorNew(session);
|
|
|
|
|
|
|
|
bool retry = false;
|
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
if (is_torrent)
|
2022-04-04 02:14:44 +00:00
|
|
|
{
|
|
|
|
if (!tr_ctorSetMetainfoFromFile(ctor, filename, nullptr))
|
|
|
|
{
|
|
|
|
retry = true;
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 13:34:51 +00:00
|
|
|
else // is_magnet
|
2022-04-04 02:14:44 +00:00
|
|
|
{
|
|
|
|
auto content = std::vector<char>{};
|
|
|
|
tr_error* error = nullptr;
|
2023-06-30 14:49:58 +00:00
|
|
|
if (!tr_file_read(filename, content, &error))
|
2022-04-04 02:14:44 +00:00
|
|
|
{
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-08-10 13:34:51 +00:00
|
|
|
fmt::arg("path", basename),
|
2022-04-04 02:14:44 +00:00
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
|
|
|
tr_error_free(error);
|
|
|
|
retry = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
content.push_back('\0'); // zero-terminated string
|
|
|
|
auto const* data = std::data(content);
|
|
|
|
if (!tr_ctorSetMetainfoFromMagnetLink(ctor, data, nullptr))
|
|
|
|
{
|
|
|
|
retry = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retry)
|
2021-12-26 16:25:07 +00:00
|
|
|
{
|
|
|
|
tr_ctorFree(ctor);
|
2022-08-10 13:34:51 +00:00
|
|
|
return Watchdir::Action::Retry;
|
2021-12-26 16:25:07 +00:00
|
|
|
}
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2021-12-26 16:25:07 +00:00
|
|
|
if (tr_torrentNew(ctor, nullptr) == nullptr)
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
tr_logAddError(fmt::format(_("Couldn't add torrent file '{path}'"), fmt::arg("path", basename)));
|
2021-12-26 16:25:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool trash = false;
|
|
|
|
bool const test = tr_ctorGetDeleteSource(ctor, &trash);
|
2009-02-28 15:27:39 +00:00
|
|
|
|
2021-12-26 16:25:07 +00:00
|
|
|
if (test && trash)
|
|
|
|
{
|
|
|
|
tr_error* error = nullptr;
|
2014-07-08 00:08:43 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Removing torrent file '{path}'"), fmt::arg("path", basename)));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-05-22 01:17:00 +00:00
|
|
|
if (!tr_sys_path_remove(filename, &error))
|
2010-07-05 23:36:05 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't remove '{path}': {error} ({error_code})"),
|
2022-08-10 13:34:51 +00:00
|
|
|
fmt::arg("path", basename),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
2021-12-26 16:25:07 +00:00
|
|
|
tr_error_free(error);
|
2010-07-05 23:36:05 +00:00
|
|
|
}
|
2010-03-17 19:23:03 +00:00
|
|
|
}
|
2021-12-26 16:25:07 +00:00
|
|
|
else
|
|
|
|
{
|
2022-04-04 02:14:44 +00:00
|
|
|
tr_sys_path_rename(filename, tr_pathbuf{ filename, ".added"sv });
|
2021-12-26 16:25:07 +00:00
|
|
|
}
|
2016-01-02 14:28:59 +00:00
|
|
|
}
|
2010-03-17 19:23:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorFree(ctor);
|
2022-08-10 13:34:51 +00:00
|
|
|
return Watchdir::Action::Done;
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-03-10 05:51:14 +00:00
|
|
|
static char const* levelName(tr_log_level level)
|
|
|
|
{
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case TR_LOG_CRITICAL:
|
2022-03-11 21:09:22 +00:00
|
|
|
return "CRT";
|
2022-03-10 05:51:14 +00:00
|
|
|
case TR_LOG_ERROR:
|
2022-03-11 21:09:22 +00:00
|
|
|
return "ERR";
|
2022-03-10 05:51:14 +00:00
|
|
|
case TR_LOG_WARN:
|
2022-03-11 21:09:22 +00:00
|
|
|
return "WRN";
|
2022-03-10 05:51:14 +00:00
|
|
|
case TR_LOG_DEBUG:
|
2022-03-11 21:09:22 +00:00
|
|
|
return "dbg";
|
2022-03-10 05:51:14 +00:00
|
|
|
case TR_LOG_TRACE:
|
2022-03-11 21:09:22 +00:00
|
|
|
return "trc";
|
2022-03-10 05:51:14 +00:00
|
|
|
default:
|
2022-03-11 21:09:22 +00:00
|
|
|
return "inf";
|
2022-03-10 05:51:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static void printMessage(
|
|
|
|
tr_sys_file_t file,
|
2022-03-10 05:51:14 +00:00
|
|
|
tr_log_level level,
|
2021-12-28 17:23:47 +00:00
|
|
|
std::string_view name,
|
|
|
|
std::string_view message,
|
|
|
|
std::string_view filename,
|
2022-12-18 16:23:44 +00:00
|
|
|
long line)
|
2009-10-23 05:00:16 +00:00
|
|
|
{
|
2023-07-04 17:00:46 +00:00
|
|
|
auto out = tr_strbuf<char, 2048U>{};
|
|
|
|
|
|
|
|
if (std::empty(name))
|
|
|
|
{
|
|
|
|
fmt::format_to(std::back_inserter(out), "{:s} ({:s}:{:d})", message, filename, line);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt::format_to(std::back_inserter(out), "{:s} {:s} ({:s}:{:d})", name, message, filename, line);
|
|
|
|
}
|
2021-12-28 17:23:47 +00:00
|
|
|
|
2020-09-11 21:07:45 +00:00
|
|
|
if (file != TR_BAD_SYS_FILE)
|
2010-02-07 22:17:42 +00:00
|
|
|
{
|
2021-12-28 17:23:47 +00:00
|
|
|
auto timestr = std::array<char, 64>{};
|
|
|
|
tr_logGetTimeStr(std::data(timestr), std::size(timestr));
|
2023-07-04 17:00:46 +00:00
|
|
|
tr_sys_file_write_line(file, fmt::format("[{:s}] {:s} {:s}", std::data(timestr), levelName(level), std::data(out)));
|
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)
|
|
|
|
{
|
2022-03-10 05:51:14 +00:00
|
|
|
case TR_LOG_CRITICAL:
|
|
|
|
priority = LOG_CRIT;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_LOG_ERROR:
|
|
|
|
priority = LOG_ERR;
|
|
|
|
break;
|
|
|
|
|
2022-03-10 05:51:14 +00:00
|
|
|
case TR_LOG_WARN:
|
|
|
|
priority = LOG_WARNING;
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
2022-03-10 05:51:14 +00:00
|
|
|
case TR_LOG_INFO:
|
2017-04-19 12:04:45 +00:00
|
|
|
priority = LOG_INFO;
|
|
|
|
break;
|
2022-03-10 05:51:14 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
priority = LOG_DEBUG;
|
|
|
|
break;
|
2009-10-23 05:00:16 +00:00
|
|
|
}
|
2010-02-07 22:17:42 +00:00
|
|
|
|
2021-12-28 17:23:47 +00:00
|
|
|
syslog(priority, "%s", out.c_str());
|
2010-02-07 22:17:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-02-07 22:17:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-18 18:46:57 +00:00
|
|
|
static void pumpLogMessages(tr_sys_file_t file, bool flush)
|
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
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
for (tr_log_message const* l = list; l != nullptr; l = l->next)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-12 00:59:58 +00:00
|
|
|
printMessage(file, l->level, l->name, l->message, l->file, l->line);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-10-23 05:00:16 +00:00
|
|
|
|
2023-01-18 18:46:57 +00:00
|
|
|
if (flush && file != TR_BAD_SYS_FILE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_flush(file);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
void tr_daemon::report_status(void)
|
2014-04-27 22:01:51 +00:00
|
|
|
{
|
2022-09-05 21:28:15 +00:00
|
|
|
double const up = tr_sessionGetRawSpeed_KBps(my_session_, TR_UP);
|
|
|
|
double const dn = tr_sessionGetRawSpeed_KBps(my_session_, 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
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
void tr_daemon::periodic_update(void)
|
2014-04-27 22:01:51 +00:00
|
|
|
{
|
2023-01-18 18:46:57 +00:00
|
|
|
pumpLogMessages(logfile_, logfile_flush_);
|
2022-09-13 06:37:12 +00:00
|
|
|
report_status();
|
2014-04-27 22:01:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
static void periodic_update(evutil_socket_t /*fd*/, short /*what*/, void* arg)
|
2022-09-05 21:28:15 +00:00
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
static_cast<tr_daemon*>(arg)->periodic_update();
|
2022-09-05 21:28:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static tr_rpc_callback_status on_rpc_callback(
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_session* /*session*/,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_rpc_callback_type type,
|
2021-10-24 16:41:54 +00:00
|
|
|
tr_torrent* /*tor*/,
|
2022-09-05 21:28:15 +00:00
|
|
|
void* arg)
|
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
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
static_cast<tr_daemon*>(arg)->stop();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-15 18:12:45 +00:00
|
|
|
return TR_RPC_OK;
|
|
|
|
}
|
|
|
|
|
2022-12-21 20:21:16 +00:00
|
|
|
bool tr_daemon::parse_args(int argc, char const* const* argv, bool* dump_settings, bool* foreground, int* exit_code)
|
2007-04-18 16:39:10 +00:00
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
int c;
|
2021-09-21 23:03:39 +00:00
|
|
|
char const* optstr;
|
2014-09-21 18:06:28 +00:00
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
paused_ = false;
|
2015-04-04 17:43:56 +00:00
|
|
|
*dump_settings = false;
|
|
|
|
*foreground = false;
|
2008-12-13 23:17:36 +00:00
|
|
|
|
|
|
|
tr_optind = 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-17 20:48:02 +00:00
|
|
|
while ((c = tr_getopt(Usage, argc, argv, std::data(Options), &optstr)) != TR_OPT_DONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'a':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_rpc_whitelist, optstr);
|
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_rpc_whitelist_enabled, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_blocklist_enabled, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_blocklist_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_watch_dir, optstr);
|
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_watch_dir_enabled, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_watch_dir_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 941:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_incomplete_dir, optstr);
|
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_incomplete_dir_enabled, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 942:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_incomplete_dir_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
2022-02-20 17:54:20 +00:00
|
|
|
case 943:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_default_trackers, optstr);
|
2022-02-20 17:54:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'd':
|
|
|
|
*dump_settings = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e':
|
2022-09-13 06:37:12 +00:00
|
|
|
if (reopen_log_file(optstr))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-05 21:28:15 +00:00
|
|
|
log_file_name_ = optstr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
*foreground = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g': /* handled above */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V': /* version */
|
2021-12-17 20:48:02 +00:00
|
|
|
fprintf(stderr, "%s %s\n", MyName, LONG_VERSION_STRING);
|
2017-04-19 12:04:45 +00:00
|
|
|
*exit_code = 0;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case 'o':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_dht_enabled, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_dht_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_rpc_port, atoi(optstr));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_rpc_authentication_required, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_rpc_authentication_required, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_rpc_username, optstr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_rpc_password, optstr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_download_dir, optstr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_peer_port, atoi(optstr));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_port_forwarding_enabled, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_port_forwarding_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'L':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_peer_limit_global, atoi(optstr));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_peer_limit_per_torrent, atoi(optstr));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 800:
|
2022-09-05 21:28:15 +00:00
|
|
|
paused_ = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 910:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_encryption, TR_ENCRYPTION_REQUIRED);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 911:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_encryption, TR_ENCRYPTION_PREFERRED);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 912:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_encryption, TR_CLEAR_PREFERRED);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_bind_address_ipv4, optstr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_bind_address_ipv6, optstr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, TR_KEY_rpc_bind_address, optstr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 953:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddReal(&settings_, TR_KEY_ratio_limit, atof(optstr));
|
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_ratio_limit_enabled, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 954:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_ratio_limit_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddStr(&settings_, key_pidfile_, optstr);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'y':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_lpd_enabled, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Y':
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_lpd_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 810:
|
2022-03-18 19:15:43 +00:00
|
|
|
if (auto const level = tr_logGetLevelFromKey(optstr); level)
|
|
|
|
{
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_message_level, *level);
|
2022-03-18 19:15:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << fmt::format(_("Couldn't parse log level '{level}'"), fmt::arg("level", optstr)) << std::endl;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 811:
|
2022-03-18 19:15:43 +00:00
|
|
|
std::cerr << "WARN: --log-error is deprecated. Use --log-level=error" << std::endl;
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_message_level, TR_LOG_ERROR);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 812:
|
2022-03-18 19:15:43 +00:00
|
|
|
std::cerr << "WARN: --log-info is deprecated. Use --log-level=info" << std::endl;
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_message_level, TR_LOG_INFO);
|
2022-03-18 19:15:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 813:
|
|
|
|
std::cerr << "WARN: --log-debug is deprecated. Use --log-level=debug" << std::endl;
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddInt(&settings_, TR_KEY_message_level, TR_LOG_DEBUG);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 830:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_utp_enabled, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 831:
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_utp_enabled, false);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
2023-02-24 21:47:51 +00:00
|
|
|
case TR_OPT_UNK:
|
|
|
|
fprintf(stderr, "Unexpected argument: %s \n", optstr);
|
|
|
|
tr_getopt_usage(MyName, Usage, std::data(Options));
|
|
|
|
*exit_code = 1;
|
|
|
|
return false;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2021-12-17 20:48:02 +00:00
|
|
|
tr_getopt_usage(MyName, Usage, std::data(Options));
|
2017-04-19 12:04:45 +00:00
|
|
|
*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
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
void tr_daemon::reconfigure(void)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-05 21:28:15 +00:00
|
|
|
if (my_session_ == nullptr)
|
2008-12-14 01:22:06 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddInfo(_("Deferring reload until session is fully started."));
|
2022-09-05 21:28:15 +00:00
|
|
|
seen_hup_ = 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
|
|
|
{
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variant newsettings;
|
2022-07-22 05:52:48 +00:00
|
|
|
char const* configDir;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
/* reopen the logfile to allow for log rotation */
|
2022-09-05 21:28:15 +00:00
|
|
|
if (log_file_name_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
reopen_log_file(log_file_name_);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
configDir = tr_sessionGetConfigDir(my_session_);
|
2022-07-22 05:52:48 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Reloading settings from '{path}'"), fmt::arg("path", configDir)));
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantInitDict(&newsettings, 0);
|
|
|
|
tr_variantDictAddBool(&newsettings, TR_KEY_rpc_enabled, true);
|
|
|
|
tr_sessionLoadSettings(&newsettings, configDir, MyName);
|
|
|
|
tr_sessionSet(my_session_, &newsettings);
|
|
|
|
tr_sessionReloadBlocklists(my_session_);
|
2007-04-18 16:39:10 +00:00
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
void tr_daemon::stop(void)
|
2022-09-05 21:28:15 +00:00
|
|
|
{
|
|
|
|
event_base_loopexit(ev_base_, nullptr);
|
|
|
|
}
|
|
|
|
|
2022-09-13 06:37:12 +00:00
|
|
|
int tr_daemon::start([[maybe_unused]] bool foreground)
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
|
|
|
bool boolVal;
|
|
|
|
bool pidfile_created = false;
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_session* session = nullptr;
|
|
|
|
struct event* status_ev = nullptr;
|
2023-07-07 22:18:38 +00:00
|
|
|
struct event* sig_ev = nullptr;
|
2022-08-10 13:34:51 +00:00
|
|
|
auto watchdir = std::unique_ptr<Watchdir>{};
|
2022-09-05 21:28:15 +00:00
|
|
|
char const* const cdir = this->config_dir_.c_str();
|
2015-04-04 17:43:56 +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
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
/* setup event state */
|
2022-09-05 21:28:15 +00:00
|
|
|
ev_base_ = event_base_new();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-07-07 22:18:38 +00:00
|
|
|
if (ev_base_ == nullptr || !setup_signals(sig_ev))
|
2014-04-27 22:01:51 +00:00
|
|
|
{
|
2022-04-02 19:34:29 +00:00
|
|
|
auto const error_code = errno;
|
|
|
|
auto const errmsg = fmt::format(
|
|
|
|
_("Couldn't initialize daemon: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code));
|
2022-09-05 21:28:15 +00:00
|
|
|
printMessage(logfile_, TR_LOG_ERROR, MyName, errmsg, __FILE__, __LINE__);
|
2023-07-07 22:18:38 +00:00
|
|
|
cleanup_signals(sig_ev);
|
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 */
|
2021-12-28 17:23:47 +00:00
|
|
|
tr_formatter_mem_init(MemK, MemKStr, MemMStr, MemGStr, MemTStr);
|
|
|
|
tr_formatter_size_init(DiskK, DiskKStr, DiskMStr, DiskGStr, DiskTStr);
|
|
|
|
tr_formatter_speed_init(SpeedK, SpeedKStr, SpeedMStr, SpeedGStr, SpeedTStr);
|
2022-09-05 21:28:15 +00:00
|
|
|
session = tr_sessionInit(cdir, true, &settings_);
|
|
|
|
tr_sessionSetRPCCallback(session, on_rpc_callback, this);
|
|
|
|
tr_logAddInfo(fmt::format(_("Loading settings from '{path}'"), fmt::arg("path", cdir)));
|
|
|
|
tr_sessionSaveSettings(session, cdir, &settings_);
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2021-11-16 05:20:06 +00:00
|
|
|
auto sv = std::string_view{};
|
2022-09-05 21:28:15 +00:00
|
|
|
(void)tr_variantDictFindStrView(&settings_, key_pidfile_, &sv);
|
2021-11-16 05:20:06 +00:00
|
|
|
auto const sz_pid_filename = std::string{ sv };
|
|
|
|
if (!std::empty(sz_pid_filename))
|
2010-05-18 20:58:12 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_error* error = nullptr;
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_sys_file_t fp = tr_sys_file_open(
|
2021-11-16 05:20:06 +00:00
|
|
|
sz_pid_filename.c_str(),
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE,
|
|
|
|
0666,
|
2019-02-10 11:05:16 +00:00
|
|
|
&error);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-09-21 18:03:13 +00:00
|
|
|
if (fp != TR_BAD_SYS_FILE)
|
2010-05-18 20:58:12 +00:00
|
|
|
{
|
2021-12-28 15:08:04 +00:00
|
|
|
auto const out = std::to_string(getpid());
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_write(fp, std::data(out), std::size(out), nullptr);
|
|
|
|
tr_sys_file_close(fp);
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Saved pidfile '{path}'"), fmt::arg("path", sz_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
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't save '{path}': {error} ({error_code})"),
|
2022-03-14 04:43:35 +00:00
|
|
|
fmt::arg("path", sz_pid_filename),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_free(error);
|
2014-09-21 18:03:13 +00:00
|
|
|
}
|
2010-05-18 20:37:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
if (tr_variantDictFindBool(&settings_, TR_KEY_rpc_authentication_required, &boolVal) && boolVal)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddInfo(_("Requiring authentication"));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
my_session_ = session;
|
2012-05-21 02:07:46 +00:00
|
|
|
|
|
|
|
/* If we got a SIGHUP during startup, process that now. */
|
2022-09-05 21:28:15 +00:00
|
|
|
if (seen_hup_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
reconfigure();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-05-21 02:07:46 +00:00
|
|
|
|
2009-02-27 21:49:31 +00:00
|
|
|
/* maybe add a watchdir */
|
2022-09-05 21:28:15 +00:00
|
|
|
if (tr_variantDictFindBool(&settings_, TR_KEY_watch_dir_enabled, &boolVal) && boolVal)
|
2009-02-27 21:49:31 +00:00
|
|
|
{
|
2021-11-15 04:49:56 +00:00
|
|
|
auto force_generic = bool{ false };
|
2022-09-05 21:28:15 +00:00
|
|
|
(void)tr_variantDictFindBool(&settings_, key_watch_dir_force_generic_, &force_generic);
|
2016-01-25 22:09:49 +00:00
|
|
|
|
2021-11-15 04:49:56 +00:00
|
|
|
auto dir = std::string_view{};
|
2022-09-05 21:28:15 +00:00
|
|
|
(void)tr_variantDictFindStrView(&settings_, TR_KEY_watch_dir, &dir);
|
2021-11-15 04:49:56 +00:00
|
|
|
if (!std::empty(dir))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-21 14:15:48 +00:00
|
|
|
tr_logAddInfo(fmt::format(_("Watching '{path}' for new torrent files"), fmt::arg("path", dir)));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
auto handler = [session](std::string_view dirname, std::string_view basename)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-10 13:34:51 +00:00
|
|
|
return onFileAdded(session, dirname, basename);
|
|
|
|
};
|
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
auto timer_maker = libtransmission::EvTimerMaker{ ev_base_ };
|
2023-05-06 04:11:05 +00:00
|
|
|
watchdir = force_generic ? Watchdir::create_generic(dir, handler, timer_maker) :
|
2022-09-05 21:28:15 +00:00
|
|
|
Watchdir::create(dir, handler, timer_maker, ev_base_);
|
2009-02-27 21:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-13 23:17:36 +00:00
|
|
|
/* load the torrents */
|
|
|
|
{
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_ctor* ctor = tr_ctorNew(my_session_);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
if (paused_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_ctorSetPaused(ctor, TR_FORCE, true);
|
|
|
|
}
|
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_sessionLoadTorrents(my_session_, ctor);
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
openlog(MyName, LOG_CONS | LOG_PID, LOG_DAEMON);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
{
|
2021-09-21 23:03:39 +00:00
|
|
|
constexpr auto one_sec = timeval{ 1, 0 }; // 1 second
|
2022-09-13 06:37:12 +00:00
|
|
|
status_ev = event_new(ev_base_, -1, EV_PERSIST, &::periodic_update, this);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (status_ev == nullptr)
|
2014-04-27 22:01:51 +00:00
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
auto const error_code = errno;
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("Couldn't create event: {error} ({error_code})"),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2020-11-05 22:46:21 +00:00
|
|
|
goto CLEANUP;
|
2014-04-27 22:01:51 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2014-04-27 22:01:51 +00:00
|
|
|
if (event_add(status_ev, &one_sec) == -1)
|
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
auto const error_code = errno;
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-17 00:23:44 +00:00
|
|
|
_("Couldn't add event: {error} ({error_code})"),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2020-11-05 22:46:21 +00:00
|
|
|
goto CLEANUP;
|
2014-04-27 22:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
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 */
|
2022-09-05 21:28:15 +00:00
|
|
|
if (event_base_dispatch(ev_base_) == -1)
|
2014-04-27 22:01:51 +00:00
|
|
|
{
|
2022-03-16 00:51:36 +00:00
|
|
|
auto const error_code = errno;
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't launch daemon event loop: {error} ({error_code})"),
|
|
|
|
fmt::arg("error", tr_strerror(error_code)),
|
|
|
|
fmt::arg("error_code", error_code)));
|
2020-11-05 22:46:21 +00:00
|
|
|
goto CLEANUP;
|
2014-04-27 22:01:51 +00:00
|
|
|
}
|
2007-08-14 04:02:50 +00:00
|
|
|
|
2020-11-05 22:46:21 +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
|
|
|
|
2022-08-10 13:34:51 +00:00
|
|
|
watchdir.reset();
|
2016-01-02 14:28:59 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (status_ev != nullptr)
|
2014-04-27 22:01:51 +00:00
|
|
|
{
|
|
|
|
event_del(status_ev);
|
|
|
|
event_free(status_ev);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-07-07 22:18:38 +00:00
|
|
|
cleanup_signals(sig_ev);
|
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
event_base_free(ev_base_);
|
2014-04-27 22:01:51 +00:00
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_sessionSaveSettings(my_session_, cdir, &settings_);
|
|
|
|
tr_sessionClose(my_session_);
|
2023-01-18 18:46:57 +00:00
|
|
|
pumpLogMessages(logfile_, logfile_flush_);
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
|
|
{
|
2022-05-22 01:17:00 +00:00
|
|
|
tr_sys_path_remove(sz_pid_filename);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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
|
|
|
|
2022-12-21 20:21:16 +00:00
|
|
|
bool tr_daemon::init(int argc, char const* const argv[], bool* foreground, int* ret)
|
2022-09-05 21:28:15 +00:00
|
|
|
{
|
2022-12-21 20:21:16 +00:00
|
|
|
config_dir_ = getConfigDir(argc, argv);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2019-03-15 18:53:53 +00:00
|
|
|
/* load settings from defaults + config file */
|
2022-09-05 21:28:15 +00:00
|
|
|
tr_variantInitDict(&settings_, 0);
|
|
|
|
tr_variantDictAddBool(&settings_, TR_KEY_rpc_enabled, true);
|
|
|
|
bool const loaded = tr_sessionLoadSettings(&settings_, config_dir_.c_str(), MyName);
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2019-03-15 18:53:53 +00:00
|
|
|
bool dumpSettings;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2019-03-15 18:53:53 +00:00
|
|
|
*ret = 0;
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2019-03-15 18:53:53 +00:00
|
|
|
/* overwrite settings from the command line */
|
2022-12-21 20:21:16 +00:00
|
|
|
if (!parse_args(argc, argv, &dumpSettings, foreground, ret))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
goto EXIT_EARLY;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
if (*foreground && logfile_ == TR_BAD_SYS_FILE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-05 21:28:15 +00:00
|
|
|
logfile_ = tr_sys_file_get_std(TR_STD_SYS_FILE_ERR);
|
2023-01-18 18:46:57 +00:00
|
|
|
logfile_flush_ = tr_sys_file_flush_possible(logfile_);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-04-04 17:43:56 +00:00
|
|
|
|
|
|
|
if (!loaded)
|
|
|
|
{
|
2022-09-05 21:28:15 +00:00
|
|
|
printMessage(logfile_, TR_LOG_ERROR, MyName, "Error loading config file -- exiting.", __FILE__, __LINE__);
|
2019-03-15 18:53:53 +00:00
|
|
|
*ret = 1;
|
2020-11-05 22:46:21 +00:00
|
|
|
goto EXIT_EARLY;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dumpSettings)
|
|
|
|
{
|
2023-08-17 16:02:45 +00:00
|
|
|
fmt::print("{:s}\n", tr_variant_serde::json().to_string(settings_));
|
2020-11-05 22:46:21 +00:00
|
|
|
goto EXIT_EARLY;
|
2019-03-15 18:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
EXIT_EARLY:
|
2019-03-15 18:53:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-15 16:25:12 +00:00
|
|
|
void tr_daemon::handle_error(tr_error* error) const
|
2022-09-13 06:37:12 +00:00
|
|
|
{
|
|
|
|
auto const errmsg = fmt::format(FMT_STRING("Couldn't daemonize: {:s} ({:d})"), error->message, error->code);
|
|
|
|
printMessage(logfile_, TR_LOG_ERROR, MyName, errmsg, __FILE__, __LINE__);
|
|
|
|
tr_error_free(error);
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:53:53 +00:00
|
|
|
int tr_main(int argc, char* argv[])
|
|
|
|
{
|
2023-07-06 03:05:03 +00:00
|
|
|
auto const init_mgr = tr_lib_init();
|
|
|
|
|
2023-04-27 14:10:33 +00:00
|
|
|
tr_locale_set_global("");
|
|
|
|
|
2019-03-15 18:53:53 +00:00
|
|
|
int ret;
|
2022-09-13 06:37:12 +00:00
|
|
|
tr_daemon daemon;
|
|
|
|
bool foreground;
|
|
|
|
tr_error* error = nullptr;
|
2019-03-15 18:53:53 +00:00
|
|
|
|
2022-09-05 21:28:15 +00:00
|
|
|
if (!daemon.init(argc, argv, &foreground, &ret))
|
2019-03-15 18:53:53 +00:00
|
|
|
{
|
|
|
|
return ret;
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
2022-09-13 06:37:12 +00:00
|
|
|
if (!daemon.spawn(foreground, &ret, &error))
|
2015-04-04 17:43:56 +00:00
|
|
|
{
|
2022-09-13 06:37:12 +00:00
|
|
|
daemon.handle_error(error);
|
2015-04-04 17:43:56 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|