blob: ef5e9cc1f614cbd9b661c69961b4dc8532d2367b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080019import com.android.server.am.ActivityManagerService;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.pm.PackageManager;
Kenny Root02c87302010-07-01 08:10:18 -070026import android.content.res.ObbInfo;
27import android.content.res.ObbScanner;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.net.Uri;
San Mehatb1043402010-02-05 08:26:50 -080029import android.os.storage.IMountService;
30import android.os.storage.IMountServiceListener;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080031import android.os.storage.IMountShutdownObserver;
San Mehatb1043402010-02-05 08:26:50 -080032import android.os.storage.StorageResultCode;
Kenny Root02c87302010-07-01 08:10:18 -070033import android.os.Binder;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080034import android.os.Handler;
Daniel Sandler5f27ef42010-03-16 15:42:02 -040035import android.os.HandlerThread;
36import android.os.Looper;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080037import android.os.Message;
San Mehat4270e1e2010-01-29 05:32:19 -080038import android.os.RemoteException;
39import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.Environment;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080041import android.os.ServiceManager;
San Mehat207e5382010-02-04 20:46:54 -080042import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.SystemProperties;
San Mehata5078592010-03-25 09:36:54 -070044import android.util.Slog;
San Mehat22dd86e2010-01-12 12:21:18 -080045import java.util.ArrayList;
San Mehat6cdd9c02010-02-09 14:45:20 -080046import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048/**
San Mehatb1043402010-02-05 08:26:50 -080049 * MountService implements back-end services for platform storage
50 * management.
51 * @hide - Applications should use android.os.storage.StorageManager
52 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 */
San Mehat22dd86e2010-01-12 12:21:18 -080054class MountService extends IMountService.Stub
55 implements INativeDaemonConnectorCallbacks {
San Mehatb1043402010-02-05 08:26:50 -080056 private static final boolean LOCAL_LOGD = false;
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -080057 private static final boolean DEBUG_UNMOUNT = false;
58 private static final boolean DEBUG_EVENTS = false;
Kenny Root02c87302010-07-01 08:10:18 -070059 private static final boolean DEBUG_OBB = true;
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 private static final String TAG = "MountService";
62
San Mehat4270e1e2010-01-29 05:32:19 -080063 /*
64 * Internal vold volume state constants
65 */
San Mehat7fd0fee2009-12-17 07:12:23 -080066 class VolumeState {
67 public static final int Init = -1;
68 public static final int NoMedia = 0;
69 public static final int Idle = 1;
70 public static final int Pending = 2;
71 public static final int Checking = 3;
72 public static final int Mounted = 4;
73 public static final int Unmounting = 5;
74 public static final int Formatting = 6;
75 public static final int Shared = 7;
76 public static final int SharedMnt = 8;
77 }
78
San Mehat4270e1e2010-01-29 05:32:19 -080079 /*
80 * Internal vold response code constants
81 */
San Mehat22dd86e2010-01-12 12:21:18 -080082 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -080083 /*
84 * 100 series - Requestion action was initiated; expect another reply
85 * before proceeding with a new command.
86 */
San Mehat22dd86e2010-01-12 12:21:18 -080087 public static final int VolumeListResult = 110;
88 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -080089 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -080090
San Mehat4270e1e2010-01-29 05:32:19 -080091 /*
92 * 200 series - Requestion action has been successfully completed.
93 */
94 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -080095 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -080096 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -080097
San Mehat4270e1e2010-01-29 05:32:19 -080098 /*
99 * 400 series - Command was accepted, but the requested action
100 * did not take place.
101 */
102 public static final int OpFailedNoMedia = 401;
103 public static final int OpFailedMediaBlank = 402;
104 public static final int OpFailedMediaCorrupt = 403;
105 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -0800106 public static final int OpFailedStorageBusy = 405;
San Mehat2d66cef2010-03-23 11:12:52 -0700107 public static final int OpFailedStorageNotFound = 406;
San Mehat4270e1e2010-01-29 05:32:19 -0800108
109 /*
110 * 600 series - Unsolicited broadcasts.
111 */
San Mehat22dd86e2010-01-12 12:21:18 -0800112 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800113 public static final int ShareAvailabilityChange = 620;
114 public static final int VolumeDiskInserted = 630;
115 public static final int VolumeDiskRemoved = 631;
116 public static final int VolumeBadRemoval = 632;
117 }
118
San Mehat4270e1e2010-01-29 05:32:19 -0800119 private Context mContext;
120 private NativeDaemonConnector mConnector;
121 private String mLegacyState = Environment.MEDIA_REMOVED;
122 private PackageManagerService mPms;
123 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800124 // Used as a lock for methods that register/unregister listeners.
125 final private ArrayList<MountServiceBinderListener> mListeners =
126 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800127 private boolean mBooted = false;
128 private boolean mReady = false;
129 private boolean mSendUmsConnectedOnBoot = false;
Mike Lockwood03559752010-07-19 18:25:03 -0400130 // true if we should fake MEDIA_MOUNTED state for external storage
131 private boolean mEmulateExternalStorage = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800132
San Mehat6cdd9c02010-02-09 14:45:20 -0800133 /**
134 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800135 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800136 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800137 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800138
Kenny Root02c87302010-07-01 08:10:18 -0700139 /**
140 * Private hash of currently mounted filesystem images.
141 */
142 final private HashSet<String> mObbMountSet = new HashSet<String>();
143
144 // Handler messages
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800145 private static final int H_UNMOUNT_PM_UPDATE = 1;
146 private static final int H_UNMOUNT_PM_DONE = 2;
147 private static final int H_UNMOUNT_MS = 3;
148 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
149 private static final int MAX_UNMOUNT_RETRIES = 4;
150
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800151 class UnmountCallBack {
152 String path;
153 int retries;
154 boolean force;
155
156 UnmountCallBack(String path, boolean force) {
157 retries = 0;
158 this.path = path;
159 this.force = force;
160 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800161
162 void handleFinished() {
San Mehata5078592010-03-25 09:36:54 -0700163 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800164 doUnmountVolume(path, true);
165 }
166 }
167
168 class UmsEnableCallBack extends UnmountCallBack {
169 String method;
170
171 UmsEnableCallBack(String path, String method, boolean force) {
172 super(path, force);
173 this.method = method;
174 }
175
176 @Override
177 void handleFinished() {
178 super.handleFinished();
179 doShareUnshareVolume(path, method, true);
180 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800181 }
182
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800183 class ShutdownCallBack extends UnmountCallBack {
184 IMountShutdownObserver observer;
185 ShutdownCallBack(String path, IMountShutdownObserver observer) {
186 super(path, true);
187 this.observer = observer;
188 }
189
190 @Override
191 void handleFinished() {
192 int ret = doUnmountVolume(path, true);
193 if (observer != null) {
194 try {
195 observer.onShutDownComplete(ret);
196 } catch (RemoteException e) {
San Mehata5078592010-03-25 09:36:54 -0700197 Slog.w(TAG, "RemoteException when shutting down");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800198 }
199 }
200 }
201 }
202
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400203 class MountServiceHandler extends Handler {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800204 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700205 boolean mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800206
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400207 MountServiceHandler(Looper l) {
208 super(l);
209 }
210
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800211 public void handleMessage(Message msg) {
212 switch (msg.what) {
213 case H_UNMOUNT_PM_UPDATE: {
San Mehata5078592010-03-25 09:36:54 -0700214 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800215 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
216 mForceUnmounts.add(ucb);
San Mehata5078592010-03-25 09:36:54 -0700217 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800218 // Register only if needed.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700219 if (!mUpdatingStatus) {
San Mehata5078592010-03-25 09:36:54 -0700220 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700221 mUpdatingStatus = true;
222 mPms.updateExternalMediaStatus(false, true);
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800223 }
224 break;
225 }
226 case H_UNMOUNT_PM_DONE: {
San Mehata5078592010-03-25 09:36:54 -0700227 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
San Mehata5078592010-03-25 09:36:54 -0700228 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700229 mUpdatingStatus = false;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800230 int size = mForceUnmounts.size();
231 int sizeArr[] = new int[size];
232 int sizeArrN = 0;
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700233 // Kill processes holding references first
234 ActivityManagerService ams = (ActivityManagerService)
235 ServiceManager.getService("activity");
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800236 for (int i = 0; i < size; i++) {
237 UnmountCallBack ucb = mForceUnmounts.get(i);
238 String path = ucb.path;
239 boolean done = false;
240 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800241 done = true;
242 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800243 int pids[] = getStorageUsers(path);
244 if (pids == null || pids.length == 0) {
245 done = true;
246 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800247 // Eliminate system process here?
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700248 ams.killPids(pids, "unmount media");
249 // Confirm if file references have been freed.
250 pids = getStorageUsers(path);
251 if (pids == null || pids.length == 0) {
252 done = true;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800253 }
254 }
255 }
Suchi Amalapurapu7af074a2010-04-05 16:46:32 -0700256 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
257 // Retry again
258 Slog.i(TAG, "Retrying to kill storage users again");
259 mHandler.sendMessageDelayed(
260 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
261 ucb.retries++),
262 RETRY_UNMOUNT_DELAY);
263 } else {
264 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
265 Slog.i(TAG, "Failed to unmount media inspite of " +
266 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
267 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800268 sizeArr[sizeArrN++] = i;
269 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
270 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800271 }
272 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800273 // Remove already processed elements from list.
274 for (int i = (sizeArrN-1); i >= 0; i--) {
275 mForceUnmounts.remove(sizeArr[i]);
276 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800277 break;
278 }
279 case H_UNMOUNT_MS : {
San Mehata5078592010-03-25 09:36:54 -0700280 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800281 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800282 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800283 break;
284 }
285 }
286 }
287 };
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400288 final private HandlerThread mHandlerThread;
289 final private Handler mHandler;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800290
San Mehat207e5382010-02-04 20:46:54 -0800291 private void waitForReady() {
292 while (mReady == false) {
293 for (int retries = 5; retries > 0; retries--) {
294 if (mReady) {
295 return;
296 }
297 SystemClock.sleep(1000);
298 }
San Mehata5078592010-03-25 09:36:54 -0700299 Slog.w(TAG, "Waiting too long for mReady!");
San Mehat207e5382010-02-04 20:46:54 -0800300 }
San Mehat1f6301e2010-01-07 22:40:27 -0800301 }
Kenny Root02c87302010-07-01 08:10:18 -0700302
San Mehat207e5382010-02-04 20:46:54 -0800303 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800305 String action = intent.getAction();
306
307 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800308 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800309
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800310 /*
311 * In the simulator, we need to broadcast a volume mounted event
312 * to make the media scanner run.
313 */
314 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
315 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
316 return;
317 }
San Mehatfafb0412010-02-18 19:40:04 -0800318 new Thread() {
319 public void run() {
320 try {
321 String path = Environment.getExternalStorageDirectory().getPath();
San Mehat6a254402010-03-22 10:21:00 -0700322 String state = getVolumeState(path);
323
Mike Lockwood03559752010-07-19 18:25:03 -0400324 if (mEmulateExternalStorage) {
325 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Mounted);
326 } else if (state.equals(Environment.MEDIA_UNMOUNTED)) {
San Mehatfafb0412010-02-18 19:40:04 -0800327 int rc = doMountVolume(path);
328 if (rc != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700329 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
San Mehatfafb0412010-02-18 19:40:04 -0800330 }
San Mehat6a254402010-03-22 10:21:00 -0700331 } else if (state.equals(Environment.MEDIA_SHARED)) {
332 /*
333 * Bootstrap UMS enabled state since vold indicates
334 * the volume is shared (runtime restart while ums enabled)
335 */
336 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
San Mehatfafb0412010-02-18 19:40:04 -0800337 }
San Mehat6a254402010-03-22 10:21:00 -0700338
San Mehat6a965af22010-02-24 17:47:30 -0800339 /*
San Mehat6a254402010-03-22 10:21:00 -0700340 * If UMS was connected on boot, send the connected event
San Mehat6a965af22010-02-24 17:47:30 -0800341 * now that we're up.
342 */
343 if (mSendUmsConnectedOnBoot) {
344 sendUmsIntent(true);
345 mSendUmsConnectedOnBoot = false;
346 }
San Mehatfafb0412010-02-18 19:40:04 -0800347 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700348 Slog.e(TAG, "Boot-time mount exception", ex);
San Mehatfafb0412010-02-18 19:40:04 -0800349 }
San Mehat207e5382010-02-04 20:46:54 -0800350 }
San Mehatfafb0412010-02-18 19:40:04 -0800351 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353 }
354 };
355
San Mehat4270e1e2010-01-29 05:32:19 -0800356 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
357 final IMountServiceListener mListener;
358
359 MountServiceBinderListener(IMountServiceListener listener) {
360 mListener = listener;
Kenny Root02c87302010-07-01 08:10:18 -0700361
San Mehat91c77612010-01-07 10:39:41 -0800362 }
363
San Mehat4270e1e2010-01-29 05:32:19 -0800364 public void binderDied() {
San Mehata5078592010-03-25 09:36:54 -0700365 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
San Mehat4270e1e2010-01-29 05:32:19 -0800366 synchronized(mListeners) {
367 mListeners.remove(this);
368 mListener.asBinder().unlinkToDeath(this, 0);
369 }
370 }
371 }
372
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800373 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800374 // TODO: Add support for multiple share methods
375 if (!method.equals("ums")) {
376 throw new IllegalArgumentException(String.format("Method %s not supported", method));
377 }
378
San Mehat4270e1e2010-01-29 05:32:19 -0800379 try {
380 mConnector.doCommand(String.format(
381 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
382 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -0700383 Slog.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800384 }
San Mehat4270e1e2010-01-29 05:32:19 -0800385 }
386
San Mehat207e5382010-02-04 20:46:54 -0800387 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800388 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -0700389 Slog.w(TAG, "Multiple volumes not currently supported");
San Mehat4270e1e2010-01-29 05:32:19 -0800390 return;
391 }
San Mehatb1043402010-02-05 08:26:50 -0800392
393 if (mLegacyState.equals(state)) {
San Mehata5078592010-03-25 09:36:54 -0700394 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
San Mehatb1043402010-02-05 08:26:50 -0800395 return;
396 }
Mike Lockwood03559752010-07-19 18:25:03 -0400397 // Update state on PackageManager, but only of real events
398 if (!mEmulateExternalStorage) {
399 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
400 mPms.updateExternalMediaStatus(false, false);
401 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
402 mPms.updateExternalMediaStatus(true, false);
403 }
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800404 }
San Mehat4270e1e2010-01-29 05:32:19 -0800405 String oldState = mLegacyState;
406 mLegacyState = state;
407
408 synchronized (mListeners) {
409 for (int i = mListeners.size() -1; i >= 0; i--) {
410 MountServiceBinderListener bl = mListeners.get(i);
411 try {
San Mehatb1043402010-02-05 08:26:50 -0800412 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800413 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700414 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800415 mListeners.remove(i);
416 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700417 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800418 }
419 }
420 }
421 }
422
423 /**
424 *
425 * Callback from NativeDaemonConnector
426 */
427 public void onDaemonConnected() {
428 /*
429 * Since we'll be calling back into the NativeDaemonConnector,
430 * we need to do our work in a new thread.
431 */
432 new Thread() {
433 public void run() {
434 /**
435 * Determine media state and UMS detection status
436 */
437 String path = Environment.getExternalStorageDirectory().getPath();
438 String state = Environment.MEDIA_REMOVED;
439
440 try {
441 String[] vols = mConnector.doListCommand(
442 "volume list", VoldResponseCode.VolumeListResult);
443 for (String volstr : vols) {
444 String[] tok = volstr.split(" ");
445 // FMT: <label> <mountpoint> <state>
446 if (!tok[1].equals(path)) {
San Mehata5078592010-03-25 09:36:54 -0700447 Slog.w(TAG, String.format(
San Mehat4270e1e2010-01-29 05:32:19 -0800448 "Skipping unknown volume '%s'",tok[1]));
449 continue;
450 }
451 int st = Integer.parseInt(tok[2]);
452 if (st == VolumeState.NoMedia) {
453 state = Environment.MEDIA_REMOVED;
454 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800455 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800456 } else if (st == VolumeState.Mounted) {
457 state = Environment.MEDIA_MOUNTED;
San Mehata5078592010-03-25 09:36:54 -0700458 Slog.i(TAG, "Media already mounted on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800459 } else if (st == VolumeState.Shared) {
460 state = Environment.MEDIA_SHARED;
San Mehata5078592010-03-25 09:36:54 -0700461 Slog.i(TAG, "Media shared on daemon connection");
San Mehat4270e1e2010-01-29 05:32:19 -0800462 } else {
463 throw new Exception(String.format("Unexpected state %d", st));
464 }
465 }
466 if (state != null) {
San Mehata5078592010-03-25 09:36:54 -0700467 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
San Mehat4270e1e2010-01-29 05:32:19 -0800468 updatePublicVolumeState(path, state);
469 }
470 } catch (Exception e) {
San Mehata5078592010-03-25 09:36:54 -0700471 Slog.e(TAG, "Error processing initial volume state", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800472 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
473 }
474
475 try {
San Mehat207e5382010-02-04 20:46:54 -0800476 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800477 notifyShareAvailabilityChange("ums", avail);
478 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700479 Slog.w(TAG, "Failed to get share availability");
San Mehat4270e1e2010-01-29 05:32:19 -0800480 }
San Mehat207e5382010-02-04 20:46:54 -0800481 /*
482 * Now that we've done our initialization, release
483 * the hounds!
484 */
485 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800486 }
487 }.start();
488 }
489
490 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800491 * Callback from NativeDaemonConnector
492 */
493 public boolean onEvent(int code, String raw, String[] cooked) {
494 Intent in = null;
495
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800496 if (DEBUG_EVENTS) {
497 StringBuilder builder = new StringBuilder();
498 builder.append("onEvent::");
499 builder.append(" raw= " + raw);
500 if (cooked != null) {
501 builder.append(" cooked = " );
502 for (String str : cooked) {
503 builder.append(" " + str);
504 }
505 }
San Mehata5078592010-03-25 09:36:54 -0700506 Slog.i(TAG, builder.toString());
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800507 }
San Mehat4270e1e2010-01-29 05:32:19 -0800508 if (code == VoldResponseCode.VolumeStateChange) {
509 /*
510 * One of the volumes we're managing has changed state.
511 * Format: "NNN Volume <label> <path> state changed
512 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
513 */
514 notifyVolumeStateChange(
515 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
516 Integer.parseInt(cooked[10]));
517 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
518 // FMT: NNN Share method <method> now <available|unavailable>
519 boolean avail = false;
520 if (cooked[5].equals("available")) {
521 avail = true;
522 }
523 notifyShareAvailabilityChange(cooked[3], avail);
524 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
525 (code == VoldResponseCode.VolumeDiskRemoved) ||
526 (code == VoldResponseCode.VolumeBadRemoval)) {
527 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
528 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
529 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
530 final String label = cooked[2];
531 final String path = cooked[3];
532 int major = -1;
533 int minor = -1;
534
535 try {
536 String devComp = cooked[6].substring(1, cooked[6].length() -1);
537 String[] devTok = devComp.split(":");
538 major = Integer.parseInt(devTok[0]);
539 minor = Integer.parseInt(devTok[1]);
540 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700541 Slog.e(TAG, "Failed to parse major/minor", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800542 }
543
San Mehat4270e1e2010-01-29 05:32:19 -0800544 if (code == VoldResponseCode.VolumeDiskInserted) {
545 new Thread() {
546 public void run() {
547 try {
548 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800549 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700550 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
San Mehat4270e1e2010-01-29 05:32:19 -0800551 }
552 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700553 Slog.w(TAG, "Failed to mount media on insertion", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800554 }
555 }
556 }.start();
557 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
558 /*
559 * This event gets trumped if we're already in BAD_REMOVAL state
560 */
561 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
562 return true;
563 }
564 /* Send the media unmounted event first */
San Mehata5078592010-03-25 09:36:54 -0700565 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800566 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
567 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
568 mContext.sendBroadcast(in);
569
San Mehata5078592010-03-25 09:36:54 -0700570 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
San Mehat4270e1e2010-01-29 05:32:19 -0800571 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
572 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
573 } else if (code == VoldResponseCode.VolumeBadRemoval) {
San Mehata5078592010-03-25 09:36:54 -0700574 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
San Mehat4270e1e2010-01-29 05:32:19 -0800575 /* Send the media unmounted event first */
576 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
577 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
578 mContext.sendBroadcast(in);
579
San Mehata5078592010-03-25 09:36:54 -0700580 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
San Mehat4270e1e2010-01-29 05:32:19 -0800581 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
582 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
583 } else {
San Mehata5078592010-03-25 09:36:54 -0700584 Slog.e(TAG, String.format("Unknown code {%d}", code));
San Mehat4270e1e2010-01-29 05:32:19 -0800585 }
586 } else {
587 return false;
588 }
589
590 if (in != null) {
591 mContext.sendBroadcast(in);
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400592 }
593 return true;
San Mehat4270e1e2010-01-29 05:32:19 -0800594 }
595
San Mehat207e5382010-02-04 20:46:54 -0800596 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800597 String vs = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -0700598 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
San Mehat4270e1e2010-01-29 05:32:19 -0800599
600 Intent in = null;
601
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500602 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehata5078592010-03-25 09:36:54 -0700603 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500604 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
605 Uri.parse("file://" + path)));
606 }
607
San Mehat4270e1e2010-01-29 05:32:19 -0800608 if (newState == VolumeState.Init) {
609 } else if (newState == VolumeState.NoMedia) {
610 // NoMedia is handled via Disk Remove events
611 } else if (newState == VolumeState.Idle) {
612 /*
613 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
614 * if we're in the process of enabling UMS
615 */
616 if (!vs.equals(
617 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
618 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800619 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehata5078592010-03-25 09:36:54 -0700620 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
San Mehat4270e1e2010-01-29 05:32:19 -0800621 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
622 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
623 }
624 } else if (newState == VolumeState.Pending) {
625 } else if (newState == VolumeState.Checking) {
San Mehata5078592010-03-25 09:36:54 -0700626 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
San Mehat4270e1e2010-01-29 05:32:19 -0800627 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
628 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
629 } else if (newState == VolumeState.Mounted) {
San Mehata5078592010-03-25 09:36:54 -0700630 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800631 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
San Mehat4270e1e2010-01-29 05:32:19 -0800632 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
633 in.putExtra("read-only", false);
634 } else if (newState == VolumeState.Unmounting) {
San Mehat4270e1e2010-01-29 05:32:19 -0800635 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
636 } else if (newState == VolumeState.Formatting) {
637 } else if (newState == VolumeState.Shared) {
San Mehata5078592010-03-25 09:36:54 -0700638 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
San Mehat4270e1e2010-01-29 05:32:19 -0800639 /* Send the media unmounted event first */
640 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
641 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
642 mContext.sendBroadcast(in);
643
San Mehata5078592010-03-25 09:36:54 -0700644 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
San Mehat4270e1e2010-01-29 05:32:19 -0800645 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
646 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehata5078592010-03-25 09:36:54 -0700647 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800648 } else if (newState == VolumeState.SharedMnt) {
San Mehata5078592010-03-25 09:36:54 -0700649 Slog.e(TAG, "Live shared mounts not supported yet!");
San Mehat4270e1e2010-01-29 05:32:19 -0800650 return;
651 } else {
San Mehata5078592010-03-25 09:36:54 -0700652 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800653 }
654
655 if (in != null) {
656 mContext.sendBroadcast(in);
657 }
658 }
659
San Mehat207e5382010-02-04 20:46:54 -0800660 private boolean doGetShareMethodAvailable(String method) {
Kenny Root85fb2062010-06-01 20:50:21 -0700661 ArrayList<String> rsp;
Kenny Roota80ce062010-06-01 13:23:53 -0700662 try {
Kenny Root85fb2062010-06-01 20:50:21 -0700663 rsp = mConnector.doCommand("share status " + method);
Kenny Roota80ce062010-06-01 13:23:53 -0700664 } catch (NativeDaemonConnectorException ex) {
665 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
666 return false;
667 }
San Mehat207e5382010-02-04 20:46:54 -0800668
669 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700670 String[] tok = line.split(" ");
671 if (tok.length < 3) {
672 Slog.e(TAG, "Malformed response to share status " + method);
673 return false;
674 }
675
San Mehat207e5382010-02-04 20:46:54 -0800676 int code;
677 try {
678 code = Integer.parseInt(tok[0]);
679 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700680 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehat207e5382010-02-04 20:46:54 -0800681 return false;
682 }
683 if (code == VoldResponseCode.ShareStatusResult) {
684 if (tok[2].equals("available"))
685 return true;
686 return false;
687 } else {
San Mehata5078592010-03-25 09:36:54 -0700688 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehat207e5382010-02-04 20:46:54 -0800689 return false;
690 }
691 }
San Mehata5078592010-03-25 09:36:54 -0700692 Slog.e(TAG, "Got an empty response");
San Mehat207e5382010-02-04 20:46:54 -0800693 return false;
694 }
695
696 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800697 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800698
San Mehata5078592010-03-25 09:36:54 -0700699 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
San Mehat207e5382010-02-04 20:46:54 -0800700 try {
701 mConnector.doCommand(String.format("volume mount %s", path));
702 } catch (NativeDaemonConnectorException e) {
703 /*
704 * Mount failed for some reason
705 */
706 Intent in = null;
707 int code = e.getCode();
708 if (code == VoldResponseCode.OpFailedNoMedia) {
709 /*
710 * Attempt to mount but no media inserted
711 */
San Mehatb1043402010-02-05 08:26:50 -0800712 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800713 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
San Mehata5078592010-03-25 09:36:54 -0700714 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
San Mehat207e5382010-02-04 20:46:54 -0800715 /*
716 * Media is blank or does not contain a supported filesystem
717 */
718 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
719 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800720 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800721 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehata5078592010-03-25 09:36:54 -0700722 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
San Mehat207e5382010-02-04 20:46:54 -0800723 /*
724 * Volume consistency check failed
725 */
726 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
727 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800728 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800729 } else {
San Mehatb1043402010-02-05 08:26:50 -0800730 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800731 }
732
733 /*
734 * Send broadcast intent (if required for the failure)
735 */
736 if (in != null) {
737 mContext.sendBroadcast(in);
738 }
739 }
740
741 return rc;
742 }
743
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800744 /*
745 * If force is not set, we do not unmount if there are
746 * processes holding references to the volume about to be unmounted.
747 * If force is set, all the processes holding references need to be
748 * killed via the ActivityManager before actually unmounting the volume.
749 * This might even take a while and might be retried after timed delays
750 * to make sure we dont end up in an instable state and kill some core
751 * processes.
752 */
San Mehatd9709982010-02-18 11:43:03 -0800753 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800754 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800755 return VoldResponseCode.OpFailedVolNotMounted;
756 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800757 // Redundant probably. But no harm in updating state again.
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700758 mPms.updateExternalMediaStatus(false, false);
San Mehat207e5382010-02-04 20:46:54 -0800759 try {
San Mehatd9709982010-02-18 11:43:03 -0800760 mConnector.doCommand(String.format(
761 "volume unmount %s%s", path, (force ? " force" : "")));
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -0700762 // We unmounted the volume. None of the asec containers are available now.
763 synchronized (mAsecMountSet) {
764 mAsecMountSet.clear();
765 }
San Mehatb1043402010-02-05 08:26:50 -0800766 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800767 } catch (NativeDaemonConnectorException e) {
768 // Don't worry about mismatch in PackageManager since the
769 // call back will handle the status changes any way.
770 int code = e.getCode();
771 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800772 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800773 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
774 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800775 } else {
San Mehatb1043402010-02-05 08:26:50 -0800776 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800777 }
778 }
779 }
780
781 private int doFormatVolume(String path) {
782 try {
783 String cmd = String.format("volume format %s", path);
784 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800785 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800786 } catch (NativeDaemonConnectorException e) {
787 int code = e.getCode();
788 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800789 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800790 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800791 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800792 } else {
San Mehatb1043402010-02-05 08:26:50 -0800793 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800794 }
795 }
796 }
797
San Mehatb1043402010-02-05 08:26:50 -0800798 private boolean doGetVolumeShared(String path, String method) {
799 String cmd = String.format("volume shared %s %s", path, method);
Kenny Roota80ce062010-06-01 13:23:53 -0700800 ArrayList<String> rsp;
801
802 try {
803 rsp = mConnector.doCommand(cmd);
804 } catch (NativeDaemonConnectorException ex) {
805 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
806 return false;
807 }
San Mehatb1043402010-02-05 08:26:50 -0800808
809 for (String line : rsp) {
Kenny Roota80ce062010-06-01 13:23:53 -0700810 String[] tok = line.split(" ");
811 if (tok.length < 3) {
812 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
813 return false;
814 }
815
San Mehatb1043402010-02-05 08:26:50 -0800816 int code;
817 try {
818 code = Integer.parseInt(tok[0]);
819 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -0700820 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
San Mehatb1043402010-02-05 08:26:50 -0800821 return false;
822 }
823 if (code == VoldResponseCode.ShareEnabledResult) {
Kenny Roota80ce062010-06-01 13:23:53 -0700824 return "enabled".equals(tok[2]);
San Mehatb1043402010-02-05 08:26:50 -0800825 } else {
San Mehata5078592010-03-25 09:36:54 -0700826 Slog.e(TAG, String.format("Unexpected response code %d", code));
San Mehatb1043402010-02-05 08:26:50 -0800827 return false;
828 }
829 }
San Mehata5078592010-03-25 09:36:54 -0700830 Slog.e(TAG, "Got an empty response");
San Mehatb1043402010-02-05 08:26:50 -0800831 return false;
832 }
833
San Mehat207e5382010-02-04 20:46:54 -0800834 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800835 if (!method.equals("ums")) {
San Mehata5078592010-03-25 09:36:54 -0700836 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
San Mehat4270e1e2010-01-29 05:32:19 -0800837 return;
838 }
839
840 synchronized (mListeners) {
841 for (int i = mListeners.size() -1; i >= 0; i--) {
842 MountServiceBinderListener bl = mListeners.get(i);
843 try {
San Mehatb1043402010-02-05 08:26:50 -0800844 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800845 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700846 Slog.e(TAG, "Listener dead");
San Mehat4270e1e2010-01-29 05:32:19 -0800847 mListeners.remove(i);
848 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700849 Slog.e(TAG, "Listener failed", ex);
San Mehat4270e1e2010-01-29 05:32:19 -0800850 }
851 }
852 }
853
San Mehat207e5382010-02-04 20:46:54 -0800854 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800855 sendUmsIntent(avail);
856 } else {
857 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800858 }
San Mehat2fe718a2010-03-11 12:01:49 -0800859
860 final String path = Environment.getExternalStorageDirectory().getPath();
861 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
862 /*
863 * USB mass storage disconnected while enabled
864 */
865 new Thread() {
866 public void run() {
867 try {
868 int rc;
San Mehata5078592010-03-25 09:36:54 -0700869 Slog.w(TAG, "Disabling UMS after cable disconnect");
San Mehat2fe718a2010-03-11 12:01:49 -0800870 doShareUnshareVolume(path, "ums", false);
871 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -0700872 Slog.e(TAG, String.format(
San Mehat2fe718a2010-03-11 12:01:49 -0800873 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
874 path, rc));
875 }
876 } catch (Exception ex) {
San Mehata5078592010-03-25 09:36:54 -0700877 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
San Mehat2fe718a2010-03-11 12:01:49 -0800878 }
879 }
880 }.start();
881 }
San Mehat4270e1e2010-01-29 05:32:19 -0800882 }
883
San Mehat6a965af22010-02-24 17:47:30 -0800884 private void sendUmsIntent(boolean c) {
885 mContext.sendBroadcast(
886 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
887 }
888
San Mehat207e5382010-02-04 20:46:54 -0800889 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800890 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
891 throw new SecurityException(String.format("Requires %s permission", perm));
892 }
893 }
894
895 /**
San Mehat207e5382010-02-04 20:46:54 -0800896 * Constructs a new MountService instance
897 *
898 * @param context Binder context for this service
899 */
900 public MountService(Context context) {
901 mContext = context;
902
Mike Lockwood03559752010-07-19 18:25:03 -0400903 mEmulateExternalStorage = context.getResources().getBoolean(
904 com.android.internal.R.bool.config_emulateExternalStorage);
905 if (mEmulateExternalStorage) {
906 Slog.d(TAG, "using emulated external storage");
907 mLegacyState = Environment.MEDIA_MOUNTED;
908 }
909
San Mehat207e5382010-02-04 20:46:54 -0800910 // XXX: This will go away soon in favor of IMountServiceObserver
911 mPms = (PackageManagerService) ServiceManager.getService("package");
912
913 mContext.registerReceiver(mBroadcastReceiver,
914 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
915
Daniel Sandler5f27ef42010-03-16 15:42:02 -0400916 mHandlerThread = new HandlerThread("MountService");
917 mHandlerThread.start();
918 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
919
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800920 /*
921 * Vold does not run in the simulator, so pretend the connector thread
922 * ran and did its thing.
923 */
924 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
925 mReady = true;
926 mUmsEnabling = true;
927 return;
928 }
929
San Mehat207e5382010-02-04 20:46:54 -0800930 mConnector = new NativeDaemonConnector(this, "vold", 10, "VoldConnector");
931 mReady = false;
932 Thread thread = new Thread(mConnector, NativeDaemonConnector.class.getName());
933 thread.start();
934 }
935
936 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800937 * Exposed API calls below here
938 */
939
940 public void registerListener(IMountServiceListener listener) {
941 synchronized (mListeners) {
942 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
943 try {
944 listener.asBinder().linkToDeath(bl, 0);
945 mListeners.add(bl);
946 } catch (RemoteException rex) {
San Mehata5078592010-03-25 09:36:54 -0700947 Slog.e(TAG, "Failed to link to listener death");
San Mehat4270e1e2010-01-29 05:32:19 -0800948 }
949 }
950 }
951
952 public void unregisterListener(IMountServiceListener listener) {
953 synchronized (mListeners) {
954 for(MountServiceBinderListener bl : mListeners) {
955 if (bl.mListener == listener) {
956 mListeners.remove(mListeners.indexOf(bl));
957 return;
958 }
959 }
960 }
961 }
962
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800963 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -0800964 validatePermission(android.Manifest.permission.SHUTDOWN);
965
San Mehata5078592010-03-25 09:36:54 -0700966 Slog.i(TAG, "Shutting down");
San Mehat4270e1e2010-01-29 05:32:19 -0800967
968 String path = Environment.getExternalStorageDirectory().getPath();
969 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -0800970
971 if (state.equals(Environment.MEDIA_SHARED)) {
972 /*
973 * If the media is currently shared, unshare it.
974 * XXX: This is still dangerous!. We should not
975 * be rebooting at *all* if UMS is enabled, since
976 * the UMS host could have dirty FAT cache entries
977 * yet to flush.
978 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800979 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -0800980 } else if (state.equals(Environment.MEDIA_CHECKING)) {
981 /*
982 * If the media is being checked, then we need to wait for
983 * it to complete before being able to proceed.
984 */
985 // XXX: @hackbod - Should we disable the ANR timer here?
986 int retries = 30;
987 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
988 try {
989 Thread.sleep(1000);
990 } catch (InterruptedException iex) {
San Mehata5078592010-03-25 09:36:54 -0700991 Slog.e(TAG, "Interrupted while waiting for media", iex);
San Mehat91c77612010-01-07 10:39:41 -0800992 break;
993 }
994 state = Environment.getExternalStorageState();
995 }
996 if (retries == 0) {
San Mehata5078592010-03-25 09:36:54 -0700997 Slog.e(TAG, "Timed out waiting for media to check");
San Mehat91c77612010-01-07 10:39:41 -0800998 }
999 }
1000
1001 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08001002 // Post a unmount message.
1003 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1004 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
San Mehat4270e1e2010-01-29 05:32:19 -08001005 }
1006 }
1007
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001008 private boolean getUmsEnabling() {
1009 synchronized (mListeners) {
1010 return mUmsEnabling;
1011 }
1012 }
1013
1014 private void setUmsEnabling(boolean enable) {
1015 synchronized (mListeners) {
1016 mUmsEnabling = true;
1017 }
1018 }
1019
San Mehatb1043402010-02-05 08:26:50 -08001020 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -08001021 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -08001022
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001023 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -08001024 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -08001025 }
San Mehatb1043402010-02-05 08:26:50 -08001026 return doGetShareMethodAvailable("ums");
1027 }
1028
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001029 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -08001030 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001031 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -08001032
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001033 // TODO: Add support for multiple share methods
1034
1035 /*
1036 * If the volume is mounted and we're enabling then unmount it
1037 */
1038 String path = Environment.getExternalStorageDirectory().getPath();
1039 String vs = getVolumeState(path);
1040 String method = "ums";
1041 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1042 // Override for isUsbMassStorageEnabled()
1043 setUmsEnabling(enable);
1044 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1045 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1046 // Clear override
1047 setUmsEnabling(false);
1048 }
1049 /*
1050 * If we disabled UMS then mount the volume
1051 */
1052 if (!enable) {
1053 doShareUnshareVolume(path, method, enable);
1054 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehata5078592010-03-25 09:36:54 -07001055 Slog.e(TAG, "Failed to remount " + path +
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -08001056 " after disabling share method " + method);
1057 /*
1058 * Even though the mount failed, the unshare didn't so don't indicate an error.
1059 * The mountVolume() call will have set the storage state and sent the necessary
1060 * broadcasts.
1061 */
1062 }
1063 }
San Mehatb1043402010-02-05 08:26:50 -08001064 }
1065
1066 public boolean isUsbMassStorageEnabled() {
1067 waitForReady();
1068 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
San Mehat4270e1e2010-01-29 05:32:19 -08001070
San Mehat7fd0fee2009-12-17 07:12:23 -08001071 /**
1072 * @return state of the volume at the specified mount point
1073 */
San Mehat4270e1e2010-01-29 05:32:19 -08001074 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001075 /*
1076 * XXX: Until we have multiple volume discovery, just hardwire
1077 * this to /sdcard
1078 */
1079 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
San Mehata5078592010-03-25 09:36:54 -07001080 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
San Mehat7fd0fee2009-12-17 07:12:23 -08001081 throw new IllegalArgumentException();
1082 }
1083
1084 return mLegacyState;
1085 }
1086
San Mehat4270e1e2010-01-29 05:32:19 -08001087 public int mountVolume(String path) {
1088 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001089
San Mehat207e5382010-02-04 20:46:54 -08001090 waitForReady();
1091 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001094 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001095 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001096 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001098 String volState = getVolumeState(path);
San Mehata5078592010-03-25 09:36:54 -07001099 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -08001100 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1101 Environment.MEDIA_REMOVED.equals(volState) ||
1102 Environment.MEDIA_SHARED.equals(volState) ||
1103 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1104 // Media already unmounted or cannot be unmounted.
1105 // TODO return valid return code when adding observer call back.
1106 return;
1107 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001108 UnmountCallBack ucb = new UnmountCallBack(path, force);
1109 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
1111
San Mehat4270e1e2010-01-29 05:32:19 -08001112 public int formatVolume(String path) {
1113 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001114 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001115
San Mehat207e5382010-02-04 20:46:54 -08001116 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
1118
San Mehatc1b4ce92010-02-16 17:13:03 -08001119 public int []getStorageUsers(String path) {
1120 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1121 waitForReady();
1122 try {
1123 String[] r = mConnector.doListCommand(
1124 String.format("storage users %s", path),
1125 VoldResponseCode.StorageUsersListResult);
1126 // FMT: <pid> <process name>
1127 int[] data = new int[r.length];
1128 for (int i = 0; i < r.length; i++) {
1129 String []tok = r[i].split(" ");
1130 try {
1131 data[i] = Integer.parseInt(tok[0]);
1132 } catch (NumberFormatException nfe) {
San Mehata5078592010-03-25 09:36:54 -07001133 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
San Mehatc1b4ce92010-02-16 17:13:03 -08001134 return new int[0];
1135 }
1136 }
1137 return data;
1138 } catch (NativeDaemonConnectorException e) {
San Mehata5078592010-03-25 09:36:54 -07001139 Slog.e(TAG, "Failed to retrieve storage users list", e);
San Mehatc1b4ce92010-02-16 17:13:03 -08001140 return new int[0];
1141 }
1142 }
1143
San Mehatb1043402010-02-05 08:26:50 -08001144 private void warnOnNotMounted() {
1145 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
San Mehata5078592010-03-25 09:36:54 -07001146 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
San Mehatb1043402010-02-05 08:26:50 -08001147 }
1148 }
1149
San Mehat4270e1e2010-01-29 05:32:19 -08001150 public String[] getSecureContainerList() {
1151 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001152 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001153 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001154
San Mehat4270e1e2010-01-29 05:32:19 -08001155 try {
1156 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1157 } catch (NativeDaemonConnectorException e) {
1158 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160 }
San Mehat36972292010-01-06 11:06:32 -08001161
San Mehat4270e1e2010-01-29 05:32:19 -08001162 public int createSecureContainer(String id, int sizeMb, String fstype,
1163 String key, int ownerUid) {
1164 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001165 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001166 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001167
San Mehatb1043402010-02-05 08:26:50 -08001168 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001169 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1170 try {
1171 mConnector.doCommand(cmd);
1172 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001173 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001174 }
San Mehata181b212010-02-11 06:50:20 -08001175
1176 if (rc == StorageResultCode.OperationSucceeded) {
1177 synchronized (mAsecMountSet) {
1178 mAsecMountSet.add(id);
1179 }
1180 }
San Mehat4270e1e2010-01-29 05:32:19 -08001181 return rc;
San Mehat36972292010-01-06 11:06:32 -08001182 }
1183
San Mehat4270e1e2010-01-29 05:32:19 -08001184 public int finalizeSecureContainer(String id) {
1185 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001186 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001187
San Mehatb1043402010-02-05 08:26:50 -08001188 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001189 try {
1190 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001191 /*
1192 * Finalization does a remount, so no need
1193 * to update mAsecMountSet
1194 */
San Mehat4270e1e2010-01-29 05:32:19 -08001195 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001196 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001197 }
San Mehat4270e1e2010-01-29 05:32:19 -08001198 return rc;
San Mehat36972292010-01-06 11:06:32 -08001199 }
1200
San Mehatd9709982010-02-18 11:43:03 -08001201 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001202 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001203 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001204 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001205
San Mehatb1043402010-02-05 08:26:50 -08001206 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001207 try {
San Mehatd9709982010-02-18 11:43:03 -08001208 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001209 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001210 int code = e.getCode();
1211 if (code == VoldResponseCode.OpFailedStorageBusy) {
1212 rc = StorageResultCode.OperationFailedStorageBusy;
1213 } else {
1214 rc = StorageResultCode.OperationFailedInternalError;
1215 }
San Mehat02735bc2010-01-26 15:18:08 -08001216 }
San Mehata181b212010-02-11 06:50:20 -08001217
1218 if (rc == StorageResultCode.OperationSucceeded) {
1219 synchronized (mAsecMountSet) {
1220 if (mAsecMountSet.contains(id)) {
1221 mAsecMountSet.remove(id);
1222 }
1223 }
1224 }
1225
San Mehat4270e1e2010-01-29 05:32:19 -08001226 return rc;
San Mehat36972292010-01-06 11:06:32 -08001227 }
1228
San Mehat4270e1e2010-01-29 05:32:19 -08001229 public int mountSecureContainer(String id, String key, int ownerUid) {
1230 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001231 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001232 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001233
San Mehata181b212010-02-11 06:50:20 -08001234 synchronized (mAsecMountSet) {
1235 if (mAsecMountSet.contains(id)) {
1236 return StorageResultCode.OperationFailedStorageMounted;
1237 }
1238 }
1239
San Mehatb1043402010-02-05 08:26:50 -08001240 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001241 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1242 try {
1243 mConnector.doCommand(cmd);
1244 } catch (NativeDaemonConnectorException e) {
Kenny Rootf0304622010-03-19 19:20:42 -07001245 int code = e.getCode();
1246 if (code != VoldResponseCode.OpFailedStorageBusy) {
1247 rc = StorageResultCode.OperationFailedInternalError;
1248 }
San Mehat02735bc2010-01-26 15:18:08 -08001249 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001250
1251 if (rc == StorageResultCode.OperationSucceeded) {
1252 synchronized (mAsecMountSet) {
1253 mAsecMountSet.add(id);
1254 }
1255 }
San Mehat4270e1e2010-01-29 05:32:19 -08001256 return rc;
San Mehat36972292010-01-06 11:06:32 -08001257 }
1258
San Mehatd9709982010-02-18 11:43:03 -08001259 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001260 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001261 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001262 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001263
San Mehat6cdd9c02010-02-09 14:45:20 -08001264 synchronized (mAsecMountSet) {
1265 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001266 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001267 }
1268 }
1269
San Mehatb1043402010-02-05 08:26:50 -08001270 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001271 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001272 try {
1273 mConnector.doCommand(cmd);
1274 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001275 int code = e.getCode();
1276 if (code == VoldResponseCode.OpFailedStorageBusy) {
1277 rc = StorageResultCode.OperationFailedStorageBusy;
1278 } else {
1279 rc = StorageResultCode.OperationFailedInternalError;
1280 }
San Mehat02735bc2010-01-26 15:18:08 -08001281 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001282
1283 if (rc == StorageResultCode.OperationSucceeded) {
1284 synchronized (mAsecMountSet) {
1285 mAsecMountSet.remove(id);
1286 }
1287 }
San Mehat4270e1e2010-01-29 05:32:19 -08001288 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001289 }
1290
San Mehat6cdd9c02010-02-09 14:45:20 -08001291 public boolean isSecureContainerMounted(String id) {
1292 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1293 waitForReady();
1294 warnOnNotMounted();
1295
1296 synchronized (mAsecMountSet) {
1297 return mAsecMountSet.contains(id);
1298 }
1299 }
1300
San Mehat4270e1e2010-01-29 05:32:19 -08001301 public int renameSecureContainer(String oldId, String newId) {
1302 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001303 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001304 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001305
San Mehata181b212010-02-11 06:50:20 -08001306 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001307 /*
1308 * Because a mounted container has active internal state which cannot be
1309 * changed while active, we must ensure both ids are not currently mounted.
1310 */
1311 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001312 return StorageResultCode.OperationFailedStorageMounted;
1313 }
1314 }
1315
San Mehatb1043402010-02-05 08:26:50 -08001316 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001317 String cmd = String.format("asec rename %s %s", oldId, newId);
1318 try {
1319 mConnector.doCommand(cmd);
1320 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001321 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001322 }
San Mehata181b212010-02-11 06:50:20 -08001323
San Mehat4270e1e2010-01-29 05:32:19 -08001324 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001325 }
1326
San Mehat4270e1e2010-01-29 05:32:19 -08001327 public String getSecureContainerPath(String id) {
1328 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001329 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001330 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001331
San Mehat2d66cef2010-03-23 11:12:52 -07001332 try {
1333 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1334 String []tok = rsp.get(0).split(" ");
San Mehat22dd86e2010-01-12 12:21:18 -08001335 int code = Integer.parseInt(tok[0]);
San Mehat2d66cef2010-03-23 11:12:52 -07001336 if (code != VoldResponseCode.AsecPathResult) {
1337 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1338 }
1339 return tok[1];
1340 } catch (NativeDaemonConnectorException e) {
1341 int code = e.getCode();
1342 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1343 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
San Mehat22dd86e2010-01-12 12:21:18 -08001344 } else {
San Mehat2d66cef2010-03-23 11:12:52 -07001345 throw new IllegalStateException(String.format("Unexpected response code %d", code));
San Mehat22dd86e2010-01-12 12:21:18 -08001346 }
1347 }
San Mehat22dd86e2010-01-12 12:21:18 -08001348 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07001349
1350 public void finishMediaUpdate() {
1351 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1352 }
Kenny Root02c87302010-07-01 08:10:18 -07001353
1354 private boolean isCallerOwnerOfPackage(String packageName) {
1355 final int callerUid = Binder.getCallingUid();
1356 return isUidOwnerOfPackage(packageName, callerUid);
1357 }
1358
1359 private boolean isUidOwnerOfPackage(String packageName, int callerUid) {
1360 if (packageName == null) {
1361 return false;
1362 }
1363
1364 final int packageUid = mPms.getPackageUid(packageName);
1365
1366 if (DEBUG_OBB) {
1367 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1368 packageUid + ", callerUid = " + callerUid);
1369 }
1370
1371 return callerUid == packageUid;
1372 }
1373
1374 public String getMountedObbPath(String filename) {
1375 waitForReady();
1376 warnOnNotMounted();
1377
1378 // XXX replace with call to IMediaContainerService
1379 ObbInfo obbInfo = ObbScanner.getObbInfo(filename);
1380 if (!isCallerOwnerOfPackage(obbInfo.packageName)) {
1381 throw new IllegalArgumentException("Caller package does not match OBB file");
1382 }
1383
1384 try {
1385 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1386 String []tok = rsp.get(0).split(" ");
1387 int code = Integer.parseInt(tok[0]);
1388 if (code != VoldResponseCode.AsecPathResult) {
1389 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1390 }
1391 return tok[1];
1392 } catch (NativeDaemonConnectorException e) {
1393 int code = e.getCode();
1394 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1395 throw new IllegalArgumentException(String.format("OBB '%s' not found", filename));
1396 } else {
1397 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1398 }
1399 }
1400 }
1401
1402 public boolean isObbMounted(String filename) {
1403 waitForReady();
1404 warnOnNotMounted();
1405
1406 // XXX replace with call to IMediaContainerService
1407 ObbInfo obbInfo = ObbScanner.getObbInfo(filename);
1408 if (!isCallerOwnerOfPackage(obbInfo.packageName)) {
1409 throw new IllegalArgumentException("Caller package does not match OBB file");
1410 }
1411
1412 synchronized (mObbMountSet) {
1413 return mObbMountSet.contains(filename);
1414 }
1415 }
1416
1417 public int mountObb(String filename, String key) {
1418 waitForReady();
1419 warnOnNotMounted();
1420
1421 synchronized (mObbMountSet) {
1422 if (mObbMountSet.contains(filename)) {
1423 return StorageResultCode.OperationFailedStorageMounted;
1424 }
1425 }
1426
1427 final int callerUid = Binder.getCallingUid();
1428
1429 // XXX replace with call to IMediaContainerService
1430 ObbInfo obbInfo = ObbScanner.getObbInfo(filename);
1431 if (!isUidOwnerOfPackage(obbInfo.packageName, callerUid)) {
1432 throw new IllegalArgumentException("Caller package does not match OBB file");
1433 }
1434
1435 if (key == null) {
1436 key = "none";
1437 }
1438
1439 int rc = StorageResultCode.OperationSucceeded;
1440 String cmd = String.format("obb mount %s %s %d", filename, key, callerUid);
1441 try {
1442 mConnector.doCommand(cmd);
1443 } catch (NativeDaemonConnectorException e) {
1444 int code = e.getCode();
1445 if (code != VoldResponseCode.OpFailedStorageBusy) {
1446 rc = StorageResultCode.OperationFailedInternalError;
1447 }
1448 }
1449
1450 if (rc == StorageResultCode.OperationSucceeded) {
1451 synchronized (mObbMountSet) {
1452 mObbMountSet.add(filename);
1453 }
1454 }
1455 return rc;
1456 }
1457
1458 public int unmountObb(String filename, boolean force) {
1459 waitForReady();
1460 warnOnNotMounted();
1461
1462 ObbInfo obbInfo = ObbScanner.getObbInfo(filename);
1463 if (!isCallerOwnerOfPackage(obbInfo.packageName)) {
1464 throw new IllegalArgumentException("Caller package does not match OBB file");
1465 }
1466
1467 synchronized (mObbMountSet) {
1468 if (!mObbMountSet.contains(filename)) {
1469 return StorageResultCode.OperationFailedStorageNotMounted;
1470 }
1471 }
1472
1473 int rc = StorageResultCode.OperationSucceeded;
1474 String cmd = String.format("obb unmount %s%s", filename, (force ? " force" : ""));
1475 try {
1476 mConnector.doCommand(cmd);
1477 } catch (NativeDaemonConnectorException e) {
1478 int code = e.getCode();
1479 if (code == VoldResponseCode.OpFailedStorageBusy) {
1480 rc = StorageResultCode.OperationFailedStorageBusy;
1481 } else {
1482 rc = StorageResultCode.OperationFailedInternalError;
1483 }
1484 }
1485
1486 if (rc == StorageResultCode.OperationSucceeded) {
1487 synchronized (mObbMountSet) {
1488 mObbMountSet.remove(filename);
1489 }
1490 }
1491 return rc;
1492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493}
1494