| 1 |
{ |
| 2 |
Copyright 2021-2024 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Editor and build tool utilities to manage process recognition / waiting. } |
| 17 |
unit ToolProcess; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
type |
| 22 |
{ Type wide enough to contain process id on all platforms. |
| 23 |
Not defined with $ifdef, |
| 24 |
because we pass it through command-line and thus want to easily convert string<->integer, |
| 25 |
so it's not opaque anyway, we just need to explicitly decide on integer type |
| 26 |
that can fit everything. |
| 27 |
|
| 28 |
On Windows this is DWORD ( https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocessid ). |
| 29 |
|
| 30 |
On Unix this is pid_t ( https://man7.org/linux/man-pages/man3/pid_t.3.html ) |
| 31 |
which should be signed and no greater than CLong. In practice it is 32 or 64 bit signed |
| 32 |
integer. } |
| 33 |
TProcessId = Int64; |
| 34 |
|
| 35 |
{ Get system-wide process identifier, that another process could use with WaitForProcessExit. } |
| 36 |
function CurrentProcessId: TProcessId; |
| 37 |
|
| 38 |
{ Wait (block execution) until another process finishes. } |
| 39 |
procedure WaitForProcessExit(const ProcessId: TProcessId); |
| 40 |
|
| 41 |
{ Stop (kill) given process. |
| 42 |
|
| 43 |
Does not guarantee that the process will be killed, but makes reasonable effort. |
| 44 |
If it fails, it just reports a warning using WritelnWarning, |
| 45 |
and continues without any exception. |
| 46 |
The purpose for now is to stop running application from CGE editor, |
| 47 |
it can assume that application is not "incredibly stubborn" in refusing to quit. |
| 48 |
|
| 49 |
On Unix this right now sends SIGTERM (but *does not* follow with more brutal SIGKILL). |
| 50 |
|
| 51 |
On Windows it uses TerminateProcess. } |
| 52 |
procedure StopProcess(const ProcessId: TProcessId); |
| 53 |
|
| 54 |
implementation |
| 55 |
|
| 56 |
uses |
| 57 |
{$ifdef MSWINDOWS} Windows {$endif} |
| 58 |
{$ifdef UNIX} CTypes, BaseUnix {$endif} |
| 59 |
, SysUtils, |
| 60 |
CastleUtils, CastleLog; |
| 61 |
|
| 62 |
function CurrentProcessId: TProcessId; |
| 63 |
begin |
| 64 |
Result := |
| 65 |
{$ifdef MSWINDOWS} GetCurrentProcessId() |
| 66 |
{$else} |
| 67 |
{$ifdef UNIX} FpGetPid() |
| 68 |
{$else} 0 |
| 69 |
{$endif} |
| 70 |
{$endif}; |
| 71 |
end; |
| 72 |
|
| 73 |
procedure WaitForProcessExit(const ProcessId: TProcessId); |
| 74 |
{$ifdef MSWINDOWS} |
| 75 |
const |
| 76 |
WaitSeconds = 10; |
| 77 |
var |
| 78 |
ProcessHandle: THandle; |
| 79 |
WaitResult, WinError: DWord; |
| 80 |
begin |
| 81 |
ProcessHandle := OpenProcess(Windows.SYNCHRONIZE, false, ProcessId); |
| 82 |
if ProcessHandle <> 0 then // otherwise assume it has already terminated |
| 83 |
begin |
| 84 |
WaitResult := WaitForSingleObject(ProcessHandle, WaitSeconds * 1000); |
| 85 |
case WaitResult of |
| 86 |
WAIT_OBJECT_0: Exit; // OK, process finished |
| 87 |
WAIT_TIMEOUT: |
| 88 |
raise Exception.CreateFmt('Waited for process for %d seconds, without success. Aborting.', [ |
| 89 |
WaitSeconds |
| 90 |
]); |
| 91 |
WAIT_FAILED: |
| 92 |
begin |
| 93 |
WinError := GetLastError; |
| 94 |
raise Exception.CreateFmt('Waiting for process failed with error %d: %s.', [ |
| 95 |
WinError, |
| 96 |
SysErrorMessage(WinError) |
| 97 |
]); |
| 98 |
end; |
| 99 |
else |
| 100 |
raise Exception.CreateFmt('Waiting for process failed with unexpected error from WaitForSingleObject: %d.', [ |
| 101 |
WaitResult |
| 102 |
]); |
| 103 |
end; |
| 104 |
end; |
| 105 |
|
| 106 |
{$else} |
| 107 |
{$ifdef UNIX} |
| 108 |
|
| 109 |
(* // This is only suitable for child processes, which is not our case. |
| 110 |
var |
| 111 |
ProcessStatus, Res: CInt; |
| 112 |
Err: LongInt; |
| 113 |
begin |
| 114 |
Res := FpWaitPid(ProcessId, @ProcessStatus, WNOHANG); |
| 115 |
if Res <> ProcessId then |
| 116 |
begin |
| 117 |
Err := FpGetErrno; |
| 118 |
raise Exception.CreateFmt('Waiting for process failed with error %d: %s.', [ |
| 119 |
Err, |
| 120 |
SysErrorMessage(Err) |
| 121 |
]); |
| 122 |
end; |
| 123 |
*) |
| 124 |
|
| 125 |
// Send a signal to test whether process works. |
| 126 |
// SIGCHLD is ignored by default ( https://en.wikipedia.org/wiki/Signal_(IPC) ). |
| 127 |
var |
| 128 |
Res: CInt; |
| 129 |
begin |
| 130 |
repeat |
| 131 |
Res := FpKill(ProcessId, SIGCHLD); |
| 132 |
// once process stops existing, kill will return -1, and FpGetErrno = ESRCH = ESysESRCH |
| 133 |
if Res = -1 then |
| 134 |
Exit; |
| 135 |
Sleep(100); |
| 136 |
until false; |
| 137 |
|
| 138 |
{$else} |
| 139 |
begin |
| 140 |
WritelnWarning('Cannot wait for process on this platform, assuming it is OK to not wait.'); |
| 141 |
{$endif} |
| 142 |
{$endif} |
| 143 |
end; |
| 144 |
|
| 145 |
procedure StopProcess(const ProcessId: TProcessId); |
| 146 |
{$ifdef MSWINDOWS} |
| 147 |
var |
| 148 |
LastError: Integer; |
| 149 |
ProcessHandle: THandle; |
| 150 |
begin |
| 151 |
{ We need to change ProcessId (DWORD in WinAPI, unique process id in system) |
| 152 |
to a handle we can use with TerminateProcess. } |
| 153 |
ProcessHandle := OpenProcess(PROCESS_TERMINATE, WinBool(false), ProcessId); |
| 154 |
if ProcessHandle = 0 then |
| 155 |
begin |
| 156 |
LastError := GetLastOSError; |
| 157 |
WritelnWarning('Getting process handle (id: %d) to terminate it failed: WinAPI error %d: %s', [ |
| 158 |
ProcessId, |
| 159 |
LastError, |
| 160 |
SysErrorMessage(LastError) |
| 161 |
]); |
| 162 |
Exit; |
| 163 |
end; |
| 164 |
|
| 165 |
if not TerminateProcess(ProcessHandle, 0) then |
| 166 |
begin |
| 167 |
LastError := GetLastOSError; |
| 168 |
WritelnWarning('Stopping process (handle: %d) by TerminateProcess failed: WinAPI error %d: %s', [ |
| 169 |
ProcessHandle, |
| 170 |
LastError, |
| 171 |
SysErrorMessage(LastError) |
| 172 |
]); |
| 173 |
end; |
| 174 |
{$endif} |
| 175 |
|
| 176 |
{$ifdef UNIX} |
| 177 |
var |
| 178 |
LastError: Integer; |
| 179 |
begin |
| 180 |
if FpKill(ProcessId, SIGTERM) = 0 then |
| 181 |
begin |
| 182 |
{ Sending SIGKILL right after SIGTERM is a bit brutal, |
| 183 |
i.e. we didn't give the ProcessId any time to react to SIGTERM and close nicely. |
| 184 |
But we don't want to introduce some delay here, |
| 185 |
we want TerminateChildrenHarder to be instant, to be responsive to user. |
| 186 |
So for now, we just don't follow it with SIGKILL. |
| 187 |
} |
| 188 |
|
| 189 |
(* |
| 190 |
if Process.Running then |
| 191 |
begin |
| 192 |
OutputList.AddLine(Format('Killing child process (id: %d) by SIGKILL', [ProcessId]), okInfo); |
| 193 |
if FpKill(ProcessId, SIGKILL) <> 0 then |
| 194 |
OutputList.AddLine(Format('Cannot send SIGKILL to child process (id: %d)', [ProcessId]), okWarning); |
| 195 |
end; |
| 196 |
*) |
| 197 |
end else |
| 198 |
begin |
| 199 |
LastError := GetLastOSError; |
| 200 |
WritelnWarning('Cannot send SIGTERM to child process (id: %d). OS error %d: %s', [ |
| 201 |
ProcessId, |
| 202 |
LastError, |
| 203 |
SysErrorMessage(LastError) |
| 204 |
]); |
| 205 |
end; |
| 206 |
{$endif} |
| 207 |
|
| 208 |
end; |
| 209 |
|
| 210 |
end. |