From 4c76bd6a4349b59865a9607fe11fc7177775e1bb Mon Sep 17 00:00:00 2001 From: Arne Juul Date: Thu, 5 Oct 2017 11:34:27 +0000 Subject: add SigCatch class --- vespalib/src/vespa/vespalib/util/CMakeLists.txt | 1 + vespalib/src/vespa/vespalib/util/sig_catch.cpp | 35 ++++++++++++++++++++++ vespalib/src/vespa/vespalib/util/sig_catch.h | 35 ++++++++++++++++++++++ vespalib/src/vespa/vespalib/util/signalhandler.cpp | 1 + vespalib/src/vespa/vespalib/util/signalhandler.h | 1 + 5 files changed, 73 insertions(+) create mode 100644 vespalib/src/vespa/vespalib/util/sig_catch.cpp create mode 100644 vespalib/src/vespa/vespalib/util/sig_catch.h (limited to 'vespalib/src') diff --git a/vespalib/src/vespa/vespalib/util/CMakeLists.txt b/vespalib/src/vespa/vespalib/util/CMakeLists.txt index 6d08c3b1126..58739ee4df6 100644 --- a/vespalib/src/vespa/vespalib/util/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/util/CMakeLists.txt @@ -37,6 +37,7 @@ vespa_add_library(vespalib_vespalib_util OBJECT rwlock.cpp sequence.cpp sha1.cpp + sig_catch.cpp signalhandler.cpp simple_thread_bundle.cpp slaveproc.cpp diff --git a/vespalib/src/vespa/vespalib/util/sig_catch.cpp b/vespalib/src/vespa/vespalib/util/sig_catch.cpp new file mode 100644 index 00000000000..b1ffecdf24c --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/sig_catch.cpp @@ -0,0 +1,35 @@ +// 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 "signalhandler.h" + +namespace vespalib { + +SigCatch::SigCatch() +{ + SignalHandler::PIPE.ignore(); + SignalHandler::INT.hook(); + SignalHandler::TERM.hook(); + SignalHandler::CHLD.hook(); +} + +bool +SigCatch::receivedStopSignal() +{ + return (SignalHandler::INT.check() || + SignalHandler::TERM.check()); +} + +bool +SigCatch::receivedChildSignal() +{ + return SignalHandler::CHLD.check(); +} + +void +SigCatch::clearChildSignalFlag() +{ + SignalHandler::CHLD.clear(); +} + +} // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/util/sig_catch.h b/vespalib/src/vespa/vespalib/util/sig_catch.h new file mode 100644 index 00000000000..80a52545801 --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/sig_catch.h @@ -0,0 +1,35 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +namespace vespalib { + +/** + * @brief Use this class for simple common-case signal handling. + **/ + +class SigCatch +{ +public: + /** + * Constructor installs signal handlers. + **/ + SigCatch(); + + /** + * Check if a signal to stop has been received. + **/ + bool receivedStopSignal(); + + /** + * Check for child process events + **/ + bool receivedChildSignal(); + + /** + * Clear flag for child process events + **/ + void clearChildSignalFlag(); +}; + +} // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/util/signalhandler.cpp b/vespalib/src/vespa/vespalib/util/signalhandler.cpp index 6bc7fcd17df..21543ef10d8 100644 --- a/vespalib/src/vespa/vespalib/util/signalhandler.cpp +++ b/vespalib/src/vespa/vespalib/util/signalhandler.cpp @@ -25,6 +25,7 @@ Shutdown shutdown; SignalHandler SignalHandler::HUP(SIGHUP); SignalHandler SignalHandler::INT(SIGINT); SignalHandler SignalHandler::TERM(SIGTERM); +SignalHandler SignalHandler::CHLD(SIGCHLD); SignalHandler SignalHandler::PIPE(SIGPIPE); SignalHandler SignalHandler::SEGV(SIGSEGV); SignalHandler SignalHandler::ABRT(SIGABRT); diff --git a/vespalib/src/vespa/vespalib/util/signalhandler.h b/vespalib/src/vespa/vespalib/util/signalhandler.h index 6b233b2e690..f34ddba5530 100644 --- a/vespalib/src/vespa/vespalib/util/signalhandler.h +++ b/vespalib/src/vespa/vespalib/util/signalhandler.h @@ -67,6 +67,7 @@ public: static SignalHandler HUP; static SignalHandler INT; static SignalHandler TERM; + static SignalHandler CHLD; static SignalHandler PIPE; static SignalHandler SEGV; static SignalHandler ABRT; -- cgit v1.2.3