Server can run as daemon

Thu, 16 Feb 2012 15:08:38 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 16 Feb 2012 15:08:38 +0100
changeset 22
adb0bda54e6b
parent 21
627b09ee74e4
child 23
a2c8fc23c90e

Server can run as daemon

src/server/config/conf.c file | annotate | diff | comparison | revisions
src/server/daemon/httprequest.c file | annotate | diff | comparison | revisions
src/server/daemon/httprequest.h file | annotate | diff | comparison | revisions
src/server/daemon/main.c file | annotate | diff | comparison | revisions
src/server/daemon/webserver.c file | annotate | diff | comparison | revisions
--- a/src/server/config/conf.c	Mon Feb 13 13:49:49 2012 +0100
+++ b/src/server/config/conf.c	Thu Feb 16 15:08:38 2012 +0100
@@ -301,7 +301,7 @@
          * Wenn man sstrdub_mp statt sstrdub nimmt, wird der Inhalt von pname
          * verunstaltet. Warum?
          */
-        param->name = sstrdub(pname); // TODO: use mempool!
+        param->name = sstrdub_mp(mp, pname);
 
         if(pvalue.length > 0) {
             param->value = sstrdub_mp(mp, pvalue);
--- a/src/server/daemon/httprequest.c	Mon Feb 13 13:49:49 2012 +0100
+++ b/src/server/daemon/httprequest.c	Thu Feb 16 15:08:38 2012 +0100
@@ -283,6 +283,10 @@
     do {
         switch(rq->phase) {
             case NSAPIAuthTrans: {
+                r = nsapi_authtrans(sn, rq);
+                if(r != REQ_PROCEED) {
+                    break;
+                }
                 rq->phase++;
                 nsapi_context_next_stage(&rq->context);
             }
@@ -341,6 +345,35 @@
     return 0;
 }
 
+int nsapi_authtrans(NSAPISession *sn, NSAPIRequest *rq) {
+    HTTPObjectConfig *objconf = rq->vs->objects;
+    httpd_object *obj = objconf->objects[0];
+    dtable *dt = object_get_dtable(obj, NSAPIAuthTrans);
+
+    int ret = rq->context.last_req_code;
+    for(int i=NCX_DI(rq);i<dt->ndir;i++) {
+        directive *d = dt->dirs[i];
+
+        ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
+        if(ret != REQ_NOACTION) {
+            /*
+             * if a saf is still processing, we need to save the context, to
+             * process this object at a later time
+             */
+            if(ret == REQ_PROCESSING) {
+                /* save nsapi context */
+                /* add +1 to start next round with next function */
+                rq->context.dtable_index = i + 1;
+            }
+
+            return ret;
+        }
+    }
+
+
+    return REQ_PROCEED;
+}
+
 int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) {
     HTTPObjectConfig *objconf = rq->vs->objects;
     //printf("nsapi_nametrans\n");
--- a/src/server/daemon/httprequest.h	Mon Feb 13 13:49:49 2012 +0100
+++ b/src/server/daemon/httprequest.h	Thu Feb 16 15:08:38 2012 +0100
@@ -79,7 +79,10 @@
 
 int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq);
 int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq);
+
+int nsapi_authtrans(NSAPISession *sn, NSAPIRequest *rq);
 int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq);
+int nsapi_pathcheck(NSAPISession *sn, NSAPIRequest *rq);
 int nsapi_objecttype(NSAPISession *sn, NSAPIRequest *rq);
 int nsapi_service(NSAPISession *sn, NSAPIRequest *rq);
 
--- a/src/server/daemon/main.c	Mon Feb 13 13:49:49 2012 +0100
+++ b/src/server/daemon/main.c	Thu Feb 16 15:08:38 2012 +0100
@@ -29,6 +29,10 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <errno.h>
+#include <pthread.h>
 
 #include "../util/pool.h"
 #include "../public/nsapi.h"
