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-12-18 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
2818
class ProcessWrap : public HandleWrap {
47
 public:
48
2940
  static void Initialize(Local<Object> target,
49
                         Local<Value> unused,
50
                         Local<Context> context) {
51
2940
    Environment* env = Environment::GetCurrent(context);
52
2940
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
53
5880
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
54
    Local<String> processString =
55
2940
        FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
56
2940
    constructor->SetClassName(processString);
57
58
2940
    AsyncWrap::AddWrapMethods(env, constructor);
59
60
2940
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
61
62
2940
    env->SetProtoMethod(constructor, "spawn", Spawn);
63
2940
    env->SetProtoMethod(constructor, "kill", Kill);
64
65
2940
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
66
2940
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
67
2940
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
68
69
5880
    target->Set(processString, constructor->GetFunction());
70
2940
  }
71
72
  size_t self_size() const override { return sizeof(*this); }
73
74
 private:
75
1438
  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
1438
    CHECK(args.IsConstructCall());
80
1438
    Environment* env = Environment::GetCurrent(args);
81
1438
    new ProcessWrap(env, args.This());
82
1438
  }
83
84
1438
  ProcessWrap(Environment* env, Local<Object> object)
85
      : HandleWrap(env,
86
                   object,
87
                   reinterpret_cast<uv_handle_t*>(&process_),
88
1438
                   AsyncWrap::PROVIDER_PROCESSWRAP) {
89
1438
  }
90
91
1432
  static void ParseStdioOptions(Environment* env,
92
                                Local<Object> js_options,
93
                                uv_process_options_t* options) {
94
1432
    Local<Context> context = env->context();
95
1432
    Local<String> stdio_key = env->stdio_string();
96
    Local<Array> stdios =
97
4296
        js_options->Get(context, stdio_key).ToLocalChecked().As<Array>();
98
99
1432
    uint32_t len = stdios->Length();
100
1432
    options->stdio = new uv_stdio_container_t[len];
101
1432
    options->stdio_count = len;
102
103
6413
    for (uint32_t i = 0; i < len; i++) {
104
      Local<Object> stdio =
105
14943
          stdios->Get(context, i).ToLocalChecked().As<Object>();
106
      Local<Value> type =
107
14943
          stdio->Get(context, env->type_string()).ToLocalChecked();
108
109
9962
      if (type->Equals(env->ignore_string())) {
110
39
        options->stdio[i].flags = UV_IGNORE;
111
9884
      } else if (type->Equals(env->pipe_string())) {
112
2532
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
113
2532
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
114
2532
        Local<String> handle_key = env->handle_string();
115
        Local<Object> handle =
116
7596
            stdio->Get(context, handle_key).ToLocalChecked().As<Object>();
117
2532
        CHECK(!handle.IsEmpty());
118
2532
        options->stdio[i].data.stream =
119
            reinterpret_cast<uv_stream_t*>(
120
2532
                Unwrap<PipeWrap>(handle)->UVHandle());
121
4820
      } 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
2410
        Local<String> fd_key = env->fd_string();
132
        int fd = static_cast<int>(
133
7230
            stdio->Get(context, fd_key).ToLocalChecked()->IntegerValue());
134
2410
        options->stdio[i].flags = UV_INHERIT_FD;
135
2410
        options->stdio[i].data.fd = fd;
136
      }
137
    }
138
1432
  }
139
140
1432
  static void Spawn(const FunctionCallbackInfo<Value>& args) {
141
1432
    Environment* env = Environment::GetCurrent(args);
142
1432
    Local<Context> context = env->context();
143
    ProcessWrap* wrap;
144
2864
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
145
146
    Local<Object> js_options =
147
5728
        args[0]->ToObject(env->context()).ToLocalChecked();
148
149
    uv_process_options_t options;
150
1432
    memset(&options, 0, sizeof(uv_process_options_t));
151
152
1432
    options.exit_cb = OnExit;
153
154
    // options.uid
155
    Local<Value> uid_v =
156
4296
        js_options->Get(context, env->uid_string()).ToLocalChecked();
157

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

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

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

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

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