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 
19 #include <string>
20 
21 #include "adb_unique_fd.h"
22 
23 #include <string_view>
24 
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.
39 unique_fd StartSubprocess(std::string name, const char* terminal_type, SubprocessType type,
40                           SubprocessProtocol protocol);
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.
51 using Command = int(std::string_view args, borrowed_fd in, borrowed_fd out, borrowed_fd err);
52 unique_fd StartCommandInProcess(std::string name, Command command, SubprocessProtocol protocol);
53 
54 // Create a pipe containing the error.
55 unique_fd ReportError(SubprocessProtocol protocol, const std::string& message);
56