GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/process_wrap.cc Lines: 148 149 99.3 %
Date: 2020-08-22 22:13:06 Branches: 70 100 70.0 %

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 "stream_base-inl.h"
24
#include "stream_wrap.h"
25
#include "util-inl.h"
26
27
#include <cstring>
28
#include <cstdlib>
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::Int32;
38
using v8::Integer;
39
using v8::Local;
40
using v8::Number;
41
using v8::Object;
42
using v8::String;
43
using v8::Value;
44
45
namespace {
46
47
3174
class ProcessWrap : public HandleWrap {
48
 public:
49
3921
  static void Initialize(Local<Object> target,
50
                         Local<Value> unused,
51
                         Local<Context> context,
52
                         void* priv) {
53
3921
    Environment* env = Environment::GetCurrent(context);
54
3921
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
55
11763
    constructor->InstanceTemplate()->SetInternalFieldCount(
56
3921
        ProcessWrap::kInternalFieldCount);
57
    Local<String> processString =
58
3921
        FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
59
3921
    constructor->SetClassName(processString);
60
61
7842
    constructor->Inherit(HandleWrap::GetConstructorTemplate(env));
62
63
3921
    env->SetProtoMethod(constructor, "spawn", Spawn);
64
3921
    env->SetProtoMethod(constructor, "kill", Kill);
65
66
7842
    target->Set(env->context(),
67
                processString,
68
15684
                constructor->GetFunction(context).ToLocalChecked()).Check();
69
3921
  }
70
71
1
  SET_NO_MEMORY_INFO()
72
1
  SET_MEMORY_INFO_NAME(ProcessWrap)
73
1
  SET_SELF_SIZE(ProcessWrap)
74
75
 private:
76
1607
  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
1607
    CHECK(args.IsConstructCall());
81
1607
    Environment* env = Environment::GetCurrent(args);
82
1607
    new ProcessWrap(env, args.This());
83
1607
  }
84
85
1607
  ProcessWrap(Environment* env, Local<Object> object)
86
1607
      : HandleWrap(env,
87
                   object,
88
1607
                   reinterpret_cast<uv_handle_t*>(&process_),
89
1607
                   AsyncWrap::PROVIDER_PROCESSWRAP) {
90
1607
    MarkAsUninitialized();
91
1607
  }
92
93
3330
  static uv_stream_t* StreamForWrap(Environment* env, Local<Object> stdio) {
94
3330
    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
13320
        stdio->Get(env->context(), handle_key).ToLocalChecked().As<Object>();
98
99
3330
    uv_stream_t* stream = LibuvStreamWrap::From(env, handle)->stream();
100
3330
    CHECK_NOT_NULL(stream);
101
3330
    return stream;
102
  }
103
104
1585
  static void ParseStdioOptions(Environment* env,
105
                                Local<Object> js_options,
106
                                uv_process_options_t* options) {
107
1585
    Local<Context> context = env->context();
108
1585
    Local<String> stdio_key = env->stdio_string();
109
    Local<Array> stdios =
110
4755
        js_options->Get(context, stdio_key).ToLocalChecked().As<Array>();
111
112
1585
    uint32_t len = stdios->Length();
113
1585
    options->stdio = new uv_stdio_container_t[len];
114
1585
    options->stdio_count = len;
115
116
6751
    for (uint32_t i = 0; i < len; i++) {
117
      Local<Object> stdio =
118
15498
          stdios->Get(context, i).ToLocalChecked().As<Object>();
119
      Local<Value> type =
120
15498
          stdio->Get(context, env->type_string()).ToLocalChecked();
121
122
10332
      if (type->StrictEquals(env->ignore_string())) {
123
52
        options->stdio[i].flags = UV_IGNORE;
124
10228
      } else if (type->StrictEquals(env->pipe_string())) {
125
3323
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
126
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
127
3323
        options->stdio[i].data.stream = StreamForWrap(env, stdio);
128
3582
      } else if (type->StrictEquals(env->wrap_string())) {
129
7
        options->stdio[i].flags = UV_INHERIT_STREAM;
130
7
        options->stdio[i].data.stream = StreamForWrap(env, stdio);
131
      } else {
132
1784
        Local<String> fd_key = env->fd_string();
133
3568
        Local<Value> fd_value = stdio->Get(context, fd_key).ToLocalChecked();
134
1784
        CHECK(fd_value->IsNumber());
135
3568
        int fd = static_cast<int>(fd_value.As<Integer>()->Value());
136
1784
        options->stdio[i].flags = UV_INHERIT_FD;
137
1784
        options->stdio[i].data.fd = fd;
138
      }
139
    }
140
1585
  }
141
142
1585
  static void Spawn(const FunctionCallbackInfo<Value>& args) {
143
1585
    Environment* env = Environment::GetCurrent(args);
144
1585
    Local<Context> context = env->context();
145
    ProcessWrap* wrap;
146
1585
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
147
148
    Local<Object> js_options =
149
6340
        args[0]->ToObject(env->context()).ToLocalChecked();
150
151
    uv_process_options_t options;
152
1585
    memset(&options, 0, sizeof(uv_process_options_t));
153
154
1585
    options.exit_cb = OnExit;
155
156
    // options.uid
157
    Local<Value> uid_v =
158
4755
        js_options->Get(context, env->uid_string()).ToLocalChecked();
159

3172
    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
4755
        js_options->Get(context, env->gid_string()).ToLocalChecked();
169

3172
    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
4755
        js_options->Get(context, env->file_string()).ToLocalChecked();
181
3170
    CHECK(file_v->IsString());
182
3170
    node::Utf8Value file(env->isolate(), file_v);
183
1585
    options.file = *file;
184
185
    // options.args
186
    Local<Value> argv_v =
187
4755
        js_options->Get(context, env->args_string()).ToLocalChecked();
188

3170
    if (!argv_v.IsEmpty() && argv_v->IsArray()) {
189
1585
      Local<Array> js_argv = Local<Array>::Cast(argv_v);
190
1585
      int argc = js_argv->Length();
191
1585
      CHECK_GT(argc + 1, 0);  // Check for overflow.
192
193
      // Heap allocate to detect errors. +1 is for nullptr.
194
1585
      options.args = new char*[argc + 1];
195
8618
      for (int i = 0; i < argc; i++) {
196
        node::Utf8Value arg(env->isolate(),
197
28132
                            js_argv->Get(context, i).ToLocalChecked());
198
7033
        options.args[i] = strdup(*arg);
199
7033
        CHECK_NOT_NULL(options.args[i]);
200
      }
201
1585
      options.args[argc] = nullptr;
202
    }
203
204
    // options.cwd
205
    Local<Value> cwd_v =
206
4755
        js_options->Get(context, env->cwd_string()).ToLocalChecked();
207
    node::Utf8Value cwd(env->isolate(),
208
6249
                        cwd_v->IsString() ? cwd_v : Local<Value>());
209
1585
    if (cwd.length() > 0) {
210
90
      options.cwd = *cwd;
211
    }
212
213
    // options.env
214
    Local<Value> env_v =
215
4755
        js_options->Get(context, env->env_pairs_string()).ToLocalChecked();
216

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

17859
NODE_MODULE_CONTEXT_AWARE_INTERNAL(process_wrap, node::ProcessWrap::Initialize)