GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: signal_wrap.cc Lines: 72 79 91.1 %
Date: 2022-05-20 04:15:46 Branches: 14 26 53.8 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "async_wrap-inl.h"
23
#include "env-inl.h"
24
#include "handle_wrap.h"
25
#include "node_external_reference.h"
26
#include "node_process-inl.h"
27
#include "util-inl.h"
28
#include "v8.h"
29
30
namespace node {
31
32
using v8::Context;
33
using v8::FunctionCallbackInfo;
34
using v8::FunctionTemplate;
35
using v8::HandleScope;
36
using v8::Integer;
37
using v8::Local;
38
using v8::Object;
39
using v8::Value;
40
41
void DecreaseSignalHandlerCount(int signum);
42
43
namespace {
44
45
static Mutex handled_signals_mutex;
46
static std::map<int, int64_t> handled_signals;  // Signal -> number of handlers
47
48
class SignalWrap : public HandleWrap {
49
 public:
50
101
  static void Initialize(Local<Object> target,
51
                         Local<Value> unused,
52
                         Local<Context> context,
53
                         void* priv) {
54
101
    Environment* env = Environment::GetCurrent(context);
55
101
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
56
202
    constructor->InstanceTemplate()->SetInternalFieldCount(
57
        SignalWrap::kInternalFieldCount);
58
101
    constructor->Inherit(HandleWrap::GetConstructorTemplate(env));
59
60
101
    env->SetProtoMethod(constructor, "start", Start);
61
101
    env->SetProtoMethod(constructor, "stop", Stop);
62
63
101
    env->SetConstructorFunction(target, "Signal", constructor);
64
101
  }
65
66
5111
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
67
5111
    registry->Register(New);
68
5111
    registry->Register(Start);
69
5111
    registry->Register(Stop);
70
5111
  }
71
72
3
  SET_NO_MEMORY_INFO()
73
3
  SET_MEMORY_INFO_NAME(SignalWrap)
74
3
  SET_SELF_SIZE(SignalWrap)
75
76
 private:
77
186
  static void New(const FunctionCallbackInfo<Value>& args) {
78
    // This constructor should not be exposed to public javascript.
79
    // Therefore we assert that we are not trying to call this as a
80
    // normal function.
81
186
    CHECK(args.IsConstructCall());
82
186
    Environment* env = Environment::GetCurrent(args);
83
186
    new SignalWrap(env, args.This());
84
186
  }
85
86
186
  SignalWrap(Environment* env, Local<Object> object)
87
186
      : HandleWrap(env,
88
                   object,
89
186
                   reinterpret_cast<uv_handle_t*>(&handle_),
90
186
                   AsyncWrap::PROVIDER_SIGNALWRAP) {
91
186
    int r = uv_signal_init(env->event_loop(), &handle_);
92
186
    CHECK_EQ(r, 0);
93
186
  }
94
95
114
  void Close(v8::Local<v8::Value> close_callback) override {
96
114
    if (active_) {
97
110
      DecreaseSignalHandlerCount(handle_.signum);
98
110
      active_ = false;
99
    }
100
114
    HandleWrap::Close(close_callback);
101
114
  }
102
103
184
  static void Start(const FunctionCallbackInfo<Value>& args) {
104
    SignalWrap* wrap;
105
185
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
106
184
    Environment* env = wrap->env();
107
    int signum;
108
368
    if (!args[0]->Int32Value(env->context()).To(&signum)) return;
109
#if defined(__POSIX__) && HAVE_INSPECTOR
110
184
    if (signum == SIGPROF) {
111
1
      Environment* env = Environment::GetCurrent(args);
112
1
      if (env->inspector_agent()->IsListening()) {
113
        ProcessEmitWarning(env,
114
1
                           "process.on(SIGPROF) is reserved while debugging");
115
1
        return;
116
      }
117
    }
118
#endif
119
183
    int err = uv_signal_start(
120
183
        &wrap->handle_,
121
21
        [](uv_signal_t* handle, int signum) {
122
21
          SignalWrap* wrap = ContainerOf(&SignalWrap::handle_, handle);
123
21
          Environment* env = wrap->env();
124
41
          HandleScope handle_scope(env->isolate());
125
21
          Context::Scope context_scope(env->context());
126
21
          Local<Value> arg = Integer::New(env->isolate(), signum);
127
21
          wrap->MakeCallback(env->onsignal_string(), 1, &arg);
128
20
        },
129
        signum);
130
131
183
    if (err == 0) {
132
183
      CHECK(!wrap->active_);
133
183
      wrap->active_ = true;
134
183
      Mutex::ScopedLock lock(handled_signals_mutex);
135
183
      handled_signals[signum]++;
136
    }
137
138
366
    args.GetReturnValue().Set(err);
139
  }
140
141
  static void Stop(const FunctionCallbackInfo<Value>& args) {
142
    SignalWrap* wrap;
143
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
144
145
    if (wrap->active_)  {
146
      wrap->active_ = false;
147
      DecreaseSignalHandlerCount(wrap->handle_.signum);
148
    }
149
150
    int err = uv_signal_stop(&wrap->handle_);
151
    args.GetReturnValue().Set(err);
152
  }
153
154
  uv_signal_t handle_;
155
  bool active_ = false;
156
};
157
158
159
}  // anonymous namespace
160
161
110
void DecreaseSignalHandlerCount(int signum) {
162
220
  Mutex::ScopedLock lock(handled_signals_mutex);
163
110
  int64_t new_handler_count = --handled_signals[signum];
164
110
  CHECK_GE(new_handler_count, 0);
165
110
  if (new_handler_count == 0)
166
104
    handled_signals.erase(signum);
167
110
}
168
169
28
bool HasSignalJSHandler(int signum) {
170
28
  Mutex::ScopedLock lock(handled_signals_mutex);
171
28
  return handled_signals.find(signum) != handled_signals.end();
172
}
173
}  // namespace node
174
175
176
5169
NODE_MODULE_CONTEXT_AWARE_INTERNAL(signal_wrap, node::SignalWrap::Initialize)
177
5111
NODE_MODULE_EXTERNAL_REFERENCE(signal_wrap,
178
                               node::SignalWrap::RegisterExternalReferences)