Skip to content

Commit db414b6

Browse files
authored
Merge pull request #94978 from bruvzg/macos_pid_map
[macOS] Fix `is_process_running` and `kill` for bundled apps.
2 parents 1d57b81 + 1dfcbcc commit db414b6

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

doc/classes/OS.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@
422422
<description>
423423
Returns the exit code of a spawned process once it has finished running (see [method is_process_running]).
424424
Returns [code]-1[/code] if the [param pid] is not a PID of a spawned child process, the process is still running, or the method is not implemented for the current platform.
425+
[b]Note:[/b] Returns [code]-1[/code] if the [param pid] is a macOS bundled app process.
425426
[b]Note:[/b] This method is implemented on Android, Linux, macOS and Windows.
426427
</description>
427428
</method>

platform/macos/os_macos.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class OS_MacOS : public OS_Unix {
109109
virtual String get_executable_path() const override;
110110
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
111111
virtual Error create_instance(const List<String> &p_arguments, ProcessID *r_child_id = nullptr) override;
112+
virtual Error kill(const ProcessID &p_pid) override;
113+
virtual bool is_process_running(const ProcessID &p_pid) const override;
112114

113115
virtual String get_unique_id() const override;
114116
virtual String get_processor_name() const override;

platform/macos/os_macos.mm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,24 @@
666666
}
667667
}
668668

669+
bool OS_MacOS::is_process_running(const ProcessID &p_pid) const {
670+
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:(pid_t)p_pid];
671+
if (!app) {
672+
return OS_Unix::is_process_running(p_pid);
673+
}
674+
675+
return ![app isTerminated];
676+
}
677+
678+
Error OS_MacOS::kill(const ProcessID &p_pid) {
679+
NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:(pid_t)p_pid];
680+
if (!app) {
681+
return OS_Unix::kill(p_pid);
682+
}
683+
684+
return [app forceTerminate] ? OK : ERR_INVALID_PARAMETER;
685+
}
686+
669687
String OS_MacOS::get_unique_id() const {
670688
static String serial_number;
671689

0 commit comments

Comments
 (0)