GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: signal_wrap.cc Lines: 72 79 91.1 %
Date: 2021-09-25 04:12:41 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
98
  static void Initialize(Local<Object> target,
51
                         Local<Value> unused,
52
                         Local<Context> context,
53
                         void* priv) {
54
98
    Environment* env = Environment::GetCurrent(context);
55
98
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
56
196
    constructor->InstanceTemplate()->SetInternalFieldCount(
57
        SignalWrap::kInternalFieldCount);
58
98
    constructor->Inherit(HandleWrap::GetConstructorTemplate(env));
59
60
98
    env->SetProtoMethod(constructor, "start", Start);
61
98
    env->SetProtoMethod(constructor, "stop", Stop);
62
63
98
    env->SetConstructorFunction(target, "Signal", constructor);
64
98
  }
65
66
4851
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
67
4851
    registry->Register(New);
68
4851
    registry->Register(Start);
69
4851
    registry->Register(Stop);
70
4851
  }
71
72
3
  SET_NO_MEMORY_INFO()
73
3
  SET_MEMORY_INFO_NAME(SignalWrap)
74
3
  SET_SELF_SIZE(SignalWrap)
75
76
 private:
77
179
  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
179
    CHECK(args.IsConstructCall());
82
179
    Environment* env = Environment::GetCurrent(args);
83
179
    new SignalWrap(env, args.This());
84
179
  }
85
86
179
  SignalWrap(Environment* env, Local<Object> object)
87
179
      : HandleWrap(env,
88
                   object,
89
179
                   reinterpret_cast<uv_handle_t*>(&handle_),
90
179
                   AsyncWrap::PROVIDER_SIGNALWRAP) {
91
179
    int r = uv_signal_init(env->event_loop(), &handle_);
92
179
    CHECK_EQ(r, 0);
93
179
  }
94
95
115
  void Close(v8::Local<v8::Value> close_callback) override {
96
115
    if (active_) {
97
111
      DecreaseSignalHandlerCount(handle_.signum);
98
111
      active_ = false;
99
    }
100
115
    HandleWrap::Close(close_callback);
101
115
  }
102
103
177
  static void Start(const FunctionCallbackInfo<Value>& args) {
104
    SignalWrap* wrap;
105
178
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
106
177
    Environment* env = wrap->env();
107
    int signum;
108
354
    if (!args[0]->Int32Value(env->context()).To(&signum)) return;
109
#if defined(__POSIX__) && HAVE_INSPECTOR
110
177
    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
176
    int err = uv_signal_start(
120
176
        &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
176
    if (err == 0) {
132
176
      CHECK(!wrap->active_);
133
176
      wrap->active_ = true;
134
176
      Mutex::ScopedLock lock(handled_signals_mutex);
135
176
      handled_signals[signum]++;
136
    }
137
138
352
    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
111
void DecreaseSignalHandlerCount(int signum) {
162
222
  Mutex::ScopedLock lock(handled_signals_mutex);
163
111
  int64_t new_handler_count = --handled_signals[signum];
164
111
  CHECK_GE(new_handler_count, 0);
165
111
  if (new_handler_count == 0)
166
105
    handled_signals.erase(signum);
167
111
}
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
4924
NODE_MODULE_CONTEXT_AWARE_INTERNAL(signal_wrap, node::SignalWrap::Initialize)
177
4851
NODE_MODULE_EXTERNAL_REFERENCE(signal_wrap,
178
                               node::SignalWrap::RegisterExternalReferences)