blob: 0e5e6440885e30789b03513be59ea7ccf7a6512d
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 <utils/Errors.h> |
21 | #include <cutils/multiuser.h> |
22 | #include <selinux/selinux.h> |
23 | |
24 | #include <vector> |
25 | #include <string> |
26 | |
27 | // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private: |
28 | // declarations in a class. |
29 | #if !defined(DISALLOW_COPY_AND_ASSIGN) |
30 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
31 | TypeName(const TypeName&) = delete; \ |
32 | void operator=(const TypeName&) = delete |
33 | #endif |
34 | |
35 | struct DIR; |
36 | |
37 | namespace android { |
38 | namespace droidvold { |
39 | |
40 | /* SELinux contexts used depending on the block device type */ |
41 | extern security_context_t sBlkidContext; |
42 | extern security_context_t sBlkidUntrustedContext; |
43 | extern security_context_t sFsckContext; |
44 | extern security_context_t sFsckUntrustedContext; |
45 | |
46 | /* fs_prepare_dir wrapper that creates with SELinux context */ |
47 | status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid); |
48 | |
49 | /* Really unmounts the path, killing active processes along the way */ |
50 | status_t ForceUnmount(const std::string& path); |
51 | |
52 | /* Kills any processes using given path */ |
53 | status_t KillProcessesUsingPath(const std::string& path); |
54 | |
55 | /* Creates bind mount from source to target */ |
56 | status_t BindMount(const std::string& source, const std::string& target); |
57 | |
58 | /* Reads filesystem metadata from untrusted device at path */ |
59 | status_t ReadPartMetadata(const std::string& path, std::string& fsType, |
60 | std::string& fsUuid, std::string& fsLabel); |
61 | |
62 | /* Returns either WEXITSTATUS() status, or a negative errno */ |
63 | status_t ForkExecvp(const std::vector<std::string>& args); |
64 | status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context); |
65 | |
66 | status_t ForkExecvp(const std::vector<std::string>& args, |
67 | std::vector<std::string>& output); |
68 | status_t ForkExecvp(const std::vector<std::string>& args, |
69 | std::vector<std::string>& output, security_context_t context); |
70 | |
71 | pid_t ForkExecvpAsync(const std::vector<std::string>& args); |
72 | |
73 | status_t ReadRandomBytes(size_t bytes, std::string& out); |
74 | |
75 | /* Converts hex string to raw bytes, ignoring [ :-] */ |
76 | status_t HexToStr(const std::string& hex, std::string& str); |
77 | /* Converts raw bytes to hex string */ |
78 | status_t StrToHex(const std::string& str, std::string& hex); |
79 | /* Normalize given hex string into consistent format */ |
80 | status_t NormalizeHex(const std::string& in, std::string& out); |
81 | |
82 | uint64_t GetFreeBytes(const std::string& path); |
83 | uint64_t GetTreeBytes(const std::string& path); |
84 | |
85 | bool IsFilesystemSupported(const std::string& fsType); |
86 | |
87 | /* Wipes contents of block device at given path */ |
88 | status_t WipeBlockDevice(const std::string& path); |
89 | |
90 | std::string BuildKeyPath(const std::string& partGuid); |
91 | |
92 | std::string BuildDataSystemLegacyPath(userid_t userid); |
93 | std::string BuildDataSystemCePath(userid_t userid); |
94 | std::string BuildDataSystemDePath(userid_t userid); |
95 | std::string BuildDataMiscLegacyPath(userid_t userid); |
96 | std::string BuildDataMiscCePath(userid_t userid); |
97 | std::string BuildDataMiscDePath(userid_t userid); |
98 | std::string BuildDataProfilesDePath(userid_t userid); |
99 | std::string BuildDataProfilesForeignDexDePath(userid_t userid); |
100 | |
101 | std::string BuildDataPath(const char* volumeUuid); |
102 | std::string BuildDataMediaCePath(const char* volumeUuid, userid_t userid); |
103 | std::string BuildDataUserCePath(const char* volumeUuid, userid_t userid); |
104 | std::string BuildDataUserDePath(const char* volumeUuid, userid_t userid); |
105 | |
106 | dev_t GetDevice(const std::string& path); |
107 | |
108 | std::string DefaultFstabPath(); |
109 | |
110 | status_t RestoreconRecursive(const std::string& path); |
111 | |
112 | status_t SaneReadLinkAt(int dirfd, const char* path, char* buf, size_t bufsiz); |
113 | |
114 | class ScopedFd { |
115 | const int fd_; |
116 | public: |
117 | ScopedFd(int fd); |
118 | ~ScopedFd(); |
119 | int get() const { return fd_; } |
120 | |
121 | DISALLOW_COPY_AND_ASSIGN(ScopedFd); |
122 | }; |
123 | |
124 | class ScopedDir { |
125 | DIR* const dir_; |
126 | public: |
127 | ScopedDir(DIR* dir); |
128 | ~ScopedDir(); |
129 | DIR* get() const { return dir_; } |
130 | |
131 | DISALLOW_COPY_AND_ASSIGN(ScopedDir); |
132 | }; |
133 | |
134 | /* Checks if Android is running in QEMU */ |
135 | bool IsRunningInEmulator(); |
136 | |
137 | /* Get physical device,such as /dev/block/mmcblk1 */ |
138 | status_t GetPhysicalDevice( |
139 | const std::string& sysPath, |
140 | std::string& physicalDev); |
141 | |
142 | /* Get logical partition device,such as /dev/block/mmcblk1p1 */ |
143 | status_t GetLogicalPartitionDevice( |
144 | const dev_t device, |
145 | const std::string& sysPath, |
146 | std::string& logicalPartitionDev); |
147 | |
148 | /* Check sd/udisk physical devices whether are used */ |
149 | bool IsJustPhysicalDevice( |
150 | const std::string& sysPath, |
151 | std::string& physicalDevName); |
152 | |
153 | status_t readBlockDevMajorAndMinor( |
154 | const std::string& devPath, |
155 | std::string& major, std::string& minor); |
156 | |
157 | } // namespace vold |
158 | } // namespace android |
159 | |
160 | #endif |
161 |