Add daemonize support.

master
Nicolas Favre-Felix 14 years ago
parent 44ee69eda3
commit 2a84258f36

@ -34,6 +34,7 @@ curl -d "GET/hello" http://127.0.0.1:7379/
* Cross-origin XHR, if compiled with libevent2 (for `OPTIONS` support).
* File upload with PUT, if compiled with libevent2 (for `PUT` support).
* With the JSON output, the return value of INFO is parsed and transformed into an object.
* Optional daemonize.
# Ideas, TODO...
* Add better support for PUT, DELETE, HEAD, OPTIONS? How? For which commands?

@ -34,6 +34,7 @@ conf_read(const char *filename) {
conf->group = getgid();
conf->logfile = "webdis.log";
conf->verbosity = WEBDIS_NOTICE;
conf->daemonize = 0;
j = json_load_file(filename, 0, &error);
if(!j) {
@ -75,6 +76,8 @@ conf_read(const char *filename) {
if(tmp < 0) conf->verbosity = WEBDIS_ERROR;
else if(tmp > (int)WEBDIS_DEBUG) conf->verbosity = WEBDIS_DEBUG;
else conf->verbosity = (log_level)tmp;
} else if(strcmp(json_object_iter_key(kv), "daemonize") == 0 && json_typeof(jtmp) == JSON_TRUE) {
conf->daemonize = 1;
}
}

@ -15,6 +15,9 @@ struct conf {
char *http_host;
short http_port;
/* daemonize process, off by default */
int daemonize;
/* ACL */
struct acl *perms;

@ -10,6 +10,9 @@
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct server *
server_new(const char *filename) {
@ -184,9 +187,31 @@ on_request(struct evhttp_request *rq, void *ctx) {
}
}
/* Taken from Redis. */
void
server_daemonize(void) {
int fd;
if (fork() != 0) exit(0); /* parent exits */
setsid(); /* create a new session */
/* Every output goes to /dev/null. */
if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO) close(fd);
}
}
void
server_start(struct server *s) {
if(s->cfg->daemonize) {
server_daemonize();
}
/* ignore sigpipe */
#ifdef SIGPIPE
signal(SIGPIPE, SIG_IGN);

@ -7,6 +7,8 @@
"http_host": "0.0.0.0",
"http_port": 7379,
"daemonize": false,
"acl": [
{
"disabled": ["DEBUG"]

Loading…
Cancel
Save