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: 127 135 94.1 %
Date: 2017-01-02 Branches: 63 92 68.5 %

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
674
class ProcessWrap : public HandleWrap {
26
 public:
27
1616
  static void Initialize(Local<Object> target,
28
                         Local<Value> unused,
29
                         Local<Context> context) {
30
1616
    Environment* env = Environment::GetCurrent(context);
31
3232
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
32
3232
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
33
1616
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"));
34
35
1616
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
36
37
1616
    env->SetProtoMethod(constructor, "spawn", Spawn);
38
1616
    env->SetProtoMethod(constructor, "kill", Kill);
39
40
1616
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
41
1616
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
42
1616
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
43
44
8080
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Process"),
45
1616
                constructor->GetFunction());
46
1616
  }
47
48
  size_t self_size() const override { return sizeof(*this); }
49
50
 private:
51
703
  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
703
    CHECK(args.IsConstructCall());
56
703
    Environment* env = Environment::GetCurrent(args);
57
1406
    new ProcessWrap(env, args.This());
58
703
  }
59
60
  ProcessWrap(Environment* env, Local<Object> object)
61
      : HandleWrap(env,
62
                   object,
63
                   reinterpret_cast<uv_handle_t*>(&process_),
64
703
                   AsyncWrap::PROVIDER_PROCESSWRAP) {
65
  }
66
67
701
  static void ParseStdioOptions(Environment* env,
68
                                Local<Object> js_options,
69
                                uv_process_options_t* options) {
70
701
    Local<String> stdio_key = env->stdio_string();
71
2103
    Local<Array> stdios = js_options->Get(stdio_key).As<Array>();
72
73
701
    uint32_t len = stdios->Length();
74
701
    options->stdio = new uv_stdio_container_t[len];
75
701
    options->stdio_count = len;
76
77
3063
    for (uint32_t i = 0; i < len; i++) {
78
4724
      Local<Object> stdio = stdios->Get(i).As<Object>();
79
4724
      Local<Value> type = stdio->Get(env->type_string());
80
81
4724
      if (type->Equals(env->ignore_string())) {
82
30
        options->stdio[i].flags = UV_IGNORE;
83
4664
      } else if (type->Equals(env->pipe_string())) {
84
1412
        options->stdio[i].flags = static_cast<uv_stdio_flags>(
85
1412
            UV_CREATE_PIPE | UV_READABLE_PIPE | UV_WRITABLE_PIPE);
86
1412
        Local<String> handle_key = env->handle_string();
87
2824
        Local<Object> handle = stdio->Get(handle_key).As<Object>();
88
1412
        CHECK(!handle.IsEmpty());
89
1412
        options->stdio[i].data.stream =
90
            reinterpret_cast<uv_stream_t*>(
91
2824
                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
701
  }
108
109
701
  static void Spawn(const FunctionCallbackInfo<Value>& args) {
110
701
    Environment* env = Environment::GetCurrent(args);
111
112
    ProcessWrap* wrap;
113
701
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
114
115
1402
    Local<Object> js_options = args[0]->ToObject(env->isolate());
116
117
    uv_process_options_t options;
118
701
    memset(&options, 0, sizeof(uv_process_options_t));
119
120
701
    options.exit_cb = OnExit;
121
122
    // options.uid
123
1402
    Local<Value> uid_v = js_options->Get(env->uid_string());
124

1403
    if (!uid_v->IsUndefined() && !uid_v->IsNull()) {
125
1
      CHECK(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
    }
130
131
    // options.gid
132
1402
    Local<Value> gid_v = js_options->Get(env->gid_string());
133

1403
    if (!gid_v->IsUndefined() && !gid_v->IsNull()) {
134
1
      CHECK(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
    }
139
140
    // TODO(bnoordhuis) is this possible to do without mallocing ?
141
142
    // options.file
143
1402
    Local<Value> file_v = js_options->Get(env->file_string());
144
1402
    CHECK(file_v->IsString());
145
1402
    node::Utf8Value file(env->isolate(), file_v);
146
701
    options.file = *file;
147
148
    // options.args
149
1402
    Local<Value> argv_v = js_options->Get(env->args_string());
150

701
    if (!argv_v.IsEmpty() && argv_v->IsArray()) {
151
701
      Local<Array> js_argv = Local<Array>::Cast(argv_v);
152
701
      int argc = js_argv->Length();
153
      // Heap allocate to detect errors. +1 is for nullptr.
154
701
      options.args = new char*[argc + 1];
155
3291
      for (int i = 0; i < argc; i++) {
156
5180
        node::Utf8Value arg(env->isolate(), js_argv->Get(i));
157
2590
        options.args[i] = strdup(*arg);
158
2590
        CHECK_NE(options.args[i], nullptr);
159
      }
160
701
      options.args[argc] = nullptr;
161
    }
162
163
    // options.cwd
164
1402
    Local<Value> cwd_v = js_options->Get(env->cwd_string());
165
    node::Utf8Value cwd(env->isolate(),
166
2103
                        cwd_v->IsString() ? cwd_v : Local<Value>());
167
701
    if (cwd.length() > 0) {
168
8
      options.cwd = *cwd;
169
    }
170
171
    // options.env
172
1402
    Local<Value> env_v = js_options->Get(env->env_pairs_string());
173

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