Skip to content
This repository
Browse code

[api] Rename start()/run() to daemonize(). Rename stop() to kill(). U…

…pdate tabs -> spaces.
  • Loading branch information...
commit b943c8b44385882e400c6b5f0877b97522a44504 1 parent 7d0c8f1
Charlie Robbins indexzero authored
2  README.md
Source Rendered
@@ -43,7 +43,7 @@ This library also exposes a higher level facility through javascript for startin
43 43
44 44 // Your awesome code here
45 45
46   - daemon.run('somefile.log', '/tmp/yourprogram.pid', function (err, started) {
  46 + daemon.daemonize('somefile.log', '/tmp/yourprogram.pid', function (err, started) {
47 47 // We are now in the daemon process
48 48 if (err) return sys.puts('Error starting daemon: ' + err);
49 49
34 example/bindings.js
@@ -19,36 +19,36 @@ catch (ex) {
19 19 }
20 20
21 21 var config = {
22   - lockFile: '/tmp/testd.pid', // Location of lockFile
23   - logFile: '/tmp/testd.log' // Location of logFile
  22 + lockFile: '/tmp/testd.pid', // Location of lockFile
  23 + logFile: '/tmp/testd.log' // Location of logFile
24 24 };
25 25
26 26 var args = process.argv;
27 27
28 28 // Handle start stop commands
29 29 switch(args[2]) {
30   - case "stop":
31   - process.kill(parseInt(fs.readFileSync(config.lockFile)));
32   - process.exit(0);
33   - break;
34   -
35   - case "start":
  30 + case "stop":
  31 + process.kill(parseInt(fs.readFileSync(config.lockFile)));
  32 + process.exit(0);
  33 + break;
  34 +
  35 + case "start":
36 36 fs.open(config.logFile, 'w+', function (err, fd) {
37 37 if (err) return sys.puts('Error starting daemon: ' + err);
38 38
39 39 daemon.start(fd);
40   - daemon.lock(config.lockFile);
  40 + daemon.lock(config.lockFile);
41 41 });
42   - break;
43   -
44   - default:
45   - sys.puts('Usage: [start|stop]');
46   - process.exit(0);
  42 + break;
  43 +
  44 + default:
  45 + sys.puts('Usage: [start|stop]');
  46 + process.exit(0);
47 47 }
48 48
49 49 // Start HTTP Server
50 50 http.createServer(function(req, res) {
51   - res.writeHead(200, { 'Content-Type': 'text/html' });
52   - res.write('<h1>Hello, World!</h1>');
53   - res.end();
  51 + res.writeHead(200, { 'Content-Type': 'text/html' });
  52 + res.write('<h1>Hello, World!</h1>');
  53 + res.end();
54 54 }).listen(8000);
43 example/wrapper.js
@@ -19,38 +19,41 @@ catch (ex) {
19 19 }
20 20
21 21 var config = {
22   - lockFile: '/tmp/testd.pid', // Location of lockFile
23   - logFile: '/tmp/testd.log' // Location of logFile
  22 + lockFile: '/tmp/testd.pid', // Location of lockFile
  23 + logFile: '/tmp/testd.log' // Location of logFile
24 24 };
25 25
26 26 var args = process.argv;
27 27
28 28 // Handle start stop commands
29 29 switch(args[2]) {
30   - case "stop":
31   - daemon.stop(config.lockFile, function (err, pid) {
32   - if (err) return sys.puts('Error stopping daemon: ' + err);
33   - sys.puts('Successfully stopped daemon with pid: ' + pid);
34   - });
35   - break;
36   -
37   - case "start":
38   - // Start HTTP Server
  30 + case "stop":
  31 + daemon.kill(config.lockFile, function (err, pid) {
  32 + if (err) return sys.puts('Error stopping daemon: ' + err);
  33 + sys.puts('Successfully stopped daemon with pid: ' + pid);
  34 + });
  35 + break;
  36 +
  37 + case "start":
  38 + // Start HTTP Server
39 39 http.createServer(function(req, res) {
40 40 // sys.puts('Incoming request for: ' + req.url);
41   - res.writeHead(200, { 'Content-Type': 'text/html' });
42   - res.write('<h1>Hello, World!</h1>');
43   - res.end();
  41 + res.writeHead(200, { 'Content-Type': 'text/html' });
  42 + res.write('<h1>Hello, World!</h1>');
  43 + res.end();
44 44 }).listen(8000);
45 45
46   - daemon.run(config.logFile, config.lockFile, function (err, started) {
47   - if (err) return sys.puts('Error starting daemon: ' + err);
  46 + daemon.daemonize(config.logFile, config.lockFile, function (err, started) {
  47 + if (err) {
  48 + console.dir(err.stack);
  49 + return sys.puts('Error starting daemon: ' + err);
  50 + }
48 51 sys.puts('Successfully started daemon');
49 52 });
50 53 break;
51   -
52   - default:
53   - sys.puts('Usage: [start|stop]');
54   - break;
  54 +
  55 + default:
  56 + sys.puts('Usage: [start|stop]');
  57 + break;
55 58 }
56 59
8 lib/daemon.js
@@ -20,12 +20,12 @@ Object.keys(binding).forEach(function (k) { daemon[k] = binding[k] });
20 20 // Run is designed to encapsulate the basic daemon operation in a single async call.
21 21 // When the callback returns you are in the the child process.
22 22 //
23   -daemon.run = function (out, lock, callback) {
  23 +daemon.daemonize = function (out, lock, callback) {
24 24 fs.open(out, 'w+', function (err, fd) {
25 25 if (err) return callback(err);
26 26
27 27 try {
28   - daemon.start(fd);
  28 + binding.daemonize(fd);
29 29 daemon.lock(lock);
30 30 callback(null, true);
31 31 }
@@ -36,11 +36,11 @@ daemon.run = function (out, lock, callback) {
36 36 };
37 37
38 38 //
39   -// function lock (lock, callback)
  39 +// function kill (lock, callback)
40 40 // Asynchronously stop the process in the lock file and
41 41 // remove the lock file
42 42 //
43   -daemon.stop = function (lock, callback) {
  43 +daemon.kill = function (lock, callback) {
44 44 fs.readFile(lock, function (err, data) {
45 45 if (err) return callback(err);
46 46
19 src/daemon.cc
@@ -30,9 +30,9 @@ using namespace node;
30 30
31 31 //
32 32 // Go through special routines to become a daemon.
33   -// if successful, returns daemon's PID
  33 +// if successful, returns daemon pid
34 34 //
35   -Handle<Value> Start(const Arguments& args) {
  35 +Handle<Value> Daemonize(const Arguments& args) {
36 36 HandleScope scope;
37 37 pid_t pid, sid;
38 38 int i, new_fd;
@@ -84,6 +84,15 @@ Handle<Value> CloseStdout(const Arguments& args) {
84 84 }
85 85
86 86 //
  87 +// Closes all stdio by redirecting to /dev/null
  88 +//
  89 +Handle<Value> CloseStdio(const Arguments& args) {
  90 + freopen("/dev/null", "r", stdin);
  91 + freopen("/dev/null", "w", stderr);
  92 + freopen("/dev/null", "w", stdout);
  93 +}
  94 +
  95 +//
87 96 // File-lock to make sure that only one instance of daemon is running, also for storing pid
88 97 // lock (filename)
89 98 // @filename: a path to a lock-file.
@@ -179,9 +188,13 @@ Handle<Value> SetReuid(const Arguments& args) {
179 188 extern "C" void init(Handle<Object> target) {
180 189 HandleScope scope;
181 190
182   - NODE_SET_METHOD(target, "start", Start);
  191 + NODE_SET_METHOD(target, "daemonize", Daemonize);
183 192 NODE_SET_METHOD(target, "lock", LockD);
184 193 NODE_SET_METHOD(target, "setsid", SetSid);
185 194 NODE_SET_METHOD(target, "chroot", Chroot);
186 195 NODE_SET_METHOD(target, "setreuid", SetReuid);
  196 + NODE_SET_METHOD(target, "closeStderr", CloseStderr);
  197 + NODE_SET_METHOD(target, "closeStdout", CloseStdout);
  198 + NODE_SET_METHOD(target, "cloudStdin", CloseStdin);
  199 + NODE_SET_METHOD(target, "closeStdio", CloseStdio);
187 200 }

0 comments on commit b943c8b

Please sign in to comment.
Something went wrong with that request. Please try again.