CernVM-FS  2.11.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
monitor.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_MONITOR_H_
6 #define CVMFS_MONITOR_H_
7 
8 #include <pthread.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 
13 #include <map>
14 #include <string>
15 
16 #include "util/platform.h"
17 #include "util/pointer.h"
18 #include "util/single_copy.h"
19 
20 struct Pipe;
21 
35  public:
39  typedef void (*FnOnCrash)(void);
40 
41  static Watchdog *Create(FnOnCrash on_crash);
42  static pid_t GetPid();
43  ~Watchdog();
44  void Spawn(const std::string &crash_dump_path);
45 
46  private:
47  typedef std::map<int, struct sigaction> SigactionMap;
48 
49  struct CrashData {
50  int signal;
51  int sys_errno;
52  pid_t pid;
53  };
54 
55  struct ControlFlow {
56  enum Flags {
61  };
62  };
63 
68  static const unsigned kSignalHandlerStacksize = 2 * 1024 * 1024; // 2 MB
73  static const unsigned kMaxBacktrace = 64;
74 
76  static Watchdog *Me() { return instance_; }
77 
78  static void *MainWatchdogListener(void *data);
79 
80  static void ReportSignalAndTerminate(int sig, siginfo_t *siginfo,
81  void *context);
82  static void SendTrace(int sig, siginfo_t *siginfo, void *context);
83 
84  explicit Watchdog(FnOnCrash on_crash);
85  void Fork();
86  bool WaitForSupervisee();
87  SigactionMap SetSignalHandlers(const SigactionMap &signal_handlers);
88  void Supervise();
89  void LogEmergency(std::string msg);
90  std::string ReportStacktrace();
91  std::string GenerateStackTrace(pid_t pid);
92  std::string ReadUntilGdbPrompt(int fd_pipe);
93 
94  bool spawned_;
95  std::string crash_dump_path_;
96  std::string exe_path_;
104  pthread_t thread_listener_;
109 };
110 
111 #endif // CVMFS_MONITOR_H_
void(* FnOnCrash)(void)
Definition: monitor.h:39
void Supervise()
Definition: monitor.cc:603
std::map< int, struct sigaction > SigactionMap
Definition: monitor.h:47
std::string GenerateStackTrace(pid_t pid)
Definition: monitor.cc:73
std::string ReportStacktrace()
Definition: monitor.cc:242
static void ReportSignalAndTerminate(int sig, siginfo_t *siginfo, void *context)
Definition: monitor.cc:280
SigactionMap old_signal_handlers_
Definition: monitor.h:108
static void SendTrace(int sig, siginfo_t *siginfo, void *context)
Definition: monitor.cc:290
static Watchdog * Me()
Definition: monitor.h:76
void LogEmergency(std::string msg)
Definition: monitor.cc:174
std::string exe_path_
Definition: monitor.h:96
static Watchdog * instance_
Definition: monitor.h:75
UniquePtr< Pipe > pipe_listener_
The supervisee makes sure its watchdog does not die.
Definition: monitor.h:101
FnOnCrash on_crash_
Definition: monitor.h:105
stack_t sighandler_stack_
Definition: monitor.h:107
static pid_t GetPid()
Definition: monitor.cc:163
static Watchdog * Create(FnOnCrash on_crash)
Definition: monitor.cc:61
pthread_t thread_listener_
Definition: monitor.h:104
Watchdog(FnOnCrash on_crash)
Definition: monitor.cc:628
pthread_spinlock_t platform_spinlock
UniquePtr< Pipe > pipe_watchdog_
Communication channel from the supervisee to the watchdog.
Definition: monitor.h:99
std::string ReadUntilGdbPrompt(int fd_pipe)
Definition: monitor.cc:203
static void * MainWatchdogListener(void *data)
Definition: monitor.cc:561
bool WaitForSupervisee()
Definition: monitor.cc:445
void Fork()
Definition: monitor.cc:378
SigactionMap SetSignalHandlers(const SigactionMap &signal_handlers)
Definition: monitor.cc:357
UniquePtr< Pipe > pipe_terminate_
Send the terminate signal to the listener.
Definition: monitor.h:103
bool spawned_
Definition: monitor.h:94
static const unsigned kSignalHandlerStacksize
Definition: monitor.h:68
~Watchdog()
Definition: monitor.cc:640
Definition: posix.h:200
void Spawn(const std::string &crash_dump_path)
Definition: monitor.cc:511
pid_t watchdog_pid_
Definition: monitor.h:97
platform_spinlock lock_handler_
Definition: monitor.h:106
std::string crash_dump_path_
Definition: monitor.h:95
static const unsigned kMaxBacktrace
Definition: monitor.h:73