Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
process_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 "env.h"
23 #include "env-inl.h"
24 #include "handle_wrap.h"
25 #include "node_wrap.h"
26 #include "util.h"
27 #include "util-inl.h"
28 
29 #include <string.h>
30 #include <stdlib.h>
31 
32 namespace node {
33 
34 using v8::Array;
35 using v8::Context;
36 using v8::FunctionCallbackInfo;
37 using v8::FunctionTemplate;
38 using v8::HandleScope;
39 using v8::Integer;
40 using v8::Local;
41 using v8::Number;
42 using v8::Object;
43 using v8::String;
44 using v8::Value;
45 
46 namespace {
47 
48 class ProcessWrap : public HandleWrap {
49  public:
50  static void Initialize(Local<Object> target,
51  Local<Value> unused,
52  Local<Context> context) {
53  Environment* env = Environment::GetCurrent(context);
54  Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
55  constructor->InstanceTemplate()->SetInternalFieldCount(1);
56  Local<String> processString =
57  FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
58  constructor->SetClassName(processString);
59 
60  AsyncWrap::AddWrapMethods(env, constructor);
61 
62  env->SetProtoMethod(constructor, "close", HandleWrap::Close);
63 
64  env->SetProtoMethod(constructor, "spawn", Spawn);
65  env->SetProtoMethod(constructor, "kill", Kill);
66 
67  env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
68  env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
69  env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
70 
71  target->Set(processString, constructor->GetFunction());
72  }
73 
74  size_t self_size() const override { return sizeof(*this); }
75 
76  private:
77  static void New(const FunctionCallbackInfo<Value>& args) {
78  // This constructor should not be exposed to public javascript.
79  // Therefore we assert that we are not trying to call this as a
80  // normal function.
81  CHECK(args.IsConstructCall());
82  Environment* env = Environment::GetCurrent(args);
83  new ProcessWrap(env, args.This());
84  }
85 
86  ProcessWrap(Environment* env, Local<Object> object)
87  : HandleWrap(env,
88  object,
89  reinterpret_cast<uv_handle_t*>(&process_),
90  AsyncWrap::PROVIDER_PROCESSWRAP) {
91  }
92 
93  static void ParseStdioOptions(Environment* env,
94  Local<Object> js_options,
95  uv_process_options_t* options) {
96  Local<String> stdio_key = env->stdio_string();
97  Local<Array> stdios = js_options->Get(stdio_key).As<Array>();
98 
99  uint32_t len = stdios->Length();
100  options->stdio = new uv_stdio_container_t[len];
101  options->stdio_count = len;
102 
103  for (uint32_t i = 0; i < len; i++) {
104  Local<Object> stdio = stdios->Get(i).As<Object>();
105  Local<Value> type = stdio->Get(env->type_string());
106 
107  if (type->Equals(env->ignore_string())) {
108  options->stdio[i].flags = UV_IGNORE;
109  } else if (type->Equals(env->pipe_string())) {
110  options->stdio[i].flags = static_cast<uv_stdio_flags>(
111  UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
112  Local<String> handle_key = env->handle_string();
113  Local<Object> handle = stdio->Get(handle_key).As<Object>();
114  CHECK(!handle.IsEmpty());
115  options->stdio[i].data.stream =
116  reinterpret_cast<uv_stream_t*>(
117  Unwrap<PipeWrap>(handle)->UVHandle());
118  } else if (type->Equals(env->wrap_string())) {
119  Local<String> handle_key = env->handle_string();
120  Local<Object> handle = stdio->Get(handle_key).As<Object>();
121  uv_stream_t* stream = HandleToStream(env, handle);
122  CHECK_NE(stream, nullptr);
123 
124  options->stdio[i].flags = UV_INHERIT_STREAM;
125  options->stdio[i].data.stream = stream;
126  } else {
127  Local<String> fd_key = env->fd_string();
128  int fd = static_cast<int>(stdio->Get(fd_key)->IntegerValue());
129  options->stdio[i].flags = UV_INHERIT_FD;
130  options->stdio[i].data.fd = fd;
131  }
132  }
133  }
134 
135  static void Spawn(const FunctionCallbackInfo<Value>& args) {
136  Environment* env = Environment::GetCurrent(args);
137 
138  ProcessWrap* wrap;
139  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
140 
141  Local<Object> js_options = args[0]->ToObject(env->isolate());
142 
143  uv_process_options_t options;
144  memset(&options, 0, sizeof(uv_process_options_t));
145 
146  options.exit_cb = OnExit;
147 
148  // options.uid
149  Local<Value> uid_v = js_options->Get(env->uid_string());
150  if (!uid_v->IsUndefined() && !uid_v->IsNull()) {
151  CHECK(uid_v->IsInt32());
152  const int32_t uid = uid_v->Int32Value(env->context()).FromJust();
153  options.flags |= UV_PROCESS_SETUID;
154  options.uid = static_cast<uv_uid_t>(uid);
155  }
156 
157  // options.gid
158  Local<Value> gid_v = js_options->Get(env->gid_string());
159  if (!gid_v->IsUndefined() && !gid_v->IsNull()) {
160  CHECK(gid_v->IsInt32());
161  const int32_t gid = gid_v->Int32Value(env->context()).FromJust();
162  options.flags |= UV_PROCESS_SETGID;
163  options.gid = static_cast<uv_gid_t>(gid);
164  }
165 
166  // TODO(bnoordhuis) is this possible to do without mallocing ?
167 
168  // options.file
169  Local<Value> file_v = js_options->Get(env->file_string());
170  CHECK(file_v->IsString());
171  node::Utf8Value file(env->isolate(), file_v);
172  options.file = *file;
173 
174  // options.args
175  Local<Value> argv_v = js_options->Get(env->args_string());
176  if (!argv_v.IsEmpty() && argv_v->IsArray()) {
177  Local<Array> js_argv = Local<Array>::Cast(argv_v);
178  int argc = js_argv->Length();
179  // Heap allocate to detect errors. +1 is for nullptr.
180  options.args = new char*[argc + 1];
181  for (int i = 0; i < argc; i++) {
182  node::Utf8Value arg(env->isolate(), js_argv->Get(i));
183  options.args[i] = strdup(*arg);
184  CHECK_NE(options.args[i], nullptr);
185  }
186  options.args[argc] = nullptr;
187  }
188 
189  // options.cwd
190  Local<Value> cwd_v = js_options->Get(env->cwd_string());
191  node::Utf8Value cwd(env->isolate(),
192  cwd_v->IsString() ? cwd_v : Local<Value>());
193  if (cwd.length() > 0) {
194  options.cwd = *cwd;
195  }
196 
197  // options.env
198  Local<Value> env_v = js_options->Get(env->env_pairs_string());
199  if (!env_v.IsEmpty() && env_v->IsArray()) {
200  Local<Array> env_opt = Local<Array>::Cast(env_v);
201  int envc = env_opt->Length();
202  options.env = new char*[envc + 1]; // Heap allocated to detect errors.
203  for (int i = 0; i < envc; i++) {
204  node::Utf8Value pair(env->isolate(), env_opt->Get(i));
205  options.env[i] = strdup(*pair);
206  CHECK_NE(options.env[i], nullptr);
207  }
208  options.env[envc] = nullptr;
209  }
210 
211  // options.stdio
212  ParseStdioOptions(env, js_options, &options);
213 
214  // options.windows_verbatim_arguments
215  Local<String> windows_verbatim_arguments_key =
216  env->windows_verbatim_arguments_string();
217  if (js_options->Get(windows_verbatim_arguments_key)->IsTrue()) {
218  options.flags |= UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS;
219  }
220 
221  // options.detached
222  Local<String> detached_key = env->detached_string();
223  if (js_options->Get(detached_key)->IsTrue()) {
224  options.flags |= UV_PROCESS_DETACHED;
225  }
226 
227  int err = uv_spawn(env->event_loop(), &wrap->process_, &options);
228 
229  if (err == 0) {
230  CHECK_EQ(wrap->process_.data, wrap);
231  wrap->object()->Set(env->pid_string(),
232  Integer::New(env->isolate(), wrap->process_.pid));
233  }
234 
235  if (options.args) {
236  for (int i = 0; options.args[i]; i++) free(options.args[i]);
237  delete [] options.args;
238  }
239 
240  if (options.env) {
241  for (int i = 0; options.env[i]; i++) free(options.env[i]);
242  delete [] options.env;
243  }
244 
245  delete[] options.stdio;
246 
247  args.GetReturnValue().Set(err);
248  }
249 
250  static void Kill(const FunctionCallbackInfo<Value>& args) {
251  ProcessWrap* wrap;
252  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
253  int signal = args[0]->Int32Value();
254  int err = uv_process_kill(&wrap->process_, signal);
255  args.GetReturnValue().Set(err);
256  }
257 
258  static void OnExit(uv_process_t* handle,
259  int64_t exit_status,
260  int term_signal) {
261  ProcessWrap* wrap = static_cast<ProcessWrap*>(handle->data);
262  CHECK_NE(wrap, nullptr);
263  CHECK_EQ(&wrap->process_, handle);
264 
265  Environment* env = wrap->env();
266  HandleScope handle_scope(env->isolate());
267  Context::Scope context_scope(env->context());
268 
269  Local<Value> argv[] = {
270  Number::New(env->isolate(), static_cast<double>(exit_status)),
271  OneByteString(env->isolate(), signo_string(term_signal))
272  };
273 
274  wrap->MakeCallback(env->onexit_string(), arraysize(argv), argv);
275  }
276 
277  uv_process_t process_;
278 };
279 
280 
281 } // anonymous namespace
282 } // namespace node
283 
uv_file file
Definition: module_wrap.cc:328
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
int len
Definition: cares_wrap.cc:485
QueryWrap * wrap
Definition: cares_wrap.cc:478
const char * signo_string(int signo)
Definition: node.cc:718
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