mirror of https://github.com/lukechilds/node.git
committed by
Ryan Dahl
5 changed files with 140 additions and 0 deletions
@ -0,0 +1,69 @@ |
|||||
|
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
|
||||
|
#include <signal_handler.h> |
||||
|
#include <assert.h> |
||||
|
|
||||
|
namespace node { |
||||
|
|
||||
|
using namespace v8; |
||||
|
|
||||
|
Persistent<FunctionTemplate> SignalHandler::constructor_template; |
||||
|
|
||||
|
void SignalHandler::Initialize(Handle<Object> target) { |
||||
|
HandleScope scope; |
||||
|
|
||||
|
Local<FunctionTemplate> t = FunctionTemplate::New(SignalHandler::New); |
||||
|
constructor_template = Persistent<FunctionTemplate>::New(t); |
||||
|
constructor_template->Inherit(EventEmitter::constructor_template); |
||||
|
constructor_template->InstanceTemplate()->SetInternalFieldCount(1); |
||||
|
constructor_template->SetClassName(String::NewSymbol("SignalHandler")); |
||||
|
|
||||
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", SignalHandler::Stop); |
||||
|
|
||||
|
target->Set(String::NewSymbol("SignalHandler"), |
||||
|
constructor_template->GetFunction()); |
||||
|
} |
||||
|
|
||||
|
void SignalHandler::OnSignal(EV_P_ ev_signal *watcher, int revents) { |
||||
|
SignalHandler *handler = static_cast<SignalHandler*>(watcher->data); |
||||
|
HandleScope scope; |
||||
|
|
||||
|
assert(revents == EV_SIGNAL); |
||||
|
|
||||
|
handler->Emit("signal", 0, NULL); |
||||
|
} |
||||
|
|
||||
|
SignalHandler::~SignalHandler() { |
||||
|
ev_signal_stop(EV_DEFAULT_UC_ &watcher_); |
||||
|
} |
||||
|
|
||||
|
Handle<Value> SignalHandler::New(const Arguments& args) { |
||||
|
HandleScope scope; |
||||
|
|
||||
|
if (args.Length() != 1 || !args[0]->IsInt32()) { |
||||
|
return ThrowException(String::New("Bad arguments")); |
||||
|
} |
||||
|
|
||||
|
int sig = args[0]->Int32Value(); |
||||
|
|
||||
|
SignalHandler *handler = new SignalHandler(); |
||||
|
handler->Wrap(args.Holder()); |
||||
|
|
||||
|
ev_signal_init(&handler->watcher_, SignalHandler::OnSignal, sig); |
||||
|
handler->watcher_.data = handler; |
||||
|
ev_signal_start(EV_DEFAULT_UC_ &handler->watcher_); |
||||
|
|
||||
|
handler->Attach(); |
||||
|
|
||||
|
return args.This(); |
||||
|
} |
||||
|
|
||||
|
Handle<Value> SignalHandler::Stop(const Arguments& args) { |
||||
|
HandleScope scope; |
||||
|
|
||||
|
SignalHandler *handler = ObjectWrap::Unwrap<SignalHandler>(args.Holder()); |
||||
|
ev_signal_stop(EV_DEFAULT_UC_ &handler->watcher_); |
||||
|
handler->Detach(); |
||||
|
return Undefined(); |
||||
|
} |
||||
|
|
||||
|
} // namespace node
|
@ -0,0 +1,33 @@ |
|||||
|
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
|
||||
|
#ifndef SRC_SIGNAL_HANDLER_H_ |
||||
|
#define SRC_SIGNAL_HANDLER_H_ |
||||
|
|
||||
|
#include <node.h> |
||||
|
#include <events.h> |
||||
|
|
||||
|
#include <v8.h> |
||||
|
#include <ev.h> |
||||
|
|
||||
|
namespace node { |
||||
|
|
||||
|
class SignalHandler : EventEmitter { |
||||
|
public: |
||||
|
static void Initialize(v8::Handle<v8::Object> target); |
||||
|
|
||||
|
protected: |
||||
|
static v8::Persistent<v8::FunctionTemplate> constructor_template; |
||||
|
|
||||
|
SignalHandler() : EventEmitter() { } |
||||
|
~SignalHandler(); |
||||
|
|
||||
|
static v8::Handle<v8::Value> New(const v8::Arguments& args); |
||||
|
static v8::Handle<v8::Value> Stop(const v8::Arguments& args); |
||||
|
|
||||
|
private: |
||||
|
static void OnSignal(EV_P_ ev_signal *watcher, int revents); |
||||
|
ev_signal watcher_; |
||||
|
}; |
||||
|
|
||||
|
} // namespace node
|
||||
|
#endif // SRC_SIGNAL_HANDLER_H_
|
||||
|
|
@ -0,0 +1,35 @@ |
|||||
|
node.mixin(require("common.js")); |
||||
|
|
||||
|
if (process.ARGV[2] === "-child") { |
||||
|
node.stdio.open(); |
||||
|
var handler = new node.SignalHandler(node.SIGUSR1); |
||||
|
handler.addListener("signal", function() { |
||||
|
node.stdio.write("handled SIGUSR1"); |
||||
|
process.exit(0); |
||||
|
}); |
||||
|
debug("CHILD!!!"); |
||||
|
|
||||
|
} else { |
||||
|
|
||||
|
var child = node.createChildProcess(ARGV[0], ['--', ARGV[1], '-child']); |
||||
|
|
||||
|
var output = ""; |
||||
|
|
||||
|
child.addListener('output', function (chunk) { |
||||
|
puts("Child (stdout) said: " + JSON.stringify(chunk)); |
||||
|
if (chunk) { output += chunk }; |
||||
|
}); |
||||
|
|
||||
|
child.addListener('error', function (chunk) { |
||||
|
if (/CHILD!!!/.exec(chunk)) { |
||||
|
puts("Sending SIGUSR1 to " + child.pid); |
||||
|
child.kill(node.SIGUSR1) |
||||
|
} |
||||
|
puts("Child (stderr) said: " + JSON.stringify(chunk)); |
||||
|
}); |
||||
|
|
||||
|
process.addListener("exit", function () { |
||||
|
assertEquals("handled SIGUSR1", output); |
||||
|
}); |
||||
|
|
||||
|
} |
Loading…
Reference in new issue