Skip to content

Commit

Permalink
Merge pull request #10 from Medium/nick-victor-node12
Browse files Browse the repository at this point in the history
daemonsauce updates for node 12
  • Loading branch information
nicks committed Jun 8, 2015
2 parents d9fef7f + 8961d90 commit b75e2a4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 51 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- "0.10"
- "0.12"

5 changes: 4 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"targets": [
{
"target_name": "daemonsauceNative",
"sources": [ "src/daemonsauceNative.cc" ]
"sources": [ "src/daemonsauceNative.cc" ],
"include_dirs" : [
"<!(node -e \"require('nan')\")"
]
}
]
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "daemonsauce",
"version": "0.3.11",
"version": "1.0.0-alpha",
"description": "Just add Daemon Sauce to turn your app into a daemon.",
"keywords": [
"daemon", "init", "upstart", "setuid", "setgid", "lock", "log"
Expand Down Expand Up @@ -37,7 +37,8 @@
},

"dependencies": {
"posix": "~0.0.13"
"nan": "^1.8.4",
"posix": "~2.0.1"
},
"devDependencies": {
"nodeunit": "0.9.1",
Expand Down
90 changes: 42 additions & 48 deletions src/daemonsauceNative.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2012 The Obvious Corporation.

#include <node.h>
#include <nan.h>
#include <v8.h>

#include <fcntl.h>
Expand All @@ -12,87 +13,80 @@ using namespace v8;

#define DEV_NULL "/dev/null"

/**
* Helper to schedule an exception with the given message and return
* undefined.
*/
static Handle<Value> scheduleException(const char* message) {
Local<Value> exception = Exception::Error(String::New(message));
ThrowException(exception);
return Undefined();
}

Handle<Value> CloseStdin(const Arguments& args) {
NAN_METHOD(CloseStdin) {
freopen(DEV_NULL, "r", stdin);
NanReturnUndefined();
}

Handle<Value> CloseStdout(const Arguments& args) {
NAN_METHOD(CloseStdout) {
freopen(DEV_NULL, "w", stdout);
NanReturnUndefined();
}

Handle<Value> CloseStderr(const Arguments& args) {
NAN_METHOD(CloseStderr) {
freopen(DEV_NULL, "w", stderr);
NanReturnUndefined();
}

Handle<Value> ReopenStdout(const Arguments& args) {
HandleScope scope;
NAN_METHOD(ReopenStdout) {
NanScope();

Local<String> name = args[0]->ToString();
if (name.IsEmpty()) {
return scheduleException("Not a string.");
if (!args[0]->IsString() || name.IsEmpty()) {
return NanThrowError("Not a string.");
}

String::Utf8Value data(name);

if (freopen(*data, "a", stdout) == NULL) {
return scheduleException("Failed to reopen stdout.");
return NanThrowError("Failed to reopen stdout.");
}

return Undefined();
NanReturnUndefined();
}

Handle<Value> ReopenStderr(const Arguments& args) {
HandleScope scope;
NAN_METHOD(ReopenStderr) {
NanScope();

Local<String> name = args[0]->ToString();
if (name.IsEmpty()) {
return scheduleException("Not a string.");
if (!args[0]->IsString() || name.IsEmpty()) {
return NanThrowError("Not a string.");
}

String::Utf8Value data(name);

if (freopen(*data, "a", stderr) == NULL) {
return scheduleException("Failed to reopen stderr.");
return NanThrowError("Failed to reopen stderr.");
}

return Undefined();
NanReturnUndefined();
}

/**
* This is adapted from the daemon.node module:
*
*
* https://github.com/indexzero/daemon.node
*
* That code is licensed under the MIT License, but also this code isn't
* just a straight copy anyway.
*/
Handle<Value> AcquireLock(const Arguments& args) {
HandleScope scope;
NAN_METHOD(AcquireLock) {
NanScope();

Local<String> name = args[0]->ToString();
if (name.IsEmpty()) {
return scheduleException("Not a string.");
if (!args[0]->IsString() || name.IsEmpty()) {
return NanThrowError("Not a string.");
}

String::Utf8Value data(name);

int lockFd = open(*data, O_RDWR | O_CREAT, 0640);
if (lockFd < 0) {
return scheduleException("Failed to open lock file");
return NanThrowError("Failed to open lock file");
}

if (lockf(lockFd, F_TLOCK, 0) < 0) {
return False();
NanReturnValue(false);
}

char *pidStr;
Expand All @@ -101,31 +95,31 @@ Handle<Value> AcquireLock(const Arguments& args) {
if (pidLen < 0) {
// This shouldn't happen, and is probably a sign of
// catastrophic failure, but we'll attempt to deal.
return scheduleException("Failed make pid string");
return NanThrowError("Failed make pid string");
}

write(lockFd, pidStr, pidLen);
free(pidStr);

ftruncate(lockFd, pidLen);
fsync(lockFd);
return True();

NanReturnValue(true);
}

void init(Handle<Object> target) {
target->Set(String::NewSymbol("closeStdin"),
FunctionTemplate::New(CloseStdin)->GetFunction());
target->Set(String::NewSymbol("closeStdout"),
FunctionTemplate::New(CloseStdout)->GetFunction());
target->Set(String::NewSymbol("closeStderr"),
FunctionTemplate::New(CloseStderr)->GetFunction());
target->Set(String::NewSymbol("reopenStdout"),
FunctionTemplate::New(ReopenStdout)->GetFunction());
target->Set(String::NewSymbol("reopenStderr"),
FunctionTemplate::New(ReopenStderr)->GetFunction());
target->Set(String::NewSymbol("acquireLock"),
FunctionTemplate::New(AcquireLock)->GetFunction());
target->Set(NanNew<String>("closeStdin"),
NanNew<FunctionTemplate>(CloseStdin)->GetFunction());
target->Set(NanNew<String>("closeStdout"),
NanNew<FunctionTemplate>(CloseStdout)->GetFunction());
target->Set(NanNew<String>("closeStderr"),
NanNew<FunctionTemplate>(CloseStderr)->GetFunction());
target->Set(NanNew<String>("reopenStdout"),
NanNew<FunctionTemplate>(ReopenStdout)->GetFunction());
target->Set(NanNew<String>("reopenStderr"),
NanNew<FunctionTemplate>(ReopenStderr)->GetFunction());
target->Set(NanNew<String>("acquireLock"),
NanNew<FunctionTemplate>(AcquireLock)->GetFunction());
}

NODE_MODULE(daemonsauceNative, init)

0 comments on commit b75e2a4

Please sign in to comment.