19 #include <sys/resource.h>
20 #include <sys/types.h>
22 #include <sys/ucontext.h>
41 #if defined(CVMFS_FUSE_MODULE)
52 #if defined(CVMFS_FUSE_MODULE)
61 SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGBUS, SIGFPE,
62 SIGUSR1, SIGSEGV, SIGUSR2, SIGTERM, SIGXCPU, SIGXFSZ};
65 SIGSEGV, SIGBUS, SIGPIPE, SIGXFSZ};
84 const bool retrievable =
true;
86 result +=
"failed to re-gain root permissions... still give it a try\n";
97 const bool double_fork =
false;
113 ReadUntilGdbPrompt(fd_stdout);
117 const string gdb_cmd =
"bt all\n"
120 const string gdb_cmd =
"thread apply all bt\n"
125 ssize_t nbytes = write(fd_stdin, gdb_cmd.data(), gdb_cmd.length());
126 if ((nbytes < 0) || (
static_cast<unsigned>(nbytes) != gdb_cmd.length())) {
127 result +=
"failed to start gdb/lldb (" +
StringifyInt(nbytes)
137 result += ReadUntilGdbPrompt(fd_stdout);
139 result += ReadUntilGdbPrompt(fd_stdout) +
"\n\n";
145 while (read(fd_stderr, &cbuf, 1) == 1)
146 result_err.push_back(cbuf);
147 if (!result_err.empty())
148 result +=
"\nError output:\n" + result_err +
"\n";
156 unsigned int timeout = 15;
158 while (timeout > 0 && waitpid(gdb_pid, &statloc, WNOHANG) != gdb_pid) {
165 result +=
"gdb did not exit as expected. sending SIGKILL... ";
166 result += (kill(gdb_pid, SIGKILL) != 0) ?
"failed\n" :
"okay\n";
174 if (instance_ != NULL) {
175 return instance_->watchdog_pid_;
185 char ctime_buffer[32];
187 if (!crash_dump_path_.empty()) {
188 FILE *fp = fopen(crash_dump_path_.c_str(),
"a");
190 time_t now = time(NULL);
191 msg +=
"\nTimestamp: " + string(ctime_r(&now, ctime_buffer));
192 if (fwrite(&msg[0], 1, msg.length(), fp) != msg.length()) {
193 msg +=
" (failed to report into crash dump file " + crash_dump_path_
196 msg +=
"\n Crash logged also on file: " + crash_dump_path_ +
"\n";
200 msg +=
" (failed to open crash dump file " + crash_dump_path_ +
")";
215 static const string gdb_prompt =
"(lldb)";
217 static const string gdb_prompt =
"\n(gdb) ";
223 unsigned int ring_buffer_pos = 0;
227 chars_io = read(fd_pipe, &mini_buffer, 1);
233 result += mini_buffer;
236 if (mini_buffer == gdb_prompt[ring_buffer_pos]) {
238 if (ring_buffer_pos == gdb_prompt.size()) {
255 if (!pipe_watchdog_->TryRead<
CrashData>(&crash_data)) {
256 return "failed to read crash data (" +
StringifyInt(errno) +
")";
259 string debug =
"--\n";
262 debug +=
", version: " + string(CVMFS_VERSION);
264 debug +=
"Executable path: " + exe_path_ +
"\n";
266 debug += GenerateStackTrace(crash_data.
pid);
269 if (kill(crash_data.
pid, SIGKILL) != 0) {
270 debug +=
"Failed to kill cvmfs client! (";
273 debug +=
"invalid signal";
276 debug +=
"permission denied";
279 debug +=
"no such process";
294 "watchdog: received unexpected signal %d from PID %d / UID %d", sig,
295 siginfo->si_pid, siginfo->si_uid);
300 int send_errno = errno;
309 (void)sigaction(SIGQUIT, &(Me()->old_signal_handlers_[sig]), NULL);
312 if (!Me()->pipe_watchdog_->Write(ControlFlow::kProduceStacktrace)) {
320 crash_data.
pid = getpid();
321 if (!Me()->pipe_watchdog_->Write<
CrashData>(crash_data)) {
331 if (++counter == 300) {
334 #if defined(CVMFS_FUSE_MODULE)
337 void *addr[kMaxBacktrace];
341 int num_addr = backtrace(addr, kMaxBacktrace);
342 char **symbols = backtrace_symbols(addr, num_addr);
343 string backtrace =
"Backtrace (" +
StringifyInt(num_addr)
345 for (
int i = 0; i < num_addr; ++i)
346 backtrace +=
string(symbols[i]) +
"\n";
370 SigactionMap::const_iterator i = signal_handlers.begin();
371 SigactionMap::const_iterator iend = signal_handlers.end();
372 for (; i != iend; ++i) {
373 struct sigaction old_signal_handler;
374 if (sigaction(i->first, &i->second, &old_signal_handler) != 0) {
377 old_signal_handlers[i->first] = old_signal_handler;
380 return old_signal_handlers;
394 switch (pid = fork()) {
403 pipe_watchdog_->CloseWriteFd();
406 pid_t watchdog_pid = getpid();
407 pipe_pid.
Write(watchdog_pid);
422 std::set<int> preserve_fds;
423 preserve_fds.insert(0);
424 preserve_fds.insert(1);
425 preserve_fds.insert(2);
426 preserve_fds.insert(pipe_watchdog_->GetReadFd());
427 preserve_fds.insert(pipe_listener_->
GetWriteFd());
432 if (WaitForSupervisee())
435 pipe_watchdog_->CloseReadFd();
443 pipe_watchdog_->CloseReadFd();
446 if (waitpid(pid, &statloc, 0) != pid)
448 if (!WIFEXITED(statloc) || WEXITSTATUS(statloc))
453 pipe_pid.
Read(&watchdog_pid_);
462 assert(rv_sig != SIG_ERR);
466 memset(&sa, 0,
sizeof(sa));
467 sa.sa_sigaction = ReportSignalAndContinue;
468 sa.sa_flags = SA_SIGINFO;
469 sigfillset(&sa.sa_mask);
472 for (
size_t i = 0; i <
sizeof(g_suppressed_signals) /
sizeof(
int); i++) {
473 signal_handlers[g_suppressed_signals[i]] = sa;
475 SetSignalHandlers(signal_handlers);
479 if (!pipe_watchdog_->TryRead(&control_flow)) {
484 switch (control_flow) {
485 case ControlFlow::kQuit:
487 case ControlFlow::kSupervise:
490 LogEmergency(
"Internal error: invalid control flow");
495 pipe_watchdog_->Read(&size);
496 crash_dump_path_.resize(size);
498 pipe_watchdog_->Read(&crash_dump_path_[0], size);
502 LogEmergency(std::string(
"Cannot change to crash dump directory: ")
518 "failed to allow ptrace() for watchdog (PID: %d). "
519 "Post crash stacktrace might not work",
524 int stack_size = kSignalHandlerStacksize;
525 sighandler_stack_.ss_sp = smalloc(stack_size);
526 sighandler_stack_.ss_size = stack_size;
527 sighandler_stack_.ss_flags = 0;
528 if (sigaltstack(&sighandler_stack_, NULL) != 0)
533 memset(&sa, 0,
sizeof(sa));
534 sa.sa_sigaction = SendTrace;
535 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
536 sigfillset(&sa.sa_mask);
539 for (
size_t i = 0; i <
sizeof(g_crash_signals) /
sizeof(
int); i++) {
540 signal_handlers[g_crash_signals[i]] = sa;
542 old_signal_handlers_ = SetSignalHandlers(signal_handlers);
549 pipe_watchdog_->Write(ControlFlow::kSupervise);
550 size_t path_size = crash_dump_path.size();
551 pipe_watchdog_->Write(path_size);
553 pipe_watchdog_->Write(crash_dump_path.data(), path_size);
564 struct pollfd watch_fds[2];
566 watch_fds[0].events = 0;
567 watch_fds[0].revents = 0;
569 watch_fds[1].events = POLLIN | POLLPRI;
570 watch_fds[1].revents = 0;
572 int retval = poll(watch_fds, 2, -1);
578 if (watch_fds[1].revents)
581 if (watch_fds[0].revents) {
582 if ((watch_fds[0].revents & POLLERR) || (watch_fds[0].revents & POLLHUP)
583 || (watch_fds[0].revents & POLLNVAL)) {
585 "watchdog disappeared, disabling stack trace reporting "
586 "(revents: %d / %d|%d|%d)",
587 watch_fds[0].revents, POLLERR, POLLHUP, POLLNVAL);
604 LogEmergency(
"watchdog: unexpected termination ("
609 switch (control_flow) {
610 case ControlFlow::kProduceStacktrace:
611 LogEmergency(ReportStacktrace());
616 case ControlFlow::kQuit:
620 LogEmergency(
"watchdog: unexpected error");
631 , on_crash_(on_crash) {
641 signal(SIGQUIT, SIG_DFL);
642 signal(SIGILL, SIG_DFL);
643 signal(SIGABRT, SIG_DFL);
644 signal(SIGFPE, SIG_DFL);
645 signal(SIGSEGV, SIG_DFL);
646 signal(SIGBUS, SIG_DFL);
647 signal(SIGPIPE, SIG_DFL);
648 signal(SIGXFSZ, SIG_DFL);
bool Write(const T &data)
std::map< int, struct sigaction > SigactionMap
std::string GenerateStackTrace(pid_t pid)
std::string ReportStacktrace()
UniquePtr< Pipe< kPipeThreadTerminator > > pipe_terminate_
Send the terminate signal to the listener.
NameString GetFileName(const PathString &path)
SigactionMap old_signal_handlers_
static void SendTrace(int sig, siginfo_t *siginfo, void *context)
static void * MainWatchdogListener(void *data)
void LogEmergency(std::string msg)
assert((mem||(size==0))&&"Out Of Memory")
static void ReportSignalAndContinue(int sig, siginfo_t *siginfo, void *context)
void SetLogMicroSyslog(const std::string &filename)
static Watchdog * instance_
#define SetLogDebugFile(filename)
static int g_crash_signals[8]
stack_t sighandler_stack_
static Watchdog * Create(FnOnCrash on_crash)
pthread_t thread_listener_
Watchdog(FnOnCrash on_crash)
#define GetLogDebugFile()
std::string ReadUntilGdbPrompt(int fd_pipe)
static void * MainWatchdogListener(void *data)
SigactionMap SetSignalHandlers(const SigactionMap &signal_handlers)
static int g_suppressed_signals[13]
string StringifyInt(const int64_t value)
UniquePtr< Pipe< kPipeWatchdogSupervisor > > pipe_listener_
The supervisee makes sure its watchdog does not die.
std::string GetLogMicroSyslog()
UniquePtr< Pipe< kPipeWatchdog > > pipe_watchdog_
bool ExecuteBinary(int *fd_stdin, int *fd_stdout, int *fd_stderr, const std::string &binary_path, const std::vector< std::string > &argv, const bool double_fork, pid_t *child_pid)
bool CloseAllFildes(const std::set< int > &preserve_fildes)
bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily)
PathString GetParentPath(const PathString &path)
void SafeSleepMs(const unsigned ms)
void Spawn(const std::string &crash_dump_path)
void Block2Nonblock(int filedes)
platform_spinlock lock_handler_
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)