aboutsummaryrefslogtreecommitdiff
path: root/comp3331/server/src/shared/shared.cc
blob: 3a13b474e64e7ae47526c9de9589576c9bcda916 (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
#include "shared/shared.hh"

namespace shared {

bool should_exit = false;

static void set_signal(const decltype(SIGINT) signal,
                       void (*const callback)(const int)) {
    struct sigaction sa {};
    sa.sa_handler = callback;
    if (sigaction(signal, &sa, nullptr) == -1) {
        throw std::runtime_error("failed to set signal handler!");
    }
}

void set_exit_handler() {
    set_signal(SIGPIPE, SIG_IGN);
    set_signal(SIGINT, [](const int) {
        std::cout << " interrupt signal received\n";
        should_exit = true;
    });
}

} // namespace shared