aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/process.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zencore/process.cpp')
-rw-r--r--src/zencore/process.cpp208
1 files changed, 48 insertions, 160 deletions
diff --git a/src/zencore/process.cpp b/src/zencore/process.cpp
index 147b00966..f62731d94 100644
--- a/src/zencore/process.cpp
+++ b/src/zencore/process.cpp
@@ -60,7 +60,7 @@ GetPidStatus(int Pid, std::error_code& OutEc)
{
std::filesystem::path EntryPath = std::filesystem::path("/proc") / fmt::format("{}", Pid);
std::filesystem::path StatPath = EntryPath / "stat";
- if (std::filesystem::is_regular_file(StatPath))
+ if (IsFile(StatPath))
{
FILE* StatFile = fopen(StatPath.c_str(), "r");
if (StatFile)
@@ -946,7 +946,7 @@ GetProcessExecutablePath(int Pid, std::error_code& OutEc)
}
std::error_code
-FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHandle)
+FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHandle, bool IncludeSelf)
{
#if ZEN_PLATFORM_WINDOWS
HANDLE ProcessSnapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
@@ -956,13 +956,15 @@ FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHand
}
auto _ = MakeGuard([&]() { CloseHandle(ProcessSnapshotHandle); });
+ const DWORD ThisProcessId = ::GetCurrentProcessId();
+
PROCESSENTRY32 Entry;
Entry.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(ProcessSnapshotHandle, (LPPROCESSENTRY32)&Entry))
{
do
{
- if (ExecutableImage.filename() == Entry.szExeFile)
+ if ((IncludeSelf || (Entry.th32ProcessID != ThisProcessId)) && (ExecutableImage.filename() == Entry.szExeFile))
{
std::error_code Ec;
std::filesystem::path EntryPath = GetProcessExecutablePath(Entry.th32ProcessID, Ec);
@@ -987,6 +989,7 @@ FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHand
}
}
} while (::Process32Next(ProcessSnapshotHandle, (LPPROCESSENTRY32)&Entry));
+ return {};
}
return MakeErrorCodeFromLastError();
#endif // ZEN_PLATFORM_WINDOWS
@@ -997,6 +1000,8 @@ FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHand
struct kinfo_proc* Processes = nullptr;
uint32_t ProcCount = 0;
+ const pid_t ThisProcessId = getpid();
+
if (sysctl(Mib, 4, NULL, &BufferSize, NULL, 0) != -1 && BufferSize > 0)
{
struct kinfo_proc* Processes = (struct kinfo_proc*)malloc(BufferSize);
@@ -1007,36 +1012,46 @@ FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHand
char Buffer[PROC_PIDPATHINFO_MAXSIZE];
for (uint32_t ProcIndex = 0; ProcIndex < ProcCount; ProcIndex++)
{
- 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;
+ if (IncludeSelf || (Pid != ThisProcessId))
{
- if (EntryPath == ExecutableImage)
+ std::error_code Ec;
+ std::filesystem::path EntryPath = GetProcessExecutablePath(Pid, Ec);
+ if (!Ec)
{
- if (Processes[ProcIndex].kp_proc.p_stat != SZOMB)
+ if (EntryPath == ExecutableImage)
{
- OutHandle.Initialize(Pid, Ec);
- return Ec;
+ if (Processes[ProcIndex].kp_proc.p_stat != SZOMB)
+ {
+ OutHandle.Initialize(Pid, Ec);
+ return Ec;
+ }
}
}
+ Ec.clear();
}
}
+ return {};
}
}
return MakeErrorCodeFromLastError();
#endif // ZEN_PLATFORM_MAC
#if ZEN_PLATFORM_LINUX
+ const pid_t ThisProcessId = getpid();
+
std::vector<uint32_t> RunningPids;
DirectoryContent ProcList;
GetDirectoryContent("/proc", DirectoryContentFlags::IncludeDirs, ProcList);
for (const std::filesystem::path& EntryPath : ProcList.Directories)
{
std::string EntryName = EntryPath.stem();
- std::optional<uint32_t> Pid = ParseInt<uint32_t>(EntryName);
- if (Pid.has_value())
+ std::optional<uint32_t> PidMaybe = ParseInt<uint32_t>(EntryName);
+ if (PidMaybe.has_value())
{
- RunningPids.push_back(Pid.value());
+ if (pid_t Pid = PidMaybe.value(); IncludeSelf || (Pid != ThisProcessId))
+ {
+ RunningPids.push_back(Pid);
+ }
}
}
@@ -1059,123 +1074,12 @@ FindProcess(const std::filesystem::path& ExecutableImage, ProcessHandle& OutHand
}
}
}
+ Ec.clear();
}
return {};
#endif // ZEN_PLATFORM_LINUX
}
-std::vector<std::string>
-ParseCommandLine(std::string_view CommandLine)
-{
- auto IsWhitespaceOrEnd = [](std::string_view CommandLine, std::string::size_type Pos) {
- if (Pos == CommandLine.length())
- {
- return true;
- }
- if (CommandLine[Pos] == ' ')
- {
- return true;
- }
- return false;
- };
-
- bool IsParsingArg = false;
- bool IsInQuote = false;
-
- std::string::size_type Pos = 0;
- std::string::size_type ArgStart = 0;
- std::vector<std::string> Args;
- while (Pos < CommandLine.length())
- {
- if (IsInQuote)
- {
- if (CommandLine[Pos] == '"' && IsWhitespaceOrEnd(CommandLine, Pos + 1))
- {
- Args.push_back(std::string(CommandLine.substr(ArgStart, Pos - ArgStart + 1)));
- Pos++;
- IsInQuote = false;
- IsParsingArg = false;
- }
- else
- {
- Pos++;
- }
- }
- else if (IsParsingArg)
- {
- ZEN_ASSERT(Pos > ArgStart);
- if (CommandLine[Pos] == ' ')
- {
- Args.push_back(std::string(CommandLine.substr(ArgStart, Pos - ArgStart)));
- Pos++;
- IsParsingArg = false;
- }
- else if (CommandLine[Pos] == '"')
- {
- IsInQuote = true;
- Pos++;
- }
- else
- {
- Pos++;
- }
- }
- else if (CommandLine[Pos] == '"')
- {
- IsInQuote = true;
- IsParsingArg = true;
- ArgStart = Pos;
- Pos++;
- }
- else if (CommandLine[Pos] != ' ')
- {
- IsParsingArg = true;
- ArgStart = Pos;
- Pos++;
- }
- else
- {
- Pos++;
- }
- }
- if (IsParsingArg)
- {
- ZEN_ASSERT(Pos > ArgStart);
- Args.push_back(std::string(CommandLine.substr(ArgStart)));
- }
-
- return Args;
-}
-
-std::vector<char*>
-StripCommandlineQuotes(std::vector<std::string>& InOutArgs)
-{
- std::vector<char*> RawArgs;
- RawArgs.reserve(InOutArgs.size());
- for (std::string& Arg : InOutArgs)
- {
- std::string::size_type EscapedQuotePos = Arg.find("\\\"", 1);
- while (EscapedQuotePos != std::string::npos && Arg.rfind('\"', EscapedQuotePos - 1) != std::string::npos)
- {
- Arg.erase(EscapedQuotePos, 1);
- EscapedQuotePos = Arg.find("\\\"", EscapedQuotePos);
- }
-
- if (Arg.starts_with("\""))
- {
- if (Arg.find('"', 1) == Arg.length() - 1)
- {
- if (Arg.find(' ', 1) == std::string::npos)
- {
- Arg = Arg.substr(1, Arg.length() - 2);
- }
- }
- }
- RawArgs.push_back(const_cast<char*>(Arg.c_str()));
- }
- return RawArgs;
-}
-
#if ZEN_WITH_TESTS
void
@@ -1194,10 +1098,24 @@ TEST_CASE("Process")
TEST_CASE("FindProcess")
{
- ProcessHandle Process;
- std::error_code Ec = FindProcess(GetRunningExecutablePath(), Process);
- CHECK(!Ec);
- CHECK(Process.IsValid());
+ {
+ ProcessHandle Process;
+ std::error_code Ec = FindProcess(GetRunningExecutablePath(), Process, /*IncludeSelf*/ true);
+ CHECK(!Ec);
+ CHECK(Process.IsValid());
+ }
+ {
+ ProcessHandle Process;
+ std::error_code Ec = FindProcess(GetRunningExecutablePath(), Process, /*IncludeSelf*/ false);
+ CHECK(!Ec);
+ CHECK(!Process.IsValid());
+ }
+ {
+ ProcessHandle Process;
+ std::error_code Ec = FindProcess("this/does\\not/exist\\123914921929412312312312asdad\\12134.no", Process, /*IncludeSelf*/ false);
+ CHECK(!Ec);
+ CHECK(!Process.IsValid());
+ }
}
TEST_CASE("BuildArgV")
@@ -1252,36 +1170,6 @@ TEST_CASE("BuildArgV")
}
}
-TEST_CASE("CommandLine")
-{
- std::vector<std::string> v1 = ParseCommandLine("c:\\my\\exe.exe \"quoted arg\" \"one\",two,\"three\\\"");
- CHECK_EQ(v1[0], "c:\\my\\exe.exe");
- CHECK_EQ(v1[1], "\"quoted arg\"");
- CHECK_EQ(v1[2], "\"one\",two,\"three\\\"");
-
- std::vector<std::string> v2 = ParseCommandLine(
- "--tracehost 127.0.0.1 builds download --url=https://jupiter.devtools.epicgames.com --namespace=ue.oplog "
- "--bucket=citysample.packaged-build.fortnite-main.windows \"c:\\just\\a\\path\" "
- "--access-token-path=\"C:\\Users\\dan.engelbrecht\\jupiter-token.json\" \"D:\\Dev\\Spaced Folder\\Target\\\" "
- "--alt-path=\"D:\\Dev\\Spaced Folder2\\Target\\\" 07dn23ifiwesnvoasjncasab --build-part-name win64,linux,ps5");
-
- std::vector<char*> v2Stripped = StripCommandlineQuotes(v2);
- CHECK_EQ(v2Stripped[0], std::string("--tracehost"));
- CHECK_EQ(v2Stripped[1], std::string("127.0.0.1"));
- CHECK_EQ(v2Stripped[2], std::string("builds"));
- CHECK_EQ(v2Stripped[3], std::string("download"));
- CHECK_EQ(v2Stripped[4], std::string("--url=https://jupiter.devtools.epicgames.com"));
- CHECK_EQ(v2Stripped[5], std::string("--namespace=ue.oplog"));
- CHECK_EQ(v2Stripped[6], std::string("--bucket=citysample.packaged-build.fortnite-main.windows"));
- CHECK_EQ(v2Stripped[7], std::string("c:\\just\\a\\path"));
- CHECK_EQ(v2Stripped[8], std::string("--access-token-path=\"C:\\Users\\dan.engelbrecht\\jupiter-token.json\""));
- CHECK_EQ(v2Stripped[9], std::string("\"D:\\Dev\\Spaced Folder\\Target\""));
- CHECK_EQ(v2Stripped[10], std::string("--alt-path=\"D:\\Dev\\Spaced Folder2\\Target\""));
- CHECK_EQ(v2Stripped[11], std::string("07dn23ifiwesnvoasjncasab"));
- CHECK_EQ(v2Stripped[12], std::string("--build-part-name"));
- CHECK_EQ(v2Stripped[13], std::string("win64,linux,ps5"));
-}
-
TEST_SUITE_END(/* core.process */);
#endif