summaryrefslogtreecommitdiff
path: root/Utils.h (plain)
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
35struct DIR;
36
37namespace android {
38namespace droidvold {
39
40/* SELinux contexts used depending on the block device type */
41extern security_context_t sBlkidContext;
42extern security_context_t sBlkidUntrustedContext;
43extern security_context_t sFsckContext;
44extern security_context_t sFsckUntrustedContext;
45
46/* fs_prepare_dir wrapper that creates with SELinux context */
47status_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 */
50status_t ForceUnmount(const std::string& path);
51
52/* Kills any processes using given path */
53status_t KillProcessesUsingPath(const std::string& path);
54
55/* Creates bind mount from source to target */
56status_t BindMount(const std::string& source, const std::string& target);
57
58/* Reads filesystem metadata from untrusted device at path */
59status_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 */
63status_t ForkExecvp(const std::vector<std::string>& args);
64status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context);
65
66status_t ForkExecvp(const std::vector<std::string>& args,
67 std::vector<std::string>& output);
68status_t ForkExecvp(const std::vector<std::string>& args,
69 std::vector<std::string>& output, security_context_t context);
70
71pid_t ForkExecvpAsync(const std::vector<std::string>& args);
72
73status_t ReadRandomBytes(size_t bytes, std::string& out);
74
75/* Converts hex string to raw bytes, ignoring [ :-] */
76status_t HexToStr(const std::string& hex, std::string& str);
77/* Converts raw bytes to hex string */
78status_t StrToHex(const std::string& str, std::string& hex);
79/* Normalize given hex string into consistent format */
80status_t NormalizeHex(const std::string& in, std::string& out);
81
82uint64_t GetFreeBytes(const std::string& path);
83uint64_t GetTreeBytes(const std::string& path);
84
85bool IsFilesystemSupported(const std::string& fsType);
86
87/* Wipes contents of block device at given path */
88status_t WipeBlockDevice(const std::string& path);
89
90std::string BuildKeyPath(const std::string& partGuid);
91
92std::string BuildDataSystemLegacyPath(userid_t userid);
93std::string BuildDataSystemCePath(userid_t userid);
94std::string BuildDataSystemDePath(userid_t userid);
95std::string BuildDataMiscLegacyPath(userid_t userid);
96std::string BuildDataMiscCePath(userid_t userid);
97std::string BuildDataMiscDePath(userid_t userid);
98std::string BuildDataProfilesDePath(userid_t userid);
99std::string BuildDataProfilesForeignDexDePath(userid_t userid);
100
101std::string BuildDataPath(const char* volumeUuid);
102std::string BuildDataMediaCePath(const char* volumeUuid, userid_t userid);
103std::string BuildDataUserCePath(const char* volumeUuid, userid_t userid);
104std::string BuildDataUserDePath(const char* volumeUuid, userid_t userid);
105
106dev_t GetDevice(const std::string& path);
107
108std::string DefaultFstabPath();
109
110status_t RestoreconRecursive(const std::string& path);
111
112status_t SaneReadLinkAt(int dirfd, const char* path, char* buf, size_t bufsiz);
113
114class ScopedFd {
115 const int fd_;
116public:
117 ScopedFd(int fd);
118 ~ScopedFd();
119 int get() const { return fd_; }
120
121 DISALLOW_COPY_AND_ASSIGN(ScopedFd);
122};
123
124class ScopedDir {
125 DIR* const dir_;
126public:
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 */
135bool IsRunningInEmulator();
136
137/* Get physical device,such as /dev/block/mmcblk1 */
138status_t GetPhysicalDevice(
139 const std::string& sysPath,
140 std::string& physicalDev);
141
142/* Get logical partition device,such as /dev/block/mmcblk1p1 */
143status_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 */
149bool IsJustPhysicalDevice(
150 const std::string& sysPath,
151 std::string& physicalDevName);
152
153status_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