blob: 03af1e72f075a28edba579427c573aed87c96211 [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
Tomasz Wasilczyk634304a2024-01-09 10:39:16 -080019#include <functional>
Yifan Hongaf766e62021-06-14 13:24:19 -070020#include <optional>
21#include <ostream>
22#include <string>
23#include <variant>
24#include <vector>
25
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070026#include <android-base/macros.h>
Frederick Maylef7b65d12024-05-14 16:55:14 -070027#include <binder/Common.h>
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070028#include <binder/unique_fd.h>
Tomasz Wasilczyk88aa8c32023-11-01 09:46:07 -070029#include <utils/Errors.h>
Yifan Hongaf766e62021-06-14 13:24:19 -070030
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
42namespace android {
43
Frederick Maylef7b65d12024-05-14 16:55:14 -070044struct LIBBINDER_EXPORTED CommandResult {
Yifan Hongaf766e62021-06-14 13:24:19 -070045 std::optional<int32_t> exitCode;
46 std::optional<int32_t> signal;
47 std::optional<pid_t> pid;
Colin Crossc9a77aa2021-09-13 16:31:38 -070048 std::string stdoutStr;
49 std::string stderrStr;
Yifan Hongaf766e62021-06-14 13:24:19 -070050
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070051 binder::unique_fd outPipe;
52 binder::unique_fd errPipe;
Yifan Hongaf766e62021-06-14 13:24:19 -070053
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 Crossc9a77aa2021-09-13 16:31:38 -070060 std::swap(stdoutStr, other.stdoutStr);
61 std::swap(stderrStr, other.stderrStr);
Yifan Hongaf766e62021-06-14 13:24:19 -070062 return *this;
63 }
64 ~CommandResult();
65 [[nodiscard]] std::string toString() const;
66
67 [[nodiscard]] bool stdoutEndsWithNewLine() const {
Colin Crossc9a77aa2021-09-13 16:31:38 -070068 return !stdoutStr.empty() && stdoutStr.back() == '\n';
Yifan Hongaf766e62021-06-14 13:24:19 -070069 }
70
71private:
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070072 CommandResult(const CommandResult&) = delete;
73 void operator=(const CommandResult&) = delete;
Yifan Hongaf766e62021-06-14 13:24:19 -070074};
75
Frederick Maylef7b65d12024-05-14 16:55:14 -070076LIBBINDER_EXPORTED std::ostream& operator<<(std::ostream& os, const CommandResult& res);
Yifan Hongaf766e62021-06-14 13:24:19 -070077
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 Maylef7b65d12024-05-14 16:55:14 -0700100LIBBINDER_EXPORTED std::optional<CommandResult> execute(
101 std::vector<std::string> argStringVec,
102 const std::function<bool(const CommandResult&)>& end);
Yifan Hongaf766e62021-06-14 13:24:19 -0700103} // namespace android