Write pidfile when daemonized (GitHub issue #10)

master
Nicolas Favre-Felix 11 years ago
parent dd18189cc4
commit b56218c84a

@ -38,7 +38,7 @@ curl -d "GET/hello" http://127.0.0.1:7379/
* Cross-origin requests, usable with XMLHttpRequest2 (Cross-Origin Resource Sharing - CORS). * Cross-origin requests, usable with XMLHttpRequest2 (Cross-Origin Resource Sharing - CORS).
* File upload with PUT. * File upload with PUT.
* With the JSON output, the return value of INFO is parsed and transformed into an object. * With the JSON output, the return value of INFO is parsed and transformed into an object.
* Optional daemonize. * Optional daemonize: set `"daemonize": true` and `"pidfile": "/var/run/webdis.pid"` in webdis.json.
* Default root object: Add `"default_root": "/GET/index.html"` in webdis.json to substitute the request to `/` with a Redis request. * Default root object: Add `"default_root": "/GET/index.html"` in webdis.json to substitute the request to `/` with a Redis request.
* HTTP request limit with `http_max_request_size` (in bytes, set to 128MB by default). * HTTP request limit with `http_max_request_size` (in bytes, set to 128MB by default).
* Database selection in the URL, using e.g. `/7/GET/key` to run the command on DB 7. * Database selection in the URL, using e.g. `/7/GET/key` to run the command on DB 7.

@ -37,6 +37,7 @@ conf_read(const char *filename) {
conf->logfile = "webdis.log"; conf->logfile = "webdis.log";
conf->verbosity = WEBDIS_NOTICE; conf->verbosity = WEBDIS_NOTICE;
conf->daemonize = 0; conf->daemonize = 0;
conf->pidfile = "webdis.pid";
conf->database = 0; conf->database = 0;
conf->pool_size_per_thread = 2; conf->pool_size_per_thread = 2;
@ -86,6 +87,8 @@ conf_read(const char *filename) {
else conf->verbosity = (log_level)tmp; else conf->verbosity = (log_level)tmp;
} else if(strcmp(json_object_iter_key(kv), "daemonize") == 0 && json_typeof(jtmp) == JSON_TRUE) { } else if(strcmp(json_object_iter_key(kv), "daemonize") == 0 && json_typeof(jtmp) == JSON_TRUE) {
conf->daemonize = 1; conf->daemonize = 1;
} else if(strcmp(json_object_iter_key(kv),"pidfile") == 0 && json_typeof(jtmp) == JSON_STRING){
conf->pidfile = strdup(json_string_value(jtmp));
} else if(strcmp(json_object_iter_key(kv), "websockets") == 0 && json_typeof(jtmp) == JSON_TRUE) { } else if(strcmp(json_object_iter_key(kv), "websockets") == 0 && json_typeof(jtmp) == JSON_TRUE) {
conf->websockets = 1; conf->websockets = 1;
} else if(strcmp(json_object_iter_key(kv), "database") == 0 && json_typeof(jtmp) == JSON_INTEGER) { } else if(strcmp(json_object_iter_key(kv), "database") == 0 && json_typeof(jtmp) == JSON_INTEGER) {

@ -22,6 +22,7 @@ struct conf {
/* daemonize process, off by default */ /* daemonize process, off by default */
int daemonize; int daemonize;
char *pidfile;
/* WebSocket support, off by default */ /* WebSocket support, off by default */
int websockets; int websockets;

@ -130,7 +130,7 @@ server_can_accept(int fd, short event, void *ptr) {
* (taken from Redis) * (taken from Redis)
*/ */
static void static void
server_daemonize(void) { server_daemonize(const char *pidfile) {
int fd; int fd;
if (fork() != 0) exit(0); /* parent exits */ if (fork() != 0) exit(0); /* parent exits */
@ -143,8 +143,16 @@ server_daemonize(void) {
dup2(fd, STDERR_FILENO); dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO) close(fd); if (fd > STDERR_FILENO) close(fd);
} }
}
/* write pidfile */
if (pidfile) {
FILE *f = fopen(pidfile, "w");
if (f) {
fprintf(f, "%d\n", (int)getpid());
fclose(f);
}
}
}
int int
server_start(struct server *s) { server_start(struct server *s) {
@ -155,7 +163,7 @@ server_start(struct server *s) {
s->base = event_base_new(); s->base = event_base_new();
if(s->cfg->daemonize) { if(s->cfg->daemonize) {
server_daemonize(); server_daemonize(s->cfg->pidfile);
/* sometimes event mech gets lost on fork */ /* sometimes event mech gets lost on fork */
if(event_reinit(s->base) != 0) { if(event_reinit(s->base) != 0) {

Loading…
Cancel
Save