blob: 6a670713ca1b764032350d1e166c96664b31b3ce [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
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
Jeff Sharkeydeb24052015-03-02 21:01:40 -080017#include "Utils.h"
Paul Crowley56292ef2017-10-20 08:07:53 -070018
Jeff Sharkeydeb24052015-03-02 21:01:40 -080019#include "Process.h"
Paul Crowley56292ef2017-10-20 08:07:53 -070020#include "sehandle.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080021
Paul Crowley298fa322018-10-30 15:59:24 -070022#include <android-base/chrono_utils.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080023#include <android-base/file.h>
24#include <android-base/logging.h>
Tom Cherryd6127ef2017-06-15 17:13:56 -070025#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/stringprintf.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070027#include <android-base/strings.h>
Sudheer Shanka40ab6742018-09-18 13:07:45 -070028#include <android-base/unique_fd.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029#include <cutils/fs.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070030#include <logwrap/logwrap.h>
Tom Cherryd6127ef2017-06-15 17:13:56 -070031#include <private/android_filesystem_config.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070033#include <dirent.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080034#include <fcntl.h>
35#include <linux/fs.h>
Martijn Coenen879fa802020-02-11 12:37:25 +010036#include <linux/posix_acl.h>
37#include <linux/posix_acl_xattr.h>
Sudheer Shanka89ddf992018-09-25 14:22:07 -070038#include <mntent.h>
39#include <stdio.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040#include <stdlib.h>
41#include <sys/mount.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080042#include <sys/stat.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070043#include <sys/statvfs.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070044#include <sys/sysmacros.h>
45#include <sys/types.h>
46#include <sys/wait.h>
Martijn Coenen879fa802020-02-11 12:37:25 +010047#include <sys/xattr.h>
Paul Crowley747b4212019-04-05 04:09:57 -070048#include <unistd.h>
Paul Crowley298fa322018-10-30 15:59:24 -070049
Sudheer Shanka89ddf992018-09-25 14:22:07 -070050#include <list>
Paul Crowley14c8c072018-09-18 13:30:21 -070051#include <mutex>
Martijn Coenen04bb17f2020-02-10 23:48:11 +010052#include <regex>
Paul Crowley298fa322018-10-30 15:59:24 -070053#include <thread>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080054
55#ifndef UMOUNT_NOFOLLOW
Paul Crowley14c8c072018-09-18 13:30:21 -070056#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
Jeff Sharkeydeb24052015-03-02 21:01:40 -080057#endif
58
Paul Crowley298fa322018-10-30 15:59:24 -070059using namespace std::chrono_literals;
Martijn Coenenba9868b2020-01-31 15:49:24 +010060using android::base::EndsWith;
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070061using android::base::ReadFileToString;
Martijn Coenen13ff6682019-12-24 12:57:16 +010062using android::base::StartsWith;
Jeff Sharkey9c484982015-03-31 10:35:33 -070063using android::base::StringPrintf;
64
Jeff Sharkeydeb24052015-03-02 21:01:40 -080065namespace android {
66namespace vold {
67
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070068security_context_t sBlkidContext = nullptr;
69security_context_t sBlkidUntrustedContext = nullptr;
70security_context_t sFsckContext = nullptr;
71security_context_t sFsckUntrustedContext = nullptr;
72
Paul Crowley56292ef2017-10-20 08:07:53 -070073bool sSleepOnUnmount = true;
74
Jeff Sharkey9c484982015-03-31 10:35:33 -070075static const char* kBlkidPath = "/system/bin/blkid";
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070076static const char* kKeyPath = "/data/misc/vold";
Jeff Sharkey9c484982015-03-31 10:35:33 -070077
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070078static const char* kProcFilesystems = "/proc/filesystems";
79
Martijn Coenen04bb17f2020-02-10 23:48:11 +010080static const char* kAndroidDir = "/Android/";
81static const char* kAppDataDir = "/Android/data/";
82static const char* kAppMediaDir = "/Android/media/";
83static const char* kAppObbDir = "/Android/obb/";
84
Jeff Sharkeyae4f85d2017-10-18 17:02:21 -060085// Lock used to protect process-level SELinux changes from racing with each
86// other between multiple threads.
87static std::mutex kSecurityLock;
88
Jeff Sharkeydeb24052015-03-02 21:01:40 -080089status_t CreateDeviceNode(const std::string& path, dev_t dev) {
Jeff Sharkeyae4f85d2017-10-18 17:02:21 -060090 std::lock_guard<std::mutex> lock(kSecurityLock);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080091 const char* cpath = path.c_str();
92 status_t res = 0;
93
94 char* secontext = nullptr;
95 if (sehandle) {
96 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) {
97 setfscreatecon(secontext);
98 }
99 }
100
101 mode_t mode = 0660 | S_IFBLK;
102 if (mknod(cpath, mode, dev) < 0) {
103 if (errno != EEXIST) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700104 PLOG(ERROR) << "Failed to create device node for " << major(dev) << ":" << minor(dev)
105 << " at " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800106 res = -errno;
107 }
108 }
109
110 if (secontext) {
111 setfscreatecon(nullptr);
112 freecon(secontext);
113 }
114
115 return res;
116}
117
118status_t DestroyDeviceNode(const std::string& path) {
119 const char* cpath = path.c_str();
120 if (TEMP_FAILURE_RETRY(unlink(cpath))) {
121 return -errno;
122 } else {
123 return OK;
124 }
125}
126
Martijn Coenen879fa802020-02-11 12:37:25 +0100127// Sets a default ACL where the owner and group can read/write/execute.
128// Other users aren't allowed anything.
129int SetDefault770Acl(const std::string& path, uid_t uid, gid_t gid) {
130 if (IsFilesystemSupported("sdcardfs")) {
131 // sdcardfs magically takes care of this
132 return OK;
133 }
134
135 static constexpr size_t size =
136 sizeof(posix_acl_xattr_header) + 3 * sizeof(posix_acl_xattr_entry);
137 auto buf = std::make_unique<uint8_t[]>(size);
138
139 posix_acl_xattr_header* acl_header = reinterpret_cast<posix_acl_xattr_header*>(buf.get());
140 acl_header->a_version = POSIX_ACL_XATTR_VERSION;
141
142 posix_acl_xattr_entry* entry =
143 reinterpret_cast<posix_acl_xattr_entry*>(buf.get() + sizeof(posix_acl_xattr_header));
144
145 entry[0].e_tag = ACL_USER_OBJ;
146 entry[0].e_perm = ACL_READ | ACL_WRITE | ACL_EXECUTE;
147 entry[0].e_id = uid;
148
149 entry[1].e_tag = ACL_GROUP_OBJ;
150 entry[1].e_perm = ACL_READ | ACL_WRITE | ACL_EXECUTE;
151 entry[1].e_id = gid;
152
153 entry[2].e_tag = ACL_OTHER;
154 entry[2].e_perm = 0;
155 entry[2].e_id = 0;
156
157 int ret = setxattr(path.c_str(), XATTR_NAME_POSIX_ACL_DEFAULT, acl_header, size, 0);
158
159 if (ret != 0) {
160 PLOG(ERROR) << "Failed to set default ACL on " << path;
161 }
162
163 return ret;
164}
165
Martijn Coenen5fe1b162020-02-06 18:57:47 +0100166int SetQuotaInherit(const std::string& path) {
167 unsigned long flags;
168
169 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
170 if (fd == -1) {
171 PLOG(ERROR) << "Failed to open " << path << " to set project id inheritance.";
172 return -1;
173 }
174
175 int ret = ioctl(fd, FS_IOC_GETFLAGS, &flags);
176 if (ret == -1) {
177 PLOG(ERROR) << "Failed to get flags for " << path << " to set project id inheritance.";
178 return ret;
179 }
180
181 flags |= FS_PROJINHERIT_FL;
182
183 ret = ioctl(fd, FS_IOC_SETFLAGS, &flags);
184 if (ret == -1) {
185 PLOG(ERROR) << "Failed to set flags for " << path << " to set project id inheritance.";
186 return ret;
187 }
188
189 return 0;
190}
191
192int SetQuotaProjectId(const std::string& path, long projectId) {
Martijn Coenenfb42bc42020-01-16 01:25:27 +0100193 struct fsxattr fsx;
194
195 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC)));
196 if (fd == -1) {
197 PLOG(ERROR) << "Failed to open " << path << " to set project id.";
198 return -1;
199 }
200
201 int ret = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
202 if (ret == -1) {
203 PLOG(ERROR) << "Failed to get extended attributes for " << path << " to get project id.";
204 return ret;
205 }
206
207 fsx.fsx_projid = projectId;
208 return ioctl(fd, FS_IOC_FSSETXATTR, &fsx);
209}
210
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100211int PrepareDirWithProjectId(const std::string& path, mode_t mode, uid_t uid, gid_t gid,
212 long projectId) {
213 int ret = fs_prepare_dir(path.c_str(), mode, uid, gid);
214
215 if (ret != 0) {
216 return ret;
Martijn Coenen13ff6682019-12-24 12:57:16 +0100217 }
Martijn Coenenba9868b2020-01-31 15:49:24 +0100218
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100219 if (!IsFilesystemSupported("sdcardfs")) {
220 ret = SetQuotaProjectId(path, projectId);
Martijn Coenen13ff6682019-12-24 12:57:16 +0100221 }
222
223 return ret;
224}
225
Martijn Coenenb5a31c92020-02-13 23:30:38 +0100226int PrepareAppDirFromRoot(const std::string& path, const std::string& root, int appUid) {
227 long projectId;
228 size_t pos;
229 int ret = 0;
230
231 // Make sure the Android/ directories exist and are setup correctly
232 ret = PrepareAndroidDirs(root);
233 if (ret != 0) {
234 LOG(ERROR) << "Failed to prepare Android/ directories.";
235 return ret;
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100236 }
237
Martijn Coenenb5a31c92020-02-13 23:30:38 +0100238 // Now create the application-specific subdir(s)
239 // path is something like /data/media/0/Android/data/com.foo/files
240 // First, chop off the volume root, eg /data/media/0
241 std::string pathFromRoot = path.substr(root.length());
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100242
Martijn Coenenb5a31c92020-02-13 23:30:38 +0100243 uid_t uid = appUid;
244 gid_t gid = AID_MEDIA_RW;
245 std::string appDir;
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100246
Martijn Coenenb5a31c92020-02-13 23:30:38 +0100247 // Check that the next part matches one of the allowed Android/ dirs
248 if (StartsWith(pathFromRoot, kAppDataDir)) {
249 appDir = kAppDataDir;
250 if (!IsFilesystemSupported("sdcardfs")) {
251 gid = AID_EXT_DATA_RW;
252 }
253 } else if (StartsWith(pathFromRoot, kAppMediaDir)) {
254 appDir = kAppMediaDir;
255 if (!IsFilesystemSupported("sdcardfs")) {
256 gid = AID_MEDIA_RW;
257 }
258 } else if (StartsWith(pathFromRoot, kAppMediaDir)) {
259 appDir = kAppObbDir;
260 if (!IsFilesystemSupported("sdcardfs")) {
261 gid = AID_EXT_OBB_RW;
262 }
263 } else {
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100264 LOG(ERROR) << "Invalid application directory: " << path;
265 return -EINVAL;
266 }
267
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100268 // mode = 770, plus sticky bit on directory to inherit GID when apps
269 // create subdirs
270 mode_t mode = S_IRWXU | S_IRWXG | S_ISGID;
Martijn Coenenb5a31c92020-02-13 23:30:38 +0100271 // the project ID for application-specific directories is directly
272 // derived from their uid
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100273
Martijn Coenenb5a31c92020-02-13 23:30:38 +0100274 // Chop off the generic application-specific part, eg /Android/data/
275 // this leaves us with something like com.foo/files/
276 std::string leftToCreate = pathFromRoot.substr(appDir.length());
277 if (!EndsWith(leftToCreate, "/")) {
278 leftToCreate += "/";
279 }
280 std::string pathToCreate = root + appDir;
281 int depth = 0;
282 bool withinCache = false;
283 while ((pos = leftToCreate.find('/')) != std::string::npos) {
284 std::string component = leftToCreate.substr(0, pos + 1);
285 leftToCreate = leftToCreate.erase(0, pos + 1);
286 pathToCreate = pathToCreate + component;
287
288 if (appDir == kAppDataDir && depth == 1 && component == "cache/") {
289 // All dirs use the "app" project ID, except for the cache dirs in
290 // Android/data, eg Android/data/com.foo/cache
291 // Note that this "sticks" - eg subdirs of this dir need the same
292 // project ID.
293 withinCache = true;
294 }
295 if (withinCache) {
296 projectId = uid - AID_APP_START + AID_CACHE_GID_START;
297 } else {
298 projectId = uid - AID_APP_START + AID_EXT_GID_START;
299 }
300 ret = PrepareDirWithProjectId(pathToCreate, mode, uid, gid, projectId);
301 if (ret != 0) {
302 return ret;
303 }
304
305 if (depth == 0) {
306 // Set the default ACL on the top-level application-specific directories,
307 // to ensure that even if applications run with a umask of 0077,
308 // new directories within these directories will allow the GID
309 // specified here to write; this is necessary for apps like
310 // installers and MTP, that require access here.
311 //
312 // See man (5) acl for more details.
313 ret = SetDefault770Acl(pathToCreate, uid, gid);
314 if (ret != 0) {
315 return ret;
316 }
317 }
318
319 depth++;
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100320 }
321
Martijn Coenenb5a31c92020-02-13 23:30:38 +0100322 return OK;
Martijn Coenen04bb17f2020-02-10 23:48:11 +0100323}
324
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700325status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
Jeff Sharkeyae4f85d2017-10-18 17:02:21 -0600326 std::lock_guard<std::mutex> lock(kSecurityLock);
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700327 const char* cpath = path.c_str();
328
329 char* secontext = nullptr;
330 if (sehandle) {
331 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFDIR)) {
332 setfscreatecon(secontext);
333 }
334 }
335
336 int res = fs_prepare_dir(cpath, mode, uid, gid);
337
338 if (secontext) {
339 setfscreatecon(nullptr);
340 freecon(secontext);
341 }
342
343 if (res == 0) {
344 return OK;
345 } else {
346 return -errno;
347 }
348}
349
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800350status_t ForceUnmount(const std::string& path) {
351 const char* cpath = path.c_str();
352 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
353 return OK;
354 }
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700355 // Apps might still be handling eject request, so wait before
356 // we start sending signals
Paul Crowley56292ef2017-10-20 08:07:53 -0700357 if (sSleepOnUnmount) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700358
Jeff Sharkey3472e522017-10-06 18:02:53 -0600359 KillProcessesWithOpenFiles(path, SIGINT);
Paul Crowley56292ef2017-10-20 08:07:53 -0700360 if (sSleepOnUnmount) sleep(5);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700361 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
362 return OK;
363 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700364
Jeff Sharkey3472e522017-10-06 18:02:53 -0600365 KillProcessesWithOpenFiles(path, SIGTERM);
Paul Crowley56292ef2017-10-20 08:07:53 -0700366 if (sSleepOnUnmount) sleep(5);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800367 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
368 return OK;
369 }
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700370
Jeff Sharkey3472e522017-10-06 18:02:53 -0600371 KillProcessesWithOpenFiles(path, SIGKILL);
Paul Crowley56292ef2017-10-20 08:07:53 -0700372 if (sSleepOnUnmount) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700373 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
374 return OK;
375 }
Zim3623a212019-07-19 16:46:53 +0100376 PLOG(INFO) << "ForceUnmount failed";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800377 return -errno;
378}
379
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700380status_t KillProcessesUsingPath(const std::string& path) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600381 if (KillProcessesWithOpenFiles(path, SIGINT) == 0) {
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700382 return OK;
383 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700384 if (sSleepOnUnmount) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700385
Jeff Sharkey3472e522017-10-06 18:02:53 -0600386 if (KillProcessesWithOpenFiles(path, SIGTERM) == 0) {
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700387 return OK;
388 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700389 if (sSleepOnUnmount) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700390
Jeff Sharkey3472e522017-10-06 18:02:53 -0600391 if (KillProcessesWithOpenFiles(path, SIGKILL) == 0) {
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700392 return OK;
393 }
Paul Crowley56292ef2017-10-20 08:07:53 -0700394 if (sSleepOnUnmount) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700395
396 // Send SIGKILL a second time to determine if we've
397 // actually killed everyone with open files
Jeff Sharkey3472e522017-10-06 18:02:53 -0600398 if (KillProcessesWithOpenFiles(path, SIGKILL) == 0) {
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700399 return OK;
400 }
401 PLOG(ERROR) << "Failed to kill processes using " << path;
402 return -EBUSY;
403}
404
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700405status_t BindMount(const std::string& source, const std::string& target) {
Sudheer Shanka023b5392019-02-06 12:39:19 -0800406 if (UnmountTree(target) < 0) {
407 return -errno;
408 }
409 if (TEMP_FAILURE_RETRY(mount(source.c_str(), target.c_str(), nullptr, MS_BIND, nullptr)) < 0) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700410 PLOG(ERROR) << "Failed to bind mount " << source << " to " << target;
411 return -errno;
412 }
413 return OK;
414}
415
Sudheer Shanka023b5392019-02-06 12:39:19 -0800416status_t Symlink(const std::string& target, const std::string& linkpath) {
417 if (Unlink(linkpath) < 0) {
418 return -errno;
419 }
420 if (TEMP_FAILURE_RETRY(symlink(target.c_str(), linkpath.c_str())) < 0) {
421 PLOG(ERROR) << "Failed to create symlink " << linkpath << " to " << target;
422 return -errno;
423 }
424 return OK;
425}
426
427status_t Unlink(const std::string& linkpath) {
428 if (TEMP_FAILURE_RETRY(unlink(linkpath.c_str())) < 0 && errno != EINVAL && errno != ENOENT) {
429 PLOG(ERROR) << "Failed to unlink " << linkpath;
430 return -errno;
431 }
432 return OK;
433}
434
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000435status_t CreateDir(const std::string& dir, mode_t mode) {
436 struct stat sb;
437 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &sb)) == 0) {
438 if (S_ISDIR(sb.st_mode)) {
439 return OK;
440 } else if (TEMP_FAILURE_RETRY(unlink(dir.c_str())) == -1) {
441 PLOG(ERROR) << "Failed to unlink " << dir;
442 return -errno;
443 }
444 } else if (errno != ENOENT) {
445 PLOG(ERROR) << "Failed to stat " << dir;
446 return -errno;
447 }
Sudheer Shanka6d285ce2019-02-19 14:12:20 -0800448 if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), mode)) == -1 && errno != EEXIST) {
Sudheer Shankaf9b38a52019-02-14 19:09:51 +0000449 PLOG(ERROR) << "Failed to mkdir " << dir;
450 return -errno;
451 }
452 return OK;
453}
454
Jeff Sharkey3472e522017-10-06 18:02:53 -0600455bool FindValue(const std::string& raw, const std::string& key, std::string* value) {
456 auto qual = key + "=\"";
Paul Crowley95abfa02019-02-05 15:33:34 -0800457 size_t start = 0;
458 while (true) {
459 start = raw.find(qual, start);
460 if (start == std::string::npos) return false;
461 if (start == 0 || raw[start - 1] == ' ') {
462 break;
463 }
464 start += 1;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600465 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600466 start += qual.length();
467
468 auto end = raw.find("\"", start);
469 if (end == std::string::npos) return false;
470
471 *value = raw.substr(start, end - start);
472 return true;
473}
474
Paul Crowley14c8c072018-09-18 13:30:21 -0700475static status_t readMetadata(const std::string& path, std::string* fsType, std::string* fsUuid,
476 std::string* fsLabel, bool untrusted) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600477 fsType->clear();
478 fsUuid->clear();
479 fsLabel->clear();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700480
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700481 std::vector<std::string> cmd;
482 cmd.push_back(kBlkidPath);
483 cmd.push_back("-c");
484 cmd.push_back("/dev/null");
Jeff Sharkeyeddf9bd2015-08-12 16:04:35 -0700485 cmd.push_back("-s");
486 cmd.push_back("TYPE");
487 cmd.push_back("-s");
488 cmd.push_back("UUID");
489 cmd.push_back("-s");
490 cmd.push_back("LABEL");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700491 cmd.push_back(path);
492
493 std::vector<std::string> output;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800494 status_t res = ForkExecvp(cmd, &output, untrusted ? sBlkidUntrustedContext : sBlkidContext);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700495 if (res != OK) {
496 LOG(WARNING) << "blkid failed to identify " << path;
497 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700498 }
499
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700500 for (const auto& line : output) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700501 // Extract values from blkid output, if defined
Jeff Sharkey3472e522017-10-06 18:02:53 -0600502 FindValue(line, "TYPE", fsType);
503 FindValue(line, "UUID", fsUuid);
504 FindValue(line, "LABEL", fsLabel);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700505 }
506
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700507 return OK;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700508}
509
Paul Crowley14c8c072018-09-18 13:30:21 -0700510status_t ReadMetadata(const std::string& path, std::string* fsType, std::string* fsUuid,
511 std::string* fsLabel) {
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700512 return readMetadata(path, fsType, fsUuid, fsLabel, false);
513}
514
Paul Crowley14c8c072018-09-18 13:30:21 -0700515status_t ReadMetadataUntrusted(const std::string& path, std::string* fsType, std::string* fsUuid,
516 std::string* fsLabel) {
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700517 return readMetadata(path, fsType, fsUuid, fsLabel, true);
518}
519
Paul Crowleyde2d6202018-11-30 11:43:47 -0800520static std::vector<const char*> ConvertToArgv(const std::vector<std::string>& args) {
521 std::vector<const char*> argv;
522 argv.reserve(args.size() + 1);
523 for (const auto& arg : args) {
524 if (argv.empty()) {
525 LOG(DEBUG) << arg;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700526 } else {
Paul Crowleyde2d6202018-11-30 11:43:47 -0800527 LOG(DEBUG) << " " << arg;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700528 }
Paul Crowleyde2d6202018-11-30 11:43:47 -0800529 argv.emplace_back(arg.data());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700530 }
Paul Crowleyde2d6202018-11-30 11:43:47 -0800531 argv.emplace_back(nullptr);
532 return argv;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700533}
534
Paul Crowleyde2d6202018-11-30 11:43:47 -0800535static status_t ReadLinesFromFdAndLog(std::vector<std::string>* output,
536 android::base::unique_fd ufd) {
537 std::unique_ptr<FILE, int (*)(FILE*)> fp(android::base::Fdopen(std::move(ufd), "r"), fclose);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700538 if (!fp) {
Paul Crowleyde2d6202018-11-30 11:43:47 -0800539 PLOG(ERROR) << "fdopen in ReadLinesFromFdAndLog";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700540 return -errno;
541 }
Paul Crowleyde2d6202018-11-30 11:43:47 -0800542 if (output) output->clear();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700543 char line[1024];
Paul Crowleyde2d6202018-11-30 11:43:47 -0800544 while (fgets(line, sizeof(line), fp.get()) != nullptr) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700545 LOG(DEBUG) << line;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800546 if (output) output->emplace_back(line);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700547 }
Paul Crowleyde2d6202018-11-30 11:43:47 -0800548 return OK;
549}
550
551status_t ForkExecvp(const std::vector<std::string>& args, std::vector<std::string>* output,
552 security_context_t context) {
553 auto argv = ConvertToArgv(args);
554
Paul Crowleye6d76632018-11-30 11:43:47 -0800555 android::base::unique_fd pipe_read, pipe_write;
556 if (!android::base::Pipe(&pipe_read, &pipe_write)) {
557 PLOG(ERROR) << "Pipe in ForkExecvp";
Paul Crowleyde2d6202018-11-30 11:43:47 -0800558 return -errno;
559 }
Paul Crowleyde2d6202018-11-30 11:43:47 -0800560
561 pid_t pid = fork();
562 if (pid == 0) {
563 if (context) {
564 if (setexeccon(context)) {
Paul Crowleye6d76632018-11-30 11:43:47 -0800565 LOG(ERROR) << "Failed to setexeccon in ForkExecvp";
Paul Crowleyde2d6202018-11-30 11:43:47 -0800566 abort();
567 }
568 }
569 pipe_read.reset();
Paul Crowleybe857bf2018-12-07 12:23:25 -0800570 if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) {
571 PLOG(ERROR) << "dup2 in ForkExecvp";
572 _exit(EXIT_FAILURE);
573 }
Paul Crowleye6d76632018-11-30 11:43:47 -0800574 pipe_write.reset();
Paul Crowleyde2d6202018-11-30 11:43:47 -0800575 execvp(argv[0], const_cast<char**>(argv.data()));
Paul Crowleye6d76632018-11-30 11:43:47 -0800576 PLOG(ERROR) << "exec in ForkExecvp";
Paul Crowleyde2d6202018-11-30 11:43:47 -0800577 _exit(EXIT_FAILURE);
578 }
579 if (pid == -1) {
580 PLOG(ERROR) << "fork in ForkExecvp";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700581 return -errno;
582 }
583
Paul Crowleyde2d6202018-11-30 11:43:47 -0800584 pipe_write.reset();
585 auto st = ReadLinesFromFdAndLog(output, std::move(pipe_read));
586 if (st != 0) return st;
587
588 int status;
589 if (waitpid(pid, &status, 0) == -1) {
590 PLOG(ERROR) << "waitpid in ForkExecvp";
591 return -errno;
592 }
593 if (!WIFEXITED(status)) {
594 LOG(ERROR) << "Process did not exit normally, status: " << status;
595 return -ECHILD;
596 }
597 if (WEXITSTATUS(status)) {
598 LOG(ERROR) << "Process exited with code: " << WEXITSTATUS(status);
599 return WEXITSTATUS(status);
600 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700601 return OK;
602}
603
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700604pid_t ForkExecvpAsync(const std::vector<std::string>& args) {
Paul Crowleyde2d6202018-11-30 11:43:47 -0800605 auto argv = ConvertToArgv(args);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700606
607 pid_t pid = fork();
608 if (pid == 0) {
609 close(STDIN_FILENO);
610 close(STDOUT_FILENO);
611 close(STDERR_FILENO);
612
Paul Crowleyde2d6202018-11-30 11:43:47 -0800613 execvp(argv[0], const_cast<char**>(argv.data()));
614 PLOG(ERROR) << "exec in ForkExecvpAsync";
615 _exit(EXIT_FAILURE);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700616 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700617 if (pid == -1) {
Paul Crowleyde2d6202018-11-30 11:43:47 -0800618 PLOG(ERROR) << "fork in ForkExecvpAsync";
619 return -1;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700620 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700621 return pid;
622}
623
Jeff Sharkey9c484982015-03-31 10:35:33 -0700624status_t ReadRandomBytes(size_t bytes, std::string& out) {
Pavel Grafove2e2d302017-08-01 17:15:53 +0100625 out.resize(bytes);
626 return ReadRandomBytes(bytes, &out[0]);
627}
Jeff Sharkey9c484982015-03-31 10:35:33 -0700628
Pavel Grafove2e2d302017-08-01 17:15:53 +0100629status_t ReadRandomBytes(size_t bytes, char* buf) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700630 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
631 if (fd == -1) {
632 return -errno;
633 }
634
Eric Biggers0ef7bfd2019-01-16 13:05:34 -0800635 ssize_t n;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100636 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], bytes))) > 0) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700637 bytes -= n;
Pavel Grafove2e2d302017-08-01 17:15:53 +0100638 buf += n;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700639 }
Elliott Hughesa6231082015-05-15 18:34:24 -0700640 close(fd);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700641
642 if (bytes == 0) {
643 return OK;
644 } else {
645 return -EIO;
646 }
647}
648
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600649status_t GenerateRandomUuid(std::string& out) {
650 status_t res = ReadRandomBytes(16, out);
651 if (res == OK) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700652 out[6] &= 0x0f; /* clear version */
653 out[6] |= 0x40; /* set to version 4 */
654 out[8] &= 0x3f; /* clear variant */
655 out[8] |= 0x80; /* set to IETF variant */
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600656 }
657 return res;
658}
659
Jeff Sharkey9c484982015-03-31 10:35:33 -0700660status_t HexToStr(const std::string& hex, std::string& str) {
661 str.clear();
662 bool even = true;
663 char cur = 0;
664 for (size_t i = 0; i < hex.size(); i++) {
665 int val = 0;
666 switch (hex[i]) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700667 // clang-format off
668 case ' ': case '-': case ':': continue;
669 case 'f': case 'F': val = 15; break;
670 case 'e': case 'E': val = 14; break;
671 case 'd': case 'D': val = 13; break;
672 case 'c': case 'C': val = 12; break;
673 case 'b': case 'B': val = 11; break;
674 case 'a': case 'A': val = 10; break;
675 case '9': val = 9; break;
676 case '8': val = 8; break;
677 case '7': val = 7; break;
678 case '6': val = 6; break;
679 case '5': val = 5; break;
680 case '4': val = 4; break;
681 case '3': val = 3; break;
682 case '2': val = 2; break;
683 case '1': val = 1; break;
684 case '0': val = 0; break;
685 default: return -EINVAL;
686 // clang-format on
Jeff Sharkey9c484982015-03-31 10:35:33 -0700687 }
688
689 if (even) {
690 cur = val << 4;
691 } else {
692 cur += val;
693 str.push_back(cur);
694 cur = 0;
695 }
696 even = !even;
697 }
698 return even ? OK : -EINVAL;
699}
700
701static const char* kLookup = "0123456789abcdef";
702
703status_t StrToHex(const std::string& str, std::string& hex) {
704 hex.clear();
705 for (size_t i = 0; i < str.size(); i++) {
Jeff Sharkeyef369752015-04-29 15:57:48 -0700706 hex.push_back(kLookup[(str[i] & 0xF0) >> 4]);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700707 hex.push_back(kLookup[str[i] & 0x0F]);
708 }
709 return OK;
710}
711
Pavel Grafove2e2d302017-08-01 17:15:53 +0100712status_t StrToHex(const KeyBuffer& str, KeyBuffer& hex) {
713 hex.clear();
714 for (size_t i = 0; i < str.size(); i++) {
715 hex.push_back(kLookup[(str.data()[i] & 0xF0) >> 4]);
716 hex.push_back(kLookup[str.data()[i] & 0x0F]);
717 }
718 return OK;
719}
720
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700721status_t NormalizeHex(const std::string& in, std::string& out) {
722 std::string tmp;
723 if (HexToStr(in, tmp)) {
724 return -EINVAL;
725 }
726 return StrToHex(tmp, out);
727}
728
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200729status_t GetBlockDevSize(int fd, uint64_t* size) {
730 if (ioctl(fd, BLKGETSIZE64, size)) {
731 return -errno;
732 }
733
734 return OK;
735}
736
737status_t GetBlockDevSize(const std::string& path, uint64_t* size) {
738 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
739 status_t res = OK;
740
741 if (fd < 0) {
742 return -errno;
743 }
744
745 res = GetBlockDevSize(fd, size);
746
747 close(fd);
748
749 return res;
750}
751
752status_t GetBlockDev512Sectors(const std::string& path, uint64_t* nr_sec) {
753 uint64_t size;
754 status_t res = GetBlockDevSize(path, &size);
755
756 if (res != OK) {
757 return res;
758 }
759
760 *nr_sec = size / 512;
761
762 return OK;
763}
764
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700765uint64_t GetFreeBytes(const std::string& path) {
766 struct statvfs sb;
767 if (statvfs(path.c_str(), &sb) == 0) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700768 return (uint64_t)sb.f_bavail * sb.f_frsize;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700769 } else {
770 return -1;
771 }
772}
773
774// TODO: borrowed from frameworks/native/libs/diskusage/ which should
775// eventually be migrated into system/
Paul Crowley14c8c072018-09-18 13:30:21 -0700776static int64_t stat_size(struct stat* s) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700777 int64_t blksize = s->st_blksize;
778 // count actual blocks used instead of nominal file size
779 int64_t size = s->st_blocks * 512;
780
781 if (blksize) {
782 /* round up to filesystem block size */
783 size = (size + blksize - 1) & (~(blksize - 1));
784 }
785
786 return size;
787}
788
789// TODO: borrowed from frameworks/native/libs/diskusage/ which should
790// eventually be migrated into system/
791int64_t calculate_dir_size(int dfd) {
792 int64_t size = 0;
793 struct stat s;
Paul Crowley14c8c072018-09-18 13:30:21 -0700794 DIR* d;
795 struct dirent* de;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700796
797 d = fdopendir(dfd);
798 if (d == NULL) {
799 close(dfd);
800 return 0;
801 }
802
803 while ((de = readdir(d))) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700804 const char* name = de->d_name;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700805 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
806 size += stat_size(&s);
807 }
808 if (de->d_type == DT_DIR) {
809 int subfd;
810
811 /* always skip "." and ".." */
812 if (name[0] == '.') {
Paul Crowley14c8c072018-09-18 13:30:21 -0700813 if (name[1] == 0) continue;
814 if ((name[1] == '.') && (name[2] == 0)) continue;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700815 }
816
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600817 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700818 if (subfd >= 0) {
819 size += calculate_dir_size(subfd);
820 }
821 }
822 }
823 closedir(d);
824 return size;
825}
826
827uint64_t GetTreeBytes(const std::string& path) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600828 int dirfd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700829 if (dirfd < 0) {
830 PLOG(WARNING) << "Failed to open " << path;
831 return -1;
832 } else {
Josh Gao72fb1a62018-05-29 19:05:16 -0700833 return calculate_dir_size(dirfd);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700834 }
835}
836
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700837bool IsFilesystemSupported(const std::string& fsType) {
838 std::string supported;
839 if (!ReadFileToString(kProcFilesystems, &supported)) {
840 PLOG(ERROR) << "Failed to read supported filesystems";
841 return false;
842 }
843 return supported.find(fsType + "\n") != std::string::npos;
844}
845
846status_t WipeBlockDevice(const std::string& path) {
847 status_t res = -1;
848 const char* c_path = path.c_str();
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200849 uint64_t range[2] = {0, 0};
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700850
851 int fd = TEMP_FAILURE_RETRY(open(c_path, O_RDWR | O_CLOEXEC));
852 if (fd == -1) {
853 PLOG(ERROR) << "Failed to open " << path;
854 goto done;
855 }
856
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200857 if (GetBlockDevSize(fd, &range[1]) != OK) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700858 PLOG(ERROR) << "Failed to determine size of " << path;
859 goto done;
860 }
861
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700862 LOG(INFO) << "About to discard " << range[1] << " on " << path;
863 if (ioctl(fd, BLKDISCARD, &range) == 0) {
864 LOG(INFO) << "Discard success on " << path;
865 res = 0;
866 } else {
867 PLOG(ERROR) << "Discard failure on " << path;
868 }
869
870done:
871 close(fd);
872 return res;
873}
874
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800875static bool isValidFilename(const std::string& name) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700876 if (name.empty() || (name == ".") || (name == "..") || (name.find('/') != std::string::npos)) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800877 return false;
878 } else {
879 return true;
880 }
881}
882
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700883std::string BuildKeyPath(const std::string& partGuid) {
884 return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str());
885}
886
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600887std::string BuildDataSystemLegacyPath(userid_t userId) {
Paul Crowley3b71fc52017-10-09 10:55:21 -0700888 return StringPrintf("%s/system/users/%u", BuildDataPath("").c_str(), userId);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600889}
890
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800891std::string BuildDataSystemCePath(userid_t userId) {
Paul Crowley3b71fc52017-10-09 10:55:21 -0700892 return StringPrintf("%s/system_ce/%u", BuildDataPath("").c_str(), userId);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700893}
894
895std::string BuildDataSystemDePath(userid_t userId) {
Paul Crowley3b71fc52017-10-09 10:55:21 -0700896 return StringPrintf("%s/system_de/%u", BuildDataPath("").c_str(), userId);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700897}
898
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600899std::string BuildDataMiscLegacyPath(userid_t userId) {
Paul Crowley3b71fc52017-10-09 10:55:21 -0700900 return StringPrintf("%s/misc/user/%u", BuildDataPath("").c_str(), userId);
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600901}
902
Jeff Sharkey47695b22016-02-01 17:02:29 -0700903std::string BuildDataMiscCePath(userid_t userId) {
Paul Crowley3b71fc52017-10-09 10:55:21 -0700904 return StringPrintf("%s/misc_ce/%u", BuildDataPath("").c_str(), userId);
Jeff Sharkey47695b22016-02-01 17:02:29 -0700905}
906
907std::string BuildDataMiscDePath(userid_t userId) {
Paul Crowley3b71fc52017-10-09 10:55:21 -0700908 return StringPrintf("%s/misc_de/%u", BuildDataPath("").c_str(), userId);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800909}
910
Calin Juravle79f55a42016-02-17 20:14:46 +0000911// Keep in sync with installd (frameworks/native/cmds/installd/utils.h)
912std::string BuildDataProfilesDePath(userid_t userId) {
Paul Crowley3b71fc52017-10-09 10:55:21 -0700913 return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath("").c_str(), userId);
Calin Juravle79f55a42016-02-17 20:14:46 +0000914}
915
Andreas Huber71cd43f2018-01-22 11:25:29 -0800916std::string BuildDataVendorCePath(userid_t userId) {
917 return StringPrintf("%s/vendor_ce/%u", BuildDataPath("").c_str(), userId);
918}
919
920std::string BuildDataVendorDePath(userid_t userId) {
921 return StringPrintf("%s/vendor_de/%u", BuildDataPath("").c_str(), userId);
922}
923
Paul Crowley3b71fc52017-10-09 10:55:21 -0700924std::string BuildDataPath(const std::string& volumeUuid) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800925 // TODO: unify with installd path generation logic
Paul Crowley3b71fc52017-10-09 10:55:21 -0700926 if (volumeUuid.empty()) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800927 return "/data";
928 } else {
929 CHECK(isValidFilename(volumeUuid));
Paul Crowley3b71fc52017-10-09 10:55:21 -0700930 return StringPrintf("/mnt/expand/%s", volumeUuid.c_str());
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800931 }
932}
933
Paul Crowley3b71fc52017-10-09 10:55:21 -0700934std::string BuildDataMediaCePath(const std::string& volumeUuid, userid_t userId) {
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700935 // TODO: unify with installd path generation logic
936 std::string data(BuildDataPath(volumeUuid));
937 return StringPrintf("%s/media/%u", data.c_str(), userId);
938}
939
Paul Crowley3b71fc52017-10-09 10:55:21 -0700940std::string BuildDataUserCePath(const std::string& volumeUuid, userid_t userId) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800941 // TODO: unify with installd path generation logic
942 std::string data(BuildDataPath(volumeUuid));
Paul Crowley3b71fc52017-10-09 10:55:21 -0700943 if (volumeUuid.empty() && userId == 0) {
cjbaoeb501142017-04-12 00:09:00 +0800944 std::string legacy = StringPrintf("%s/data", data.c_str());
945 struct stat sb;
946 if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
947 /* /data/data is dir, return /data/data for legacy system */
948 return legacy;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800949 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800950 }
cjbaoeb501142017-04-12 00:09:00 +0800951 return StringPrintf("%s/user/%u", data.c_str(), userId);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800952}
953
Paul Crowley3b71fc52017-10-09 10:55:21 -0700954std::string BuildDataUserDePath(const std::string& volumeUuid, userid_t userId) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800955 // TODO: unify with installd path generation logic
956 std::string data(BuildDataPath(volumeUuid));
957 return StringPrintf("%s/user_de/%u", data.c_str(), userId);
958}
959
Jeff Sharkey66270a22015-06-24 11:49:24 -0700960dev_t GetDevice(const std::string& path) {
961 struct stat sb;
962 if (stat(path.c_str(), &sb)) {
963 PLOG(WARNING) << "Failed to stat " << path;
964 return 0;
965 } else {
966 return sb.st_dev;
967 }
968}
969
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600970status_t RestoreconRecursive(const std::string& path) {
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700971 LOG(DEBUG) << "Starting restorecon of " << path;
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600972
Tom Cherryd6127ef2017-06-15 17:13:56 -0700973 static constexpr const char* kRestoreconString = "selinux.restorecon_recursive";
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600974
Tom Cherryd6127ef2017-06-15 17:13:56 -0700975 android::base::SetProperty(kRestoreconString, "");
976 android::base::SetProperty(kRestoreconString, path);
977
978 android::base::WaitForProperty(kRestoreconString, path);
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600979
Sudheer Shanka4b6ca4e2018-09-21 10:54:54 -0700980 LOG(DEBUG) << "Finished restorecon of " << path;
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600981 return OK;
982}
983
Jeff Sharkey3472e522017-10-06 18:02:53 -0600984bool Readlinkat(int dirfd, const std::string& path, std::string* result) {
985 // Shamelessly borrowed from android::base::Readlink()
986 result->clear();
987
988 // Most Linux file systems (ext2 and ext4, say) limit symbolic links to
989 // 4095 bytes. Since we'll copy out into the string anyway, it doesn't
990 // waste memory to just start there. We add 1 so that we can recognize
991 // whether it actually fit (rather than being truncated to 4095).
992 std::vector<char> buf(4095 + 1);
993 while (true) {
994 ssize_t size = readlinkat(dirfd, path.c_str(), &buf[0], buf.size());
995 // Unrecoverable error?
Paul Crowley14c8c072018-09-18 13:30:21 -0700996 if (size == -1) return false;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600997 // It fit! (If size == buf.size(), it may have been truncated.)
998 if (static_cast<size_t>(size) < buf.size()) {
999 result->assign(&buf[0], size);
1000 return true;
1001 }
1002 // Double our buffer and try again.
1003 buf.resize(buf.size() * 2);
Daichi Hirono10d34882016-01-29 14:33:51 +09001004 }
1005}
1006
Yu Ning942d4e82016-01-08 17:36:47 +08001007bool IsRunningInEmulator() {
Tom Cherryd6127ef2017-06-15 17:13:56 -07001008 return android::base::GetBoolProperty("ro.kernel.qemu", false);
Yu Ning942d4e82016-01-08 17:36:47 +08001009}
1010
Sudheer Shanka295fb242019-01-16 23:04:07 -08001011static status_t findMountPointsWithPrefix(const std::string& prefix,
1012 std::list<std::string>& mountPoints) {
1013 // Add a trailing slash if the client didn't provide one so that we don't match /foo/barbaz
1014 // when the prefix is /foo/bar
1015 std::string prefixWithSlash(prefix);
1016 if (prefix.back() != '/') {
1017 android::base::StringAppendF(&prefixWithSlash, "/");
1018 }
1019
1020 std::unique_ptr<FILE, int (*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
1021 if (!mnts) {
1022 PLOG(ERROR) << "Unable to open /proc/mounts";
1023 return -errno;
1024 }
1025
1026 // Some volumes can be stacked on each other, so force unmount in
1027 // reverse order to give us the best chance of success.
1028 struct mntent* mnt; // getmntent returns a thread local, so it's safe.
1029 while ((mnt = getmntent(mnts.get())) != nullptr) {
1030 auto mountPoint = std::string(mnt->mnt_dir) + "/";
1031 if (android::base::StartsWith(mountPoint, prefixWithSlash)) {
1032 mountPoints.push_front(mountPoint);
1033 }
1034 }
1035 return OK;
1036}
1037
1038// Unmount all mountpoints that start with prefix. prefix itself doesn't need to be a mountpoint.
1039status_t UnmountTreeWithPrefix(const std::string& prefix) {
1040 std::list<std::string> toUnmount;
1041 status_t result = findMountPointsWithPrefix(prefix, toUnmount);
1042 if (result < 0) {
1043 return result;
1044 }
1045 for (const auto& path : toUnmount) {
1046 if (umount2(path.c_str(), MNT_DETACH)) {
1047 PLOG(ERROR) << "Failed to unmount " << path;
1048 result = -errno;
1049 }
1050 }
1051 return result;
1052}
1053
1054status_t UnmountTree(const std::string& mountPoint) {
Sudheer Shanka023b5392019-02-06 12:39:19 -08001055 if (TEMP_FAILURE_RETRY(umount2(mountPoint.c_str(), MNT_DETACH)) < 0 && errno != EINVAL &&
1056 errno != ENOENT) {
Sudheer Shanka295fb242019-01-16 23:04:07 -08001057 PLOG(ERROR) << "Failed to unmount " << mountPoint;
Sudheer Shanka89ddf992018-09-25 14:22:07 -07001058 return -errno;
1059 }
Sudheer Shanka89ddf992018-09-25 14:22:07 -07001060 return OK;
1061}
1062
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001063static status_t delete_dir_contents(DIR* dir) {
1064 // Shamelessly borrowed from android::installd
1065 int dfd = dirfd(dir);
1066 if (dfd < 0) {
1067 return -errno;
1068 }
1069
Sudheer Shanka6bf14802019-01-17 13:38:10 -08001070 status_t result = OK;
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001071 struct dirent* de;
1072 while ((de = readdir(dir))) {
1073 const char* name = de->d_name;
1074 if (de->d_type == DT_DIR) {
1075 /* always skip "." and ".." */
1076 if (name[0] == '.') {
1077 if (name[1] == 0) continue;
1078 if ((name[1] == '.') && (name[2] == 0)) continue;
1079 }
1080
1081 android::base::unique_fd subfd(
1082 openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC));
1083 if (subfd.get() == -1) {
1084 PLOG(ERROR) << "Couldn't openat " << name;
1085 result = -errno;
1086 continue;
1087 }
Josh Gaoe3c32e02018-11-05 13:47:28 -08001088 std::unique_ptr<DIR, decltype(&closedir)> subdirp(
1089 android::base::Fdopendir(std::move(subfd)), closedir);
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001090 if (!subdirp) {
1091 PLOG(ERROR) << "Couldn't fdopendir " << name;
1092 result = -errno;
1093 continue;
1094 }
1095 result = delete_dir_contents(subdirp.get());
1096 if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
1097 PLOG(ERROR) << "Couldn't unlinkat " << name;
1098 result = -errno;
1099 }
1100 } else {
1101 if (unlinkat(dfd, name, 0) < 0) {
1102 PLOG(ERROR) << "Couldn't unlinkat " << name;
1103 result = -errno;
1104 }
1105 }
1106 }
1107 return result;
1108}
1109
1110status_t DeleteDirContentsAndDir(const std::string& pathname) {
Sudheer Shanka30df1c62019-02-22 17:03:02 -08001111 status_t res = DeleteDirContents(pathname);
1112 if (res < 0) {
1113 return res;
1114 }
Sudheer Shanka8255a2b2019-02-25 12:21:23 -08001115 if (TEMP_FAILURE_RETRY(rmdir(pathname.c_str())) < 0 && errno != ENOENT) {
Sudheer Shanka30df1c62019-02-22 17:03:02 -08001116 PLOG(ERROR) << "rmdir failed on " << pathname;
1117 return -errno;
1118 }
1119 LOG(VERBOSE) << "Success: rmdir on " << pathname;
1120 return OK;
1121}
1122
1123status_t DeleteDirContents(const std::string& pathname) {
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001124 // Shamelessly borrowed from android::installd
1125 std::unique_ptr<DIR, decltype(&closedir)> dirp(opendir(pathname.c_str()), closedir);
1126 if (!dirp) {
1127 if (errno == ENOENT) {
1128 return OK;
1129 }
1130 PLOG(ERROR) << "Failed to opendir " << pathname;
1131 return -errno;
1132 }
Sudheer Shanka30df1c62019-02-22 17:03:02 -08001133 return delete_dir_contents(dirp.get());
Sudheer Shanka40ab6742018-09-18 13:07:45 -07001134}
1135
Paul Crowley298fa322018-10-30 15:59:24 -07001136// TODO(118708649): fix duplication with init/util.h
1137status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout) {
1138 android::base::Timer t;
1139 while (t.duration() < timeout) {
1140 struct stat sb;
1141 if (stat(filename, &sb) != -1) {
1142 LOG(INFO) << "wait for '" << filename << "' took " << t;
1143 return 0;
1144 }
1145 std::this_thread::sleep_for(10ms);
1146 }
1147 LOG(WARNING) << "wait for '" << filename << "' timed out and took " << t;
1148 return -1;
1149}
1150
Paul Crowley621d9b92018-12-07 15:36:09 -08001151bool FsyncDirectory(const std::string& dirname) {
1152 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dirname.c_str(), O_RDONLY | O_CLOEXEC)));
1153 if (fd == -1) {
1154 PLOG(ERROR) << "Failed to open " << dirname;
1155 return false;
1156 }
1157 if (fsync(fd) == -1) {
1158 if (errno == EROFS || errno == EINVAL) {
1159 PLOG(WARNING) << "Skip fsync " << dirname
1160 << " on a file system does not support synchronization";
1161 } else {
1162 PLOG(ERROR) << "Failed to fsync " << dirname;
1163 return false;
1164 }
1165 }
1166 return true;
1167}
1168
Tommy Chiu0bd2d112019-03-26 17:18:09 +08001169bool writeStringToFile(const std::string& payload, const std::string& filename) {
1170 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
1171 open(filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0666)));
1172 if (fd == -1) {
1173 PLOG(ERROR) << "Failed to open " << filename;
1174 return false;
1175 }
1176 if (!android::base::WriteStringToFd(payload, fd)) {
1177 PLOG(ERROR) << "Failed to write to " << filename;
1178 unlink(filename.c_str());
1179 return false;
1180 }
1181 // fsync as close won't guarantee flush data
1182 // see close(2), fsync(2) and b/68901441
1183 if (fsync(fd) == -1) {
1184 if (errno == EROFS || errno == EINVAL) {
1185 PLOG(WARNING) << "Skip fsync " << filename
1186 << " on a file system does not support synchronization";
1187 } else {
1188 PLOG(ERROR) << "Failed to fsync " << filename;
1189 unlink(filename.c_str());
1190 return false;
1191 }
1192 }
1193 return true;
1194}
1195
Zima438b242019-09-25 14:37:38 +01001196status_t MountUserFuse(userid_t user_id, const std::string& absolute_lower_path,
1197 const std::string& relative_upper_path, android::base::unique_fd* fuse_fd) {
1198 std::string pre_fuse_path(StringPrintf("/mnt/user/%d", user_id));
1199 std::string fuse_path(
1200 StringPrintf("%s/%s", pre_fuse_path.c_str(), relative_upper_path.c_str()));
1201
1202 std::string pre_pass_through_path(StringPrintf("/mnt/pass_through/%d", user_id));
1203 std::string pass_through_path(
1204 StringPrintf("%s/%s", pre_pass_through_path.c_str(), relative_upper_path.c_str()));
Zim3623a212019-07-19 16:46:53 +01001205
Zim1242be82020-01-22 18:22:29 +00001206 // Ensure that /mnt/user is 0700. With FUSE, apps don't need access to /mnt/user paths directly.
1207 // Without FUSE however, apps need /mnt/user access so /mnt/user in init.rc is 0755 until here
Zim4dd47092020-01-29 02:44:46 +00001208 auto result = PrepareDir("/mnt/user", 0750, AID_ROOT, AID_MEDIA_RW);
Zim1242be82020-01-22 18:22:29 +00001209 if (result != android::OK) {
1210 PLOG(ERROR) << "Failed to prepare directory /mnt/user";
1211 return -1;
1212 }
1213
Zim06b0caf2020-01-05 02:11:47 +00001214 // Shell is neither AID_ROOT nor AID_EVERYBODY. Since it equally needs 'execute' access to
1215 // /mnt/user/0 to 'adb shell ls /sdcard' for instance, we set the uid bit of /mnt/user/0 to
1216 // AID_SHELL. This gives shell access along with apps running as group everybody (user 0 apps)
1217 // These bits should be consistent with what is set in zygote in
1218 // com_android_internal_os_Zygote#MountEmulatedStorage on volume bind mount during app fork
Zim1242be82020-01-22 18:22:29 +00001219 result = PrepareDir(pre_fuse_path, 0710, user_id ? AID_ROOT : AID_SHELL,
Zim06b0caf2020-01-05 02:11:47 +00001220 multiuser_get_uid(user_id, AID_EVERYBODY));
Zim3623a212019-07-19 16:46:53 +01001221 if (result != android::OK) {
Zima438b242019-09-25 14:37:38 +01001222 PLOG(ERROR) << "Failed to prepare directory " << pre_fuse_path;
Zim3623a212019-07-19 16:46:53 +01001223 return -1;
1224 }
1225
Zima438b242019-09-25 14:37:38 +01001226 result = PrepareDir(fuse_path, 0700, AID_ROOT, AID_ROOT);
1227 if (result != android::OK) {
1228 PLOG(ERROR) << "Failed to prepare directory " << fuse_path;
1229 return -1;
1230 }
1231
Zim26eec702020-01-31 16:00:58 +00001232 result = PrepareDir(pre_pass_through_path, 0710, AID_ROOT, AID_MEDIA_RW);
Zima438b242019-09-25 14:37:38 +01001233 if (result != android::OK) {
1234 PLOG(ERROR) << "Failed to prepare directory " << pre_pass_through_path;
1235 return -1;
1236 }
1237
Zim26eec702020-01-31 16:00:58 +00001238 result = PrepareDir(pass_through_path, 0710, AID_ROOT, AID_MEDIA_RW);
Zima438b242019-09-25 14:37:38 +01001239 if (result != android::OK) {
1240 PLOG(ERROR) << "Failed to prepare directory " << pass_through_path;
1241 return -1;
1242 }
1243
1244 if (relative_upper_path == "emulated") {
Zime5393d42019-11-15 11:44:12 +00001245 std::string linkpath(StringPrintf("/mnt/user/%d/self", user_id));
1246 result = PrepareDir(linkpath, 0755, AID_ROOT, AID_ROOT);
Zima438b242019-09-25 14:37:38 +01001247 if (result != android::OK) {
Zime5393d42019-11-15 11:44:12 +00001248 PLOG(ERROR) << "Failed to prepare directory " << linkpath;
Zima438b242019-09-25 14:37:38 +01001249 return -1;
1250 }
Zime5393d42019-11-15 11:44:12 +00001251 linkpath += "/primary";
Zim53d16d32020-01-17 01:21:24 +00001252 Symlink("/storage/emulated/" + std::to_string(user_id), linkpath);
Zimaea12472020-01-08 11:09:47 +00001253
1254 std::string pass_through_linkpath(StringPrintf("/mnt/pass_through/%d/self", user_id));
Zim26eec702020-01-31 16:00:58 +00001255 result = PrepareDir(pass_through_linkpath, 0710, AID_ROOT, AID_MEDIA_RW);
Zimaea12472020-01-08 11:09:47 +00001256 if (result != android::OK) {
1257 PLOG(ERROR) << "Failed to prepare directory " << pass_through_linkpath;
1258 return -1;
1259 }
1260 pass_through_linkpath += "/primary";
Zim53d16d32020-01-17 01:21:24 +00001261 Symlink("/storage/emulated/" + std::to_string(user_id), pass_through_linkpath);
Zima438b242019-09-25 14:37:38 +01001262 }
1263
Nandana Dutta914cc72019-08-29 15:22:42 +01001264 // Open fuse fd.
1265 fuse_fd->reset(open("/dev/fuse", O_RDWR | O_CLOEXEC));
1266 if (fuse_fd->get() == -1) {
Zim3623a212019-07-19 16:46:53 +01001267 PLOG(ERROR) << "Failed to open /dev/fuse";
1268 return -1;
1269 }
1270
1271 // Note: leaving out default_permissions since we don't want kernel to do lower filesystem
1272 // permission checks before routing to FUSE daemon.
1273 const auto opts = StringPrintf(
1274 "fd=%i,"
1275 "rootmode=40000,"
1276 "allow_other,"
1277 "user_id=0,group_id=0,",
Nandana Dutta914cc72019-08-29 15:22:42 +01001278 fuse_fd->get());
Zim3623a212019-07-19 16:46:53 +01001279
Zima438b242019-09-25 14:37:38 +01001280 result = TEMP_FAILURE_RETRY(mount("/dev/fuse", fuse_path.c_str(), "fuse",
1281 MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME | MS_LAZYTIME,
1282 opts.c_str()));
1283 if (result != 0) {
1284 PLOG(ERROR) << "Failed to mount " << fuse_path;
Zim3623a212019-07-19 16:46:53 +01001285 return -errno;
1286 }
Martijn Coenen6f5802e2019-11-28 11:53:53 +01001287
Martijn Coenen86f21a22020-01-06 09:48:14 +01001288 if (IsFilesystemSupported("sdcardfs")) {
1289 std::string sdcardfs_path(
1290 StringPrintf("/mnt/runtime/full/%s", relative_upper_path.c_str()));
1291
1292 LOG(INFO) << "Bind mounting " << sdcardfs_path << " to " << pass_through_path;
1293 return BindMount(sdcardfs_path, pass_through_path);
1294 } else {
1295 LOG(INFO) << "Bind mounting " << absolute_lower_path << " to " << pass_through_path;
1296 return BindMount(absolute_lower_path, pass_through_path);
1297 }
Zima438b242019-09-25 14:37:38 +01001298}
1299
Martijn Coenen6f5802e2019-11-28 11:53:53 +01001300status_t UnmountUserFuse(userid_t user_id, const std::string& absolute_lower_path,
1301 const std::string& relative_upper_path) {
1302 std::string fuse_path(StringPrintf("/mnt/user/%d/%s", user_id, relative_upper_path.c_str()));
1303 std::string pass_through_path(
1304 StringPrintf("/mnt/pass_through/%d/%s", user_id, relative_upper_path.c_str()));
1305
Zima438b242019-09-25 14:37:38 +01001306 // Best effort unmount pass_through path
1307 sSleepOnUnmount = false;
Martijn Coenen6f5802e2019-11-28 11:53:53 +01001308 LOG(INFO) << "Unmounting pass_through_path " << pass_through_path;
1309 auto status = ForceUnmount(pass_through_path);
1310 if (status != android::OK) {
1311 LOG(ERROR) << "Failed to unmount " << pass_through_path;
1312 }
Martijn Coenen57002612019-11-28 11:56:13 +01001313 rmdir(pass_through_path.c_str());
Martijn Coenen6f5802e2019-11-28 11:53:53 +01001314
1315 LOG(INFO) << "Unmounting fuse path " << fuse_path;
Zima438b242019-09-25 14:37:38 +01001316 android::status_t result = ForceUnmount(fuse_path);
1317 sSleepOnUnmount = true;
1318 if (result != android::OK) {
1319 // TODO(b/135341433): MNT_DETACH is needed for fuse because umount2 can fail with EBUSY.
1320 // Figure out why we get EBUSY and remove this special casing if possible.
1321 PLOG(ERROR) << "Failed to unmount. Trying MNT_DETACH " << fuse_path << " ...";
1322 if (umount2(fuse_path.c_str(), UMOUNT_NOFOLLOW | MNT_DETACH) && errno != EINVAL &&
1323 errno != ENOENT) {
1324 PLOG(ERROR) << "Failed to unmount with MNT_DETACH " << fuse_path;
1325 return -errno;
1326 }
Martijn Coenen57002612019-11-28 11:56:13 +01001327 result = android::OK;
Zima438b242019-09-25 14:37:38 +01001328 }
Martijn Coenen57002612019-11-28 11:56:13 +01001329 rmdir(fuse_path.c_str());
1330
Zima438b242019-09-25 14:37:38 +01001331 return result;
Zim3623a212019-07-19 16:46:53 +01001332}
1333
Martijn Coenen62a4b272020-01-31 15:23:09 +01001334status_t PrepareAndroidDirs(const std::string& volumeRoot) {
1335 std::string androidDir = volumeRoot + kAndroidDir;
1336 std::string androidDataDir = volumeRoot + kAppDataDir;
1337 std::string androidObbDir = volumeRoot + kAppObbDir;
Zima13d81b2020-02-07 16:39:31 +00001338 std::string androidMediaDir = volumeRoot + kAppMediaDir;
Martijn Coenen62a4b272020-01-31 15:23:09 +01001339
1340 bool useSdcardFs = IsFilesystemSupported("sdcardfs");
1341
1342 if (fs_prepare_dir(androidDir.c_str(), 0771, AID_MEDIA_RW, AID_MEDIA_RW) != 0) {
1343 PLOG(ERROR) << "Failed to create " << androidDir;
1344 return -errno;
1345 }
1346
1347 gid_t dataGid = useSdcardFs ? AID_MEDIA_RW : AID_EXT_DATA_RW;
1348 if (fs_prepare_dir(androidDataDir.c_str(), 0771, AID_MEDIA_RW, dataGid) != 0) {
1349 PLOG(ERROR) << "Failed to create " << androidDataDir;
1350 return -errno;
1351 }
1352
1353 gid_t obbGid = useSdcardFs ? AID_MEDIA_RW : AID_EXT_OBB_RW;
1354 if (fs_prepare_dir(androidObbDir.c_str(), 0771, AID_MEDIA_RW, obbGid) != 0) {
1355 PLOG(ERROR) << "Failed to create " << androidObbDir;
1356 return -errno;
1357 }
1358
Zima13d81b2020-02-07 16:39:31 +00001359 if (fs_prepare_dir(androidMediaDir.c_str(), 0771, AID_MEDIA_RW, AID_MEDIA_RW) != 0) {
1360 PLOG(ERROR) << "Failed to create " << androidMediaDir;
1361 return -errno;
1362 }
1363
Martijn Coenen62a4b272020-01-31 15:23:09 +01001364 return OK;
1365}
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001366} // namespace vold
1367} // namespace android