2018-05-22 19:30:54 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-05-22 19:30:54 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/QtUtils/SignalDaemon.h"
|
2018-05-22 19:30:54 +00:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <QSocketNotifier>
|
|
|
|
|
|
|
|
int SignalDaemon::s_sigterm_fd[2];
|
|
|
|
|
|
|
|
static constexpr char message[] =
|
|
|
|
"\nA signal was received. A second signal will force Dolphin to stop.\n";
|
|
|
|
|
|
|
|
SignalDaemon::SignalDaemon(QObject* parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, s_sigterm_fd))
|
|
|
|
qFatal("Couldn't create TERM socketpair");
|
|
|
|
|
|
|
|
m_term = new QSocketNotifier(s_sigterm_fd[1], QSocketNotifier::Read, this);
|
|
|
|
connect(m_term, &QSocketNotifier::activated, this, &SignalDaemon::OnNotifierActivated);
|
|
|
|
}
|
|
|
|
|
|
|
|
SignalDaemon::~SignalDaemon()
|
|
|
|
{
|
|
|
|
close(s_sigterm_fd[0]);
|
|
|
|
close(s_sigterm_fd[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignalDaemon::OnNotifierActivated()
|
|
|
|
{
|
|
|
|
m_term->setEnabled(false);
|
|
|
|
|
|
|
|
char tmp;
|
2021-05-17 19:59:44 +00:00
|
|
|
if (read(s_sigterm_fd[1], &tmp, sizeof(char)) != sizeof(char))
|
2018-05-22 19:30:54 +00:00
|
|
|
{
|
2021-05-17 19:59:44 +00:00
|
|
|
// Not much we can do here.
|
2018-05-22 19:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_term->setEnabled(true);
|
|
|
|
|
|
|
|
emit InterruptReceived();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SignalDaemon::HandleInterrupt(int)
|
|
|
|
{
|
2021-05-17 19:59:44 +00:00
|
|
|
if (write(STDERR_FILENO, message, sizeof(message)) != sizeof(message))
|
|
|
|
{
|
|
|
|
// Not much we can do here.
|
|
|
|
}
|
2018-05-22 19:30:54 +00:00
|
|
|
|
|
|
|
char a = 1;
|
2021-05-17 19:59:44 +00:00
|
|
|
if (write(s_sigterm_fd[0], &a, sizeof(a)) != sizeof(a))
|
2018-05-22 19:30:54 +00:00
|
|
|
{
|
2021-05-17 19:59:44 +00:00
|
|
|
// Not much we can do here.
|
2018-05-22 19:30:54 +00:00
|
|
|
}
|
|
|
|
}
|