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

Line 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::Function;
16
using v8::FunctionCallbackInfo;
17
using v8::FunctionTemplate;
18
using v8::HandleScope;
19
using v8::Integer;
20
using v8::Local;
21
using v8::Number;
22
using v8::Object;
23
using v8::String;
24
using v8::Value;
25
26
486
class ProcessWrap : public HandleWrap {
27
 public:
28
1430
  static void Initialize(Local<Object> target,
29
                         Local<Value> unused,
30
                         Local<Context> context) {
31
1430
    Environment* env = Environment::GetCurrent(context);
32
2860
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
33
2860
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
34
1430
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"));
35
36
1430
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
37
38
1430
    env->SetProtoMethod(constructor, "spawn", Spawn);
39
1430
    env->SetProtoMethod(constructor, "kill", Kill);
40
41
1430
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
42
1430
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
43
1430
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
44
45
7150
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"),
46
1430
                constructor->GetFunction());
47
1430
  }
48
49
  size_t self_size() const override { return sizeof(*this); }
50
51
 private:
52
516
  static void New(const FunctionCallbackInfo<Value>& args) {
53
    // This constructor should not be exposed to public javascript.
54
    // Therefore we assert that we are not trying to call this as a
55
    // normal function.
56
516
    CHECK(args.IsConstructCall());
57
516
    Environment* env = Environment::GetCurrent(args);
58
1032
    new ProcessWrap(env, args.This());
59
516
  }
60
61
  ProcessWrap(Environment* env, Local<Object> object)
62
      : HandleWrap(env,
63
                   object,
64
                   reinterpret_cast<uv_handle_t*>(&process_),
65
516
                   AsyncWrap::PROVIDER_PROCESSWRAP) {
66
  }
67
68
514
  static void ParseStdioOptions(Environment* env,
69
                                Local<Object> js_options,
70
                                uv_process_options_t* options) {
71
514
    Local<String> stdio_key = env->stdio_string();
72
1542
    Local<Array> stdios = js_options->Get(stdio_key).As<Array>();
73
74
514
    uint32_t len = stdios->Length();
75
514
    options->stdio = new uv_stdio_container_t[len];
76
514
    options->stdio_count = len;
77
78
2222
    for (uint32_t i = 0; i < len; i++) {
79
3416
      Local<Object> stdio = stdios->Get(i).As<Object>();
80
3416
      Local<Value> type = stdio->Get(env->type_string());
81
82
3416
      if (type->Equals(env->ignore_string())) {
83
29
        options->stdio[i].flags = UV_IGNORE;
84
3358
      } else if (type->Equals(env->pipe_string())) {
85
1177
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
86
1177
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
87
1177
        Local<String> handle_key = env->handle_string();
88
2354
        Local<Object> handle = stdio->Get(handle_key).As<Object>();
89
1177
        CHECK(!handle.IsEmpty());
90
1177
        options->stdio[i].data.stream =
91
            reinterpret_cast<uv_stream_t*>(
92
1177
                Unwrap<PipeWrap>(handle)->UVHandle());
93
1004
      } else if (type->Equals(env->wrap_string())) {
94
        Local<String> handle_key = env->handle_string();
95
        Local<Object> handle = stdio->Get(handle_key).As<Object>();
96
        uv_stream_t* stream = HandleToStream(env, handle);
97
        CHECK_NE(stream, nullptr);
98
99
        options->stdio[i].flags = UV_INHERIT_STREAM;
100
        options->stdio[i].data.stream = stream;
101
      } else {
102
502
        Local<String> fd_key = env->fd_string();
103
1004
        int fd = static_cast<int>(stdio->Get(fd_key)->IntegerValue());
104
502
        options->stdio[i].flags = UV_INHERIT_FD;
105
502
        options->stdio[i].data.fd = fd;
106
      }
107
    }
108
514
  }
