blob: 4485c79ee6a3aeaea2e8d41d5ed1d168c986da0d [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;
29import android.os.storage.StorageResultCode;
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -080030import android.os.Handler;
31import android.os.Message;
San Mehat4270e1e2010-01-29 05:32:19 -080032import android.os.RemoteException;
33import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Environment;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -080035import android.os.ServiceManager;
San Mehat207e5382010-02-04 20:46:54 -080036import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.util.Log;
San Mehat22dd86e2010-01-12 12:21:18 -080039import java.util.ArrayList;
San Mehat6cdd9c02010-02-09 14:45:20 -080040import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042/**
San Mehatb1043402010-02-05 08:26:50 -080043 * MountService implements back-end services for platform storage
44 * management.
45 * @hide - Applications should use android.os.storage.StorageManager
46 * to access the MountService.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 */
San Mehat22dd86e2010-01-12 12:21:18 -080048class MountService extends IMountService.Stub
49 implements INativeDaemonConnectorCallbacks {
San Mehatb1043402010-02-05 08:26:50 -080050 private static final boolean LOCAL_LOGD = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52 private static final String TAG = "MountService";
53
San Mehat4270e1e2010-01-29 05:32:19 -080054 /*
55 * Internal vold volume state constants
56 */
San Mehat7fd0fee2009-12-17 07:12:23 -080057 class VolumeState {
58 public static final int Init = -1;
59 public static final int NoMedia = 0;
60 public static final int Idle = 1;
61 public static final int Pending = 2;
62 public static final int Checking = 3;
63 public static final int Mounted = 4;
64 public static final int Unmounting = 5;
65 public static final int Formatting = 6;
66 public static final int Shared = 7;
67 public static final int SharedMnt = 8;
68 }
69
San Mehat4270e1e2010-01-29 05:32:19 -080070 /*
71 * Internal vold response code constants
72 */
San Mehat22dd86e2010-01-12 12:21:18 -080073 class VoldResponseCode {
San Mehat4270e1e2010-01-29 05:32:19 -080074 /*
75 * 100 series - Requestion action was initiated; expect another reply
76 * before proceeding with a new command.
77 */
San Mehat22dd86e2010-01-12 12:21:18 -080078 public static final int VolumeListResult = 110;
79 public static final int AsecListResult = 111;
San Mehatc1b4ce92010-02-16 17:13:03 -080080 public static final int StorageUsersListResult = 112;
San Mehat22dd86e2010-01-12 12:21:18 -080081
San Mehat4270e1e2010-01-29 05:32:19 -080082 /*
83 * 200 series - Requestion action has been successfully completed.
84 */
85 public static final int ShareStatusResult = 210;
San Mehat22dd86e2010-01-12 12:21:18 -080086 public static final int AsecPathResult = 211;
San Mehat4270e1e2010-01-29 05:32:19 -080087 public static final int ShareEnabledResult = 212;
San Mehat22dd86e2010-01-12 12:21:18 -080088
San Mehat4270e1e2010-01-29 05:32:19 -080089 /*
90 * 400 series - Command was accepted, but the requested action
91 * did not take place.
92 */
93 public static final int OpFailedNoMedia = 401;
94 public static final int OpFailedMediaBlank = 402;
95 public static final int OpFailedMediaCorrupt = 403;
96 public static final int OpFailedVolNotMounted = 404;
San Mehatd9709982010-02-18 11:43:03 -080097 public static final int OpFailedStorageBusy = 405;
San Mehat4270e1e2010-01-29 05:32:19 -080098
99 /*
100 * 600 series - Unsolicited broadcasts.
101 */
San Mehat22dd86e2010-01-12 12:21:18 -0800102 public static final int VolumeStateChange = 605;
San Mehat22dd86e2010-01-12 12:21:18 -0800103 public static final int ShareAvailabilityChange = 620;
104 public static final int VolumeDiskInserted = 630;
105 public static final int VolumeDiskRemoved = 631;
106 public static final int VolumeBadRemoval = 632;
107 }
108
San Mehat4270e1e2010-01-29 05:32:19 -0800109 private Context mContext;
110 private NativeDaemonConnector mConnector;
111 private String mLegacyState = Environment.MEDIA_REMOVED;
112 private PackageManagerService mPms;
113 private boolean mUmsEnabling;
114 private ArrayList<MountServiceBinderListener> mListeners;
San Mehat6a965af22010-02-24 17:47:30 -0800115 private boolean mBooted = false;
116 private boolean mReady = false;
117 private boolean mSendUmsConnectedOnBoot = false;
Suchi Amalapurapufd3530f2010-01-18 00:15:59 -0800118
San Mehat6cdd9c02010-02-09 14:45:20 -0800119 /**
120 * Private hash of currently mounted secure containers.
121 */
122 private HashSet<String> mAsecMountSet = new HashSet<String>();
123
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800124 private static final int H_UNMOUNT_PM_UPDATE = 1;
125 private static final int H_UNMOUNT_PM_DONE = 2;
126 private static final int H_UNMOUNT_MS = 3;
127 private static final int RETRY_UNMOUNT_DELAY = 30; // in ms
128 private static final int MAX_UNMOUNT_RETRIES = 4;
129
130 private IntentFilter mPmFilter = new IntentFilter(
131 Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
132 private BroadcastReceiver mPmReceiver = new BroadcastReceiver() {
133 public void onReceive(Context context, Intent intent) {
134 String action = intent.getAction();
135 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
136 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
137 }
138 }
139 };
140
141 class UnmountCallBack {
142 String path;
143 int retries;
144 boolean force;
145
146 UnmountCallBack(String path, boolean force) {
147 retries = 0;
148 this.path = path;
149 this.force = force;
150 }
151 }
152
153 final private Handler mHandler = new Handler() {
154 ArrayList<UnmountCallBack> mForceUnmounts = new ArrayList<UnmountCallBack>();
155
156 public void handleMessage(Message msg) {
157 switch (msg.what) {
158 case H_UNMOUNT_PM_UPDATE: {
159 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
160 mForceUnmounts.add(ucb);
161 mContext.registerReceiver(mPmReceiver, mPmFilter);
162 boolean hasExtPkgs = mPms.updateExternalMediaStatus(false);
163 if (!hasExtPkgs) {
164 // Unregister right away
165 mHandler.sendEmptyMessage(H_UNMOUNT_PM_DONE);
166 }
167 break;
168 }
169 case H_UNMOUNT_PM_DONE: {
170 // Unregister receiver
171 mContext.unregisterReceiver(mPmReceiver);
172 UnmountCallBack ucb = mForceUnmounts.get(0);
173 if (ucb == null || ucb.path == null) {
174 // Just ignore
175 return;
176 }
177 String path = ucb.path;
178 boolean done = false;
179 if (!ucb.force) {
180 done = true;
181 } else {
182 int pids[] = getStorageUsers(path);
183 if (pids == null || pids.length == 0) {
184 done = true;
185 } else {
186 // Kill processes holding references first
187 ActivityManagerService ams = (ActivityManagerService)
188 ServiceManager.getService("activity");
189 // Eliminate system process here?
190 boolean ret = ams.killPidsForMemory(pids);
191 if (ret) {
192 // Confirm if file references have been freed.
193 pids = getStorageUsers(path);
194 if (pids == null || pids.length == 0) {
195 done = true;
196 }
197 }
198 }
199 }
200 if (done) {
201 mForceUnmounts.remove(0);
202 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_MS,
203 ucb));
204 } else {
205 if (ucb.retries >= MAX_UNMOUNT_RETRIES) {
206 Log.i(TAG, "Cannot unmount inspite of " +
207 MAX_UNMOUNT_RETRIES + " to unmount media");
208 // Send final broadcast indicating failure to unmount.
209 } else {
210 mHandler.sendMessageDelayed(
211 mHandler.obtainMessage(H_UNMOUNT_PM_DONE,
212 ucb.retries++),
213 RETRY_UNMOUNT_DELAY);
214 }
215 }
216 break;
217 }
218 case H_UNMOUNT_MS : {
219 UnmountCallBack ucb = (UnmountCallBack) msg.obj;
220 String path = ucb.path;
221 doUnmountVolume(path, true);
222 break;
223 }
224 }
225 }
226 };
227
San Mehat207e5382010-02-04 20:46:54 -0800228 private void waitForReady() {
229 while (mReady == false) {
230 for (int retries = 5; retries > 0; retries--) {
231 if (mReady) {
232 return;
233 }
234 SystemClock.sleep(1000);
235 }
236 Log.w(TAG, "Waiting too long for mReady!");
237 }
San Mehat1f6301e2010-01-07 22:40:27 -0800238 }
239
San Mehat207e5382010-02-04 20:46:54 -0800240 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 public void onReceive(Context context, Intent intent) {
San Mehat91c77612010-01-07 10:39:41 -0800242 String action = intent.getAction();
243
244 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
San Mehat207e5382010-02-04 20:46:54 -0800245 mBooted = true;
San Mehat22dd86e2010-01-12 12:21:18 -0800246
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800247 /*
248 * In the simulator, we need to broadcast a volume mounted event
249 * to make the media scanner run.
250 */
251 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
252 notifyVolumeStateChange(null, "/sdcard", VolumeState.NoMedia, VolumeState.Mounted);
253 return;
254 }
San Mehatfafb0412010-02-18 19:40:04 -0800255 new Thread() {
256 public void run() {
257 try {
258 String path = Environment.getExternalStorageDirectory().getPath();
259 if (getVolumeState(
260 Environment.getExternalStorageDirectory().getPath()).equals(
261 Environment.MEDIA_UNMOUNTED)) {
262 int rc = doMountVolume(path);
263 if (rc != StorageResultCode.OperationSucceeded) {
264 Log.e(TAG, String.format("Boot-time mount failed (%d)", rc));
265 }
266 }
San Mehat6a965af22010-02-24 17:47:30 -0800267 /*
268 * If UMS is connected in boot, send the connected event
269 * now that we're up.
270 */
271 if (mSendUmsConnectedOnBoot) {
272 sendUmsIntent(true);
273 mSendUmsConnectedOnBoot = false;
274 }
San Mehatfafb0412010-02-18 19:40:04 -0800275 } catch (Exception ex) {
276 Log.e(TAG, "Boot-time mount exception", ex);
277 }
San Mehat207e5382010-02-04 20:46:54 -0800278 }
San Mehatfafb0412010-02-18 19:40:04 -0800279 }.start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281 }
282 };
283
San Mehat4270e1e2010-01-29 05:32:19 -0800284 private final class MountServiceBinderListener implements IBinder.DeathRecipient {
285 final IMountServiceListener mListener;
286
287 MountServiceBinderListener(IMountServiceListener listener) {
288 mListener = listener;
289
San Mehat91c77612010-01-07 10:39:41 -0800290 }
291
San Mehat4270e1e2010-01-29 05:32:19 -0800292 public void binderDied() {
San Mehatb1043402010-02-05 08:26:50 -0800293 if (LOCAL_LOGD) Log.d(TAG, "An IMountServiceListener has died!");
San Mehat4270e1e2010-01-29 05:32:19 -0800294 synchronized(mListeners) {
295 mListeners.remove(this);
296 mListener.asBinder().unlinkToDeath(this, 0);
297 }
298 }
299 }
300
San Mehat207e5382010-02-04 20:46:54 -0800301 private int doShareUnshareVolume(String path, String method, boolean enable) {
San Mehat4270e1e2010-01-29 05:32:19 -0800302 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
303
304 // TODO: Add support for multiple share methods
305 if (!method.equals("ums")) {
306 throw new IllegalArgumentException(String.format("Method %s not supported", method));
307 }
308
309 /*
310 * If the volume is mounted and we're enabling then unmount it
311 */
312 String vs = getVolumeState(path);
313 if (enable && vs.equals(Environment.MEDIA_MOUNTED)) {
San Mehatb1043402010-02-05 08:26:50 -0800314 mUmsEnabling = enable; // Override for isUsbMassStorageEnabled()
San Mehat3d488102010-02-22 11:39:16 -0800315 int rc = doUnmountVolume(path, true);
San Mehatb1043402010-02-05 08:26:50 -0800316 mUmsEnabling = false; // Clear override
San Mehat59443a62010-02-09 13:28:45 -0800317 if (rc != StorageResultCode.OperationSucceeded) {
318 Log.e(TAG, String.format("Failed to unmount before enabling UMS (%d)", rc));
319 return rc;
320 }
San Mehat4270e1e2010-01-29 05:32:19 -0800321 }
322
323 try {
324 mConnector.doCommand(String.format(
325 "volume %sshare %s %s", (enable ? "" : "un"), path, method));
326 } catch (NativeDaemonConnectorException e) {
327 Log.e(TAG, "Failed to share/unshare", e);
San Mehatb1043402010-02-05 08:26:50 -0800328 return StorageResultCode.OperationFailedInternalError;
San Mehat4270e1e2010-01-29 05:32:19 -0800329 }
330
331 /*
332 * If we disabled UMS then mount the volume
333 */
334 if (!enable) {
San Mehatb1043402010-02-05 08:26:50 -0800335 if (doMountVolume(path) != StorageResultCode.OperationSucceeded) {
San Mehat4270e1e2010-01-29 05:32:19 -0800336 Log.e(TAG, String.format(
337 "Failed to remount %s after disabling share method %s", path, method));
338 /*
339 * Even though the mount failed, the unshare didn't so don't indicate an error.
340 * The mountVolume() call will have set the storage state and sent the necessary
341 * broadcasts.
342 */
343 }
344 }
345
San Mehatb1043402010-02-05 08:26:50 -0800346 return StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -0800347 }
348
San Mehat207e5382010-02-04 20:46:54 -0800349 private void updatePublicVolumeState(String path, String state) {
San Mehat4270e1e2010-01-29 05:32:19 -0800350 if (!path.equals(Environment.getExternalStorageDirectory().getPath())) {
351 Log.w(TAG, "Multiple volumes not currently supported");
352 return;
353 }
San Mehatb1043402010-02-05 08:26:50 -0800354
355 if (mLegacyState.equals(state)) {
356 Log.w(TAG, String.format("Duplicate state transition (%s -> %s)", mLegacyState, state));
357 return;
358 }
San Mehat4270e1e2010-01-29 05:32:19 -0800359
360 String oldState = mLegacyState;
361 mLegacyState = state;
362
363 synchronized (mListeners) {
364 for (int i = mListeners.size() -1; i >= 0; i--) {
365 MountServiceBinderListener bl = mListeners.get(i);
366 try {
San Mehatb1043402010-02-05 08:26:50 -0800367 bl.mListener.onStorageStateChanged(path, oldState, state);
San Mehat4270e1e2010-01-29 05:32:19 -0800368 } catch (RemoteException rex) {
369 Log.e(TAG, "Listener dead");
370 mListeners.remove(i);
371 } catch (Exception ex) {
372 Log.e(TAG, "Listener failed", ex);
373 }
374 }
375 }
376 }
377
378 /**
379 *
380 * Callback from NativeDaemonConnector
381 */
382 public void onDaemonConnected() {
383 /*
384 * Since we'll be calling back into the NativeDaemonConnector,
385 * we need to do our work in a new thread.
386 */
387 new Thread() {
388 public void run() {
389 /**
390 * Determine media state and UMS detection status
391 */
392 String path = Environment.getExternalStorageDirectory().getPath();
393 String state = Environment.MEDIA_REMOVED;
394
395 try {
396 String[] vols = mConnector.doListCommand(
397 "volume list", VoldResponseCode.VolumeListResult);
398 for (String volstr : vols) {
399 String[] tok = volstr.split(" ");
400 // FMT: <label> <mountpoint> <state>
401 if (!tok[1].equals(path)) {
402 Log.w(TAG, String.format(
403 "Skipping unknown volume '%s'",tok[1]));
404 continue;
405 }
406 int st = Integer.parseInt(tok[2]);
407 if (st == VolumeState.NoMedia) {
408 state = Environment.MEDIA_REMOVED;
409 } else if (st == VolumeState.Idle) {
San Mehat207e5382010-02-04 20:46:54 -0800410 state = Environment.MEDIA_UNMOUNTED;
San Mehat4270e1e2010-01-29 05:32:19 -0800411 } else if (st == VolumeState.Mounted) {
412 state = Environment.MEDIA_MOUNTED;
413 Log.i(TAG, "Media already mounted on daemon connection");
414 } else if (st == VolumeState.Shared) {
415 state = Environment.MEDIA_SHARED;
416 Log.i(TAG, "Media shared on daemon connection");
417 } else {
418 throw new Exception(String.format("Unexpected state %d", st));
419 }
420 }
421 if (state != null) {
422 updatePublicVolumeState(path, state);
423 }
424 } catch (Exception e) {
425 Log.e(TAG, "Error processing initial volume state", e);
426 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
427 }
428
429 try {
San Mehat207e5382010-02-04 20:46:54 -0800430 boolean avail = doGetShareMethodAvailable("ums");
San Mehat4270e1e2010-01-29 05:32:19 -0800431 notifyShareAvailabilityChange("ums", avail);
432 } catch (Exception ex) {
433 Log.w(TAG, "Failed to get share availability");
434 }
San Mehat207e5382010-02-04 20:46:54 -0800435 /*
436 * Now that we've done our initialization, release
437 * the hounds!
438 */
439 mReady = true;
San Mehat4270e1e2010-01-29 05:32:19 -0800440 }
441 }.start();
442 }
443
444 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800445 * Callback from NativeDaemonConnector
446 */
447 public boolean onEvent(int code, String raw, String[] cooked) {
448 Intent in = null;
449
San Mehat4270e1e2010-01-29 05:32:19 -0800450 if (code == VoldResponseCode.VolumeStateChange) {
451 /*
452 * One of the volumes we're managing has changed state.
453 * Format: "NNN Volume <label> <path> state changed
454 * from <old_#> (<old_str>) to <new_#> (<new_str>)"
455 */
456 notifyVolumeStateChange(
457 cooked[2], cooked[3], Integer.parseInt(cooked[7]),
458 Integer.parseInt(cooked[10]));
459 } else if (code == VoldResponseCode.ShareAvailabilityChange) {
460 // FMT: NNN Share method <method> now <available|unavailable>
461 boolean avail = false;
462 if (cooked[5].equals("available")) {
463 avail = true;
464 }
465 notifyShareAvailabilityChange(cooked[3], avail);
466 } else if ((code == VoldResponseCode.VolumeDiskInserted) ||
467 (code == VoldResponseCode.VolumeDiskRemoved) ||
468 (code == VoldResponseCode.VolumeBadRemoval)) {
469 // FMT: NNN Volume <label> <mountpoint> disk inserted (<major>:<minor>)
470 // FMT: NNN Volume <label> <mountpoint> disk removed (<major>:<minor>)
471 // FMT: NNN Volume <label> <mountpoint> bad removal (<major>:<minor>)
472 final String label = cooked[2];
473 final String path = cooked[3];
474 int major = -1;
475 int minor = -1;
476
477 try {
478 String devComp = cooked[6].substring(1, cooked[6].length() -1);
479 String[] devTok = devComp.split(":");
480 major = Integer.parseInt(devTok[0]);
481 minor = Integer.parseInt(devTok[1]);
482 } catch (Exception ex) {
483 Log.e(TAG, "Failed to parse major/minor", ex);
484 }
485
San Mehat4270e1e2010-01-29 05:32:19 -0800486 if (code == VoldResponseCode.VolumeDiskInserted) {
487 new Thread() {
488 public void run() {
489 try {
490 int rc;
San Mehatb1043402010-02-05 08:26:50 -0800491 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
San Mehat4270e1e2010-01-29 05:32:19 -0800492 Log.w(TAG, String.format("Insertion mount failed (%d)", rc));
493 }
494 } catch (Exception ex) {
495 Log.w(TAG, "Failed to mount media on insertion", ex);
496 }
497 }
498 }.start();
499 } else if (code == VoldResponseCode.VolumeDiskRemoved) {
500 /*
501 * This event gets trumped if we're already in BAD_REMOVAL state
502 */
503 if (getVolumeState(path).equals(Environment.MEDIA_BAD_REMOVAL)) {
504 return true;
505 }
506 /* Send the media unmounted event first */
507 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
508 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
509 mContext.sendBroadcast(in);
510
511 updatePublicVolumeState(path, Environment.MEDIA_REMOVED);
512 in = new Intent(Intent.ACTION_MEDIA_REMOVED, Uri.parse("file://" + path));
513 } else if (code == VoldResponseCode.VolumeBadRemoval) {
514 /* Send the media unmounted event first */
515 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
516 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
517 mContext.sendBroadcast(in);
518
519 updatePublicVolumeState(path, Environment.MEDIA_BAD_REMOVAL);
520 in = new Intent(Intent.ACTION_MEDIA_BAD_REMOVAL, Uri.parse("file://" + path));
521 } else {
522 Log.e(TAG, String.format("Unknown code {%d}", code));
523 }
524 } else {
525 return false;
526 }
527
528 if (in != null) {
529 mContext.sendBroadcast(in);
530 }
531 return true;
532 }
533
San Mehat207e5382010-02-04 20:46:54 -0800534 private void notifyVolumeStateChange(String label, String path, int oldState, int newState) {
San Mehat4270e1e2010-01-29 05:32:19 -0800535 String vs = getVolumeState(path);
536
537 Intent in = null;
538
539 if (newState == VolumeState.Init) {
540 } else if (newState == VolumeState.NoMedia) {
541 // NoMedia is handled via Disk Remove events
542 } else if (newState == VolumeState.Idle) {
543 /*
544 * Don't notify if we're in BAD_REMOVAL, NOFS, UNMOUNTABLE, or
545 * if we're in the process of enabling UMS
546 */
547 if (!vs.equals(
548 Environment.MEDIA_BAD_REMOVAL) && !vs.equals(
549 Environment.MEDIA_NOFS) && !vs.equals(
550 Environment.MEDIA_UNMOUNTABLE) && !mUmsEnabling) {
551 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
552 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
553 }
554 } else if (newState == VolumeState.Pending) {
555 } else if (newState == VolumeState.Checking) {
556 updatePublicVolumeState(path, Environment.MEDIA_CHECKING);
557 in = new Intent(Intent.ACTION_MEDIA_CHECKING, Uri.parse("file://" + path));
558 } else if (newState == VolumeState.Mounted) {
559 updatePublicVolumeState(path, Environment.MEDIA_MOUNTED);
560 // Update media status on PackageManagerService to mount packages on sdcard
561 mPms.updateExternalMediaStatus(true);
562 in = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + path));
563 in.putExtra("read-only", false);
564 } else if (newState == VolumeState.Unmounting) {
565 mPms.updateExternalMediaStatus(false);
566 in = new Intent(Intent.ACTION_MEDIA_EJECT, Uri.parse("file://" + path));
567 } else if (newState == VolumeState.Formatting) {
568 } else if (newState == VolumeState.Shared) {
569 /* Send the media unmounted event first */
570 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTED);
571 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTED, Uri.parse("file://" + path));
572 mContext.sendBroadcast(in);
573
574 updatePublicVolumeState(path, Environment.MEDIA_SHARED);
575 in = new Intent(Intent.ACTION_MEDIA_SHARED, Uri.parse("file://" + path));
576 } else if (newState == VolumeState.SharedMnt) {
577 Log.e(TAG, "Live shared mounts not supported yet!");
578 return;
579 } else {
580 Log.e(TAG, "Unhandled VolumeState {" + newState + "}");
581 }
582
583 if (in != null) {
584 mContext.sendBroadcast(in);
585 }
586 }
587
San Mehat207e5382010-02-04 20:46:54 -0800588 private boolean doGetShareMethodAvailable(String method) {
589 ArrayList<String> rsp = mConnector.doCommand("share status " + method);
590
591 for (String line : rsp) {
592 String []tok = line.split(" ");
593 int code;
594 try {
595 code = Integer.parseInt(tok[0]);
596 } catch (NumberFormatException nfe) {
597 Log.e(TAG, String.format("Error parsing code %s", tok[0]));
598 return false;
599 }
600 if (code == VoldResponseCode.ShareStatusResult) {
601 if (tok[2].equals("available"))
602 return true;
603 return false;
604 } else {
605 Log.e(TAG, String.format("Unexpected response code %d", code));
606 return false;
607 }
608 }
609 Log.e(TAG, "Got an empty response");
610 return false;
611 }
612
613 private int doMountVolume(String path) {
San Mehatb1043402010-02-05 08:26:50 -0800614 int rc = StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800615
616 try {
617 mConnector.doCommand(String.format("volume mount %s", path));
618 } catch (NativeDaemonConnectorException e) {
619 /*
620 * Mount failed for some reason
621 */
622 Intent in = null;
623 int code = e.getCode();
624 if (code == VoldResponseCode.OpFailedNoMedia) {
625 /*
626 * Attempt to mount but no media inserted
627 */
San Mehatb1043402010-02-05 08:26:50 -0800628 rc = StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800629 } else if (code == VoldResponseCode.OpFailedMediaBlank) {
630 /*
631 * Media is blank or does not contain a supported filesystem
632 */
633 updatePublicVolumeState(path, Environment.MEDIA_NOFS);
634 in = new Intent(Intent.ACTION_MEDIA_NOFS, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800635 rc = StorageResultCode.OperationFailedMediaBlank;
San Mehat207e5382010-02-04 20:46:54 -0800636 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
637 /*
638 * Volume consistency check failed
639 */
640 updatePublicVolumeState(path, Environment.MEDIA_UNMOUNTABLE);
641 in = new Intent(Intent.ACTION_MEDIA_UNMOUNTABLE, Uri.parse("file://" + path));
San Mehatb1043402010-02-05 08:26:50 -0800642 rc = StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800643 } else {
San Mehatb1043402010-02-05 08:26:50 -0800644 rc = StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800645 }
646
647 /*
648 * Send broadcast intent (if required for the failure)
649 */
650 if (in != null) {
651 mContext.sendBroadcast(in);
652 }
653 }
654
655 return rc;
656 }
657
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800658 /*
659 * If force is not set, we do not unmount if there are
660 * processes holding references to the volume about to be unmounted.
661 * If force is set, all the processes holding references need to be
662 * killed via the ActivityManager before actually unmounting the volume.
663 * This might even take a while and might be retried after timed delays
664 * to make sure we dont end up in an instable state and kill some core
665 * processes.
666 */
San Mehatd9709982010-02-18 11:43:03 -0800667 private int doUnmountVolume(String path, boolean force) {
San Mehat59443a62010-02-09 13:28:45 -0800668 if (!getVolumeState(path).equals(Environment.MEDIA_MOUNTED)) {
San Mehat207e5382010-02-04 20:46:54 -0800669 return VoldResponseCode.OpFailedVolNotMounted;
670 }
671
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800672 // We unmounted the volume. No of the asec containers are available now.
673 synchronized (mAsecMountSet) {
674 mAsecMountSet.clear();
675 }
San Mehat207e5382010-02-04 20:46:54 -0800676 // Notify PackageManager of potential media removal and deal with
677 // return code later on. The caller of this api should be aware or have been
678 // notified that the applications installed on the media will be killed.
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800679 // Redundant probably. But no harm in updating state again.
San Mehat207e5382010-02-04 20:46:54 -0800680 mPms.updateExternalMediaStatus(false);
681 try {
San Mehatd9709982010-02-18 11:43:03 -0800682 mConnector.doCommand(String.format(
683 "volume unmount %s%s", path, (force ? " force" : "")));
San Mehatb1043402010-02-05 08:26:50 -0800684 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800685 } catch (NativeDaemonConnectorException e) {
686 // Don't worry about mismatch in PackageManager since the
687 // call back will handle the status changes any way.
688 int code = e.getCode();
689 if (code == VoldResponseCode.OpFailedVolNotMounted) {
San Mehata181b212010-02-11 06:50:20 -0800690 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehatd9709982010-02-18 11:43:03 -0800691 } else if (code == VoldResponseCode.OpFailedStorageBusy) {
692 return StorageResultCode.OperationFailedStorageBusy;
San Mehat207e5382010-02-04 20:46:54 -0800693 } else {
San Mehatb1043402010-02-05 08:26:50 -0800694 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800695 }
696 }
697 }
698
699 private int doFormatVolume(String path) {
700 try {
701 String cmd = String.format("volume format %s", path);
702 mConnector.doCommand(cmd);
San Mehatb1043402010-02-05 08:26:50 -0800703 return StorageResultCode.OperationSucceeded;
San Mehat207e5382010-02-04 20:46:54 -0800704 } catch (NativeDaemonConnectorException e) {
705 int code = e.getCode();
706 if (code == VoldResponseCode.OpFailedNoMedia) {
San Mehatb1043402010-02-05 08:26:50 -0800707 return StorageResultCode.OperationFailedNoMedia;
San Mehat207e5382010-02-04 20:46:54 -0800708 } else if (code == VoldResponseCode.OpFailedMediaCorrupt) {
San Mehatb1043402010-02-05 08:26:50 -0800709 return StorageResultCode.OperationFailedMediaCorrupt;
San Mehat207e5382010-02-04 20:46:54 -0800710 } else {
San Mehatb1043402010-02-05 08:26:50 -0800711 return StorageResultCode.OperationFailedInternalError;
San Mehat207e5382010-02-04 20:46:54 -0800712 }
713 }
714 }
715
San Mehatb1043402010-02-05 08:26:50 -0800716 private boolean doGetVolumeShared(String path, String method) {
717 String cmd = String.format("volume shared %s %s", path, method);
718 ArrayList<String> rsp = mConnector.doCommand(cmd);
719
720 for (String line : rsp) {
721 String []tok = line.split(" ");
722 int code;
723 try {
724 code = Integer.parseInt(tok[0]);
725 } catch (NumberFormatException nfe) {
726 Log.e(TAG, String.format("Error parsing code %s", tok[0]));
727 return false;
728 }
729 if (code == VoldResponseCode.ShareEnabledResult) {
730 if (tok[2].equals("enabled"))
731 return true;
732 return false;
733 } else {
734 Log.e(TAG, String.format("Unexpected response code %d", code));
735 return false;
736 }
737 }
738 Log.e(TAG, "Got an empty response");
739 return false;
740 }
741
San Mehat207e5382010-02-04 20:46:54 -0800742 private void notifyShareAvailabilityChange(String method, final boolean avail) {
San Mehat4270e1e2010-01-29 05:32:19 -0800743 if (!method.equals("ums")) {
744 Log.w(TAG, "Ignoring unsupported share method {" + method + "}");
745 return;
746 }
747
748 synchronized (mListeners) {
749 for (int i = mListeners.size() -1; i >= 0; i--) {
750 MountServiceBinderListener bl = mListeners.get(i);
751 try {
San Mehatb1043402010-02-05 08:26:50 -0800752 bl.mListener.onUsbMassStorageConnectionChanged(avail);
San Mehat4270e1e2010-01-29 05:32:19 -0800753 } catch (RemoteException rex) {
754 Log.e(TAG, "Listener dead");
755 mListeners.remove(i);
756 } catch (Exception ex) {
757 Log.e(TAG, "Listener failed", ex);
758 }
759 }
760 }
761
San Mehat207e5382010-02-04 20:46:54 -0800762 if (mBooted == true) {
San Mehat6a965af22010-02-24 17:47:30 -0800763 sendUmsIntent(avail);
764 } else {
765 mSendUmsConnectedOnBoot = avail;
San Mehat4270e1e2010-01-29 05:32:19 -0800766 }
San Mehat4270e1e2010-01-29 05:32:19 -0800767 }
768
San Mehat6a965af22010-02-24 17:47:30 -0800769 private void sendUmsIntent(boolean c) {
770 mContext.sendBroadcast(
771 new Intent((c ? Intent.ACTION_UMS_CONNECTED : Intent.ACTION_UMS_DISCONNECTED)));
772 }
773
San Mehat207e5382010-02-04 20:46:54 -0800774 private void validatePermission(String perm) {
San Mehat4270e1e2010-01-29 05:32:19 -0800775 if (mContext.checkCallingOrSelfPermission(perm) != PackageManager.PERMISSION_GRANTED) {
776 throw new SecurityException(String.format("Requires %s permission", perm));
777 }
778 }
779
780 /**
San Mehat207e5382010-02-04 20:46:54 -0800781 * Constructs a new MountService instance
782 *
783 * @param context Binder context for this service
784 */
785 public MountService(Context context) {
786 mContext = context;
787
San Mehat207e5382010-02-04 20:46:54 -0800788 // XXX: This will go away soon in favor of IMountServiceObserver
789 mPms = (PackageManagerService) ServiceManager.getService("package");
790
791 mContext.registerReceiver(mBroadcastReceiver,
792 new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
793
794 mListeners = new ArrayList<MountServiceBinderListener>();
795
Marco Nelissenc34ebce2010-02-18 13:39:41 -0800796 /*
797 * Vold does not run in the simulator, so pretend the connector thread
798 * ran and did its thing.
799 */
800 if ("simulator".equals(SystemProperties.get("ro.product.device"))) {
801 mReady = true;
802 mUmsEnabling = true;
803 return;
804 }
805
San Mehat207e5382010-02-04 20:46:54 -0800806 mConnector = new NativeDaemonConnector(this, "vold", 10, "VoldConnector");
807 mReady = false;
808 Thread thread = new Thread(mConnector, NativeDaemonConnector.class.getName());
809 thread.start();
810 }
811
812 /**
San Mehat4270e1e2010-01-29 05:32:19 -0800813 * Exposed API calls below here
814 */
815
816 public void registerListener(IMountServiceListener listener) {
817 synchronized (mListeners) {
818 MountServiceBinderListener bl = new MountServiceBinderListener(listener);
819 try {
820 listener.asBinder().linkToDeath(bl, 0);
821 mListeners.add(bl);
822 } catch (RemoteException rex) {
823 Log.e(TAG, "Failed to link to listener death");
824 }
825 }
826 }
827
828 public void unregisterListener(IMountServiceListener listener) {
829 synchronized (mListeners) {
830 for(MountServiceBinderListener bl : mListeners) {
831 if (bl.mListener == listener) {
832 mListeners.remove(mListeners.indexOf(bl));
833 return;
834 }
835 }
836 }
837 }
838
839 public void shutdown() {
840 validatePermission(android.Manifest.permission.SHUTDOWN);
841
842 Log.i(TAG, "Shutting down");
843
844 String path = Environment.getExternalStorageDirectory().getPath();
845 String state = getVolumeState(path);
San Mehat91c77612010-01-07 10:39:41 -0800846
847 if (state.equals(Environment.MEDIA_SHARED)) {
848 /*
849 * If the media is currently shared, unshare it.
850 * XXX: This is still dangerous!. We should not
851 * be rebooting at *all* if UMS is enabled, since
852 * the UMS host could have dirty FAT cache entries
853 * yet to flush.
854 */
San Mehatb1043402010-02-05 08:26:50 -0800855 if (setUsbMassStorageEnabled(false) != StorageResultCode.OperationSucceeded) {
San Mehat4270e1e2010-01-29 05:32:19 -0800856 Log.e(TAG, "UMS disable on shutdown failed");
San Mehat91c77612010-01-07 10:39:41 -0800857 }
858 } else if (state.equals(Environment.MEDIA_CHECKING)) {
859 /*
860 * If the media is being checked, then we need to wait for
861 * it to complete before being able to proceed.
862 */
863 // XXX: @hackbod - Should we disable the ANR timer here?
864 int retries = 30;
865 while (state.equals(Environment.MEDIA_CHECKING) && (retries-- >=0)) {
866 try {
867 Thread.sleep(1000);
868 } catch (InterruptedException iex) {
869 Log.e(TAG, "Interrupted while waiting for media", iex);
870 break;
871 }
872 state = Environment.getExternalStorageState();
873 }
874 if (retries == 0) {
875 Log.e(TAG, "Timed out waiting for media to check");
876 }
877 }
878
879 if (state.equals(Environment.MEDIA_MOUNTED)) {
880 /*
881 * If the media is mounted, then gracefully unmount it.
882 */
San Mehatd9709982010-02-18 11:43:03 -0800883 if (doUnmountVolume(path, true) != StorageResultCode.OperationSucceeded) {
San Mehat4270e1e2010-01-29 05:32:19 -0800884 Log.e(TAG, "Failed to unmount media for shutdown");
885 }
886 }
887 }
888
San Mehatb1043402010-02-05 08:26:50 -0800889 public boolean isUsbMassStorageConnected() {
San Mehat207e5382010-02-04 20:46:54 -0800890 waitForReady();
San Mehat91c77612010-01-07 10:39:41 -0800891
San Mehatb1043402010-02-05 08:26:50 -0800892 if (mUmsEnabling) {
893 return true;
San Mehat7fd0fee2009-12-17 07:12:23 -0800894 }
San Mehatb1043402010-02-05 08:26:50 -0800895 return doGetShareMethodAvailable("ums");
896 }
897
898 public int setUsbMassStorageEnabled(boolean enable) {
899 waitForReady();
900
901 return doShareUnshareVolume(Environment.getExternalStorageDirectory().getPath(), "ums", enable);
902 }
903
904 public boolean isUsbMassStorageEnabled() {
905 waitForReady();
906 return doGetVolumeShared(Environment.getExternalStorageDirectory().getPath(), "ums");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
San Mehat4270e1e2010-01-29 05:32:19 -0800908
San Mehat7fd0fee2009-12-17 07:12:23 -0800909 /**
910 * @return state of the volume at the specified mount point
911 */
San Mehat4270e1e2010-01-29 05:32:19 -0800912 public String getVolumeState(String mountPoint) {
San Mehat7fd0fee2009-12-17 07:12:23 -0800913 /*
914 * XXX: Until we have multiple volume discovery, just hardwire
915 * this to /sdcard
916 */
917 if (!mountPoint.equals(Environment.getExternalStorageDirectory().getPath())) {
918 Log.w(TAG, "getVolumeState(" + mountPoint + "): Unknown volume");
919 throw new IllegalArgumentException();
920 }
921
922 return mLegacyState;
923 }
924
San Mehat4270e1e2010-01-29 05:32:19 -0800925 public int mountVolume(String path) {
926 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat4270e1e2010-01-29 05:32:19 -0800927
San Mehat207e5382010-02-04 20:46:54 -0800928 waitForReady();
929 return doMountVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 }
931
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800932 public void unmountVolume(String path, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -0800933 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -0800934 waitForReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -0800936 UnmountCallBack ucb = new UnmountCallBack(path, force);
937 mHandler.sendMessage(mHandler.obtainMessage(H_UNMOUNT_PM_UPDATE, ucb));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
939
San Mehat4270e1e2010-01-29 05:32:19 -0800940 public int formatVolume(String path) {
941 validatePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
San Mehat207e5382010-02-04 20:46:54 -0800942 waitForReady();
San Mehat5b77dab2010-01-26 13:28:50 -0800943
San Mehat207e5382010-02-04 20:46:54 -0800944 return doFormatVolume(path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 }
946
San Mehatc1b4ce92010-02-16 17:13:03 -0800947 public int []getStorageUsers(String path) {
948 validatePermission(android.Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS);
949 waitForReady();
950 try {
951 String[] r = mConnector.doListCommand(
952 String.format("storage users %s", path),
953 VoldResponseCode.StorageUsersListResult);
954 // FMT: <pid> <process name>
955 int[] data = new int[r.length];
956 for (int i = 0; i < r.length; i++) {
957 String []tok = r[i].split(" ");
958 try {
959 data[i] = Integer.parseInt(tok[0]);
960 } catch (NumberFormatException nfe) {
961 Log.e(TAG, String.format("Error parsing pid %s", tok[0]));
962 return new int[0];
963 }
964 }
965 return data;
966 } catch (NativeDaemonConnectorException e) {
967 Log.e(TAG, "Failed to retrieve storage users list", e);
968 return new int[0];
969 }
970 }
971
San Mehatb1043402010-02-05 08:26:50 -0800972 private void warnOnNotMounted() {
973 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
974 Log.w(TAG, "getSecureContainerList() called when storage not mounted");
975 }
976 }
977
San Mehat4270e1e2010-01-29 05:32:19 -0800978 public String[] getSecureContainerList() {
979 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -0800980 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -0800981 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -0800982
San Mehat4270e1e2010-01-29 05:32:19 -0800983 try {
984 return mConnector.doListCommand("asec list", VoldResponseCode.AsecListResult);
985 } catch (NativeDaemonConnectorException e) {
986 return new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 }
988 }
San Mehat36972292010-01-06 11:06:32 -0800989
San Mehat4270e1e2010-01-29 05:32:19 -0800990 public int createSecureContainer(String id, int sizeMb, String fstype,
991 String key, int ownerUid) {
992 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehat207e5382010-02-04 20:46:54 -0800993 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -0800994 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -0800995
San Mehatb1043402010-02-05 08:26:50 -0800996 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -0800997 String cmd = String.format("asec create %s %d %s %s %d", id, sizeMb, fstype, key, ownerUid);
998 try {
999 mConnector.doCommand(cmd);
1000 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001001 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001002 }
San Mehata181b212010-02-11 06:50:20 -08001003
1004 if (rc == StorageResultCode.OperationSucceeded) {
1005 synchronized (mAsecMountSet) {
1006 mAsecMountSet.add(id);
1007 }
1008 }
San Mehat4270e1e2010-01-29 05:32:19 -08001009 return rc;
San Mehat36972292010-01-06 11:06:32 -08001010 }
1011
San Mehat4270e1e2010-01-29 05:32:19 -08001012 public int finalizeSecureContainer(String id) {
1013 validatePermission(android.Manifest.permission.ASEC_CREATE);
San Mehatb1043402010-02-05 08:26:50 -08001014 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001015
San Mehatb1043402010-02-05 08:26:50 -08001016 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001017 try {
1018 mConnector.doCommand(String.format("asec finalize %s", id));
San Mehata181b212010-02-11 06:50:20 -08001019 /*
1020 * Finalization does a remount, so no need
1021 * to update mAsecMountSet
1022 */
San Mehat4270e1e2010-01-29 05:32:19 -08001023 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001024 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001025 }
San Mehat4270e1e2010-01-29 05:32:19 -08001026 return rc;
San Mehat36972292010-01-06 11:06:32 -08001027 }
1028
San Mehatd9709982010-02-18 11:43:03 -08001029 public int destroySecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001030 validatePermission(android.Manifest.permission.ASEC_DESTROY);
San Mehat207e5382010-02-04 20:46:54 -08001031 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001032 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001033
San Mehatb1043402010-02-05 08:26:50 -08001034 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001035 try {
San Mehatd9709982010-02-18 11:43:03 -08001036 mConnector.doCommand(String.format("asec destroy %s%s", id, (force ? " force" : "")));
San Mehat4270e1e2010-01-29 05:32:19 -08001037 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001038 int code = e.getCode();
1039 if (code == VoldResponseCode.OpFailedStorageBusy) {
1040 rc = StorageResultCode.OperationFailedStorageBusy;
1041 } else {
1042 rc = StorageResultCode.OperationFailedInternalError;
1043 }
San Mehat02735bc2010-01-26 15:18:08 -08001044 }
San Mehata181b212010-02-11 06:50:20 -08001045
1046 if (rc == StorageResultCode.OperationSucceeded) {
1047 synchronized (mAsecMountSet) {
1048 if (mAsecMountSet.contains(id)) {
1049 mAsecMountSet.remove(id);
1050 }
1051 }
1052 }
1053
San Mehat4270e1e2010-01-29 05:32:19 -08001054 return rc;
San Mehat36972292010-01-06 11:06:32 -08001055 }
1056
San Mehat4270e1e2010-01-29 05:32:19 -08001057 public int mountSecureContainer(String id, String key, int ownerUid) {
1058 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001059 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001060 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001061
San Mehata181b212010-02-11 06:50:20 -08001062 synchronized (mAsecMountSet) {
1063 if (mAsecMountSet.contains(id)) {
1064 return StorageResultCode.OperationFailedStorageMounted;
1065 }
1066 }
1067
San Mehatb1043402010-02-05 08:26:50 -08001068 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001069 String cmd = String.format("asec mount %s %s %d", id, key, ownerUid);
1070 try {
1071 mConnector.doCommand(cmd);
1072 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001073 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001074 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001075
1076 if (rc == StorageResultCode.OperationSucceeded) {
1077 synchronized (mAsecMountSet) {
1078 mAsecMountSet.add(id);
1079 }
1080 }
San Mehat4270e1e2010-01-29 05:32:19 -08001081 return rc;
San Mehat36972292010-01-06 11:06:32 -08001082 }
1083
San Mehatd9709982010-02-18 11:43:03 -08001084 public int unmountSecureContainer(String id, boolean force) {
San Mehat4270e1e2010-01-29 05:32:19 -08001085 validatePermission(android.Manifest.permission.ASEC_MOUNT_UNMOUNT);
San Mehat207e5382010-02-04 20:46:54 -08001086 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001087 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001088
San Mehat6cdd9c02010-02-09 14:45:20 -08001089 synchronized (mAsecMountSet) {
1090 if (!mAsecMountSet.contains(id)) {
San Mehata181b212010-02-11 06:50:20 -08001091 return StorageResultCode.OperationFailedStorageNotMounted;
San Mehat6cdd9c02010-02-09 14:45:20 -08001092 }
1093 }
1094
San Mehatb1043402010-02-05 08:26:50 -08001095 int rc = StorageResultCode.OperationSucceeded;
San Mehatd9709982010-02-18 11:43:03 -08001096 String cmd = String.format("asec unmount %s%s", id, (force ? " force" : ""));
San Mehat4270e1e2010-01-29 05:32:19 -08001097 try {
1098 mConnector.doCommand(cmd);
1099 } catch (NativeDaemonConnectorException e) {
San Mehatd9709982010-02-18 11:43:03 -08001100 int code = e.getCode();
1101 if (code == VoldResponseCode.OpFailedStorageBusy) {
1102 rc = StorageResultCode.OperationFailedStorageBusy;
1103 } else {
1104 rc = StorageResultCode.OperationFailedInternalError;
1105 }
San Mehat02735bc2010-01-26 15:18:08 -08001106 }
San Mehat6cdd9c02010-02-09 14:45:20 -08001107
1108 if (rc == StorageResultCode.OperationSucceeded) {
1109 synchronized (mAsecMountSet) {
1110 mAsecMountSet.remove(id);
1111 }
1112 }
San Mehat4270e1e2010-01-29 05:32:19 -08001113 return rc;
San Mehat9dba7092010-01-18 06:47:41 -08001114 }
1115
San Mehat6cdd9c02010-02-09 14:45:20 -08001116 public boolean isSecureContainerMounted(String id) {
1117 validatePermission(android.Manifest.permission.ASEC_ACCESS);
1118 waitForReady();
1119 warnOnNotMounted();
1120
1121 synchronized (mAsecMountSet) {
1122 return mAsecMountSet.contains(id);
1123 }
1124 }
1125
San Mehat4270e1e2010-01-29 05:32:19 -08001126 public int renameSecureContainer(String oldId, String newId) {
1127 validatePermission(android.Manifest.permission.ASEC_RENAME);
San Mehat207e5382010-02-04 20:46:54 -08001128 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001129 warnOnNotMounted();
San Mehat4270e1e2010-01-29 05:32:19 -08001130
San Mehata181b212010-02-11 06:50:20 -08001131 synchronized (mAsecMountSet) {
San Mehat85451ee2010-02-24 08:54:18 -08001132 /*
1133 * Because a mounted container has active internal state which cannot be
1134 * changed while active, we must ensure both ids are not currently mounted.
1135 */
1136 if (mAsecMountSet.contains(oldId) || mAsecMountSet.contains(newId)) {
San Mehata181b212010-02-11 06:50:20 -08001137 return StorageResultCode.OperationFailedStorageMounted;
1138 }
1139 }
1140
San Mehatb1043402010-02-05 08:26:50 -08001141 int rc = StorageResultCode.OperationSucceeded;
San Mehat4270e1e2010-01-29 05:32:19 -08001142 String cmd = String.format("asec rename %s %s", oldId, newId);
1143 try {
1144 mConnector.doCommand(cmd);
1145 } catch (NativeDaemonConnectorException e) {
San Mehatb1043402010-02-05 08:26:50 -08001146 rc = StorageResultCode.OperationFailedInternalError;
San Mehat02735bc2010-01-26 15:18:08 -08001147 }
Suchi Amalapurapuc42e29e2010-02-22 16:03:53 -08001148 if (rc == StorageResultCode.OperationSucceeded) {
1149 synchronized (mAsecMountSet) {
1150 if (!mAsecMountSet.contains(newId)) {
1151 mAsecMountSet.add(newId);
1152 }
1153 }
1154 }
San Mehata181b212010-02-11 06:50:20 -08001155
San Mehat4270e1e2010-01-29 05:32:19 -08001156 return rc;
San Mehat45f61042010-01-23 08:12:43 -08001157 }
1158
San Mehat4270e1e2010-01-29 05:32:19 -08001159 public String getSecureContainerPath(String id) {
1160 validatePermission(android.Manifest.permission.ASEC_ACCESS);
San Mehat207e5382010-02-04 20:46:54 -08001161 waitForReady();
San Mehatb1043402010-02-05 08:26:50 -08001162 warnOnNotMounted();
San Mehatf919cd022010-02-04 15:10:38 -08001163
San Mehat4270e1e2010-01-29 05:32:19 -08001164 ArrayList<String> rsp = mConnector.doCommand("asec path " + id);
San Mehat36972292010-01-06 11:06:32 -08001165
San Mehat22dd86e2010-01-12 12:21:18 -08001166 for (String line : rsp) {
1167 String []tok = line.split(" ");
1168 int code = Integer.parseInt(tok[0]);
1169 if (code == VoldResponseCode.AsecPathResult) {
1170 return tok[1];
1171 } else {
San Mehat4270e1e2010-01-29 05:32:19 -08001172 Log.e(TAG, String.format("Unexpected response code %d", code));
1173 return "";
San Mehat22dd86e2010-01-12 12:21:18 -08001174 }
1175 }
San Mehat4270e1e2010-01-29 05:32:19 -08001176
1177 Log.e(TAG, "Got an empty response");
1178 return "";
San Mehat22dd86e2010-01-12 12:21:18 -08001179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180}
1181