Skip to content

Commit d171dac

Browse files
committed
[ssh] Treat stfp thread exit as failure
1 parent 0f9ec1a commit d171dac

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

include/multipass/platform.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ std::unique_ptr<Process> make_sshfs_server_process(const SSHFSServerConfig& conf
9191
std::unique_ptr<Process> make_process(std::unique_ptr<ProcessSpec>&& process_spec);
9292
int symlink_attr_from(const char* path, sftp_attributes_struct* attr);
9393

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

9897
std::string reinterpret_interface_id(const std::string& ux_id); // give platforms a chance to reinterpret network IDs
9998

src/platform/platform_unix.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,17 @@ timespec make_timespec(std::chrono::duration<Rep, Period> duration)
186186
return out;
187187
}
188188

189-
std::function<int()> mp::platform::make_quit_watchdog(const std::chrono::milliseconds& timeout,
190-
const std::function<bool()>& condition)
189+
std::function<int(const std::function<bool()>&)> mp::platform::make_quit_watchdog(
190+
const std::chrono::milliseconds& timeout)
191191
{
192-
return [sigset = make_and_block_signals({SIGQUIT, SIGTERM, SIGHUP}), time = make_timespec(timeout), condition]() {
192+
return [sigset = make_and_block_signals({SIGQUIT, SIGTERM, SIGHUP}),
193+
time = make_timespec(timeout)](const std::function<bool()>& condition) {
193194
while (condition())
194195
{
195196
if (const int sig = sigtimedwait(&sigset, nullptr, &time); sig != -1)
196197
return sig;
197198
}
198199

199-
return SIGCHLD;
200+
return -1;
200201
};
201202
}

src/sshfs_mount/sshfs_server.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,22 @@ int main(int argc, char* argv[])
105105

106106
try
107107
{
108+
auto watchdog =
109+
mpp::make_quit_watchdog(std::chrono::milliseconds{500}); // called while there is only one thread
110+
108111
mp::SSHSession session{host, port, username, mp::SSHClientKeyProvider{priv_key_blob}};
109112
mp::SshfsMount sshfs_mount(std::move(session), source_path, target_path, gid_mappings, uid_mappings);
110113

111-
auto watchdog = mpp::make_quit_watchdog(std::chrono::milliseconds{2500}, [&sshfs_mount] {
112-
return sshfs_mount.alive();
113-
}); // called while there is only one thread
114-
115114
// ssh lives on its own thread, use this thread to listen for quit signal
116-
if (int sig = watchdog())
115+
int sig = watchdog([&sshfs_mount] { return sshfs_mount.alive(); });
116+
117+
if (sig != -1)
117118
cout << "Received signal " << sig << ". Stopping" << endl;
119+
else
120+
cout << "SFTP server thread stopped unexpectedly." << endl;
118121

119122
sshfs_mount.stop();
120-
exit(0);
123+
exit(sig == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
121124
}
122125
catch (const mp::SSHFSMissingError&)
123126
{

0 commit comments

Comments
 (0)