Skip to content

Commit

Permalink
Add daemonize support.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Jan 27, 2011
1 parent 44ee69e commit 2a84258
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.markdown
Expand Up @@ -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?
Expand Down
3 changes: 3 additions & 0 deletions conf.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}

Expand Down
3 changes: 3 additions & 0 deletions conf.h
Expand Up @@ -15,6 +15,9 @@ struct conf {
char *http_host;
short http_port;

/* daemonize process, off by default */
int daemonize;

/* ACL */
struct acl *perms;

Expand Down
25 changes: 25 additions & 0 deletions server.c
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions webdis.json
Expand Up @@ -7,6 +7,8 @@
"http_host": "0.0.0.0",
"http_port": 7379,

"daemonize": false,

"acl": [
{
"disabled": ["DEBUG"]
Expand Down

0 comments on commit 2a84258

Please sign in to comment.