diff --git a/src/wslbridge2-backend.cpp b/src/wslbridge2-backend.cpp
index ad60a1f..13dceae 100644
--- a/src/wslbridge2-backend.cpp
+++ b/src/wslbridge2-backend.cpp
@@ -306,6 +306,7 @@ int main(int argc, char *argv[])
act.sa_sigaction = [](int x, siginfo_t *y, void *z) { wait(NULL); };
sigaction(SIGCHLD, &act, NULL);
+ int childExitStatus = -1;
int mfd;
char ptyname[16];
const pid_t child = forkpty(&mfd, ptyname, NULL, &winp);
@@ -372,6 +373,18 @@ int main(int argc, char *argv[])
/* Shutdown I/O sockets when child process terminates */
if (fds[2].revents & (POLLERR | POLLHUP))
{
+ int status;
+ if (wait(&status))
+ {
+ if (WIFEXITED(status))
+ {
+ childExitStatus = WEXITSTATUS(status);
+ printf("exit-status 0x%x 0x%x\n", childExitStatus, status);
+ }
+ } else {
+ perror("wait");
+ }
+
for (size_t i = 0; i < ARRAYSIZE(ioSockets.sock); i++)
shutdown(ioSockets.sock[i], SHUT_RDWR);
@@ -454,7 +467,7 @@ int main(int argc, char *argv[])
* Do not use exit() because it performs clean-up
* related to user-mode constructs in the library
*/
- _exit(0);
+ _exit(-1);
}
else
perror("fork");
@@ -463,5 +476,5 @@ int main(int argc, char *argv[])
for (size_t i = 0; i < ARRAYSIZE(ioSockets.sock); i++)
close(ioSockets.sock[i]);
- return 0;
+ return childExitStatus;
}
diff --git a/src/wslbridge2.cpp b/src/wslbridge2.cpp
index f0b49ae..595a22e 100644
--- a/src/wslbridge2.cpp
+++ b/src/wslbridge2.cpp
@@ -481,7 +481,8 @@ int main(int argc, char *argv[])
if (!ret)
fatal("SetHandleInformation: %s", GetErrorMessage(GetLastError()).c_str());
- const auto watchdog = std::thread([&]() {
+ DWORD exitCode = 0;
+ auto watchdog = std::thread([&]() {
WaitForSingleObject(pi.hProcess, INFINITE);
std::vector<char> outVec = readAllFromHandle(outputPipe.rh);
@@ -501,6 +502,10 @@ int main(int argc, char *argv[])
msg.append(err);
}
+ if (!GetExitCodeProcess(pi.hProcess, &exitCode)) {
+ exitCode = -1;
+ }
+
/* Exit with error if output/error message is not empty. */
if (!msg.empty())
termState.fatal("%s", msg.c_str());
@@ -546,6 +551,8 @@ int main(int argc, char *argv[])
ret = pthread_create(&tidOutput, nullptr, receive_buffer, nullptr);
assert(ret == 0);
+ watchdog.join();
+
termState.enterRawMode();
/*
@@ -562,5 +569,5 @@ int main(int argc, char *argv[])
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
WSACleanup();
- termState.exitCleanly(0);
+ termState.exitCleanly(exitCode);
}