109
110
515
  static void Spawn(const FunctionCallbackInfo<Value>& args) {
111
515
    Environment* env = Environment::GetCurrent(args);
112
113
    ProcessWrap* wrap;
114
516
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
115
116
1030
    Local<Object> js_options = args[0]->ToObject(env->isolate());
117
118
    uv_process_options_t options;
119
515
    memset(&options, 0, sizeof(uv_process_options_t));
120
121
515
    options.exit_cb = OnExit;
122
123
    // options.uid
124
1030
    Local<Value> uid_v = js_options->Get(env->uid_string());
125
515
    if (uid_v->IsInt32()) {
126
2
      const int32_t uid = uid_v->Int32Value(env->context()).FromJust();
127
1
      options.flags |= UV_PROCESS_SETUID;
128
1
      options.uid = static_cast<uv_uid_t>(uid);
129
1028
    } else if (!uid_v->IsUndefined() && !uid_v->IsNull()) {
130
      return env->ThrowTypeError("options.uid should be a number");
131
    }
132
133
    // options.gid
134
1030
    Local<Value> gid_v = js_options->Get(env->gid_string());
135
515
    if (gid_v->IsInt32()) {
136
2
      const int32_t gid = gid_v->Int32Value(env->context()).FromJust();
137
1
      options.flags |= UV_PROCESS_SETGID;
138
1
      options.gid = static_cast<uv_gid_t>(gid);
139
1028
    } else if (!gid_v->IsUndefined() && !gid_v->IsNull()) {
140
      return env->ThrowTypeError("options.gid should be a number");
141
    }
142
143
    // TODO(bnoordhuis) is this possible to do without mallocing ?
144
145
    // options.file
146
1030
    Local<Value> file_v = js_options->Get(env->file_string());
147
    node::Utf8Value file(env->isolate(),
148
1544
                         file_v->IsString() ? file_v : Local<Value>());
149
515
    if (file.length() > 0) {
150
514
      options.file = *file;
151
    } else {
152
1
      return env->ThrowTypeError("Bad argument");
153
    }
154
155
    // options.args
156
1028
    Local<Value> argv_v = js_options->Get(env->args_string());
157
1028
    if (!argv_v.IsEmpty() && argv_v->IsArray()) {
158
514
      Local<Array> js_argv = Local<Array>::Cast(argv_v);
159
514
      int argc = js_argv->Length();
160
      // Heap allocate to detect errors. +1 is for nullptr.
161
514
      options.args = new char*[argc + 1];
162
1996
      for (int i = 0; i < argc; i++) {
163
2964
        node::Utf8Value arg(env->isolate(), js_argv->Get(i));
164
1482
        options.args[i] = strdup(*arg);
165
1482
        CHECK_NE(options.args[i], nullptr);
166
      }
167
514
      options.args[argc] = nullptr;
168
    }
169
170
    // options.cwd
171
1028
    Local<Value> cwd_v = js_options->Get(env->cwd_string());
172
    node::Utf8Value cwd(env->isolate(),
173
1542
                        cwd_v->IsString() ? cwd_v : Local<Value>());
174
514
    if (cwd.length() > 0) {
175
8
      options.cwd = *cwd;
176
    }
177
178
    // options.env
179
1028
    Local<Value> env_v = js_options->Get(env->env_pairs_string());
180
514
    if (!env_v.IsEmpty() && env_v->IsArray()) {
181
512
      Local<Array> env_opt = Local<Array>::Cast(env_v);
182
512
      int envc = env_opt->Length();
183
512
      options.env = new char*[envc + 1];  // Heap allocated to detect errors.
184
7962
      for (int i = 0; i < envc; i++) {
185
14900
        node::Utf8Value pair(env->isolate(), env_opt->Get(i));
186
7450
        options.env[i] = strdup(*pair);
187
7450
        CHECK_NE(options.env[i], nullptr);
188
      }
189
512
      options.env[envc] = nullptr;
190
    }
191
192
    // options.stdio
193
514
    ParseStdioOptions(env, js_options, &options);
194
195
    // options.windows_verbatim_arguments
196
    Local<String> windows_verbatim_arguments_key =
197
514
        env->windows_verbatim_arguments_string();
198
1028
    if (js_options->Get(windows_verbatim_arguments_key)->IsTrue()) {
199
      options.flags |= UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS;
200
    }
201
202
    // options.detached
203
514
    Local<String> detached_key = env->detached_string();
204
1028
    if (js_options->Get(detached_key)->IsTrue()) {
205
8
      options.flags |= UV_PROCESS_DETACHED;
206
    }
207
208
1028
    int err = uv_spawn(env->event_loop(), &wrap->process_, &options);
209
210
514
    if (err == 0) {
211
508
      CHECK_EQ(wrap->process_.data, wrap);
212
3048
      wrap->object()->Set(env->pid_string(),
213
508
                          Integer::New(env->isolate(), wrap->process_.pid));
214
    }
215
216
514
    if (options.args) {
217
1996
      for (int i = 0; options.args[i]; i++) free(options.args[i]);
218
514
      delete [] options.args;
219
    }
220
221
514
    if (options.env) {
222
7962
      for (int i = 0; options.env[i]; i++) free(options.env[i]);
223
512
      delete [] options.env;
224
    }
225
226
514
    delete[] options.stdio;
227
228
1028
    args.GetReturnValue().Set(err);
229
  }
230
231
75
  static void Kill(const FunctionCallbackInfo<Value>& args) {
232
    ProcessWrap* wrap;
233
75
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
234
75
    int signal = args[0]->Int32Value();
235
75
    int err = uv_process_kill(&wrap->process_, signal);
236
150
    args.GetReturnValue().Set(err);
237
  }
238
239
491
  static void OnExit(uv_process_t* handle,
240
                     int64_t exit_status,
241
                     int term_signal) {
242
491
    ProcessWrap* wrap = static_cast<ProcessWrap*>(handle->data);
243
491
    CHECK_NE(wrap, nullptr);
244
491
    CHECK_EQ(&wrap->process_, handle);
245
246
491
    Environment* env = wrap->env();
247
971
    HandleScope handle_scope(env->isolate());
248
1462
    Context::Scope context_scope(env->context());
249
250
    Local<Value> argv[] = {
251
      Number::New(env->isolate(), static_cast<double>(exit_status)),
252
      OneByteString(env->isolate(), signo_string(term_signal))
253
1473
    };
254
255
982
    wrap->MakeCallback(env->onexit_string(), arraysize(argv), argv);
256
480
  }
257
258
  uv_process_t process_;
259
};
260
261
262
}  // namespace node
263
264
1565
NODE_MODULE_CONTEXT_AWARE_BUILTIN(process_wrap, node::ProcessWrap::Initialize)