Browse Source

Move system signal related functions to its own file

And revise "signal safe print" function.
PR #16953.
adaptive-webui-19844
Chocobo1 3 years ago committed by GitHub
parent
commit
64d8db0c93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/app/CMakeLists.txt
  2. 2
      src/app/app.pri
  3. 125
      src/app/main.cpp
  4. 141
      src/app/signalhandler.cpp
  5. 33
      src/app/signalhandler.h

2
src/app/CMakeLists.txt

@ -28,6 +28,7 @@ target_sources(qbt_app PRIVATE
cmdoptions.h cmdoptions.h
filelogger.h filelogger.h
qtlocalpeer/qtlocalpeer.h qtlocalpeer/qtlocalpeer.h
signalhandler.h
upgrade.h upgrade.h
# sources # sources
@ -37,6 +38,7 @@ target_sources(qbt_app PRIVATE
filelogger.cpp filelogger.cpp
main.cpp main.cpp
qtlocalpeer/qtlocalpeer.cpp qtlocalpeer/qtlocalpeer.cpp
signalhandler.cpp
upgrade.cpp upgrade.cpp
# resources # resources

2
src/app/app.pri

@ -6,6 +6,7 @@ HEADERS += \
$$PWD/cmdoptions.h \ $$PWD/cmdoptions.h \
$$PWD/filelogger.h \ $$PWD/filelogger.h \
$$PWD/qtlocalpeer/qtlocalpeer.h \ $$PWD/qtlocalpeer/qtlocalpeer.h \
$$PWD/signalhandler.h \
$$PWD/upgrade.h $$PWD/upgrade.h
SOURCES += \ SOURCES += \
@ -15,6 +16,7 @@ SOURCES += \
$$PWD/filelogger.cpp \ $$PWD/filelogger.cpp \
$$PWD/main.cpp \ $$PWD/main.cpp \
$$PWD/qtlocalpeer/qtlocalpeer.cpp \ $$PWD/qtlocalpeer/qtlocalpeer.cpp \
$$PWD/signalhandler.cpp \
$$PWD/upgrade.cpp $$PWD/upgrade.cpp
stacktrace { stacktrace {

125
src/app/main.cpp

@ -29,17 +29,18 @@
#include <QtGlobal> #include <QtGlobal>
#include <csignal>
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
#include <tuple>
#if defined(Q_OS_UNIX) #ifdef Q_OS_UNIX
#include <sys/resource.h> #include <sys/resource.h>
#endif #endif
#if !defined Q_OS_WIN && !defined Q_OS_HAIKU
#ifndef Q_OS_WIN
#ifndef Q_OS_HAIKU
#include <unistd.h> #include <unistd.h>
#elif defined Q_OS_WIN && defined DISABLE_GUI #endif // Q_OS_HAIKU
#elif defined DISABLE_GUI
#include <io.h> #include <io.h>
#endif #endif
@ -61,75 +62,39 @@
Q_IMPORT_PLUGIN(QICOPlugin) Q_IMPORT_PLUGIN(QICOPlugin)
#endif // QBT_STATIC_QT #endif // QBT_STATIC_QT
#else #else // DISABLE_GUI
// NoGUI-only includes
#include <cstdio> #include <cstdio>
#endif // DISABLE_GUI #endif // DISABLE_GUI
#ifdef STACKTRACE
#ifdef Q_OS_UNIX
#include "stacktrace.h"
#else
#include "stacktrace_win.h"
#ifndef DISABLE_GUI
#include "stacktracedialog.h"
#endif // DISABLE_GUI
#endif // Q_OS_UNIX
#endif //STACKTRACE
#include "base/global.h" #include "base/global.h"
#include "base/preferences.h" #include "base/preferences.h"
#include "base/profile.h" #include "base/profile.h"
#include "base/version.h" #include "base/version.h"
#include "application.h" #include "application.h"
#include "cmdoptions.h" #include "cmdoptions.h"
#include "signalhandler.h"
#include "upgrade.h" #include "upgrade.h"
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
#include "gui/utils.h" #include "gui/utils.h"
#endif #endif
// Signal handlers
void sigNormalHandler(int signum);
#ifdef STACKTRACE
void sigAbnormalHandler(int signum);
#endif
// sys_signame[] is only defined in BSD
const char *const sysSigName[] =
{
#if defined(Q_OS_WIN)
"", "", "SIGINT", "", "SIGILL", "", "SIGABRT_COMPAT", "", "SIGFPE", "",
"", "SIGSEGV", "", "", "", "SIGTERM", "", "", "", "",
"", "SIGBREAK", "SIGABRT", "", "", "", "", "", "", "",
"", ""
#else
"", "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL",
"SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP",
"SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGIO",
"SIGPWR", "SIGUNUSED"
#endif
};
#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU
void reportToUser(const char *str);
#endif
void displayVersion(); void displayVersion();
bool userAgreesWithLegalNotice(); bool userAgreesWithLegalNotice();
void displayBadArgMessage(const QString &message); void displayBadArgMessage(const QString &message);
#if !defined(DISABLE_GUI) #ifndef DISABLE_GUI
void showSplashScreen(); void showSplashScreen();
#endif // DISABLE_GUI #endif // DISABLE_GUI
#if defined(Q_OS_UNIX) #ifdef Q_OS_UNIX
void adjustFileDescriptorLimit(); void adjustFileDescriptorLimit();
#endif #endif
// Main // Main
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
#if defined(Q_OS_UNIX) #ifdef Q_OS_UNIX
adjustFileDescriptorLimit(); adjustFileDescriptorLimit();
#endif #endif
@ -219,7 +184,7 @@ int main(int argc, char *argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
#if defined(Q_OS_WIN) #ifdef Q_OS_WIN
// This affects only Windows apparently and Qt5. // This affects only Windows apparently and Qt5.
// When QNetworkAccessManager is instantiated it regularly starts polling // When QNetworkAccessManager is instantiated it regularly starts polling
// the network interfaces to see what's available and their status. // the network interfaces to see what's available and their status.
@ -238,7 +203,7 @@ int main(int argc, char *argv[])
#endif #endif
#endif // Q_OS_WIN #endif // Q_OS_WIN
#if defined(Q_OS_MACOS) #ifdef Q_OS_MACOS
// Since Apple made difficult for users to set PATH, we set here for convenience. // Since Apple made difficult for users to set PATH, we set here for convenience.
// Users are supposed to install Homebrew Python for search function. // Users are supposed to install Homebrew Python for search function.
// For more info see issue #5571. // For more info see issue #5571.
@ -297,12 +262,7 @@ int main(int argc, char *argv[])
showSplashScreen(); showSplashScreen();
#endif #endif
signal(SIGINT, sigNormalHandler); registerSignalHandlers();
signal(SIGTERM, sigNormalHandler);
#ifdef STACKTRACE
signal(SIGABRT, sigAbnormalHandler);
signal(SIGSEGV, sigAbnormalHandler);
#endif
return app->exec(params.paramList()); return app->exec(params.paramList());
} }
@ -313,61 +273,6 @@ int main(int argc, char *argv[])
} }
} }
#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU
void reportToUser(const char *str)
{
const size_t strLen = strlen(str);
#ifdef Q_OS_WIN
if (_write(_fileno(stderr), str, strLen) < static_cast<int>(strLen))
std::ignore = _write(_fileno(stdout), str, strLen);
#else
if (write(STDERR_FILENO, str, strLen) < static_cast<ssize_t>(strLen))
std::ignore = write(STDOUT_FILENO, str, strLen);
#endif
}
#endif
void sigNormalHandler(int signum)
{
#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU
const char msg1[] = "Catching signal: ";
const char msg2[] = "\nExiting cleanly\n";
reportToUser(msg1);
reportToUser(sysSigName[signum]);
reportToUser(msg2);
#endif // !defined Q_OS_WIN && !defined Q_OS_HAIKU
signal(signum, SIG_DFL);
qApp->exit(); // unsafe, but exit anyway
}
#ifdef STACKTRACE
void sigAbnormalHandler(int signum)
{
const char *sigName = sysSigName[signum];
#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU
const char msg[] = "\n\n*************************************************************\n"
"Please file a bug report at http://bug.qbittorrent.org and provide the following information:\n\n"
"qBittorrent version: " QBT_VERSION "\n\n"
"Caught signal: ";
reportToUser(msg);
reportToUser(sigName);
reportToUser("\n");
#if !defined Q_OS_WIN
print_stacktrace(); // unsafe
#endif
#endif
#if defined Q_OS_WIN && !defined DISABLE_GUI
StacktraceDialog dlg; // unsafe
dlg.setStacktraceString(QString::fromLatin1(sigName), straceWin::getBacktrace());
dlg.exec();
#endif
signal(signum, SIG_DFL);
raise(signum);
}
#endif // STACKTRACE
#if !defined(DISABLE_GUI) #if !defined(DISABLE_GUI)
void showSplashScreen() void showSplashScreen()
{ {
@ -445,7 +350,7 @@ bool userAgreesWithLegalNotice()
return false; return false;
} }
#if defined(Q_OS_UNIX) #ifdef Q_OS_UNIX
void adjustFileDescriptorLimit() void adjustFileDescriptorLimit()
{ {
rlimit limit {}; rlimit limit {};

141
src/app/signalhandler.cpp

@ -0,0 +1,141 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Mike Tzou (Chocobo1)
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. 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), 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.
*/
#include "signalhandler.h"
#include <QtGlobal>
#include <algorithm>
#include <csignal>
#include <tuple>
#ifndef Q_OS_WIN
#ifndef Q_OS_HAIKU
#include <unistd.h>
#endif
#elif defined DISABLE_GUI
#include <io.h>
#endif
#include <QCoreApplication>
#include "base/version.h"
#ifdef STACKTRACE
#ifdef Q_OS_UNIX
#include "stacktrace.h"
#else
#include "stacktrace_win.h"
#ifndef DISABLE_GUI
#include "stacktracedialog.h"
#endif // DISABLE_GUI
#endif // Q_OS_UNIX
#endif //STACKTRACE
namespace
{
// sys_signame[] is only defined in BSD
const char *const sysSigName[] =
{
#ifdef Q_OS_WIN
"", "", "SIGINT", "", "SIGILL", "", "SIGABRT_COMPAT", "", "SIGFPE", "",
"", "SIGSEGV", "", "", "", "SIGTERM", "", "", "", "",
"", "SIGBREAK", "SIGABRT", "", "", "", "", "", "", "",
"", ""
#else
"", "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL",
"SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP",
"SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGIO",
"SIGPWR", "SIGUNUSED"
#endif
};
#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU
void safePrint(const char *str)
{
const size_t strLen = strlen(str);
#ifdef Q_OS_WIN
if (_write(_fileno(stderr), str, strLen) < static_cast<int>(strLen))
std::ignore = _write(_fileno(stdout), str, strLen);
#else
if (write(STDERR_FILENO, str, strLen) < static_cast<ssize_t>(strLen))
std::ignore = write(STDOUT_FILENO, str, strLen);
#endif
}
#endif
void normalExitHandler(const int signum)
{
#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU
const char *msgs[] = {"Catching signal: ", sysSigName[signum], "\nExiting cleanly\n"};
std::for_each(std::begin(msgs), std::end(msgs), safePrint);
#endif // !defined Q_OS_WIN && !defined Q_OS_HAIKU
signal(signum, SIG_DFL);
QCoreApplication::exit(); // unsafe, but exit anyway
}
#ifdef STACKTRACE
void abnormalExitHandler(const int signum)
{
const char *sigName = sysSigName[signum];
#if !(defined Q_OS_WIN && !defined DISABLE_GUI) && !defined Q_OS_HAIKU
const char msg[] = "\n\n*************************************************************\n"
"Please file a bug report at http://bug.qbittorrent.org and provide the following information:\n\n"
"qBittorrent version: " QBT_VERSION "\n\n"
"Caught signal: ";
const char *msgs[] = {msg, sigName, "\n"};
std::for_each(std::begin(msgs), std::end(msgs), safePrint);
#if !defined Q_OS_WIN
print_stacktrace(); // unsafe
#endif
#endif
#if defined Q_OS_WIN && !defined DISABLE_GUI
StacktraceDialog dlg; // unsafe
dlg.setStacktraceString(QString::fromLatin1(sigName), straceWin::getBacktrace());
dlg.exec();
#endif
signal(signum, SIG_DFL);
raise(signum);
}
#endif // STACKTRACE
}
void registerSignalHandlers()
{
signal(SIGINT, normalExitHandler);
signal(SIGTERM, normalExitHandler);
#ifdef STACKTRACE
signal(SIGABRT, abnormalExitHandler);
signal(SIGSEGV, abnormalExitHandler);
#endif
}

33
src/app/signalhandler.h

@ -0,0 +1,33 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Mike Tzou (Chocobo1)
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. 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), 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.
*/
#pragma once
void registerSignalHandlers();
Loading…
Cancel
Save