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 SIGILL, SIGABRT, SIGBUS,
62 SIGFPE, SIGUSR1, SIGSEGV,
63 SIGUSR2, SIGTERM, SIGXCPU,
67 SIGFPE, SIGSEGV, SIGBUS,
87 const bool retrievable =
true;
89 result +=
"failed to re-gain root permissions... still give it a try\n";
100 const bool double_fork =
false;
116 ReadUntilGdbPrompt(fd_stdout);
120 const string gdb_cmd =
"bt all\n" "quit\n";
122 const string gdb_cmd =
"thread apply all bt\n" "quit\n";
126 ssize_t nbytes = write(fd_stdin, gdb_cmd.data(), gdb_cmd.length());
127 if ((nbytes < 0) || (
static_cast<unsigned>(nbytes) != gdb_cmd.length())) {
128 result +=
"failed to start gdb/lldb (" +
StringifyInt(nbytes) +
" bytes "
136 result += ReadUntilGdbPrompt(fd_stdout);
138 result += ReadUntilGdbPrompt(fd_stdout) +
"\n\n";
144 while (read(fd_stderr, &cbuf, 1) == 1)
145 result_err.push_back(cbuf);
146 if (!result_err.empty())
147 result +=
"\nError output:\n" + result_err +
"\n";
155 unsigned int timeout = 15;
157 while (timeout > 0 && waitpid(gdb_pid, &statloc, WNOHANG) != gdb_pid) {
164 result +=
"gdb did not exit as expected. sending SIGKILL... ";
165 result += (kill(gdb_pid, SIGKILL) != 0) ?
"failed\n" :
"okay\n";
173 if (instance_ != NULL) {
174 return instance_->watchdog_pid_;
184 char ctime_buffer[32];
186 if (!crash_dump_path_.empty()) {
187 FILE *fp = fopen(crash_dump_path_.c_str(),
"a");
189 time_t now = time(NULL);
190 msg +=
"\nTimestamp: " + string(ctime_r(&now, ctime_buffer));
191 if (fwrite(&msg[0], 1, msg.length(), fp) != msg.length()) {
193 " (failed to report into crash dump file " + crash_dump_path_ +
")";
195 msg +=
"\n Crash logged also on file: " + crash_dump_path_ +
"\n";
199 msg +=
" (failed to open crash dump file " + crash_dump_path_ +
")";
214 static const string gdb_prompt =
"(lldb)";
216 static const string gdb_prompt =
"\n(gdb) ";
222 unsigned int ring_buffer_pos = 0;
226 chars_io = read(fd_pipe, &mini_buffer, 1);
229 if (chars_io <= 0)
break;
231 result += mini_buffer;
234 if (mini_buffer == gdb_prompt[ring_buffer_pos]) {
236 if (ring_buffer_pos == gdb_prompt.size()) {
253 if (!pipe_watchdog_->TryRead<
CrashData>(&crash_data)) {
254 return "failed to read crash data (" +
StringifyInt(errno) +
")";
257 string debug =
"--\n";
260 debug +=
", version: " + string(CVMFS_VERSION);
262 debug +=
"Executable path: " + exe_path_ +
"\n";
264 debug += GenerateStackTrace(crash_data.
pid);
267 if (kill(crash_data.
pid, SIGKILL) != 0) {
268 debug +=
"Failed to kill cvmfs client! (";
271 debug +=
"invalid signal";
274 debug +=
"permission denied";
277 debug +=
"no such process";
290 int sig, siginfo_t *siginfo,
void * )
293 "watchdog: received unexpected signal %d from PID %d / UID %d",
294 sig, siginfo->si_pid, siginfo->si_uid);
300 int send_errno = errno;
308 (void) sigaction(SIGQUIT, &(Me()->old_signal_handlers_[sig]), NULL);
311 if (!Me()->pipe_watchdog_->Write(ControlFlow::kProduceStacktrace)) {
319 crash_data.
pid = getpid();
320 if (!Me()->pipe_watchdog_->Write<
CrashData>(crash_data)) {
330 if (++counter == 300) {
333 #if defined(CVMFS_FUSE_MODULE)
336 void *addr[kMaxBacktrace];
340 int num_addr = backtrace(addr, kMaxBacktrace);
341 char **symbols = backtrace_symbols(addr, num_addr);
342 string backtrace =
"Backtrace (" +
StringifyInt(num_addr) +
344 for (
int i = 0; i < num_addr; ++i)
345 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()) {
395 case -1:
PANIC(NULL);
401 pipe_watchdog_->CloseWriteFd();
404 pid_t watchdog_pid = getpid();
405 pipe_pid.
Write(watchdog_pid);
420 std::set<int> preserve_fds;
421 preserve_fds.insert(0);
422 preserve_fds.insert(1);
423 preserve_fds.insert(2);
424 preserve_fds.insert(pipe_watchdog_->GetReadFd());
425 preserve_fds.insert(pipe_listener_->
GetWriteFd());
430 if (WaitForSupervisee())
433 pipe_watchdog_->CloseReadFd();
441 pipe_watchdog_->CloseReadFd();
444 if (waitpid(pid, &statloc, 0) != pid)
PANIC(NULL);
445 if (!WIFEXITED(statloc) || WEXITSTATUS(statloc))
PANIC(NULL);
449 pipe_pid.
Read(&watchdog_pid_);
458 assert(rv_sig != SIG_ERR);
462 memset(&sa, 0,
sizeof(sa));
463 sa.sa_sigaction = ReportSignalAndTerminate;
464 sa.sa_flags = SA_SIGINFO;
465 sigfillset(&sa.sa_mask);
468 for (
size_t i = 0; i <
sizeof(g_suppressed_signals)/
sizeof(
int); i++) {
469 signal_handlers[g_suppressed_signals[i]] = sa;
471 SetSignalHandlers(signal_handlers);
475 if (!pipe_watchdog_->TryRead(&control_flow)) {
480 switch (control_flow) {
481 case ControlFlow::kQuit:
483 case ControlFlow::kSupervise:
486 LogEmergency(
"Internal error: invalid control flow");
491 pipe_watchdog_->Read(&size);
492 crash_dump_path_.resize(size);
494 pipe_watchdog_->Read(&crash_dump_path_[0], size);
498 LogEmergency(std::string(
"Cannot change to crash dump directory: ") +
514 "failed to allow ptrace() for watchdog (PID: %d). "
515 "Post crash stacktrace might not work",
520 int stack_size = kSignalHandlerStacksize;
521 sighandler_stack_.ss_sp = smalloc(stack_size);
522 sighandler_stack_.ss_size = stack_size;
523 sighandler_stack_.ss_flags = 0;
524 if (sigaltstack(&sighandler_stack_, NULL) != 0)
529 memset(&sa, 0,
sizeof(sa));
530 sa.sa_sigaction = SendTrace;
531 sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
532 sigfillset(&sa.sa_mask);
535 for (
size_t i = 0; i <
sizeof(g_crash_signals)/
sizeof(
int); i++) {
536 signal_handlers[g_crash_signals[i]] = sa;
538 old_signal_handlers_ = SetSignalHandlers(signal_handlers);
545 pipe_watchdog_->Write(ControlFlow::kSupervise);
546 size_t path_size = crash_dump_path.size();
547 pipe_watchdog_->Write(path_size);
549 pipe_watchdog_->Write(crash_dump_path.data(), path_size);
560 struct pollfd watch_fds[2];
562 watch_fds[0].events = 0;
563 watch_fds[0].revents = 0;
565 watch_fds[1].events = POLLIN | POLLPRI;
566 watch_fds[1].revents = 0;
568 int retval = poll(watch_fds, 2, -1);
574 if (watch_fds[1].revents)
577 if (watch_fds[0].revents) {
578 if ((watch_fds[0].revents & POLLERR) ||
579 (watch_fds[0].revents & POLLHUP) ||
580 (watch_fds[0].revents & POLLNVAL))
583 "watchdog disappeared, disabling stack trace reporting "
584 "(revents: %d / %d|%d|%d)",
585 watch_fds[0].revents, POLLERR, POLLHUP, POLLNVAL);
602 LogEmergency(
"watchdog: unexpected termination (" +
604 if (on_crash_) on_crash_();
606 switch (control_flow) {
607 case ControlFlow::kProduceStacktrace:
608 LogEmergency(ReportStacktrace());
609 if (on_crash_) on_crash_();
612 case ControlFlow::kQuit:
616 LogEmergency(
"watchdog: unexpected error");
627 , on_crash_(on_crash)
638 signal(SIGQUIT, SIG_DFL);
639 signal(SIGILL, SIG_DFL);
640 signal(SIGABRT, SIG_DFL);
641 signal(SIGFPE, SIG_DFL);
642 signal(SIGSEGV, SIG_DFL);
643 signal(SIGBUS, SIG_DFL);
644 signal(SIGPIPE, SIG_DFL);
645 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)
static void ReportSignalAndTerminate(int sig, siginfo_t *siginfo, void *context)
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")
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,...)