Skip to content

Commit

Permalink
[sshfs] Change watchdog to not use sigtimedwait
Browse files Browse the repository at this point in the history
  • Loading branch information
Sploder12 committed Dec 5, 2024
1 parent 848a4e5 commit 3df817f
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/platform/platform_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <multipass/utils.h>

#include <grp.h>
#include <scope_guard.hpp>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
Expand Down Expand Up @@ -185,14 +186,34 @@ timespec make_timespec(std::chrono::duration<Rep, Period> duration)
std::function<std::optional<int>(const std::function<bool()>&)> mp::platform::make_quit_watchdog(

Check warning on line 186 in src/platform/platform_unix.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/platform_unix.cpp#L186

Added line #L186 was not covered by tests
const std::chrono::milliseconds& timeout)
{
return [sigset = make_and_block_signals({SIGQUIT, SIGTERM, SIGHUP}),
return [sigset = make_and_block_signals({SIGQUIT, SIGTERM, SIGHUP, SIGUSR2}),
time = make_timespec(timeout)](const std::function<bool()>& condition) -> std::optional<int> {

Check warning on line 190 in src/platform/platform_unix.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/platform_unix.cpp#L189-L190

Added lines #L189 - L190 were not covered by tests
while (condition())
// create a timer to send SIGUSR2 which unblocks this thread
sigevent sevp{};
sevp.sigev_notify = SIGEV_SIGNAL;
sevp.sigev_signo = SIGUSR2;

Check warning on line 194 in src/platform/platform_unix.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/platform_unix.cpp#L192-L194

Added lines #L192 - L194 were not covered by tests

timer_t timer{};
if (timer_create(CLOCK_MONOTONIC, &sevp, &timer) == -1)
throw std::runtime_error(fmt::format("Could not create timer: {}", strerror(errno)));

Check warning on line 198 in src/platform/platform_unix.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/platform_unix.cpp#L196-L198

Added lines #L196 - L198 were not covered by tests

// scope guard to make sure the timer is deleted once it's no longer needed
const auto timer_guard = sg::make_scope_guard([timer]() noexcept { timer_delete(timer); });

Check warning on line 201 in src/platform/platform_unix.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/platform_unix.cpp#L201

Added line #L201 was not covered by tests

// set timer interval and initial time to provided timeout
const itimerspec timer_interval{time, time};
if (timer_settime(timer, 0, &timer_interval, nullptr) == -1)
throw std::runtime_error(fmt::format("Could not set timer: {}", strerror(errno)));

Check warning on line 206 in src/platform/platform_unix.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/platform_unix.cpp#L204-L206

Added lines #L204 - L206 were not covered by tests

// wait on signals and condition
int sig = SIGUSR2;
while (sig == SIGUSR2 && condition())

Check warning on line 210 in src/platform/platform_unix.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/platform_unix.cpp#L209-L210

Added lines #L209 - L210 were not covered by tests
{
if (const int sig = sigtimedwait(&sigset, nullptr, &time); sig != -1)
return sig;
// can't use sigtimedwait since macOS doesn't support it
sigwait(&sigset, &sig);

Check warning on line 213 in src/platform/platform_unix.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/platform_unix.cpp#L213

Added line #L213 was not covered by tests
}

return std::nullopt;
// if sig is SIGUSR2 then we know we're exiting because condition() was false
return sig == SIGUSR2 ? std::nullopt : std::make_optional(sig);

Check warning on line 217 in src/platform/platform_unix.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/platform_unix.cpp#L217

Added line #L217 was not covered by tests
};
}

0 comments on commit 3df817f

Please sign in to comment.