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