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