aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/include
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2023-12-06 15:13:38 +0100
committerStefan Boberg <[email protected]>2023-12-06 15:13:38 +0100
commit332bbfefcf174a905ed492f42ba26fb041901746 (patch)
treed18e2ed45422e8e137916f8ac1af30f1baeaa44d /src/zenutil/include
parentadded zentrack module (diff)
downloadzen-332bbfefcf174a905ed492f42ba26fb041901746.tar.xz
zen-332bbfefcf174a905ed492f42ba26fb041901746.zip
move core/process into util/process
this prepares for the implementation of child process resource tracking
Diffstat (limited to 'src/zenutil/include')
-rw-r--r--src/zenutil/include/zenutil/process.h99
-rw-r--r--src/zenutil/include/zenutil/zenserverprocess.h2
2 files changed, 100 insertions, 1 deletions
diff --git a/src/zenutil/include/zenutil/process.h b/src/zenutil/include/zenutil/process.h
new file mode 100644
index 000000000..429ab113a
--- /dev/null
+++ b/src/zenutil/include/zenutil/process.h
@@ -0,0 +1,99 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#pragma once
+
+#include <zencore/thread.h>
+#include <zencore/zencore.h>
+
+#include <filesystem>
+
+namespace zen {
+
+/** Basic process abstraction
+ */
+class ProcessHandle
+{
+public:
+ ZENCORE_API ProcessHandle();
+
+ ProcessHandle(const ProcessHandle&) = delete;
+ ProcessHandle& operator=(const ProcessHandle&) = delete;
+
+ ZENCORE_API ~ProcessHandle();
+
+ ZENCORE_API void Initialize(int Pid);
+ ZENCORE_API void Initialize(void* ProcessHandle); /// Initialize with an existing handle - takes ownership of the handle
+ ZENCORE_API [[nodiscard]] bool IsRunning() const;
+ ZENCORE_API [[nodiscard]] bool IsValid() const;
+ ZENCORE_API bool Wait(int TimeoutMs = -1);
+ ZENCORE_API int WaitExitCode();
+ ZENCORE_API void Terminate(int ExitCode);
+ ZENCORE_API void Reset();
+ [[nodiscard]] inline int Pid() const { return m_Pid; }
+
+private:
+ void* m_ProcessHandle = nullptr;
+ int m_Pid = 0;
+#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC
+ int m_ExitCode = -1;
+#endif
+};
+
+/** Basic process creation
+ */
+struct CreateProcOptions
+{
+ enum
+ {
+ Flag_NewConsole = 1 << 0,
+ Flag_Elevated = 1 << 1,
+ Flag_Unelevated = 1 << 2,
+ };
+
+ const std::filesystem::path* WorkingDirectory = nullptr;
+ uint32_t Flags = 0;
+ std::filesystem::path StdoutFile;
+ bool WithTracking = false;
+};
+
+#if ZEN_PLATFORM_WINDOWS
+using CreateProcResult = void*; // handle to the process
+#else
+using CreateProcResult = int32_t; // pid
+#endif
+
+ZENCORE_API CreateProcResult CreateProc(const std::filesystem::path& Executable,
+ std::string_view CommandLine, // should also include arg[0] (executable name)
+ const CreateProcOptions& Options = {});
+
+/** Process monitor - monitors a list of running processes via polling
+
+ Intended to be used to monitor a set of "sponsor" processes, where
+ we need to determine when none of them remain alive
+
+ */
+
+class ProcessMonitor
+{
+public:
+ ProcessMonitor();
+ ~ProcessMonitor();
+
+ ZENCORE_API bool IsRunning();
+ ZENCORE_API void AddPid(int Pid);
+ ZENCORE_API bool IsActive() const;
+
+private:
+ using HandleType = void*;
+
+ mutable RwLock m_Lock;
+ std::vector<HandleType> m_ProcessHandles;
+};
+
+ZENCORE_API bool IsProcessRunning(int pid);
+ZENCORE_API int GetCurrentProcessId();
+int GetProcessId(CreateProcResult ProcId);
+
+void process_forcelink(); // internal
+
+} // namespace zen
diff --git a/src/zenutil/include/zenutil/zenserverprocess.h b/src/zenutil/include/zenutil/zenserverprocess.h
index 15138341c..6af48c33d 100644
--- a/src/zenutil/include/zenutil/zenserverprocess.h
+++ b/src/zenutil/include/zenutil/zenserverprocess.h
@@ -4,9 +4,9 @@
#include <zencore/enumflags.h>
#include <zencore/logging.h>
-#include <zencore/process.h>
#include <zencore/thread.h>
#include <zencore/uid.h>
+#include <zenutil/process.h>
#include <atomic>
#include <filesystem>