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: 74 81 91.4 %
Date: 2020-09-03 22:13:26 Branches: 15 30 50.0 %

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

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