Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.storage; |
| 18 | |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 19 | import android.os.ParcelFileDescriptor; |
| 20 | import android.system.ErrnoException; |
| 21 | import android.system.Os; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 22 | import com.android.internal.util.Preconditions; |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 23 | import libcore.io.IoUtils; |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 24 | import java.io.File; |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 25 | import java.io.FileNotFoundException; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 26 | import java.util.concurrent.BlockingQueue; |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 27 | |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 28 | /** |
| 29 | * Runnable that delegates FUSE command from the kernel to application. |
| 30 | * run() blocks until all opened files on the FUSE mount point are closed. So this should be run in |
| 31 | * a separated thread. |
| 32 | */ |
| 33 | public class AppFuseBridge implements Runnable, AutoCloseable { |
| 34 | public static final String TAG = "AppFuseBridge"; |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 35 | |
| 36 | /** |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 37 | * The path AppFuse is mounted to. |
| 38 | * The first number is UID who is mounting the FUSE. |
| 39 | * THe second number is mount ID. |
| 40 | * The path must be sync with vold. |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 41 | */ |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 42 | private static final String APPFUSE_MOUNT_NAME_TEMPLATE = "/mnt/appfuse/%d_%d"; |
| 43 | |
| 44 | private final IMountScope mMountScope; |
| 45 | private final ParcelFileDescriptor mProxyFd; |
| 46 | private final BlockingQueue<Boolean> mChannel; |
| 47 | |
| 48 | /** |
| 49 | * @param mountScope Listener to unmount mount point. |
| 50 | * @param proxyFd FD of socket pair. Ownership of FD is taken by AppFuseBridge. |
| 51 | * @param channel Channel that the runnable send mount result to. |
| 52 | */ |
| 53 | public AppFuseBridge( |
| 54 | IMountScope mountScope, ParcelFileDescriptor proxyFd, BlockingQueue<Boolean> channel) { |
| 55 | Preconditions.checkNotNull(mountScope); |
| 56 | Preconditions.checkNotNull(proxyFd); |
| 57 | Preconditions.checkNotNull(channel); |
| 58 | mMountScope = mountScope; |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 59 | mProxyFd = proxyFd; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 60 | mChannel = channel; |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void run() { |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 65 | try { |
| 66 | // deviceFd and proxyFd must be closed in native_start_loop. |
| 67 | native_start_loop( |
| 68 | mMountScope.getDeviceFileDescriptor().detachFd(), |
| 69 | mProxyFd.detachFd()); |
| 70 | } finally { |
| 71 | close(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public static ParcelFileDescriptor openFile(int uid, int mountId, int fileId, int mode) |
| 76 | throws FileNotFoundException { |
| 77 | final File mountPoint = getMountPoint(uid, mountId); |
| 78 | try { |
| 79 | if (Os.stat(mountPoint.getPath()).st_ino != 1) { |
| 80 | throw new FileNotFoundException("Could not find bridge mount point."); |
| 81 | } |
| 82 | } catch (ErrnoException e) { |
| 83 | throw new FileNotFoundException( |
| 84 | "Failed to stat mount point: " + mountPoint.getParent()); |
| 85 | } |
| 86 | return ParcelFileDescriptor.open(new File(mountPoint, String.valueOf(fileId)), mode); |
| 87 | } |
| 88 | |
| 89 | private static File getMountPoint(int uid, int mountId) { |
| 90 | return new File(String.format(APPFUSE_MOUNT_NAME_TEMPLATE, uid, mountId)); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void close() { |
| 95 | IoUtils.closeQuietly(mMountScope); |
| 96 | IoUtils.closeQuietly(mProxyFd); |
| 97 | // Invoke countDown here in case where close is invoked before mount. |
| 98 | mChannel.offer(false); |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Used by com_android_server_storage_AppFuse.cpp. |
| 102 | private void onMount() { |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 103 | mChannel.offer(true); |
| 104 | } |
| 105 | |
| 106 | public static interface IMountScope extends AutoCloseable { |
| 107 | ParcelFileDescriptor getDeviceFileDescriptor(); |
Daichi Hirono | 3ff1c01 | 2016-10-28 12:53:44 +0900 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private native boolean native_start_loop(int deviceFd, int proxyFd); |
| 111 | } |