GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/process_wrap.cc Lines: 141 149 94.6 %
Date: 2017-11-19 Branches: 70 102 68.6 %

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 "env-inl.h"
23
#include "handle_wrap.h"
24
#include "node_wrap.h"
25
#include "util-inl.h"
26
27
#include <string.h>
28
#include <stdlib.h>
29
30
namespace node {
31
32
using v8::Array;
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::Number;
40
using v8::Object;
41
using v8::String;
42
using v8::Value;
43
44
namespace {
45
46
2782
class ProcessWrap : public HandleWrap {
47
 public:
48
2898
  static void Initialize(Local<Object> target,
49
                         Local<Value> unused,
50
                         Local<Context> context) {
51
2898
    Environment* env = Environment::GetCurrent(context);
52
2898
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
53
5796
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
54
    Local<String> processString =
55
2898
        FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
56
2898
    constructor->SetClassName(processString);
57
58
2898
    AsyncWrap::AddWrapMethods(env, constructor);
59
60
2898
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
61
62
2898
    env->SetProtoMethod(constructor, "spawn", Spawn);
63
2898
    env->SetProtoMethod(constructor, "kill", Kill);
64
65
2898
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
66
2898
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
67
2898
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
68
69
5796
    target->Set(processString, constructor->GetFunction());
70
2898
  }
71
72
  size_t self_size() const override { return sizeof(*this); }
73
74
 private:
75
1426
  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
1426
    CHECK(args.IsConstructCall());
80
1426
    Environment* env = Environment::GetCurrent(args);
81
1426
    new ProcessWrap(env, args.This());
82
1426
  }
83
84
1426
  ProcessWrap(Environment* env, Local<Object> object)
85
      : HandleWrap(env,
86
                   object,
87
                   reinterpret_cast<uv_handle_t*>(&process_),
88
1426
                   AsyncWrap::PROVIDER_PROCESSWRAP) {
89
1426
  }
90
91
1420
  static void ParseStdioOptions(Environment* env,
92
                                Local<Object> js_options,
93
                                uv_process_options_t* options) {
94
1420
    Local<Context> context = env->context();
95
1420
    Local<String> stdio_key = env->stdio_string();
96
    Local<Array> stdios =
97
4260
        js_options->Get(context, stdio_key).ToLocalChecked().As<Array>();
98
99
1420
    uint32_t len = stdios->Length();
100
1420
    options->stdio = new uv_stdio_container_t[len];
101
1420
    options->stdio_count = len;
102
103
6364
    for (uint32_t i = 0; i < len; i++) {
104
      Local<Object> stdio =
105
14832
          stdios->Get(context, i).ToLocalChecked().As<Object>();
106
      Local<Value> type =
107
14832
          stdio->Get(context, env->type_string()).ToLocalChecked();
108
109
9888
      if (type->Equals(env->ignore_string())) {
110
39
        options->stdio[i].flags = UV_IGNORE;
111
9810
      } else if (type->Equals(env->pipe_string())) {
112
2504
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
113
2504
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
114
2504
        Local<String> handle_key = env->handle_string();
115
        Local<Object> handle =
116
7512
            stdio->Get(context, handle_key).ToLocalChecked().As<Object>();
117
2504
        CHECK(!handle.IsEmpty());
118
2504
        options->stdio[i].data.stream =
119
            reinterpret_cast<uv_stream_t*>(
120
2504
                Unwrap<PipeWrap>(handle)->UVHandle());
121
4802
      } else if (type->Equals(env->wrap_string())) {
122
        Local<String> handle_key = env->handle_string();
123
        Local<Object> handle =
124
            stdio->Get(context, handle_key).ToLocalChecked().As<Object>();
125
        uv_stream_t* stream = HandleToStream(env, handle);
126
        CHECK_NE(stream, nullptr);
127
128
        options->stdio[i].flags = UV_INHERIT_STREAM;
129
        options->stdio[i].data.stream = stream;
130
      } else {
131
2401
        Local<String> fd_key = env->fd_string();
132
        int fd = static_cast<int>(
133
7203
            stdio->Get(context, fd_key).ToLocalChecked()->IntegerValue());
134
2401
        options->stdio[i].flags = UV_INHERIT_FD;
135
2401
        options->stdio[i].data.fd = fd;
136
      }
137
    }
138
1420
  }
139
140
1420
  static void Spawn(const FunctionCallbackInfo<Value>& args) {
141
1420
    Environment* env = Environment::GetCurrent(args);
142
1420
    Local<Context> context = env->context();
143
    ProcessWrap* wrap;
144
2840
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
145
146
4260
    Local<Object> js_options = args[0]->ToObject(env->isolate());
147
148
    uv_process_options_t options;
149
1420
    memset(&options, 0, sizeof(uv_process_options_t));
150
151
1420
    options.exit_cb = OnExit;
152
153
    // options.uid
154
    Local<Value> uid_v =
155
4260
        js_options->Get(context, env->uid_string()).ToLocalChecked();
156

2842
    if (!uid_v->IsUndefined() && !uid_v->IsNull()) {
157
1
      CHECK(uid_v->IsInt32());
158
2
      const int32_t uid = uid_v->Int32Value(context).FromJust();
159
1
      options.flags |= UV_PROCESS_SETUID;
160
1
      options.uid = static_cast<uv_uid_t>(uid);
161
    }
162
163
    // options.gid
164
    Local<Value> gid_v =
165
4260
        js_options->Get(context, env->gid_string()).ToLocalChecked();
166

2842
    if (!gid_v->IsUndefined() && !gid_v->IsNull()) {
167
1
      CHECK(gid_v->IsInt32());
168
2
      const int32_t gid = gid_v->Int32Value(context).FromJust();
169
1
      options.flags |= UV_PROCESS_SETGID;
170
1
      options.gid = static_cast<uv_gid_t>(gid);
171
    }
172
173
    // TODO(bnoordhuis) is this possible to do without mallocing ?
174
175
    // options.file
176
    Local<Value> file_v =
177
4260
        js_options->Get(context, env->file_string()).ToLocalChecked();
178
2840
    CHECK(file_v->IsString());
179
1420
    node::Utf8Value file(env->isolate(), file_v);
180
1420
    options.file = *file;
181
182
    // options.args
183
    Local<Value> argv_v =
184
4260
        js_options->Get(context, env->args_string()).ToLocalChecked();
185

2840
    if (!argv_v.IsEmpty() && argv_v->IsArray()) {
186
1420
      Local<Array> js_argv = Local<Array>::Cast(argv_v);
187
1420
      int argc = js_argv->Length();
188
1420
      CHECK_GT(argc + 1, 0);  // Check for overflow.
189
190
      // Heap allocate to detect errors. +1 is for nullptr.
191
1420
      options.args = new char*[argc + 1];
192
9244
      for (int i = 0; i < argc; i++) {
193
        node::Utf8Value arg(env->isolate(),
194
23472
                            js_argv->Get(context, i).ToLocalChecked());
195
7824
        options.args[i] = strdup(*arg);
196
7824
        CHECK_NE(options.args[i], nullptr);
197
7824
      }
198
1420
      options.args[argc] = nullptr;
199
    }
200
201
    // options.cwd
202
    Local<Value> cwd_v =
203
4260
        js_options->Get(context, env->cwd_string()).ToLocalChecked();
204
    node::Utf8Value cwd(env->isolate(),
205

5671
                        cwd_v->IsString() ? cwd_v : Local<Value>());
206
1420
    if (cwd.length() > 0) {
207
8
      options.cwd = *cwd;
208
    }
209
210
    // options.env
211
    Local<Value> env_v =
212
4260
        js_options->Get(context, env->env_pairs_string()).ToLocalChecked();
213

2840
    if (!env_v.IsEmpty() && env_v->IsArray()) {
214
1418
      Local<Array> env_opt = Local<Array>::Cast(env_v);
215
1418
      int envc = env_opt->Length();
216
1418
      CHECK_GT(envc + 1, 0);  // Check for overflow.
217
1418
      options.env = new char*[envc + 1];  // Heap allocated to detect errors.
218
87786
      for (int i = 0; i < envc; i++) {
219
        node::Utf8Value pair(env->isolate(),
220
259104
                             env_opt->Get(context, i).ToLocalChecked());
221
86368
        options.env[i] = strdup(*pair);
222
86368
        CHECK_NE(options.env[i], nullptr);
223
86368
      }
224
1418
      options.env[envc] = nullptr;
225
    }
226
227
    // options.stdio
228
1420
    ParseStdioOptions(env, js_options, &options);
229
230
    // options.windowsHide
231
    Local<Value> hide_v =
232
4260
        js_options->Get(context, env->windows_hide_string()).ToLocalChecked();
233
234
1420
    if (hide_v->IsTrue()) {
235
2
      options.flags |= UV_PROCESS_WINDOWS_HIDE;
236
    }
237
238
    // options.windows_verbatim_arguments
239
    Local<Value> wva_v =
240
4260
        js_options->Get(context, env->windows_verbatim_arguments_string())
241
2840
            .ToLocalChecked();
242
243
1420
    if (wva_v->IsTrue()) {
244
      options.flags |= UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS;
245
    }
246
247
    // options.detached
248
    Local<Value> detached_v =
249
4260
        js_options->Get(context, env->detached_string()).ToLocalChecked();
250
251
1420
    if (detached_v->IsTrue()) {
252
4
      options.flags |= UV_PROCESS_DETACHED;
253
    }
254
255
1420
    int err = uv_spawn(env->event_loop(), &wrap->process_, &options);
256
257
1420
    if (err == 0) {
258
1413
      CHECK_EQ(wrap->process_.data, wrap);
259
1413
      wrap->object()->Set(context, env->pid_string(),
260
                          Integer::New(env->isolate(),
261
7065
                                       wrap->process_.pid)).FromJust();
262
    }
263
264
1420
    if (options.args) {
265
1420
      for (int i = 0; options.args[i]; i++) free(options.args[i]);
266
1420
      delete [] options.args;
267
    }
268
269
1420
    if (options.env) {
270
1418
      for (int i = 0; options.env[i]; i++) free(options.env[i]);
271
1418
      delete [] options.env;
272
    }
273
274
1420
    delete[] options.stdio;
275
276
4260
    args.GetReturnValue().Set(err);
277
  }
278
279
154
  static void Kill(const FunctionCallbackInfo<Value>& args) {
280
154
    Environment* env = Environment::GetCurrent(args);
281
    ProcessWrap* wrap;
282
308
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
283
616
    int signal = args[0]->Int32Value(env->context()).FromJust();
284
154
    int err = uv_process_kill(&wrap->process_, signal);
285
308
    args.GetReturnValue().Set(err);
286
  }
287
288
1395
  static void OnExit(uv_process_t* handle,
289
                     int64_t exit_status,
290
                     int term_signal) {
291
1395
    ProcessWrap* wrap = static_cast<ProcessWrap*>(handle->data);
292
1395
    CHECK_NE(wrap, nullptr);
293
1395
    CHECK_EQ(&wrap->process_, handle);
294
295
1395
    Environment* env = wrap->env();
296
1395
    HandleScope handle_scope(env->isolate());
297
1395
    Context::Scope context_scope(env->context());
298
299
    Local<Value> argv[] = {
300
      Number::New(env->isolate(), static_cast<double>(exit_status)),
301
      OneByteString(env->isolate(), signo_string(term_signal))
302
4185
    };
303
304
2778
    wrap->MakeCallback(env->onexit_string(), arraysize(argv), argv);
305
1383
  }
306
307
  uv_process_t process_;
308
};
309
310
311
}  // anonymous namespace
312
}  // namespace node
313
314
3332
NODE_BUILTIN_MODULE_CONTEXT_AWARE(process_wrap, node::ProcessWrap::Initialize)