SignalDaemon.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2018 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DolphinQt/QtUtils/SignalDaemon.h"
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <QSocketNotifier>
  8. int SignalDaemon::s_sigterm_fd[2];
  9. static constexpr char message[] =
  10. "\nA signal was received. A second signal will force Dolphin to stop.\n";
  11. SignalDaemon::SignalDaemon(QObject* parent) : QObject(parent)
  12. {
  13. if (socketpair(AF_UNIX, SOCK_STREAM, 0, s_sigterm_fd))
  14. qFatal("Couldn't create TERM socketpair");
  15. m_term = new QSocketNotifier(s_sigterm_fd[1], QSocketNotifier::Read, this);
  16. connect(m_term, &QSocketNotifier::activated, this, &SignalDaemon::OnNotifierActivated);
  17. }
  18. SignalDaemon::~SignalDaemon()
  19. {
  20. close(s_sigterm_fd[0]);
  21. close(s_sigterm_fd[1]);
  22. }
  23. void SignalDaemon::OnNotifierActivated()
  24. {
  25. m_term->setEnabled(false);
  26. char tmp;
  27. if (read(s_sigterm_fd[1], &tmp, sizeof(char)) != sizeof(char))
  28. {
  29. // Not much we can do here.
  30. }
  31. m_term->setEnabled(true);
  32. emit InterruptReceived();
  33. }
  34. void SignalDaemon::HandleInterrupt(int)
  35. {
  36. if (write(STDERR_FILENO, message, sizeof(message)) != sizeof(message))
  37. {
  38. // Not much we can do here.
  39. }
  40. char a = 1;
  41. if (write(s_sigterm_fd[0], &a, sizeof(a)) != sizeof(a))
  42. {
  43. // Not much we can do here.
  44. }
  45. }