2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server;
19 import com.android.internal.app.IMediaContainerService;
20 import com.android.server.am.ActivityManagerService;
22 import android.content.BroadcastReceiver;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.ServiceConnection;
28 import android.content.pm.PackageManager;
29 import android.content.res.ObbInfo;
30 import android.net.Uri;
31 import android.os.Binder;
32 import android.os.Environment;
33 import android.os.Handler;
34 import android.os.HandlerThread;
35 import android.os.IBinder;
36 import android.os.Looper;
37 import android.os.Message;
38 import android.os.RemoteException;
39 import android.os.ServiceManager;
40 import android.os.SystemClock;
41 import android.os.SystemProperties;
42 import android.os.storage.IMountService;
43 import android.os.storage.IMountServiceListener;
44 import android.os.storage.IMountShutdownObserver;
45 import android.os.storage.IObbActionListener;
46 import android.os.storage.OnObbStateChangeListener;
47 import android.os.storage.StorageResultCode;
48 import android.util.Slog;
50 import java.io.FileDescriptor;
51 import java.io.IOException;
52 import java.io.PrintWriter;
53 import java.math.BigInteger;
54 import java.security.NoSuchAlgorithmException;
55 import java.security.spec.InvalidKeySpecException;
56 import java.security.spec.KeySpec;
57 import java.util.ArrayList;
58 import java.util.HashMap;
59 import java.util.HashSet;
60 import java.util.Iterator;
61 import java.util.LinkedList;
62 import java.util.List;
64 import java.util.Map.Entry;
66 import javax.crypto.SecretKey;
67 import javax.crypto.SecretKeyFactory;
68 import javax.crypto.spec.PBEKeySpec;
71 * MountService implements back-end services for platform storage
73 * @hide - Applications should use android.os.storage.StorageManager
74 * to access the MountService.
76 class MountService extends IMountService.Stub
77 implements INativeDaemonConnectorCallbacks {
78 private static final boolean LOCAL_LOGD = false;
79 private static final boolean DEBUG_UNMOUNT = false;
80 private static final boolean DEBUG_EVENTS = false;
81 private static final boolean DEBUG_OBB = false;
83 private static final String TAG = "MountService";
85 private static final String VOLD_TAG = "VoldConnector";
88 * Internal vold volume state constants
91 public static final int Init = -1;
92 public static final int NoMedia = 0;
93 public static final int Idle = 1;
94 public static final int Pending = 2;
95 public static final int Checking = 3;
96 public static final int Mounted = 4;
97 public static final int Unmounting = 5;
98 public static final int Formatting = 6;
99 public static final int Shared = 7;
100 public static final int SharedMnt = 8;
104 * Internal vold response code constants
106 class VoldResponseCode {
108 * 100 series - Requestion action was initiated; expect another reply
109 * before proceeding with a new command.
111 public static final int VolumeListResult = 110;
112 public static final int AsecListResult = 111;
113 public static final int StorageUsersListResult = 112;
116 * 200 series - Requestion action has been successfully completed.
118 public static final int ShareStatusResult = 210;
119 public static final int AsecPathResult = 211;
120 public static final int ShareEnabledResult = 212;
123 * 400 series - Command was accepted, but the requested action
124 * did not take place.
126 public static final int OpFailedNoMedia = 401;
127 public static final int OpFailedMediaBlank = 402;
128 public static final int OpFailedMediaCorrupt = 403;
129 public static final int OpFailedVolNotMounted = 404;
130 public static final int OpFailedStorageBusy = 405;
131 public static final int OpFailedStorageNotFound = 406;
134 * 600 series - Unsolicited broadcasts.
136 public static final int VolumeStateChange = 605;
137 public static final int ShareAvailabilityChange = 620;
138 public static final int VolumeDiskInserted = 630;
139 public static final int VolumeDiskRemoved = 631;
140 public static final int VolumeBadRemoval = 632;
143 private Context mContext;
144 private NativeDaemonConnector mConnector;
145 private String mLegacyState = Environment.MEDIA_REMOVED;
146 private PackageManagerService mPms;
147 private boolean mUmsEnabling;
148 // Used as a lock for methods that register/unregister listeners.
149 final private ArrayList<MountServiceBinderListener> mListeners =
150 new ArrayList<MountServiceBinderListener>();
151 private boolean mBooted = false;
152 private boolean mReady = false;
153 private boolean mSendUmsConnectedOnBoot = false;
156 * Private hash of currently mounted secure containers.
157 * Used as a lock in methods to manipulate secure containers.
159 final private HashSet<String> mAsecMountSet = new HashSet<String>();
162 * The size of the crypto algorithm key in bits for OBB files. Currently
163 * Twofish is used which takes 128-bit keys.
165 private static final int CRYPTO_ALGORITHM_KEY_SIZE = 128;
168 * The number of times to run SHA1 in the PBKDF2 function for OBB files.
169 * 1024 is reasonably secure and not too slow.
171 private static final int PBKDF2_HASH_ROUNDS = 1024;
174 * Mounted OBB tracking information. Used to track the current state of all
177 final private Map<IBinder, List<ObbState>> mObbMounts = new HashMap<IBinder, List<ObbState>>();
178 final private Map<String, ObbState> mObbPathToStateMap = new HashMap<String, ObbState>();
180 class ObbState implements IBinder.DeathRecipient {
181 public ObbState(String filename, int callerUid, IObbActionListener token, int nonce)
182 throws RemoteException {
183 this.filename = filename;
184 this.callerUid = callerUid;
189 // OBB source filename
192 // Binder.callingUid()
193 final public int callerUid;
195 // Token of remote Binder caller
196 final IObbActionListener token;
198 // Identifier to pass back to the token
201 public IBinder getBinder() {
202 return token.asBinder();
206 public void binderDied() {
207 ObbAction action = new UnmountObbAction(this, true);
208 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
211 public void link() throws RemoteException {
212 getBinder().linkToDeath(this, 0);
215 public void unlink() {
216 getBinder().unlinkToDeath(this, 0);
220 public String toString() {
221 StringBuilder sb = new StringBuilder("ObbState{");
222 sb.append("filename=");
224 sb.append(",token=");
225 sb.append(token.toString());
226 sb.append(",callerUid=");
227 sb.append(callerUid);
229 return sb.toString();
233 // OBB Action Handler
234 final private ObbActionHandler mObbActionHandler;
236 // OBB action handler messages
237 private static final int OBB_RUN_ACTION = 1;
238 private static final int OBB_MCS_BOUND = 2;
239 private static final int OBB_MCS_UNBIND = 3;
240 private static final int OBB_MCS_RECONNECT = 4;
241 private static final int OBB_FLUSH_MOUNT_STATE = 5;
244 * Default Container Service information
246 static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
247 "com.android.defcontainer", "com.android.defcontainer.DefaultContainerService");
249 final private DefaultContainerConnection mDefContainerConn = new DefaultContainerConnection();
251 class DefaultContainerConnection implements ServiceConnection {
252 public void onServiceConnected(ComponentName name, IBinder service) {
254 Slog.i(TAG, "onServiceConnected");
255 IMediaContainerService imcs = IMediaContainerService.Stub.asInterface(service);
256 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_MCS_BOUND, imcs));
259 public void onServiceDisconnected(ComponentName name) {
261 Slog.i(TAG, "onServiceDisconnected");
265 // Used in the ObbActionHandler
266 private IMediaContainerService mContainerService = null;
269 private static final int H_UNMOUNT_PM_UPDATE = 1;
270 private static final int H_UNMOUNT_PM_DONE = 2;
271 private static final int H_UNMOUNT_MS = 3;
272 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
273 private static final int MAX_UNMOUNT_RETRIES = 4;
275 class UnmountCallBack {
280 UnmountCallBack(String path, boolean force) {
286 void handleFinished() {
287 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path);
288 doUnmountVolume(path, true);
292 class UmsEnableCallBack extends UnmountCallBack {
295 UmsEnableCallBack(String path, String method, boolean force) {
297 this.method = method;
301 void handleFinished() {
302 super.handleFinished();
303 doShareUnshareVolume(path, method, true);
307 class ShutdownCallBack extends UnmountCallBack {
308 IMountShutdownObserver observer;
309 ShutdownCallBack(String path, IMountShutdownObserver observer) {
311 this.observer = observer;
315 void handleFinished() {
316 int ret = doUnmountVolume(path, true);
317 if (observer != null) {
319 observer.onShutDownComplete(ret);
320 } catch (RemoteException e) {
321 Slog.w(TAG, "RemoteException when shutting down");
327 class MountServiceHandler extends Handler {
328 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
329 boolean mUpdatingStatus = false;
331 MountServiceHandler(Looper l) {
335 public void handleMessage(Message msg) {
337 case H_UNMOUNT_PM_UPDATE: {
338 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_UPDATE");
339 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
340 mForceUnmounts.add(ucb);
341 if (DEBUG_UNMOUNT) Slog.i(TAG, " registered = " + mUpdatingStatus);
342 // Register only if needed.
343 if (!mUpdatingStatus) {
344 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updating external media status on PackageManager");
345 mUpdatingStatus = true;
346 mPms.updateExternalMediaStatus(false, true);
350 case H_UNMOUNT_PM_DONE: {
351 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_PM_DONE");
352 if (DEBUG_UNMOUNT) Slog.i(TAG, "Updated status. Processing requests");
353 mUpdatingStatus = false;
354 int size = mForceUnmounts.size();
355 int sizeArr[] = new int[size];
357 // Kill processes holding references first
358 ActivityManagerService ams = (ActivityManagerService)
359 ServiceManager.getService("activity");
360 for (int i = 0; i < size; i++) {
361 UnmountCallBack ucb = mForceUnmounts.get(i);
362 String path = ucb.path;
363 boolean done = false;
367 int pids[] = getStorageUsers(path);
368 if (pids == null || pids.length == 0) {
371 // Eliminate system process here?
372 ams.killPids(pids, "unmount media");
373 // Confirm if file references have been freed.
374 pids = getStorageUsers(path);
375 if (pids == null || pids.length == 0) {
380 if (!done && (ucb.retries < MAX_UNMOUNT_RETRIES)) {
382 Slog.i(TAG, "Retrying to kill storage users again");
383 mHandler.sendMessageDelayed(
384 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
386 RETRY_UNMOUNT_DELAY);
388 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
389 Slog.i(TAG, "Failed to unmount media inspite of " +
390 MAX_UNMOUNT_RETRIES + " retries. Forcibly killing processes now");
392 sizeArr[sizeArrN++] = i;
393 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
397 // Remove already processed elements from list.
398 for (int i = (sizeArrN-1); i >= 0; i--) {
399 mForceUnmounts.remove(sizeArr[i]);
403 case H_UNMOUNT_MS : {
404 if (DEBUG_UNMOUNT) Slog.i(TAG, "H_UNMOUNT_MS");
405 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
406 ucb.handleFinished();
412 final private HandlerThread mHandlerThread;
413 final private Handler mHandler;
415 private void waitForReady() {
416 while (mReady == false) {
417 for (int retries = 5; retries > 0; retries--) {
421 SystemClock.sleep(1000);
423 Slog.w(TAG, "Waiting too long for mReady!");
427 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
428 public void onReceive(Context context, Intent intent) {
429 String action = intent.getAction();
431 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
435 * In the simulator, we need to broadcast a volume mounted event
436 * to make the media scanner run.
438 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
439 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
445 String path = Environment.getExternalStorageDirectory().getPath();
446 String state = getVolumeState(path);
448 if (state.equals(Environment.MEDIA_UNMOUNTED)) {
449 int rc = doMountVolume(path);
450 if (rc != StorageResultCode.OperationSucceeded) {
451 Slog.e(TAG, String.format("Boot-time mount failed (%d)", rc));
453 } else if (state.equals(Environment.MEDIA_SHARED)) {
455 * Bootstrap UMS enabled state since vold indicates
456 * the volume is shared (runtime restart while ums enabled)
458 notifyVolumeStateChange(null, path, VolumeState.NoMedia, VolumeState.Shared);
462 * If UMS was connected on boot, send the connected event
465 if (mSendUmsConnectedOnBoot) {
467 mSendUmsConnectedOnBoot = false;
469 } catch (Exception ex) {
470 Slog.e(TAG, "Boot-time mount exception", ex);
478 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
479 final IMountServiceListener mListener;
481 MountServiceBinderListener(IMountServiceListener listener) {
482 mListener = listener;
486 public void binderDied() {
487 if (LOCAL_LOGD) Slog.d(TAG, "An IMountServiceListener has died!");
488 synchronized (mListeners) {
489 mListeners.remove(this);
490 mListener.asBinder().unlinkToDeath(this, 0);
495 private void doShareUnshareVolume(String path, String method, boolean enable) {
496 // TODO: Add support for multiple share methods
497 if (!method.equals("ums")) {
498 throw new IllegalArgumentException(String.format("Method %s not supported", method));
502 mConnector.doCommand(String.format(
503 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
504 } catch (NativeDaemonConnectorException e) {
505 Slog.e(TAG, "Failed to share/unshare", e);
509 private void updatePublicVolumeState(String path, String state) {
510 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
511 Slog.w(TAG, "Multiple volumes not currently supported");
515 if (mLegacyState.equals(state)) {
516 Slog.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
520 if (Environment.MEDIA_UNMOUNTED.equals(state)) {
521 // Tell the package manager the media is gone.
522 mPms.updateExternalMediaStatus(false, false);
525 * Some OBBs might have been unmounted when this volume was
526 * unmounted, so send a message to the handler to let it know to
527 * remove those from the list of mounted OBBS.
529 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_FLUSH_MOUNT_STATE,
531 } else if (Environment.MEDIA_MOUNTED.equals(state)) {
532 // Tell the package manager the media is available for use.
533 mPms.updateExternalMediaStatus(true, false);
536 String oldState = mLegacyState;
537 mLegacyState = state;
539 synchronized (mListeners) {
540 for (int i = mListeners.size() -1; i >= 0; i--) {
541 MountServiceBinderListener bl = mListeners.get(i);
543 bl.mListener.onStorageStateChanged(path, oldState, state);
544 } catch (RemoteException rex) {
545 Slog.e(TAG, "Listener dead");
546 mListeners.remove(i);
547 } catch (Exception ex) {
548 Slog.e(TAG, "Listener failed", ex);
556 * Callback from NativeDaemonConnector
558 public void onDaemonConnected() {
560 * Since we'll be calling back into the NativeDaemonConnector,
561 * we need to do our work in a new thread.
566 * Determine media state and UMS detection status
568 String path = Environment.getExternalStorageDirectory().getPath();
569 String state = Environment.MEDIA_REMOVED;
572 String[] vols = mConnector.doListCommand(
573 "volume list", VoldResponseCode.VolumeListResult);
574 for (String volstr : vols) {
575 String[] tok = volstr.split(" ");
576 // FMT: <label> <mountpoint> <state>
577 if (!tok[1].equals(path)) {
578 Slog.w(TAG, String.format(
579 "Skipping unknown volume '%s'",tok[1]));
582 int st = Integer.parseInt(tok[2]);
583 if (st == VolumeState.NoMedia) {
584 state = Environment.MEDIA_REMOVED;
585 } else if (st == VolumeState.Idle) {
586 state = Environment.MEDIA_UNMOUNTED;
587 } else if (st == VolumeState.Mounted) {
588 state = Environment.MEDIA_MOUNTED;
589 Slog.i(TAG, "Media already mounted on daemon connection");
590 } else if (st == VolumeState.Shared) {
591 state = Environment.MEDIA_SHARED;
592 Slog.i(TAG, "Media shared on daemon connection");
594 throw new Exception(String.format("Unexpected state %d", st));
598 if (DEBUG_EVENTS) Slog.i(TAG, "Updating valid state " + state);
599 updatePublicVolumeState(path, state);
601 } catch (Exception e) {
602 Slog.e(TAG, "Error processing initial volume state", e);
603 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
607 boolean avail = doGetShareMethodAvailable("ums");
608 notifyShareAvailabilityChange("ums", avail);
609 } catch (Exception ex) {
610 Slog.w(TAG, "Failed to get share availability");
613 * Now that we've done our initialization, release
622 * Callback from NativeDaemonConnector
624 public boolean onEvent(int code, String raw, String[] cooked) {
628 StringBuilder builder = new StringBuilder();
629 builder.append("onEvent::");
630 builder.append(" raw= " + raw);
631 if (cooked != null) {
632 builder.append(" cooked = " );
633 for (String str : cooked) {
634 builder.append(" " + str);
637 Slog.i(TAG, builder.toString());
639 if (code == VoldResponseCode.VolumeStateChange) {
641 * One of the volumes we're managing has changed state.
642 * Format: "NNN Volume <label> <path> state changed
643 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
645 notifyVolumeStateChange(
646 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
647 Integer.parseInt(cooked[10]));
648 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
649 // FMT: NNN Share method <method> now <available|unavailable>
650 boolean avail = false;
651 if (cooked[5].equals("available")) {
654 notifyShareAvailabilityChange(cooked[3], avail);
655 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
656 (code == VoldResponseCode.VolumeDiskRemoved) ||
657 (code == VoldResponseCode.VolumeBadRemoval)) {
658 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
659 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
660 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
661 final String label = cooked[2];
662 final String path = cooked[3];
667 String devComp = cooked[6].substring(1, cooked[6].length() -1);
668 String[] devTok = devComp.split(":");
669 major = Integer.parseInt(devTok[0]);
670 minor = Integer.parseInt(devTok[1]);
671 } catch (Exception ex) {
672 Slog.e(TAG, "Failed to parse major/minor", ex);
675 if (code == VoldResponseCode.VolumeDiskInserted) {
680 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
681 Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
683 } catch (Exception ex) {
684 Slog.w(TAG, "Failed to mount media on insertion", ex);
688 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
690 * This event gets trumped if we're already in BAD_REMOVAL state
692 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
695 /* Send the media unmounted event first */
696 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
697 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
698 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
699 mContext.sendBroadcast(in);
701 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media removed");
702 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
703 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
704 } else if (code == VoldResponseCode.VolumeBadRemoval) {
705 if (DEBUG_EVENTS) Slog.i(TAG, "Sending unmounted event first");
706 /* Send the media unmounted event first */
707 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
708 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
709 mContext.sendBroadcast(in);
711 if (DEBUG_EVENTS) Slog.i(TAG, "Sending media bad removal");
712 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
713 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
715 Slog.e(TAG, String.format("Unknown code {%d}", code));
722 mContext.sendBroadcast(in);
727 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
728 String vs = getVolumeState(path);
729 if (DEBUG_EVENTS) Slog.i(TAG, "notifyVolumeStateChanged::" + vs);
733 if (oldState == VolumeState.Shared && newState != oldState) {
734 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
735 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
736 Uri.parse("file://" + path)));
739 if (newState == VolumeState.Init) {
740 } else if (newState == VolumeState.NoMedia) {
741 // NoMedia is handled via Disk Remove events
742 } else if (newState == VolumeState.Idle) {
744 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
745 * if we're in the process of enabling UMS
748 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
749 Environment.MEDIA_NOFS) && !vs.equals(
750 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
751 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state for media bad removal nofs and unmountable");
752 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
753 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
755 } else if (newState == VolumeState.Pending) {
756 } else if (newState == VolumeState.Checking) {
757 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state checking");
758 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
759 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
760 } else if (newState == VolumeState.Mounted) {
761 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state mounted");
762 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
763 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
764 in.putExtra("read-only", false);
765 } else if (newState == VolumeState.Unmounting) {
766 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
767 } else if (newState == VolumeState.Formatting) {
768 } else if (newState == VolumeState.Shared) {
769 if (DEBUG_EVENTS) Slog.i(TAG, "Updating volume state media mounted");
770 /* Send the media unmounted event first */
771 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
772 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
773 mContext.sendBroadcast(in);
775 if (DEBUG_EVENTS) Slog.i(TAG, "Updating media shared");
776 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
777 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
778 if (LOCAL_LOGD) Slog.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
779 } else if (newState == VolumeState.SharedMnt) {
780 Slog.e(TAG, "Live shared mounts not supported yet!");
783 Slog.e(TAG, "Unhandled VolumeState {" + newState + "}");
787 mContext.sendBroadcast(in);
791 private boolean doGetShareMethodAvailable(String method) {
792 ArrayList<String> rsp;
794 rsp = mConnector.doCommand("share status " + method);
795 } catch (NativeDaemonConnectorException ex) {
796 Slog.e(TAG, "Failed to determine whether share method " + method + " is available.");
800 for (String line : rsp) {
801 String[] tok = line.split(" ");
802 if (tok.length < 3) {
803 Slog.e(TAG, "Malformed response to share status " + method);
809 code = Integer.parseInt(tok[0]);
810 } catch (NumberFormatException nfe) {
811 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
814 if (code == VoldResponseCode.ShareStatusResult) {
815 if (tok[2].equals("available"))
819 Slog.e(TAG, String.format("Unexpected response code %d", code));
823 Slog.e(TAG, "Got an empty response");
827 private int doMountVolume(String path) {
828 int rc = StorageResultCode.OperationSucceeded;
830 if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
832 mConnector.doCommand(String.format("volume mount %s", path));
833 } catch (NativeDaemonConnectorException e) {
835 * Mount failed for some reason
838 int code = e.getCode();
839 if (code == VoldResponseCode.OpFailedNoMedia) {
841 * Attempt to mount but no media inserted
843 rc = StorageResultCode.OperationFailedNoMedia;
844 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
845 if (DEBUG_EVENTS) Slog.i(TAG, " updating volume state :: media nofs");
847 * Media is blank or does not contain a supported filesystem
849 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
850 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
851 rc = StorageResultCode.OperationFailedMediaBlank;
852 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
853 if (DEBUG_EVENTS) Slog.i(TAG, "updating volume state media corrupt");
855 * Volume consistency check failed
857 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
858 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
859 rc = StorageResultCode.OperationFailedMediaCorrupt;
861 rc = StorageResultCode.OperationFailedInternalError;
865 * Send broadcast intent (if required for the failure)
868 mContext.sendBroadcast(in);
876 * If force is not set, we do not unmount if there are
877 * processes holding references to the volume about to be unmounted.
878 * If force is set, all the processes holding references need to be
879 * killed via the ActivityManager before actually unmounting the volume.
880 * This might even take a while and might be retried after timed delays
881 * to make sure we dont end up in an instable state and kill some core
884 private int doUnmountVolume(String path, boolean force) {
885 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
886 return VoldResponseCode.OpFailedVolNotMounted;
890 * Force a GC to make sure AssetManagers in other threads of the
891 * system_server are cleaned up. We have to do this since AssetManager
892 * instances are kept as a WeakReference and it's possible we have files
893 * open on the external storage.
895 Runtime.getRuntime().gc();
897 // Redundant probably. But no harm in updating state again.
898 mPms.updateExternalMediaStatus(false, false);
900 mConnector.doCommand(String.format(
901 "volume unmount %s%s", path, (force ? " force" : "")));
902 // We unmounted the volume. None of the asec containers are available now.
903 synchronized (mAsecMountSet) {
904 mAsecMountSet.clear();
906 return StorageResultCode.OperationSucceeded;
907 } catch (NativeDaemonConnectorException e) {
908 // Don't worry about mismatch in PackageManager since the
909 // call back will handle the status changes any way.
910 int code = e.getCode();
911 if (code == VoldResponseCode.OpFailedVolNotMounted) {
912 return StorageResultCode.OperationFailedStorageNotMounted;
913 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
914 return StorageResultCode.OperationFailedStorageBusy;
916 return StorageResultCode.OperationFailedInternalError;
921 private int doFormatVolume(String path) {
923 String cmd = String.format("volume format %s", path);
924 mConnector.doCommand(cmd);
925 return StorageResultCode.OperationSucceeded;
926 } catch (NativeDaemonConnectorException e) {
927 int code = e.getCode();
928 if (code == VoldResponseCode.OpFailedNoMedia) {
929 return StorageResultCode.OperationFailedNoMedia;
930 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
931 return StorageResultCode.OperationFailedMediaCorrupt;
933 return StorageResultCode.OperationFailedInternalError;
938 private boolean doGetVolumeShared(String path, String method) {
939 String cmd = String.format("volume shared %s %s", path, method);
940 ArrayList<String> rsp;
943 rsp = mConnector.doCommand(cmd);
944 } catch (NativeDaemonConnectorException ex) {
945 Slog.e(TAG, "Failed to read response to volume shared " + path + " " + method);
949 for (String line : rsp) {
950 String[] tok = line.split(" ");
951 if (tok.length < 3) {
952 Slog.e(TAG, "Malformed response to volume shared " + path + " " + method + " command");
958 code = Integer.parseInt(tok[0]);
959 } catch (NumberFormatException nfe) {
960 Slog.e(TAG, String.format("Error parsing code %s", tok[0]));
963 if (code == VoldResponseCode.ShareEnabledResult) {
964 return "enabled".equals(tok[2]);
966 Slog.e(TAG, String.format("Unexpected response code %d", code));
970 Slog.e(TAG, "Got an empty response");
974 private void notifyShareAvailabilityChange(String method, final boolean avail) {
975 if (!method.equals("ums")) {
976 Slog.w(TAG, "Ignoring unsupported share method {" + method + "}");
980 synchronized (mListeners) {
981 for (int i = mListeners.size() -1; i >= 0; i--) {
982 MountServiceBinderListener bl = mListeners.get(i);
984 bl.mListener.onUsbMassStorageConnectionChanged(avail);
985 } catch (RemoteException rex) {
986 Slog.e(TAG, "Listener dead");
987 mListeners.remove(i);
988 } catch (Exception ex) {
989 Slog.e(TAG, "Listener failed", ex);
994 if (mBooted == true) {
995 sendUmsIntent(avail);
997 mSendUmsConnectedOnBoot = avail;
1000 final String path = Environment.getExternalStorageDirectory().getPath();
1001 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
1003 * USB mass storage disconnected while enabled
1009 Slog.w(TAG, "Disabling UMS after cable disconnect");
1010 doShareUnshareVolume(path, "ums", false);
1011 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
1012 Slog.e(TAG, String.format(
1013 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
1016 } catch (Exception ex) {
1017 Slog.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
1024 private void sendUmsIntent(boolean c) {
1025 mContext.sendBroadcast(
1026 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
1029 private void validatePermission(String perm) {
1030 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
1031 throw new SecurityException(String.format("Requires %s permission", perm));
1036 * Constructs a new MountService instance
1038 * @param context Binder context for this service
1040 public MountService(Context context) {
1043 // XXX: This will go away soon in favor of IMountServiceObserver
1044 mPms = (PackageManagerService) ServiceManager.getService("package");
1046 mContext.registerReceiver(mBroadcastReceiver,
1047 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
1049 mHandlerThread = new HandlerThread("MountService");
1050 mHandlerThread.start();
1051 mHandler = new MountServiceHandler(mHandlerThread.getLooper());
1053 // Add OBB Action Handler to MountService thread.
1054 mObbActionHandler = new ObbActionHandler(mHandlerThread.getLooper());
1057 * Vold does not run in the simulator, so pretend the connector thread
1058 * ran and did its thing.
1060 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
1062 mUmsEnabling = true;
1067 * Create the connection to vold with a maximum queue of twice the
1068 * amount of containers we'd ever expect to have. This keeps an
1069 * "asec list" from blocking a thread repeatedly.
1071 mConnector = new NativeDaemonConnector(this, "vold",
1072 PackageManagerService.MAX_CONTAINERS * 2, VOLD_TAG);
1074 Thread thread = new Thread(mConnector, VOLD_TAG);
1079 * Exposed API calls below here
1082 public void registerListener(IMountServiceListener listener) {
1083 synchronized (mListeners) {
1084 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
1086 listener.asBinder().linkToDeath(bl, 0);
1088 } catch (RemoteException rex) {
1089 Slog.e(TAG, "Failed to link to listener death");
1094 public void unregisterListener(IMountServiceListener listener) {
1095 synchronized (mListeners) {
1096 for(MountServiceBinderListener bl : mListeners) {
1097 if (bl.mListener == listener) {
1098 mListeners.remove(mListeners.indexOf(bl));
1105 public void shutdown(final IMountShutdownObserver observer) {
1106 validatePermission(android.Manifest.permission.SHUTDOWN);
1108 Slog.i(TAG, "Shutting down");
1110 String path = Environment.getExternalStorageDirectory().getPath();
1111 String state = getVolumeState(path);
1113 if (state.equals(Environment.MEDIA_SHARED)) {
1115 * If the media is currently shared, unshare it.
1116 * XXX: This is still dangerous!. We should not
1117 * be rebooting at *all* if UMS is enabled, since
1118 * the UMS host could have dirty FAT cache entries
1121 setUsbMassStorageEnabled(false);
1122 } else if (state.equals(Environment.MEDIA_CHECKING)) {
1124 * If the media is being checked, then we need to wait for
1125 * it to complete before being able to proceed.
1127 // XXX: @hackbod - Should we disable the ANR timer here?
1129 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
1132 } catch (InterruptedException iex) {
1133 Slog.e(TAG, "Interrupted while waiting for media", iex);
1136 state = Environment.getExternalStorageState();
1139 Slog.e(TAG, "Timed out waiting for media to check");
1143 if (state.equals(Environment.MEDIA_MOUNTED)) {
1144 // Post a unmount message.
1145 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
1146 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
1150 private boolean getUmsEnabling() {
1151 synchronized (mListeners) {
1152 return mUmsEnabling;
1156 private void setUmsEnabling(boolean enable) {
1157 synchronized (mListeners) {
1158 mUmsEnabling = enable;
1162 public boolean isUsbMassStorageConnected() {
1165 if (getUmsEnabling()) {
1168 return doGetShareMethodAvailable("ums");
1171 public void setUsbMassStorageEnabled(boolean enable) {
1173 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1175 // TODO: Add support for multiple share methods
1178 * If the volume is mounted and we're enabling then unmount it
1180 String path = Environment.getExternalStorageDirectory().getPath();
1181 String vs = getVolumeState(path);
1182 String method = "ums";
1183 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
1184 // Override for isUsbMassStorageEnabled()
1185 setUmsEnabling(enable);
1186 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
1187 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
1189 setUmsEnabling(false);
1192 * If we disabled UMS then mount the volume
1195 doShareUnshareVolume(path, method, enable);
1196 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
1197 Slog.e(TAG, "Failed to remount " + path +
1198 " after disabling share method " + method);
1200 * Even though the mount failed, the unshare didn't so don't indicate an error.
1201 * The mountVolume() call will have set the storage state and sent the necessary
1208 public boolean isUsbMassStorageEnabled() {
1210 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
1214 * @return state of the volume at the specified mount point
1216 public String getVolumeState(String mountPoint) {
1218 * XXX: Until we have multiple volume discovery, just hardwire
1221 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
1222 Slog.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
1223 throw new IllegalArgumentException();
1226 return mLegacyState;
1229 public int mountVolume(String path) {
1230 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1233 return doMountVolume(path);
1236 public void unmountVolume(String path, boolean force) {
1237 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1240 String volState = getVolumeState(path);
1241 if (DEBUG_UNMOUNT) Slog.i(TAG, "Unmounting " + path + " force = " + force);
1242 if (Environment.MEDIA_UNMOUNTED.equals(volState) ||
1243 Environment.MEDIA_REMOVED.equals(volState) ||
1244 Environment.MEDIA_SHARED.equals(volState) ||
1245 Environment.MEDIA_UNMOUNTABLE.equals(volState)) {
1246 // Media already unmounted or cannot be unmounted.
1247 // TODO return valid return code when adding observer call back.
1250 UnmountCallBack ucb = new UnmountCallBack(path, force);
1251 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
1254 public int formatVolume(String path) {
1255 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
1258 return doFormatVolume(path);
1261 public int []getStorageUsers(String path) {
1262 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1265 String[] r = mConnector.doListCommand(
1266 String.format("storage users %s", path),
1267 VoldResponseCode.StorageUsersListResult);
1268 // FMT: <pid> <process name>
1269 int[] data = new int[r.length];
1270 for (int i = 0; i < r.length; i++) {
1271 String []tok = r[i].split(" ");
1273 data[i] = Integer.parseInt(tok[0]);
1274 } catch (NumberFormatException nfe) {
1275 Slog.e(TAG, String.format("Error parsing pid %s", tok[0]));
1280 } catch (NativeDaemonConnectorException e) {
1281 Slog.e(TAG, "Failed to retrieve storage users list", e);
1286 private void warnOnNotMounted() {
1287 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
1288 Slog.w(TAG, "getSecureContainerList() called when storage not mounted");
1292 public String[] getSecureContainerList() {
1293 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1298 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1299 } catch (NativeDaemonConnectorException e) {
1300 return new String[0];
1304 public int createSecureContainer(String id, int sizeMb, String fstype,
1305 String key, int ownerUid) {
1306 validatePermission(android.Manifest.permission.ASEC_CREATE);
1310 int rc = StorageResultCode.OperationSucceeded;
1311 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1313 mConnector.doCommand(cmd);
1314 } catch (NativeDaemonConnectorException e) {
1315 rc = StorageResultCode.OperationFailedInternalError;
1318 if (rc == StorageResultCode.OperationSucceeded) {
1319 synchronized (mAsecMountSet) {
1320 mAsecMountSet.add(id);
1326 public int finalizeSecureContainer(String id) {
1327 validatePermission(android.Manifest.permission.ASEC_CREATE);
1330 int rc = StorageResultCode.OperationSucceeded;
1332 mConnector.doCommand(String.format("asec finalize %s", id));
1334 * Finalization does a remount, so no need
1335 * to update mAsecMountSet
1337 } catch (NativeDaemonConnectorException e) {
1338 rc = StorageResultCode.OperationFailedInternalError;
1343 public int destroySecureContainer(String id, boolean force) {
1344 validatePermission(android.Manifest.permission.ASEC_DESTROY);
1349 * Force a GC to make sure AssetManagers in other threads of the
1350 * system_server are cleaned up. We have to do this since AssetManager
1351 * instances are kept as a WeakReference and it's possible we have files
1352 * open on the external storage.
1354 Runtime.getRuntime().gc();
1356 int rc = StorageResultCode.OperationSucceeded;
1358 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
1359 } catch (NativeDaemonConnectorException e) {
1360 int code = e.getCode();
1361 if (code == VoldResponseCode.OpFailedStorageBusy) {
1362 rc = StorageResultCode.OperationFailedStorageBusy;
1364 rc = StorageResultCode.OperationFailedInternalError;
1368 if (rc == StorageResultCode.OperationSucceeded) {
1369 synchronized (mAsecMountSet) {
1370 if (mAsecMountSet.contains(id)) {
1371 mAsecMountSet.remove(id);
1379 public int mountSecureContainer(String id, String key, int ownerUid) {
1380 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
1384 synchronized (mAsecMountSet) {
1385 if (mAsecMountSet.contains(id)) {
1386 return StorageResultCode.OperationFailedStorageMounted;
1390 int rc = StorageResultCode.OperationSucceeded;
1391 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1393 mConnector.doCommand(cmd);
1394 } catch (NativeDaemonConnectorException e) {
1395 int code = e.getCode();
1396 if (code != VoldResponseCode.OpFailedStorageBusy) {
1397 rc = StorageResultCode.OperationFailedInternalError;
1401 if (rc == StorageResultCode.OperationSucceeded) {
1402 synchronized (mAsecMountSet) {
1403 mAsecMountSet.add(id);
1409 public int unmountSecureContainer(String id, boolean force) {
1410 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
1414 synchronized (mAsecMountSet) {
1415 if (!mAsecMountSet.contains(id)) {
1416 return StorageResultCode.OperationFailedStorageNotMounted;
1421 * Force a GC to make sure AssetManagers in other threads of the
1422 * system_server are cleaned up. We have to do this since AssetManager
1423 * instances are kept as a WeakReference and it's possible we have files
1424 * open on the external storage.
1426 Runtime.getRuntime().gc();
1428 int rc = StorageResultCode.OperationSucceeded;
1429 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
1431 mConnector.doCommand(cmd);
1432 } catch (NativeDaemonConnectorException e) {
1433 int code = e.getCode();
1434 if (code == VoldResponseCode.OpFailedStorageBusy) {
1435 rc = StorageResultCode.OperationFailedStorageBusy;
1437 rc = StorageResultCode.OperationFailedInternalError;
1441 if (rc == StorageResultCode.OperationSucceeded) {
1442 synchronized (mAsecMountSet) {
1443 mAsecMountSet.remove(id);
1449 public boolean isSecureContainerMounted(String id) {
1450 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1454 synchronized (mAsecMountSet) {
1455 return mAsecMountSet.contains(id);
1459 public int renameSecureContainer(String oldId, String newId) {
1460 validatePermission(android.Manifest.permission.ASEC_RENAME);
1464 synchronized (mAsecMountSet) {
1466 * Because a mounted container has active internal state which cannot be
1467 * changed while active, we must ensure both ids are not currently mounted.
1469 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
1470 return StorageResultCode.OperationFailedStorageMounted;
1474 int rc = StorageResultCode.OperationSucceeded;
1475 String cmd = String.format("asec rename %s %s", oldId, newId);
1477 mConnector.doCommand(cmd);
1478 } catch (NativeDaemonConnectorException e) {
1479 rc = StorageResultCode.OperationFailedInternalError;
1485 public String getSecureContainerPath(String id) {
1486 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1491 ArrayList<String> rsp = mConnector.doCommand(String.format("asec path %s", id));
1492 String []tok = rsp.get(0).split(" ");
1493 int code = Integer.parseInt(tok[0]);
1494 if (code != VoldResponseCode.AsecPathResult) {
1495 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1498 } catch (NativeDaemonConnectorException e) {
1499 int code = e.getCode();
1500 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1501 throw new IllegalArgumentException(String.format("Container '%s' not found", id));
1503 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1508 public void finishMediaUpdate() {
1509 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
1512 private boolean isUidOwnerOfPackageOrSystem(String packageName, int callerUid) {
1513 if (callerUid == android.os.Process.SYSTEM_UID) {
1517 if (packageName == null) {
1521 final int packageUid = mPms.getPackageUid(packageName);
1524 Slog.d(TAG, "packageName = " + packageName + ", packageUid = " +
1525 packageUid + ", callerUid = " + callerUid);
1528 return callerUid == packageUid;
1531 public String getMountedObbPath(String filename) {
1532 if (filename == null) {
1533 throw new IllegalArgumentException("filename cannot be null");
1540 ArrayList<String> rsp = mConnector.doCommand(String.format("obb path %s", filename));
1541 String []tok = rsp.get(0).split(" ");
1542 int code = Integer.parseInt(tok[0]);
1543 if (code != VoldResponseCode.AsecPathResult) {
1544 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1547 } catch (NativeDaemonConnectorException e) {
1548 int code = e.getCode();
1549 if (code == VoldResponseCode.OpFailedStorageNotFound) {
1552 throw new IllegalStateException(String.format("Unexpected response code %d", code));
1557 public boolean isObbMounted(String filename) {
1558 if (filename == null) {
1559 throw new IllegalArgumentException("filename cannot be null");
1562 synchronized (mObbMounts) {
1563 return mObbPathToStateMap.containsKey(filename);
1567 public void mountObb(String filename, String key, IObbActionListener token, int nonce)
1568 throws RemoteException {
1569 if (filename == null) {
1570 throw new IllegalArgumentException("filename cannot be null");
1573 if (token == null) {
1574 throw new IllegalArgumentException("token cannot be null");
1577 final int callerUid = Binder.getCallingUid();
1578 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1579 final ObbAction action = new MountObbAction(obbState, key);
1580 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1583 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1586 public void unmountObb(String filename, boolean force, IObbActionListener token, int nonce)
1587 throws RemoteException {
1588 if (filename == null) {
1589 throw new IllegalArgumentException("filename cannot be null");
1592 final int callerUid = Binder.getCallingUid();
1593 final ObbState obbState = new ObbState(filename, callerUid, token, nonce);
1594 final ObbAction action = new UnmountObbAction(obbState, force);
1595 mObbActionHandler.sendMessage(mObbActionHandler.obtainMessage(OBB_RUN_ACTION, action));
1598 Slog.i(TAG, "Send to OBB handler: " + action.toString());
1601 private void addObbStateLocked(ObbState obbState) throws RemoteException {
1602 final IBinder binder = obbState.getBinder();
1603 List<ObbState> obbStates = mObbMounts.get(binder);
1605 if (obbStates == null) {
1606 obbStates = new ArrayList<ObbState>();
1607 mObbMounts.put(binder, obbStates);
1609 for (final ObbState o : obbStates) {
1610 if (o.filename.equals(obbState.filename)) {
1611 throw new IllegalStateException("Attempt to add ObbState twice. "
1612 + "This indicates an error in the MountService logic.");
1617 obbStates.add(obbState);
1620 } catch (RemoteException e) {
1622 * The binder died before we could link it, so clean up our state
1623 * and return failure.
1625 obbStates.remove(obbState);
1626 if (obbStates.isEmpty()) {
1627 mObbMounts.remove(binder);
1630 // Rethrow the error so mountObb can get it
1634 mObbPathToStateMap.put(obbState.filename, obbState);
1637 private void removeObbStateLocked(ObbState obbState) {
1638 final IBinder binder = obbState.getBinder();
1639 final List<ObbState> obbStates = mObbMounts.get(binder);
1640 if (obbStates != null) {
1641 if (obbStates.remove(obbState)) {
1644 if (obbStates.isEmpty()) {
1645 mObbMounts.remove(binder);
1649 mObbPathToStateMap.remove(obbState.filename);
1652 private class ObbActionHandler extends Handler {
1653 private boolean mBound = false;
1654 private final List<ObbAction> mActions = new LinkedList<ObbAction>();
1656 ObbActionHandler(Looper l) {
1661 public void handleMessage(Message msg) {
1663 case OBB_RUN_ACTION: {
1664 final ObbAction action = (ObbAction) msg.obj;
1667 Slog.i(TAG, "OBB_RUN_ACTION: " + action.toString());
1669 // If a bind was already initiated we don't really
1670 // need to do anything. The pending install
1671 // will be processed later on.
1673 // If this is the only one pending we might
1674 // have to bind to the service again.
1675 if (!connectToService()) {
1676 Slog.e(TAG, "Failed to bind to media container service");
1677 action.handleError();
1682 mActions.add(action);
1685 case OBB_MCS_BOUND: {
1687 Slog.i(TAG, "OBB_MCS_BOUND");
1688 if (msg.obj != null) {
1689 mContainerService = (IMediaContainerService) msg.obj;
1691 if (mContainerService == null) {
1692 // Something seriously wrong. Bail out
1693 Slog.e(TAG, "Cannot bind to media container service");
1694 for (ObbAction action : mActions) {
1695 // Indicate service bind error
1696 action.handleError();
1699 } else if (mActions.size() > 0) {
1700 final ObbAction action = mActions.get(0);
1701 if (action != null) {
1702 action.execute(this);
1705 // Should never happen ideally.
1706 Slog.w(TAG, "Empty queue");
1710 case OBB_MCS_RECONNECT: {
1712 Slog.i(TAG, "OBB_MCS_RECONNECT");
1713 if (mActions.size() > 0) {
1715 disconnectService();
1717 if (!connectToService()) {
1718 Slog.e(TAG, "Failed to bind to media container service");
1719 for (ObbAction action : mActions) {
1720 // Indicate service bind error
1721 action.handleError();
1728 case OBB_MCS_UNBIND: {
1730 Slog.i(TAG, "OBB_MCS_UNBIND");
1732 // Delete pending install
1733 if (mActions.size() > 0) {
1736 if (mActions.size() == 0) {
1738 disconnectService();
1741 // There are more pending requests in queue.
1742 // Just post MCS_BOUND message to trigger processing
1743 // of next pending install.
1744 mObbActionHandler.sendEmptyMessage(OBB_MCS_BOUND);
1748 case OBB_FLUSH_MOUNT_STATE: {
1749 final String path = (String) msg.obj;
1752 Slog.i(TAG, "Flushing all OBB state for path " + path);
1754 synchronized (mObbMounts) {
1755 final List<ObbState> obbStatesToRemove = new LinkedList<ObbState>();
1757 final Iterator<Entry<String, ObbState>> i =
1758 mObbPathToStateMap.entrySet().iterator();
1759 while (i.hasNext()) {
1760 final Entry<String, ObbState> obbEntry = i.next();
1763 * If this entry's source file is in the volume path
1764 * that got unmounted, remove it because it's no
1767 if (obbEntry.getKey().startsWith(path)) {
1768 obbStatesToRemove.add(obbEntry.getValue());
1772 for (final ObbState obbState : obbStatesToRemove) {
1774 Slog.i(TAG, "Removing state for " + obbState.filename);
1776 removeObbStateLocked(obbState);
1779 obbState.token.onObbResult(obbState.filename, obbState.nonce,
1780 OnObbStateChangeListener.UNMOUNTED);
1781 } catch (RemoteException e) {
1782 Slog.i(TAG, "Couldn't send unmount notification for OBB: "
1783 + obbState.filename);
1792 private boolean connectToService() {
1794 Slog.i(TAG, "Trying to bind to DefaultContainerService");
1796 Intent service = new Intent().setComponent(DEFAULT_CONTAINER_COMPONENT);
1797 if (mContext.bindService(service, mDefContainerConn, Context.BIND_AUTO_CREATE)) {
1804 private void disconnectService() {
1805 mContainerService = null;
1807 mContext.unbindService(mDefContainerConn);
1811 abstract class ObbAction {
1812 private static final int MAX_RETRIES = 3;
1813 private int mRetries;
1817 ObbAction(ObbState obbState) {
1818 mObbState = obbState;
1821 public void execute(ObbActionHandler handler) {
1824 Slog.i(TAG, "Starting to execute action: " + this.toString());
1826 if (mRetries > MAX_RETRIES) {
1827 Slog.w(TAG, "Failed to invoke remote methods on default container service. Giving up");
1828 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1834 Slog.i(TAG, "Posting install MCS_UNBIND");
1835 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1837 } catch (RemoteException e) {
1839 Slog.i(TAG, "Posting install MCS_RECONNECT");
1840 mObbActionHandler.sendEmptyMessage(OBB_MCS_RECONNECT);
1841 } catch (Exception e) {
1843 Slog.d(TAG, "Error handling OBB action", e);
1845 mObbActionHandler.sendEmptyMessage(OBB_MCS_UNBIND);
1849 abstract void handleExecute() throws RemoteException, IOException;
1850 abstract void handleError();
1852 protected ObbInfo getObbInfo() throws IOException {
1855 obbInfo = mContainerService.getObbInfo(mObbState.filename);
1856 } catch (RemoteException e) {
1857 Slog.d(TAG, "Couldn't call DefaultContainerService to fetch OBB info for "
1858 + mObbState.filename);
1861 if (obbInfo == null) {
1862 throw new IOException("Couldn't read OBB file: " + mObbState.filename);
1867 protected void sendNewStatusOrIgnore(int status) {
1868 if (mObbState == null || mObbState.token == null) {
1873 mObbState.token.onObbResult(mObbState.filename, mObbState.nonce, status);
1874 } catch (RemoteException e) {
1875 Slog.w(TAG, "MountServiceListener went away while calling onObbStateChanged");
1880 class MountObbAction extends ObbAction {
1881 private String mKey;
1883 MountObbAction(ObbState obbState, String key) {
1888 public void handleExecute() throws IOException, RemoteException {
1892 final ObbInfo obbInfo = getObbInfo();
1894 if (!isUidOwnerOfPackageOrSystem(obbInfo.packageName, mObbState.callerUid)) {
1895 Slog.w(TAG, "Denied attempt to mount OBB " + obbInfo.filename
1896 + " which is owned by " + obbInfo.packageName);
1897 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
1901 final boolean isMounted;
1902 synchronized (mObbMounts) {
1903 isMounted = mObbPathToStateMap.containsKey(obbInfo.filename);
1906 Slog.w(TAG, "Attempt to mount OBB which is already mounted: " + obbInfo.filename);
1907 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);
1912 * The filename passed in might not be the canonical name, so just
1913 * set the filename to the canonicalized version.
1915 mObbState.filename = obbInfo.filename;
1917 final String hashedKey;
1922 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
1924 KeySpec ks = new PBEKeySpec(mKey.toCharArray(), obbInfo.salt,
1925 PBKDF2_HASH_ROUNDS, CRYPTO_ALGORITHM_KEY_SIZE);
1926 SecretKey key = factory.generateSecret(ks);
1927 BigInteger bi = new BigInteger(key.getEncoded());
1928 hashedKey = bi.toString(16);
1929 } catch (NoSuchAlgorithmException e) {
1930 Slog.e(TAG, "Could not load PBKDF2 algorithm", e);
1931 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
1933 } catch (InvalidKeySpecException e) {
1934 Slog.e(TAG, "Invalid key spec when loading PBKDF2 algorithm", e);
1935 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
1940 int rc = StorageResultCode.OperationSucceeded;
1941 String cmd = String.format("obb mount %s %s %d", mObbState.filename, hashedKey,
1942 mObbState.callerUid);
1944 mConnector.doCommand(cmd);
1945 } catch (NativeDaemonConnectorException e) {
1946 int code = e.getCode();
1947 if (code != VoldResponseCode.OpFailedStorageBusy) {
1948 rc = StorageResultCode.OperationFailedInternalError;
1952 if (rc == StorageResultCode.OperationSucceeded) {
1954 Slog.d(TAG, "Successfully mounted OBB " + mObbState.filename);
1956 synchronized (mObbMounts) {
1957 addObbStateLocked(mObbState);
1960 sendNewStatusOrIgnore(OnObbStateChangeListener.MOUNTED);
1962 Slog.e(TAG, "Couldn't mount OBB file: " + rc);
1964 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
1968 public void handleError() {
1969 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
1973 public String toString() {
1974 StringBuilder sb = new StringBuilder();
1975 sb.append("MountObbAction{");
1976 sb.append("filename=");
1977 sb.append(mObbState.filename);
1978 sb.append(",callerUid=");
1979 sb.append(mObbState.callerUid);
1980 sb.append(",token=");
1981 sb.append(mObbState.token != null ? mObbState.token.toString() : "NULL");
1982 sb.append(",binder=");
1983 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
1985 return sb.toString();
1989 class UnmountObbAction extends ObbAction {
1990 private boolean mForceUnmount;
1992 UnmountObbAction(ObbState obbState, boolean force) {
1994 mForceUnmount = force;
1997 public void handleExecute() throws IOException {
2001 final ObbInfo obbInfo = getObbInfo();
2003 final ObbState obbState;
2004 synchronized (mObbMounts) {
2005 obbState = mObbPathToStateMap.get(obbInfo.filename);
2008 if (obbState == null) {
2009 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_NOT_MOUNTED);
2013 if (obbState.callerUid != mObbState.callerUid) {
2014 Slog.w(TAG, "Permission denied attempting to unmount OBB " + obbInfo.filename
2015 + " (owned by " + obbInfo.packageName + ")");
2016 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
2020 mObbState.filename = obbInfo.filename;
2022 int rc = StorageResultCode.OperationSucceeded;
2023 String cmd = String.format("obb unmount %s%s", mObbState.filename,
2024 (mForceUnmount ? " force" : ""));
2026 mConnector.doCommand(cmd);
2027 } catch (NativeDaemonConnectorException e) {
2028 int code = e.getCode();
2029 if (code == VoldResponseCode.OpFailedStorageBusy) {
2030 rc = StorageResultCode.OperationFailedStorageBusy;
2031 } else if (code == VoldResponseCode.OpFailedStorageNotFound) {
2032 // If it's not mounted then we've already won.
2033 rc = StorageResultCode.OperationSucceeded;
2035 rc = StorageResultCode.OperationFailedInternalError;
2039 if (rc == StorageResultCode.OperationSucceeded) {
2040 synchronized (mObbMounts) {
2041 removeObbStateLocked(obbState);
2044 sendNewStatusOrIgnore(OnObbStateChangeListener.UNMOUNTED);
2046 Slog.w(TAG, "Could not mount OBB: " + mObbState.filename);
2047 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_COULD_NOT_UNMOUNT);
2051 public void handleError() {
2052 sendNewStatusOrIgnore(OnObbStateChangeListener.ERROR_INTERNAL);
2056 public String toString() {
2057 StringBuilder sb = new StringBuilder();
2058 sb.append("UnmountObbAction{");
2059 sb.append("filename=");
2060 sb.append(mObbState.filename != null ? mObbState.filename : "null");
2061 sb.append(",force=");
2062 sb.append(mForceUnmount);
2063 sb.append(",callerUid=");
2064 sb.append(mObbState.callerUid);
2065 sb.append(",token=");
2066 sb.append(mObbState.token != null ? mObbState.token.toString() : "null");
2067 sb.append(",binder=");
2068 sb.append(mObbState.token != null ? mObbState.getBinder().toString() : "null");
2070 return sb.toString();
2075 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2076 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
2077 pw.println("Permission Denial: can't dump ActivityManager from from pid="
2078 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
2079 + " without permission " + android.Manifest.permission.DUMP);
2083 synchronized (mObbMounts) {
2084 pw.println(" mObbMounts:");
2086 final Iterator<Entry<IBinder, List<ObbState>>> binders = mObbMounts.entrySet().iterator();
2087 while (binders.hasNext()) {
2088 Entry<IBinder, List<ObbState>> e = binders.next();
2089 pw.print(" Key="); pw.println(e.getKey().toString());
2090 final List<ObbState> obbStates = e.getValue();
2091 for (final ObbState obbState : obbStates) {
2092 pw.print(" "); pw.println(obbState.toString());
2097 pw.println(" mObbPathToStateMap:");
2098 final Iterator<Entry<String, ObbState>> maps = mObbPathToStateMap.entrySet().iterator();
2099 while (maps.hasNext()) {
2100 final Entry<String, ObbState> e = maps.next();
2101 pw.print(" "); pw.print(e.getKey());
2102 pw.print(" -> "); pw.println(e.getValue().toString());