blob: 030228c7f7ee43f85e998793df28256c2bba6c69 [file] [log] [blame]
Josh Gao076b5ba2018-07-25 16:07:26 -07001/*
2 * Copyright (C) 2018 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
Josh Gaof0fa1e42018-12-13 13:06:03 -080019#include <string>
20
Josh Gao5607e922018-07-25 18:15:52 -070021#include "adb_unique_fd.h"
22
Alex Buynytskyyef34f012018-12-12 10:48:50 -080023#include <string_view>
24
Josh Gao076b5ba2018-07-25 16:07:26 -070025enum class SubprocessType {
26 kPty,
27 kRaw,
28};
29
30enum class SubprocessProtocol {
31 kNone,
32 kShell,
33};
34
35// Forks and starts a new shell subprocess. If |name| is empty an interactive
36// shell is started, otherwise |name| is executed non-interactively.
37//
38// Returns an open FD connected to the subprocess or -1 on failure.
Josh Gaof0fa1e42018-12-13 13:06:03 -080039unique_fd StartSubprocess(std::string name, const char* terminal_type, SubprocessType type,
Josh Gao5607e922018-07-25 18:15:52 -070040 SubprocessProtocol protocol);
Alex Buynytskyyef34f012018-12-12 10:48:50 -080041
42// The same as above but with more fined grained control and custom error handling.
43unique_fd StartSubprocess(std::string name, const char* terminal_type, SubprocessType type,
44 SubprocessProtocol protocol, bool make_pty_raw,
45 SubprocessProtocol error_protocol, unique_fd* error_fd);
46
47// Executes |command| in a separate thread.
48// Sets up in/out and error streams to emulate shell-like behavior.
49//
50// Returns an open FD connected to the thread or -1 on failure.
Josh Gao90228a62019-04-25 14:04:57 -070051using Command = int(std::string_view args, borrowed_fd in, borrowed_fd out, borrowed_fd err);
Alex Buynytskyy4f3fa052019-02-21 14:22:51 -080052unique_fd StartCommandInProcess(std::string name, Command command, SubprocessProtocol protocol);
Alex Buynytskyyef34f012018-12-12 10:48:50 -080053
54// Create a pipe containing the error.
55unique_fd ReportError(SubprocessProtocol protocol, const std::string& message);