blob: d4d53366a78492d1f64f3aead0f628302eb81569 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.net.Uri;
San Mehatb1043402010-02-05 08:26:50 -080027import android.os.storage.IMountService;
28import android.os.storage.IMountServiceListener;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080029import android.os.storage.IMountShutdownObserver;
San Mehatb1043402010-02-05 08:26:50 -080030import android.os.storage.StorageResultCode;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080031import android.os.Handler;
32import android.os.Message;
San Mehat4270e1e2010-01-29 05:32:19 -080033import android.os.RemoteException;
34import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Environment;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080036import android.os.ServiceManager;
San Mehat207e5382010-02-04 20:46:54 -080037import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.util.Log;
San Mehat22dd86e2010-01-12 12:21:18 -080040import java.util.ArrayList;
San Mehat6cdd9c02010-02-09 14:45:20 -080041import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043/**
San Mehatb1043402010-02-05 08:26:50 -080044 * MountService implements back-end services for platform storage
45 * management.
46 * @hide - Applications should use android.os.storage.StorageManager
47 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 */
San Mehat22dd86e2010-01-12 12:21:18 -080049class MountService extends IMountService.Stub
50 implements INativeDaemonConnectorCallbacks {
San Mehatb1043402010-02-05 08:26:50 -080051 private static final boolean LOCAL_LOGD = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53 private static final String TAG = "MountService";
54
San Mehat4270e1e2010-01-29 05:32:19 -080055 /*
56 * Internal vold volume state constants
57 */
San Mehat7fd0fee2009-12-17 07:12:23 -080058 class VolumeState {
59 public static final int Init = -1;
60 public static final int NoMedia = 0;
61 public static final int Idle = 1;
62 public static final int Pending = 2;
63 public static final int Checking = 3;
64 public static final int Mounted = 4;
65 public static final int Unmounting = 5;
66 public static final int Formatting = 6;
67 public static final int Shared = 7;
68 public static final int SharedMnt = 8;
69 }
70
San Mehat4270e1e2010-01-29 05:32:19 -080071 /*
72 * Internal vold response code constants
73 */
San Mehat22dd86e2010-01-12 12:21:18 -080074 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -080075 /*
76 * 100 series - Requestion action was initiated; expect another reply
77 * before proceeding with a new command.
78 */
San Mehat22dd86e2010-01-12 12:21:18 -080079 public static final int VolumeListResult = 110;
80 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -080081 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -080082
San Mehat4270e1e2010-01-29 05:32:19 -080083 /*
84 * 200 series - Requestion action has been successfully completed.
85 */
86 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -080087 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -080088 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -080089
San Mehat4270e1e2010-01-29 05:32:19 -080090 /*
91 * 400 series - Command was accepted, but the requested action
92 * did not take place.
93 */
94 public static final int OpFailedNoMedia = 401;
95 public static final int OpFailedMediaBlank = 402;
96 public static final int OpFailedMediaCorrupt = 403;
97 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -080098 public static final int OpFailedStorageBusy = 405;
San Mehat4270e1e2010-01-29 05:32:19 -080099
100 /*
101 * 600 series - Unsolicited broadcasts.
102 */
San Mehat22dd86e2010-01-12 12:21:18 -0800103 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800104 public static final int ShareAvailabilityChange = 620;
105 public static final int VolumeDiskInserted = 630;
106 public static final int VolumeDiskRemoved = 631;
107 public static final int VolumeBadRemoval = 632;
108 }
109
San Mehat4270e1e2010-01-29 05:32:19 -0800110 private Context mContext;
111 private NativeDaemonConnector mConnector;
112 private String mLegacyState = Environment.MEDIA_REMOVED;
113 private PackageManagerService mPms;
114 private boolean mUmsEnabling;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800115 // Used as a lock for methods that register/unregister listeners.
116 final private ArrayList<MountServiceBinderListener> mListeners =
117 new ArrayList<MountServiceBinderListener>();
San Mehat6a965af22010-02-24 17:47:30 -0800118 private boolean mBooted = false;
119 private boolean mReady = false;
120 private boolean mSendUmsConnectedOnBoot = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800121
San Mehat6cdd9c02010-02-09 14:45:20 -0800122 /**
123 * Private hash of currently mounted secure containers.
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800124 * Used as a lock in methods to manipulate secure containers.
San Mehat6cdd9c02010-02-09 14:45:20 -0800125 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800126 final private HashSet<String> mAsecMountSet = new HashSet<String>();
San Mehat6cdd9c02010-02-09 14:45:20 -0800127
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800128 private static final int H_UNMOUNT_PM_UPDATE = 1;
129 private static final int H_UNMOUNT_PM_DONE = 2;
130 private static final int H_UNMOUNT_MS = 3;
131 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
132 private static final int MAX_UNMOUNT_RETRIES = 4;
133
134 private IntentFilter mPmFilter = new IntentFilter(
135 Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
136 private BroadcastReceiver mPmReceiver = new BroadcastReceiver() {
137 public void onReceive(Context context, Intent intent) {
138 String action = intent.getAction();
139 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
140 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
141 }
142 }
143 };
144
145 class UnmountCallBack {
146 String path;
147 int retries;
148 boolean force;
149
150 UnmountCallBack(String path, boolean force) {
151 retries = 0;
152 this.path = path;
153 this.force = force;
154 }
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800155
156 void handleFinished() {
157 doUnmountVolume(path, true);
158 }
159 }
160
161 class UmsEnableCallBack extends UnmountCallBack {
162 String method;
163
164 UmsEnableCallBack(String path, String method, boolean force) {
165 super(path, force);
166 this.method = method;
167 }
168
169 @Override
170 void handleFinished() {
171 super.handleFinished();
172 doShareUnshareVolume(path, method, true);
173 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800174 }
175
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800176 class ShutdownCallBack extends UnmountCallBack {
177 IMountShutdownObserver observer;
178 ShutdownCallBack(String path, IMountShutdownObserver observer) {
179 super(path, true);
180 this.observer = observer;
181 }
182
183 @Override
184 void handleFinished() {
185 int ret = doUnmountVolume(path, true);
186 if (observer != null) {
187 try {
188 observer.onShutDownComplete(ret);
189 } catch (RemoteException e) {
190 Log.w(TAG, "RemoteException when shutting down");
191 }
192 }
193 }
194 }
195
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800196 final private Handler mHandler = new Handler() {
197 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800198 boolean mRegistered = false;
199
200 void registerReceiver() {
201 mRegistered = true;
202 mContext.registerReceiver(mPmReceiver, mPmFilter);
203 }
204
205 void unregisterReceiver() {
206 mRegistered = false;
207 mContext.unregisterReceiver(mPmReceiver);
208 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800209
210 public void handleMessage(Message msg) {
211 switch (msg.what) {
212 case H_UNMOUNT_PM_UPDATE: {
213 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
214 mForceUnmounts.add(ucb);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800215 // Register only if needed.
216 if (!mRegistered) {
217 registerReceiver();
218 boolean hasExtPkgs = mPms.updateExternalMediaStatus(false);
219 if (!hasExtPkgs) {
220 // Unregister right away
221 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
222 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800223 }
224 break;
225 }
226 case H_UNMOUNT_PM_DONE: {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800227 // Unregister now.
228 if (mRegistered) {
229 unregisterReceiver();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800230 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800231 int size = mForceUnmounts.size();
232 int sizeArr[] = new int[size];
233 int sizeArrN = 0;
234 for (int i = 0; i < size; i++) {
235 UnmountCallBack ucb = mForceUnmounts.get(i);
236 String path = ucb.path;
237 boolean done = false;
238 if (!ucb.force) {
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800239 done = true;
240 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800241 int pids[] = getStorageUsers(path);
242 if (pids == null || pids.length == 0) {
243 done = true;
244 } else {
245 // Kill processes holding references first
246 ActivityManagerService ams = (ActivityManagerService)
247 ServiceManager.getService("activity");
248 // Eliminate system process here?
249 boolean ret = ams.killPidsForMemory(pids);
250 if (ret) {
251 // Confirm if file references have been freed.
252 pids = getStorageUsers(path);
253 if (pids == null || pids.length == 0) {
254 done = true;
255 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800256 }
257 }
258 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800259 if (done) {
260 sizeArr[sizeArrN++] = i;
261 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
262 ucb));
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800263 } else {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800264 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
265 Log.i(TAG, "Cannot unmount inspite of " +
266 MAX_UNMOUNT_RETRIES + " to unmount media");
267 // Send final broadcast indicating failure to unmount.
268 } else {
269 mHandler.sendMessageDelayed(
270 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
271 ucb.retries++),
272 RETRY_UNMOUNT_DELAY);
273 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800274 }
275 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800276 // Remove already processed elements from list.
277 for (int i = (sizeArrN-1); i >= 0; i--) {
278 mForceUnmounts.remove(sizeArr[i]);
279 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800280 break;
281 }
282 case H_UNMOUNT_MS : {
283 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800284 ucb.handleFinished();
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800285 break;
286 }
287 }
288 }
289 };
290
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 }
299 Log.w(TAG, "Waiting too long for mReady!");
300 }
San Mehat1f6301e2010-01-07 22:40:27 -0800301 }
302
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();
322 if (getVolumeState(
323 Environment.getExternalStorageDirectory().getPath()).equals(
324 Environment.MEDIA_UNMOUNTED)) {
325 int rc = doMountVolume(path);
326 if (rc != StorageResultCode.OperationSucceeded) {
327 Log.e(TAG, String.format("Boot-time mount failed (%d)", rc));
328 }
329 }
San Mehat6a965af22010-02-24 17:47:30 -0800330 /*
331 * If UMS is connected in boot, send the connected event
332 * now that we're up.
333 */
334 if (mSendUmsConnectedOnBoot) {
335 sendUmsIntent(true);
336 mSendUmsConnectedOnBoot = false;
337 }
San Mehatfafb0412010-02-18 19:40:04 -0800338 } catch (Exception ex) {
339 Log.e(TAG, "Boot-time mount exception", ex);
340 }
San Mehat207e5382010-02-04 20:46:54 -0800341 }
San Mehatfafb0412010-02-18 19:40:04 -0800342 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
344 }
345 };
346
San Mehat4270e1e2010-01-29 05:32:19 -0800347 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
348 final IMountServiceListener mListener;
349
350 MountServiceBinderListener(IMountServiceListener listener) {
351 mListener = listener;
352
San Mehat91c77612010-01-07 10:39:41 -0800353 }
354
San Mehat4270e1e2010-01-29 05:32:19 -0800355 public void binderDied() {
San Mehatb1043402010-02-05 08:26:50 -0800356 if (LOCAL_LOGD) Log.d(TAG, "An IMountServiceListener has died!");
San Mehat4270e1e2010-01-29 05:32:19 -0800357 synchronized(mListeners) {
358 mListeners.remove(this);
359 mListener.asBinder().unlinkToDeath(this, 0);
360 }
361 }
362 }
363
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800364 private void doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800365 // TODO: Add support for multiple share methods
366 if (!method.equals("ums")) {
367 throw new IllegalArgumentException(String.format("Method %s not supported", method));
368 }
369
San Mehat4270e1e2010-01-29 05:32:19 -0800370 try {
371 mConnector.doCommand(String.format(
372 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
373 } catch (NativeDaemonConnectorException e) {
374 Log.e(TAG, "Failed to share/unshare", e);
San Mehat4270e1e2010-01-29 05:32:19 -0800375 }
San Mehat4270e1e2010-01-29 05:32:19 -0800376 }
377
San Mehat207e5382010-02-04 20:46:54 -0800378 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800379 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
380 Log.w(TAG, "Multiple volumes not currently supported");
381 return;
382 }
San Mehatb1043402010-02-05 08:26:50 -0800383
384 if (mLegacyState.equals(state)) {
385 Log.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
386 return;
387 }
San Mehat4270e1e2010-01-29 05:32:19 -0800388
389 String oldState = mLegacyState;
390 mLegacyState = state;
391
392 synchronized (mListeners) {
393 for (int i = mListeners.size() -1; i >= 0; i--) {
394 MountServiceBinderListener bl = mListeners.get(i);
395 try {
San Mehatb1043402010-02-05 08:26:50 -0800396 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800397 } catch (RemoteException rex) {
398 Log.e(TAG, "Listener dead");
399 mListeners.remove(i);
400 } catch (Exception ex) {
401 Log.e(TAG, "Listener failed", ex);
402 }
403 }
404 }
405 }
406
407 /**
408 *
409 * Callback from NativeDaemonConnector
410 */
411 public void onDaemonConnected() {
412 /*
413 * Since we'll be calling back into the NativeDaemonConnector,
414 * we need to do our work in a new thread.
415 */
416 new Thread() {
417 public void run() {
418 /**
419 * Determine media state and UMS detection status
420 */
421 String path = Environment.getExternalStorageDirectory().getPath();
422 String state = Environment.MEDIA_REMOVED;
423
424 try {
425 String[] vols = mConnector.doListCommand(
426 "volume list", VoldResponseCode.VolumeListResult);
427 for (String volstr : vols) {
428 String[] tok = volstr.split(" ");
429 // FMT: <label> <mountpoint> <state>
430 if (!tok[1].equals(path)) {
431 Log.w(TAG, String.format(
432 "Skipping unknown volume '%s'",tok[1]));
433 continue;
434 }
435 int st = Integer.parseInt(tok[2]);
436 if (st == VolumeState.NoMedia) {
437 state = Environment.MEDIA_REMOVED;
438 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800439 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800440 } else if (st == VolumeState.Mounted) {
441 state = Environment.MEDIA_MOUNTED;
442 Log.i(TAG, "Media already mounted on daemon connection");
443 } else if (st == VolumeState.Shared) {
444 state = Environment.MEDIA_SHARED;
445 Log.i(TAG, "Media shared on daemon connection");
446 } else {
447 throw new Exception(String.format("Unexpected state %d", st));
448 }
449 }
450 if (state != null) {
451 updatePublicVolumeState(path, state);
452 }
453 } catch (Exception e) {
454 Log.e(TAG, "Error processing initial volume state", e);
455 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
456 }
457
458 try {
San Mehat207e5382010-02-04 20:46:54 -0800459 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800460 notifyShareAvailabilityChange("ums", avail);
461 } catch (Exception ex) {
462 Log.w(TAG, "Failed to get share availability");
463 }
San Mehat207e5382010-02-04 20:46:54 -0800464 /*
465 * Now that we've done our initialization, release
466 * the hounds!
467 */
468 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800469 }
470 }.start();
471 }
472
473 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800474 * Callback from NativeDaemonConnector
475 */
476 public boolean onEvent(int code, String raw, String[] cooked) {
477 Intent in = null;
478
San Mehat4270e1e2010-01-29 05:32:19 -0800479 if (code == VoldResponseCode.VolumeStateChange) {
480 /*
481 * One of the volumes we're managing has changed state.
482 * Format: "NNN Volume <label> <path> state changed
483 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
484 */
485 notifyVolumeStateChange(
486 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
487 Integer.parseInt(cooked[10]));
488 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
489 // FMT: NNN Share method <method> now <available|unavailable>
490 boolean avail = false;
491 if (cooked[5].equals("available")) {
492 avail = true;
493 }
494 notifyShareAvailabilityChange(cooked[3], avail);
495 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
496 (code == VoldResponseCode.VolumeDiskRemoved) ||
497 (code == VoldResponseCode.VolumeBadRemoval)) {
498 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
499 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
500 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
501 final String label = cooked[2];
502 final String path = cooked[3];
503 int major = -1;
504 int minor = -1;
505
506 try {
507 String devComp = cooked[6].substring(1, cooked[6].length() -1);
508 String[] devTok = devComp.split(":");
509 major = Integer.parseInt(devTok[0]);
510 minor = Integer.parseInt(devTok[1]);
511 } catch (Exception ex) {
512 Log.e(TAG, "Failed to parse major/minor", ex);
513 }
514
San Mehat4270e1e2010-01-29 05:32:19 -0800515 if (code == VoldResponseCode.VolumeDiskInserted) {
516 new Thread() {
517 public void run() {
518 try {
519 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800520 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehat4270e1e2010-01-29 05:32:19 -0800521 Log.w(TAG, String.format("Insertion mount failed (%d)", rc));
522 }
523 } catch (Exception ex) {
524 Log.w(TAG, "Failed to mount media on insertion", ex);
525 }
526 }
527 }.start();
528 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
529 /*
530 * This event gets trumped if we're already in BAD_REMOVAL state
531 */
532 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
533 return true;
534 }
535 /* Send the media unmounted event first */
536 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
537 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
538 mContext.sendBroadcast(in);
539
540 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
541 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
542 } else if (code == VoldResponseCode.VolumeBadRemoval) {
543 /* Send the media unmounted event first */
544 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
545 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
546 mContext.sendBroadcast(in);
547
548 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
549 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
550 } else {
551 Log.e(TAG, String.format("Unknown code {%d}", code));
552 }
553 } else {
554 return false;
555 }
556
557 if (in != null) {
558 mContext.sendBroadcast(in);
559 }
560 return true;
561 }
562
San Mehat207e5382010-02-04 20:46:54 -0800563 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800564 String vs = getVolumeState(path);
565
566 Intent in = null;
567
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500568 if (oldState == VolumeState.Shared && newState != oldState) {
San Mehat2fe718a2010-03-11 12:01:49 -0800569 if (LOCAL_LOGD) Log.d(TAG, "Sending ACTION_MEDIA_UNSHARED intent");
Mike Lockwoodbf2dd442010-03-03 06:16:52 -0500570 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_UNSHARED,
571 Uri.parse("file://" + path)));
572 }
573
San Mehat4270e1e2010-01-29 05:32:19 -0800574 if (newState == VolumeState.Init) {
575 } else if (newState == VolumeState.NoMedia) {
576 // NoMedia is handled via Disk Remove events
577 } else if (newState == VolumeState.Idle) {
578 /*
579 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
580 * if we're in the process of enabling UMS
581 */
582 if (!vs.equals(
583 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
584 Environment.MEDIA_NOFS) && !vs.equals(
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800585 Environment.MEDIA_UNMOUNTABLE) && !getUmsEnabling()) {
San Mehat4270e1e2010-01-29 05:32:19 -0800586 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
587 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
588 }
589 } else if (newState == VolumeState.Pending) {
590 } else if (newState == VolumeState.Checking) {
591 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
592 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
593 } else if (newState == VolumeState.Mounted) {
594 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
595 // Update media status on PackageManagerService to mount packages on sdcard
596 mPms.updateExternalMediaStatus(true);
597 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
598 in.putExtra("read-only", false);
599 } else if (newState == VolumeState.Unmounting) {
600 mPms.updateExternalMediaStatus(false);
601 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
602 } else if (newState == VolumeState.Formatting) {
603 } else if (newState == VolumeState.Shared) {
604 /* Send the media unmounted event first */
605 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
606 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
607 mContext.sendBroadcast(in);
608
609 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
610 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
San Mehat2fe718a2010-03-11 12:01:49 -0800611 if (LOCAL_LOGD) Log.d(TAG, "Sending ACTION_MEDIA_SHARED intent");
San Mehat4270e1e2010-01-29 05:32:19 -0800612 } else if (newState == VolumeState.SharedMnt) {
613 Log.e(TAG, "Live shared mounts not supported yet!");
614 return;
615 } else {
616 Log.e(TAG, "Unhandled VolumeState {" + newState + "}");
617 }
618
619 if (in != null) {
620 mContext.sendBroadcast(in);
621 }
622 }
623
San Mehat207e5382010-02-04 20:46:54 -0800624 private boolean doGetShareMethodAvailable(String method) {
625 ArrayList<String> rsp = mConnector.doCommand("share status " + method);
626
627 for (String line : rsp) {
628 String []tok = line.split(" ");
629 int code;
630 try {
631 code = Integer.parseInt(tok[0]);
632 } catch (NumberFormatException nfe) {
633 Log.e(TAG, String.format("Error parsing code %s", tok[0]));
634 return false;
635 }
636 if (code == VoldResponseCode.ShareStatusResult) {
637 if (tok[2].equals("available"))
638 return true;
639 return false;
640 } else {
641 Log.e(TAG, String.format("Unexpected response code %d", code));
642 return false;
643 }
644 }
645 Log.e(TAG, "Got an empty response");
646 return false;
647 }
648
649 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800650 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800651
652 try {
653 mConnector.doCommand(String.format("volume mount %s", path));
654 } catch (NativeDaemonConnectorException e) {
655 /*
656 * Mount failed for some reason
657 */
658 Intent in = null;
659 int code = e.getCode();
660 if (code == VoldResponseCode.OpFailedNoMedia) {
661 /*
662 * Attempt to mount but no media inserted
663 */
San Mehatb1043402010-02-05 08:26:50 -0800664 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800665 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
666 /*
667 * Media is blank or does not contain a supported filesystem
668 */
669 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
670 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800671 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800672 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
673 /*
674 * Volume consistency check failed
675 */
676 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
677 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800678 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800679 } else {
San Mehatb1043402010-02-05 08:26:50 -0800680 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800681 }
682
683 /*
684 * Send broadcast intent (if required for the failure)
685 */
686 if (in != null) {
687 mContext.sendBroadcast(in);
688 }
689 }
690
691 return rc;
692 }
693
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800694 /*
695 * If force is not set, we do not unmount if there are
696 * processes holding references to the volume about to be unmounted.
697 * If force is set, all the processes holding references need to be
698 * killed via the ActivityManager before actually unmounting the volume.
699 * This might even take a while and might be retried after timed delays
700 * to make sure we dont end up in an instable state and kill some core
701 * processes.
702 */
San Mehatd9709982010-02-18 11:43:03 -0800703 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800704 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800705 return VoldResponseCode.OpFailedVolNotMounted;
706 }
707
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800708 // We unmounted the volume. No of the asec containers are available now.
709 synchronized (mAsecMountSet) {
710 mAsecMountSet.clear();
711 }
San Mehat207e5382010-02-04 20:46:54 -0800712 // Notify PackageManager of potential media removal and deal with
713 // return code later on. The caller of this api should be aware or have been
714 // notified that the applications installed on the media will be killed.
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800715 // Redundant probably. But no harm in updating state again.
San Mehat207e5382010-02-04 20:46:54 -0800716 mPms.updateExternalMediaStatus(false);
717 try {
San Mehatd9709982010-02-18 11:43:03 -0800718 mConnector.doCommand(String.format(
719 "volume unmount %s%s", path, (force ? " force" : "")));
San Mehatb1043402010-02-05 08:26:50 -0800720 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800721 } catch (NativeDaemonConnectorException e) {
722 // Don't worry about mismatch in PackageManager since the
723 // call back will handle the status changes any way.
724 int code = e.getCode();
725 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800726 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800727 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
728 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800729 } else {
San Mehatb1043402010-02-05 08:26:50 -0800730 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800731 }
732 }
733 }
734
735 private int doFormatVolume(String path) {
736 try {
737 String cmd = String.format("volume format %s", path);
738 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800739 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800740 } catch (NativeDaemonConnectorException e) {
741 int code = e.getCode();
742 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800743 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800744 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800745 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800746 } else {
San Mehatb1043402010-02-05 08:26:50 -0800747 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800748 }
749 }
750 }
751
San Mehatb1043402010-02-05 08:26:50 -0800752 private boolean doGetVolumeShared(String path, String method) {
753 String cmd = String.format("volume shared %s %s", path, method);
754 ArrayList<String> rsp = mConnector.doCommand(cmd);
755
756 for (String line : rsp) {
757 String []tok = line.split(" ");
758 int code;
759 try {
760 code = Integer.parseInt(tok[0]);
761 } catch (NumberFormatException nfe) {
762 Log.e(TAG, String.format("Error parsing code %s", tok[0]));
763 return false;
764 }
765 if (code == VoldResponseCode.ShareEnabledResult) {
766 if (tok[2].equals("enabled"))
767 return true;
768 return false;
769 } else {
770 Log.e(TAG, String.format("Unexpected response code %d", code));
771 return false;
772 }
773 }
774 Log.e(TAG, "Got an empty response");
775 return false;
776 }
777
San Mehat207e5382010-02-04 20:46:54 -0800778 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800779 if (!method.equals("ums")) {
780 Log.w(TAG, "Ignoring unsupported share method {" + method + "}");
781 return;
782 }
783
784 synchronized (mListeners) {
785 for (int i = mListeners.size() -1; i >= 0; i--) {
786 MountServiceBinderListener bl = mListeners.get(i);
787 try {
San Mehatb1043402010-02-05 08:26:50 -0800788 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800789 } catch (RemoteException rex) {
790 Log.e(TAG, "Listener dead");
791 mListeners.remove(i);
792 } catch (Exception ex) {
793 Log.e(TAG, "Listener failed", ex);
794 }
795 }
796 }
797
San Mehat207e5382010-02-04 20:46:54 -0800798 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800799 sendUmsIntent(avail);
800 } else {
801 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800802 }
San Mehat2fe718a2010-03-11 12:01:49 -0800803
804 final String path = Environment.getExternalStorageDirectory().getPath();
805 if (avail == false && getVolumeState(path).equals(Environment.MEDIA_SHARED)) {
806 /*
807 * USB mass storage disconnected while enabled
808 */
809 new Thread() {
810 public void run() {
811 try {
812 int rc;
813 Log.w(TAG, "Disabling UMS after cable disconnect");
814 doShareUnshareVolume(path, "ums", false);
815 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
816 Log.e(TAG, String.format(
817 "Failed to remount {%s} on UMS enabled-disconnect (%d)",
818 path, rc));
819 }
820 } catch (Exception ex) {
821 Log.w(TAG, "Failed to mount media on UMS enabled-disconnect", ex);
822 }
823 }
824 }.start();
825 }
San Mehat4270e1e2010-01-29 05:32:19 -0800826 }
827
San Mehat6a965af22010-02-24 17:47:30 -0800828 private void sendUmsIntent(boolean c) {
829 mContext.sendBroadcast(
830 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
831 }
832
San Mehat207e5382010-02-04 20:46:54 -0800833 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800834 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
835 throw new SecurityException(String.format("Requires %s permission", perm));
836 }
837 }
838
839 /**
San Mehat207e5382010-02-04 20:46:54 -0800840 * Constructs a new MountService instance
841 *
842 * @param context Binder context for this service
843 */
844 public MountService(Context context) {
845 mContext = context;
846
San Mehat207e5382010-02-04 20:46:54 -0800847 // XXX: This will go away soon in favor of IMountServiceObserver
848 mPms = (PackageManagerService) ServiceManager.getService("package");
849
850 mContext.registerReceiver(mBroadcastReceiver,
851 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
852
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800853 /*
854 * Vold does not run in the simulator, so pretend the connector thread
855 * ran and did its thing.
856 */
857 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
858 mReady = true;
859 mUmsEnabling = true;
860 return;
861 }
862
San Mehat207e5382010-02-04 20:46:54 -0800863 mConnector = new NativeDaemonConnector(this, "vold", 10, "VoldConnector");
864 mReady = false;
865 Thread thread = new Thread(mConnector, NativeDaemonConnector.class.getName());
866 thread.start();
867 }
868
869 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800870 * Exposed API calls below here
871 */
872
873 public void registerListener(IMountServiceListener listener) {
874 synchronized (mListeners) {
875 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
876 try {
877 listener.asBinder().linkToDeath(bl, 0);
878 mListeners.add(bl);
879 } catch (RemoteException rex) {
880 Log.e(TAG, "Failed to link to listener death");
881 }
882 }
883 }
884
885 public void unregisterListener(IMountServiceListener listener) {
886 synchronized (mListeners) {
887 for(MountServiceBinderListener bl : mListeners) {
888 if (bl.mListener == listener) {
889 mListeners.remove(mListeners.indexOf(bl));
890 return;
891 }
892 }
893 }
894 }
895
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800896 public void shutdown(final IMountShutdownObserver observer) {
San Mehat4270e1e2010-01-29 05:32:19 -0800897 validatePermission(android.Manifest.permission.SHUTDOWN);
898
899 Log.i(TAG, "Shutting down");
900
901 String path = Environment.getExternalStorageDirectory().getPath();
902 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -0800903
904 if (state.equals(Environment.MEDIA_SHARED)) {
905 /*
906 * If the media is currently shared, unshare it.
907 * XXX: This is still dangerous!. We should not
908 * be rebooting at *all* if UMS is enabled, since
909 * the UMS host could have dirty FAT cache entries
910 * yet to flush.
911 */
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800912 setUsbMassStorageEnabled(false);
San Mehat91c77612010-01-07 10:39:41 -0800913 } else if (state.equals(Environment.MEDIA_CHECKING)) {
914 /*
915 * If the media is being checked, then we need to wait for
916 * it to complete before being able to proceed.
917 */
918 // XXX: @hackbod - Should we disable the ANR timer here?
919 int retries = 30;
920 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
921 try {
922 Thread.sleep(1000);
923 } catch (InterruptedException iex) {
924 Log.e(TAG, "Interrupted while waiting for media", iex);
925 break;
926 }
927 state = Environment.getExternalStorageState();
928 }
929 if (retries == 0) {
930 Log.e(TAG, "Timed out waiting for media to check");
931 }
932 }
933
934 if (state.equals(Environment.MEDIA_MOUNTED)) {
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800935 // Post a unmount message.
936 ShutdownCallBack ucb = new ShutdownCallBack(path, observer);
937 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
San Mehat4270e1e2010-01-29 05:32:19 -0800938 }
939 }
940
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800941 private boolean getUmsEnabling() {
942 synchronized (mListeners) {
943 return mUmsEnabling;
944 }
945 }
946
947 private void setUmsEnabling(boolean enable) {
948 synchronized (mListeners) {
949 mUmsEnabling = true;
950 }
951 }
952
San Mehatb1043402010-02-05 08:26:50 -0800953 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -0800954 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -0800955
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800956 if (getUmsEnabling()) {
San Mehatb1043402010-02-05 08:26:50 -0800957 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -0800958 }
San Mehatb1043402010-02-05 08:26:50 -0800959 return doGetShareMethodAvailable("ums");
960 }
961
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800962 public void setUsbMassStorageEnabled(boolean enable) {
San Mehatb1043402010-02-05 08:26:50 -0800963 waitForReady();
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800964 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehatb1043402010-02-05 08:26:50 -0800965
Suchi Amalapurapu0eec21d2010-02-25 17:07:14 -0800966 // TODO: Add support for multiple share methods
967
968 /*
969 * If the volume is mounted and we're enabling then unmount it
970 */
971 String path = Environment.getExternalStorageDirectory().getPath();
972 String vs = getVolumeState(path);
973 String method = "ums";
974 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
975 // Override for isUsbMassStorageEnabled()
976 setUmsEnabling(enable);
977 UmsEnableCallBack umscb = new UmsEnableCallBack(path, method, true);
978 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, umscb));
979 // Clear override
980 setUmsEnabling(false);
981 }
982 /*
983 * If we disabled UMS then mount the volume
984 */
985 if (!enable) {
986 doShareUnshareVolume(path, method, enable);
987 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
988 Log.e(TAG, "Failed to remount " + path +
989 " after disabling share method " + method);
990 /*
991 * Even though the mount failed, the unshare didn't so don't indicate an error.
992 * The mountVolume() call will have set the storage state and sent the necessary
993 * broadcasts.
994 */
995 }
996 }
San Mehatb1043402010-02-05 08:26:50 -0800997 }
998
999 public boolean isUsbMassStorageEnabled() {
1000 waitForReady();
1001 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
San Mehat4270e1e2010-01-29 05:32:19 -08001003
San Mehat7fd0fee2009-12-17 07:12:23 -08001004 /**
1005 * @return state of the volume at the specified mount point
1006 */
San Mehat4270e1e2010-01-29 05:32:19 -08001007 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -08001008 /*
1009 * XXX: Until we have multiple volume discovery, just hardwire
1010 * this to /sdcard
1011 */
1012 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
1013 Log.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
1014 throw new IllegalArgumentException();
1015 }
1016
1017 return mLegacyState;
1018 }
1019
San Mehat4270e1e2010-01-29 05:32:19 -08001020 public int mountVolume(String path) {
1021 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -08001022
San Mehat207e5382010-02-04 20:46:54 -08001023 waitForReady();
1024 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001027 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001028 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001029 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001031 UnmountCallBack ucb = new UnmountCallBack(path, force);
1032 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
1034
San Mehat4270e1e2010-01-29 05:32:19 -08001035 public int formatVolume(String path) {
1036 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -08001037 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -08001038
San Mehat207e5382010-02-04 20:46:54 -08001039 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
San Mehatc1b4ce92010-02-16 17:13:03 -08001042 public int []getStorageUsers(String path) {
1043 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
1044 waitForReady();
1045 try {
1046 String[] r = mConnector.doListCommand(
1047 String.format("storage users %s", path),
1048 VoldResponseCode.StorageUsersListResult);
1049 // FMT: <pid> <process name>
1050 int[] data = new int[r.length];
1051 for (int i = 0; i < r.length; i++) {
1052 String []tok = r[i].split(" ");
1053 try {
1054 data[i] = Integer.parseInt(tok[0]);
1055 } catch (NumberFormatException nfe) {
1056 Log.e(TAG, String.format("Error parsing pid %s", tok[0]));
1057 return new int[0];
1058 }
1059 }
1060 return data;
1061 } catch (NativeDaemonConnectorException e) {
1062 Log.e(TAG, "Failed to retrieve storage users list", e);
1063 return new int[0];
1064 }
1065 }
1066
San Mehatb1043402010-02-05 08:26:50 -08001067 private void warnOnNotMounted() {
1068 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
1069 Log.w(TAG, "getSecureContainerList() called when storage not mounted");
1070 }
1071 }
1072
San Mehat4270e1e2010-01-29 05:32:19 -08001073 public String[] getSecureContainerList() {
1074 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001075 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001076 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001077
San Mehat4270e1e2010-01-29 05:32:19 -08001078 try {
1079 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
1080 } catch (NativeDaemonConnectorException e) {
1081 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
1083 }
San Mehat36972292010-01-06 11:06:32 -08001084
San Mehat4270e1e2010-01-29 05:32:19 -08001085 public int createSecureContainer(String id, int sizeMb, String fstype,
1086 String key, int ownerUid) {
1087 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -08001088 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001089 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001090
San Mehatb1043402010-02-05 08:26:50 -08001091 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001092 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
1093 try {
1094 mConnector.doCommand(cmd);
1095 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001096 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001097 }
San Mehata181b212010-02-11 06:50:20 -08001098
1099 if (rc == StorageResultCode.OperationSucceeded) {
1100 synchronized (mAsecMountSet) {
1101 mAsecMountSet.add(id);
1102 }
1103 }
San Mehat4270e1e2010-01-29 05:32:19 -08001104 return rc;
San Mehat36972292010-01-06 11:06:32 -08001105 }
1106
San Mehat4270e1e2010-01-29 05:32:19 -08001107 public int finalizeSecureContainer(String id) {
1108 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001109 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001110
San Mehatb1043402010-02-05 08:26:50 -08001111 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001112 try {
1113 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001114 /*
1115 * Finalization does a remount, so no need
1116 * to update mAsecMountSet
1117 */
San Mehat4270e1e2010-01-29 05:32:19 -08001118 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001119 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001120 }
San Mehat4270e1e2010-01-29 05:32:19 -08001121 return rc;
San Mehat36972292010-01-06 11:06:32 -08001122 }
1123
San Mehatd9709982010-02-18 11:43:03 -08001124 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001125 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001126 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001127 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001128
San Mehatb1043402010-02-05 08:26:50 -08001129 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001130 try {
San Mehatd9709982010-02-18 11:43:03 -08001131 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001132 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001133 int code = e.getCode();
1134 if (code == VoldResponseCode.OpFailedStorageBusy) {
1135 rc = StorageResultCode.OperationFailedStorageBusy;
1136 } else {
1137 rc = StorageResultCode.OperationFailedInternalError;
1138 }
San Mehat02735bc2010-01-26 15:18:08 -08001139 }
San Mehata181b212010-02-11 06:50:20 -08001140
1141 if (rc == StorageResultCode.OperationSucceeded) {
1142 synchronized (mAsecMountSet) {
1143 if (mAsecMountSet.contains(id)) {
1144 mAsecMountSet.remove(id);
1145 }
1146 }
1147 }
1148
San Mehat4270e1e2010-01-29 05:32:19 -08001149 return rc;
San Mehat36972292010-01-06 11:06:32 -08001150 }
1151
San Mehat4270e1e2010-01-29 05:32:19 -08001152 public int mountSecureContainer(String id, String key, int ownerUid) {
1153 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001154 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001155 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001156
San Mehata181b212010-02-11 06:50:20 -08001157 synchronized (mAsecMountSet) {
1158 if (mAsecMountSet.contains(id)) {
1159 return StorageResultCode.OperationFailedStorageMounted;
1160 }
1161 }
1162
San Mehatb1043402010-02-05 08:26:50 -08001163 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001164 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1165 try {
1166 mConnector.doCommand(cmd);
1167 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001168 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001169 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001170
1171 if (rc == StorageResultCode.OperationSucceeded) {
1172 synchronized (mAsecMountSet) {
1173 mAsecMountSet.add(id);
1174 }
1175 }
San Mehat4270e1e2010-01-29 05:32:19 -08001176 return rc;
San Mehat36972292010-01-06 11:06:32 -08001177 }
1178
San Mehatd9709982010-02-18 11:43:03 -08001179 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001180 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001181 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001182 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001183
San Mehat6cdd9c02010-02-09 14:45:20 -08001184 synchronized (mAsecMountSet) {
1185 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001186 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001187 }
1188 }
1189
San Mehatb1043402010-02-05 08:26:50 -08001190 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001191 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001192 try {
1193 mConnector.doCommand(cmd);
1194 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001195 int code = e.getCode();
1196 if (code == VoldResponseCode.OpFailedStorageBusy) {
1197 rc = StorageResultCode.OperationFailedStorageBusy;
1198 } else {
1199 rc = StorageResultCode.OperationFailedInternalError;
1200 }
San Mehat02735bc2010-01-26 15:18:08 -08001201 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001202
1203 if (rc == StorageResultCode.OperationSucceeded) {
1204 synchronized (mAsecMountSet) {
1205 mAsecMountSet.remove(id);
1206 }
1207 }
San Mehat4270e1e2010-01-29 05:32:19 -08001208 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001209 }
1210
San Mehat6cdd9c02010-02-09 14:45:20 -08001211 public boolean isSecureContainerMounted(String id) {
1212 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1213 waitForReady();
1214 warnOnNotMounted();
1215
1216 synchronized (mAsecMountSet) {
1217 return mAsecMountSet.contains(id);
1218 }
1219 }
1220
San Mehat4270e1e2010-01-29 05:32:19 -08001221 public int renameSecureContainer(String oldId, String newId) {
1222 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001223 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001224 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001225
San Mehata181b212010-02-11 06:50:20 -08001226 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001227 /*
1228 * Because a mounted container has active internal state which cannot be
1229 * changed while active, we must ensure both ids are not currently mounted.
1230 */
1231 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001232 return StorageResultCode.OperationFailedStorageMounted;
1233 }
1234 }
1235
San Mehatb1043402010-02-05 08:26:50 -08001236 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001237 String cmd = String.format("asec rename %s %s", oldId, newId);
1238 try {
1239 mConnector.doCommand(cmd);
1240 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001241 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001242 }
San Mehata181b212010-02-11 06:50:20 -08001243
San Mehat4270e1e2010-01-29 05:32:19 -08001244 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001245 }
1246
San Mehat4270e1e2010-01-29 05:32:19 -08001247 public String getSecureContainerPath(String id) {
1248 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001249 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001250 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001251
San Mehat4270e1e2010-01-29 05:32:19 -08001252 ArrayList<String> rsp = mConnector.doCommand("asec path " + id);
San Mehat36972292010-01-06 11:06:32 -08001253
San Mehat22dd86e2010-01-12 12:21:18 -08001254 for (String line : rsp) {
1255 String []tok = line.split(" ");
1256 int code = Integer.parseInt(tok[0]);
1257 if (code == VoldResponseCode.AsecPathResult) {
1258 return tok[1];
1259 } else {
San Mehat4270e1e2010-01-29 05:32:19 -08001260 Log.e(TAG, String.format("Unexpected response code %d", code));
1261 return "";
San Mehat22dd86e2010-01-12 12:21:18 -08001262 }
1263 }
San Mehat4270e1e2010-01-29 05:32:19 -08001264
1265 Log.e(TAG, "Got an empty response");
1266 return "";
San Mehat22dd86e2010-01-12 12:21:18 -08001267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268}
1269