diff options
author | Bjørn Meland <bjormel@users.noreply.github.com> | 2017-10-05 11:59:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-05 11:59:57 +0200 |
commit | cf86d459aa9d3d136fd7b4d5de91429165b9a8c3 (patch) | |
tree | 0878f31d717e79465523ec138d60796683543e54 /logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp | |
parent | 7de6a28f74be7515a8fabe1a778b9c0728725b9e (diff) | |
parent | c206adf7dfde86c32653937a43e0ef08de67ed29 (diff) |
Merge pull request #3654 from vespa-engine/arnej/cpp-logforwarder-start-2
Arnej/cpp logforwarder start 2
Diffstat (limited to 'logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp')
-rw-r--r-- | logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp b/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp new file mode 100644 index 00000000000..b10275d515e --- /dev/null +++ b/logforwarder/src/apps/vespa-logforwarder-start/sig-catch.cpp @@ -0,0 +1,41 @@ +// 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(SIGPIPE, SIG_IGN); + 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); +} |