Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit f3c4c59fc83da8da25914aa40299db74c4dfea5e 0 parents
@Slashed authored
Showing with 132 additions and 0 deletions.
  1. +6 −0 README
  2. +74 −0 daemon.cc
  3. +37 −0 example.js
  4. +15 −0 wscript
6 README
@@ -0,0 +1,6 @@
+Daemon Addon for Node.js
+
+To build this module, type:
+> node-waf configure build
+
+Read
74 daemon.cc
@@ -0,0 +1,74 @@
+/*
+* Daemon.node
+*** A node.JS addon that allows creating Unix/Linux Daemons in pure Javascript.
+*** Copyright 2010 (c) <arthur@norgic.com>
+* Under MIT License. See LICENSE file.
+*/
+
+#include <v8.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define PID_MAXLEN 10
+
+using namespace v8;
+
+// Go through special routines to become a daemon.
+// if successful, returns daemon's PID
+Handle<Value> Start(const Arguments& args) {
+ pid_t pid, sid;
+
+ pid = fork();
+ if(pid > 0) exit(0);
+ if(pid < 0) exit(1);
+
+ // Can be changed after with process.umaks
+ umask(0);
+
+ sid = setsid();
+ if(sid < 0) exit(1);
+
+ // Can be changed with process.chdir
+ chdir("/");
+
+ return Integer::New(getpid());
+}
+
+// Close Standard IN/OUT/ERR Streams
+Handle<Value> CloseIO(const Arguments& args) {
+ close(STDIN_FILENO);
+ close(STDOUT_FILENO);
+ close(STDERR_FILENO);
+}
+
+// File-lock to make sure that only one instance of daemon is running.. also for storing PID
+/* lock ( filename )
+*** filename: a path to a lock-file.
+*** Note: if filename doesn't exist, it will be created when function is called.
+*/
+Handle<Value> LockD(const Arguments& args) {
+ if(!args[0]->IsString())
+ return Boolean::New(false);
+
+ String::Utf8Value data(args[0]->ToString());
+ char pid_str[PID_MAXLEN+1];
+
+ int lfp = open(*data, O_RDWR | O_CREAT, 0640);
+ if(lfp < 0) exit(1);
+ if(lockf(lfp, F_TLOCK, 0) < 0) exit(0);
+
+ int len = snprintf(pid_str, PID_MAXLEN, "%d", getpid());
+ write(lfp, pid_str, len);
+
+ return Boolean::New(true);
+}
+
+extern "C" void init(Handle<Object> target) {
+ HandleScope scope;
+
+ target->Set(String::New("start"), FunctionTemplate::New(Start)->GetFunction());
+ target->Set(String::New("lock"), FunctionTemplate::New(LockD)->GetFunction());
+ target->Set(String::New("closeIO"), FunctionTemplate::New(CloseIO)->GetFunction());
+}
37 example.js
@@ -0,0 +1,37 @@
+var daemon = require('./daemon');
+var fs = require('fs');
+var http = require('http');
+var sys = require('sys');
+
+var config = {
+ lockFile: '/tmp/testd.pid' //Location of lockFile
+};
+
+var args = process.argv;
+var dPID;
+
+// Handle start stop commands
+switch(args[2]) {
+ case "stop":
+ process.kill(parseInt(fs.readFileSync(config.lockFile)));
+ process.exit(0);
+ break;
+
+ case "start":
+ dPID = daemon.start();
+ daemon.lock(config.lockFile);
+ daemon.closeIO();
+ break;
+
+ default:
+ sys.puts('Usage: [start|stop]');
+ process.exit(0);
+}
+
+// Start HTTP Server
+http.createServer(function(req, res) {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.write('<h1>Hello, World!</h1>');
+ res.close();
+}).listen(8000);
+
15 wscript
@@ -0,0 +1,15 @@
+srcdir = "."
+blddir = "build"
+VERSION = "0.0.1"
+
+def set_options(opt):
+ opt.tool_options("compiler_cxx")
+
+def configure(conf):
+ conf.check_tool("compiler_cxx")
+ conf.check_tool("node_addon")
+
+def build(bld):
+ obj = bld.new_task_gen("cxx", "shlib", "node_addon")
+ obj.target = "daemon"
+ obj.source = "daemon.cc"
Please sign in to comment.
Something went wrong with that request. Please try again.