GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/process_wrap.cc Lines: 128 136 94.1 %
Date: 2016-12-18 Branches: 62 94 66.0 %

Line Branch Exec Source
1
#include "env.h"
2
#include "env-inl.h"
3
#include "handle_wrap.h"
4
#include "node_wrap.h"
5
#include "util.h"
6
#include "util-inl.h"
7
8
#include <string.h>
9
#include <stdlib.h>
10
11
namespace node {
12
13
using v8::Array;
14
using v8::Context;
15
using v8::FunctionCallbackInfo;
16
using v8::FunctionTemplate;
17
using v8::HandleScope;
18
using v8::Integer;
19
using v8::Local;
20
using v8::Number;
21
using v8::Object;
22
using v8::String;
23
using v8::Value;
24
25
670
class ProcessWrap : public HandleWrap {
26
 public:
27
1595
  static void Initialize(Local<Object> target,
28
                         Local<Value> unused,
29
                         Local<Context> context) {
30
1595
    Environment* env = Environment::GetCurrent(context);
31
3190
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
32
3190
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
33
1595
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"));
34
35
1595
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
36
37
1595
    env->SetProtoMethod(constructor, "spawn", Spawn);
38
1595
    env->SetProtoMethod(constructor, "kill", Kill);
39
40
1595
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
41
1595
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
42
1595
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
43
44
7975
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"),
45
1595
                constructor->GetFunction());
46
1595
  }
47
48
  size_t self_size() const override { return sizeof(*this); }
49
50
 private:
51
702
  static void New(const FunctionCallbackInfo<Value>& args) {
52
    // This constructor should not be exposed to public javascript.
53
    // Therefore we assert that we are not trying to call this as a
54
    // normal function.
55
702
    CHECK(args.IsConstructCall());
56
702
    Environment* env = Environment::GetCurrent(args);
57
1404
    new ProcessWrap(env, args.This());
58
702
  }
59
60
  ProcessWrap(Environment* env, Local<Object> object)
61
      : HandleWrap(env,
62
                   object,
63
                   reinterpret_cast<uv_handle_t*>(&process_),
64
702
                   AsyncWrap::PROVIDER_PROCESSWRAP) {
65
  }
66
67
699
  static void ParseStdioOptions(Environment* env,
68
                                Local<Object> js_options,
69
                                uv_process_options_t* options) {
70
699
    Local<String> stdio_key = env->stdio_string();
71
2097
    Local<Array> stdios = js_options->Get(stdio_key).As<Array>();
72
73
699
    uint32_t len = stdios->Length();
74
699
    options->stdio = new uv_stdio_container_t[len];
75
699
    options->stdio_count = len;
76
77
3055
    for (uint32_t i = 0; i < len; i++) {
78
4712
      Local<Object> stdio = stdios->Get(i).As<Object>();
79
4712
      Local<Value> type = stdio->Get(env->type_string());
80
81
4712
      if (type->Equals(env->ignore_string())) {
82
30
        options->stdio[i].flags = UV_IGNORE;
83
4652
      } else if (type->Equals(env->pipe_string())) {
84
1406
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
85
1406
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
86
1406
        Local<String> handle_key = env->handle_string();
87
2812
        Local<Object> handle = stdio->Get(handle_key).As<Object>();
88
1406
        CHECK(!handle.IsEmpty());
89
1406
        options->stdio[i].data.stream =
90
            reinterpret_cast<uv_stream_t*>(
91
2812
                Unwrap<PipeWrap>(handle)->UVHandle());
92
1840
      } else if (type->Equals(env->wrap_string())) {
93
        Local<String> handle_key = env->handle_string();
94
        Local<Object> handle = stdio->Get(handle_key).As<Object>();
95
        uv_stream_t* stream = HandleToStream(env, handle);
96
        CHECK_NE(stream, nullptr);
97
98
        options->stdio[i].flags = UV_INHERIT_STREAM;
99
        options->stdio[i].data.stream = stream;
100
      } else {
101
920
        Local<String> fd_key = env->fd_string();
102
1840
        int fd = static_cast<int>(stdio->Get(fd_key)->IntegerValue());
103
920
        options->stdio[i].flags = UV_INHERIT_FD;
104
920
        options->stdio[i].data.fd = fd;
105
      }
106
    }
107
699
  }
108
109
700
  static void Spawn(const FunctionCallbackInfo<Value>& args) {
110
700
    Environment* env = Environment::GetCurrent(args);
111
112
    ProcessWrap* wrap;
113
701
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
114
115
1400
    Local<Object> js_options = args[0]->ToObject(env->isolate());
116
117
    uv_process_options_t options;
118
700
    memset(&options, 0, sizeof(uv_process_options_t));
119
120
700
    options.exit_cb = OnExit;
121
122
    // options.uid
123
1400
    Local<Value> uid_v = js_options->Get(env->uid_string());
124
700
    if (uid_v->IsInt32()) {
125
2
      const int32_t uid = uid_v->Int32Value(env->context()).FromJust();
126
1
      options.flags |= UV_PROCESS_SETUID;
127
1
      options.uid = static_cast<uv_uid_t>(uid);
128

1398
    } else if (!uid_v->IsUndefined() && !uid_v->IsNull()) {
129
      return env->ThrowTypeError("options.uid should be a number");
130
    }
131
132
    // options.gid
133
1400
    Local<Value> gid_v = js_options->Get(env->gid_string());
134
700
    if (gid_v->IsInt32()) {
135
2
      const int32_t gid = gid_v->Int32Value(env->context()).FromJust();
136
1
      options.flags |= UV_PROCESS_SETGID;
137
1
      options.gid = static_cast<uv_gid_t>(gid);
138

1398
    } else if (!gid_v->IsUndefined() && !gid_v->IsNull()) {
139
      return env->ThrowTypeError("options.gid should be a number");
140
    }
141
142
    // TODO(bnoordhuis) is this possible to do without mallocing ?
143
144
    // options.file
145
1400
    Local<Value> file_v = js_options->Get(env->file_string());
146
    node::Utf8Value file(env->isolate(),
147
2099
                         file_v->IsString() ? file_v : Local<Value>());
148
700
    if (file.length() > 0) {
149
699
      options.file = *file;
150
    } else {
151
1
      return env->ThrowTypeError("Bad argument");
152
    }
153
154
    // options.args
155
1398
    Local<Value> argv_v = js_options->Get(env->args_string());
156

1398
    if (!argv_v.IsEmpty() && argv_v->IsArray()) {
157
699
      Local<Array> js_argv = Local<Array>::Cast(argv_v);
158
699
      int argc = js_argv->Length();
159
      // Heap allocate to detect errors. +1 is for nullptr.
160
699
      options.args = new char*[argc + 1];
161
3279
      for (int i = 0; i < argc; i++) {
162
5160
        node::Utf8Value arg(env->isolate(), js_argv->Get(i));
163
2580
        options.args[i] = strdup(*arg);
164
2580
        CHECK_NE(options.args[i], nullptr);
165
      }
166
699
      options.args[argc] = nullptr;
167
    }
168
169
    // options.cwd
170
1398
    Local<Value> cwd_v = js_options->Get(env->cwd_string());
171
    node::Utf8Value cwd(env->isolate(),
172
2097
                        cwd_v->IsString() ? cwd_v : Local<Value>());
173
699
    if (cwd.length() > 0) {
174
8
      options.cwd = *cwd;
175
    }
176
177
    // options.env
178
1398
    Local<Value> env_v = js_options->Get(env->env_pairs_string());
179

699
    if (!env_v.IsEmpty() && env_v->IsArray()) {
180
697
      Local<Array> env_opt = Local<Array>::Cast(env_v);
181
697
      int envc = env_opt->Length();
182
697
      options.env = new char*[envc + 1];  // Heap allocated to detect errors.
183
11095
      for (int i = 0; i < envc; i++) {
184
20796
        node::Utf8Value pair(env->isolate(), env_opt->Get(i));
185
10398
        options.env[i] = strdup(*pair);
186
10398
        CHECK_NE(options.env[i], nullptr);
187
      }
188
697
      options.env[envc] = nullptr;
189
    }
190
191
    // options.stdio
192
699
    ParseStdioOptions(env, js_options, &options);
193
194
    // options.windows_verbatim_arguments
195
    Local<String> windows_verbatim_arguments_key =
196
699
        env->windows_verbatim_arguments_string();
197
1398
    if (js_options->Get(windows_verbatim_arguments_key)->IsTrue()) {
198
      options.flags |= UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS;
199
    }
200
201
    // options.detached
202
699
    Local<String> detached_key = env->detached_string();
203
1398
    if (js_options->Get(detached_key)->IsTrue()) {
204
8
      options.flags |= UV_PROCESS_DETACHED;
205
    }
206
207
1398
    int err = uv_spawn(env->event_loop(), &wrap->process_, &options);
208
209
699
    if (err == 0) {
210
693
      CHECK_EQ(wrap->process_.data, wrap);
211
4158
      wrap->object()->Set(env->pid_string(),
212
693
                          Integer::New(env->isolate(), wrap->process_.pid));
213
    }
214
215
699
    if (options.args) {
216
3279
      for (int i = 0; options.args[i]; i++) free(options.args[i]);
217
699
      delete [] options.args;
218
    }
219
220
699
    if (options.env) {
221
11095
      for (int i = 0; options.env[i]; i++) free(options.env[i]);
222
697
      delete [] options.env;
223
    }
224
225
699
    delete[] options.stdio;
226
227
1398
    args.GetReturnValue().Set(err);
228
  }
229
230
166
  static void Kill(const FunctionCallbackInfo<Value>& args) {
231
    ProcessWrap* wrap;
232
166
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
233
166
    int signal = args[0]->Int32Value();
234
166
    int err = uv_process_kill(&wrap->process_, signal);
235
332
    args.GetReturnValue().Set(err);
236
  }
237
238
674
  static void OnExit(uv_process_t* handle,
239
                     int64_t exit_status,
240
                     int term_signal) {
241
674
    ProcessWrap* wrap = static_cast<ProcessWrap*>(handle->data);
242
674
    CHECK_NE(wrap, nullptr);
243
674
    CHECK_EQ(&wrap->process_, handle);
244
245
674
    Environment* env = wrap->env();
246
1338
    HandleScope handle_scope(env->isolate());
247
2012
    Context::Scope context_scope(env->context());
248
249
    Local<Value> argv[] = {
250
      Number::New(env->isolate(), static_cast<double>(exit_status)),
251
      OneByteString(env->isolate(), signo_string(term_signal))
252
2022
    };
253
254
1348
    wrap->MakeCallback(env->onexit_string(), arraysize(argv), argv);
255
664
  }
256
257
  uv_process_t process_;
258
};
259
260
261
}  // namespace node
262
263
1734
NODE_MODULE_CONTEXT_AWARE_BUILTIN(process_wrap, node::ProcessWrap::Initialize)