Permalink
Browse files

[dist] Upgrade aeternum to b74ada0

  • Loading branch information...
1 parent 79e40ec commit 4e416065ae14784b534aea23245c3c31c610c124 @AvianFlu committed May 15, 2012
View
@@ -10,7 +10,8 @@
#include <assert.h>
#include "aeternum.h"
-static int json;
+static int json = 0;
+static int silent = 1;
options_t aeternum_options(int argc, char *argv[]) {
options_t opts = options_parse(argc, argv);
@@ -22,60 +23,93 @@ void aeternum_start(options_t opts) {
assert(opts.child_args != NULL);
if (opts.json) json = 1;
- aeternum_fork();
- aeternum_redirect(opts.outfile, STDOUT_FILENO);
- aeternum_redirect(opts.errfile, STDERR_FILENO);
+ if (!opts.silent) silent = 0;
+
+ if (aeternum_fork() < 0) {
+ fprintf(stderr, "fork(), %s", strerror(errno));
+ exit(1);
+ }
+
+ if (aeternum_dup(-1, STDIN_FILENO) < 0) {
+ fprintf(stderr, "stdin redirect, %s", strerror(errno));
+ exit(1);
+ }
+ if (aeternum_redirect(opts.outfile, STDOUT_FILENO) < 0) {
+ fprintf(stderr, "stdout redirect, %s", strerror(errno));
+ exit(1);
+ }
+ if (aeternum_redirect(opts.errfile, STDERR_FILENO) < 0) {
+ fprintf(stderr, "stderr redirect, %s", strerror(errno));
+ exit(1);
+ }
aeternum_exec(opts.target, opts.child_args);
}
-void aeternum_fork() {
+int aeternum_fork() {
+ int pid = -1;
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
- int pid = fork();
+ pid = fork();
if (pid < 0) {
if (json) {
- printf("{\"error\": \"%s\"}", strerror(errno));
+ fprintf(stderr, "{\"error\": \"%s\"}", strerror(errno));
}
else {
- printf("An error occurred: %s", strerror(errno));
+ fprintf(stderr, "An error occurred: %s", strerror(errno));
}
- exit(pid);
+ return pid;
}
else if (pid > 0) {
- if (json) {
+ if (json && !silent) {
printf("{\"pid\": %d}", pid);
}
- else {
+ else if (!silent) {
printf("Child has been spawned and daemonized. PID: %d\n", pid);
}
exit(0);
}
if (setsid() == -1) {
- perror("setsid()");
- exit(errno);
+ return -1;
}
umask(027);
+ return 0;
}
-void aeternum_redirect(const char *dest, int fileno) {
+int aeternum_redirect(const char *dest, int fileno) {
int out;
+ const char *filename;
if (dest == NULL) {
- dest = "/dev/null\0";
+ filename = "/dev/null";
}
- out = open(dest, O_WRONLY | O_APPEND | O_CREAT,
+ else {
+ filename = dest;
+ }
+ out = open(filename, O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
if (out == -1) {
- perror("open()");
- exit(errno);
+ return -1;
}
if (dup2(out, fileno) == -1) {
- perror("dup2()");
- exit(errno);
+ return -1;
+ }
+ return 0;
+}
+
+int aeternum_dup(int oldfd, int newfd) {
+ if (oldfd == -1) {
+ oldfd = open("/dev/null", newfd == 0 ? O_RDONLY : O_WRONLY);
+ }
+ if (close(newfd) == -1) {
+ return -1;
+ }
+ if (dup2(oldfd, newfd) == -1) {
+ return -1;
}
+ return 0;
}
void aeternum_exec(const char *filename, char *args[]) {
View
@@ -7,15 +7,17 @@ extern "C" {
#include "options.h"
-extern options_t aeternum_options(int argc, char *argv[]);
+options_t aeternum_options(int argc, char *argv[]);
-extern void aeternum_start(options_t opts);
+void aeternum_start(options_t opts);
-extern void aeternum_fork();
+int aeternum_fork();
-extern void aeternum_redirect(const char *filename, int fileno);
+int aeternum_redirect(const char *filename, int fileno);
-extern void aeternum_exec(const char *filename, char *args[]);
+int aeternum_dup(int oldfd, int newfd);
+
+void aeternum_exec(const char *filename, char *args[]);
#endif
View
@@ -12,6 +12,7 @@ options_t options_parse(int argc, char *argv[]) {
opts.errfile = NULL;
opts.target = NULL;
opts.json = 0;
+ opts.silent = 0;
opts.child_args = NULL;
int i;
@@ -32,6 +33,9 @@ options_t options_parse(int argc, char *argv[]) {
case 'j':
opts.json = 1;
break;
+ case 's':
+ opts.silent = 1;
+ break;
case '-':
if (argv[i + 1] != NULL) {
opts.target = &argv[i + 1][0];
View
@@ -10,6 +10,7 @@ struct options_s {
char *errfile;
char *target;
int json;
+ int silent;
char **child_args;
};
View
@@ -1,12 +0,0 @@
-
-
-var http = require('http');
-
-console.error(process.argv[2])
-
-http.createServer(function(req, res) {
- console.log(req.socket.remoteAddress || req.connection.remoteAddress);
- res.end(JSON.stringify(req.headers, true, 2));
-}).listen(8080);
-
-
@@ -1,12 +0,0 @@
-
-
-var childProcess = require('child_process');
-
-childProcess.execFile('./aeternum', ['node', 'test.js', 'hello'], function (err, stdout, stderr) {
- if (err) {
- return console.error(err.stack);
- }
- console.log(stdout);
- console.error(stderr);
-});
-
@@ -1,7 +0,0 @@
-
-
-var http = require('http');
-
-http.createServer(function (req, res) {
- res.end(JSON.stringify(process.argv));
-}).listen(8080);

0 comments on commit 4e41606

Please sign in to comment.