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