aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/reloader.c
blob: 8d930a1637e3532f2f7271164d0a1517d71597d9 (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2018 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include "../common/utils.h"
#include "ctx.h"
#include "rules.h"
#include "reloader.h"

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

static pthread_mutex_t mutex_running = PTHREAD_MUTEX_INITIALIZER;
static bool running = false;
static int reloader_status_code = 0;
static void (*handler_func)(int) = NULL;

static void
sig_handler(int signum)
{
    bm_reloader_stop(signum + 128);
}


int
bm_reloader_run(bm_ctx_t **ctx, bm_rule_exec_func_t rule_exec,
    bc_slist_t *outputs, bc_trie_t *args)
{
    // install ^C handler
    struct sigaction current_action;
    if (sigaction(SIGINT, NULL, &current_action) < 0) {
        fprintf(stderr, "blogc-make: failed to run reloader: %s\n", strerror(errno));
        return 3;
    }
    if (current_action.sa_handler != sig_handler) {  // not installed yet
        // backup current handler
        pthread_mutex_lock(&mutex_running);
        handler_func = current_action.sa_handler;
        pthread_mutex_unlock(&mutex_running);

        // set new handler
        struct sigaction new_action;
        new_action.sa_handler = sig_handler;
        sigemptyset(&new_action.sa_mask);
        new_action.sa_flags = 0;
        if (sigaction(SIGINT, &new_action, NULL) < 0) {
            fprintf(stderr, "blogc-make: failed to run reloader: %s\n",
                strerror(errno));
            return 3;
        }
    }

    pthread_mutex_lock(&mutex_running);
    running = true;
    pthread_mutex_unlock(&mutex_running);

    while (running) {
        if (!bm_ctx_reload(ctx)) {
            fprintf(stderr, "blogc-make: warning: failed to reload context. "
                "retrying in 5 seconds ...\n\n");
            sleep(5);
            continue;
        }
        if (0 != rule_exec(*ctx, outputs, args)) {
            fprintf(stderr, "blogc-make: warning: failed to rebuild website. "
                "retrying in 5 seconds ...\n\n");
            sleep(5);
            continue;
        }
        sleep(1);
    }

    return reloader_status_code;
}


void
bm_reloader_stop(int status_code)
{
    pthread_mutex_lock(&mutex_running);

    running = false;
    reloader_status_code = status_code;

    // reraise if SIGINT
    if (status_code == SIGINT + 128) {

        // reinstall old ^C handler
        struct sigaction new_action;
        new_action.sa_handler = handler_func;
        sigemptyset(&new_action.sa_mask);
        new_action.sa_flags = 0;
        sigaction(SIGINT, &new_action, NULL);

        // run it
        raise(SIGINT);

        // SIGINT will usually kill the process, but in the case that the
        // `handler_func` prevents it, our custom handler will be reinstalled
        // by `bm_reloader_run`.
    }

    pthread_mutex_unlock(&mutex_running);
}