|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 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 android.util.Log |
| 20 | +import java.io.IOException |
| 21 | +import java.io.InputStream |
| 22 | +import java.io.OutputStream |
| 23 | +import java.util.concurrent.Executors |
| 24 | +import kotlin.time.Duration.Companion.milliseconds |
| 25 | +import kotlinx.coroutines.CoroutineScope |
| 26 | +import kotlinx.coroutines.asCoroutineDispatcher |
| 27 | +import kotlinx.coroutines.launch |
| 28 | +import kotlinx.coroutines.runBlocking |
| 29 | +import kotlinx.coroutines.runInterruptible |
| 30 | + |
| 31 | +/** Variant of ShellMain that uses a LocalSocket to communicate with the client. */ |
| 32 | +class LocalSocketShellMain { |
| 33 | + |
| 34 | + suspend fun run(args: Array<String>): Int { |
| 35 | + val scope = CoroutineScope(Executors.newCachedThreadPool().asCoroutineDispatcher()) |
| 36 | + val server = ShellCommandLocalSocketExecutorServer(scope = scope) |
| 37 | + server.start() |
| 38 | + |
| 39 | + val processArgs = args.toMutableList() |
| 40 | + processArgs.addAll( |
| 41 | + processArgs.size - 1, |
| 42 | + listOf("-e", ShellExecSharedConstants.BINDER_KEY, server.binderKey()), |
| 43 | + ) |
| 44 | + val pb = ProcessBuilder(processArgs.toList()) |
| 45 | + |
| 46 | + val exitCode: Int |
| 47 | + |
| 48 | + try { |
| 49 | + val process = pb.start() |
| 50 | + |
| 51 | + val stdinCopier = scope.launch { copyStream("stdin", System.`in`, process.outputStream) } |
| 52 | + val stdoutCopier = scope.launch { copyStream("stdout", process.inputStream, System.out) } |
| 53 | + val stderrCopier = scope.launch { copyStream("stderr", process.errorStream, System.err) } |
| 54 | + |
| 55 | + runInterruptible { process.waitFor() } |
| 56 | + exitCode = process.exitValue() |
| 57 | + |
| 58 | + stdinCopier.cancel() // System.`in`.close() does not force input.read() to return |
| 59 | + stdoutCopier.join() |
| 60 | + stderrCopier.join() |
| 61 | + } finally { |
| 62 | + server.stop(100.milliseconds) |
| 63 | + } |
| 64 | + return exitCode |
| 65 | + } |
| 66 | + |
| 67 | + suspend fun copyStream(name: String, input: InputStream, output: OutputStream) { |
| 68 | + val buf = ByteArray(1024) |
| 69 | + try { |
| 70 | + while (true) { |
| 71 | + val size = input.read(buf) |
| 72 | + if (size == -1) break |
| 73 | + output.write(buf, 0, size) |
| 74 | + } |
| 75 | + output.flush() |
| 76 | + } catch (x: IOException) { |
| 77 | + Log.e(TAG, "IOException on $name. Terminating.", x) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + companion object { |
| 82 | + private const val TAG = "LocalSocketShellMain" |
| 83 | + |
| 84 | + @JvmStatic |
| 85 | + public fun main(args: Array<String>) { |
| 86 | + System.exit(runBlocking { LocalSocketShellMain().run(args) }) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments