bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef ANDROID_VOLD_UTILS_H |
| 18 | #define ANDROID_VOLD_UTILS_H |
| 19 | |
| 20 | #include "KeyBuffer.h" |
| 21 | |
| 22 | #include <android-base/macros.h> |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 23 | #include <android-base/unique_fd.h> |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 24 | #include <cutils/multiuser.h> |
| 25 | #include <selinux/selinux.h> |
| 26 | #include <utils/Errors.h> |
| 27 | |
| 28 | #include <chrono> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | struct DIR; |
| 33 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 34 | static const char* kPropFuse = "persist.sys.fuse"; |
| 35 | static const char* kVoldAppDataIsolationEnabled = "persist.sys.vold_app_data_isolation_enabled"; |
| 36 | static const char* kExternalStorageSdcardfs = "external_storage.sdcardfs.enabled"; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 37 | |
| 38 | /* SELinux contexts used depending on the block device type */ |
| 39 | extern security_context_t sBlkidContext; |
| 40 | extern security_context_t sBlkidUntrustedContext; |
| 41 | extern security_context_t sFsckContext; |
| 42 | extern security_context_t sFsckUntrustedContext; |
| 43 | |
| 44 | // TODO remove this with better solution, b/64143519 |
| 45 | extern bool sSleepOnUnmount; |
| 46 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 47 | std::string GetFuseMountPathForUser(userid_t user_id, const std::string& relative_upper_path); |
| 48 | |
| 49 | android::status_t CreateDeviceNode(const std::string& path, dev_t dev); |
| 50 | android::status_t DestroyDeviceNode(const std::string& path); |
| 51 | |
| 52 | android::status_t AbortFuseConnections(); |
| 53 | |
| 54 | int SetQuotaInherit(const std::string& path); |
| 55 | int SetQuotaProjectId(const std::string& path, long projectId); |
| 56 | /* |
| 57 | * Creates and sets up an application-specific path on external |
| 58 | * storage with the correct ACL and project ID (if needed). |
| 59 | * |
| 60 | * ONLY for use with app-specific data directories on external storage! |
| 61 | * (eg, /Android/data/com.foo, /Android/obb/com.foo, etc.) |
| 62 | */ |
| 63 | int PrepareAppDirFromRoot(const std::string& path, const std::string& root, int appUid, |
| 64 | bool fixupExisting); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 65 | |
| 66 | /* fs_prepare_dir wrapper that creates with SELinux context */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 67 | android::status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 68 | |
| 69 | /* Really unmounts the path, killing active processes along the way */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 70 | android::status_t ForceUnmount(const std::string& path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 71 | |
| 72 | /* Kills any processes using given path */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 73 | android::status_t KillProcessesUsingPath(const std::string& path); |
| 74 | |
| 75 | /* Kills any processes using given mount prifix */ |
| 76 | android::status_t KillProcessesWithMountPrefix(const std::string& path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 77 | |
| 78 | /* Creates bind mount from source to target */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 79 | android::status_t BindMount(const std::string& source, const std::string& target); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 80 | |
| 81 | /** Creates a symbolic link to target */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 82 | android::status_t Symlink(const std::string& target, const std::string& linkpath); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 83 | |
| 84 | /** Calls unlink(2) at linkpath */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 85 | android::status_t Unlink(const std::string& linkpath); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 86 | |
| 87 | /** Creates the given directory if it is not already available */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 88 | android::status_t CreateDir(const std::string& dir, mode_t mode); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 89 | |
| 90 | bool FindValue(const std::string& raw, const std::string& key, std::string* value); |
| 91 | |
| 92 | /* Reads filesystem metadata from device at path */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 93 | android::status_t ReadMetadata(const std::string& path, std::string* fsType, std::string* fsUuid, |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 94 | std::string* fsLabel); |
| 95 | |
| 96 | /* Reads filesystem metadata from untrusted device at path */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 97 | android::status_t ReadMetadataUntrusted(const std::string& path, std::string* fsType, std::string* fsUuid, |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 98 | std::string* fsLabel); |
| 99 | |
| 100 | /* Returns either WEXITSTATUS() status, or a negative errno */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 101 | android::status_t ForkExecvp(const std::vector<std::string>& args, std::vector<std::string>* output = nullptr, |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 102 | security_context_t context = nullptr); |
| 103 | |
| 104 | pid_t ForkExecvpAsync(const std::vector<std::string>& args); |
| 105 | |
| 106 | /* Gets block device size in bytes */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 107 | android::status_t GetBlockDevSize(int fd, uint64_t* size); |
| 108 | android::status_t GetBlockDevSize(const std::string& path, uint64_t* size); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 109 | /* Gets block device size in 512 byte sectors */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 110 | android::status_t GetBlockDev512Sectors(const std::string& path, uint64_t* nr_sec); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 111 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 112 | android::status_t ReadRandomBytes(size_t bytes, std::string& out); |
| 113 | android::status_t ReadRandomBytes(size_t bytes, char* buffer); |
| 114 | android::status_t GenerateRandomUuid(std::string& out); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 115 | |
| 116 | /* Converts hex string to raw bytes, ignoring [ :-] */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 117 | android::status_t HexToStr(const std::string& hex, std::string& str); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 118 | /* Converts raw bytes to hex string */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 119 | android::status_t StrToHex(const std::string& str, std::string& hex); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 120 | /* Converts raw key bytes to hex string */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 121 | android::status_t StrToHex(const KeyBuffer& str, KeyBuffer& hex); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 122 | /* Normalize given hex string into consistent format */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 123 | android::status_t NormalizeHex(const std::string& in, std::string& out); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 124 | |
| 125 | uint64_t GetFreeBytes(const std::string& path); |
| 126 | uint64_t GetTreeBytes(const std::string& path); |
| 127 | |
| 128 | bool IsFilesystemSupported(const std::string& fsType); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 129 | bool IsSdcardfsUsed(); |
| 130 | bool IsFuseDaemon(const pid_t pid); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 131 | |
| 132 | /* Wipes contents of block device at given path */ |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 133 | android::status_t WipeBlockDevice(const std::string& path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 134 | |
| 135 | std::string BuildKeyPath(const std::string& partGuid); |
| 136 | |
| 137 | std::string BuildDataSystemLegacyPath(userid_t userid); |
| 138 | std::string BuildDataSystemCePath(userid_t userid); |
| 139 | std::string BuildDataSystemDePath(userid_t userid); |
| 140 | std::string BuildDataMiscLegacyPath(userid_t userid); |
| 141 | std::string BuildDataMiscCePath(userid_t userid); |
| 142 | std::string BuildDataMiscDePath(userid_t userid); |
| 143 | std::string BuildDataProfilesDePath(userid_t userid); |
| 144 | std::string BuildDataVendorCePath(userid_t userid); |
| 145 | std::string BuildDataVendorDePath(userid_t userid); |
| 146 | |
| 147 | std::string BuildDataPath(const std::string& volumeUuid); |
| 148 | std::string BuildDataMediaCePath(const std::string& volumeUuid, userid_t userid); |
| 149 | std::string BuildDataUserCePath(const std::string& volumeUuid, userid_t userid); |
| 150 | std::string BuildDataUserDePath(const std::string& volumeUuid, userid_t userid); |
| 151 | |
| 152 | dev_t GetDevice(const std::string& path); |
| 153 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 154 | android::status_t EnsureDirExists(const std::string& path, mode_t mode, uid_t uid, gid_t gid); |
| 155 | |
| 156 | android::status_t RestoreconRecursive(const std::string& path); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 157 | |
| 158 | // TODO: promote to android::base |
| 159 | bool Readlinkat(int dirfd, const std::string& path, std::string* result); |
| 160 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 161 | // Handles dynamic major assignment for virtio-block |
| 162 | bool IsVirtioBlkDevice(unsigned int major); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 163 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 164 | android::status_t UnmountTreeWithPrefix(const std::string& prefix); |
| 165 | android::status_t UnmountTree(const std::string& mountPoint); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 166 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 167 | android::status_t DeleteDirContentsAndDir(const std::string& pathname); |
| 168 | android::status_t DeleteDirContents(const std::string& pathname); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 169 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 170 | android::status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 171 | |
| 172 | bool FsyncDirectory(const std::string& dirname); |
| 173 | |
| 174 | bool writeStringToFile(const std::string& payload, const std::string& filename); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 175 | |
| 176 | void ConfigureMaxDirtyRatioForFuse(const std::string& fuse_mount, unsigned int max_ratio); |
| 177 | |
| 178 | void ConfigureReadAheadForFuse(const std::string& fuse_mount, size_t read_ahead_kb); |
| 179 | |
| 180 | android::status_t MountUserFuse(userid_t user_id, const std::string& absolute_lower_path, |
| 181 | const std::string& relative_upper_path, android::base::unique_fd* fuse_fd); |
| 182 | |
| 183 | android::status_t UnmountUserFuse(userid_t userId, const std::string& absolute_lower_path, |
| 184 | const std::string& relative_upper_path); |
| 185 | |
| 186 | android::status_t PrepareAndroidDirs(const std::string& volumeRoot); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 187 | |
| 188 | #endif |