粒子连接器修改
This commit is contained in:
1 parent
bd0771747d
commit
8ab3672c4e
5 files changed
+170
-6
No files matched your search
@@ -5,6 +5,8 @@
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <fcntl.h>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
@@ -12,6 +14,38 @@
|
||||
#include <unistd.h>
|
||||
|
||||
namespace connector {
|
||||
namespace {
|
||||
|
||||
constexpr unsigned short kConnectorPort = 27843;
|
||||
|
||||
std::filesystem::path lsof_path() {
|
||||
for (const char* candidate : {"/usr/sbin/lsof", "/usr/bin/lsof"}) {
|
||||
if (std::filesystem::exists(candidate)) return candidate;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::set<pid_t> connector_port_pids() {
|
||||
const std::filesystem::path executable = lsof_path();
|
||||
if (executable.empty()) return {};
|
||||
const ProcessResult result = run_process(executable.string(),
|
||||
{"-nP", "-tiTCP:" + std::to_string(kConnectorPort), "-sTCP:LISTEN"}, {}, 3);
|
||||
if (result.timed_out || (result.exit_code != 0 && result.exit_code != 1)) return {};
|
||||
std::set<pid_t> pids;
|
||||
std::istringstream lines(result.output);
|
||||
std::string line;
|
||||
while (std::getline(lines, line)) {
|
||||
try {
|
||||
const long value = std::stol(line);
|
||||
if (value > 0) pids.insert(static_cast<pid_t>(value));
|
||||
} catch (...) {
|
||||
// lsof 可能输出诊断信息;只接受纯数字 PID。
|
||||
}
|
||||
}
|
||||
return pids;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ProcessResult run_process(const std::string& executable,
|
||||
const std::vector<std::string>& arguments,
|
||||
@@ -70,6 +104,46 @@ ProcessResult run_process(const std::string& executable,
|
||||
return result;
|
||||
}
|
||||
|
||||
bool connector_port_in_use() {
|
||||
return !connector_port_pids().empty();
|
||||
}
|
||||
|
||||
bool stop_connector_port(std::string& error) {
|
||||
const std::set<pid_t> pids = connector_port_pids();
|
||||
if (pids.empty()) {
|
||||
error.clear();
|
||||
return true;
|
||||
}
|
||||
for (const pid_t pid : pids) {
|
||||
if (pid == getpid()) {
|
||||
error = "无法从外部停止当前连接器进程";
|
||||
return false;
|
||||
}
|
||||
if (kill(pid, SIGTERM) != 0) {
|
||||
error = "无法停止占用端口 27843 的进程,请检查系统权限";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(3);
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
if (!connector_port_in_use()) {
|
||||
error.clear();
|
||||
return true;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
for (const pid_t pid : pids) {
|
||||
if (kill(pid, 0) == 0) kill(pid, SIGKILL);
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
if (connector_port_in_use()) {
|
||||
error = "已请求停止进程,但端口 27843 仍未释放";
|
||||
return false;
|
||||
}
|
||||
error.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace connector
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user