|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 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 | +package androidx.test.services.shellexecutor |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream |
| 20 | +import java.io.File |
| 21 | +import java.io.IOException |
| 22 | +import java.io.OutputStreamWriter |
| 23 | +import java.io.PrintWriter |
| 24 | +import java.util.UUID |
| 25 | + |
| 26 | +/** |
| 27 | + * The protocol for communicating by FileObserver is: |
| 28 | + * 1. The server creates the server directory in /data/local/tmp. |
| 29 | + * 2. The client creates [UUID].request in the server directory. |
| 30 | + * 3. The server reads and deletes [UUID].request, then writes [UUID].response. |
| 31 | + * 4. The client reads and deletes [UUID].response. |
| 32 | + * |
| 33 | + * The underlying communication is handled by inotify, which only generates events for the |
| 34 | + * directories it is explicitly watching. (The FileObserver documentation makes it sound like it can |
| 35 | + * pick things up in subdirectories; this is erroneous.) |
| 36 | + * |
| 37 | + * The underlying directory and file are set world-readable and -writable so the client can write |
| 38 | + * the request and read the response. Because this only works when someone is already running |
| 39 | + * FileObserverShellMain, there is very little threat here; if someone is able to put a program onto |
| 40 | + * your test device that can watch /data/local/tmp for the appearance of the exchange directory, you |
| 41 | + * have bigger problems than whatever it's going to do with root privileges. |
| 42 | + */ |
| 43 | +@Suppress("SetWorldReadable", "SetWorldWritable") |
| 44 | +object FileObserverProtocol { |
| 45 | + const val REQUEST = "request" |
| 46 | + const val RESPONSE = "response" |
| 47 | + |
| 48 | + /** Creates the exchange directory with appropriate permissions. */ |
| 49 | + fun createExchangeDir(commonDir: File): File { |
| 50 | + val exchangeDir = File.createTempFile("androidx", ".tmp", commonDir) |
| 51 | + exchangeDir.delete() |
| 52 | + exchangeDir.mkdir() |
| 53 | + exchangeDir.setReadable(/* readable= */ true, /* ownerOnly= */ false) |
| 54 | + exchangeDir.setWritable(/* writable= */ true, /* ownerOnly= */ false) |
| 55 | + exchangeDir.setExecutable(/* executable= */ true, /* ownerOnly= */ false) |
| 56 | + return exchangeDir |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Writes a request file to the exchange directory. |
| 61 | + * |
| 62 | + * @return the location for the response file |
| 63 | + */ |
| 64 | + fun writeRequestFile(exchangeDir: File, message: Messages.Command): File { |
| 65 | + val stem = UUID.randomUUID().toString() |
| 66 | + val request = File(exchangeDir, "${stem}.$REQUEST") |
| 67 | + request.outputStream().use { |
| 68 | + request.setReadable(/* readable= */ true, /* ownerOnly= */ false) |
| 69 | + request.setWritable(/* writable= */ true, /* ownerOnly= */ false) |
| 70 | + message.writeTo(it) |
| 71 | + } |
| 72 | + return File(exchangeDir, "${stem}.response") |
| 73 | + } |
| 74 | + |
| 75 | + fun isRequestFile(file: File) = file.name.endsWith(".$REQUEST") |
| 76 | + |
| 77 | + fun calculateResponseFile(requestFile: File) = |
| 78 | + File(requestFile.parentFile, "${requestFile.name.split(".").first()}.$RESPONSE") |
| 79 | + |
| 80 | + /** Reads and deletes the request file */ |
| 81 | + fun readRequestFile(request: File): Messages.Command { |
| 82 | + val command: Messages.Command |
| 83 | + request.inputStream().use { command = Messages.Command.readFrom(it) } |
| 84 | + request.delete() |
| 85 | + return command |
| 86 | + } |
| 87 | + |
| 88 | + /** Writes the response file */ |
| 89 | + fun writeResponseFile(path: File, result: Messages.CommandResult) { |
| 90 | + path.outputStream().use { |
| 91 | + path.setReadable(/* readable= */ true, /* ownerOnly= */ false) |
| 92 | + path.setWritable(/* writable= */ true, /* ownerOnly= */ false) |
| 93 | + result.writeTo(it) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** Reads and deletes the response file. */ |
| 98 | + fun readResponseFile(response: File): Messages.CommandResult { |
| 99 | + try { |
| 100 | + val result: Messages.CommandResult |
| 101 | + response.inputStream().use { result = Messages.CommandResult.readFrom(it) } |
| 102 | + response.delete() |
| 103 | + return result |
| 104 | + } catch (x: IOException) { |
| 105 | + return Messages.CommandResult( |
| 106 | + resultType = Messages.ResultType.CLIENT_ERROR, |
| 107 | + stderr = x.toByteArray() |
| 108 | + ) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Writes an exception stack trace to a ByteArray as UTF-8, to make them easy to pass through |
| 115 | + * Messages.CommandResult. |
| 116 | + */ |
| 117 | +internal fun Exception.toByteArray(): ByteArray { |
| 118 | + val bos = ByteArrayOutputStream() |
| 119 | + val pw = PrintWriter(OutputStreamWriter(bos, Charsets.UTF_8)) |
| 120 | + printStackTrace(pw) |
| 121 | + pw.close() |
| 122 | + return bos.toByteArray() |
| 123 | +} |
0 commit comments