2007-05-20 13:57:56 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-05-20 13:57:56 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "MultiUrlRequestInfo.h"
|
2008-11-03 08:18:58 +00:00
|
|
|
|
2013-03-17 06:42:16 +00:00
|
|
|
#include <signal.h>
|
2008-11-03 08:18:58 +00:00
|
|
|
|
2011-02-08 14:05:40 +00:00
|
|
|
#include <cstring>
|
2008-11-03 08:18:58 +00:00
|
|
|
#include <ostream>
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "RequestGroupMan.h"
|
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "RequestGroup.h"
|
2007-05-20 13:57:56 +00:00
|
|
|
#include "prefs.h"
|
|
|
|
#include "DownloadEngineFactory.h"
|
|
|
|
#include "RecoverableException.h"
|
|
|
|
#include "message.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-04-22 10:29:48 +00:00
|
|
|
#include "Option.h"
|
2013-07-06 10:26:30 +00:00
|
|
|
#include "ConsoleStatCalc.h"
|
|
|
|
#include "NullStatCalc.h"
|
2008-09-01 13:46:03 +00:00
|
|
|
#include "CookieStorage.h"
|
|
|
|
#include "File.h"
|
2008-11-03 10:06:25 +00:00
|
|
|
#include "Netrc.h"
|
|
|
|
#include "AuthConfigFactory.h"
|
2010-04-08 12:54:14 +00:00
|
|
|
#include "SessionSerializer.h"
|
2010-10-09 14:22:49 +00:00
|
|
|
#include "TimeA2.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2011-03-17 07:08:39 +00:00
|
|
|
#include "SocketCore.h"
|
2013-07-06 10:26:30 +00:00
|
|
|
#include "NullOutputFile.h"
|
2012-02-05 09:57:16 +00:00
|
|
|
#include "UriListParser.h"
|
2012-03-20 12:42:09 +00:00
|
|
|
#include "SingletonHolder.h"
|
|
|
|
#include "Notifier.h"
|
2013-07-06 10:26:30 +00:00
|
|
|
#include "console.h"
|
2012-04-07 15:50:25 +00:00
|
|
|
#ifdef ENABLE_WEBSOCKET
|
|
|
|
# include "WebSocketSessionMan.h"
|
|
|
|
#else // !ENABLE_WEBSOCKET
|
|
|
|
# include "NullWebSocketSessionMan.h"
|
|
|
|
#endif // !ENABLE_WEBSOCKET
|
2008-11-08 13:21:32 +00:00
|
|
|
#ifdef ENABLE_SSL
|
|
|
|
# include "TLSContext.h"
|
|
|
|
#endif // ENABLE_SSL
|
2012-05-16 14:02:55 +00:00
|
|
|
#ifdef ENABLE_ASYNC_DNS
|
|
|
|
#include "AsyncNameResolver.h"
|
|
|
|
#endif // ENABLE_ASYNC_DNS
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2007-05-20 13:57:56 +00:00
|
|
|
|
2010-03-06 08:29:53 +00:00
|
|
|
namespace global {
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
extern volatile sig_atomic_t globalHaltRequested;
|
2007-05-20 13:57:56 +00:00
|
|
|
|
2010-03-06 08:29:53 +00:00
|
|
|
} // namespace global
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2013-08-17 21:04:15 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
static const DWORD mainThread = GetCurrentThreadId();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void handler(int signal)
|
|
|
|
{
|
2010-06-03 11:52:44 +00:00
|
|
|
if(
|
|
|
|
#ifdef SIGHUP
|
|
|
|
signal == SIGHUP ||
|
|
|
|
#endif // SIGHUP
|
|
|
|
signal == SIGTERM) {
|
2013-08-17 21:04:15 +00:00
|
|
|
if(global::globalHaltRequested <= 2) {
|
2010-06-03 11:52:44 +00:00
|
|
|
global::globalHaltRequested = 3;
|
|
|
|
}
|
2013-08-17 21:04:15 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (::GetCurrentThreadId() != mainThread) {
|
|
|
|
// SIGTERM may arrive on another thread (via SetConsoleCtrlHandler), and
|
|
|
|
// the process will be forcefully terminated as soon as that thread is
|
|
|
|
// done. So better make sure it isn't done prematurely. ;)
|
|
|
|
while (global::globalHaltRequested != 5) {
|
|
|
|
::Sleep(100); // Yeah, semi-busy waiting for now.
|
|
|
|
}
|
2010-06-03 11:52:44 +00:00
|
|
|
}
|
2013-08-17 21:04:15 +00:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SIGINT
|
|
|
|
|
|
|
|
if (global::globalHaltRequested == 0) {
|
|
|
|
global::globalHaltRequested = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (global::globalHaltRequested == 2) {
|
|
|
|
global::globalHaltRequested = 3;
|
|
|
|
return;
|
2007-12-07 13:33:59 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2013-07-06 10:26:30 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::unique_ptr<StatCalc> getStatCalc(const std::shared_ptr<Option>& op)
|
|
|
|
{
|
|
|
|
if(op->getAsBool(PREF_QUIET)) {
|
|
|
|
return make_unique<NullStatCalc>();
|
|
|
|
}
|
2013-08-21 04:06:10 +00:00
|
|
|
auto impl = make_unique<ConsoleStatCalc>(op->getAsInt(PREF_SUMMARY_INTERVAL),
|
|
|
|
op->getAsBool(PREF_HUMAN_READABLE));
|
|
|
|
impl->setReadoutVisibility(op->getAsBool(PREF_SHOW_CONSOLE_READOUT));
|
|
|
|
impl->setTruncate(op->getAsBool(PREF_TRUNCATE_CONSOLE_READOUT));
|
|
|
|
return std::move(impl);
|
2013-07-06 10:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2008-04-22 10:29:48 +00:00
|
|
|
MultiUrlRequestInfo::MultiUrlRequestInfo
|
2013-06-23 07:35:14 +00:00
|
|
|
(std::vector<std::shared_ptr<RequestGroup> > requestGroups,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<Option>& op,
|
|
|
|
const std::shared_ptr<UriListParser>& uriListParser)
|
2013-06-23 07:35:14 +00:00
|
|
|
: requestGroups_(std::move(requestGroups)),
|
|
|
|
option_(op),
|
2013-05-04 14:56:19 +00:00
|
|
|
uriListParser_(uriListParser),
|
|
|
|
useSignalHandler_(true)
|
2013-04-25 12:46:31 +00:00
|
|
|
{
|
2013-05-22 15:45:50 +00:00
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
sigemptyset(&mask_);
|
|
|
|
#else // !HAVE_SIGACTION
|
|
|
|
mask_ = 0;
|
|
|
|
#endif // !HAVE_SIGACTION
|
2013-04-25 12:46:31 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
|
|
|
|
|
2007-10-13 15:47:22 +00:00
|
|
|
void MultiUrlRequestInfo::printMessageForContinue()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2013-07-06 10:26:30 +00:00
|
|
|
if(!option_->getAsBool(PREF_QUIET)) {
|
|
|
|
global::cout()->printf
|
|
|
|
("\n%s\n%s\n",
|
|
|
|
_("aria2 will resume download if the transfer is restarted."),
|
|
|
|
_("If there are any errors, then see the log file. See '-l' option in help/man page for details."));
|
|
|
|
}
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 14:55:51 +00:00
|
|
|
int MultiUrlRequestInfo::prepare()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2013-05-05 08:47:14 +00:00
|
|
|
global::globalHaltRequested = 0;
|
2007-05-20 13:57:56 +00:00
|
|
|
try {
|
2013-06-22 10:36:42 +00:00
|
|
|
SingletonHolder<Notifier>::instance(make_unique<Notifier>());
|
2012-03-20 12:42:09 +00:00
|
|
|
|
2012-09-29 15:50:14 +00:00
|
|
|
#ifdef ENABLE_SSL
|
|
|
|
if(option_->getAsBool(PREF_ENABLE_RPC) &&
|
|
|
|
option_->getAsBool(PREF_RPC_SECURE)) {
|
2013-09-22 10:14:51 +00:00
|
|
|
if(option_->blank(PREF_RPC_CERTIFICATE)) {
|
2012-09-29 15:50:14 +00:00
|
|
|
throw DL_ABORT_EX("Specify --rpc-certificate and --rpc-private-key "
|
|
|
|
"options in order to use secure RPC.");
|
|
|
|
}
|
2013-08-21 04:06:10 +00:00
|
|
|
// We set server TLS context to the SocketCore before creating
|
|
|
|
// DownloadEngine instance.
|
|
|
|
std::shared_ptr<TLSContext> svTlsContext(TLSContext::make(TLS_SERVER));
|
2013-09-25 15:55:18 +00:00
|
|
|
if(!svTlsContext->addCredentialFile
|
|
|
|
(option_->get(PREF_RPC_CERTIFICATE),
|
|
|
|
option_->get(PREF_RPC_PRIVATE_KEY))) {
|
|
|
|
throw DL_ABORT_EX("Loading private key and/or certificate for secure "
|
|
|
|
"RPC failed.");
|
|
|
|
}
|
2013-08-21 04:06:10 +00:00
|
|
|
SocketCore::setServerTLSContext(svTlsContext);
|
2012-09-29 15:50:14 +00:00
|
|
|
}
|
|
|
|
#endif // ENABLE_SSL
|
|
|
|
|
2013-06-23 07:35:14 +00:00
|
|
|
// RequestGroups will be transferred to DownloadEngine
|
2013-04-25 12:46:31 +00:00
|
|
|
e_ = DownloadEngineFactory().newDownloadEngine(option_.get(),
|
2013-06-23 07:35:14 +00:00
|
|
|
std::move(requestGroups_));
|
2007-05-20 13:57:56 +00:00
|
|
|
|
2013-05-25 13:38:57 +00:00
|
|
|
#ifdef ENABLE_WEBSOCKET
|
2013-05-11 02:04:47 +00:00
|
|
|
if(option_->getAsBool(PREF_ENABLE_RPC)) {
|
2013-07-06 09:33:59 +00:00
|
|
|
e_->setWebSocketSessionMan(make_unique<rpc::WebSocketSessionMan>());
|
2013-05-11 02:04:47 +00:00
|
|
|
SingletonHolder<Notifier>::instance()->addDownloadEventListener
|
2013-07-06 09:33:59 +00:00
|
|
|
(e_->getWebSocketSessionMan().get());
|
2013-05-11 02:04:47 +00:00
|
|
|
}
|
2013-05-25 13:38:57 +00:00
|
|
|
#endif // ENABLE_WEBSOCKET
|
2013-05-11 02:04:47 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
if(!option_->blank(PREF_LOAD_COOKIES)) {
|
|
|
|
File cookieFile(option_->get(PREF_LOAD_COOKIES));
|
2011-05-12 00:39:03 +00:00
|
|
|
if(cookieFile.isFile() &&
|
2013-04-25 12:46:31 +00:00
|
|
|
e_->getCookieStorage()->load(cookieFile.getPath(),
|
|
|
|
Time().getTime())) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO(fmt("Loaded cookies from '%s'.",
|
|
|
|
cookieFile.getPath().c_str()));
|
2009-06-21 10:26:14 +00:00
|
|
|
} else {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_ERROR(fmt(MSG_LOADING_COOKIE_FAILED,
|
|
|
|
cookieFile.getPath().c_str()));
|
2008-09-01 13:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 13:06:29 +00:00
|
|
|
auto authConfigFactory = make_unique<AuthConfigFactory>();
|
2010-06-21 13:51:56 +00:00
|
|
|
File netrccf(option_->get(PREF_NETRC_PATH));
|
|
|
|
if(!option_->getAsBool(PREF_NO_NETRC) && netrccf.isFile()) {
|
2011-01-17 13:36:19 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
// Windows OS does not have permission, so set it to 0.
|
|
|
|
mode_t mode = 0;
|
|
|
|
#else // !__MINGW32__
|
2008-11-03 10:06:25 +00:00
|
|
|
mode_t mode = netrccf.mode();
|
2011-01-17 13:36:19 +00:00
|
|
|
#endif // !__MINGW32__
|
2008-11-03 10:06:25 +00:00
|
|
|
if(mode&(S_IRWXG|S_IRWXO)) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_NOTICE(fmt(MSG_INCORRECT_NETRC_PERMISSION,
|
|
|
|
option_->get(PREF_NETRC_PATH).c_str()));
|
2008-11-03 10:06:25 +00:00
|
|
|
} else {
|
2013-06-26 12:45:29 +00:00
|
|
|
auto netrc = make_unique<Netrc>();
|
2010-06-21 13:51:56 +00:00
|
|
|
netrc->parse(option_->get(PREF_NETRC_PATH));
|
2013-06-26 12:45:29 +00:00
|
|
|
authConfigFactory->setNetrc(std::move(netrc));
|
2008-11-03 10:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-25 13:06:29 +00:00
|
|
|
e_->setAuthConfigFactory(std::move(authConfigFactory));
|
2008-11-03 10:06:25 +00:00
|
|
|
|
2008-11-08 13:21:32 +00:00
|
|
|
#ifdef ENABLE_SSL
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<TLSContext> clTlsContext(TLSContext::make(TLS_CLIENT));
|
2010-06-21 13:51:56 +00:00
|
|
|
if(!option_->blank(PREF_CERTIFICATE) &&
|
|
|
|
!option_->blank(PREF_PRIVATE_KEY)) {
|
2012-09-29 15:50:14 +00:00
|
|
|
clTlsContext->addCredentialFile(option_->get(PREF_CERTIFICATE),
|
|
|
|
option_->get(PREF_PRIVATE_KEY));
|
2008-11-08 13:21:32 +00:00
|
|
|
}
|
2012-07-05 14:22:34 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
if(!option_->blank(PREF_CA_CERTIFICATE)) {
|
2012-09-29 15:50:14 +00:00
|
|
|
if(!clTlsContext->addTrustedCACertFile
|
|
|
|
(option_->get(PREF_CA_CERTIFICATE))) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO(MSG_WARN_NO_CA_CERT);
|
2010-10-03 14:13:03 +00:00
|
|
|
}
|
|
|
|
} else if(option_->getAsBool(PREF_CHECK_CERTIFICATE)) {
|
2012-09-29 15:50:14 +00:00
|
|
|
if(!clTlsContext->addSystemTrustedCACerts()) {
|
2012-07-05 14:22:34 +00:00
|
|
|
A2_LOG_INFO(MSG_WARN_NO_CA_CERT);
|
|
|
|
}
|
2008-11-09 07:36:44 +00:00
|
|
|
}
|
2013-04-05 03:55:57 +00:00
|
|
|
clTlsContext->setVerifyPeer(option_->getAsBool(PREF_CHECK_CERTIFICATE));
|
2012-09-29 15:50:14 +00:00
|
|
|
SocketCore::setClientTLSContext(clTlsContext);
|
2008-11-08 13:21:32 +00:00
|
|
|
#endif
|
2011-02-17 16:04:11 +00:00
|
|
|
#ifdef HAVE_ARES_ADDR_NODE
|
2011-02-08 14:05:40 +00:00
|
|
|
ares_addr_node* asyncDNSServers =
|
|
|
|
parseAsyncDNSServers(option_->get(PREF_ASYNC_DNS_SERVER));
|
2013-04-25 12:46:31 +00:00
|
|
|
e_->setAsyncDNSServers(asyncDNSServers);
|
2011-02-17 16:04:11 +00:00
|
|
|
#endif // HAVE_ARES_ADDR_NODE
|
2010-04-12 13:05:41 +00:00
|
|
|
if(!Timer::monotonicClock()) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_WARN("Don't change system time while aria2c is running."
|
|
|
|
" Doing this may make aria2c hang for long time.");
|
2010-04-12 13:05:41 +00:00
|
|
|
}
|
2008-11-08 13:21:32 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
std::string serverStatIf = option_->get(PREF_SERVER_STAT_IF);
|
2008-08-10 15:22:55 +00:00
|
|
|
if(!serverStatIf.empty()) {
|
2013-04-25 12:46:31 +00:00
|
|
|
e_->getRequestGroupMan()->loadServerStat(serverStatIf);
|
|
|
|
e_->getRequestGroupMan()->removeStaleServerStat
|
2010-06-21 13:51:56 +00:00
|
|
|
(option_->getAsInt(PREF_SERVER_STAT_TIMEOUT));
|
2008-08-10 15:22:55 +00:00
|
|
|
}
|
2013-07-06 10:26:30 +00:00
|
|
|
e_->setStatCalc(getStatCalc(option_));
|
2012-02-05 09:57:16 +00:00
|
|
|
if(uriListParser_) {
|
2013-04-25 12:46:31 +00:00
|
|
|
e_->getRequestGroupMan()->setUriListParser(uriListParser_);
|
2012-02-05 09:57:16 +00:00
|
|
|
}
|
2013-05-04 14:56:19 +00:00
|
|
|
if(useSignalHandler_) {
|
|
|
|
setupSignalHandlers();
|
|
|
|
}
|
2013-04-25 12:46:31 +00:00
|
|
|
e_->getRequestGroupMan()->getNetStat().downloadStart();
|
|
|
|
} catch(RecoverableException& e) {
|
2013-09-25 15:55:18 +00:00
|
|
|
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
|
2013-04-25 12:46:31 +00:00
|
|
|
SingletonHolder<Notifier>::clear();
|
2013-05-04 14:56:19 +00:00
|
|
|
if(useSignalHandler_) {
|
|
|
|
resetSignalHandlers();
|
|
|
|
}
|
2013-04-26 14:55:51 +00:00
|
|
|
return -1;
|
2013-04-25 12:46:31 +00:00
|
|
|
}
|
2013-04-26 14:55:51 +00:00
|
|
|
return 0;
|
2013-04-25 12:46:31 +00:00
|
|
|
}
|
2012-09-29 15:50:14 +00:00
|
|
|
|
2013-04-25 12:46:31 +00:00
|
|
|
error_code::Value MultiUrlRequestInfo::getResult()
|
|
|
|
{
|
|
|
|
error_code::Value returnValue = error_code::FINISHED;
|
|
|
|
if(!option_->blank(PREF_SAVE_COOKIES)) {
|
|
|
|
e_->getCookieStorage()->saveNsFormat(option_->get(PREF_SAVE_COOKIES));
|
|
|
|
}
|
2009-05-22 14:51:57 +00:00
|
|
|
|
2013-04-25 12:46:31 +00:00
|
|
|
const std::string& serverStatOf = option_->get(PREF_SERVER_STAT_OF);
|
|
|
|
if(!serverStatOf.empty()) {
|
|
|
|
e_->getRequestGroupMan()->saveServerStat(serverStatOf);
|
|
|
|
}
|
2013-07-06 10:26:30 +00:00
|
|
|
if(!option_->getAsBool(PREF_QUIET)) {
|
|
|
|
e_->getRequestGroupMan()->showDownloadResults
|
|
|
|
(*global::cout(), option_->get(PREF_DOWNLOAD_RESULT) == A2_V_FULL);
|
|
|
|
global::cout()->flush();
|
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2013-04-25 12:46:31 +00:00
|
|
|
RequestGroupMan::DownloadStat s =
|
|
|
|
e_->getRequestGroupMan()->getDownloadStat();
|
|
|
|
if(!s.allCompleted()) {
|
|
|
|
printMessageForContinue();
|
|
|
|
if(s.getLastErrorResult() == error_code::FINISHED &&
|
|
|
|
s.getInProgress() > 0) {
|
|
|
|
returnValue = error_code::IN_PROGRESS;
|
|
|
|
} else {
|
|
|
|
returnValue = s.getLastErrorResult();
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
2013-04-25 12:46:31 +00:00
|
|
|
}
|
2013-07-05 15:59:43 +00:00
|
|
|
SessionSerializer sessionSerializer{e_->getRequestGroupMan().get()};
|
2013-04-25 12:46:31 +00:00
|
|
|
// TODO Add option: --save-session-status=error,inprogress,waiting
|
|
|
|
if(!option_->blank(PREF_SAVE_SESSION)) {
|
|
|
|
const std::string& filename = option_->get(PREF_SAVE_SESSION);
|
|
|
|
if(sessionSerializer.save(filename)) {
|
|
|
|
A2_LOG_NOTICE(fmt(_("Serialized session to '%s' successfully."),
|
|
|
|
filename.c_str()));
|
|
|
|
} else {
|
|
|
|
A2_LOG_NOTICE(fmt(_("Failed to serialize session to '%s'."),
|
|
|
|
filename.c_str()));
|
2010-04-08 12:54:14 +00:00
|
|
|
}
|
2013-04-25 12:46:31 +00:00
|
|
|
}
|
|
|
|
SingletonHolder<Notifier>::clear();
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_code::Value MultiUrlRequestInfo::execute()
|
|
|
|
{
|
2013-04-26 14:55:51 +00:00
|
|
|
if(prepare() != 0) {
|
|
|
|
return error_code::UNKNOWN_ERROR;
|
2013-04-25 12:46:31 +00:00
|
|
|
}
|
2013-04-26 14:55:51 +00:00
|
|
|
// TODO Enclosed in try..catch block for just in case. Really need
|
|
|
|
// this?
|
2013-04-25 12:46:31 +00:00
|
|
|
try {
|
|
|
|
e_->run();
|
2008-11-03 10:06:25 +00:00
|
|
|
} catch(RecoverableException& e) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
2013-04-26 14:55:51 +00:00
|
|
|
error_code::Value returnValue = getResult();
|
2013-05-04 14:56:19 +00:00
|
|
|
if(useSignalHandler_) {
|
|
|
|
resetSignalHandlers();
|
|
|
|
}
|
2013-04-25 12:46:31 +00:00
|
|
|
return returnValue;
|
|
|
|
}
|
2013-02-14 12:51:30 +00:00
|
|
|
|
2013-05-04 14:56:19 +00:00
|
|
|
void MultiUrlRequestInfo::setupSignalHandlers()
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
sigemptyset(&mask_);
|
|
|
|
#else // !HAVE_SIGACTION
|
|
|
|
mask_ = 0;
|
|
|
|
#endif // !HAVE_SIGACTION
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
util::setGlobalSignalHandler(SIGPIPE, &mask_, SIG_IGN, 0);
|
|
|
|
#endif // SIGPIPE
|
|
|
|
#ifdef SIGCHLD
|
|
|
|
// Avoid to create zombie process when forked child processes are
|
|
|
|
// died.
|
|
|
|
util::setGlobalSignalHandler(SIGCHLD, &mask_, SIG_IGN, 0);
|
|
|
|
#endif // SIGCHILD
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGACTION
|
|
|
|
sigaddset(&mask_, SIGINT);
|
|
|
|
sigaddset(&mask_, SIGTERM);
|
|
|
|
# ifdef SIGHUP
|
|
|
|
sigaddset(&mask_, SIGHUP);
|
|
|
|
# endif // SIGHUP
|
|
|
|
#endif // HAVE_SIGACTION
|
|
|
|
|
|
|
|
#ifdef SIGHUP
|
|
|
|
util::setGlobalSignalHandler(SIGHUP, &mask_, handler, 0);
|
|
|
|
#endif // SIGHUP
|
|
|
|
util::setGlobalSignalHandler(SIGINT, &mask_, handler, 0);
|
|
|
|
util::setGlobalSignalHandler(SIGTERM, &mask_, handler, 0);
|
|
|
|
}
|
|
|
|
|
2013-04-25 12:46:31 +00:00
|
|
|
void MultiUrlRequestInfo::resetSignalHandlers()
|
|
|
|
{
|
2013-02-14 12:51:30 +00:00
|
|
|
#ifdef HAVE_SIGACTION
|
2013-04-25 12:46:31 +00:00
|
|
|
sigemptyset(&mask_);
|
2013-02-14 12:51:30 +00:00
|
|
|
#endif // HAVE_SIGACTION
|
2010-01-09 13:06:45 +00:00
|
|
|
#ifdef SIGHUP
|
2013-04-25 12:46:31 +00:00
|
|
|
util::setGlobalSignalHandler(SIGHUP, &mask_, SIG_DFL, 0);
|
2010-01-09 13:06:45 +00:00
|
|
|
#endif // SIGHUP
|
2013-04-25 12:46:31 +00:00
|
|
|
util::setGlobalSignalHandler(SIGINT, &mask_, SIG_DFL, 0);
|
|
|
|
util::setGlobalSignalHandler(SIGTERM, &mask_, SIG_DFL, 0);
|
2013-05-04 14:56:19 +00:00
|
|
|
|
|
|
|
#ifdef SIGCHLD
|
|
|
|
util::setGlobalSignalHandler(SIGCHLD, &mask_, SIG_DFL, 0);
|
|
|
|
#endif // SIGCHILD
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
util::setGlobalSignalHandler(SIGPIPE, &mask_, SIG_DFL, 0);
|
|
|
|
#endif // SIGPIPE
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2013-07-06 10:45:01 +00:00
|
|
|
const std::unique_ptr<DownloadEngine>&
|
2013-04-26 14:55:51 +00:00
|
|
|
MultiUrlRequestInfo::getDownloadEngine() const
|
|
|
|
{
|
|
|
|
return e_;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|