src/server/daemon/main.c

Sun, 26 Feb 2012 19:51:14 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 26 Feb 2012 19:51:14 +0100
changeset 26
37ff8bf54b89
parent 25
5dee29c7c530
child 27
05b7576dca2b
permissions
-rw-r--r--

Minimal webdav support

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2011 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#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"
#include "../util/plist.h"

#include "webserver.h"

#include "httprequest.h"

#include "configmanager.h"

int std_pipe_fds[2];

void test() {
    
}

/*
 * SIGUSR1: reload the configuration files
 */
void sig_usr1_reload(int sig) {
    printf("reload\n");

    if(cfgmgr_load_config() != 0) {
        fprintf(stderr, "Error: cannot reload configuration");
    }

    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);
    
    return NULL;
}

int main(int argc, char **argv) {
    //test();

    /* if the -c parameter is specified, we don't create a daemon */
    int d = 0;
    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);

    /* add signal handler */
    signal(SIGUSR1, sig_usr1_reload);
    signal(SIGTERM, sig_term);

    /* start webserver */
    int status;
    status = webserver_init();
    if(status != 0) {
        fprintf(stderr, "Cannot initialize server!\n");
        return EXIT_FAILURE;
    }

    status = webserver_run();
    if(status != 0) {
        fprintf(stderr, "Cannot run server!\n");
        return EXIT_FAILURE;
    }

    /* TODO: join threads (or not?) */
    while(1) {
        if(d) {
            fflush(stdout);
            fflush(stderr);
        }
        sleep(10000);
        if(0) {
            break;
        }
    }

    return EXIT_SUCCESS;
}

mercurial