Move system signal related functions to its own file

And revise "signal safe print" function.
PR #16953.
This commit is contained in:
Chocobo1 2022-05-01 12:32:51 +08:00 committed by GitHub
parent 7b608fd0c9
commit 64d8db0c93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 193 additions and 110 deletions

View file

@ -29,17 +29,18 @@
#include <QtGlobal>
#include <csignal>
#include <cstdlib>
#include <memory>
#include <tuple>
#if defined(Q_OS_UNIX)
#ifdef Q_OS_UNIX
#include <sys/resource.h>
#endif
#if !defined Q_OS_WIN && !defined Q_OS_HAIKU
#ifndef Q_OS_WIN
#ifndef Q_OS_HAIKU
#include <unistd.h>
#elif defined Q_OS_WIN && defined DISABLE_GUI
#endif // Q_OS_HAIKU
#elif defined DISABLE_GUI
#include <io.h>
#endif
@ -61,75 +62,39 @@
Q_IMPORT_PLUGIN(QICOPlugin)
#endif // QBT_STATIC_QT
#else
// NoGUI-only includes
#else // DISABLE_GUI
#include <cstdio>
#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<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)
void showSplashScreen()
{
@ -445,7 +350,7 @@ bool userAgreesWithLegalNotice()
return false;
}
#if defined(Q_OS_UNIX)
#ifdef Q_OS_UNIX
void adjustFileDescriptorLimit()
{
rlimit limit {};