aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/httpd.c
blob: c352da0212b701d6182442902a4d57f35ba81f83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2020 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "../common/utils.h"
#include "ctx.h"
#include "exec.h"
#include "reloader.h"
#include "httpd.h"

// we are not going to unit-test these functions, then printing errors
// directly is not a big issue


typedef struct {
    bm_ctx_t *ctx;
    bc_trie_t *args;
} bm_httpd_t;

static pthread_mutex_t mutex_httpd_starting = PTHREAD_MUTEX_INITIALIZER;
static bool httpd_starting = false;


static void*
httpd_thread(void *arg)
{
    bm_httpd_t *httpd = arg;

    pthread_mutex_lock(&mutex_httpd_starting);
    httpd_starting = true;
    pthread_mutex_unlock(&mutex_httpd_starting);

    int rv = bm_exec_blogc_runserver(httpd->ctx, bc_trie_lookup(httpd->args, "host"),
        bc_trie_lookup(httpd->args, "port"), bc_trie_lookup(httpd->args, "threads"));

    pthread_mutex_lock(&mutex_httpd_starting);
    httpd_starting = false;
    pthread_mutex_unlock(&mutex_httpd_starting);

    free(httpd);

    // stop the reloader
    bm_reloader_stop(rv);

    return NULL;
}


int
bm_httpd_run(bm_ctx_t **ctx, bm_rule_exec_func_t rule_exec, bc_slist_t *outputs,
    bc_trie_t *args)
{
    pthread_mutex_lock(&mutex_httpd_starting);
    bool starting = httpd_starting;
    pthread_mutex_unlock(&mutex_httpd_starting);

    if (starting) {
        fprintf(stderr, "blogc-make: error: httpd already running\n");
        return 1;
    }

    int err;

    pthread_attr_t attr;
    if (0 != (err = pthread_attr_init(&attr))) {
        fprintf(stderr, "blogc-make: error: failed to initialize httpd "
            "thread attributes: %s\n", strerror(err));
        return 1;
    }

    // we run the thread detached, because we don't want to wait it to join
    // before exiting. the OS can clean it properly
    if (0 != (err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) {
        fprintf(stderr, "blogc-make: error: failed to mark httpd thread as "
            "detached: %s\n", strerror(err));
        return 1;
    }

    bm_httpd_t *rv = bc_malloc(sizeof(bm_httpd_t));
    rv->ctx = *ctx;
    rv->args = args;

    pthread_t thread;
    if (0 != (err = pthread_create(&thread, &attr, httpd_thread, rv))) {
        fprintf(stderr, "blogc-make: error: failed to create httpd "
            "thread: %s\n", strerror(err));
        free(rv);
        return 1;
    }

    // we could use some pthread_*_timedwait() apis here, but I decided to
    // just use simple mutexes for the sake of portability.
    size_t count = 0;
    while (true) {
        pthread_mutex_lock(&mutex_httpd_starting);
        starting = httpd_starting;
        pthread_mutex_unlock(&mutex_httpd_starting);

        if (starting)
            break;

        if (++count > 100) {
            fprintf(stderr, "blogc-make: error: failed to start httpd thread: "
                "too many retries\n");
            // rv will leak, but it is not safe to free here
            return 1;
        }
        usleep(100000);
    }

    return bm_reloader_run(ctx, rule_exec, outputs, args);
}