Compare commits

...

13 Commits

Author SHA1 Message Date
rakshasa 6f8c1246dc Tagged release 0.15.3. 2025-05-01 14:34:45 +02:00
Jari Sundell 97415c656b Various scheduler and therad cleanups. 2025-05-01 16:38:49 +09:00
rakshasa 3d9c083032 Fix scheduler access. 2025-04-21 11:09:26 +02:00
Jari Sundell dd221ac66a Replaced Thread::next_timeout_usec(). 2025-04-16 02:39:40 +09:00
Rosen Penev 25f729fab5 use priotity_enum
The _t typedef is only useful with C, not C++.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2025-04-15 10:07:29 +02:00
rakshasa 316b0b51d5 Use new thread event loop method. 2025-04-14 11:15:07 +02:00
rakshasa a8622e5c80 Cleaned up thread-related code. 2025-04-12 23:03:08 +02:00
Abzie d1dd5ac080 Add RPC options to rtorrent.rc-example documentation 2025-04-11 16:16:44 +02:00
Abzie 9f2b81534a Add RPC options to rtorrent.rc documentation 2025-04-11 16:16:44 +02:00
Jari Sundell 2ab7460cbc Fix SCGI threading and added missing header. 2025-04-01 01:26:07 +09:00
rakshasa 2a998df4f1 Fixed LUA compilaition issue. 2025-03-31 06:19:02 +02:00
rakshasa d5b28c30a9 Added default switch cases to RpcManager. 2025-03-30 15:38:07 +02:00
rakshasa a5a1e2fde5 Updated common.m4. 2025-03-30 15:38:07 +02:00
40 changed files with 331 additions and 824 deletions
@@ -16,6 +16,9 @@ jobs:
# OPTIONAL: auto-closing conversations requires the `contents` permission
contents: write
steps:
- name: Sleep for 30 seconds
run: sleep 30s
shell: bash
- name: Download analysis results
uses: actions/github-script@v7
with:
+3 -3
View File
@@ -1,6 +1,6 @@
m4_pattern_allow([PKG_CHECK_EXISTS])
AC_INIT([rtorrent],[0.15.2],[sundell.software@gmail.com])
AC_INIT([rtorrent],[0.15.3],[sundell.software@gmail.com])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIRS([scripts])
@@ -10,7 +10,7 @@ AM_PROG_AR
LT_INIT
AC_PROG_CXX
AC_DEFINE([API_VERSION], [11], [api version])
AC_DEFINE([API_VERSION], [12], [api version])
# Filter out unwanted flags added by autoconf on some systems, e.g. MacOS.
TORRENT_REMOVE_UNWANTED(CXX, $CXX, -std=c++11 -std=gnu++11)
@@ -54,7 +54,7 @@ fi
PKG_CHECK_MODULES([LIBCURL], [libcurl],, [LIBCURL_CHECK_CONFIG])
PKG_CHECK_MODULES([CPPUNIT], [cppunit],, [no_cppunit="yes"])
PKG_CHECK_MODULES([DEPENDENCIES], [libtorrent >= 0.15.2])
PKG_CHECK_MODULES([DEPENDENCIES], [libtorrent >= 0.15.3])
AC_LANG_PUSH(C++)
TORRENT_WITH_XMLRPC_C
+4
View File
@@ -73,6 +73,10 @@
#
#network.port_random.set = no
# Set RPC type
#network.rpc.use_xmlrpc.set = true
#network.rpc.use_jsonrpc.set = true
# Check hash for finished torrents. Might be useful until the bug is
# fixed that causes lack of disk-space not to be properly reported.
#
+2
View File
@@ -76,6 +76,8 @@ schedule2 = monitor_diskspace, 15, 60, ((close_low_diskspace, 1000M))
##network.http.capath.set = "/etc/ssl/certs"
##network.http.ssl_verify_peer.set = 0
##network.http.ssl_verify_host.set = 0
#network.rpc.use_xmlrpc.set = true
#network.rpc.use_jsonrpc.set = true
# Some additional values and commands
method.insert = system.startup_time, value|const, (system.time)
+17 -2
View File
@@ -29,7 +29,7 @@ AC_DEFUN([TORRENT_REMOVE_UNWANTED],
$1="$2"
else
result=`echo "${values_to_check}" | $GREP -Fvx -- "${unwanted_values}" | $GREP -v '^$'`
$1=${result//$'\n'/ }
$1=$(echo "$result" | tr -d '\n')
fi
])
@@ -117,7 +117,7 @@ AC_DEFUN([TORRENT_CHECK_MADVISE], [
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <sys/types.h>
#include <sys/mman.h>
void f() { static char test@<:@1024@:>@; madvise((void *)test, sizeof(test), MADV_NORMAL); }
void f() { static char test@<:@1024@:>@; madvise((void *)test, sizeof(test), MADV_NORMAL); }
])],
[
AC_MSG_RESULT(yes)
@@ -127,6 +127,21 @@ AC_DEFUN([TORRENT_CHECK_MADVISE], [
])
])
AC_DEFUN([TORRENT_CHECK_POSIX_FADVISE], [
AC_MSG_CHECKING(for posix_fadvise)
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <fcntl.h>
void f() { posix_fadvise(0, 0, 0, POSIX_FADV_RANDOM); }
])],
[
AC_MSG_RESULT(yes)
AC_DEFINE(USE_POSIX_FADVISE, 1, Use posix_fadvise)
], [
AC_MSG_RESULT(no)
])
])
AC_DEFUN([TORRENT_CHECK_POPCOUNT], [
AC_MSG_CHECKING(for __builtin_popcount)
-2
View File
@@ -200,8 +200,6 @@ libsub_root_a_SOURCES = \
option_parser.h \
signal_handler.cc \
signal_handler.h \
thread_base.cc \
thread_base.h \
thread_worker.cc \
thread_worker.h
+4 -1
View File
@@ -1,5 +1,6 @@
#include "config.h"
#include <cassert>
#include <cstdio>
#include <functional>
#include <netdb.h>
@@ -308,8 +309,10 @@ apply_d_add_peer(core::Download* download, const std::string& arg) {
if (port < 1 || port > 65535)
throw torrent::input_error("Invalid port number.");
assert(std::this_thread::get_id() == torrent::main_thread()->thread_id());
// Currently discarding SOCK_STREAM.
torrent::main_thread()->resolver()->resolve_preferred(NULL, host, AF_UNSPEC, AF_INET, [download, port](torrent::c_sa_shared_ptr sa, int err) {
torrent::this_thread::resolver()->resolve_preferred(NULL, host, AF_UNSPEC, AF_INET, [download, port](torrent::c_sa_shared_ptr sa, int err) {
if (sa == nullptr) {
lt_log_print(torrent::LOG_TORRENT_WARN, "could not resolve hostname for added peer: %s", gai_strerror(err));
return;
+1 -1
View File
@@ -53,7 +53,7 @@ apply_f_set_priority(torrent::File* file, uint32_t value) {
if (value > torrent::PRIORITY_HIGH)
throw torrent::input_error("Invalid value.");
file->set_priority((torrent::priority_t)value);
file->set_priority(static_cast<torrent::priority_enum>(value));
}
// TODO: Redundant.
+5 -3
View File
@@ -220,9 +220,11 @@ initialize_command_local() {
CMD2_VAR_LIST ("file.prioritize_toc.first");
CMD2_VAR_LIST ("file.prioritize_toc.last");
CMD2_ANY ("system.files.advise_random", std::bind(&FM_t::advise_random, fileManager));
CMD2_ANY_VALUE_V ("system.files.advise_random.set", std::bind(&FM_t::set_advise_random, fileManager, std::placeholders::_2));
CMD2_VAR_BOOL ("system.files.session.fdatasync", true);
CMD2_ANY ("system.files.advise_random", std::bind(&FM_t::advise_random, fileManager));
CMD2_ANY_VALUE_V ("system.files.advise_random.set", std::bind(&FM_t::set_advise_random, fileManager, std::placeholders::_2));
// CMD2_ANY ("system.files.advise_random.hashing", std::bind(&FM_t::advise_random_hashing, fileManager));
// CMD2_ANY_VALUE_V ("system.files.advise_random.hashing.set", std::bind(&FM_t::set_advise_random_hashing, fileManager, std::placeholders::_2));
CMD2_VAR_BOOL ("system.files.session.fdatasync", true);
CMD2_ANY ("system.files.opened_counter", std::bind(&FM_t::files_opened_counter, fileManager));
CMD2_ANY ("system.files.closed_counter", std::bind(&FM_t::files_closed_counter, fileManager));
+4 -1
View File
@@ -1,5 +1,6 @@
#include "config.h"
#include <cassert>
#include <cstdio>
#include <netdb.h>
#include <rak/address_info.h>
@@ -44,8 +45,10 @@ apply_dht_add_node(const std::string& arg) {
if (port < 1 || port > 65535)
throw torrent::input_error("Invalid port number.");
assert(std::this_thread::get_id() == torrent::main_thread()->thread_id());
// Currently discarding SOCK_STREAM.
torrent::main_thread()->resolver()->resolve_specific(nullptr, host, PF_INET, [port](torrent::c_sa_shared_ptr sa, int err) {
torrent::this_thread::resolver()->resolve_specific(nullptr, host, PF_INET, [port](torrent::c_sa_shared_ptr sa, int err) {
if (sa == nullptr) {
lt_log_print(torrent::LOG_DHT_WARN, "Could not resolve host: %s", gai_strerror(err));
return;
+2 -1
View File
@@ -95,7 +95,8 @@ apply_view_set(const torrent::Object::list_type& args) {
// if (args.front().as_string() == "main")
// control->ui()->download_list()->set_view(*itr);
// else
throw torrent::input_error("No such target.");
throw torrent::input_error("No such target.");
}
torrent::Object
+3 -7
View File
@@ -96,7 +96,6 @@ Control::initialize() {
void
Control::cleanup() {
// delete m_scgi; m_scgi = NULL;
rpc::rpc.cleanup();
priority_queue_erase(&taskScheduler, &m_taskShutdown);
@@ -118,8 +117,6 @@ Control::cleanup() {
void
Control::cleanup_exception() {
// delete m_scgi; m_scgi = NULL;
display::Canvas::cleanup();
}
@@ -142,11 +139,11 @@ Control::handle_shutdown() {
rpc::commands.call_catch("event.system.shutdown", rpc::make_target(), "shutdown", "System shutdown event action failed: ");
if (!m_shutdownQuick) {
// Temporary hack:
if (worker_thread->is_active())
worker_thread->queue_item(&ThreadBase::stop_thread);
worker_thread->stop_thread();
torrent::connection_manager()->listen_close();
m_directory_events->close();
m_core->shutdown(false);
@@ -154,9 +151,8 @@ Control::handle_shutdown() {
priority_queue_insert(&taskScheduler, &m_taskShutdown, cachedTime + rak::timer::from_seconds(5));
} else {
// Temporary hack:
if (worker_thread->is_active())
worker_thread->queue_item(&ThreadBase::stop_thread);
worker_thread->stop_thread_wait();
m_core->shutdown(true);
}
+2 -38
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_CONTROL_H
#define RTORRENT_CONTROL_H
@@ -61,7 +25,7 @@ namespace display {
namespace input {
class InputEvent;
class Manager;
}
}
namespace rpc {
class CommandScheduler;
@@ -78,7 +42,7 @@ class Control {
public:
Control();
~Control();
bool is_shutdown_completed();
bool is_shutdown_received() { return m_shutdownReceived; }
bool is_shutdown_started() { return m_shutdownQuick; }
+2 -2
View File
@@ -90,11 +90,11 @@ DownloadList::create(torrent::Object* obj, uint32_t tracker_key, bool printLog)
download = torrent::download_add(obj, tracker_key);
} catch (torrent::local_error& e) {
delete obj;
if (printLog)
lt_log_print(torrent::LOG_TORRENT_ERROR, "Could not create download: %s", e.what());
delete obj;
return NULL;
}
+1
View File
@@ -14,6 +14,7 @@
#define RTORRENT_CORE_VIEW_DOWNLOADS_H
#include <functional>
#include <list>
#include <string>
#include <vector>
-37
View File
@@ -1,46 +1,9 @@
// rTorrent - BitTorrent library
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef TORRENT_GLOBALS_H
#define TORRENT_GLOBALS_H
#include <rak/timer.h>
#include <rak/priority_queue_default.h>
#include "thread_base.h"
#include "thread_worker.h"
#include "rpc/ip_table_list.h"
-36
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <algorithm>
-36
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include "input_event.h"
-36
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_INPUT_INPUT_EVENT_H
#define RTORRENT_INPUT_INPUT_EVENT_H
+1 -37
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <algorithm>
@@ -64,7 +28,7 @@ Manager::pressed(int key) {
if (m_textInput != NULL)
m_textInput->pressed(key);
else
std::find_if(rbegin(), rend(), [&key](Bindings* bind) { return bind->pressed(key); });
[[maybe_unused]] auto result = std::find_if(rbegin(), rend(), [&key](Bindings* bind) { return bind->pressed(key); });
}
}
-36
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_INPUT_MANAGER_H
#define RTORRENT_INPUT_MANAGER_H
+2 -3
View File
@@ -191,8 +191,7 @@ main(int argc, char** argv) {
torrent::Poll::slot_create_poll() = std::bind(&core::create_poll);
torrent::initialize();
torrent::main_thread()->slot_do_work() = std::bind(&client_perform);
torrent::main_thread()->slot_next_timeout() = std::bind(&client_next_timeout);
torrent::set_main_thread_slots(std::bind(&client_perform), std::bind(&client_next_timeout));
worker_thread = new ThreadWorker();
worker_thread->init_thread();
@@ -457,7 +456,7 @@ main(int argc, char** argv) {
rpc::commands.call_catch("event.system.startup_done", rpc::make_target(), "startup_done", "System startup_done event action failed: ");
torrent::utils::Thread::event_loop(torrent::main_thread());
torrent::main_thread()->event_loop();
control->core()->download_list()->session_save();
control->cleanup();
-36
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <torrent/exceptions.h>
+5 -41
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <fcntl.h>
@@ -43,16 +7,16 @@
#include <rak/path.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <torrent/utils/thread.h>
#include "exec_file.h"
#include "parse.h"
#include "thread_base.h"
namespace rpc {
const unsigned int ExecFile::max_args;
const unsigned int ExecFile::buffer_size;
const int ExecFile::flag_expand_tilde;
const int ExecFile::flag_throw;
const int ExecFile::flag_capture;
@@ -98,7 +62,7 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
if (detached_pid != 0) {
if (m_logFd != -1)
result = write(m_logFd, "\n--- Background task ---\n", sizeof("\n--- Background task ---\n"));
_exit(0);
}
@@ -139,7 +103,7 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
// We yield the global lock when waiting for the executed command to
// finish so that XMLRPC and other threads can continue working.
ThreadBase::release_global_lock();
torrent::utils::Thread::release_global_lock();
if (flags & flag_capture) {
m_capture = std::string();
@@ -170,7 +134,7 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
wpid = waitpid(childPid, &status, 0);
} while (wpid == -1 && rak::error_number::current().value() == rak::error_number::e_intr);
ThreadBase::acquire_global_lock();
torrent::utils::Thread::acquire_global_lock();
if (wpid != childPid)
throw torrent::internal_error("ExecFile::execute(...) waitpid failed.");
-1
View File
@@ -7,7 +7,6 @@
#include <torrent/common.h>
#include <torrent/torrent.h>
#include "thread_base.h"
#include "rpc/rpc_manager.h"
#include "rpc/command.h"
#include "rpc/command_map.h"
+67 -31
View File
@@ -86,19 +86,28 @@ int
LuaEngine::lua_rtorrent_call(lua_State* l_state) {
auto method = lua_tostring(l_state, 1);
lua_remove(l_state, 1);
torrent::Object object;
rpc::target_type target = rpc::make_target();
;
rpc::CommandMap::iterator itr = rpc::commands.find(std::string(method).c_str());
if (itr == rpc::commands.end()) {
throw torrent::input_error("method not found: " + std::string(method));
}
object = lua_callstack_to_object(l_state, itr->second.m_flags, &target);
auto target = rpc::make_target();
auto deleter = std::function<void()>();
auto object = lua_callstack_to_object(l_state, itr->second.m_flags, &target, &deleter);
try {
const auto& result = rpc::commands.call_command(itr, object, target);
object_to_lua(l_state, result);
deleter();
return 1;
} catch (torrent::base_error& e) {
deleter();
throw luaL_error(l_state, e.what());
}
}
@@ -107,27 +116,33 @@ int
LuaEngine::lua_init_module(lua_State* l_state) {
// Should this throw if it fails to find the file?
auto lua_file = search_lua_path(l_state);
if (!lua_file.empty()) {
check_lua_status(l_state, luaL_loadfile(l_state, lua_file.c_str()));
}
lua_createtable(l_state, 0, 1);
// Assign functions
lua_pushliteral(l_state, "call");
lua_pushcfunction(l_state, LuaEngine::lua_rtorrent_call);
lua_settable(l_state, -3);
if (!lua_file.empty()) {
check_lua_status(l_state, lua_pcall(l_state, 1, 1, 0));
}
return 1;
}
void
object_to_target(const torrent::Object& obj, int call_flags, rpc::target_type* target) {
object_to_target(const torrent::Object& obj, int call_flags, rpc::target_type* target, std::function<void()>* deleter) {
if (!obj.is_string()) {
throw torrent::input_error("invalid parameters: target must be a string");
}
std::string target_string = obj.as_string();
bool require_index = (call_flags & (CommandMap::flag_tracker_target | CommandMap::flag_file_target));
if (target_string.empty() && !require_index) {
return;
}
@@ -141,16 +156,20 @@ object_to_target(const torrent::Object& obj, int call_flags, rpc::target_type* t
std::string hash;
std::string index;
const auto& delim_pos = target_string.find_first_of(':', 40);
if (delim_pos == target_string.npos || delim_pos + 2 >= target_string.size()) {
if (require_index) {
throw torrent::input_error("invalid parameters: no index");
}
hash = target_string;
} else {
hash = target_string.substr(0, delim_pos);
type = target_string[delim_pos + 1];
index = target_string.substr(delim_pos + 2);
}
core::Download* download = rpc.slot_find_download()(hash.c_str());
if (download == nullptr)
@@ -161,22 +180,32 @@ object_to_target(const torrent::Object& obj, int call_flags, rpc::target_type* t
case 'd':
*target = rpc::make_target(download);
break;
case 'f':
*target = rpc::make_target(command_base::target_file, rpc.slot_find_file()(download, std::stoi(std::string(index))));
break;
case 't':
*target =
rpc::make_target(command_base::target_tracker, rpc.slot_find_tracker()(download, std::stoi(std::string(index))));
break;
case 'p': {
if (index.size() < 40) {
throw torrent::input_error("Not a hash string.");
{
auto tracker = new torrent::tracker::Tracker(rpc.slot_find_tracker()(download, std::stoi(std::string(index))));
*deleter = [tracker]() { delete tracker; };
*target = rpc::make_target(command_base::target_tracker, target);
}
torrent::HashString hash;
torrent::hash_string_from_hex_c_str(index.c_str(), hash);
*target = rpc::make_target(command_base::target_peer, rpc.slot_find_peer()(download, hash));
break;
}
case 'p':
{
if (index.size() < 40) {
throw torrent::input_error("Not a hash string.");
}
torrent::HashString hash;
torrent::hash_string_from_hex_c_str(index.c_str(), hash);
*target = rpc::make_target(command_base::target_peer, rpc.slot_find_peer()(download, hash));
}
break;
default:
throw torrent::input_error("invalid parameters: unexpected target type");
}
@@ -291,8 +320,7 @@ lua_to_object(lua_State* l_state) {
}
torrent::Object
lua_callstack_to_object(lua_State* l_state, int command_flags, rpc::target_type* target) {
torrent::Object object;
lua_callstack_to_object(lua_State* l_state, int command_flags, rpc::target_type* target, std::function<void()>* deleter) {
if (lua_gettop(l_state) == 0) {
return torrent::Object();
}
@@ -300,30 +328,28 @@ lua_callstack_to_object(lua_State* l_state, int command_flags, rpc::target_type*
if (!lua_isstring(l_state, 1)) {
throw torrent::input_error("invalid parameters: target must be a string");
}
object_to_target(torrent::Object(lua_tostring(l_state, 1)), command_flags, target);
object_to_target(torrent::Object(lua_tostring(l_state, 1)), command_flags, target, deleter);
// start from the second argument since the first is the target
lua_remove(l_state, 1);
if (lua_gettop(l_state) == 0) {
if (lua_gettop(l_state) == 0)
return torrent::Object();
} else {
torrent::Object result = torrent::Object::create_list();
torrent::Object::list_type& list_ref = result.as_list();
while (lua_gettop(l_state) != 0) {
list_ref.insert(list_ref.begin(), lua_to_object(l_state));
lua_remove(l_state, -1);
}
return result;
torrent::Object result = torrent::Object::create_list();
torrent::Object::list_type& list_ref = result.as_list();
while (lua_gettop(l_state) != 0) {
list_ref.insert(list_ref.begin(), lua_to_object(l_state));
lua_remove(l_state, -1);
}
return object;
return result;
}
int
lua_rtorrent_call(lua_State* l_state) {
torrent::Object object;
rpc::target_type target = rpc::make_target();
auto method = lua_tostring(l_state, 1);
lua_remove(l_state, 1);
@@ -331,12 +357,22 @@ lua_rtorrent_call(lua_State* l_state) {
if (itr == rpc::commands.end()) {
throw torrent::input_error("method not found: " + std::string(method));
}
object = lua_callstack_to_object(l_state, itr->second.m_flags, &target);
auto target = rpc::make_target();
auto deleter = std::function<void()>();
auto object = lua_callstack_to_object(l_state, itr->second.m_flags, &target, &deleter);
try {
const auto& result = rpc::commands.call_command(itr, object, target);
object_to_lua(l_state, result);
deleter();
return 1;
} catch (torrent::base_error& e) {
deleter();
throw luaL_error(l_state, e.what());
}
}
+5 -1
View File
@@ -17,6 +17,7 @@ public:
LuaEngine();
~LuaEngine();
#ifdef HAVE_LUA
// lua_CFunctions
static int lua_init_module(lua_State* l_state);
@@ -29,15 +30,18 @@ private:
lua_State* m_luaState;
#endif
};
torrent::Object execute_lua(LuaEngine* engine, rpc::target_type target, const torrent::Object& raw_args, int flags);
#ifdef HAVE_LUA
int rtorrent_call(lua_State* l_state);
void init_rtorrent_module(lua_State* l_state);
void object_to_lua(lua_State* l_state, torrent::Object const& object);
void check_lua_status(lua_State* l_state, int status);
torrent::Object lua_to_object(lua_State* l_state);
torrent::Object lua_callstack_to_object(lua_State* l_state, int command_flags, rpc::target_type* target);
torrent::Object lua_callstack_to_object(lua_State* l_state, int command_flags, rpc::target_type* target, std::function<void()>* deleter);
#endif
} // namespace rpc
+8 -44
View File
@@ -1,39 +1,3 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#include "config.h"
#include <algorithm>
@@ -43,8 +7,8 @@
#include <rak/path.h>
#include <torrent/exceptions.h>
#include "parse.h"
#include "parse_commands.h"
#include "rpc/parse.h"
#include "rpc/parse_commands.h"
#include "rpc/rpc_manager.h"
namespace rpc {
@@ -128,12 +92,12 @@ parse_command(target_type target, const char* first, const char* last) {
if (first == last || *first == '#')
return std::make_pair(torrent::Object(), first);
char key[128];
first = parse_command_name(first, last, key, key + 128);
first = std::find_if(first, last, [&](char c) { return !command_map_is_space(c); });
if (first == last || *first != '=')
throw torrent::input_error("Could not find '=' in command '" + std::string(key) + "'.");
@@ -144,7 +108,7 @@ parse_command(target_type target, const char* first, const char* last) {
// the whitespace at the end. This ensures us that the caller
// doesn't need to do this nor check for junk at the end.
first = std::find_if(first, last, [&](char c) { return !command_map_is_space(c); });
if (first != last) {
if (!command_map_is_newline(*first))
throw torrent::input_error("Junk at end of input.");
@@ -189,7 +153,7 @@ parse_command_file(const std::string& path) {
while (file.good()
&& !file.getline(buffer + getCount, 4096 - getCount).fail()) {
if (file.gcount() == 0)
throw torrent::internal_error("parse_command_file(...) file.gcount() == 0.");
int lineLength = file.gcount() - 1;
@@ -198,7 +162,7 @@ parse_command_file(const std::string& path) {
// that the last would also be included in option line.
if (file.eof() && file.get() != '\n')
lineLength++;
int escaped = parse_count_escaped(buffer + getCount, buffer + getCount + lineLength);
lineNumber++;
@@ -206,7 +170,7 @@ parse_command_file(const std::string& path) {
if (getCount == 4096 - 1)
throw torrent::input_error("Exceeded max line length.");
if (escaped & 0x1) {
// Remove the escape characters and continue reading.
getCount -= escaped;
+17 -4
View File
@@ -99,22 +99,29 @@ RpcManager::object_to_target(const torrent::Object& obj, int call_flags, rpc::ta
bool
RpcManager::process(RPCType type, const char* in_buffer, uint32_t length, slot_response_callback callback) {
switch (type) {
case RPCType::XML: {
case RPCType::XML:
// TODO: 'network.rpc.use_xmlrpc' should be a bool in RpcManager, not a command variable.
if (m_xmlrpc.is_valid() && rpc::call_command_value("network.rpc.use_xmlrpc")) {
return m_xmlrpc.process(in_buffer, length, callback);
} else {
const std::string response = "<?xml version=\"1.0\"?><methodResponse><fault><struct><member><name>faultCode</name><value><i8>-501</i8></value></member><member><name>faultString</name><value><string>XML-RPC not supported</string></value></member></struct></fault></methodResponse>";
return callback(response.c_str(), response.size());
}
}
case RPCType::JSON: {
break;
case RPCType::JSON:
if (rpc::call_command_value("network.rpc.use_jsonrpc")) {
return m_jsonrpc.process(in_buffer, length, callback);
} else {
const std::string response = "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32601,\"message\":\"JSON-RPC not supported\"},\"id\":null}";
return callback(response.c_str(), response.size());
}
}
break;
default:
throw torrent::input_error("invalid parameters: unknown RPC type");
}
}
@@ -139,6 +146,8 @@ RpcManager::is_type_enabled(RPCType type) const {
return m_is_xmlrpc_enabled;
case RPCType::JSON:
return m_is_jsonrpc_enabled;
default:
throw torrent::input_error("invalid parameters: unknown RPC type");
}
}
@@ -147,8 +156,12 @@ RpcManager::set_type_enabled(RPCType type, bool enabled) {
switch (type) {
case RPCType::XML:
m_is_xmlrpc_enabled = enabled;
break;
case RPCType::JSON:
m_is_jsonrpc_enabled = enabled;
break;
default:
throw torrent::input_error("invalid parameters: unknown RPC type");
}
}
+6 -27
View File
@@ -1,6 +1,5 @@
#include "config.h"
#include "rpc/scgi_task.h"
#include <rak/error_number.h>
#include <rak/socket_address.h>
#include <sys/un.h>
@@ -9,12 +8,12 @@
#include <torrent/torrent.h>
#include <torrent/exceptions.h>
#include "utils/socket_fd.h"
#include "control.h"
#include "globals.h"
#include "scgi.h"
#include "parse_commands.h"
#include "rpc/scgi_task.h"
#include "utils/socket_fd.h"
#include "rpc/scgi.h"
namespace rpc {
@@ -87,6 +86,8 @@ SCgi::open(void* sa, unsigned int length) {
}
}
// TODO: Verify this is run in correct thread, also only ever call poll methods from thread_self.
void
SCgi::activate() {
worker_thread->poll()->open(this);
@@ -110,7 +111,6 @@ SCgi::event_read() {
SCgiTask* task = std::find_if(m_task, m_task + max_tasks, std::mem_fn(&SCgiTask::is_available));
if (task == m_task + max_tasks) {
// Ergh... just closing for now.
fd.close();
continue;
}
@@ -129,25 +129,4 @@ SCgi::event_error() {
throw torrent::internal_error("SCGI listener port received an error event.");
}
bool
SCgi::receive_call(SCgiTask* task, const char* buffer, uint32_t length) {
bool result = false;
torrent::utils::Thread::acquire_global_lock();
torrent::main_thread()->interrupt();
switch (task->content_type()) {
case rpc::SCgiTask::ContentType::JSON:
result = rpc.process(RpcManager::RPCType::JSON, buffer, length, [task](const char* b, uint32_t l) { return task->receive_write(b, l); });
break;
case rpc::SCgiTask::ContentType::XML:
result = rpc.process(RpcManager::RPCType::XML, buffer, length, [task](const char* b, uint32_t l) { return task->receive_write(b, l); });
break;
}
torrent::utils::Thread::release_global_lock();
return result;
}
}
+4 -45
View File
@@ -1,46 +1,11 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_RPC_SCGI_H
#define RTORRENT_RPC_SCGI_H
#include <functional>
#include <torrent/event.h>
#include "scgi_task.h"
#include "rpc/scgi_task.h"
#include "utils/socket_fd.h"
namespace utils {
class SocketFd;
@@ -48,17 +13,14 @@ namespace utils {
namespace rpc {
class lt_cacheline_aligned SCgi : public torrent::Event {
class SCgi : public torrent::Event {
public:
typedef std::function<bool (const char*, uint32_t)> slot_write;
static const int max_tasks = 100;
// Global lock:
SCgi() : m_logFd(-1) {}
virtual ~SCgi();
const char* type_name() const { return "scgi"; }
virtual const char* type_name() const { return "scgi"; }
void open_port(void* sa, unsigned int length, bool dontRoute);
void open_named(const std::string& filename);
@@ -71,13 +33,10 @@ public:
int log_fd() const { return m_logFd; }
void set_log_fd(int fd) { m_logFd = fd; }
// Thread local:
virtual void event_read();
virtual void event_write();
virtual void event_error();
bool receive_call(SCgiTask* task, const char* buffer, uint32_t length);
utils::SocketFd& get_fd() { return *reinterpret_cast<utils::SocketFd*>(&m_fileDesc); }
private:
+97 -59
View File
@@ -8,26 +8,22 @@
#include <sys/socket.h>
#include <torrent/exceptions.h>
#include <torrent/poll.h>
#include <torrent/torrent.h>
#include <torrent/utils/log.h>
#include "utils/socket_fd.h"
#include <torrent/utils/thread.h>
#include "control.h"
#include "globals.h"
#include "scgi.h"
// Test:
// #include "core/manager.h"
// #include <rak/timer.h>
// static rak::timer scgiTimer;
#include "rpc/parse_commands.h"
#include "utils/socket_fd.h"
namespace rpc {
// If bufferSize is zero then memcpy won't do anything.
inline void
SCgiTask::realloc_buffer(uint32_t size, const char* buffer, uint32_t bufferSize) {
char* tmp = rak::cacheline_allocator<char>::alloc_size(size);
char* tmp = new char[size];
std::memcpy(tmp, buffer, bufferSize);
::free(m_buffer);
@@ -36,17 +32,16 @@ SCgiTask::realloc_buffer(uint32_t size, const char* buffer, uint32_t bufferSize)
void
SCgiTask::open(SCgi* parent, int fd) {
m_parent = parent;
m_fileDesc = fd;
m_buffer = rak::cacheline_allocator<char>::alloc_size((m_bufferSize = default_buffer_size) + 1);
m_position = m_buffer;
m_body = NULL;
m_parent = parent;
m_fileDesc = fd;
m_buffer = new char[default_buffer_size + 1];
m_buffer_size = default_buffer_size;
m_position = m_buffer;
m_body = NULL;
worker_thread->poll()->open(this);
worker_thread->poll()->insert_read(this);
worker_thread->poll()->insert_error(this);
// scgiTimer = rak::timer::current();
torrent::thread_self()->poll()->open(this);
torrent::thread_self()->poll()->insert_read(this);
torrent::thread_self()->poll()->insert_error(this);
}
void
@@ -54,26 +49,26 @@ SCgiTask::close() {
if (!get_fd().is_valid())
return;
worker_thread->poll()->remove_read(this);
worker_thread->poll()->remove_write(this);
worker_thread->poll()->remove_error(this);
worker_thread->poll()->close(this);
torrent::main_thread()->cancel_callback_and_wait(this);
torrent::thread_self()->cancel_callback(this);
torrent::thread_self()->poll()->remove_read(this);
torrent::thread_self()->poll()->remove_write(this);
torrent::thread_self()->poll()->remove_error(this);
torrent::thread_self()->poll()->close(this);
get_fd().close();
get_fd().clear();
::free(m_buffer);
m_buffer = NULL;
auto lock = std::lock_guard<std::mutex>(m_result_mutex);
// Test
// char buffer[512];
// sprintf(buffer, "SCgi system call processed: %i", (int)(rak::timer::current() - scgiTimer).usec());
// control->core()->push_log(std::string(buffer));
delete[] m_buffer;
m_buffer = NULL;
}
void
SCgiTask::event_read() {
int bytes = ::recv(m_fileDesc, m_position, m_bufferSize - (m_position - m_buffer), 0);
int bytes = ::recv(m_fileDesc, m_position, m_buffer_size - (m_position - m_buffer), 0);
if (bytes <= 0) {
if (bytes == 0 || !rak::error_number::current().is_blocked_momentary())
@@ -119,17 +114,21 @@ SCgiTask::event_read() {
while (current < header_end) {
char* key = current;
char* key_end = static_cast<char*>(std::memchr(current, '\0', header_end - current));
if (!key_end)
goto event_read_failed;
current = key_end + 1;
if (current >= header_end)
goto event_read_failed;
char* value = current;
char* value_end = static_cast<char*>(std::memchr(current, '\0', header_end - current));
if (!value_end)
goto event_read_failed;
current = value_end + 1;
if (strcmp(key, "CONTENT_LENGTH") == 0) {
@@ -164,45 +163,41 @@ SCgiTask::event_read() {
goto event_read_failed;
}
if ((unsigned int)(content_length + header_size) < m_bufferSize) {
m_bufferSize = content_length + header_size;
if ((unsigned int)(content_length + header_size) < m_buffer_size) {
m_buffer_size = content_length + header_size;
} else if ((unsigned int)content_length <= default_buffer_size) {
m_bufferSize = content_length;
m_buffer_size = content_length;
std::memmove(m_buffer, m_body, std::distance(m_body, m_position));
m_position = m_buffer + std::distance(m_body, m_position);
m_body = m_buffer;
} else {
realloc_buffer((m_bufferSize = content_length) + 1, m_body, std::distance(m_body, m_position));
realloc_buffer((m_buffer_size = content_length) + 1, m_body, std::distance(m_body, m_position));
m_position = m_buffer + std::distance(m_body, m_position);
m_body = m_buffer;
}
}
if ((unsigned int)std::distance(m_buffer, m_position) != m_bufferSize)
if ((unsigned int)std::distance(m_buffer, m_position) != m_buffer_size)
return;
worker_thread->poll()->remove_read(this);
worker_thread->poll()->insert_write(this);
torrent::thread_self()->poll()->remove_read(this);
if (m_parent->log_fd() >= 0) {
int __UNUSED result;
// Clean up logging, this is just plain ugly...
// write(m_logFd, "\n---\n", sizeof("\n---\n"));
result = write(m_parent->log_fd(), m_buffer, m_bufferSize);
result = write(m_parent->log_fd(), m_buffer, m_buffer_size);
result = write(m_parent->log_fd(), "\n---\n", sizeof("\n---\n"));
}
lt_log_print_dump(torrent::LOG_RPC_DUMP, m_body, m_bufferSize - std::distance(m_buffer, m_body), "scgi", "RPC read.", 0);
// Close if the call failed, else stay open to write back data.
if (!m_parent->receive_call(this, m_body, m_bufferSize - std::distance(m_buffer, m_body)))
close();
lt_log_print_dump(torrent::LOG_RPC_DUMP, m_body, m_buffer_size - std::distance(m_buffer, m_body), "scgi", "RPC read.", 0);
receive_call(m_body, m_buffer_size - std::distance(m_buffer, m_body));
return;
event_read_failed:
@@ -215,9 +210,9 @@ SCgiTask::event_write() {
// Apple and Solaris do not support MSG_NOSIGNAL,
// so disable this fix until we find a better solution
#if defined(__APPLE__) || defined(__sun__)
int bytes = ::send(m_fileDesc, m_position, m_bufferSize, 0);
int bytes = ::send(m_fileDesc, m_position, m_buffer_size, 0);
#else
int bytes = ::send(m_fileDesc, m_position, m_bufferSize, MSG_NOSIGNAL);
int bytes = ::send(m_fileDesc, m_position, m_buffer_size, MSG_NOSIGNAL);
#endif
if (bytes == -1) {
@@ -228,9 +223,9 @@ SCgiTask::event_write() {
}
m_position += bytes;
m_bufferSize -= bytes;
m_buffer_size -= bytes;
if (bytes == 0 || m_bufferSize == 0)
if (bytes == 0 || m_buffer_size == 0)
return close();
}
@@ -239,13 +234,61 @@ SCgiTask::event_error() {
close();
}
bool
void
SCgiTask::receive_call(const char* buffer, uint32_t length) {
// TODO: Rewrite RpcManager.process to pass the result buffer instead of having to copy it.
auto scgi_thread = torrent::thread_self();
auto result_callback = [this, scgi_thread](const char* b, uint32_t l) {
receive_write(b, l);
scgi_thread->callback(this, [this]() {
// Only need to lock once here as a memory barrier.
m_result_mutex.lock();
m_result_mutex.unlock();
torrent::thread_self()->poll()->insert_write(this);
});
};
auto lock = std::lock_guard<std::mutex>(m_result_mutex);
switch (content_type()) {
case rpc::SCgiTask::ContentType::JSON:
torrent::main_thread()->callback(this, [buffer, length, result_callback]() {
rpc.process(RpcManager::RPCType::JSON, buffer, length,
[result_callback](const char* b, uint32_t l) {
result_callback(b, l);
return true;
});
});
break;
case rpc::SCgiTask::ContentType::XML:
torrent::main_thread()->callback(this, [buffer, length, result_callback]() {
rpc.process(RpcManager::RPCType::XML, buffer, length,
[result_callback](const char* b, uint32_t l) {
result_callback(b, l);
return true;
});
});
break;
default:
throw torrent::internal_error("SCgiTask::receive_call(...) received bad input.");
}
}
void
SCgiTask::receive_write(const char* buffer, uint32_t length) {
if (buffer == NULL || length > (100 << 20))
throw torrent::internal_error("SCgiTask::receive_write(...) received bad input.");
auto lock = std::lock_guard<std::mutex>(m_result_mutex);
// Need to cast due to a bug in MacOSX gcc-4.0.1.
if (length + 256 > std::max(m_bufferSize, (unsigned int)default_buffer_size))
if (length + 256 > std::max(m_buffer_size, (unsigned int)default_buffer_size))
realloc_buffer(length + 256, NULL, 0);
const auto header = m_content_type == ContentType::JSON
@@ -253,25 +296,20 @@ SCgiTask::receive_write(const char* buffer, uint32_t length) {
: "Status: 200 OK\r\nContent-Type: text/xml\r\nContent-Length: %i\r\n\r\n";
// Who ever bothers to check the return value?
int headerSize = sprintf(m_buffer, header, length);
int headerSize = snprintf(m_buffer, m_buffer_size, header, length);
m_position = m_buffer;
m_bufferSize = length + headerSize;
m_buffer_size = length + headerSize;
std::memcpy(m_buffer + headerSize, buffer, length);
if (m_parent->log_fd() >= 0) {
int __UNUSED result;
// Clean up logging, this is just plain ugly...
// write(m_logFd, "\n---\n", sizeof("\n---\n"));
result = write(m_parent->log_fd(), m_buffer, m_bufferSize);
int result [[maybe_unused]];
result = write(m_parent->log_fd(), m_buffer, m_buffer_size);
result = write(m_parent->log_fd(), "\n---\n", sizeof("\n---\n"));
}
lt_log_print_dump(torrent::LOG_RPC_DUMP, m_buffer, m_bufferSize, "scgi", "RPC write.", 0);
event_write();
return true;
lt_log_print_dump(torrent::LOG_RPC_DUMP, m_buffer, m_buffer_size, "scgi", "RPC write.", 0);
}
} // namespace rpc
+12 -43
View File
@@ -1,42 +1,8 @@
// rTorrent - BitTorrent client
// Copyright (C) 2005-2011, Jari Sundell
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// In addition, as a special exception, the copyright holders give
// permission to link the code of portions of this program with the
// OpenSSL library under certain conditions as described in each
// individual source file, and distribute linked combinations
// including the two.
//
// You must obey the GNU General Public License in all respects for
// all of the code used other than OpenSSL. If you modify file(s)
// with this exception, you may extend this exception to your version
// of the file(s), but you are not obligated to do so. If you do not
// wish to do so, delete this exception statement from your version.
// If you delete this exception statement from all source files in the
// program, then also delete it here.
//
// Contact: Jari Sundell <jaris@ifi.uio.no>
//
// Skomakerveien 33
// 3185 Skoppum, NORWAY
#ifndef RTORRENT_RPC_SCGI_TASK_H
#define RTORRENT_RPC_SCGI_TASK_H
#include <memory>
#include <mutex>
#include <torrent/event.h>
namespace utils {
@@ -69,20 +35,23 @@ public:
virtual void event_write();
virtual void event_error();
bool receive_write(const char* buffer, uint32_t length);
utils::SocketFd& get_fd() { return *reinterpret_cast<utils::SocketFd*>(&m_fileDesc); }
utils::SocketFd& get_fd() { return *reinterpret_cast<utils::SocketFd*>(&m_fileDesc); }
private:
inline void realloc_buffer(uint32_t size, const char* buffer, uint32_t bufferSize);
void receive_call(const char* buffer, uint32_t length);
void receive_write(const char* buffer, uint32_t length);
SCgi* m_parent;
char* m_buffer;
char* m_position;
char* m_body;
std::mutex m_result_mutex;
unsigned int m_bufferSize;
char* m_buffer{nullptr};
char* m_position{nullptr};
char* m_body{nullptr};
unsigned int m_buffer_size{0};
ContentType m_content_type{ XML };
};
-105
View File
@@ -1,105 +0,0 @@
#include "config.h"
#include "thread_base.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <rak/error_number.h>
#include <torrent/exceptions.h>
#include <torrent/torrent.h>
#include <torrent/utils/log.h>
#include <unistd.h>
#include "globals.h"
class thread_queue_hack : private std::vector<ThreadBase::thread_base_func> {
public:
static constexpr unsigned int max_size = 32;
using value_type = ThreadBase::thread_base_func;
using base_type = std::vector<value_type>;
thread_queue_hack() { reserve(max_size); };
bool empty() {
std::lock_guard<std::mutex> lguard(m_lock);
return base_type::empty();
}
void push_back(value_type v) {
const std::lock_guard<std::mutex> lguard(m_lock);
if (base_type::size() >= max_size)
throw torrent::internal_error("Overflowed thread_queue max size of " + std::to_string(max_size) + ".");
base_type::push_back(v);
}
void copy_and_clear(base_type& target) {
std::lock_guard<std::mutex> lguard(m_lock);
target.assign(begin(), end());
base_type::clear();
}
private:
std::mutex m_lock;
};
void
throw_shutdown_exception() { throw torrent::shutdown_exception(); }
ThreadBase::ThreadBase() {
m_taskShutdown.slot() = std::bind(&throw_shutdown_exception);
m_threadQueue = std::make_unique<thread_queue_hack>();
}
// Defined in here and not the header so that the default destructor
// can properly deduce how to destruct the unique_ptr<thread_queue_hack>
ThreadBase::~ThreadBase() = default;
// Move to libtorrent...
void
ThreadBase::stop_thread(ThreadBase* thread) {
if (!thread->m_taskShutdown.is_queued())
priority_queue_insert(&thread->m_taskScheduler, &thread->m_taskShutdown, cachedTime);
}
int64_t
ThreadBase::next_timeout_usec() {
if (m_taskScheduler.empty())
return rak::timer::from_seconds(600).usec();
else if (m_taskScheduler.top()->time() <= cachedTime)
return 0;
else
return (m_taskScheduler.top()->time() - cachedTime).usec();
}
void
ThreadBase::call_queued_items() {
std::vector<thread_queue_hack::value_type> queue;
m_threadQueue->copy_and_clear(queue);
for (auto itr : queue) {
itr(this);
}
}
void
ThreadBase::call_events() {
// Check for new queued items set by other threads.
if (!m_threadQueue->empty())
call_queued_items();
rak::priority_queue_perform(&m_taskScheduler, cachedTime);
process_callbacks();
}
void
ThreadBase::queue_item(thread_base_func newFunc) {
m_threadQueue->push_back(newFunc);
// Make it also restart inactive threads?
if (m_state == STATE_ACTIVE)
interrupt();
}
-53
View File
@@ -1,53 +0,0 @@
#ifndef RTORRENT_UTILS_THREAD_BASE_H
#define RTORRENT_UTILS_THREAD_BASE_H
#include <memory>
#include <pthread.h>
#include <sys/types.h>
#include <torrent/utils/thread.h>
#include "rak/priority_queue_default.h"
#include "core/poll_manager.h"
// Move this class to libtorrent.
class thread_queue_hack;
class ThreadBase : public torrent::utils::Thread {
public:
typedef rak::priority_queue_default priority_queue;
typedef void (*thread_base_func)(ThreadBase*);
ThreadBase();
virtual ~ThreadBase();
priority_queue& task_scheduler() { return m_taskScheduler; }
// Throw torrent::shutdown_exception to stop the thread.
static void stop_thread(ThreadBase* thread);
// ATM, only interaction with a thread's allowed by other threads is
// through the queue_item call.
void queue_item(thread_base_func newFunc);
protected:
int64_t next_timeout_usec();
void call_queued_items();
virtual void call_events();
// TODO: Add thread name.
// The timer needs to be sync'ed when updated...
rak::priority_queue_default m_taskScheduler;
rak::priority_item m_taskShutdown;
// Temporary hack to pass messages to a thread. This really needs to
// be cleaned up and/or integrated into the priority queue itself.
std::unique_ptr<thread_queue_hack> m_threadQueue;
};
#endif
+39 -30
View File
@@ -1,8 +1,6 @@
#include "config.h"
#include "thread_worker.h"
#include "globals.h"
#include "control.h"
#include <fcntl.h>
#include <unistd.h>
@@ -11,12 +9,14 @@
#include <torrent/exceptions.h>
#include <torrent/poll.h>
#include "globals.h"
#include "control.h"
#include "core/manager.h"
#include "rpc/scgi.h"
#include "rpc/parse_commands.h"
ThreadWorker::~ThreadWorker() {
if (m_scgi)
if (m_scgi != nullptr)
m_scgi.load()->deactivate();
}
@@ -29,39 +29,28 @@ ThreadWorker::init_thread() {
bool
ThreadWorker::set_scgi(rpc::SCgi* scgi) {
rpc::SCgi* expected = nullptr;
if (!m_scgi.compare_exchange_strong(expected, scgi))
return false;
change_rpc_log();
queue_item((thread_base_func)&start_scgi);
callback(nullptr, [this]() {
if (m_scgi == NULL)
throw torrent::internal_error("Tried to start SCGI but object was not present.");
m_scgi.load()->activate();
});
return true;
}
void
ThreadWorker::set_rpc_log(const std::string& filename) {
m_rpcLog = filename;
queue_item((thread_base_func)&msg_change_rpc_log);
}
void
ThreadWorker::start_scgi(ThreadBase* baseThread) {
ThreadWorker* thread = (ThreadWorker*)baseThread;
if (thread->scgi() == NULL)
throw torrent::internal_error("Tried to start SCGI but object was not present.");
thread->scgi()->activate();
}
void
ThreadWorker::msg_change_rpc_log(ThreadBase* baseThread) {
ThreadWorker* thread = (ThreadWorker*)baseThread;
acquire_global_lock();
thread->change_rpc_log();
release_global_lock();
callback(nullptr, [this, filename]() {
m_rpc_log_filename = filename;
change_rpc_log();
});
}
void
@@ -75,15 +64,35 @@ ThreadWorker::change_rpc_log() {
control->core()->push_log("Closed RPC log.");
}
if (m_rpcLog.empty())
if (m_rpc_log_filename.empty())
return;
scgi()->set_log_fd(open(rak::path_expand(m_rpcLog).c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644));
scgi()->set_log_fd(open(rak::path_expand(m_rpc_log_filename).c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644));
if (scgi()->log_fd() == -1) {
control->core()->push_log_std("Could not open RPC log file '" + m_rpcLog + "'.");
control->core()->push_log_std("Could not open RPC log file '" + m_rpc_log_filename + "'.");
return;
}
control->core()->push_log_std("Logging RPC events to '" + m_rpcLog + "'.");
control->core()->push_log_std("Logging RPC events to '" + m_rpc_log_filename + "'.");
}
void
ThreadWorker::call_events() {
if ((m_flags & flag_do_shutdown)) {
if ((m_flags & flag_did_shutdown))
throw torrent::internal_error("Already trigged shutdown.");
m_flags |= flag_did_shutdown;
throw torrent::shutdown_exception();
}
cachedTime = rak::timer::current();
process_callbacks();
}
std::chrono::microseconds
ThreadWorker::next_timeout() {
return std::chrono::microseconds(10min);
}
+10 -18
View File
@@ -2,44 +2,36 @@
#define RTORRENT_THREAD_WORKER_H
#include <atomic>
#include "thread_base.h"
#include "rak/priority_queue_default.h"
#include <string>
#include <torrent/utils/thread.h>
namespace rpc {
class SCgi;
}
// Check if cacheline aligned with inheritance ends up taking two
// cachelines.
class ThreadWorker : public ThreadBase {
class ThreadWorker : public torrent::utils::Thread {
public:
ThreadWorker() = default;
~ThreadWorker();
const char* name() const { return "rtorrent scgi"; }
const char* name() const override { return "rtorrent scgi"; }
virtual void init_thread();
void init_thread() override;
rpc::SCgi* scgi() { return m_scgi; }
rpc::SCgi* scgi() { return m_scgi; }
bool set_scgi(rpc::SCgi* scgi);
void set_rpc_log(const std::string& filename);
static void start_scgi(ThreadBase* thread);
static void msg_change_rpc_log(ThreadBase* thread);
private:
void task_touch_log();
void change_rpc_log();
std::atomic<rpc::SCgi*> m_scgi{ nullptr };
void call_events() override;
std::chrono::microseconds next_timeout() override;
// The following types shall only be modified while holding the
// global lock.
std::string m_rpcLog;
std::atomic<rpc::SCgi*> m_scgi{nullptr};
std::string m_rpc_log_filename;
};
#endif
+1 -1
View File
@@ -70,7 +70,7 @@ ElementDownloadList::ElementDownloadList() :
}
void
ElementDownloadList::activate(display::Frame* frame, bool focus) {
ElementDownloadList::activate(display::Frame* frame, [[maybe_unused]] bool focus) {
if (is_active())
throw torrent::internal_error("ui::ElementDownloadList::activate(...) is_active().");
+2 -2
View File
@@ -265,7 +265,7 @@ ElementFileList::receive_priority() {
if (m_window == NULL)
return;
torrent::priority_t priority = torrent::priority_t((m_selected.file()->priority() + 2) % 3);
auto priority = torrent::priority_enum((m_selected.file()->priority() + 2) % 3);
iterator first = m_selected;
iterator last = m_selected;
@@ -288,7 +288,7 @@ ElementFileList::receive_change_all() {
return;
torrent::FileList* fl = m_download->download()->file_list();
torrent::priority_t priority = torrent::priority_t((m_selected.file()->priority() + 2) % 3);
auto priority = torrent::priority_enum((m_selected.file()->priority() + 2) % 3);
for (torrent::FileList::iterator itr = fl->begin(), last = fl->end(); itr != last; ++itr)
(*itr)->set_priority(priority);
+2 -1
View File
@@ -17,7 +17,8 @@ rtorrent_Test_Common = \
helpers/progress_listener.cc \
helpers/progress_listener.h \
helpers/protectors.cc \
helpers/protectors.h
helpers/protectors.h \
helpers/utils.h
rtorrent_Test_Rpc_SOURCES = $(rtorrent_Test_Common) \
rpc/command_test.cc \