Josh Gao | 076b5ba | 2018-07-25 16:07:26 -0700 | [diff] [blame] | 1 | /* |
| 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 Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 21 | #include "adb_unique_fd.h" |
| 22 | |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 23 | #include <string_view> |
| 24 | |
Josh Gao | 076b5ba | 2018-07-25 16:07:26 -0700 | [diff] [blame] | 25 | enum class SubprocessType { |
| 26 | kPty, |
| 27 | kRaw, |
| 28 | }; |
| 29 | |
| 30 | enum 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 Gao | f0fa1e4 | 2018-12-13 13:06:03 -0800 | [diff] [blame] | 39 | unique_fd StartSubprocess(std::string name, const char* terminal_type, SubprocessType type, |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 40 | SubprocessProtocol protocol); |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 41 | |
| 42 | // The same as above but with more fined grained control and custom error handling. |
| 43 | unique_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 Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 51 | using Command = int(std::string_view args, borrowed_fd in, borrowed_fd out, borrowed_fd err); |
Alex Buynytskyy | 4f3fa05 | 2019-02-21 14:22:51 -0800 | [diff] [blame] | 52 | unique_fd StartCommandInProcess(std::string name, Command command, SubprocessProtocol protocol); |
Alex Buynytskyy | ef34f01 | 2018-12-12 10:48:50 -0800 | [diff] [blame] | 53 | |
| 54 | // Create a pipe containing the error. |
| 55 | unique_fd ReportError(SubprocessProtocol protocol, const std::string& message); |