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: 145 149 97.3 %
Date: 2019-01-07 12:15:22 Branches: 72 102 70.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 "node_internals.h"
24
#include "stream_base-inl.h"
25
#include "stream_wrap.h"
26
#include "util-inl.h"
27
28
#include <string.h>
29
#include <stdlib.h>
30
31
namespace node {
32
33
using v8::Array;
34
using v8::Context;
35
using v8::FunctionCallbackInfo;
36
using v8::FunctionTemplate;
37
using v8::HandleScope;
38
using v8::Int32;
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
2544
class ProcessWrap : public HandleWrap {
49
 public:
50
3130
  static void Initialize(Local<Object> target,
51
                         Local<Value> unused,
52
                         Local<Context> context,
53
                         void* priv) {
54
3130
    Environment* env = Environment::GetCurrent(context);
55
3130
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
56
6260
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
57
    Local<String> processString =
58
3130
        FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
59
3130
    constructor->SetClassName(processString);
60
61
6260
    constructor->Inherit(HandleWrap::GetConstructorTemplate(env));
62
63
3130
    env->SetProtoMethod(constructor, "spawn", Spawn);
64
3130
    env->SetProtoMethod(constructor, "kill", Kill);
65
66
    target->Set(env->context(),
67
                processString,
68
12520
                constructor->GetFunction(context).ToLocalChecked()).FromJust();
69
3130
  }
70
71
  SET_NO_MEMORY_INFO()
72
  SET_MEMORY_INFO_NAME(ProcessWrap)
73
  SET_SELF_SIZE(ProcessWrap)
74
75
 private:
76
1305
  static void New(const FunctionCallbackInfo<Value>& args) {
77
    // This constructor should not be exposed to public javascript.
78
    // Therefore we assert that we are not trying to call this as a
79
    // normal function.
80
1305
    CHECK(args.IsConstructCall());
81
1305
    Environment* env = Environment::GetCurrent(args);
82
1305
    new ProcessWrap(env, args.This());
83
1305
  }
84
85
1305
  ProcessWrap(Environment* env, Local<Object> object)
86
      : HandleWrap(env,
87
                   object,
88
                   reinterpret_cast<uv_handle_t*>(&process_),
89
1305
                   AsyncWrap::PROVIDER_PROCESSWRAP) {
90
1305
    MarkAsUninitialized();
91
1305
  }
92
93
2572
  static uv_stream_t* StreamForWrap(Environment* env, Local<Object> stdio) {
94
2572
    Local<String> handle_key = env->handle_string();
95
    // This property has always been set by JS land if we are in this code path.
96
    Local<Object> handle =
97
10288
        stdio->Get(env->context(), handle_key).ToLocalChecked().As<Object>();
98
99
2572
    uv_stream_t* stream = LibuvStreamWrap::From(env, handle)->stream();
100
2572
    CHECK_NOT_NULL(stream);
101
2572
    return stream;
102
  }
103
104
1299
  static void ParseStdioOptions(Environment* env,
105
                                Local<Object> js_options,
106
                                uv_process_options_t* options) {
107
1299
    Local<Context> context = env->context();
108
1299
    Local<String> stdio_key = env->stdio_string();
109
    Local<Array> stdios =
110
3897
        js_options->Get(context, stdio_key).ToLocalChecked().As<Array>();
111
112
1299
    uint32_t len = stdios->Length();
113
1299
    options->stdio = new uv_stdio_container_t[len];
114
1299
    options->stdio_count = len;
115
116
5603
    for (uint32_t i = 0; i < len; i++) {
117
      Local<Object> stdio =
118
12912
          stdios->Get(context, i).ToLocalChecked().As<Object>();
119
      Local<Value> type =
120
12912
          stdio->Get(context, env->type_string()).ToLocalChecked();
121
122
8608
      if (type->StrictEquals(env->ignore_string())) {
123
44
        options->stdio[i].flags = UV_IGNORE;
124
8520
      } else if (type->StrictEquals(env->pipe_string())) {
125
2571
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
126
2571
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
127
2571
        options->stdio[i].data.stream = StreamForWrap(env, stdio);
128
3378
      } else if (type->StrictEquals(env->wrap_string())) {
129
1
        options->stdio[i].flags = UV_INHERIT_STREAM;
130
1
        options->stdio[i].data.stream = StreamForWrap(env, stdio);
131
      } else {
132
1688
        Local<String> fd_key = env->fd_string();
133
3376
        Local<Value> fd_value = stdio->Get(context, fd_key).ToLocalChecked();
134
1688
        CHECK(fd_value->IsNumber());
135
3376
        int fd = static_cast<int>(fd_value.As<Integer>()->Value());
136
1688
        options->stdio[i].flags = UV_INHERIT_FD;
137
1688
        options->stdio[i].data.fd = fd;
138
      }
139
    }
140
1299
  }
141
142
1299
  static void Spawn(const FunctionCallbackInfo<Value>& args) {
143
1299
    Environment* env = Environment::GetCurrent(args);
144
1299
    Local<Context> context = env->context();
145
    ProcessWrap* wrap;
146
2598
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
147
148
    Local<Object> js_options =
149
5196
        args[0]->ToObject(env->context()).ToLocalChecked();
150
151
    uv_process_options_t options;
152
1299
    memset(&options, 0, sizeof(uv_process_options_t));
153
154
1299
    options.exit_cb = OnExit;
155
156
    // options.uid
157
    Local<Value> uid_v =
158
3897
        js_options->Get(context, env->uid_string()).ToLocalChecked();
159

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

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

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

5161
                        cwd_v->IsString() ? cwd_v : Local<Value>());
209
1299
    if (cwd.length() > 0) {
210
34
      options.cwd = *cwd;
211
    }
212
213
    // options.env
214
    Local<Value> env_v =
215
3897
        js_options->Get(context, env->env_pairs_string()).ToLocalChecked();
216

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