blob: e820711f127cad111d3c6785b77012cbc3302b4b [file] [log] [blame]
bigbiff7ba75002020-04-11 20:47:09 -04001/*
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>
bigbiffa957f072021-03-07 18:20:29 -050023#include <android-base/unique_fd.h>
bigbiff7ba75002020-04-11 20:47:09 -040024#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
32struct DIR;
33
bigbiffa957f072021-03-07 18:20:29 -050034static const char* kPropFuse = "persist.sys.fuse";
35static const char* kVoldAppDataIsolationEnabled = "persist.sys.vold_app_data_isolation_enabled";
36static const char* kExternalStorageSdcardfs = "external_storage.sdcardfs.enabled";
bigbiff7ba75002020-04-11 20:47:09 -040037
38/* SELinux contexts used depending on the block device type */
39extern security_context_t sBlkidContext;
40extern security_context_t sBlkidUntrustedContext;
41extern security_context_t sFsckContext;
42extern security_context_t sFsckUntrustedContext;
43
44// TODO remove this with better solution, b/64143519
45extern bool sSleepOnUnmount;
46
bigbiffa957f072021-03-07 18:20:29 -050047std::string GetFuseMountPathForUser(userid_t user_id, const std::string& relative_upper_path);
48
49android::status_t CreateDeviceNode(const std::string& path, dev_t dev);
50android::status_t DestroyDeviceNode(const std::string& path);
51
52android::status_t AbortFuseConnections();
53
54int SetQuotaInherit(const std::string& path);
55int 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 */
63int PrepareAppDirFromRoot(const std::string& path, const std::string& root, int appUid,
64 bool fixupExisting);
bigbiff7ba75002020-04-11 20:47:09 -040065
66/* fs_prepare_dir wrapper that creates with SELinux context */
bigbiffa957f072021-03-07 18:20:29 -050067android::status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid);
bigbiff7ba75002020-04-11 20:47:09 -040068
69/* Really unmounts the path, killing active processes along the way */
bigbiffa957f072021-03-07 18:20:29 -050070android::status_t ForceUnmount(const std::string& path);
bigbiff7ba75002020-04-11 20:47:09 -040071
72/* Kills any processes using given path */
bigbiffa957f072021-03-07 18:20:29 -050073android::status_t KillProcessesUsingPath(const std::string& path);
74
75/* Kills any processes using given mount prifix */
76android::status_t KillProcessesWithMountPrefix(const std::string& path);
bigbiff7ba75002020-04-11 20:47:09 -040077
78/* Creates bind mount from source to target */
bigbiffa957f072021-03-07 18:20:29 -050079android::status_t BindMount(const std::string& source, const std::string& target);
bigbiff7ba75002020-04-11 20:47:09 -040080
81/** Creates a symbolic link to target */
bigbiffa957f072021-03-07 18:20:29 -050082android::status_t Symlink(const std::string& target, const std::string& linkpath);
bigbiff7ba75002020-04-11 20:47:09 -040083
84/** Calls unlink(2) at linkpath */
bigbiffa957f072021-03-07 18:20:29 -050085android::status_t Unlink(const std::string& linkpath);
bigbiff7ba75002020-04-11 20:47:09 -040086
87/** Creates the given directory if it is not already available */
bigbiffa957f072021-03-07 18:20:29 -050088android::status_t CreateDir(const std::string& dir, mode_t mode);
bigbiff7ba75002020-04-11 20:47:09 -040089
90bool FindValue(const std::string& raw, const std::string& key, std::string* value);
91
92/* Reads filesystem metadata from device at path */
bigbiffa957f072021-03-07 18:20:29 -050093android::status_t ReadMetadata(const std::string& path, std::string* fsType, std::string* fsUuid,
bigbiff7ba75002020-04-11 20:47:09 -040094 std::string* fsLabel);
95
96/* Reads filesystem metadata from untrusted device at path */
bigbiffa957f072021-03-07 18:20:29 -050097android::status_t ReadMetadataUntrusted(const std::string& path, std::string* fsType, std::string* fsUuid,
bigbiff7ba75002020-04-11 20:47:09 -040098 std::string* fsLabel);
99
100/* Returns either WEXITSTATUS() status, or a negative errno */
bigbiffa957f072021-03-07 18:20:29 -0500101android::status_t ForkExecvp(const std::vector<std::string>& args, std::vector<std::string>* output = nullptr,
bigbiff7ba75002020-04-11 20:47:09 -0400102 security_context_t context = nullptr);
103
104pid_t ForkExecvpAsync(const std::vector<std::string>& args);
105
106/* Gets block device size in bytes */
bigbiffa957f072021-03-07 18:20:29 -0500107android::status_t GetBlockDevSize(int fd, uint64_t* size);
108android::status_t GetBlockDevSize(const std::string& path, uint64_t* size);
bigbiff7ba75002020-04-11 20:47:09 -0400109/* Gets block device size in 512 byte sectors */
bigbiffa957f072021-03-07 18:20:29 -0500110android::status_t GetBlockDev512Sectors(const std::string& path, uint64_t* nr_sec);
bigbiff7ba75002020-04-11 20:47:09 -0400111
bigbiffa957f072021-03-07 18:20:29 -0500112android::status_t ReadRandomBytes(size_t bytes, std::string& out);
113android::status_t ReadRandomBytes(size_t bytes, char* buffer);
114android::status_t GenerateRandomUuid(std::string& out);
bigbiff7ba75002020-04-11 20:47:09 -0400115
116/* Converts hex string to raw bytes, ignoring [ :-] */
bigbiffa957f072021-03-07 18:20:29 -0500117android::status_t HexToStr(const std::string& hex, std::string& str);
bigbiff7ba75002020-04-11 20:47:09 -0400118/* Converts raw bytes to hex string */
bigbiffa957f072021-03-07 18:20:29 -0500119android::status_t StrToHex(const std::string& str, std::string& hex);
bigbiff7ba75002020-04-11 20:47:09 -0400120/* Converts raw key bytes to hex string */
bigbiffa957f072021-03-07 18:20:29 -0500121android::status_t StrToHex(const KeyBuffer& str, KeyBuffer& hex);
bigbiff7ba75002020-04-11 20:47:09 -0400122/* Normalize given hex string into consistent format */
bigbiffa957f072021-03-07 18:20:29 -0500123android::status_t NormalizeHex(const std::string& in, std::string& out);
bigbiff7ba75002020-04-11 20:47:09 -0400124
125uint64_t GetFreeBytes(const std::string& path);
126uint64_t GetTreeBytes(const std::string& path);
127
128bool IsFilesystemSupported(const std::string& fsType);
bigbiffa957f072021-03-07 18:20:29 -0500129bool IsSdcardfsUsed();
130bool IsFuseDaemon(const pid_t pid);
bigbiff7ba75002020-04-11 20:47:09 -0400131
132/* Wipes contents of block device at given path */
bigbiffa957f072021-03-07 18:20:29 -0500133android::status_t WipeBlockDevice(const std::string& path);
bigbiff7ba75002020-04-11 20:47:09 -0400134
135std::string BuildKeyPath(const std::string& partGuid);
136
137std::string BuildDataSystemLegacyPath(userid_t userid);
138std::string BuildDataSystemCePath(userid_t userid);
139std::string BuildDataSystemDePath(userid_t userid);
140std::string BuildDataMiscLegacyPath(userid_t userid);
141std::string BuildDataMiscCePath(userid_t userid);
142std::string BuildDataMiscDePath(userid_t userid);
143std::string BuildDataProfilesDePath(userid_t userid);
144std::string BuildDataVendorCePath(userid_t userid);
145std::string BuildDataVendorDePath(userid_t userid);
146
147std::string BuildDataPath(const std::string& volumeUuid);
148std::string BuildDataMediaCePath(const std::string& volumeUuid, userid_t userid);
149std::string BuildDataUserCePath(const std::string& volumeUuid, userid_t userid);
150std::string BuildDataUserDePath(const std::string& volumeUuid, userid_t userid);
151
152dev_t GetDevice(const std::string& path);
153
bigbiffa957f072021-03-07 18:20:29 -0500154android::status_t EnsureDirExists(const std::string& path, mode_t mode, uid_t uid, gid_t gid);
155
156android::status_t RestoreconRecursive(const std::string& path);
bigbiff7ba75002020-04-11 20:47:09 -0400157
158// TODO: promote to android::base
159bool Readlinkat(int dirfd, const std::string& path, std::string* result);
160
bigbiffa957f072021-03-07 18:20:29 -0500161// Handles dynamic major assignment for virtio-block
162bool IsVirtioBlkDevice(unsigned int major);
bigbiff7ba75002020-04-11 20:47:09 -0400163
bigbiffa957f072021-03-07 18:20:29 -0500164android::status_t UnmountTreeWithPrefix(const std::string& prefix);
165android::status_t UnmountTree(const std::string& mountPoint);
bigbiff7ba75002020-04-11 20:47:09 -0400166
bigbiffa957f072021-03-07 18:20:29 -0500167android::status_t DeleteDirContentsAndDir(const std::string& pathname);
168android::status_t DeleteDirContents(const std::string& pathname);
bigbiff7ba75002020-04-11 20:47:09 -0400169
bigbiffa957f072021-03-07 18:20:29 -0500170android::status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout);
bigbiff7ba75002020-04-11 20:47:09 -0400171
172bool FsyncDirectory(const std::string& dirname);
173
174bool writeStringToFile(const std::string& payload, const std::string& filename);
bigbiffa957f072021-03-07 18:20:29 -0500175
176void ConfigureMaxDirtyRatioForFuse(const std::string& fuse_mount, unsigned int max_ratio);
177
178void ConfigureReadAheadForFuse(const std::string& fuse_mount, size_t read_ahead_kb);
179
180android::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
183android::status_t UnmountUserFuse(userid_t userId, const std::string& absolute_lower_path,
184 const std::string& relative_upper_path);
185
186android::status_t PrepareAndroidDirs(const std::string& volumeRoot);
bigbiff7ba75002020-04-11 20:47:09 -0400187
188#endif