1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#ifndef CVMFS_MONITOR_H_ |
6 |
|
|
#define CVMFS_MONITOR_H_ |
7 |
|
|
|
8 |
|
|
#include <signal.h> |
9 |
|
|
#include <sys/types.h> |
10 |
|
|
#include <unistd.h> |
11 |
|
|
|
12 |
|
|
#include <map> |
13 |
|
|
#include <string> |
14 |
|
|
|
15 |
|
|
#include "platform.h" |
16 |
|
|
|
17 |
|
|
struct Pipe; |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* This class can fork a watchdog process that listens on a pipe and prints a |
21 |
|
|
* stackstrace into syslog, when cvmfs fails. The crash dump is also appended |
22 |
|
|
* to the crash dump file, if the path is not empty. Singleton. |
23 |
|
|
*/ |
24 |
|
|
class Watchdog { |
25 |
|
|
public: |
26 |
|
|
static Watchdog *Create(const std::string &crash_dump_path); |
27 |
|
|
static pid_t GetPid(); |
28 |
|
|
~Watchdog(); |
29 |
|
|
void Spawn(); |
30 |
|
|
void RegisterOnCrash(void (*CleanupOnCrash)(void)); |
31 |
|
|
|
32 |
|
|
private: |
33 |
|
|
typedef std::map<int, struct sigaction> SigactionMap; |
34 |
|
|
|
35 |
|
|
struct CrashData { |
36 |
|
|
int signal; |
37 |
|
|
int sys_errno; |
38 |
|
|
pid_t pid; |
39 |
|
|
}; |
40 |
|
|
|
41 |
|
|
struct ControlFlow { |
42 |
|
|
enum Flags { |
43 |
|
|
kProduceStacktrace = 0, |
44 |
|
|
kQuit, |
45 |
|
|
kUnknown, |
46 |
|
|
}; |
47 |
|
|
}; |
48 |
|
|
|
49 |
|
|
/** |
50 |
|
|
* Preallocated memory block to make sure that signal handler don't run into |
51 |
|
|
* stack overflows. |
52 |
|
|
*/ |
53 |
|
|
static const unsigned kSignalHandlerStacksize = 2 * 1024 * 1024; // 2 MB |
54 |
|
|
/** |
55 |
|
|
* If the GDB/LLDB method of generating a stack trace fails, fall back to |
56 |
|
|
* libc's backtrace with a maximum depth. |
57 |
|
|
*/ |
58 |
|
|
static const unsigned kMaxBacktrace = 64; |
59 |
|
|
|
60 |
|
|
static Watchdog *instance_; |
61 |
|
|
static Watchdog *Me() { return instance_; } |
62 |
|
|
|
63 |
|
|
static void SendTrace(int sig, siginfo_t *siginfo, void *context); |
64 |
|
|
|
65 |
|
|
explicit Watchdog(const std::string &crash_dump_path); |
66 |
|
|
SigactionMap SetSignalHandlers(const SigactionMap &signal_handlers); |
67 |
|
|
void Supervise(); |
68 |
|
|
void LogEmergency(std::string msg); |
69 |
|
|
std::string ReportStacktrace(); |
70 |
|
|
std::string GenerateStackTrace(const std::string &exe_path, pid_t pid); |
71 |
|
|
std::string ReadUntilGdbPrompt(int fd_pipe); |
72 |
|
|
|
73 |
|
|
bool spawned_; |
74 |
|
|
std::string crash_dump_path_; |
75 |
|
|
std::string exe_path_; |
76 |
|
|
pid_t watchdog_pid_; |
77 |
|
|
Pipe *pipe_watchdog_; |
78 |
|
|
void (*on_crash_)(void); |
79 |
|
|
platform_spinlock lock_handler_; |
80 |
|
|
stack_t sighandler_stack_; |
81 |
|
|
SigactionMap old_signal_handlers_; |
82 |
|
|
}; |
83 |
|
|
|
84 |
|
|
namespace monitor { |
85 |
|
|
// TODO(jblomer): move me |
86 |
|
|
unsigned GetMaxOpenFiles(); |
87 |
|
|
} // namespace monitor |
88 |
|
|
|
89 |
|
|
#endif // CVMFS_MONITOR_H_ |