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: 2019-10-08 22:34:21 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 "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
4376
class ProcessWrap : public HandleWrap {
48
 public:
49
4360
  static void Initialize(Local<Object> target,
50
                         Local<Value> unused,
51
                         Local<Context> context,
52
                         void* priv) {
53
4360
    Environment* env = Environment::GetCurrent(context);
54
4360
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
55
8720
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
56
    Local<String> processString =
57
4360
        FIXED_ONE_BYTE_STRING(env->isolate(), "Process");
58
4360
    constructor->SetClassName(processString);
59
60
8720
    constructor->Inherit(HandleWrap::GetConstructorTemplate(env));
61
62
4360
    env->SetProtoMethod(constructor, "spawn", Spawn);
63
4360
    env->SetProtoMethod(constructor, "kill", Kill);
64
65
    target->Set(env->context(),
66
                processString,
67
17440
                constructor->GetFunction(context).ToLocalChecked()).Check();
68
4360
  }
69
70
1
  SET_NO_MEMORY_INFO()
71
1
  SET_MEMORY_INFO_NAME(ProcessWrap)
72
1
  SET_SELF_SIZE(ProcessWrap)
73
74
 private:
75
2213
  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
2213
    CHECK(args.IsConstructCall());
80
2213
    Environment* env = Environment::GetCurrent(args);
81
2213
    new ProcessWrap(env, args.This());
82
2213
  }
83
84
2213
  ProcessWrap(Environment* env, Local<Object> object)
85
      : HandleWrap(env,
86
                   object,
87
                   reinterpret_cast<uv_handle_t*>(&process_),
88
2213
                   AsyncWrap::PROVIDER_PROCESSWRAP) {
89
2213
    MarkAsUninitialized();
90
2213
  }
91
92
3986
  static uv_stream_t* StreamForWrap(Environment* env, Local<Object> stdio) {
93
3986
    Local<String> handle_key = env->handle_string();
94
    // This property has always been set by JS land if we are in this code path.
95
    Local<Object> handle =
96
15944
        stdio->Get(env->context(), handle_key).ToLocalChecked().As<Object>();
97
98
3986
    uv_stream_t* stream = LibuvStreamWrap::From(env, handle)->stream();
99
3986
    CHECK_NOT_NULL(stream);
100
3986
    return stream;
101
  }
102
103
2195
  static void ParseStdioOptions(Environment* env,
104
                                Local<Object> js_options,
105
                                uv_process_options_t* options) {
106
2195
    Local<Context> context = env->context();
107
2195
    Local<String> stdio_key = env->stdio_string();
108
    Local<Array> stdios =
109
6585
        js_options->Get(context, stdio_key).ToLocalChecked().As<Array>();
110
111
2195
    uint32_t len = stdios->Length();
112
2195
    options->stdio = new uv_stdio_container_t[len];
113
2195
    options->stdio_count = len;
114
115
9691
    for (uint32_t i = 0; i < len; i++) {
116
      Local<Object> stdio =
117
22488
          stdios->Get(context, i).ToLocalChecked().As<Object>();
118
      Local<Value> type =
119
22488
          stdio->Get(context, env->type_string()).ToLocalChecked();
120
121
14992
      if (type->StrictEquals(env->ignore_string())) {
122
61
        options->stdio[i].flags = UV_IGNORE;
123
14870
      } else if (type->StrictEquals(env->pipe_string())) {
124
3979
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
125
3979
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
126
3979
        options->stdio[i].data.stream = StreamForWrap(env, stdio);
127
6912
      } else if (type->StrictEquals(env->wrap_string())) {
128
7
        options->stdio[i].flags = UV_INHERIT_STREAM;
129
7
        options->stdio[i].data.stream = StreamForWrap(env, stdio);
130
      } else {
131
3449
        Local<String> fd_key = env->fd_string();
132
6898
        Local<Value> fd_value = stdio->Get(context, fd_key).ToLocalChecked();
133
3449
        CHECK(fd_value->IsNumber());
134
6898
        int fd = static_cast<int>(fd_value.As<Integer>()->Value());
135
3449
        options->stdio[i].flags = UV_INHERIT_FD;
136
3449
        options->stdio[i].data.fd = fd;
137
      }
138
    }
139
2195
  }
140
141
2195
  static void Spawn(const FunctionCallbackInfo<Value>& args) {
142
2195
    Environment* env = Environment::GetCurrent(args);
143
2195
    Local<Context> context = env->context();
144
    ProcessWrap* wrap;
145
4390
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
146
147
    Local<Object> js_options =
148
8780
        args[0]->ToObject(env->context()).ToLocalChecked();
149
150
    uv_process_options_t options;
151
2195
    memset(&options, 0, sizeof(uv_process_options_t));
152
153
2195
    options.exit_cb = OnExit;
154
155
    // options.uid
156
    Local<Value> uid_v =
157
6585
        js_options->Get(context, env->uid_string()).ToLocalChecked();
158

4392
    if (!uid_v->IsUndefined() && !uid_v->IsNull()) {
159
1
      CHECK(uid_v->IsInt32());
160
2
      const int32_t uid = uid_v.As<Int32>()->Value();
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
6585
        js_options->Get(context, env->gid_string()).ToLocalChecked();
168

4392
    if (!gid_v->IsUndefined() && !gid_v->IsNull()) {
169
1
      CHECK(gid_v->IsInt32());
170
2
      const int32_t gid = gid_v.As<Int32>()->Value();
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
6585
        js_options->Get(context, env->file_string()).ToLocalChecked();
180
4390
    CHECK(file_v->IsString());
181
2195
    node::Utf8Value file(env->isolate(), file_v);
182
2195
    options.file = *file;
183
184
    // options.args
185
    Local<Value> argv_v =
186
6585
        js_options->Get(context, env->args_string()).ToLocalChecked();
187

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

8698
                        cwd_v->IsString() ? cwd_v : Local<Value>());
208
2195
    if (cwd.length() > 0) {
209
81
      options.cwd = *cwd;
210
    }
211
212
    // options.env
213
    Local<Value> env_v =
214
6585
        js_options->Get(context, env->env_pairs_string()).ToLocalChecked();
215

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