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 
19 #include <functional>
20 #include <optional>
21 #include <ostream>
22 #include <string>
23 #include <variant>
24 #include <vector>
25 
26 #include <android-base/macros.h>
27 #include <binder/Common.h>
28 #include <binder/unique_fd.h>
29 #include <utils/Errors.h>
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 
44 struct LIBBINDER_EXPORTED CommandResult {
45     std::optional<int32_t> exitCode;
46     std::optional<int32_t> signal;
47     std::optional<pid_t> pid;
48     std::string stdoutStr;
49     std::string stderrStr;
50 
51     binder::unique_fd outPipe;
52     binder::unique_fd errPipe;
53 
54     CommandResult() = default;
CommandResultCommandResult55     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);
60         std::swap(stdoutStr, other.stdoutStr);
61         std::swap(stderrStr, other.stderrStr);
62         return *this;
63     }
64     ~CommandResult();
65     [[nodiscard]] std::string toString() const;
66 
stdoutEndsWithNewLineCommandResult67     [[nodiscard]] bool stdoutEndsWithNewLine() const {
68         return !stdoutStr.empty() && stdoutStr.back() == '\n';
69     }
70 
71 private:
72     CommandResult(const CommandResult&) = delete;
73     void operator=(const CommandResult&) = delete;
74 };
75 
76 LIBBINDER_EXPORTED std::ostream& operator<<(std::ostream& os, const CommandResult& res);
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.
100 LIBBINDER_EXPORTED std::optional<CommandResult> execute(
101         std::vector<std::string> argStringVec,
102         const std::function<bool(const CommandResult&)>& end);
103 } // namespace android
104