blob: d6fe9facdede48241b4fdc1a40175686a38950b7 [file] [log] [blame]
Yifan Hongaf766e62021-06-14 13:24:19 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
Tomasz Wasilczyk634304a2024-01-09 10:39:16 -080019#include <functional>
Yifan Hongaf766e62021-06-14 13:24:19 -070020#include <optional>
21#include <ostream>
22#include <string>
23#include <variant>
24#include <vector>
25
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070026#include <android-base/macros.h>
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070027#include <binder/unique_fd.h>
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070028#include <utils/Errors.h>
Yifan Hongaf766e62021-06-14 13:24:19 -070029
30/**
31 * Log a lot more information about host-device binder communication, when debugging issues.
32 */
33#define SHOULD_LOG_HOST false
34
35#if SHOULD_LOG_HOST
36#define LOG_HOST(...) ALOGI(__VA_ARGS__)
37#else
38#define LOG_HOST(...) ALOGV(__VA_ARGS__) // for type checking
39#endif
40
41namespace android {
42
43struct CommandResult {
44 std::optional<int32_t> exitCode;
45 std::optional<int32_t> signal;
46 std::optional<pid_t> pid;
Colin Crossc9a77aa2021-09-13 16:31:38 -070047 std::string stdoutStr;
48 std::string stderrStr;
Yifan Hongaf766e62021-06-14 13:24:19 -070049
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070050 binder::unique_fd outPipe;
51 binder::unique_fd errPipe;
Yifan Hongaf766e62021-06-14 13:24:19 -070052
53 CommandResult() = default;
54 CommandResult(CommandResult&& other) noexcept { (*this) = std::move(other); }
55 CommandResult& operator=(CommandResult&& other) noexcept {
56 std::swap(exitCode, other.exitCode);
57 std::swap(signal, other.signal);
58 std::swap(pid, other.pid);
Colin Crossc9a77aa2021-09-13 16:31:38 -070059 std::swap(stdoutStr, other.stdoutStr);
60 std::swap(stderrStr, other.stderrStr);
Yifan Hongaf766e62021-06-14 13:24:19 -070061 return *this;
62 }
63 ~CommandResult();
64 [[nodiscard]] std::string toString() const;
65
66 [[nodiscard]] bool stdoutEndsWithNewLine() const {
Colin Crossc9a77aa2021-09-13 16:31:38 -070067 return !stdoutStr.empty() && stdoutStr.back() == '\n';
Yifan Hongaf766e62021-06-14 13:24:19 -070068 }
69
70private:
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070071 CommandResult(const CommandResult&) = delete;
72 void operator=(const CommandResult&) = delete;
Yifan Hongaf766e62021-06-14 13:24:19 -070073};
74
75std::ostream& operator<<(std::ostream& os, const CommandResult& res);
76
77// Execute a command using tokens specified in @a argStringVec.
78//
79// @a end is a predicate checked periodically when the command emits any output to stdout or
80// stderr. When it is evaluated to true, the function returns immediately even though
81// the child process has not been terminated. The function also assumes that, after @a end
82// is evaluated to true, the child process does not emit any other messages.
83// If this is not the case, caller to execute() must handle these I/O in the pipes in the returned
84// CommandResult object. Otherwise the child program may hang on I/O.
85//
86// If @a end is nullptr, it is equivalent to a predicate that always returns false. In this
87// case, execute() returns after the child process is terminated.
88//
89// If @a end is evaluated to true, and execute() returns with the child process running,
90// the returned CommandResult has pid, outPipe, and errPipe set. In this case, the caller is
91// responsible for holding the returned CommandResult. When the CommandResult object is destroyed,
92// the child process is killed.
93//
94// On the other hand, execute() returns with the child process terminated, either exitCode or signal
95// is set.
96//
97// If the parent process has encountered any errors for system calls, return ExecuteError with
98// the proper errno set.
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070099std::optional<CommandResult> execute(std::vector<std::string> argStringVec,
100 const std::function<bool(const CommandResult&)>& end);
Yifan Hongaf766e62021-06-14 13:24:19 -0700101} // namespace android