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