From 64d8db0c93485efb30daf6e72fc3ae2add58990c Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 1 May 2022 12:32:51 +0800 Subject: [PATCH] Move system signal related functions to its own file And revise "signal safe print" function. PR #16953. --- src/app/CMakeLists.txt | 2 + src/app/app.pri | 2 + src/app/main.cpp | 125 ++++----------------------------- src/app/signalhandler.cpp | 141 ++++++++++++++++++++++++++++++++++++++ src/app/signalhandler.h | 33 +++++++++ 5 files changed, 193 insertions(+), 110 deletions(-) create mode 100644 src/app/signalhandler.cpp create mode 100644 src/app/signalhandler.h diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 93e5ea4e9..cd0e5e761 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -28,6 +28,7 @@ target_sources(qbt_app PRIVATE cmdoptions.h filelogger.h qtlocalpeer/qtlocalpeer.h + signalhandler.h upgrade.h # sources @@ -37,6 +38,7 @@ target_sources(qbt_app PRIVATE filelogger.cpp main.cpp qtlocalpeer/qtlocalpeer.cpp + signalhandler.cpp upgrade.cpp # resources diff --git a/src/app/app.pri b/src/app/app.pri index 14db0a17d..94956d11d 100644 --- a/src/app/app.pri +++ b/src/app/app.pri @@ -6,6 +6,7 @@ HEADERS += \ $$PWD/cmdoptions.h \ $$PWD/filelogger.h \ $$PWD/qtlocalpeer/qtlocalpeer.h \ + $$PWD/signalhandler.h \ $$PWD/upgrade.h SOURCES += \ @@ -15,6 +16,7 @@ SOURCES += \ $$PWD/filelogger.cpp \ $$PWD/main.cpp \ $$PWD/qtlocalpeer/qtlocalpeer.cpp \ + $$PWD/signalhandler.cpp \ $$PWD/upgrade.cpp stacktrace { diff --git a/src/app/main.cpp b/src/app/main.cpp index 5ab06e28b..f0579f0ee 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -29,17 +29,18 @@ #include -#include #include #include -#include -#if defined(Q_OS_UNIX) +#ifdef Q_OS_UNIX #include #endif -#if !defined Q_OS_WIN && !defined Q_OS_HAIKU + +#ifndef Q_OS_WIN +#ifndef Q_OS_HAIKU #include -#elif defined Q_OS_WIN && defined DISABLE_GUI +#endif // Q_OS_HAIKU +#elif defined DISABLE_GUI #include #endif @@ -61,75 +62,39 @@ Q_IMPORT_PLUGIN(QICOPlugin) #endif // QBT_STATIC_QT -#else -// NoGUI-only includes +#else // DISABLE_GUI #include #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/preferences.h" #include "base/profile.h" #include "base/version.h" #include "application.h" #include "cmdoptions.h" +#include "signalhandler.h" #include "upgrade.h" #ifndef DISABLE_GUI #include "gui/utils.h" #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(); bool userAgreesWithLegalNotice(); void displayBadArgMessage(const QString &message); -#if !defined(DISABLE_GUI) +#ifndef DISABLE_GUI void showSplashScreen(); #endif // DISABLE_GUI -#if defined(Q_OS_UNIX) +#ifdef Q_OS_UNIX void adjustFileDescriptorLimit(); #endif // Main int main(int argc, char *argv[]) { -#if defined(Q_OS_UNIX) +#ifdef Q_OS_UNIX adjustFileDescriptorLimit(); #endif @@ -219,7 +184,7 @@ int main(int argc, char *argv[]) return EXIT_SUCCESS; } -#if defined(Q_OS_WIN) +#ifdef Q_OS_WIN // This affects only Windows apparently and Qt5. // When QNetworkAccessManager is instantiated it regularly starts polling // the network interfaces to see what's available and their status. @@ -238,7 +203,7 @@ int main(int argc, char *argv[]) #endif #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. // Users are supposed to install Homebrew Python for search function. // For more info see issue #5571. @@ -297,12 +262,7 @@ int main(int argc, char *argv[]) showSplashScreen(); #endif - signal(SIGINT, sigNormalHandler); - signal(SIGTERM, sigNormalHandler); -#ifdef STACKTRACE - signal(SIGABRT, sigAbnormalHandler); - signal(SIGSEGV, sigAbnormalHandler); -#endif + registerSignalHandlers(); 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(strLen)) - std::ignore = _write(_fileno(stdout), str, strLen); -#else - if (write(STDERR_FILENO, str, strLen) < static_cast(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) void showSplashScreen() { @@ -445,7 +350,7 @@ bool userAgreesWithLegalNotice() return false; } -#if defined(Q_OS_UNIX) +#ifdef Q_OS_UNIX void adjustFileDescriptorLimit() { rlimit limit {}; diff --git a/src/app/signalhandler.cpp b/src/app/signalhandler.cpp new file mode 100644 index 000000000..939b88514 --- /dev/null +++ b/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 + * Copyright (C) 2006 Christophe Dumez + * + * 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 + +#include +#include +#include + +#ifndef Q_OS_WIN +#ifndef Q_OS_HAIKU +#include +#endif +#elif defined DISABLE_GUI +#include +#endif + +#include + +#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(strLen)) + std::ignore = _write(_fileno(stdout), str, strLen); +#else + if (write(STDERR_FILENO, str, strLen) < static_cast(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 +} diff --git a/src/app/signalhandler.h b/src/app/signalhandler.h new file mode 100644 index 000000000..e5d6e0b91 --- /dev/null +++ b/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 + * Copyright (C) 2006 Christophe Dumez + * + * 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();