Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / app / signalhandler.cpp
blobbe70f254d02725667b18b43f4bf7d439634f41f3
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Mike Tzou (Chocobo1)
4 * Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
5 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
31 #include "signalhandler.h"
33 #include <QtGlobal>
35 #include <algorithm>
36 #include <csignal>
37 #include <tuple>
39 #ifdef Q_OS_UNIX
40 #include <unistd.h>
41 #elif defined Q_OS_WIN
42 #include <io.h>
43 #endif
45 #include <QCoreApplication>
46 #include <QMetaObject>
48 #include "base/version.h"
50 #ifdef STACKTRACE
51 #include "stacktrace.h"
53 #ifndef DISABLE_GUI
54 #include "gui/stacktracedialog.h"
55 #endif
56 #endif //STACKTRACE
58 namespace
60 // sys_signame[] is only defined in BSD
61 const char *const sysSigName[] =
63 #ifdef Q_OS_WIN
64 "", "", "SIGINT", "", "SIGILL", "", "SIGABRT_COMPAT", "", "SIGFPE", "",
65 "", "SIGSEGV", "", "", "", "SIGTERM", "", "", "", "",
66 "", "SIGBREAK", "SIGABRT", "", "", "", "", "", "", "",
67 "", ""
68 #else
69 "", "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL",
70 "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP",
71 "SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGIO",
72 "SIGPWR", "SIGUNUSED"
73 #endif
76 void safePrint(const char *str)
78 const size_t strLen = strlen(str);
79 #ifdef Q_OS_WIN
80 if (_write(_fileno(stderr), str, static_cast<unsigned int>(strLen)) < static_cast<int>(strLen))
81 std::ignore = _write(_fileno(stdout), str, static_cast<unsigned int>(strLen));
82 #else
83 if (write(STDERR_FILENO, str, strLen) < static_cast<ssize_t>(strLen))
84 std::ignore = write(STDOUT_FILENO, str, strLen);
85 #endif
88 void normalExitHandler(const int signum)
90 const char *msgs[] = {"Catching signal: ", sysSigName[signum], "\nExiting cleanly\n"};
91 std::for_each(std::begin(msgs), std::end(msgs), safePrint);
92 signal(signum, SIG_DFL);
93 QMetaObject::invokeMethod(qApp, [] { QCoreApplication::exit(); }, Qt::QueuedConnection); // unsafe, but exit anyway
96 #ifdef STACKTRACE
97 void abnormalExitHandler(const int signum)
99 const char msg[] = "\n\n*************************************************************\n"
100 "Please file a bug report at https://bug.qbittorrent.org and provide the following information:\n\n"
101 "qBittorrent version: " QBT_VERSION "\n\n"
102 "Caught signal: ";
103 const char *sigName = sysSigName[signum];
104 const std::string stacktrace = getStacktrace();
106 const char *msgs[] = {msg, sigName, "\n```\n", stacktrace.c_str(), "```\n\n"};
107 std::for_each(std::begin(msgs), std::end(msgs), safePrint);
109 #ifndef DISABLE_GUI
110 StacktraceDialog dlg; // unsafe
111 dlg.setText(QString::fromLatin1(sigName), QString::fromStdString(stacktrace));
112 dlg.exec();
113 #endif
115 signal(signum, SIG_DFL);
116 raise(signum);
118 #endif // STACKTRACE
121 void registerSignalHandlers()
123 signal(SIGINT, normalExitHandler);
124 signal(SIGTERM, normalExitHandler);
126 #ifdef STACKTRACE
127 signal(SIGABRT, abnormalExitHandler);
128 signal(SIGSEGV, abnormalExitHandler);
129 #endif