| 1 |
#include "LibLsp/lsp/ParentProcessWatcher.h" |
| 2 |
#include <algorithm> |
| 3 |
#include <cstdlib> // for std::system |
| 4 |
#include <thread> |
| 5 |
#include <chrono> |
| 6 |
#include <LibLsp/lsp/asio.h> |
| 7 |
#include <iostream> |
| 8 |
|
| 9 |
#ifdef _WIN32 |
| 10 |
#include <windows.h> // for Windows-specific system exit codes if needed |
| 11 |
#else |
| 12 |
#include <sys/wait.h> // for WIFEXITED / WEXITSTATUS |
| 13 |
#endif |
| 14 |
|
| 15 |
#include "LibLsp/lsp/ProcessIoService.h" |
| 16 |
#include "LibLsp/lsp/SimpleTimer.h" |
| 17 |
|
| 18 |
using namespace asio::ip; |
| 19 |
using namespace std; |
| 20 |
|
| 21 |
struct ParentProcessWatcher::ParentProcessWatcherData : std::enable_shared_from_this<ParentProcessWatcherData> |
| 22 |
{ |
| 23 |
std::unique_ptr<SimpleTimer<std::chrono::seconds>> timer; |
| 24 |
lsp::Log& _log; |
| 25 |
std::function<void()> on_exit; |
| 26 |
lsp::ProcessIoService asio_io; |
| 27 |
int pid; |
| 28 |
int const _poll_delay_secs; |
| 29 |
std::string command; |
| 30 |
|
| 31 |
ParentProcessWatcherData(lsp::Log& log, int _pid, std::function<void()> const&& callback, uint32_t poll_delay_secs) |
| 32 |
: _log(log), on_exit(callback), pid(_pid), _poll_delay_secs(poll_delay_secs) |
| 33 |
{ |
| 34 |
#ifdef _WIN32 |
| 35 |
command = |
| 36 |
"cmd /c \"tasklist /FI \"PID eq " + std::to_string(pid) + "\" | findstr " + std::to_string(pid) + "\""; |
| 37 |
#else |
| 38 |
command = "ps -p " + std::to_string(pid); |
| 39 |
#endif |
| 40 |
} |
| 41 |
|
| 42 |
void run() |
| 43 |
{ |
| 44 |
auto self = shared_from_this(); |
| 45 |
// launch the check in a detached thread so we don't block the ASIO event loop |
| 46 |
std::thread([self]() { |
| 47 |
int status = std::system(self->command.c_str()); |
| 48 |
int exit_code = -1; |
| 49 |
|
| 50 |
if (status == -1) { |
| 51 |
// system call failed entirely |
| 52 |
self->_log.log(lsp::Log::Level::WARNING, |
| 53 |
"System call failed for command: '" + self->command + "'."); |
| 54 |
exit_code = 2; // treat as generic error >1 to re-arm timer |
| 55 |
} else { |
| 56 |
#ifdef _WIN32 |
| 57 |
// On Windows, system() returns the program exit code directly |
| 58 |
exit_code = status; |
| 59 |
#else |
| 60 |
if (WIFEXITED(status)) |
| 61 |
exit_code = WEXITSTATUS(status); |
| 62 |
else |
| 63 |
exit_code = status; |
| 64 |
#endif |
| 65 |
} |
| 66 |
|
| 67 |
// now dispatch back into ASIO so timer and on_exit run in the right context |
| 68 |
asio::post(self->asio_io.getIOService(), [self, exit_code]() { |
| 69 |
if (exit_code == 1) // parent process not found |
| 70 |
{ |
| 71 |
if (self->on_exit) |
| 72 |
{ |
| 73 |
// small delay before notifying |
| 74 |
std::thread([self]() { |
| 75 |
std::this_thread::sleep_for(std::chrono::seconds(3)); |
| 76 |
asio::post(self->asio_io.getIOService(), [self]() { |
| 77 |
self->on_exit(); |
| 78 |
}); |
| 79 |
}).detach(); |
| 80 |
} |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
if (exit_code > 1) |
| 85 |
{ |
| 86 |
self->_log.log( |
| 87 |
lsp::Log::Level::WARNING, |
| 88 |
"The tasklist command: '" + self->command + "' returned " + std::to_string(exit_code) |
| 89 |
); |
| 90 |
} |
| 91 |
// re‑arm the poll timer |
| 92 |
self->timer = std::make_unique<SimpleTimer<std::chrono::seconds>>( |
| 93 |
self->_poll_delay_secs, |
| 94 |
[self]() { self->run(); } |
| 95 |
); |
| 96 |
} |
| 97 |
}); |
| 98 |
}).detach(); |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
ParentProcessWatcher::ParentProcessWatcher( |
| 103 |
lsp::Log& log, int pid, std::function<void()> const&& callback, uint32_t poll_delay_secs |
| 104 |
) |
| 105 |
: d_ptr(new ParentProcessWatcherData(log, pid, std::move(callback), poll_delay_secs)) |
| 106 |
{ |
| 107 |
d_ptr->run(); |
| 108 |
} |
| 109 |
|
| 110 |
ParentProcessWatcher::~ParentProcessWatcher() |
| 111 |
{ |
| 112 |
if (d_ptr->timer) |
| 113 |
{ |
| 114 |
d_ptr->timer->Stop(); |
| 115 |
} |
| 116 |
} |