Skip to content

Commit

Permalink
[sshfs] Use optional for watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
Sploder12 committed Nov 27, 2024
1 parent baca578 commit de690f1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion include/multipass/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ std::unique_ptr<Process> make_sshfs_server_process(const SSHFSServerConfig& conf
std::unique_ptr<Process> make_process(std::unique_ptr<ProcessSpec>&& process_spec);
int symlink_attr_from(const char* path, sftp_attributes_struct* attr);

std::function<int(const std::function<bool()>&)> make_quit_watchdog(
std::function<std::optional<int>(const std::function<bool()>&)> make_quit_watchdog(
const std::chrono::milliseconds& timeout); // call while single-threaded; call result later, in dedicated thread

std::string reinterpret_interface_id(const std::string& ux_id); // give platforms a chance to reinterpret network IDs
Expand Down
8 changes: 4 additions & 4 deletions src/platform/platform_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,17 @@ timespec make_timespec(std::chrono::duration<Rep, Period> duration)
return out;
}

std::function<int(const std::function<bool()>&)> mp::platform::make_quit_watchdog(
std::function<std::optional<int>(const std::function<bool()>&)> mp::platform::make_quit_watchdog(
const std::chrono::milliseconds& timeout)
{
return [sigset = make_and_block_signals({SIGQUIT, SIGTERM, SIGHUP}),
time = make_timespec(timeout)](const std::function<bool()>& condition) {
time = make_timespec(timeout)](const std::function<bool()>& condition) -> std::optional<int> {
while (condition())
{
if (const int sig = sigtimedwait(&sigset, nullptr, &time); sig != -1)
return sig;
}

return -1;
return std::nullopt;
};
}
}
8 changes: 4 additions & 4 deletions src/sshfs_mount/sshfs_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ int main(int argc, char* argv[])
mp::SshfsMount sshfs_mount(std::move(session), source_path, target_path, gid_mappings, uid_mappings);

// ssh lives on its own thread, use this thread to listen for quit signal
int sig = watchdog([&sshfs_mount] { return sshfs_mount.alive(); });
auto sig = watchdog([&sshfs_mount] { return sshfs_mount.alive(); });

if (sig != -1)
cout << "Received signal " << sig << ". Stopping" << endl;
if (sig.has_value())
cout << "Received signal " << *sig << ". Stopping" << endl;
else
cout << "SFTP server thread stopped unexpectedly." << endl;

sshfs_mount.stop();
exit(sig == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
exit(sig.has_value() ? EXIT_SUCCESS : EXIT_FAILURE);
}
catch (const mp::SSHFSMissingError&)
{
Expand Down

0 comments on commit de690f1

Please sign in to comment.