• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static Mutex handled_signals_mutex;
45 static std::map<int, int64_t> handled_signals;  // Signal -> number of handlers
46 
47 class SignalWrap : public HandleWrap {
48  public:
Initialize(Local<Object> target,Local<Value> unused,Local<Context> context,void * priv)49   static void Initialize(Local<Object> target,
50                          Local<Value> unused,
51                          Local<Context> context,
52                          void* priv) {
53     Environment* env = Environment::GetCurrent(context);
54     Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
55     constructor->InstanceTemplate()->SetInternalFieldCount(
56         SignalWrap::kInternalFieldCount);
57     constructor->Inherit(HandleWrap::GetConstructorTemplate(env));
58 
59     env->SetProtoMethod(constructor, "start", Start);
60     env->SetProtoMethod(constructor, "stop", Stop);
61 
62     env->SetConstructorFunction(target, "Signal", constructor);
63   }
64 
65   SET_NO_MEMORY_INFO()
66   SET_MEMORY_INFO_NAME(SignalWrap)
67   SET_SELF_SIZE(SignalWrap)
68 
69  private:
New(const FunctionCallbackInfo<Value> & args)70   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     CHECK(args.IsConstructCall());
75     Environment* env = Environment::GetCurrent(args);
76     new SignalWrap(env, args.This());
77   }
78 
SignalWrap(Environment * env,Local<Object> object)79   SignalWrap(Environment* env, Local<Object> object)
80       : HandleWrap(env,
81                    object,
82                    reinterpret_cast<uv_handle_t*>(&handle_),
83                    AsyncWrap::PROVIDER_SIGNALWRAP) {
84     int r = uv_signal_init(env->event_loop(), &handle_);
85     CHECK_EQ(r, 0);
86   }
87 
Close(v8::Local<v8::Value> close_callback)88   void Close(v8::Local<v8::Value> close_callback) override {
89     if (active_) {
90       DecreaseSignalHandlerCount(handle_.signum);
91       active_ = false;
92     }
93     HandleWrap::Close(close_callback);
94   }
95 
Start(const FunctionCallbackInfo<Value> & args)96   static void Start(const FunctionCallbackInfo<Value>& args) {
97     SignalWrap* wrap;
98     ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
99     Environment* env = wrap->env();
100     int signum;
101     if (!args[0]->Int32Value(env->context()).To(&signum)) return;
102 #if defined(__POSIX__) && HAVE_INSPECTOR
103     if (signum == SIGPROF) {
104       Environment* env = Environment::GetCurrent(args);
105       if (env->inspector_agent()->IsListening()) {
106         ProcessEmitWarning(env,
107                            "process.on(SIGPROF) is reserved while debugging");
108         return;
109       }
110     }
111 #endif
112     int err = uv_signal_start(
113         &wrap->handle_,
114         [](uv_signal_t* handle, int signum) {
115           SignalWrap* wrap = ContainerOf(&SignalWrap::handle_, handle);
116           Environment* env = wrap->env();
117           HandleScope handle_scope(env->isolate());
118           Context::Scope context_scope(env->context());
119           Local<Value> arg = Integer::New(env->isolate(), signum);
120           wrap->MakeCallback(env->onsignal_string(), 1, &arg);
121         },
122         signum);
123 
124     if (err == 0) {
125       CHECK(!wrap->active_);
126       wrap->active_ = true;
127       Mutex::ScopedLock lock(handled_signals_mutex);
128       handled_signals[signum]++;
129     }
130 
131     args.GetReturnValue().Set(err);
132   }
133 
Stop(const FunctionCallbackInfo<Value> & args)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 
DecreaseSignalHandlerCount(int signum)154 void DecreaseSignalHandlerCount(int signum) {
155   Mutex::ScopedLock lock(handled_signals_mutex);
156   int64_t new_handler_count = --handled_signals[signum];
157   CHECK_GE(new_handler_count, 0);
158   if (new_handler_count == 0)
159     handled_signals.erase(signum);
160 }
161 
HasSignalJSHandler(int signum)162 bool HasSignalJSHandler(int signum) {
163   Mutex::ScopedLock lock(handled_signals_mutex);
164   return handled_signals.find(signum) != handled_signals.end();
165 }
166 }  // namespace node
167 
168 
169 NODE_MODULE_CONTEXT_AWARE_INTERNAL(signal_wrap, node::SignalWrap::Initialize)
170