diff options
| author | Dan Engelbrecht <[email protected]> | 2024-04-18 12:44:26 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2024-04-18 12:44:26 +0200 |
| commit | 6d634ab59c05adf1ba028d95b16031a7f8e8db2a (patch) | |
| tree | 87e8215fed65506f98f6838b2af08c4ffd5819f0 /src/zencore/process.cpp | |
| parent | zen startup hardening (#49) (diff) | |
| download | zen-6d634ab59c05adf1ba028d95b16031a7f8e8db2a.tar.xz zen-6d634ab59c05adf1ba028d95b16031a7f8e8db2a.zip | |
improved lock file handling (#50)
- Feature: `zen down`
- --`data-dir` to specify a data directory to deduce which zen instance to bring down
- Feature: `zen attach`
- --`data-dir` to specify a data directory to deduce which zen instance to attach to222
- Feature: `zen status`
- --`port` filter running zen instances based on port
- --`data-dir` filter running zen instances based on information in the data directory
- Improvement: Trying to load a compact binary object from an empty file no longer causes access violation
Diffstat (limited to 'src/zencore/process.cpp')
| -rw-r--r-- | src/zencore/process.cpp | 105 |
1 files changed, 73 insertions, 32 deletions
diff --git a/src/zencore/process.cpp b/src/zencore/process.cpp index df2a87352..8fddc9562 100644 --- a/src/zencore/process.cpp +++ b/src/zencore/process.cpp @@ -813,6 +813,54 @@ GetProcessId(CreateProcResult ProcId) #endif } +std::filesystem::path +GetProcessExecutablePath(int Pid, std::error_code& OutEc) +{ +#if ZEN_PLATFORM_WINDOWS + HANDLE ModuleSnapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, (DWORD)Pid); + if (ModuleSnapshotHandle != INVALID_HANDLE_VALUE) + { + auto __ = MakeGuard([&]() { CloseHandle(ModuleSnapshotHandle); }); + MODULEENTRY32 ModuleEntry; + ModuleEntry.dwSize = sizeof(MODULEENTRY32); + if (Module32First(ModuleSnapshotHandle, (LPMODULEENTRY32)&ModuleEntry)) + { + std::filesystem::path ProcessExecutablePath(ModuleEntry.szExePath); + return ProcessExecutablePath; + } + OutEc = MakeErrorCodeFromLastError(); + return {}; + } + OutEc = MakeErrorCodeFromLastError(); + return {}; +#endif // ZEN_PLATFORM_WINDOWS +#if ZEN_PLATFORM_MAC + char Buffer[PROC_PIDPATHINFO_MAXSIZE]; + int Res = proc_pidpath(Pid, Buffer, sizeof(Buffer)); + if (Res > 0) + { + std::filesystem::path ProcessExecutablePath(Buffer); + return ProcessExecutablePath; + } + OutEc = MakeErrorCodeFromLastError(); + return {}; +#endif // ZEN_PLATFORM_MAC +#if ZEN_PLATFORM_LINUX + std::filesystem::path EntryPath = std::filesystem::path("/proc") / fmt::format("{}", Pid); + std::filesystem::path ExeLinkPath = EntryPath / "exe"; + char Link[4096]; + ssize_t BytesRead = readlink(ExeLinkPath.c_str(), Link, sizeof(Link) - 1); + if (BytesRead > 0) + { + Link[BytesRead] = '\0'; + std::filesystem::path ProcessExecutablePath(Link); + return ProcessExecutablePath; + } + OutEc = MakeErrorCodeFromLastError(); + return {}; +#endif // ZEN_PLATFORM_LINUX +} + std::error_code FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHandle) { @@ -832,23 +880,22 @@ FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHand { if (ExecutableImage.filename() == Entry.szExeFile) { - HANDLE ModuleSnapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, Entry.th32ProcessID); - if (ModuleSnapshotHandle != INVALID_HANDLE_VALUE) + std::error_code Ec; + std::filesystem::path EntryPath = GetProcessExecutablePath(Entry.th32ProcessID, Ec); + if (!Ec) { - auto __ = MakeGuard([&]() { CloseHandle(ModuleSnapshotHandle); }); - MODULEENTRY32 ModuleEntry; - ModuleEntry.dwSize = sizeof(MODULEENTRY32); - if (Module32First(ModuleSnapshotHandle, (LPMODULEENTRY32)&ModuleEntry)) + if (EntryPath == ExecutableImage) { - std::filesystem::path EntryPath(ModuleEntry.szExePath); - if (EntryPath == ExecutableImage) + HANDLE Handle = + OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, Entry.th32ProcessID); + if (Handle == NULL) + { + return MakeErrorCodeFromLastError(); + } + DWORD ExitCode = 0; + GetExitCodeProcess(Handle, &ExitCode); + if (ExitCode == STILL_ACTIVE) { - HANDLE Handle = - OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, Entry.th32ProcessID); - if (Handle == NULL) - { - return MakeErrorCodeFromLastError(); - } OutHandle.Initialize((void*)Handle); return {}; } @@ -876,16 +923,15 @@ FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHand char Buffer[PROC_PIDPATHINFO_MAXSIZE]; for (uint32_t ProcIndex = 0; ProcIndex < ProcCount; ProcIndex++) { - if (Processes[ProcIndex].kp_proc.p_stat != SZOMB) + pid_t Pid = Processes[ProcIndex].kp_proc.p_pid; + std::error_code Ec; + std::filesystem::path EntryPath = GetProcessExecutablePath(Pid, Ec); + if (!Ec) { - pid_t Pid = Processes[ProcIndex].kp_proc.p_pid; - int Res = proc_pidpath(Pid, Buffer, sizeof(Buffer)); - if (Res > 0) + if (EntryPath == ExecutableImage) { - std::filesystem::path EntryPath(Buffer); - if (EntryPath == ExecutableImage) + if (Processes[ProcIndex].kp_proc.p_stat != SZOMB) { - std::error_code Ec; OutHandle.Initialize(Pid, Ec); return Ec; } @@ -912,20 +958,15 @@ FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHand for (uint32_t Pid : RunningPids) { - char Status = GetPidStatus(Pid); - if (Status && (Status != 'Z')) + std::error_code Ec; + std::filesystem::path EntryPath = GetProcessExecutablePath((int)Pid, Ec); + if (!Ec) { - std::filesystem::path EntryPath = std::filesystem::path("/proc") / fmt::format("{}", Pid); - std::filesystem::path ExeLinkPath = EntryPath / "exe"; - char Link[4096]; - ssize_t BytesRead = readlink(ExeLinkPath.c_str(), Link, sizeof(Link) - 1); - if (BytesRead > 0) + if (EntryPath == ExecutableImage) { - Link[BytesRead] = '\0'; - std::filesystem::path ExePath(Link); - if (ExePath == ExecutableImage) + char Status = GetPidStatus(Pid); + if (Status && (Status != 'Z')) { - std::error_code Ec; OutHandle.Initialize(Pid, Ec); return Ec; } |