@@ -38,15 +42,95 @@
 
 #include "httprequest.h"
 
+
+int std_pipe_fds[2];
+
 void test() {
     
 }
 
+/*
+ * SIGUSR1: reload the configuration files
+ */
+void sig_usr1_reload(int sig) {
+    printf("reload\n");
+
+    signal(SIGUSR1, sig_usr1_reload);
+}
+
+/*
+ * SIGTERM: stop the server
+ */
+void sig_term(int sig) {
+    exit(EXIT_SUCCESS);
+}
+
+void* log_pipe_thread(void *data) {
+    FILE *log_out = fopen("log.txt", "a");
+
+    char buf[1024];
+    ssize_t r;
+    while((r = read(std_pipe_fds[0], buf, 1024)) > 0) {
+        fwrite(buf, 1, r, log_out);
+        fflush(log_out);
+    }
+
+    fclose(log_out);
+}
+
 int main(int argc, char **argv) {
+    //test();
+
+    /* if the -c parameter is specified, we don't create a daemon */
+    int d = 1;
+    for(int i=0;i<argc;i++) {
+        char *p = argv[i];
+        if(p[0] == '-' && p[1] == 'c') {
+            d = 0;
+            break;
+        }
+    }
+    if(d) {
+        /* create daemon */
+        pid_t pid = fork();
+        if(pid < 0) {
+            return EXIT_FAILURE;
+        } else if(pid > 0) {
+            return EXIT_SUCCESS;
+        }
+
+        if(setsid() < 0) {
+            printf("setsid failed\n");
+            return EXIT_FAILURE;
+        }
+
+        for(int i=0;i<3;i++) {
+            close(i);
+        }
+
+        /* stdio redirection */
+        /* create pipes */
+        if(pipe(std_pipe_fds) != 0) {
+            perror("pipe");
+            return EXIT_FAILURE;
+        }
+        //FILE *ws_out = fdopen(std_pipe_fds[1], "w");
+        //*stdout = *ws_out;
+        //*stderr = *ws_out;
+        dup2(std_pipe_fds[1], 1);
+        dup2(std_pipe_fds[1], 2);
+
+        pthread_t tid;
+        pthread_create(&tid, NULL, log_pipe_thread, NULL);
+    }
+
     pool_init(NULL, NULL, NULL);
 
-    //test();
+    /* add signal handler */
+    signal(SIGUSR1, sig_usr1_reload);
+    signal(SIGTERM, sig_term);
 
+    /* start webserver */
     int status;
     status = webserver_init();
     if(status != 0) {
@@ -60,7 +144,17 @@
         return EXIT_FAILURE;
     }
 
-    getchar();
+    /* TODO: join threads (or not?) */
+    while(1) {
+        if(d) {
+            fflush(stdout);
+            fflush(stderr);
+        }
+        sleep(10000);
+        if(0) {
+            break;
+        }
+    }
 
     return EXIT_SUCCESS;
 }
--- a/src/server/daemon/webserver.c	Mon Feb 13 13:49:49 2012 +0100
+++ b/src/server/daemon/webserver.c	Thu Feb 16 15:08:38 2012 +0100
@@ -61,6 +61,21 @@
         return -1;
     }
 
+    // create tmp dir and pid file
+    ServerConfiguration *cfg = cfgmgr_get_server_config();
+    char *mkdir_cmd = NULL;
+    asprintf(&mkdir_cmd, "mkdir -p %s", cfg->tmp.ptr);
+    system(mkdir_cmd);
+    free(mkdir_cmd);
+
+    char *pid_file_path = NULL;
+    asprintf(&pid_file_path, "%s/pid", cfg->tmp.ptr);
+    FILE *pidfile = fopen(pid_file_path, "w");
+    pid_t pid = getpid();
+    fprintf(pidfile, "%d", pid);
+    fclose(pidfile);
+    free(pid_file_path);
+
     // init NSAPI functions
 
 

mercurial