UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UnixPlatformProcess.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================================
4 UnixPlatformProcess.h: Unix platform Process functions
5==============================================================================================*/
6
7#pragma once
8
9#include "Containers/Array.h"
11#include "CoreTypes.h"
12#include "PosixOS/PosixOSPlatformProcess.h" // IWYU pragma: export
13#include "HAL/PlatformMisc.h"
15#include "Unix/UnixSystemIncludes.h" // IWYU pragma: export
16
17class Error;
18
20struct FProcState
21{
23 inline FProcState()
24 : ProcessId(0)
27 , ReturnCode(-1)
29 {
30 }
31
34
37
40 {
41 return ProcessId;
42 }
43
49 bool IsRunning();
50
59
64 void Wait();
65
66protected: // the below is not a public API!
67
68 // FProcState should not be copyable since it breeds problems (e.g. one copy could have wait()ed, but another would not know it)
69
73 , bIsRunning(Other.bIsRunning) // assume it is
77 {
78 checkf(false, TEXT("FProcState should not be copied"));
79 }
80
83 {
84 checkf(false, TEXT("FProcState should not be copied"));
85 return *this;
86 }
87
88 friend struct FUnixPlatformProcess;
89
90 // -------------------------
91
94
96 bool bIsRunning;
97
100
103
106};
107
109struct FProcHandle
110{
113
120
121 constexpr FProcHandle()
122 : ProcInfo(nullptr), OpenedPid(-1)
123 {
124 }
125
130
132 : ProcInfo(nullptr), OpenedPid(InProcPid)
133 {
134 }
135
138 {
140 }
141
143 inline void Reset()
144 {
145 ProcInfo = nullptr;
146 OpenedPid = -1;
147 }
148
151 {
152 return ProcInfo != nullptr || OpenedPid != -1;
153 }
154
155 // the below is not part of FProcHandle API and is specific to Unix implementation
157 {
158 return ProcInfo;
159 }
160};
161
164{
165 FPipeHandle(int Fd, int PairFd)
166 : PipeDesc(Fd)
168 {
169 }
170
172
176 FString Read();
177
182
186 int GetHandle() const
187 {
188 return PipeDesc;
189 }
190
194 int GetPairHandle() const
195 {
196 return PairDesc;
197 }
198
199protected:
200
203};
204
209{
210 struct FProcEnumInfo;
211
216 {
217 public:
218 // Constructor
222
223 // Destructor
225
226 // Gets current process enumerator info.
228
234 CORE_API bool MoveNext();
235 private:
236 // Private implementation data.
237 struct FProcEnumData* Data;
238 };
239
244 {
246
247 // Gets process PID.
248 CORE_API uint32 GetPID() const;
249
250 // Gets parent process PID.
252
253 // Gets process name. I.e. exec name.
254 CORE_API FString GetName() const;
255
256 // Gets process full image path. I.e. full path of the exec file.
257 CORE_API FString GetFullPath() const;
258
259 private:
260 // Private constructor.
262
263 // Current process PID.
264 uint32 PID;
265 };
266
267 static CORE_API void* GetDllHandle( const TCHAR* Filename );
268 static CORE_API void FreeDllHandle( void* DllHandle );
269 static CORE_API void* GetDllExport( void* DllHandle, const TCHAR* ProcName );
270 static CORE_API const TCHAR* ComputerName();
271 static CORE_API const TCHAR* UserName(bool bOnlyAlphaNumeric = true);
272 static CORE_API const TCHAR* UserTempDir();
273 static CORE_API const TCHAR* UserDir();
274 static CORE_API const TCHAR* UserSettingsDir();
275 static CORE_API const TCHAR* ApplicationSettingsDir();
276 static CORE_API FString GetApplicationSettingsDir(const ApplicationSettingsContext& Settings);
278 static CORE_API FString GetCurrentWorkingDirectory();
279 static CORE_API FString GenerateApplicationPath(const FString& AppName, EBuildConfiguration BuildConfiguration);
280 static CORE_API FString GetApplicationName( uint32 ProcessId );
281 static CORE_API bool SetProcessLimits(EProcessResource::Type Resource, uint64 Limit);
282 static CORE_API const TCHAR* ExecutablePath();
283 static CORE_API const TCHAR* ExecutableName(bool bRemoveExtension = true);
284 static CORE_API const TCHAR* GetModulePrefix();
285 static CORE_API const TCHAR* GetModuleExtension();
286 static CORE_API void ClosePipe( void* ReadPipe, void* WritePipe );
287 static CORE_API bool CreatePipe(void*& ReadPipe, void*& WritePipe, bool bWritePipeLocal = false);
288 static CORE_API FString ReadPipe( void* ReadPipe );
290 static CORE_API bool WritePipe(void* WritePipe, const FString& Message, FString* OutWritten = nullptr);
291 static CORE_API bool WritePipe(void* WritePipe, const uint8* Data, const int32 DataLength, int32* OutDataLength = nullptr);
293 static CORE_API const FString GetModulesDirectory();
294 static CORE_API bool CanLaunchURL(const TCHAR* URL);
295 static CORE_API void LaunchURL(const TCHAR* URL, const TCHAR* Parms, FString* Error);
296 static CORE_API FProcHandle CreateProc(const TCHAR* URL, const TCHAR* Parms, bool bLaunchDetached, bool bLaunchHidden, bool bLaunchReallyHidden, uint32* OutProcessID, int32 PriorityModifier, const TCHAR* OptionalWorkingDirectory, void* PipeWriteChild, void* PipeReadChild = nullptr);
297 static CORE_API FProcHandle CreateProc(const TCHAR* URL, const TCHAR* Parms, bool bLaunchDetached, bool bLaunchHidden, bool bLaunchReallyHidden, uint32* OutProcessID, int32 PriorityModifier, const TCHAR* OptionalWorkingDirectory, void* PipeWriteChild, void* PipeReadChild, void* PipeStdErrChild);
300 static CORE_API bool IsProcRunning( FProcHandle & ProcessHandle );
301 static CORE_API void WaitForProc( FProcHandle & ProcessHandle );
302 static CORE_API void CloseProc( FProcHandle & ProcessHandle );
303 static CORE_API void TerminateProc( FProcHandle & ProcessHandle, bool KillTree = false );
307 static CORE_API bool GetProcReturnCode( FProcHandle & ProcHandle, int32* ReturnCode );
308 static CORE_API bool Daemonize();
309 static CORE_API bool IsApplicationRunning( uint32 ProcessId );
310 static CORE_API bool IsApplicationRunning( const TCHAR* ProcName );
311 static CORE_API bool ExecProcess(const TCHAR* URL, const TCHAR* Params, int32* OutReturnCode, FString* OutStdOut, FString* OutStdErr, const TCHAR* OptionalWorkingDirectory = NULL, bool bShouldEndWithParentProcess = false);
312 static CORE_API void ExploreFolder( const TCHAR* FilePath );
313 static CORE_API bool LaunchFileInDefaultExternalApplication( const TCHAR* FileName, const TCHAR* Parms = NULL, ELaunchVerb::Type Verb = ELaunchVerb::Open, bool bPromptToOpenOnFailure = true);
314 static CORE_API void OnChildEndFramePostFork();
316 static CORE_API void SetThreadNiceValue( uint32_t ThreadId, int32 NiceValue );
319
320
326 static CORE_API const TCHAR* UserHomeDir();
327
328private:
329 static FProcHandle CreateProcInternal(const TCHAR* URL, const TCHAR* Parms, bool bLaunchDetached, bool bLaunchHidden, bool bLaunchReallyHidden, uint32* OutProcessID, int32 PriorityModifier, const TCHAR* OptionalWorkingDirectory, void* PipeWriteChild, void* PipeReadChild, void* PipeStdErrChild, bool bInheritHandles);
330};
#define NULL
Definition oodle2base.h:134
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EThreadPriority
Definition GenericPlatformAffinity.h:26
EBuildConfiguration
Definition GenericPlatformMisc.h:59
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition RunnableThread.h:20
Definition UnixPlatformProcess.h:216
CORE_API ~FProcEnumerator()
Definition UnixPlatformProcess.cpp:2221
FProcEnumerator(const FProcEnumerator &)=delete
CORE_API FProcEnumerator()
Definition UnixPlatformProcess.cpp:2183
FProcEnumerator & operator=(const FProcEnumerator &)=delete
CORE_API bool MoveNext()
Definition UnixPlatformProcess.cpp:2231
CORE_API FProcEnumInfo GetCurrent() const
Definition UnixPlatformProcess.cpp:2226
Definition Array.h:670
Type
Definition GenericPlatformProcess.h:67
@ Open
Definition GenericPlatformProcess.h:69
Type
Definition GenericPlatformProcess.h:53
@ false
Definition radaudio_common.h:23
Definition GenericPlatformProcess.h:414
EWaitAndForkResult
Definition GenericPlatformProcess.h:666
Definition UnixPlatformProcess.h:164
FString Read()
Definition UnixPlatformProcess.cpp:512
bool ReadToArray(TArray< uint8 > &Output)
Definition UnixPlatformProcess.cpp:539
int PipeDesc
Definition UnixPlatformProcess.h:201
FPipeHandle(int Fd, int PairFd)
Definition UnixPlatformProcess.h:165
int GetPairHandle() const
Definition UnixPlatformProcess.h:194
int PairDesc
Definition UnixPlatformProcess.h:202
int GetHandle() const
Definition UnixPlatformProcess.h:186
CORE_API ~FPipeHandle()
Definition UnixPlatformProcess.cpp:507
Definition PosixOSPlatformProcess.h:14
Definition UnixPlatformProcess.cpp:2175
Definition AndroidPlatformProcess.h:13
UE_FORCEINLINE_HINT pid_t Get() const
Definition UnixPlatformProcess.h:137
pid_t OpenedPid
Definition MacPlatformProcess.h:79
FProcHandle(pid_t InProcPid)
Definition UnixPlatformProcess.h:131
FProcState * ProcInfo
Definition MacPlatformProcess.h:72
constexpr FProcHandle()
Definition UnixPlatformProcess.h:121
void Reset()
Definition UnixPlatformProcess.h:143
FProcHandle(FProcState *InHandle)
Definition UnixPlatformProcess.h:126
UE_FORCEINLINE_HINT FProcState * GetProcessInfo() const
Definition UnixPlatformProcess.h:156
UE_FORCEINLINE_HINT bool IsValid() const
Definition UnixPlatformProcess.h:150
Definition MacPlatformProcess.h:15
bool bHasBeenWaitedFor
Definition MacPlatformProcess.h:62
FProcState & operator=(const FProcState &Other)
Definition UnixPlatformProcess.h:82
FProcState()
Definition UnixPlatformProcess.h:23
bool IsRunning()
UE_FORCEINLINE_HINT pid_t GetProcessId() const
Definition UnixPlatformProcess.h:39
void Wait()
bool GetReturnCode(int32 *ReturnCodePtr)
bool bFireAndForget
Definition UnixPlatformProcess.h:105
bool bIsRunning
Definition MacPlatformProcess.h:59
int32 ReturnCode
Definition MacPlatformProcess.h:65
FProcState(const FProcState &Other)
Definition UnixPlatformProcess.h:71
pid_t ProcessId
Definition MacPlatformProcess.h:56
Definition UnixPlatformProcess.h:244
CORE_API FString GetName() const
Definition UnixPlatformProcess.cpp:2282
CORE_API FString GetFullPath() const
Definition UnixPlatformProcess.cpp:2277
CORE_API uint32 GetParentPID() const
Definition UnixPlatformProcess.cpp:2254
CORE_API uint32 GetPID() const
Definition UnixPlatformProcess.cpp:2249
Definition UnixPlatformProcess.h:209
static CORE_API const TCHAR * ApplicationSettingsDir()
Definition UnixPlatformProcess.cpp:347
static CORE_API FString GetCurrentWorkingDirectory()
Definition UnixPlatformProcess.cpp:1811
static CORE_API const TCHAR * GetModulePrefix()
Definition UnixPlatformProcess.cpp:161
static CORE_API const TCHAR * UserSettingsDir()
Definition UnixPlatformProcess.cpp:340
static CORE_API void ClosePipe(void *ReadPipe, void *WritePipe)
Definition UnixPlatformProcess.cpp:568
static CORE_API FString GenerateApplicationPath(const FString &AppName, EBuildConfiguration BuildConfiguration)
Definition UnixPlatformProcess.cpp:477
static CORE_API void OnChildEndFramePostFork()
Definition UnixPlatformProcess.cpp:2287
static CORE_API const TCHAR * UserName(bool bOnlyAlphaNumeric=true)
Definition UnixPlatformProcess.cpp:202
static CORE_API const TCHAR * GetModuleExtension()
Definition UnixPlatformProcess.cpp:166
static CORE_API void SetThreadPriority(EThreadPriority NewPriority)
Definition UnixPlatformProcess.cpp:2355
static CORE_API bool LaunchFileInDefaultExternalApplication(const TCHAR *FileName, const TCHAR *Parms=NULL, ELaunchVerb::Type Verb=ELaunchVerb::Open, bool bPromptToOpenOnFailure=true)
Definition UnixPlatformProcess.cpp:2129
static CORE_API void LaunchURL(const TCHAR *URL, const TCHAR *Parms, FString *Error)
Definition UnixPlatformProcess.cpp:680
static CORE_API bool CanLaunchURL(const TCHAR *URL)
Definition UnixPlatformProcess.cpp:675
static CORE_API uint32 GetCurrentProcessId()
Definition UnixPlatformProcess.cpp:1791
static CORE_API FString GetApplicationName(uint32 ProcessId)
Definition UnixPlatformProcess.cpp:490
static CORE_API bool IsApplicationRunning(uint32 ProcessId)
Definition UnixPlatformProcess.cpp:2003
static CORE_API bool CreatePipe(void *&ReadPipe, void *&WritePipe, bool bWritePipeLocal=false)
Definition UnixPlatformProcess.cpp:583
static CORE_API FString ReadPipe(void *ReadPipe)
Definition UnixPlatformProcess.cpp:600
static CORE_API void FreeDllHandle(void *DllHandle)
Definition UnixPlatformProcess.cpp:148
static CORE_API int32 TranslateThreadPriority(EThreadPriority Priority)
Definition UnixPlatformProcess.cpp:2295
static CORE_API const TCHAR * ComputerName()
Definition UnixPlatformProcess.cpp:182
static CORE_API void WaitForProc(FProcHandle &ProcessHandle)
Definition UnixPlatformProcess.cpp:1396
static CORE_API bool IsProcRunning(FProcHandle &ProcessHandle)
Definition UnixPlatformProcess.cpp:1373
static CORE_API void SetThreadNiceValue(uint32_t ThreadId, int32 NiceValue)
Definition UnixPlatformProcess.cpp:2338
static CORE_API FString GetApplicationSettingsDir(const ApplicationSettingsContext &Settings)
Definition UnixPlatformProcess.cpp:360
static CORE_API uint32 GetCurrentCoreNumber()
Definition UnixPlatformProcess.cpp:1796
static CORE_API bool GetPerFrameProcessorUsage(uint32 ProcessId, float &ProcessUsageFraction, float &IdleUsageFraction)
Definition UnixPlatformProcess.cpp:1819
static CORE_API const FString GetModulesDirectory()
Definition UnixPlatformProcess.cpp:419
static CORE_API const TCHAR * UserDir()
Definition UnixPlatformProcess.cpp:271
static CORE_API void TerminateProc(FProcHandle &ProcessHandle, bool KillTree=false)
Definition UnixPlatformProcess.cpp:1418
static CORE_API void SetCurrentWorkingDirectoryToBaseDir()
Definition UnixPlatformProcess.cpp:1801
static CORE_API void ExploreFolder(const TCHAR *FilePath)
Definition UnixPlatformProcess.cpp:2141
static CORE_API bool ExecProcess(const TCHAR *URL, const TCHAR *Params, int32 *OutReturnCode, FString *OutStdOut, FString *OutStdErr, const TCHAR *OptionalWorkingDirectory=NULL, bool bShouldEndWithParentProcess=false)
Definition UnixPlatformProcess.cpp:2044
static CORE_API FProcHandle CreateProc(const TCHAR *URL, const TCHAR *Parms, bool bLaunchDetached, bool bLaunchHidden, bool bLaunchReallyHidden, uint32 *OutProcessID, int32 PriorityModifier, const TCHAR *OptionalWorkingDirectory, void *PipeWriteChild, void *PipeReadChild=nullptr)
Definition UnixPlatformProcess.cpp:823
static CORE_API const TCHAR * UserTempDir()
Definition UnixPlatformProcess.cpp:247
static CORE_API bool GetProcReturnCode(FProcHandle &ProcHandle, int32 *ReturnCode)
Definition UnixPlatformProcess.cpp:1970
static CORE_API FProcHandle OpenProcess(uint32 ProcessID)
Definition UnixPlatformProcess.cpp:1209
static CORE_API bool ReadPipeToArray(void *ReadPipe, TArray< uint8 > &Output)
Definition UnixPlatformProcess.cpp:611
static CORE_API bool Daemonize()
Definition UnixPlatformProcess.cpp:1990
static CORE_API class FRunnableThread * CreateRunnableThread()
Definition UnixPlatformProcess.cpp:670
static CORE_API bool WritePipe(void *WritePipe, const FString &Message, FString *OutWritten=nullptr)
Definition UnixPlatformProcess.cpp:622
static CORE_API void * GetDllExport(void *DllHandle, const TCHAR *ProcName)
Definition UnixPlatformProcess.cpp:154
static CORE_API const TCHAR * ExecutablePath()
Definition UnixPlatformProcess.cpp:430
static CORE_API const TCHAR * ExecutableName(bool bRemoveExtension=true)
Definition UnixPlatformProcess.cpp:453
static CORE_API void CloseProc(FProcHandle &ProcessHandle)
Definition UnixPlatformProcess.cpp:1409
static CORE_API bool SetProcessLimits(EProcessResource::Type Resource, uint64 Limit)
Definition UnixPlatformProcess.cpp:377
static CORE_API EWaitAndForkResult WaitAndFork()
Definition UnixPlatformProcess.cpp:1454
static CORE_API const TCHAR * UserHomeDir()
Returns user home directory (i.e. $HOME).
Definition UnixPlatformProcess.cpp:308
static CORE_API void * GetDllHandle(const TCHAR *Filename)
Definition UnixPlatformProcess.cpp:86
Definition Tuple.h:652
Definition GenericPlatformProcess.h:197