Implement graceful termination and group-based app tracking

This commit is contained in:
Cameron Gutman
2024-01-07 16:27:14 -06:00
parent d05a67166e
commit 593e170da8
8 changed files with 262 additions and 10 deletions

View File

@@ -249,6 +249,33 @@ namespace platf {
lifetime::exit_sunshine(0, true);
}
/**
* @brief Attempt to gracefully terminate a process group.
* @param native_handle The process group ID.
* @return true if termination was successfully requested.
*/
bool
request_process_group_exit(std::uintptr_t native_handle) {
if (kill(-((pid_t) native_handle), SIGTERM) == 0 || errno == ESRCH) {
BOOST_LOG(debug) << "Successfully sent SIGTERM to process group: "sv << native_handle;
return true;
}
else {
BOOST_LOG(warning) << "Unable to send SIGTERM to process group ["sv << native_handle << "]: "sv << errno;
return false;
}
}
/**
* @brief Checks if a process group still has running children.
* @param native_handle The process group ID.
* @return true if processes are still running.
*/
bool
process_group_running(std::uintptr_t native_handle) {
return waitpid(-((pid_t) native_handle), nullptr, WNOHANG) >= 0;
}
struct sockaddr_in
to_sockaddr(boost::asio::ip::address_v4 address, uint16_t port) {
struct sockaddr_in saddr_v4 = {};