This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
forked from AvianFlu/node-waitpid
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwaitpid.cc
57 lines (45 loc) · 1.88 KB
/
waitpid.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <node.h>
#include <v8.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
using namespace v8;
using namespace node;
void Waitpid(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
int r, status;
// check arguments
if (args.Length() < 2) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Two arguments are required, PID and options")));
return;
}
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "PID and options must be numbers")));
return;
}
// do the waitpid call
r = waitpid(args[0]->NumberValue(), &status, args[1]->NumberValue());
// return an object
Local<Object> result = Object::New(isolate);
result->Set(String::NewFromUtf8(isolate, "return"), Number::New(isolate, r));
if (WIFEXITED(status)) {
result->Set(String::NewFromUtf8(isolate, "exitCode"), Number::New(isolate, WEXITSTATUS(status)));
result->Set(String::NewFromUtf8(isolate, "signalCode"), Null(isolate));
} else if (WIFSIGNALED(status)) {
result->Set(String::NewFromUtf8(isolate, "exitCode"), Null(isolate));
result->Set(String::NewFromUtf8(isolate, "signalCode"), Number::New(isolate, WTERMSIG(status)));
}
args.GetReturnValue().Set(result);
}
void init(Local<Object> exports) {
Isolate* isolate = exports->GetIsolate();
NODE_SET_METHOD(exports, "waitpid", Waitpid);
// expose the option constants
exports->Set(String::NewFromUtf8(isolate, "WNOHANG"), Number::New(isolate, WNOHANG));
exports->Set(String::NewFromUtf8(isolate, "WUNTRACED"), Number::New(isolate, WUNTRACED));
exports->Set(String::NewFromUtf8(isolate, "WCONTINUED"), Number::New(isolate, WCONTINUED));
}
NODE_MODULE(waitpid2, init)