GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/signal_wrap.cc Lines: 71 78 91.0 %
Date: 2021-06-05 04:12:00 Branches: 16 30 53.3 %

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_process-inl.h"
26
#include "util-inl.h"
27
#include "v8.h"
28
29
namespace node {
30
31
using v8::Context;
32
using v8::FunctionCallbackInfo;
33
using v8::FunctionTemplate;
34
using v8::HandleScope;
35
using v8::Integer;
36
using v8::Local;
37
using v8::Object;
38
using v8::Value;
39
40
void DecreaseSignalHandlerCount(int signum);
41
42
namespace {
43
44
4844
static Mutex handled_signals_mutex;
45
4844
static std::map<int, int64_t> handled_signals;  // Signal -> number of handlers
46
47
262
class SignalWrap : public HandleWrap {
48
 public:
49
96
  static void Initialize(Local<Object> target,
50
                         Local<Value> unused,
51
                         Local<Context> context,
52
                         void* priv) {
53
96
    Environment* env = Environment::GetCurrent(context);
54
96
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
55
288
    constructor->InstanceTemplate()->SetInternalFieldCount(
56
96
        SignalWrap::kInternalFieldCount);
57
192
    constructor->Inherit(HandleWrap::GetConstructorTemplate(env));
58
59
96
    env->SetProtoMethod(constructor, "start", Start);
60
96
    env->SetProtoMethod(constructor, "stop", Stop);
61
62
96
    env->SetConstructorFunction(target, "Signal", constructor);
63
96
  }
64
65
3
  SET_NO_MEMORY_INFO()
66
3
  SET_MEMORY_INFO_NAME(SignalWrap)
67
3
  SET_SELF_SIZE(SignalWrap)
68
69
 private:
70
191
  static void New(const FunctionCallbackInfo<Value>& args) {
71
    // This constructor should not be exposed to public javascript.
72
    // Therefore we assert that we are not trying to call this as a
73
    // normal function.
74
191
    CHECK(args.IsConstructCall());
75
191
    Environment* env = Environment::GetCurrent(args);
76
191
    new SignalWrap(env, args.This());
77
191
  }
78
79
191
  SignalWrap(Environment* env, Local<Object> object)
80
191
      : HandleWrap(env,
81
                   object,
82
191
                   reinterpret_cast<uv_handle_t*>(&handle_),
83
191
                   AsyncWrap::PROVIDER_SIGNALWRAP) {
84
191
    int r = uv_signal_init(env->event_loop(), &handle_);
85
191
    CHECK_EQ(r, 0);
86
191
  }
87
88
133
  void Close(v8::Local<v8::Value> close_callback) override {
89
133
    if (active_) {
90
129
      DecreaseSignalHandlerCount(handle_.signum);
91
129
      active_ = false;
92
    }
93
133
    HandleWrap::Close(close_callback);
94
133
  }
95
96
189
  static void Start(const FunctionCallbackInfo<Value>& args) {
97
    SignalWrap* wrap;
98
190
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
99
189
    Environment* env = wrap->env();
100
    int signum;
101
756
    if (!args[0]->Int32Value(env->context()).To(&signum)) return;
102
#if defined(__POSIX__) && HAVE_INSPECTOR
103
189
    if (signum == SIGPROF) {
104
1
      Environment* env = Environment::GetCurrent(args);
105
1
      if (env->inspector_agent()->IsListening()) {
106
        ProcessEmitWarning(env,
107
1
                           "process.on(SIGPROF) is reserved while debugging");
108
1
        return;
109
      }
110
    }
111
#endif
112
564
    int err = uv_signal_start(
113
188
        &wrap->handle_,
114
230
        [](uv_signal_t* handle, int signum) {
115
21
          SignalWrap* wrap = ContainerOf(&SignalWrap::handle_, handle);
116
21
          Environment* env = wrap->env();
117
41
          HandleScope handle_scope(env->isolate());
118
21
          Context::Scope context_scope(env->context());
119
21
          Local<Value> arg = Integer::New(env->isolate(), signum);
120
21
          wrap->MakeCallback(env->onsignal_string(), 1, &arg);
121
229
        },
122
188
        signum);
123
124
188
    if (err == 0) {
125
188
      CHECK(!wrap->active_);
126
188
      wrap->active_ = true;
127
376
      Mutex::ScopedLock lock(handled_signals_mutex);
128
188
      handled_signals[signum]++;
129
    }
130
131
376
    args.GetReturnValue().Set(err);
132
  }
133
134
  static void Stop(const FunctionCallbackInfo<Value>& args) {
135
    SignalWrap* wrap;
136
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
137
138
    if (wrap->active_)  {
139
      wrap->active_ = false;
140
      DecreaseSignalHandlerCount(wrap->handle_.signum);
141
    }
142
143
    int err = uv_signal_stop(&wrap->handle_);
144
    args.GetReturnValue().Set(err);
145
  }
146
147
  uv_signal_t handle_;
148
  bool active_ = false;
149
};
150
151
152
}  // anonymous namespace
153
154
129
void DecreaseSignalHandlerCount(int signum) {
155
258
  Mutex::ScopedLock lock(handled_signals_mutex);
156
129
  int64_t new_handler_count = --handled_signals[signum];
157
129
  CHECK_GE(new_handler_count, 0);
158
129
  if (new_handler_count == 0)
159
120
    handled_signals.erase(signum);
160
129
}
161
162
28
bool HasSignalJSHandler(int signum) {
163
56
  Mutex::ScopedLock lock(handled_signals_mutex);
164
56
  return handled_signals.find(signum) != handled_signals.end();
165
}
166
}  // namespace node
167
168
169

19368
NODE_MODULE_CONTEXT_AWARE_INTERNAL(signal_wrap, node::SignalWrap::Initialize)