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>
40 #elif defined Q_OS_WIN
44 #include <QCoreApplication>
45 #include <QMetaObject>
47 #include "base/version.h"
50 #include "stacktrace.h"
53 #include "gui/stacktracedialog.h"
59 // sys_signame[] is only defined in BSD
60 const char *const sysSigName
[] =
63 "", "", "SIGINT", "", "SIGILL", "", "SIGABRT_COMPAT", "", "SIGFPE", "",
64 "", "SIGSEGV", "", "", "", "SIGTERM", "", "", "", "",
65 "", "SIGBREAK", "SIGABRT", "", "", "", "", "", "", "",
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",
75 void safePrint(const char *str
)
77 const size_t strLen
= strlen(str
);
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
));
82 if (write(STDERR_FILENO
, str
, strLen
) < static_cast<ssize_t
>(strLen
))
83 std::ignore
= write(STDOUT_FILENO
, str
, strLen
);
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
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"
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
);
109 StacktraceDialog dlg
; // unsafe
110 dlg
.setText(QString::fromLatin1(sigName
), QString::fromStdString(stacktrace
));
114 signal(signum
, SIG_DFL
);
120 void registerSignalHandlers()
122 signal(SIGINT
, normalExitHandler
);
123 signal(SIGTERM
, normalExitHandler
);
126 signal(SIGABRT
, abnormalExitHandler
);
127 signal(SIGSEGV
, abnormalExitHandler
);