blob: 8ba5da072ea53bcb6c7ca5dec673fc3dcbc81735 [file]
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/developer/debug/shared/message_loop_linux.h"
#include <lib/syslog/cpp/macros.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <sys/wait.h>
#include <optional>
#include "src/lib/files/eintr_wrapper.h"
namespace debug {
struct MessageLoopLinux::SignalWatchInfo {
pid_t pid = -1;
SignalWatcher watcher;
};
MessageLoopLinux::MessageLoopLinux() {
// Register for signals from child processes. We may need to add to the set of signals in the
// future as requirements grow.
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
// Prevents the signals that are being processed via the signal fd from being sent via the normal
// signal channel.
sigprocmask(SIG_BLOCK, &mask, NULL);
signal_fd_.reset(signalfd(-1, &mask, 0));
}
MessageLoopLinux::~MessageLoopLinux() = default;
MessageLoopLinux* MessageLoopLinux::Current() {
return reinterpret_cast<MessageLoopLinux*>(MessageLoop::Current());
}
bool MessageLoopLinux::Init(std::string* error_message) {
if (!MessageLoopPoll::Init(error_message))
return false;
signal_fd_watch_ =
WatchFD(WatchMode::kRead, signal_fd_.get(), [this](int fd, bool readable, bool, bool) {
if (!readable)
return;
FX_DCHECK(fd == signal_fd_.get());
struct signalfd_siginfo fdsi;
auto nread = HANDLE_EINTR(read(signal_fd_.get(), &fdsi, sizeof(struct signalfd_siginfo)));
FX_DCHECK(nread == sizeof(signalfd_siginfo));
// Poll each watched child at most once per SIGCHLD wakeup, including watches added inside
// callbacks, without holding map iterators across callbacks or dropping coalesced events if
// an earlier callback requested loop exit.
std::optional<int> last_watch_id;
while (true) {
auto next_it =
last_watch_id ? signal_watches_.upper_bound(*last_watch_id) : signal_watches_.begin();
if (next_it == signal_watches_.end()) {
break;
}
int watch_id = next_it->first;
pid_t target_pid = next_it->second.pid;
last_watch_id = watch_id;
if (target_pid <= 0) {
continue;
}
int status = 0;
pid_t res = HANDLE_EINTR(waitpid(target_pid, &status, __WALL | WUNTRACED | WNOHANG));
if (res <= 0) {
continue;
}
auto it = signal_watches_.find(watch_id);
if (it == signal_watches_.end()) {
continue;
}
// Keep the watcher callable alive on the stack in case the callback removes the watch.
auto watcher = it->second.watcher.share();
watcher(res, status);
}
});
return true;
}
void MessageLoopLinux::Cleanup() {
// Force unregister our signal watch before cleaning up current MessageLoop.
signal_fd_watch_ = WatchHandle();
MessageLoopPoll::Cleanup();
}
MessageLoop::WatchHandle MessageLoopLinux::WatchChildSignals(pid_t pid, SignalWatcher watcher) {
// The dispatch code for watch callbacks requires this be called on the same thread as the message
// loop is.
FX_DCHECK(Current() == static_cast<MessageLoop*>(this));
FX_DCHECK(pid > 0);
SignalWatchInfo info;
info.pid = pid;
info.watcher = std::move(watcher);
// The reason this function must be called on the message loop thread is that otherwise adding a
// new watch would require synchronously breaking out of the existing poll() call to add the new
// handle and then resuming it.
int watch_id = GetNextWatchId();
signal_watches_[watch_id] = std::move(info);
return WatchHandle(this, watch_id);
}
void MessageLoopLinux::StopWatching(int id) {
// The dispatch code for watch callbacks requires this be called on the
// same thread as the message loop is.
FX_DCHECK(MessageLoopLinux::Current() == this);
{
std::lock_guard<std::mutex> guard(mutex_);
// Check for signal watches.
auto found = signal_watches_.find(id);
if (found != signal_watches_.end()) {
signal_watches_.erase(found);
return;
}
}
// All other watches are removed by the base class.
MessageLoopPoll::StopWatching(id);
}
} // namespace debug