| 6 |
| 7 | #include <lib/fdio/limits.h> |
| 8 | #include <lib/fdio/namespace.h> |
| 9 | #include <lib/fdio/spawn.h> |
| 10 | #include <lib/zx/job.h> |
| 11 | #include <stdint.h> |
| 12 | #include <unistd.h> |
| 13 | #include <zircon/processargs.h> |
| 14 |
| 15 | #include "base/command_line.h" |
| 16 | #include "base/files/file_util.h" |
| 17 | #include "base/fuchsia/default_job.h" |
| 18 | #include "base/fuchsia/file_utils.h" |
| 19 | #include "base/fuchsia/fuchsia_logging.h" |
| 20 | #include "base/logging.h" |
| 21 | #include "base/memory/ptr_util.h" |
| 22 | #include "base/process/environment_internal.h" |
| 23 | #include "base/scoped_generic.h" |
| 24 |
| 25 | namespace base { |
| 26 |
| 27 | namespace { |
| 28 |
| 29 | bool GetAppOutputInternal(const CommandLine& cmd_line, |
| 30 | bool include_stderr, |
| 31 | std::string* output, |
| 32 | int* exit_code) { |
| 33 | DCHECK(exit_code); |
| 34 |
| 35 | LaunchOptions options; |
| 36 |
| 37 | // LaunchProcess will automatically clone any stdio fd we do not explicitly |
| 38 | // map. |
| 39 | int pipe_fd[2]; |
| 40 | if (pipe(pipe_fd) < 0) |
| 41 | return false; |
| 42 | options.fds_to_remap.emplace_back(pipe_fd[1], STDOUT_FILENO); |
| 43 | if (include_stderr) |
| 44 | options.fds_to_remap.emplace_back(pipe_fd[1], STDERR_FILENO); |
| 45 |
| 46 | Process process = LaunchProcess(cmd_line, options); |
| 47 | close(pipe_fd[1]); |
| 48 | if (!process.IsValid()) { |
| 49 | close(pipe_fd[0]); |
| 50 | return false; |
| 51 | } |
| 52 |
| 53 | output->clear(); |
| 54 | for (;;) { |
| 55 | char buffer[256]; |
| 56 | ssize_t bytes_read = read(pipe_fd[0], buffer, sizeof(buffer)); |
| 57 | if (bytes_read <= 0) |
| 58 | break; |
| 59 | output->append(buffer, bytes_read); |
| 60 | } |
| 61 | close(pipe_fd[0]); |
| 62 |
| 63 | return process.WaitForExit(exit_code); |
| 64 | } |
| 65 |
| 66 | fdio_spawn_action_t FdioSpawnAction(uint32_t action) { |
| 67 | fdio_spawn_action_t new_action = {}; |
| 68 | new_action.action = action; |
| 69 | return new_action; |
| 70 | } |
| 71 |
| 72 | fdio_spawn_action_t FdioSpawnActionCloneFd(int local_fd, int target_fd) { |
| 73 | fdio_spawn_action_t action = FdioSpawnAction(FDIO_SPAWN_ACTION_CLONE_FD); |
| 74 | action.fd.local_fd = local_fd; |
| 75 | action.fd.target_fd = target_fd; |
| 76 | return action; |
| 77 | } |
| 78 |
| 79 | fdio_spawn_action_t FdioSpawnActionAddNamespaceEntry(const char* prefix, |
| 80 | zx_handle_t handle) { |
| 81 | fdio_spawn_action_t action = FdioSpawnAction(FDIO_SPAWN_ACTION_ADD_NS_ENTRY); |
| 82 | action.ns.prefix = prefix; |
| 83 | action.ns.handle = handle; |
| 84 | return action; |
| 85 | } |
| 86 |
| 87 | fdio_spawn_action_t FdioSpawnActionAddHandle(uint32_t id, zx_handle_t handle) { |
| 88 | fdio_spawn_action_t action = FdioSpawnAction(FDIO_SPAWN_ACTION_ADD_HANDLE); |
| 89 | action.h.id = id; |
| 90 | action.h.handle = handle; |
| 91 | return action; |
| 92 | } |
| 93 |
| 94 | fdio_spawn_action_t FdioSpawnActionSetName(const char* name) { |
| 95 | fdio_spawn_action_t action = FdioSpawnAction(FDIO_SPAWN_ACTION_SET_NAME); |
| 96 | action.name.data = name; |
| 97 | return action; |
| 98 | } |
| 99 |
| 100 | } // namespace |
| 101 |
| 102 | // static |
| 103 | uint32_t LaunchOptions::AddHandleToTransfer( |
| 104 | HandlesToTransferVector* handles_to_transfer, |
| 105 | zx_handle_t handle) { |
| 106 | uint32_t handle_id = PA_HND(PA_USER1, handles_to_transfer->size()); |
| 107 | handles_to_transfer->push_back({handle_id, handle}); |
| 108 | return handle_id; |
| 109 | } |
| 110 |
| 111 | Process LaunchProcess(const CommandLine& cmdline, |
| 112 | const LaunchOptions& options) { |
| 113 | return LaunchProcess(cmdline.argv(), options); |
| 114 | } |
| 115 |
| 116 | // TODO(768416): Investigate whether we can make LaunchProcess() create |
| 117 | // unprivileged processes by default (no implicit capabilities are granted). |
| 118 | Process LaunchProcess(const std::vector<std::string>& argv, |
| 119 | const LaunchOptions& options) { |
| 120 | // fdio_spawn_etc() accepts an array of |fdio_spawn_action_t|, describing |
| 121 | // namespace entries, descriptors and handles to launch the child process |
| 122 | // with. |
| 123 | std::vector<fdio_spawn_action_t> spawn_actions; |
| 124 |
| 125 | // Handles to be transferred to the child are owned by this vector, so that |
| 126 | // they they are closed on early-exit, and can be release()d otherwise. |
| 127 | std::vector<zx::handle> transferred_handles; |
| 128 |
| 129 | // Add caller-supplied handles for transfer. We must do this first to ensure |
| 130 | // that the handles are consumed even if some later step fails. |
| 131 | for (const auto& id_and_handle : options.handles_to_transfer) { |
| 132 | spawn_actions.push_back( |
| 133 | FdioSpawnActionAddHandle(id_and_handle.id, id_and_handle.handle)); |
| 134 | transferred_handles.emplace_back(id_and_handle.handle); |
| 135 | } |
| 136 |
| 137 | // Determine the job under which to launch the new process. |
| 138 | zx::unowned_job job = options.job_handle != ZX_HANDLE_INVALID |
| 139 | ? zx::unowned_job(options.job_handle) |
| 140 | : GetDefaultJob(); |
| 141 | DCHECK(job->is_valid()); |
| 142 |
| 143 | // Construct an |argv| array of C-strings from the supplied std::strings. |
| 144 | std::vector<const char*> argv_cstr; |
| 145 | argv_cstr.reserve(argv.size() + 1); |
| 146 | for (const auto& arg : argv) |
| 147 | argv_cstr.push_back(arg.c_str()); |
| 148 | argv_cstr.push_back(nullptr); |
| 149 |
| 150 | // Determine the environment to pass to the new process. If |
| 151 | // |clear_environment|, |environment| or |current_directory| are set then we |
| 152 | // construct a new (possibly empty) environment, otherwise we let fdio_spawn() |
| 153 | // clone the caller's environment into the new process. |
| 154 | uint32_t spawn_flags = FDIO_SPAWN_CLONE_LDSVC | options.spawn_flags; |
| 155 |
| 156 | EnvironmentMap environ_modifications = options.environment; |
| 157 | if (!options.current_directory.empty()) { |
| 158 | environ_modifications["PWD"] = options.current_directory.value(); |
| 159 | } else { |
| 160 | FilePath cwd; |
| 161 | GetCurrentDirectory(&cwd); |
| 162 | environ_modifications["PWD"] = cwd.value(); |
| 163 | } |
| 164 |
| 165 | std::unique_ptr<char* []> new_environ; |
| 166 | if (!environ_modifications.empty()) { |
| 167 | char* const empty_environ = nullptr; |
| 168 | char* const* old_environ = |
| 169 | options.clear_environment ? &empty_environ : environ; |
| 170 | new_environ = |
| 171 | internal::AlterEnvironment(old_environ, environ_modifications); |
| 172 | } else if (!options.clear_environment) { |
| 173 | spawn_flags |= FDIO_SPAWN_CLONE_ENVIRON; |
| 174 | } |
| 175 |
| 176 | // Add actions to clone handles for any specified paths into the new process' |
| 177 | // namespace. |
| 178 | if (!options.paths_to_clone.empty() || !options.paths_to_transfer.empty()) { |
| 179 | DCHECK((options.spawn_flags & FDIO_SPAWN_CLONE_NAMESPACE) == 0); |
| 180 | transferred_handles.reserve(transferred_handles.size() + |
| 181 | options.paths_to_clone.size() + |
| 182 | options.paths_to_transfer.size()); |
| 183 |
| 184 | for (const auto& path_to_transfer : options.paths_to_transfer) { |
| 185 | zx::handle handle(path_to_transfer.handle); |
| 186 | spawn_actions.push_back(FdioSpawnActionAddNamespaceEntry( |
| 187 | path_to_transfer.path.value().c_str(), handle.get())); |
| 188 | transferred_handles.push_back(std::move(handle)); |
| 189 | } |
| 190 |
| 191 | for (const auto& path_to_clone : options.paths_to_clone) { |
| 192 | fidl::InterfaceHandle<::fuchsia::io::Directory> directory = |
| 193 | base::fuchsia::OpenDirectory(path_to_clone); |
| 194 | if (!directory) { |
| 195 | LOG(WARNING) << "Could not open handle for path: " << path_to_clone; |
| 196 | return base::Process(); |
| 197 | } |
| 198 |
| 199 | zx::handle handle = directory.TakeChannel(); |
| 200 |
| 201 | spawn_actions.push_back(FdioSpawnActionAddNamespaceEntry( |
| 202 | path_to_clone.value().c_str(), handle.get())); |
| 203 | transferred_handles.push_back(std::move(handle)); |
| 204 | } |
| 205 | } |
| 206 |
| 207 | // Add any file-descriptors to be cloned into the new process. |
| 208 | // Note that if FDIO_SPAWN_CLONE_STDIO is set, then any stdio entries in |
| 209 | // |fds_to_remap| will be used in place of the parent process' descriptors. |
| 210 | for (const auto& src_target : options.fds_to_remap) { |
| 211 | spawn_actions.push_back( |
| 212 | FdioSpawnActionCloneFd(src_target.first, src_target.second)); |
| 213 | } |
| 214 |
| 215 | // If |process_name_suffix| is specified then set process name as |
| 216 | // "<file_name><suffix>", otherwise leave the default value. |
| 217 | std::string process_name; |
| 218 | if (!options.process_name_suffix.empty()) { |
| 219 | process_name = base::FilePath(argv[0]).BaseName().value() + |
| 220 | options.process_name_suffix; |
| 221 | spawn_actions.push_back(FdioSpawnActionSetName(process_name.c_str())); |
| 222 | } |
| 223 |
| 224 | zx::process process_handle; |
| 225 | // fdio_spawn_etc() will write a null-terminated scring to |error_message| in |
| 226 | // case of failure, so we avoid unnecessarily initializing it here. |
| 227 | char error_message[FDIO_SPAWN_ERR_MSG_MAX_LENGTH]; |
| 228 | zx_status_t status = fdio_spawn_etc( |
| 229 | job->get(), spawn_flags, argv_cstr[0], argv_cstr.data(), |
| 230 | new_environ.get(), spawn_actions.size(), spawn_actions.data(), |
| 231 | process_handle.reset_and_get_address(), error_message); |
| 232 |
| 233 | // fdio_spawn_etc() will close all handles specified in add-handle actions, |
| 234 | // regardless of whether it succeeds or fails, so release our copies. |
| 235 | for (auto& transferred_handle : transferred_handles) |
| 236 | ignore_result(transferred_handle.release()); |
| 237 |
| 238 | if (status != ZX_OK) { |
| 239 | ZX_LOG(ERROR, status) << "fdio_spawn: " << error_message; |
| 240 | return Process(); |
| 241 | } |
| 242 |
| 243 | // Wrap the handle into a Process, and wait for it to terminate, if requested. |
| 244 | Process process(process_handle.release()); |
| 245 | if (options.wait) { |
| 246 | status = zx_object_wait_one(process.Handle(), ZX_TASK_TERMINATED, |
| 247 | ZX_TIME_INFINITE, nullptr); |
| 248 | ZX_DCHECK(status == ZX_OK, status) << "zx_object_wait_one"; |
| 249 | } |
| 250 |
| 251 | return process; |
| 252 | } |
| 253 |
| 254 | bool GetAppOutput(const CommandLine& cl, std::string* output) { |
| 255 | int exit_code; |
| 256 | bool result = GetAppOutputInternal(cl, false, output, &exit_code); |
| 257 | return result && exit_code == EXIT_SUCCESS; |
| 258 | } |
| 259 |
| 260 | bool GetAppOutput(const std::vector<std::string>& argv, std::string* output) { |
| 261 | return GetAppOutput(CommandLine(argv), output); |
| 262 | } |
| 263 |
| 264 | bool GetAppOutputAndError(const CommandLine& cl, std::string* output) { |
| 265 | int exit_code; |
| 266 | bool result = GetAppOutputInternal(cl, true, output, &exit_code); |
| 267 | return result && exit_code == EXIT_SUCCESS; |
| 268 | } |
| 269 |
| 270 | bool GetAppOutputAndError(const std::vector<std::string>& argv, |
| 271 | std::string* output) { |
| 272 | return GetAppOutputAndError(CommandLine(argv), output); |
| 273 | } |
| 274 |
| 275 | bool GetAppOutputWithExitCode(const CommandLine& cl, |
| 276 | std::string* output, |
| 277 | int* exit_code) { |
| 278 | // Contrary to GetAppOutput(), |true| return here means that the process was |
| 279 | // launched and the exit code was waited upon successfully, but not |
| 280 | // necessarily that the exit code was EXIT_SUCCESS. |
| 281 | return GetAppOutputInternal(cl, false, output, exit_code); |
| 282 | } |
| 283 |
| 284 | void RaiseProcessToHighPriority() { |
| 285 | // Fuchsia doesn't provide an API to change process priority. |
| 286 | } |
| 287 |
| 288 | } // namespace base |
| 289 |