diff --git a/README.markdown b/README.markdown index 4727b40..d76fe83 100644 --- a/README.markdown +++ b/README.markdown @@ -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? diff --git a/conf.c b/conf.c index 37696c3..6402acb 100644 --- a/conf.c +++ b/conf.c @@ -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; } } diff --git a/conf.h b/conf.h index 2a3e566..4ddf8a4 100644 --- a/conf.h +++ b/conf.h @@ -15,6 +15,9 @@ struct conf { char *http_host; short http_port; + /* daemonize process, off by default */ + int daemonize; + /* ACL */ struct acl *perms; diff --git a/server.c b/server.c index e07f349..a3f84a1 100644 --- a/server.c +++ b/server.c @@ -10,6 +10,9 @@ #include #include #include +#include +#include +#include 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); diff --git a/webdis.json b/webdis.json index 15db98d..4766261 100644 --- a/webdis.json +++ b/webdis.json @@ -7,6 +7,8 @@ "http_host": "0.0.0.0", "http_port": 7379, + "daemonize": false, + "acl": [ { "disabled": ["DEBUG"]