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