1
#pragma once
2

            
3
#include <ostream>
4

            
5
#include "envoy/common/pure.h"
6

            
7
#include "source/common/signal/fatal_action.h"
8

            
9
namespace Envoy {
10

            
11
// A simple class which allows registering functions to be called when Envoy
12
// receives one of the fatal signals, documented in signal_action.h.
13
class FatalErrorHandlerInterface {
14
public:
15
77814
  virtual ~FatalErrorHandlerInterface() = default;
16
  // Called when Envoy receives a fatal signal. Must be async-signal-safe: in
17
  // particular, it can't allocate memory.
18
  virtual void onFatalError(std::ostream& os) const PURE;
19

            
20
  virtual void
21
  runFatalActionsOnTrackedObject(const FatalAction::FatalActionPtrList& actions) const PURE;
22
};
23

            
24
namespace FatalErrorHandler {
25
/**
26
 * Add this handler to the list of functions which will be called if Envoy
27
 * receives a fatal signal.
28
 */
29
void registerFatalErrorHandler(const FatalErrorHandlerInterface& handler);
30

            
31
/**
32
 * Removes this handler from the list of functions which will be called if Envoy
33
 * receives a fatal signal.
34
 */
35
void removeFatalErrorHandler(const FatalErrorHandlerInterface& handler);
36

            
37
/**
38
 * Calls and unregisters the fatal error handlers registered with
39
 * registerFatalErrorHandler. This is async-signal-safe and intended to be
40
 * called from a fatal signal handler.
41
 */
42
void callFatalErrorHandlers(std::ostream& os);
43

            
44
/**
45
 * Creates the singleton FatalActionManager if not already created and
46
 * registers the specified actions to run on failure.
47
 */
48
void registerFatalActions(FatalAction::FatalActionPtrList safe_actions,
49
                          FatalAction::FatalActionPtrList unsafe_actions,
50
                          Thread::ThreadFactory& thread_factory);
51

            
52
/**
53
 * Tries to run all of the safe fatal actions. Only one thread will
54
 * be allowed to run the fatal functions.
55
 *
56
 * Returns a FatalAction::Status which the caller should use to
57
 * determine how to proceed.
58
 */
59
FatalAction::Status runSafeActions();
60

            
61
/**
62
 * Tries to run all of the unsafe fatal actions. Call this only
63
 * after calling runSafeActions.
64
 *
65
 * Returns a FatalAction::Status which the caller should use to determine
66
 * how to proceed.
67
 */
68
FatalAction::Status runUnsafeActions();
69

            
70
/**
71
 *  Clear the Fatal Actions at the end of the server's call to terminate().
72
 *  We should clean up the Fatal Action to prevent the memory from leaking.
73
 */
74
void clearFatalActionsOnTerminate();
75
} // namespace FatalErrorHandler
76
} // namespace Envoy