GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: process_wrap.cc Lines: 148 150 98.7 %
Date: 2022-09-25 04:23:55 Branches: 71 100 71.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::Isolate;
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
class ProcessWrap : public HandleWrap {
49
 public:
50
4887
  static void Initialize(Local<Object> target,
51
                         Local<Value> unused,
52
                         Local<Context> context,
53
                         void* priv) {
54
4887
    Environment* env = Environment::GetCurrent(context);
55
4887
    Isolate* isolate = env->isolate();
56
4887
    Local<FunctionTemplate> constructor = NewFunctionTemplate(isolate, New);
57
9774
    constructor->InstanceTemplate()->SetInternalFieldCount(
58
        ProcessWrap::kInternalFieldCount);
59
60
4887
    constructor->Inherit(HandleWrap::GetConstructorTemplate(env));
61
62
4887
    SetProtoMethod(isolate, constructor, "spawn", Spawn);
63
4887
    SetProtoMethod(isolate, constructor, "kill", Kill);
64
65
4887
    SetConstructorFunction(context, target, "Process", constructor);
66
4887
  }
67
68
1
  SET_NO_MEMORY_INFO()
69
9
  SET_MEMORY_INFO_NAME(ProcessWrap)
70
1
  SET_SELF_SIZE(ProcessWrap)
71
72
 private:
73
2073
  static void New(const FunctionCallbackInfo<Value>& args) {
74
    // This constructor should not be exposed to public javascript.
75
    // Therefore we assert that we are not trying to call this as a
76
    // normal function.
77
2073
    CHECK(args.IsConstructCall());
78
2073
    Environment* env = Environment::GetCurrent(args);
79
2073
    new ProcessWrap(env, args.This());
80
2073
  }
81
82
2073
  ProcessWrap(Environment* env, Local<Object> object)
83
2073
      : HandleWrap(env,
84
                   object,
85
2073
                   reinterpret_cast<uv_handle_t*>(&process_),
86
2073
                   AsyncWrap::PROVIDER_PROCESSWRAP) {
87
2073
    MarkAsUninitialized();
88
2073
  }
89
90
4450
  static uv_stream_t* StreamForWrap(Environment* env, Local<Object> stdio) {
91
4450
    Local<String> handle_key = env->handle_string();
92
    // This property has always been set by JS land if we are in this code path.
93
    Local<Object> handle =
94
8900
        stdio->Get(env->context(), handle_key).ToLocalChecked().As<Object>();
95
96
4450
    uv_stream_t* stream = LibuvStreamWrap::From(env, handle)->stream();
97
4450
    CHECK_NOT_NULL(stream);
98
4450
    return stream;
99
  }
100
101
2063
  static void ParseStdioOptions(Environment* env,
102
                                Local<Object> js_options,
103
                                uv_process_options_t* options) {
104
2063
    Local<Context> context = env->context();
105
2063
    Local<String> stdio_key = env->stdio_string();
106
    Local<Array> stdios =
107
6189
        js_options->Get(context, stdio_key).ToLocalChecked().As<Array>();
108
109
2063
    uint32_t len = stdios->Length();
110
2063
    options->stdio = new uv_stdio_container_t[len];
111
2063
    options->stdio_count = len;
112
113
8796
    for (uint32_t i = 0; i < len; i++) {
114
      Local<Object> stdio =
115
20199
          stdios->Get(context, i).ToLocalChecked().As<Object>();
116
      Local<Value> type =
117
20199
          stdio->Get(context, env->type_string()).ToLocalChecked();
118
119
13466
      if (type->StrictEquals(env->ignore_string())) {
120
56
        options->stdio[i].flags = UV_IGNORE;
121
13354
      } else if (type->StrictEquals(env->pipe_string())) {
122
4442
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
123
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
124
4442
        options->stdio[i].data.stream = StreamForWrap(env, stdio);
125
4470
      } else if (type->StrictEquals(env->overlapped_string())) {
126
1
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
127
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE |
128
            UV_OVERLAPPED_PIPE);
129
1
        options->stdio[i].data.stream = StreamForWrap(env, stdio);
130
4468
      } else if (type->StrictEquals(env->wrap_string())) {
131
7
        options->stdio[i].flags = UV_INHERIT_STREAM;
132
7
        options->stdio[i].data.stream = StreamForWrap(env, stdio);
133
      } else {
134
2227
        Local<String> fd_key = env->fd_string();
135
4454
        Local<Value> fd_value = stdio->Get(context, fd_key).ToLocalChecked();
136
2227
        CHECK(fd_value->IsNumber());
137
2227
        int fd = static_cast<int>(fd_value.As<Integer>()->Value());
138
2227
        options->stdio[i].flags = UV_INHERIT_FD;
139
2227
        options->stdio[i].data.fd = fd;
140
      }
141
    }
142
2063
  }
143
144
2063
  static void Spawn(const FunctionCallbackInfo<Value>& args) {
145
2063
    Environment* env = Environment::GetCurrent(args);
146
2063
    Local<Context> context = env->context();
147
    ProcessWrap* wrap;
148
2063
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
149
150
    Local<Object> js_options =
151
2063
        args[0]->ToObject(env->context()).ToLocalChecked();
152
153
    uv_process_options_t options;
154
2063
    memset(&options, 0, sizeof(uv_process_options_t));
155
156
2063
    options.exit_cb = OnExit;
157
158
    // options.uid
159
    Local<Value> uid_v =
160
6189
        js_options->Get(context, env->uid_string()).ToLocalChecked();
161

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

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

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

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