San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 com.android.server; |
| 18 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 19 | import android.net.LocalSocket; |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 20 | import android.net.LocalSocketAddress; |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 21 | import android.os.Build; |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 23 | import android.os.Looper; |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 24 | import android.os.Message; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 25 | import android.os.PowerManager; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 26 | import android.os.SystemClock; |
Tetsutoki Shiozawa | e83724e | 2017-07-13 15:37:40 +0900 | [diff] [blame] | 27 | import android.os.SystemProperties; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 28 | import android.util.LocalLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 29 | import android.util.Slog; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 30 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 31 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 8948c01 | 2015-11-03 12:33:54 -0800 | [diff] [blame] | 32 | import com.android.internal.util.Preconditions; |
Tetsutoki Shiozawa | e83724e | 2017-07-13 15:37:40 +0900 | [diff] [blame] | 33 | import com.android.server.power.ShutdownThread; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 34 | import com.google.android.collect.Lists; |
| 35 | |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 36 | import java.io.FileDescriptor; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.io.OutputStream; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 40 | import java.io.PrintWriter; |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 41 | import java.nio.charset.StandardCharsets; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 42 | import java.util.ArrayList; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 43 | import java.util.concurrent.atomic.AtomicInteger; |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 44 | import java.util.concurrent.ArrayBlockingQueue; |
| 45 | import java.util.concurrent.BlockingQueue; |
Rebecca Silberstein | efdb845 | 2016-04-21 12:14:41 -0700 | [diff] [blame] | 46 | import java.util.concurrent.CountDownLatch; |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 47 | import java.util.concurrent.TimeUnit; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 48 | import java.util.LinkedList; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 51 | * Generic connector class for interfacing with a native daemon which uses the |
| 52 | * {@code libsysutils} FrameworkListener protocol. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 53 | */ |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 54 | final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdog.Monitor { |
Robert Greenwalt | 7f44ff8 | 2014-05-07 23:49:08 -0700 | [diff] [blame] | 55 | private final static boolean VDBG = false; |
| 56 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 57 | private final String TAG; |
| 58 | |
| 59 | private String mSocket; |
| 60 | private OutputStream mOutputStream; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 61 | private LocalLog mLocalLog; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 62 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 63 | private volatile boolean mDebug = false; |
Jeff Sharkey | 8948c01 | 2015-11-03 12:33:54 -0800 | [diff] [blame] | 64 | private volatile Object mWarnIfHeld; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 65 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 66 | private final ResponseQueue mResponseQueue; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 67 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 68 | private final PowerManager.WakeLock mWakeLock; |
| 69 | |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 70 | private final Looper mLooper; |
| 71 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 72 | private INativeDaemonConnectorCallbacks mCallbacks; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 73 | private Handler mCallbackHandler; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 74 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 75 | private AtomicInteger mSequenceNumber; |
| 76 | |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 77 | private static final long DEFAULT_TIMEOUT = 1 * 60 * 1000; /* 1 minute */ |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 78 | private static final long WARN_EXECUTE_DELAY_MS = 500; /* .5 sec */ |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 79 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 80 | /** Lock held whenever communicating with native daemon. */ |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 81 | private final Object mDaemonLock = new Object(); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 82 | |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 83 | private final int BUFFER_SIZE = 4096; |
| 84 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 85 | NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket, |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 86 | int responseQueueSize, String logTag, int maxLogSize, PowerManager.WakeLock wl) { |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 87 | this(callbacks, socket, responseQueueSize, logTag, maxLogSize, wl, |
| 88 | FgThread.get().getLooper()); |
| 89 | } |
| 90 | |
| 91 | NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket, |
| 92 | int responseQueueSize, String logTag, int maxLogSize, PowerManager.WakeLock wl, |
| 93 | Looper looper) { |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 94 | mCallbacks = callbacks; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 95 | mSocket = socket; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 96 | mResponseQueue = new ResponseQueue(responseQueueSize); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 97 | mWakeLock = wl; |
| 98 | if (mWakeLock != null) { |
| 99 | mWakeLock.setReferenceCounted(true); |
| 100 | } |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 101 | mLooper = looper; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 102 | mSequenceNumber = new AtomicInteger(0); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 103 | TAG = logTag != null ? logTag : "NativeDaemonConnector"; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 104 | mLocalLog = new LocalLog(maxLogSize); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Enable Set debugging mode, which causes messages to also be written to both |
| 109 | * {@link Slog} in addition to internal log. |
| 110 | */ |
| 111 | public void setDebug(boolean debug) { |
| 112 | mDebug = debug; |
| 113 | } |
| 114 | |
Jeff Sharkey | e41dc59 | 2015-11-03 10:39:42 -0800 | [diff] [blame] | 115 | /** |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 116 | * Like SystemClock.uptimeMillis, except truncated to an int so it will fit in a message arg. |
| 117 | * Inaccurate across 49.7 days of uptime, but only used for debugging. |
| 118 | */ |
| 119 | private int uptimeMillisInt() { |
| 120 | return (int) SystemClock.uptimeMillis() & Integer.MAX_VALUE; |
| 121 | } |
| 122 | |
| 123 | /** |
Jeff Sharkey | 8948c01 | 2015-11-03 12:33:54 -0800 | [diff] [blame] | 124 | * Yell loudly if someone tries making future {@link #execute(Command)} |
| 125 | * calls while holding a lock on the given object. |
Jeff Sharkey | e41dc59 | 2015-11-03 10:39:42 -0800 | [diff] [blame] | 126 | */ |
Jeff Sharkey | 8948c01 | 2015-11-03 12:33:54 -0800 | [diff] [blame] | 127 | public void setWarnIfHeld(Object warnIfHeld) { |
| 128 | Preconditions.checkState(mWarnIfHeld == null); |
| 129 | mWarnIfHeld = Preconditions.checkNotNull(warnIfHeld); |
Jeff Sharkey | e41dc59 | 2015-11-03 10:39:42 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 132 | @Override |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 133 | public void run() { |
Dianne Hackborn | 2ffa11e | 2014-04-21 15:56:18 -0700 | [diff] [blame] | 134 | mCallbackHandler = new Handler(mLooper, this); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 135 | |
| 136 | while (true) { |
Tetsutoki Shiozawa | be12b81 | 2017-10-27 14:03:00 +0900 | [diff] [blame] | 137 | if (isShuttingDown()) break; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 138 | try { |
| 139 | listenToSocket(); |
| 140 | } catch (Exception e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 141 | loge("Error in NativeDaemonConnector: " + e); |
Tetsutoki Shiozawa | be12b81 | 2017-10-27 14:03:00 +0900 | [diff] [blame] | 142 | if (isShuttingDown()) break; |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 143 | SystemClock.sleep(5000); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Tetsutoki Shiozawa | be12b81 | 2017-10-27 14:03:00 +0900 | [diff] [blame] | 148 | private static boolean isShuttingDown() { |
| 149 | String shutdownAct = SystemProperties.get( |
| 150 | ShutdownThread.SHUTDOWN_ACTION_PROPERTY, ""); |
| 151 | return shutdownAct != null && shutdownAct.length() > 0; |
| 152 | } |
| 153 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public boolean handleMessage(Message msg) { |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 156 | final String event = (String) msg.obj; |
| 157 | final int start = uptimeMillisInt(); |
| 158 | final int sent = msg.arg1; |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 159 | try { |
Robert Greenwalt | 2d34b4a | 2012-04-20 13:08:02 -0700 | [diff] [blame] | 160 | if (!mCallbacks.onEvent(msg.what, event, NativeDaemonEvent.unescapeArgs(event))) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 161 | log(String.format("Unhandled event '%s'", event)); |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 162 | } |
| 163 | } catch (Exception e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 164 | loge("Error handling '" + event + "': " + e); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 165 | } finally { |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 166 | if (mCallbacks.onCheckHoldWakeLock(msg.what) && mWakeLock != null) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 167 | mWakeLock.release(); |
| 168 | } |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 169 | final int end = uptimeMillisInt(); |
| 170 | if (start > sent && start - sent > WARN_EXECUTE_DELAY_MS) { |
| 171 | loge(String.format("NDC event {%s} processed too late: %dms", event, start - sent)); |
| 172 | } |
| 173 | if (end > start && end - start > WARN_EXECUTE_DELAY_MS) { |
| 174 | loge(String.format("NDC event {%s} took too long: %dms", event, end - start)); |
| 175 | } |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 176 | } |
| 177 | return true; |
| 178 | } |
| 179 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 180 | private LocalSocketAddress determineSocketAddress() { |
| 181 | // If we're testing, set up a socket in a namespace that's accessible to test code. |
| 182 | // In order to ensure that unprivileged apps aren't able to impersonate native daemons on |
| 183 | // production devices, even if said native daemons ill-advisedly pick a socket name that |
| 184 | // starts with __test__, only allow this on debug builds. |
| 185 | if (mSocket.startsWith("__test__") && Build.IS_DEBUGGABLE) { |
| 186 | return new LocalSocketAddress(mSocket); |
| 187 | } else { |
| 188 | return new LocalSocketAddress(mSocket, LocalSocketAddress.Namespace.RESERVED); |
| 189 | } |
| 190 | } |
| 191 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 192 | private void listenToSocket() throws IOException { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 193 | LocalSocket socket = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 194 | |
| 195 | try { |
| 196 | socket = new LocalSocket(); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 197 | LocalSocketAddress address = determineSocketAddress(); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 198 | |
| 199 | socket.connect(address); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 200 | |
| 201 | InputStream inputStream = socket.getInputStream(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 202 | synchronized (mDaemonLock) { |
| 203 | mOutputStream = socket.getOutputStream(); |
| 204 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 205 | |
anga | 030bc88 | 2011-02-01 14:10:25 +0100 | [diff] [blame] | 206 | mCallbacks.onDaemonConnected(); |
| 207 | |
Daichi Hirono | dda6552 | 2015-11-19 16:58:57 +0900 | [diff] [blame] | 208 | FileDescriptor[] fdList = null; |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 209 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 210 | int start = 0; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 211 | |
| 212 | while (true) { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 213 | int count = inputStream.read(buffer, start, BUFFER_SIZE - start); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 214 | if (count < 0) { |
| 215 | loge("got " + count + " reading with start = " + start); |
| 216 | break; |
| 217 | } |
Daichi Hirono | dda6552 | 2015-11-19 16:58:57 +0900 | [diff] [blame] | 218 | fdList = socket.getAncillaryFileDescriptors(); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 219 | |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 220 | // Add our starting point to the count and reset the start. |
| 221 | count += start; |
| 222 | start = 0; |
| 223 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 224 | for (int i = 0; i < count; i++) { |
| 225 | if (buffer[i] == 0) { |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 226 | // Note - do not log this raw message since it may contain |
| 227 | // sensitive data |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 228 | final String rawEvent = new String( |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 229 | buffer, start, i - start, StandardCharsets.UTF_8); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 230 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 231 | boolean releaseWl = false; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 232 | try { |
Daichi Hirono | dda6552 | 2015-11-19 16:58:57 +0900 | [diff] [blame] | 233 | final NativeDaemonEvent event = |
| 234 | NativeDaemonEvent.parseRawEvent(rawEvent, fdList); |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 235 | |
| 236 | log("RCV <- {" + event + "}"); |
| 237 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 238 | if (event.isClassUnsolicited()) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 239 | // TODO: migrate to sending NativeDaemonEvent instances |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 240 | if (mCallbacks.onCheckHoldWakeLock(event.getCode()) |
| 241 | && mWakeLock != null) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 242 | mWakeLock.acquire(); |
| 243 | releaseWl = true; |
| 244 | } |
Lorenzo Colitti | cd63d24 | 2016-04-10 15:39:53 +0900 | [diff] [blame] | 245 | Message msg = mCallbackHandler.obtainMessage( |
| 246 | event.getCode(), uptimeMillisInt(), 0, event.getRawEvent()); |
| 247 | if (mCallbackHandler.sendMessage(msg)) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 248 | releaseWl = false; |
| 249 | } |
Irfan Sheriff | 1cd94ef | 2011-01-16 14:31:55 -0800 | [diff] [blame] | 250 | } else { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 251 | mResponseQueue.add(event.getCmdNumber(), event); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 252 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 253 | } catch (IllegalArgumentException e) { |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 254 | log("Problem parsing message " + e); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 255 | } finally { |
| 256 | if (releaseWl) { |
Lorenzo Colitti | ae107af | 2016-04-11 17:08:10 +0900 | [diff] [blame] | 257 | mWakeLock.release(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 258 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 259 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 260 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 261 | start = i + 1; |
| 262 | } |
| 263 | } |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 264 | |
Robert Greenwalt | f0be1d89 | 2012-01-20 16:33:15 -0800 | [diff] [blame] | 265 | if (start == 0) { |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 266 | log("RCV incomplete"); |
Robert Greenwalt | f0be1d89 | 2012-01-20 16:33:15 -0800 | [diff] [blame] | 267 | } |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 268 | |
| 269 | // We should end at the amount we read. If not, compact then |
| 270 | // buffer and read again. |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 271 | if (start != count) { |
| 272 | final int remaining = BUFFER_SIZE - start; |
| 273 | System.arraycopy(buffer, start, buffer, 0, remaining); |
| 274 | start = remaining; |
| 275 | } else { |
| 276 | start = 0; |
| 277 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 278 | } |
| 279 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 280 | loge("Communications error: " + ex); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 281 | throw ex; |
| 282 | } finally { |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 283 | synchronized (mDaemonLock) { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 284 | if (mOutputStream != null) { |
| 285 | try { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 286 | loge("closing stream for " + mSocket); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 287 | mOutputStream.close(); |
| 288 | } catch (IOException e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 289 | loge("Failed closing output stream: " + e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 290 | } |
| 291 | mOutputStream = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 292 | } |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 293 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 294 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 295 | try { |
| 296 | if (socket != null) { |
| 297 | socket.close(); |
| 298 | } |
| 299 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 300 | loge("Failed closing socket: " + ex); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 301 | } |
| 302 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 303 | } |
| 304 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 305 | /** |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 306 | * Wrapper around argument that indicates it's sensitive and shouldn't be |
| 307 | * logged. |
| 308 | */ |
| 309 | public static class SensitiveArg { |
| 310 | private final Object mArg; |
| 311 | |
| 312 | public SensitiveArg(Object arg) { |
| 313 | mArg = arg; |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public String toString() { |
| 318 | return String.valueOf(mArg); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 323 | * Make command for daemon, escaping arguments as needed. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 324 | */ |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 325 | @VisibleForTesting |
| 326 | static void makeCommand(StringBuilder rawBuilder, StringBuilder logBuilder, int sequenceNumber, |
| 327 | String cmd, Object... args) { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 328 | if (cmd.indexOf('\0') >= 0) { |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 329 | throw new IllegalArgumentException("Unexpected command: " + cmd); |
| 330 | } |
| 331 | if (cmd.indexOf(' ') >= 0) { |
| 332 | throw new IllegalArgumentException("Arguments must be separate from command"); |
Jeff Sharkey | b0aec07 | 2011-10-14 18:32:24 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 335 | rawBuilder.append(sequenceNumber).append(' ').append(cmd); |
| 336 | logBuilder.append(sequenceNumber).append(' ').append(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 337 | for (Object arg : args) { |
| 338 | final String argString = String.valueOf(arg); |
| 339 | if (argString.indexOf('\0') >= 0) { |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 340 | throw new IllegalArgumentException("Unexpected argument: " + arg); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 341 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 342 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 343 | rawBuilder.append(' '); |
| 344 | logBuilder.append(' '); |
| 345 | |
| 346 | appendEscaped(rawBuilder, argString); |
| 347 | if (arg instanceof SensitiveArg) { |
| 348 | logBuilder.append("[scrubbed]"); |
| 349 | } else { |
| 350 | appendEscaped(logBuilder, argString); |
| 351 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 352 | } |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 353 | |
| 354 | rawBuilder.append('\0'); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 355 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 356 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 357 | /** |
Rebecca Silberstein | efdb845 | 2016-04-21 12:14:41 -0700 | [diff] [blame] | 358 | * Method that waits until all asychronous notifications sent by the native daemon have |
| 359 | * been processed. This method must not be called on the notification thread or an |
| 360 | * exception will be thrown. |
| 361 | */ |
| 362 | public void waitForCallbacks() { |
| 363 | if (Thread.currentThread() == mLooper.getThread()) { |
| 364 | throw new IllegalStateException("Must not call this method on callback thread"); |
| 365 | } |
| 366 | |
| 367 | final CountDownLatch latch = new CountDownLatch(1); |
| 368 | mCallbackHandler.post(new Runnable() { |
| 369 | @Override |
| 370 | public void run() { |
| 371 | latch.countDown(); |
| 372 | } |
| 373 | }); |
| 374 | try { |
| 375 | latch.await(); |
| 376 | } catch (InterruptedException e) { |
| 377 | Slog.wtf(TAG, "Interrupted while waiting for unsolicited response handling", e); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 382 | * Issue the given command to the native daemon and return a single expected |
| 383 | * response. |
| 384 | * |
| 385 | * @throws NativeDaemonConnectorException when problem communicating with |
| 386 | * native daemon, or if the response matches |
| 387 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 388 | * {@link NativeDaemonEvent#isClassServerError()}. |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 389 | */ |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 390 | public NativeDaemonEvent execute(Command cmd) throws NativeDaemonConnectorException { |
| 391 | return execute(cmd.mCmd, cmd.mArguments.toArray()); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Issue the given command to the native daemon and return a single expected |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 396 | * response. Any arguments must be separated from base command so they can |
| 397 | * be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 398 | * |
| 399 | * @throws NativeDaemonConnectorException when problem communicating with |
| 400 | * native daemon, or if the response matches |
| 401 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 402 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 403 | */ |
| 404 | public NativeDaemonEvent execute(String cmd, Object... args) |
| 405 | throws NativeDaemonConnectorException { |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 406 | return execute(DEFAULT_TIMEOUT, cmd, args); |
| 407 | } |
| 408 | |
| 409 | public NativeDaemonEvent execute(long timeoutMs, String cmd, Object... args) |
| 410 | throws NativeDaemonConnectorException { |
| 411 | final NativeDaemonEvent[] events = executeForList(timeoutMs, cmd, args); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 412 | if (events.length != 1) { |
| 413 | throw new NativeDaemonConnectorException( |
| 414 | "Expected exactly one response, but received " + events.length); |
| 415 | } |
| 416 | return events[0]; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Issue the given command to the native daemon and return any |
| 421 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
| 422 | * final terminal response. |
| 423 | * |
| 424 | * @throws NativeDaemonConnectorException when problem communicating with |
| 425 | * native daemon, or if the response matches |
| 426 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 427 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 428 | */ |
| 429 | public NativeDaemonEvent[] executeForList(Command cmd) throws NativeDaemonConnectorException { |
| 430 | return executeForList(cmd.mCmd, cmd.mArguments.toArray()); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Issue the given command to the native daemon and return any |
| 435 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 436 | * final terminal response. Any arguments must be separated from base |
| 437 | * command so they can be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 438 | * |
| 439 | * @throws NativeDaemonConnectorException when problem communicating with |
| 440 | * native daemon, or if the response matches |
| 441 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 442 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 443 | */ |
| 444 | public NativeDaemonEvent[] executeForList(String cmd, Object... args) |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 445 | throws NativeDaemonConnectorException { |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 446 | return executeForList(DEFAULT_TIMEOUT, cmd, args); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 447 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 448 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 449 | /** |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 450 | * Issue the given command to the native daemon and return any {@linke |
| 451 | * NativeDaemonEvent@isClassContinue()} responses, including the final |
| 452 | * terminal response. Note that the timeout does not count time in deep |
| 453 | * sleep. Any arguments must be separated from base command so they can be |
| 454 | * properly escaped. |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 455 | * |
| 456 | * @throws NativeDaemonConnectorException when problem communicating with |
| 457 | * native daemon, or if the response matches |
| 458 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 459 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 460 | */ |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 461 | public NativeDaemonEvent[] executeForList(long timeoutMs, String cmd, Object... args) |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 462 | throws NativeDaemonConnectorException { |
Jeff Sharkey | 8948c01 | 2015-11-03 12:33:54 -0800 | [diff] [blame] | 463 | if (mWarnIfHeld != null && Thread.holdsLock(mWarnIfHeld)) { |
| 464 | Slog.wtf(TAG, "Calling thread " + Thread.currentThread().getName() + " is holding 0x" |
| 465 | + Integer.toHexString(System.identityHashCode(mWarnIfHeld)), new Throwable()); |
Jeff Sharkey | e41dc59 | 2015-11-03 10:39:42 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 468 | final long startTime = SystemClock.elapsedRealtime(); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 469 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 470 | final ArrayList<NativeDaemonEvent> events = Lists.newArrayList(); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 471 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 472 | final StringBuilder rawBuilder = new StringBuilder(); |
| 473 | final StringBuilder logBuilder = new StringBuilder(); |
| 474 | final int sequenceNumber = mSequenceNumber.incrementAndGet(); |
| 475 | |
| 476 | makeCommand(rawBuilder, logBuilder, sequenceNumber, cmd, args); |
| 477 | |
| 478 | final String rawCmd = rawBuilder.toString(); |
| 479 | final String logCmd = logBuilder.toString(); |
| 480 | |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 481 | log("SND -> {" + logCmd + "}"); |
| 482 | |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 483 | synchronized (mDaemonLock) { |
| 484 | if (mOutputStream == null) { |
| 485 | throw new NativeDaemonConnectorException("missing output stream"); |
| 486 | } else { |
| 487 | try { |
Elliott Hughes | b829283 | 2013-06-28 16:50:13 -0700 | [diff] [blame] | 488 | mOutputStream.write(rawCmd.getBytes(StandardCharsets.UTF_8)); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 489 | } catch (IOException e) { |
| 490 | throw new NativeDaemonConnectorException("problem sending command", e); |
| 491 | } |
| 492 | } |
| 493 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 494 | |
| 495 | NativeDaemonEvent event = null; |
| 496 | do { |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 497 | event = mResponseQueue.remove(sequenceNumber, timeoutMs, logCmd); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 498 | if (event == null) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 499 | loge("timed-out waiting for response to " + logCmd); |
Todd Kennedy | 8101ee6 | 2015-06-23 13:35:28 -0700 | [diff] [blame] | 500 | throw new NativeDaemonTimeoutException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 501 | } |
Robert Greenwalt | 7f44ff8 | 2014-05-07 23:49:08 -0700 | [diff] [blame] | 502 | if (VDBG) log("RMV <- {" + event + "}"); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 503 | events.add(event); |
| 504 | } while (event.isClassContinue()); |
| 505 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 506 | final long endTime = SystemClock.elapsedRealtime(); |
| 507 | if (endTime - startTime > WARN_EXECUTE_DELAY_MS) { |
| 508 | loge("NDC Command {" + logCmd + "} took too long (" + (endTime - startTime) + "ms)"); |
| 509 | } |
| 510 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 511 | if (event.isClassClientError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 512 | throw new NativeDaemonArgumentException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 513 | } |
| 514 | if (event.isClassServerError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 515 | throw new NativeDaemonFailureException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | return events.toArray(new NativeDaemonEvent[events.size()]); |
| 519 | } |
| 520 | |
| 521 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 522 | * Append the given argument to {@link StringBuilder}, escaping as needed, |
| 523 | * and surrounding with quotes when it contains spaces. |
| 524 | */ |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 525 | @VisibleForTesting |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 526 | static void appendEscaped(StringBuilder builder, String arg) { |
| 527 | final boolean hasSpaces = arg.indexOf(' ') >= 0; |
| 528 | if (hasSpaces) { |
| 529 | builder.append('"'); |
| 530 | } |
| 531 | |
| 532 | final int length = arg.length(); |
| 533 | for (int i = 0; i < length; i++) { |
| 534 | final char c = arg.charAt(i); |
| 535 | |
| 536 | if (c == '"') { |
| 537 | builder.append("\\\""); |
| 538 | } else if (c == '\\') { |
| 539 | builder.append("\\\\"); |
| 540 | } else { |
| 541 | builder.append(c); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | if (hasSpaces) { |
| 546 | builder.append('"'); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | private static class NativeDaemonArgumentException extends NativeDaemonConnectorException { |
| 551 | public NativeDaemonArgumentException(String command, NativeDaemonEvent event) { |
| 552 | super(command, event); |
| 553 | } |
| 554 | |
| 555 | @Override |
| 556 | public IllegalArgumentException rethrowAsParcelableException() { |
| 557 | throw new IllegalArgumentException(getMessage(), this); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | private static class NativeDaemonFailureException extends NativeDaemonConnectorException { |
| 562 | public NativeDaemonFailureException(String command, NativeDaemonEvent event) { |
| 563 | super(command, event); |
| 564 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 565 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 566 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 567 | /** |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 568 | * Command builder that handles argument list building. Any arguments must |
| 569 | * be separated from base command so they can be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 570 | */ |
| 571 | public static class Command { |
| 572 | private String mCmd; |
| 573 | private ArrayList<Object> mArguments = Lists.newArrayList(); |
| 574 | |
| 575 | public Command(String cmd, Object... args) { |
| 576 | mCmd = cmd; |
| 577 | for (Object arg : args) { |
| 578 | appendArg(arg); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | public Command appendArg(Object arg) { |
| 583 | mArguments.add(arg); |
| 584 | return this; |
| 585 | } |
| 586 | } |
| 587 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 588 | /** {@inheritDoc} */ |
| 589 | public void monitor() { |
| 590 | synchronized (mDaemonLock) { } |
| 591 | } |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 592 | |
| 593 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 594 | mLocalLog.dump(fd, pw, args); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 595 | pw.println(); |
| 596 | mResponseQueue.dump(fd, pw, args); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | private void log(String logstring) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 600 | if (mDebug) Slog.d(TAG, logstring); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 601 | mLocalLog.log(logstring); |
| 602 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 603 | |
| 604 | private void loge(String logstring) { |
| 605 | Slog.e(TAG, logstring); |
| 606 | mLocalLog.log(logstring); |
| 607 | } |
| 608 | |
| 609 | private static class ResponseQueue { |
| 610 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 611 | private static class PendingCmd { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 612 | public final int cmdNum; |
| 613 | public final String logCmd; |
| 614 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 615 | public BlockingQueue<NativeDaemonEvent> responses = |
| 616 | new ArrayBlockingQueue<NativeDaemonEvent>(10); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 617 | |
| 618 | // The availableResponseCount member is used to track when we can remove this |
| 619 | // instance from the ResponseQueue. |
| 620 | // This is used under the protection of a sync of the mPendingCmds object. |
| 621 | // A positive value means we've had more writers retreive this object while |
| 622 | // a negative value means we've had more readers. When we've had an equal number |
| 623 | // (it goes to zero) we can remove this object from the mPendingCmds list. |
| 624 | // Note that we may have more responses for this command (and more readers |
| 625 | // coming), but that would result in a new PendingCmd instance being created |
| 626 | // and added with the same cmdNum. |
| 627 | // Also note that when this goes to zero it just means a parity of readers and |
| 628 | // writers have retrieved this object - not that they are done using it. The |
| 629 | // responses queue may well have more responses yet to be read or may get more |
| 630 | // responses added to it. But all those readers/writers have retreived and |
| 631 | // hold references to this instance already so it can be removed from |
| 632 | // mPendingCmds queue. |
| 633 | public int availableResponseCount; |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 634 | |
| 635 | public PendingCmd(int cmdNum, String logCmd) { |
| 636 | this.cmdNum = cmdNum; |
| 637 | this.logCmd = logCmd; |
| 638 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 641 | private final LinkedList<PendingCmd> mPendingCmds; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 642 | private int mMaxCount; |
| 643 | |
| 644 | ResponseQueue(int maxCount) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 645 | mPendingCmds = new LinkedList<PendingCmd>(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 646 | mMaxCount = maxCount; |
| 647 | } |
| 648 | |
| 649 | public void add(int cmdNum, NativeDaemonEvent response) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 650 | PendingCmd found = null; |
| 651 | synchronized (mPendingCmds) { |
| 652 | for (PendingCmd pendingCmd : mPendingCmds) { |
| 653 | if (pendingCmd.cmdNum == cmdNum) { |
| 654 | found = pendingCmd; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | } |
| 658 | if (found == null) { |
| 659 | // didn't find it - make sure our queue isn't too big before adding |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 660 | while (mPendingCmds.size() >= mMaxCount) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 661 | Slog.e("NativeDaemonConnector.ResponseQueue", |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 662 | "more buffered than allowed: " + mPendingCmds.size() + |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 663 | " >= " + mMaxCount); |
| 664 | // let any waiter timeout waiting for this |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 665 | PendingCmd pendingCmd = mPendingCmds.remove(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 666 | Slog.e("NativeDaemonConnector.ResponseQueue", |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 667 | "Removing request: " + pendingCmd.logCmd + " (" + |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 668 | pendingCmd.cmdNum + ")"); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 669 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 670 | found = new PendingCmd(cmdNum, null); |
| 671 | mPendingCmds.add(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 672 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 673 | found.availableResponseCount++; |
| 674 | // if a matching remove call has already retrieved this we can remove this |
| 675 | // instance from our list |
| 676 | if (found.availableResponseCount == 0) mPendingCmds.remove(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 677 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 678 | try { |
| 679 | found.responses.put(response); |
| 680 | } catch (InterruptedException e) { } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | // note that the timeout does not count time in deep sleep. If you don't want |
| 684 | // the device to sleep, hold a wakelock |
Jeff Sharkey | 14cbe52 | 2015-07-08 14:06:37 -0700 | [diff] [blame] | 685 | public NativeDaemonEvent remove(int cmdNum, long timeoutMs, String logCmd) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 686 | PendingCmd found = null; |
| 687 | synchronized (mPendingCmds) { |
| 688 | for (PendingCmd pendingCmd : mPendingCmds) { |
| 689 | if (pendingCmd.cmdNum == cmdNum) { |
| 690 | found = pendingCmd; |
| 691 | break; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 692 | } |
| 693 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 694 | if (found == null) { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 695 | found = new PendingCmd(cmdNum, logCmd); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 696 | mPendingCmds.add(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 697 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 698 | found.availableResponseCount--; |
| 699 | // if a matching add call has already retrieved this we can remove this |
| 700 | // instance from our list |
| 701 | if (found.availableResponseCount == 0) mPendingCmds.remove(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 702 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 703 | NativeDaemonEvent result = null; |
| 704 | try { |
| 705 | result = found.responses.poll(timeoutMs, TimeUnit.MILLISECONDS); |
| 706 | } catch (InterruptedException e) {} |
| 707 | if (result == null) { |
| 708 | Slog.e("NativeDaemonConnector.ResponseQueue", "Timeout waiting for response"); |
| 709 | } |
| 710 | return result; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 714 | pw.println("Pending requests:"); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 715 | synchronized (mPendingCmds) { |
| 716 | for (PendingCmd pendingCmd : mPendingCmds) { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 717 | pw.println(" Cmd " + pendingCmd.cmdNum + " - " + pendingCmd.logCmd); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | } |
| 721 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 722 | } |