summaryrefslogtreecommitdiffstats
path: root/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp
blob: acd82ffd29e5c012609423f29669c4839cd987f9 (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "sig-catch.h"
#include <csignal>
#include <unistd.h>
#include <string.h>

static int sigPermanent(int sig, void(*handler)(int));
static void setStopFlag(int sig);
sig_atomic_t stop = 0;

SigCatch::SigCatch()
{
    sigPermanent(SIGTERM, setStopFlag);
    sigPermanent(SIGINT, setStopFlag);
}

bool
SigCatch::receivedStopSignal() {
    return stop != 0;
}

static void
setStopFlag(int sig)
{
    (void)sig;
    stop = 1;
}

static int
sigPermanent(int sig, void(*handler)(int))
{
    struct sigaction sa;

    memset(&sa, 0, sizeof(sa));
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0; // no SA_RESTART!
    sa.sa_handler = handler;
    return sigaction(sig, &sa, nullptr);
}