Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 1 | /* |
| 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 Wasilczyk | 634304a | 2024-01-09 10:39:16 -0800 | [diff] [blame] | 19 | #include <functional> |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 20 | #include <optional> |
| 21 | #include <ostream> |
| 22 | #include <string> |
| 23 | #include <variant> |
| 24 | #include <vector> |
| 25 | |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 26 | #include <android-base/macros.h> |
Frederick Mayle | f7b65d1 | 2024-05-14 16:55:14 -0700 | [diff] [blame] | 27 | #include <binder/Common.h> |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 28 | #include <binder/unique_fd.h> |
Tomasz Wasilczyk | 88aa8c3 | 2023-11-01 09:46:07 -0700 | [diff] [blame] | 29 | #include <utils/Errors.h> |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Log a lot more information about host-device binder communication, when debugging issues. |
| 33 | */ |
| 34 | #define SHOULD_LOG_HOST false |
| 35 | |
| 36 | #if SHOULD_LOG_HOST |
| 37 | #define LOG_HOST(...) ALOGI(__VA_ARGS__) |
| 38 | #else |
| 39 | #define LOG_HOST(...) ALOGV(__VA_ARGS__) // for type checking |
| 40 | #endif |
| 41 | |
| 42 | namespace android { |
| 43 | |
Frederick Mayle | f7b65d1 | 2024-05-14 16:55:14 -0700 | [diff] [blame] | 44 | struct LIBBINDER_EXPORTED CommandResult { |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 45 | std::optional<int32_t> exitCode; |
| 46 | std::optional<int32_t> signal; |
| 47 | std::optional<pid_t> pid; |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 48 | std::string stdoutStr; |
| 49 | std::string stderrStr; |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 50 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 51 | binder::unique_fd outPipe; |
| 52 | binder::unique_fd errPipe; |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 53 | |
| 54 | CommandResult() = default; |
| 55 | CommandResult(CommandResult&& other) noexcept { (*this) = std::move(other); } |
| 56 | CommandResult& operator=(CommandResult&& other) noexcept { |
| 57 | std::swap(exitCode, other.exitCode); |
| 58 | std::swap(signal, other.signal); |
| 59 | std::swap(pid, other.pid); |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 60 | std::swap(stdoutStr, other.stdoutStr); |
| 61 | std::swap(stderrStr, other.stderrStr); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 62 | return *this; |
| 63 | } |
| 64 | ~CommandResult(); |
| 65 | [[nodiscard]] std::string toString() const; |
| 66 | |
| 67 | [[nodiscard]] bool stdoutEndsWithNewLine() const { |
Colin Cross | c9a77aa | 2021-09-13 16:31:38 -0700 | [diff] [blame] | 68 | return !stdoutStr.empty() && stdoutStr.back() == '\n'; |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private: |
Tomasz Wasilczyk | df07f94 | 2023-11-02 15:07:45 -0700 | [diff] [blame] | 72 | CommandResult(const CommandResult&) = delete; |
| 73 | void operator=(const CommandResult&) = delete; |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Frederick Mayle | f7b65d1 | 2024-05-14 16:55:14 -0700 | [diff] [blame] | 76 | LIBBINDER_EXPORTED std::ostream& operator<<(std::ostream& os, const CommandResult& res); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 77 | |
| 78 | // Execute a command using tokens specified in @a argStringVec. |
| 79 | // |
| 80 | // @a end is a predicate checked periodically when the command emits any output to stdout or |
| 81 | // stderr. When it is evaluated to true, the function returns immediately even though |
| 82 | // the child process has not been terminated. The function also assumes that, after @a end |
| 83 | // is evaluated to true, the child process does not emit any other messages. |
| 84 | // If this is not the case, caller to execute() must handle these I/O in the pipes in the returned |
| 85 | // CommandResult object. Otherwise the child program may hang on I/O. |
| 86 | // |
| 87 | // If @a end is nullptr, it is equivalent to a predicate that always returns false. In this |
| 88 | // case, execute() returns after the child process is terminated. |
| 89 | // |
| 90 | // If @a end is evaluated to true, and execute() returns with the child process running, |
| 91 | // the returned CommandResult has pid, outPipe, and errPipe set. In this case, the caller is |
| 92 | // responsible for holding the returned CommandResult. When the CommandResult object is destroyed, |
| 93 | // the child process is killed. |
| 94 | // |
| 95 | // On the other hand, execute() returns with the child process terminated, either exitCode or signal |
| 96 | // is set. |
| 97 | // |
| 98 | // If the parent process has encountered any errors for system calls, return ExecuteError with |
| 99 | // the proper errno set. |
Frederick Mayle | f7b65d1 | 2024-05-14 16:55:14 -0700 | [diff] [blame] | 100 | LIBBINDER_EXPORTED std::optional<CommandResult> execute( |
| 101 | std::vector<std::string> argStringVec, |
| 102 | const std::function<bool(const CommandResult&)>& end); |
Yifan Hong | af766e6 | 2021-06-14 13:24:19 -0700 | [diff] [blame] | 103 | } // namespace android |