📄 signalhandler.h
字号:
//
// SignalHandler.h
//
// $Id: //poco/Main/Foundation/include/Foundation/SignalHandler.h#2 $
//
// Definition of the SignalHandler class.
//
// Copyright (c) 2004-2005, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#ifndef Foundation_SignalHandler_INCLUDED
#define Foundation_SignalHandler_INCLUDED
#ifndef Foundation_Foundation_INCLUDED
#include "Foundation/Foundation.h"
#endif
#if defined(POCO_OS_FAMILY_UNIX)
#ifndef STD_VECTOR_INCLUDED
#include <vector>
#define STD_VECTOR_INCLUDED
#endif
#ifndef STD_SETJMP_H_INCLUDED
#include <setjmp.h>
#define STD_SETJMP_H_INCLUDED
#endif
Foundation_BEGIN
class Foundation_API SignalHandler
/// This helper class simplifies the handling of POSIX signals.
///
/// The class provides a signal handler (installed with
/// installHandlers()) that translates certain POSIX
/// signals (SIGILL, SIGBUS, SIGSEGV, SIGSYS) into
/// C++ exceptions.
///
/// Internally, a stack of sigjmp_buf structs is maintained for
/// each thread. The constructor pushes a new sigjmp_buf onto
/// the current thread's stack. The destructor pops the sigjmp_buf
/// from the stack.
///
/// The poco_throw_on_signal macro creates an instance of SignalHandler
/// on the stack, which results in a new sigjmp_buf being created.
/// The sigjmp_buf is then set-up with sigsetjmp().
///
/// The handleSignal() method, which is invoked when a signal arrives,
/// checks if a sigjmp_buf is available for the current thread.
/// If so, siglongjmp() is used to jump out of the signal handler.
///
/// Typical usage is as follows:
///
/// try
/// {
/// poco_throw_on_signal;
/// ...
/// }
/// catch (Foundation::SignalException&)
/// {
/// ...
/// }
///
/// The best way to deal with a SignalException is to log as much context
/// information as possible, to aid in debugging, and then to exit.
{
public:
SignalHandler();
/// Creates the SignalHandler.
~SignalHandler();
/// Destroys the SignalHandler.
sigjmp_buf& jumpBuffer();
/// Returns the top-most sigjmp_buf for the current thread.
static void throwSignalException(int sig);
/// Throws a SignalException with a textual description
/// of the given signal as argument.
static void install();
/// Installs signal handlers for SIGILL, SIGBUS, SIGSEGV
/// and SIGSYS.
protected:
static void handleSignal(int sig);
/// The actual signal handler.
struct JumpBuffer
/// sigjmp_buf cannot be used to instantiate a std::vector,
/// so we provide a wrapper struct.
{
sigjmp_buf buf;
};
typedef std::vector<JumpBuffer> JumpBufferVec;
static JumpBufferVec& jumpBufferVec();
/// Returns the JumpBufferVec for the current thread.
private:
static JumpBufferVec _jumpBufferVec;
friend class ThreadImp;
};
#define poco_throw_on_signal \
Foundation::SignalHandler _poco_signalHandler; \
int _poco_signal = sigsetjmp(_poco_signalHandler.jumpBuffer(), 1); \
if (_poco_signal) _poco_signalHandler.throwSignalException(_poco_signal);
Foundation_END
#endif // POCO_OS_FAMILY_UNIX
#endif // Foundation_SignalHandler_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -