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