summaryrefslogtreecommitdiff
path: root/src/SignalHandler.cpp
blob: 2d32ac6d038a929226d0a157179191e1c39fcd84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (C) 2020-2021 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include "SignalHandler.hpp"

#ifndef _WIN32

#  include "Context.hpp"
#  include "assertions.hpp"

#  include <signal.h> // NOLINT: sigaddset et al are defined in signal.h
#  include <sys/types.h>
#  include <sys/wait.h>
#  include <unistd.h>

namespace {

SignalHandler* g_the_signal_handler = nullptr;
sigset_t g_fatal_signal_set;

void
register_signal_handler(int signum)
{
  struct sigaction act;
  memset(&act, 0, sizeof(act));
  act.sa_handler = SignalHandler::on_signal;
  act.sa_mask = g_fatal_signal_set;
#  ifdef SA_RESTART
  act.sa_flags = SA_RESTART;
#  endif
  sigaction(signum, &act, nullptr);
}

} // namespace

SignalHandler::SignalHandler(Context& ctx) : m_ctx(ctx)
{
  ASSERT(!g_the_signal_handler);
  g_the_signal_handler = this;

  sigemptyset(&g_fatal_signal_set);
  sigaddset(&g_fatal_signal_set, SIGINT);
  sigaddset(&g_fatal_signal_set, SIGTERM);
#  ifdef SIGHUP
  sigaddset(&g_fatal_signal_set, SIGHUP);
#  endif
#  ifdef SIGQUIT
  sigaddset(&g_fatal_signal_set, SIGQUIT);
#  endif

  register_signal_handler(SIGINT);
  register_signal_handler(SIGTERM);
#  ifdef SIGHUP
  register_signal_handler(SIGHUP);
#  endif
#  ifdef SIGQUIT
  register_signal_handler(SIGQUIT);
#  endif

  signal(SIGPIPE, SIG_IGN); // NOLINT: This is no error, clang-tidy
}

SignalHandler::~SignalHandler()
{
  ASSERT(g_the_signal_handler);
  g_the_signal_handler = nullptr;
}

void
SignalHandler::on_signal(int signum)
{
  ASSERT(g_the_signal_handler);
  Context& ctx = g_the_signal_handler->m_ctx;

  // Unregister handler for this signal so that we can send the signal to
  // ourselves at the end of the handler.
  signal(signum, SIG_DFL);

  // If ccache was killed explicitly, then bring the compiler subprocess (if
  // any) with us as well.
  if (signum == SIGTERM && ctx.compiler_pid != 0
      && waitpid(ctx.compiler_pid, nullptr, WNOHANG) == 0) {
    kill(ctx.compiler_pid, signum);
  }

  ctx.unlink_pending_tmp_files_signal_safe();

  if (ctx.compiler_pid != 0) {
    // Wait for compiler subprocess to exit before we snuff it.
    waitpid(ctx.compiler_pid, nullptr, 0);
  }

  // Resend signal to ourselves to exit properly after returning from the
  // handler.
  kill(getpid(), signum);
}

#else // !_WIN32

SignalHandler::SignalHandler(Context& ctx) : m_ctx(ctx)
{
}

SignalHandler::~SignalHandler()
{
}

#endif // !_WIN32

void
SignalHandler::block_signals()
{
#ifndef _WIN32
  sigprocmask(SIG_BLOCK, &g_fatal_signal_set, nullptr);
#endif
}

void
SignalHandler::unblock_signals()
{
#ifndef _WIN32
  sigset_t empty;
  sigemptyset(&empty);
  sigprocmask(SIG_SETMASK, &empty, nullptr);
#endif
}

SignalHandlerBlocker::SignalHandlerBlocker()
{
  SignalHandler::block_signals();
}

SignalHandlerBlocker::~SignalHandlerBlocker()
{
  SignalHandler::unblock_signals();
}