Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
signal_wrap.cc
Go to the documentation of this file.
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.h"
23 #include "async-wrap-inl.h"
24 #include "env.h"
25 #include "env-inl.h"
26 #include "handle_wrap.h"
27 #include "util.h"
28 #include "util-inl.h"
29 #include "v8.h"
30 
31 namespace node {
32 
33 using v8::Context;
34 using v8::FunctionCallbackInfo;
35 using v8::FunctionTemplate;
36 using v8::HandleScope;
37 using v8::Integer;
38 using v8::Local;
39 using v8::Object;
40 using v8::String;
41 using v8::Value;
42 
43 namespace {
44 
45 class SignalWrap : public HandleWrap {
46  public:
47  static void Initialize(Local<Object> target,
48  Local<Value> unused,
49  Local<Context> context) {
50  Environment* env = Environment::GetCurrent(context);
51  Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
52  constructor->InstanceTemplate()->SetInternalFieldCount(1);
53  Local<String> signalString =
54  FIXED_ONE_BYTE_STRING(env->isolate(), "Signal");
55  constructor->SetClassName(signalString);
56 
57  AsyncWrap::AddWrapMethods(env, constructor);
58  env->SetProtoMethod(constructor, "close", HandleWrap::Close);
59  env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
60  env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
61  env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
62  env->SetProtoMethod(constructor, "start", Start);
63  env->SetProtoMethod(constructor, "stop", Stop);
64 
65  target->Set(signalString, constructor->GetFunction());
66  }
67 
68  size_t self_size() const override { return sizeof(*this); }
69 
70  private:
71  static void New(const FunctionCallbackInfo<Value>& args) {
72  // This constructor should not be exposed to public javascript.
73  // Therefore we assert that we are not trying to call this as a
74  // normal function.
75  CHECK(args.IsConstructCall());
76  Environment* env = Environment::GetCurrent(args);
77  new SignalWrap(env, args.This());
78  }
79 
80  SignalWrap(Environment* env, Local<Object> object)
81  : HandleWrap(env,
82  object,
83  reinterpret_cast<uv_handle_t*>(&handle_),
84  AsyncWrap::PROVIDER_SIGNALWRAP) {
85  int r = uv_signal_init(env->event_loop(), &handle_);
86  CHECK_EQ(r, 0);
87  }
88 
89  static void Start(const FunctionCallbackInfo<Value>& args) {
90  SignalWrap* wrap;
91  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
92  int signum = args[0]->Int32Value();
93 #if defined(__POSIX__) && HAVE_INSPECTOR
94  if (signum == SIGPROF) {
95  Environment* env = Environment::GetCurrent(args);
96  if (env->inspector_agent()->IsStarted()) {
98  "process.on(SIGPROF) is reserved while debugging");
99  return;
100  }
101  }
102 #endif
103  int err = uv_signal_start(&wrap->handle_, OnSignal, signum);
104  args.GetReturnValue().Set(err);
105  }
106 
107  static void Stop(const FunctionCallbackInfo<Value>& args) {
108  SignalWrap* wrap;
109  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
110  int err = uv_signal_stop(&wrap->handle_);
111  args.GetReturnValue().Set(err);
112  }
113 
114  static void OnSignal(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 
120  Local<Value> arg = Integer::New(env->isolate(), signum);
121  wrap->MakeCallback(env->onsignal_string(), 1, &arg);
122  }
123 
124  uv_signal_t handle_;
125 };
126 
127 
128 } // anonymous namespace
129 } // namespace node
130 
131 
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
void ProcessEmitWarning(Environment *env, const char *fmt,...)
Definition: node.cc:2707
QueryWrap * wrap
Definition: cares_wrap.cc:478
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
Definition: node_http2.cc:1172
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536