diff options
Diffstat (limited to 'drivers/unix/os_unix.cpp')
-rw-r--r-- | drivers/unix/os_unix.cpp | 65 |
1 files changed, 54 insertions, 11 deletions
diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index ffeb5ad752..ce2553456d 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -168,11 +168,13 @@ void OS_Unix::initialize_core() { NetSocketPosix::make_default(); IPUnix::make_default(); + process_map = memnew((HashMap<ProcessID, ProcessInfo>)); _setup_clock(); } void OS_Unix::finalize_core() { + memdelete(process_map); NetSocketPosix::cleanup(); } @@ -544,8 +546,8 @@ Dictionary OS_Unix::execute_with_pipe(const String &p_path, const List<String> & // The child process. Vector<CharString> cs; cs.push_back(p_path.utf8()); - for (int i = 0; i < p_arguments.size(); i++) { - cs.push_back(p_arguments[i].utf8()); + for (const String &arg : p_arguments) { + cs.push_back(arg.utf8()); } Vector<char *> args; @@ -582,6 +584,11 @@ Dictionary OS_Unix::execute_with_pipe(const String &p_path, const List<String> & err_pipe.instantiate(); err_pipe->open_existing(pipe_err[0], 0); + ProcessInfo pi; + process_map_mutex.lock(); + process_map->insert(pid, pi); + process_map_mutex.unlock(); + ret["stdio"] = main_pipe; ret["stderr"] = err_pipe; ret["pid"] = pid; @@ -599,8 +606,8 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, St #else if (r_pipe) { String command = "\"" + p_path + "\""; - for (int i = 0; i < p_arguments.size(); i++) { - command += String(" \"") + p_arguments[i] + "\""; + for (const String &arg : p_arguments) { + command += String(" \"") + arg + "\""; } if (read_stderr) { command += " 2>&1"; // Include stderr @@ -640,8 +647,8 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, St // The child process Vector<CharString> cs; cs.push_back(p_path.utf8()); - for (int i = 0; i < p_arguments.size(); i++) { - cs.push_back(p_arguments[i].utf8()); + for (const String &arg : p_arguments) { + cs.push_back(arg.utf8()); } Vector<char *> args; @@ -682,8 +689,8 @@ Error OS_Unix::create_process(const String &p_path, const List<String> &p_argume Vector<CharString> cs; cs.push_back(p_path.utf8()); - for (int i = 0; i < p_arguments.size(); i++) { - cs.push_back(p_arguments[i].utf8()); + for (const String &arg : p_arguments) { + cs.push_back(arg.utf8()); } Vector<char *> args; @@ -698,6 +705,11 @@ Error OS_Unix::create_process(const String &p_path, const List<String> &p_argume raise(SIGKILL); } + ProcessInfo pi; + process_map_mutex.lock(); + process_map->insert(pid, pi); + process_map_mutex.unlock(); + if (r_child_id) { *r_child_id = pid; } @@ -720,14 +732,45 @@ int OS_Unix::get_process_id() const { } bool OS_Unix::is_process_running(const ProcessID &p_pid) const { + MutexLock lock(process_map_mutex); + const ProcessInfo *pi = process_map->getptr(p_pid); + + if (pi && !pi->is_running) { + return false; + } + int status = 0; if (waitpid(p_pid, &status, WNOHANG) != 0) { + if (pi) { + pi->is_running = false; + pi->exit_code = status; + } return false; } return true; } +int OS_Unix::get_process_exit_code(const ProcessID &p_pid) const { + MutexLock lock(process_map_mutex); + const ProcessInfo *pi = process_map->getptr(p_pid); + + if (pi && !pi->is_running) { + return pi->exit_code; + } + + int status = 0; + if (waitpid(p_pid, &status, WNOHANG) != 0) { + status = WIFEXITED(status) ? WEXITSTATUS(status) : status; + if (pi) { + pi->is_running = false; + pi->exit_code = status; + } + return status; + } + return -1; +} + String OS_Unix::get_locale() const { if (!has_environment("LANG")) { return "en"; @@ -741,7 +784,7 @@ String OS_Unix::get_locale() const { return locale; } -Error OS_Unix::open_dynamic_library(const String &p_path, void *&p_library_handle, bool p_also_set_library_path, String *r_resolved_path, bool p_generate_temp_files) { +Error OS_Unix::open_dynamic_library(const String &p_path, void *&p_library_handle, GDExtensionData *p_data) { String path = p_path; if (FileAccess::exists(path) && path.is_relative_path()) { @@ -765,8 +808,8 @@ Error OS_Unix::open_dynamic_library(const String &p_path, void *&p_library_handl p_library_handle = dlopen(path.utf8().get_data(), GODOT_DLOPEN_MODE); ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, dlerror())); - if (r_resolved_path != nullptr) { - *r_resolved_path = path; + if (p_data != nullptr && p_data->r_resolved_path != nullptr) { + *p_data->r_resolved_path = path; } return OK; |