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: 139 147 94.6 %
Date: 2017-10-21 Branches: 68 98 69.4 %

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

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

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

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

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

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