summaryrefslogtreecommitdiff
path: root/libs/binder/UtilsHost.h
blob: 98ac4e0c48a9d571ed1408f4d5b6a48a7cf92f79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <optional>
#include <ostream>
#include <string>
#include <variant>
#include <vector>

#include <android-base/macros.h>
#include <android-base/result.h>
#include <android-base/unique_fd.h>

/**
 * Log a lot more information about host-device binder communication, when debugging issues.
 */
#define SHOULD_LOG_HOST false

#if SHOULD_LOG_HOST
#define LOG_HOST(...) ALOGI(__VA_ARGS__)
#else
#define LOG_HOST(...) ALOGV(__VA_ARGS__) // for type checking
#endif

namespace android {

struct CommandResult {
    std::optional<int32_t> exitCode;
    std::optional<int32_t> signal;
    std::optional<pid_t> pid;
    std::string stdoutStr;
    std::string stderrStr;

    android::base::unique_fd outPipe;
    android::base::unique_fd errPipe;

    CommandResult() = default;
    CommandResult(CommandResult&& other) noexcept { (*this) = std::move(other); }
    CommandResult& operator=(CommandResult&& other) noexcept {
        std::swap(exitCode, other.exitCode);
        std::swap(signal, other.signal);
        std::swap(pid, other.pid);
        std::swap(stdoutStr, other.stdoutStr);
        std::swap(stderrStr, other.stderrStr);
        return *this;
    }
    ~CommandResult();
    [[nodiscard]] std::string toString() const;

    [[nodiscard]] bool stdoutEndsWithNewLine() const {
        return !stdoutStr.empty() && stdoutStr.back() == '\n';
    }

private:
    DISALLOW_COPY_AND_ASSIGN(CommandResult);
};

std::ostream& operator<<(std::ostream& os, const CommandResult& res);

// Execute a command using tokens specified in @a argStringVec.
//
// @a end is a predicate checked periodically when the command emits any output to stdout or
// stderr. When it is evaluated to true, the function returns immediately even though
// the child process has not been terminated. The function also assumes that, after @a end
// is evaluated to true, the child process does not emit any other messages.
// If this is not the case, caller to execute() must handle these I/O in the pipes in the returned
// CommandResult object. Otherwise the child program may hang on I/O.
//
// If @a end is nullptr, it is equivalent to a predicate that always returns false. In this
// case, execute() returns after the child process is terminated.
//
// If @a end is evaluated to true, and execute() returns with the child process running,
// the returned CommandResult has pid, outPipe, and errPipe set. In this case, the caller is
// responsible for holding the returned CommandResult. When the CommandResult object is destroyed,
// the child process is killed.
//
// On the other hand, execute() returns with the child process terminated, either exitCode or signal
// is set.
//
// If the parent process has encountered any errors for system calls, return ExecuteError with
// the proper errno set.
android::base::Result<CommandResult> execute(std::vector<std::string> argStringVec,
                                             const std::function<bool(const CommandResult&)>& end);
} // namespace android