Skip to content

Commit

Permalink
Add OS::is_process_running function.
Browse files Browse the repository at this point in the history
Adds the is_process_running function to the native OS class and exposes it to script.

This is implemented on Windows and Unix platforms. A stub is provided for other platforms that do not support this function.

Documentation is updated to reflect new API function.
  • Loading branch information
mdavisprog committed May 4, 2022
1 parent 1b29927 commit f3c1232
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ Error OS::kill(int p_pid) {
return ::OS::get_singleton()->kill(p_pid);
}

bool OS::is_process_running(int p_pid) const {
return ::OS::get_singleton()->is_process_running(p_pid);
}

int OS::get_process_id() const {
return ::OS::get_singleton()->get_process_id();
}
Expand Down Expand Up @@ -571,6 +575,7 @@ void OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_instance", "arguments"), &OS::create_instance);
ClassDB::bind_method(D_METHOD("kill", "pid"), &OS::kill);
ClassDB::bind_method(D_METHOD("shell_open", "uri"), &OS::shell_open);
ClassDB::bind_method(D_METHOD("is_process_running", "pid"), &OS::is_process_running);
ClassDB::bind_method(D_METHOD("get_process_id"), &OS::get_process_id);

ClassDB::bind_method(D_METHOD("get_environment", "variable"), &OS::get_environment);
Expand Down
1 change: 1 addition & 0 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class OS : public Object {
Error kill(int p_pid);
Error shell_open(String p_uri);

bool is_process_running(int p_pid) const;
int get_process_id() const;

bool has_environment(const String &p_var) const;
Expand Down
1 change: 1 addition & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class OS {
virtual Error create_instance(const List<String> &p_arguments, ProcessID *r_child_id = nullptr) { return create_process(get_executable_path(), p_arguments, r_child_id); };
virtual Error kill(const ProcessID &p_pid) = 0;
virtual int get_process_id() const;
virtual bool is_process_running(const ProcessID &p_pid) const = 0;
virtual void vibrate_handheld(int p_duration_ms = 500);

virtual Error shell_open(String p_uri);
Expand Down
9 changes: 9 additions & 0 deletions doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,15 @@
Returns [code]true[/code] if the input keycode corresponds to a Unicode character.
</description>
</method>
<method name="is_process_running" qualifiers="const">
<return type="bool" />
<argument index="0" name="pid" type="int" />
<description>
Returns [code]true[/code] if the child process ID ([code]pid[/code]) is still running or [code]false[/code] if it has terminated.
Must be a valid ID generated from [method create_process].
[b]Note:[/b] This method is implemented on Android, iOS, Linux, macOS and Windows.
</description>
</method>
<method name="is_stdout_verbose" qualifiers="const">
<return type="bool" />
<description>
Expand Down
9 changes: 9 additions & 0 deletions drivers/unix/os_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,15 @@ int OS_Unix::get_process_id() const {
return getpid();
}

bool OS_Unix::is_process_running(const ProcessID &p_pid) const {
int status = 0;
if (waitpid(p_pid, &status, WNOHANG) != 0) {
return false;
}

return true;
}

bool OS_Unix::has_environment(const String &p_var) const {
return getenv(p_var.utf8().get_data()) != nullptr;
}
Expand Down
1 change: 1 addition & 0 deletions drivers/unix/os_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class OS_Unix : public OS {
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
virtual Error kill(const ProcessID &p_pid) override;
virtual int get_process_id() const override;
virtual bool is_process_running(const ProcessID &p_pid) const override;

virtual bool has_environment(const String &p_var) const override;
virtual String get_environment(const String &p_var) const override;
Expand Down
4 changes: 4 additions & 0 deletions platform/javascript/os_javascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ int OS_JavaScript::get_process_id() const {
ERR_FAIL_V_MSG(0, "OS::get_process_id() is not available on the HTML5 platform.");
}

bool OS_JavaScript::is_process_running(const ProcessID &p_pid) const {
return false;
}

int OS_JavaScript::get_processor_count() const {
return godot_js_os_hw_concurrency_get();
}
Expand Down
1 change: 1 addition & 0 deletions platform/javascript/os_javascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class OS_JavaScript : public OS_Unix {
Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
Error kill(const ProcessID &p_pid) override;
int get_process_id() const override;
bool is_process_running(const ProcessID &p_pid) const override;
int get_processor_count() const override;
int get_default_thread_pool_size() const override { return 1; }

Expand Down
4 changes: 4 additions & 0 deletions platform/uwp/os_uwp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,10 @@ Error OS_UWP::kill(const ProcessID &p_pid) {
return FAILED;
}

bool OS_UWP::is_process_running(const ProcessID &p_pid) const {
return false;
}

Error OS_UWP::set_cwd(const String &p_cwd) {
return FAILED;
}
Expand Down
1 change: 1 addition & 0 deletions platform/uwp/os_uwp.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class OS_UWP : public OS {
virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr, bool p_open_console = false);
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false);
virtual Error kill(const ProcessID &p_pid);
virtual bool is_process_running(const ProcessID &p_pid) const;

virtual bool has_environment(const String &p_var) const;
virtual String get_environment(const String &p_var) const;
Expand Down
19 changes: 19 additions & 0 deletions platform/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,25 @@ int OS_Windows::get_process_id() const {
return _getpid();
}

bool OS_Windows::is_process_running(const ProcessID &p_pid) const {
if (!process_map->has(p_pid)) {
return false;
}

const PROCESS_INFORMATION &pi = (*process_map)[p_pid].pi;

DWORD dw_exit_code = 0;
if (!GetExitCodeProcess(pi.hProcess, &dw_exit_code)) {
return false;
}

if (dw_exit_code != STILL_ACTIVE) {
return false;
}

return true;
}

Error OS_Windows::set_cwd(const String &p_cwd) {
if (_wchdir((LPCWSTR)(p_cwd.utf16().get_data())) != 0) {
return ERR_CANT_OPEN;
Expand Down
1 change: 1 addition & 0 deletions platform/windows/os_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class OS_Windows : public OS {
virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) override;
virtual Error kill(const ProcessID &p_pid) override;
virtual int get_process_id() const override;
virtual bool is_process_running(const ProcessID &p_pid) const override;

virtual bool has_environment(const String &p_var) const override;
virtual String get_environment(const String &p_var) const override;
Expand Down

0 comments on commit f3c1232

Please sign in to comment.