| 120 | |
| 121 | |
| 122 | bool AsyncCommandExecutor::execute() |
| 123 | { |
| 124 | DBG_FUNCTION_ENTER_MSG; |
| 125 | if (this->running_ || this->stopped_cleanup_needed()) { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | cleanup_members(); |
| 130 | clear_errors(); |
| 131 | str_stdout_.clear(); |
| 132 | str_stderr_.clear(); |
| 133 | |
| 134 | |
| 135 | // Set the locale for a child to Classic - otherwise it may mangle the output. |
| 136 | // TODO: Disable this for JSON format. |
| 137 | bool change_lang = true; |
| 138 | if constexpr(BuildEnv::is_kernel_family_windows()) { |
| 139 | // LANG is posix-only, so it has no effect on win32. |
| 140 | // Unfortunately, I was unable to find a way to execute a child with a different |
| 141 | // locale in win32. Locale seems to be non-inheritable, so setting it here won't help. |
| 142 | change_lang = false; |
| 143 | } |
| 144 | |
| 145 | std::unique_ptr<gchar*, decltype(&g_strfreev)> child_env(g_get_environ(), &g_strfreev); |
| 146 | if (change_lang) { |
| 147 | child_env.reset(g_environ_setenv(child_env.release(), "LC_ALL", "C", TRUE)); |
| 148 | } |
| 149 | const std::vector<std::string> envp = Glib::ArrayHandler<std::string>::array_to_vector(child_env.release(), |
| 150 | Glib::OWNERSHIP_DEEP); |
| 151 | |
| 152 | // Set the current directory to application directory so CWD does not interfere with finding binaries. |
| 153 | auto current_path = hz::fs::current_path(); |
| 154 | bool path_changed = false; |
| 155 | if (auto app_dir = hz::fs_get_application_dir(); !app_dir.empty()) { |
| 156 | std::error_code ec; |
| 157 | hz::fs::current_path(app_dir, ec); |
| 158 | path_changed = !ec; |
| 159 | } |
| 160 | |
| 161 | debug_out_info("app", DBG_FUNC_MSG << "Executing \"" << command_exec_ << "\".\n"); |
| 162 | debug_out_info("app", DBG_FUNC_MSG << "Arguments:\n"); |
| 163 | for (const auto& arg : command_args_) { |
| 164 | debug_out_info("app", " " << arg << "\n"); |
| 165 | } |
| 166 | |
| 167 | std::vector<std::string> argvp = {command_exec_}; |
| 168 | argvp.insert(argvp.end(), command_args_.begin(), command_args_.end()); |
| 169 | |
| 170 | // Execute the command |
| 171 | try { |
| 172 | Glib::spawn_async_with_pipes(Glib::get_current_dir(), argvp, envp, |
| 173 | Glib::SpawnFlags::SPAWN_SEARCH_PATH | Glib::SpawnFlags::SPAWN_DO_NOT_REAP_CHILD, |
| 174 | Glib::SlotSpawnChildSetup(), |
| 175 | &this->pid_, nullptr, &fd_stdout_, &fd_stderr_); |
| 176 | } |
| 177 | catch(Glib::SpawnError& e) { |
| 178 | // no data is returned to &-parameters on error. |
| 179 | push_error(Error<void>("gspawn", ErrorLevel::Error, e.what())); |
nothing calls this directly
no test coverage detected