| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 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 |  | 
| David Pursell | b9e2e84 | 2015-08-31 15:36:18 -0700 | [diff] [blame] | 17 | // This file contains classes and functionality to launch shell subprocesses | 
|  | 18 | // in adbd and communicate between those subprocesses and the adb client. | 
|  | 19 | // | 
|  | 20 | // The main features exposed here are: | 
|  | 21 | //   1. A ShellPacket class to wrap data in a simple protocol. Both adbd and | 
|  | 22 | //      the adb client use this class to transmit data between them. | 
|  | 23 | //   2. Functions to launch a subprocess on the adbd side. | 
|  | 24 |  | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 25 | #ifndef SHELL_SERVICE_H_ | 
|  | 26 | #define SHELL_SERVICE_H_ | 
|  | 27 |  | 
| David Pursell | b9e2e84 | 2015-08-31 15:36:18 -0700 | [diff] [blame] | 28 | #include <stdint.h> | 
|  | 29 |  | 
|  | 30 | #include <base/macros.h> | 
|  | 31 |  | 
|  | 32 | #include "adb.h" | 
|  | 33 |  | 
|  | 34 | // Class to send and receive shell protocol packets. | 
|  | 35 | // | 
|  | 36 | // To keep things simple and predictable, reads and writes block until an entire | 
|  | 37 | // packet is complete. | 
|  | 38 | // | 
|  | 39 | // Example: read raw data from |fd| and send it in a packet. | 
|  | 40 | //   ShellProtocol* p = new ShellProtocol(protocol_fd); | 
|  | 41 | //   int len = adb_read(stdout_fd, p->data(), p->data_capacity()); | 
|  | 42 | //   packet->WritePacket(ShellProtocol::kIdStdout, len); | 
|  | 43 | // | 
|  | 44 | // Example: read a packet and print it to |stdout|. | 
|  | 45 | //   ShellProtocol* p = new ShellProtocol(protocol_fd); | 
|  | 46 | //   if (p->ReadPacket() && p->id() == kIdStdout) { | 
|  | 47 | //       fwrite(p->data(), 1, p->data_length(), stdout); | 
|  | 48 | //   } | 
|  | 49 | class ShellProtocol { | 
|  | 50 | public: | 
|  | 51 | // This is an unscoped enum to make it easier to compare against raw bytes. | 
|  | 52 | enum Id : uint8_t { | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 53 | kIdStdin = 0, | 
| David Pursell | b9e2e84 | 2015-08-31 15:36:18 -0700 | [diff] [blame] | 54 | kIdStdout = 1, | 
|  | 55 | kIdStderr = 2, | 
| David Pursell | 1ed57f0 | 2015-10-06 15:30:03 -0700 | [diff] [blame] | 56 | kIdExit = 3, | 
|  | 57 | kIdCloseStdin = 4,  // Close subprocess stdin if possible. | 
|  | 58 | kIdInvalid = 255,  // Indicates an invalid or unknown packet. | 
| David Pursell | b9e2e84 | 2015-08-31 15:36:18 -0700 | [diff] [blame] | 59 | }; | 
|  | 60 |  | 
|  | 61 | // ShellPackets will probably be too large to allocate on the stack so they | 
|  | 62 | // should be dynamically allocated on the heap instead. | 
|  | 63 | // | 
|  | 64 | // |fd| is an open file descriptor to be used to send or receive packets. | 
|  | 65 | explicit ShellProtocol(int fd); | 
|  | 66 | virtual ~ShellProtocol(); | 
|  | 67 |  | 
|  | 68 | // Returns a pointer to the data buffer. | 
|  | 69 | const char* data() const { return buffer_ + kHeaderSize; } | 
|  | 70 | char* data() { return buffer_ + kHeaderSize; } | 
|  | 71 |  | 
|  | 72 | // Returns the total capacity of the data buffer. | 
|  | 73 | size_t data_capacity() const { return buffer_end_ - data(); } | 
|  | 74 |  | 
|  | 75 | // Reads a packet from the FD. | 
|  | 76 | // | 
|  | 77 | // If a packet is too big to fit in the buffer then Read() will split the | 
|  | 78 | // packet across multiple calls. For example, reading a 50-byte packet into | 
|  | 79 | // a 20-byte buffer would read 20 bytes, 20 bytes, then 10 bytes. | 
|  | 80 | // | 
|  | 81 | // Returns false if the FD closed or errored. | 
|  | 82 | bool Read(); | 
|  | 83 |  | 
|  | 84 | // Returns the ID of the packet in the buffer. | 
|  | 85 | int id() const { return buffer_[0]; } | 
|  | 86 |  | 
|  | 87 | // Returns the number of bytes that have been read into the data buffer. | 
|  | 88 | size_t data_length() const { return data_length_; } | 
|  | 89 |  | 
|  | 90 | // Writes the packet currently in the buffer to the FD. | 
|  | 91 | // | 
|  | 92 | // Returns false if the FD closed or errored. | 
|  | 93 | bool Write(Id id, size_t length); | 
|  | 94 |  | 
|  | 95 | private: | 
|  | 96 | // Packets support 4-byte lengths. | 
|  | 97 | typedef uint32_t length_t; | 
|  | 98 |  | 
|  | 99 | enum { | 
|  | 100 | // It's OK if MAX_PAYLOAD doesn't match on the sending and receiving | 
|  | 101 | // end, reading will split larger packets into multiple smaller ones. | 
|  | 102 | kBufferSize = MAX_PAYLOAD, | 
|  | 103 |  | 
|  | 104 | // Header is 1 byte ID + 4 bytes length. | 
|  | 105 | kHeaderSize = sizeof(Id) + sizeof(length_t) | 
|  | 106 | }; | 
|  | 107 |  | 
|  | 108 | int fd_; | 
|  | 109 | char buffer_[kBufferSize]; | 
|  | 110 | size_t data_length_ = 0, bytes_left_ = 0; | 
|  | 111 |  | 
|  | 112 | // We need to be able to modify this value for testing purposes, but it | 
|  | 113 | // will stay constant during actual program use. | 
|  | 114 | char* buffer_end_ = buffer_ + sizeof(buffer_); | 
|  | 115 |  | 
|  | 116 | friend class ShellProtocolTest; | 
|  | 117 |  | 
|  | 118 | DISALLOW_COPY_AND_ASSIGN(ShellProtocol); | 
|  | 119 | }; | 
|  | 120 |  | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 121 | #if !ADB_HOST | 
|  | 122 |  | 
|  | 123 | enum class SubprocessType { | 
|  | 124 | kPty, | 
|  | 125 | kRaw, | 
|  | 126 | }; | 
|  | 127 |  | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 128 | enum class SubprocessProtocol { | 
|  | 129 | kNone, | 
|  | 130 | kShell, | 
|  | 131 | }; | 
|  | 132 |  | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 133 | // Forks and starts a new shell subprocess. If |name| is empty an interactive | 
|  | 134 | // shell is started, otherwise |name| is executed non-interactively. | 
|  | 135 | // | 
|  | 136 | // Returns an open FD connected to the subprocess or -1 on failure. | 
| David Pursell | 0955c66 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 137 | int StartSubprocess(const char* name, SubprocessType type, | 
|  | 138 | SubprocessProtocol protocol); | 
| David Pursell | 80f6702 | 2015-08-28 15:08:49 -0700 | [diff] [blame] | 139 |  | 
|  | 140 | #endif  // !ADB_HOST | 
|  | 141 |  | 
|  | 142 | #endif  // SHELL_SERVICE_H_ |