Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 64 lines (46 sloc) 1.348 kB
48c2747 @pgte cleaned up a bit daemons and moved it to deps folder
authored
1 /*
2 * Daemon.node
3 *** A node.JS addon that allows creating Unix/Linux Daemons in pure Javascript.
4 *** Copyright 2010 (c) <arthur@norgic.com>
5 * Under MIT License. See LICENSE file.
6 */
7
8 #include <v8.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
17fb85b @pgte daemonizing branching and testing for linux
authored
13 #include <ev.h>
48c2747 @pgte cleaned up a bit daemons and moved it to deps folder
authored
14
15 #define PID_MAXLEN 10
16
17 using namespace v8;
18
19 // Go through special routines to become a daemon.
20 // if successful, returns daemon's PID
21 Handle<Value> Start(const Arguments& args) {
22 pid_t pid, sid;
c8ce9bb @pgte daemonizing fixesand testing on linux
authored
23 int i, new_fd;
24
25 if (args.Length() < 1) {
26 return ThrowException(Exception::TypeError(
27 String::New("Must have at least one arg containing the file descriptor")));
28 }
29
30 new_fd = args[0]->Int32Value();
48c2747 @pgte cleaned up a bit daemons and moved it to deps folder
authored
31
32 pid = fork();
33 if(pid > 0) exit(0);
34 if(pid < 0) exit(1);
35
17fb85b @pgte daemonizing branching and testing for linux
authored
36 ev_default_fork();
48c2747 @pgte cleaned up a bit daemons and moved it to deps folder
authored
37
262cb0d @pgte daemonizing
authored
38 close(STDIN_FILENO);
c8ce9bb @pgte daemonizing fixesand testing on linux
authored
39 //close(STDOUT_FILENO);
40 //close(STDERR_FILENO);
41 dup2(new_fd, STDOUT_FILENO);
42 dup2(new_fd, STDERR_FILENO);
48c2747 @pgte cleaned up a bit daemons and moved it to deps folder
authored
43
262cb0d @pgte daemonizing
authored
44 sid = setsid();
48c2747 @pgte cleaned up a bit daemons and moved it to deps folder
authored
45
17fb85b @pgte daemonizing branching and testing for linux
authored
46 return Integer::New(getpid());
48c2747 @pgte cleaned up a bit daemons and moved it to deps folder
authored
47 }
48
262cb0d @pgte daemonizing
authored
49 Handle<Value> SetSid(const Arguments& args) {
50 pid_t sid;
51
52 sid = setsid();
53
54 return Integer::New(sid);
55 }
56
48c2747 @pgte cleaned up a bit daemons and moved it to deps folder
authored
57 extern "C" void init(Handle<Object> target) {
58 HandleScope scope;
59
60 target->Set(String::New("start"), FunctionTemplate::New(Start)->GetFunction());
262cb0d @pgte daemonizing
authored
61 target->Set(String::New("setSid"), FunctionTemplate::New(SetSid)->GetFunction());
48c2747 @pgte cleaned up a bit daemons and moved it to deps folder
authored
62
3a82913 @pgte daemonizing
authored
63 }
Something went wrong with that request. Please try again.