WebUI: Show 'Rename...' context menu item only when one torrent is selected
[qBittorrent.git] / src / app / signalhandler.cpp
blob1116b5a34260ee0e76f9d72058ca631b7c458d0f
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 <QtSystemDetection>
35 #include <algorithm>
36 #include <csignal>
38 #ifdef Q_OS_UNIX
39 #include <unistd.h>
40 #elif defined Q_OS_WIN
41 #include <io.h>
42 #endif
44 #include <QCoreApplication>
45 #include <QMetaObject>
47 #include "base/version.h"
49 #ifdef STACKTRACE
50 #include "stacktrace.h"
52 #ifndef DISABLE_GUI
53 #include "gui/stacktracedialog.h"
54 #endif
55 #endif //STACKTRACE
57 namespace
59 // sys_signame[] is only defined in BSD
60 const char *const sysSigName[] =
62 #ifdef Q_OS_WIN
63 "", "", "SIGINT", "", "SIGILL", "", "SIGABRT_COMPAT", "", "SIGFPE", "",
64 "", "SIGSEGV", "", "", "", "SIGTERM", "", "", "", "",
65 "", "SIGBREAK", "SIGABRT", "", "", "", "", "", "", "",
66 "", ""
67 #else
68 "", "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL",
69 "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP",
70 "SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGIO",
71 "SIGPWR", "SIGUNUSED"
72 #endif
75 void safePrint(const char *str)
77 const size_t strLen = strlen(str);
78 #ifdef Q_OS_WIN
79 if (_write(_fileno(stderr), str, static_cast<unsigned int>(strLen)) < static_cast<int>(strLen))
80 std::ignore = _write(_fileno(stdout), str, static_cast<unsigned int>(strLen));
81 #else
82 if (write(STDERR_FILENO, str, strLen) < static_cast<ssize_t>(strLen))
83 std::ignore = write(STDOUT_FILENO, str, strLen);
84 #endif
87 void normalExitHandler(const int signum)
89 const char *msgs[] = {"Catching signal: ", sysSigName[signum], "\nExiting cleanly\n"};
90 std::for_each(std::begin(msgs), std::end(msgs), safePrint);
91 signal(signum, SIG_DFL);
92 QMetaObject::invokeMethod(qApp, [] { QCoreApplication::exit(); }, Qt::QueuedConnection); // unsafe, but exit anyway
95 #ifdef STACKTRACE
96 void abnormalExitHandler(const int signum)
98 const char msg[] = "\n\n*************************************************************\n"
99 "Please file a bug report at https://bug.qbittorrent.org and provide the following information:\n\n"
100 "qBittorrent version: " QBT_VERSION "\n\n"
101 "Caught signal: ";
102 const char *sigName = sysSigName[signum];
103 const std::string stacktrace = getStacktrace();
105 const char *msgs[] = {msg, sigName, "\n```\n", stacktrace.c_str(), "```\n\n"};
106 std::for_each(std::begin(msgs), std::end(msgs), safePrint);
108 #ifndef DISABLE_GUI
109 StacktraceDialog dlg; // unsafe
110 dlg.setText(QString::fromLatin1(sigName), QString::fromStdString(stacktrace));
111 dlg.exec();
112 #endif
114 signal(signum, SIG_DFL);
115 raise(signum);
117 #endif // STACKTRACE
120 void registerSignalHandlers()
122 signal(SIGINT, normalExitHandler);
123 signal(SIGTERM, normalExitHandler);
125 #ifdef STACKTRACE
126 signal(SIGABRT, abnormalExitHandler);
127 signal(SIGSEGV, abnormalExitHandler);
128 #endif