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

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