blob: 2b8d0a5a11ab92fd6125d6f499188a45f41c8727 [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 "sehandle.h"
18#include "Utils.h"
19#include "Process.h"
Keun-young Park375ac252017-08-02 17:45:48 -070020#include "VolumeManager.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080021
Elliott Hughes7e128fb2015-12-04 15:50:53 -080022#include <android-base/file.h>
23#include <android-base/logging.h>
24#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025#include <cutils/fs.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070026#include <cutils/properties.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080027#include <private/android_filesystem_config.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070028#include <logwrap/logwrap.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070030#include <mutex>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070031#include <dirent.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032#include <fcntl.h>
33#include <linux/fs.h>
34#include <stdlib.h>
35#include <sys/mount.h>
36#include <sys/types.h>
37#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070038#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080039#include <sys/wait.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070040#include <sys/statvfs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080041
42#ifndef UMOUNT_NOFOLLOW
43#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
44#endif
45
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070046using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070047using android::base::StringPrintf;
48
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049namespace android {
50namespace vold {
51
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070052security_context_t sBlkidContext = nullptr;
53security_context_t sBlkidUntrustedContext = nullptr;
54security_context_t sFsckContext = nullptr;
55security_context_t sFsckUntrustedContext = nullptr;
56
Jeff Sharkey9c484982015-03-31 10:35:33 -070057static const char* kBlkidPath = "/system/bin/blkid";
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070058static const char* kKeyPath = "/data/misc/vold";
Jeff Sharkey9c484982015-03-31 10:35:33 -070059
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070060static const char* kProcFilesystems = "/proc/filesystems";
61
Jeff Sharkeydeb24052015-03-02 21:01:40 -080062status_t CreateDeviceNode(const std::string& path, dev_t dev) {
63 const char* cpath = path.c_str();
64 status_t res = 0;
65
66 char* secontext = nullptr;
67 if (sehandle) {
68 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) {
69 setfscreatecon(secontext);
70 }
71 }
72
73 mode_t mode = 0660 | S_IFBLK;
74 if (mknod(cpath, mode, dev) < 0) {
75 if (errno != EEXIST) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070076 PLOG(ERROR) << "Failed to create device node for " << major(dev)
77 << ":" << minor(dev) << " at " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078 res = -errno;
79 }
80 }
81
82 if (secontext) {
83 setfscreatecon(nullptr);
84 freecon(secontext);
85 }
86
87 return res;
88}
89
90status_t DestroyDeviceNode(const std::string& path) {
91 const char* cpath = path.c_str();
92 if (TEMP_FAILURE_RETRY(unlink(cpath))) {
93 return -errno;
94 } else {
95 return OK;
96 }
97}
98
Jeff Sharkeyf0121c52015-04-06 14:08:45 -070099status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
100 const char* cpath = path.c_str();
101
102 char* secontext = nullptr;
103 if (sehandle) {
104 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFDIR)) {
105 setfscreatecon(secontext);
106 }
107 }
108
109 int res = fs_prepare_dir(cpath, mode, uid, gid);
110
111 if (secontext) {
112 setfscreatecon(nullptr);
113 freecon(secontext);
114 }
115
116 if (res == 0) {
117 return OK;
118 } else {
119 return -errno;
120 }
121}
122
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800123status_t ForceUnmount(const std::string& path) {
124 const char* cpath = path.c_str();
125 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
126 return OK;
127 }
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700128 // Apps might still be handling eject request, so wait before
129 // we start sending signals
Keun-young Park375ac252017-08-02 17:45:48 -0700130 if (!VolumeManager::shutting_down) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700131
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700132 Process::killProcessesWithOpenFiles(cpath, SIGINT);
Keun-young Park375ac252017-08-02 17:45:48 -0700133 if (!VolumeManager::shutting_down) sleep(5);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700134 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
135 return OK;
136 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700137
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800138 Process::killProcessesWithOpenFiles(cpath, SIGTERM);
Keun-young Park375ac252017-08-02 17:45:48 -0700139 if (!VolumeManager::shutting_down) sleep(5);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800140 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
141 return OK;
142 }
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700143
144 Process::killProcessesWithOpenFiles(cpath, SIGKILL);
Keun-young Park375ac252017-08-02 17:45:48 -0700145 if (!VolumeManager::shutting_down) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700146 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
147 return OK;
148 }
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700149
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800150 return -errno;
151}
152
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700153status_t KillProcessesUsingPath(const std::string& path) {
154 const char* cpath = path.c_str();
155 if (Process::killProcessesWithOpenFiles(cpath, SIGINT) == 0) {
156 return OK;
157 }
Keun-young Park375ac252017-08-02 17:45:48 -0700158 if (!VolumeManager::shutting_down) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700159
160 if (Process::killProcessesWithOpenFiles(cpath, SIGTERM) == 0) {
161 return OK;
162 }
Keun-young Park375ac252017-08-02 17:45:48 -0700163 if (!VolumeManager::shutting_down) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700164
165 if (Process::killProcessesWithOpenFiles(cpath, SIGKILL) == 0) {
166 return OK;
167 }
Keun-young Park375ac252017-08-02 17:45:48 -0700168 if (!VolumeManager::shutting_down) sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700169
170 // Send SIGKILL a second time to determine if we've
171 // actually killed everyone with open files
172 if (Process::killProcessesWithOpenFiles(cpath, SIGKILL) == 0) {
173 return OK;
174 }
175 PLOG(ERROR) << "Failed to kill processes using " << path;
176 return -EBUSY;
177}
178
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179status_t BindMount(const std::string& source, const std::string& target) {
180 if (::mount(source.c_str(), target.c_str(), "", MS_BIND, NULL)) {
181 PLOG(ERROR) << "Failed to bind mount " << source << " to " << target;
182 return -errno;
183 }
184 return OK;
185}
186
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700187static status_t readMetadata(const std::string& path, std::string& fsType,
188 std::string& fsUuid, std::string& fsLabel, bool untrusted) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700189 fsType.clear();
190 fsUuid.clear();
191 fsLabel.clear();
192
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700193 std::vector<std::string> cmd;
194 cmd.push_back(kBlkidPath);
195 cmd.push_back("-c");
196 cmd.push_back("/dev/null");
Jeff Sharkeyeddf9bd2015-08-12 16:04:35 -0700197 cmd.push_back("-s");
198 cmd.push_back("TYPE");
199 cmd.push_back("-s");
200 cmd.push_back("UUID");
201 cmd.push_back("-s");
202 cmd.push_back("LABEL");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700203 cmd.push_back(path);
204
205 std::vector<std::string> output;
206 status_t res = ForkExecvp(cmd, output, untrusted ? sBlkidUntrustedContext : sBlkidContext);
207 if (res != OK) {
208 LOG(WARNING) << "blkid failed to identify " << path;
209 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700210 }
211
Jeff Sharkey9c484982015-03-31 10:35:33 -0700212 char value[128];
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700213 for (const auto& line : output) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700214 // Extract values from blkid output, if defined
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700215 const char* cline = line.c_str();
Dan Austin49ab5f92016-03-24 12:26:39 -0700216 const char* start = strstr(cline, "TYPE=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700217 if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
218 fsType = value;
219 }
220
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700221 start = strstr(cline, "UUID=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700222 if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
223 fsUuid = value;
224 }
225
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700226 start = strstr(cline, "LABEL=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700227 if (start != nullptr && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) {
228 fsLabel = value;
229 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700230 }
231
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700232 return OK;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700233}
234
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700235status_t ReadMetadata(const std::string& path, std::string& fsType,
236 std::string& fsUuid, std::string& fsLabel) {
237 return readMetadata(path, fsType, fsUuid, fsLabel, false);
238}
239
240status_t ReadMetadataUntrusted(const std::string& path, std::string& fsType,
241 std::string& fsUuid, std::string& fsLabel) {
242 return readMetadata(path, fsType, fsUuid, fsLabel, true);
243}
244
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700245status_t ForkExecvp(const std::vector<std::string>& args) {
246 return ForkExecvp(args, nullptr);
247}
248
249status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context) {
250 size_t argc = args.size();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700251 char** argv = (char**) calloc(argc, sizeof(char*));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700252 for (size_t i = 0; i < argc; i++) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700253 argv[i] = (char*) args[i].c_str();
254 if (i == 0) {
255 LOG(VERBOSE) << args[i];
256 } else {
257 LOG(VERBOSE) << " " << args[i];
258 }
259 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700260
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700261 if (setexeccon(context)) {
262 LOG(ERROR) << "Failed to setexeccon";
263 abort();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700264 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700265 status_t res = android_fork_execvp(argc, argv, NULL, false, true);
266 if (setexeccon(nullptr)) {
267 LOG(ERROR) << "Failed to setexeccon";
268 abort();
269 }
270
Jeff Sharkey9c484982015-03-31 10:35:33 -0700271 free(argv);
272 return res;
273}
274
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700275status_t ForkExecvp(const std::vector<std::string>& args,
276 std::vector<std::string>& output) {
277 return ForkExecvp(args, output, nullptr);
278}
279
280status_t ForkExecvp(const std::vector<std::string>& args,
281 std::vector<std::string>& output, security_context_t context) {
282 std::string cmd;
283 for (size_t i = 0; i < args.size(); i++) {
284 cmd += args[i] + " ";
285 if (i == 0) {
286 LOG(VERBOSE) << args[i];
287 } else {
288 LOG(VERBOSE) << " " << args[i];
289 }
290 }
291 output.clear();
292
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700293 if (setexeccon(context)) {
294 LOG(ERROR) << "Failed to setexeccon";
295 abort();
296 }
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600297 FILE* fp = popen(cmd.c_str(), "r"); // NOLINT
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700298 if (setexeccon(nullptr)) {
299 LOG(ERROR) << "Failed to setexeccon";
300 abort();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700301 }
302
303 if (!fp) {
304 PLOG(ERROR) << "Failed to popen " << cmd;
305 return -errno;
306 }
307 char line[1024];
308 while (fgets(line, sizeof(line), fp) != nullptr) {
309 LOG(VERBOSE) << line;
310 output.push_back(std::string(line));
311 }
312 if (pclose(fp) != 0) {
313 PLOG(ERROR) << "Failed to pclose " << cmd;
314 return -errno;
315 }
316
317 return OK;
318}
319
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700320pid_t ForkExecvpAsync(const std::vector<std::string>& args) {
321 size_t argc = args.size();
322 char** argv = (char**) calloc(argc + 1, sizeof(char*));
323 for (size_t i = 0; i < argc; i++) {
324 argv[i] = (char*) args[i].c_str();
325 if (i == 0) {
326 LOG(VERBOSE) << args[i];
327 } else {
328 LOG(VERBOSE) << " " << args[i];
329 }
330 }
331
332 pid_t pid = fork();
333 if (pid == 0) {
334 close(STDIN_FILENO);
335 close(STDOUT_FILENO);
336 close(STDERR_FILENO);
337
338 if (execvp(argv[0], argv)) {
339 PLOG(ERROR) << "Failed to exec";
340 }
341
342 _exit(1);
343 }
344
345 if (pid == -1) {
346 PLOG(ERROR) << "Failed to exec";
347 }
348
349 free(argv);
350 return pid;
351}
352
Jeff Sharkey9c484982015-03-31 10:35:33 -0700353status_t ReadRandomBytes(size_t bytes, std::string& out) {
354 out.clear();
355
356 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
357 if (fd == -1) {
358 return -errno;
359 }
360
361 char buf[BUFSIZ];
362 size_t n;
363 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], std::min(sizeof(buf), bytes)))) > 0) {
364 out.append(buf, n);
365 bytes -= n;
366 }
Elliott Hughesa6231082015-05-15 18:34:24 -0700367 close(fd);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700368
369 if (bytes == 0) {
370 return OK;
371 } else {
372 return -EIO;
373 }
374}
375
376status_t HexToStr(const std::string& hex, std::string& str) {
377 str.clear();
378 bool even = true;
379 char cur = 0;
380 for (size_t i = 0; i < hex.size(); i++) {
381 int val = 0;
382 switch (hex[i]) {
383 case ' ': case '-': case ':': continue;
384 case 'f': case 'F': val = 15; break;
385 case 'e': case 'E': val = 14; break;
386 case 'd': case 'D': val = 13; break;
387 case 'c': case 'C': val = 12; break;
388 case 'b': case 'B': val = 11; break;
389 case 'a': case 'A': val = 10; break;
390 case '9': val = 9; break;
391 case '8': val = 8; break;
392 case '7': val = 7; break;
393 case '6': val = 6; break;
394 case '5': val = 5; break;
395 case '4': val = 4; break;
396 case '3': val = 3; break;
397 case '2': val = 2; break;
398 case '1': val = 1; break;
399 case '0': val = 0; break;
400 default: return -EINVAL;
401 }
402
403 if (even) {
404 cur = val << 4;
405 } else {
406 cur += val;
407 str.push_back(cur);
408 cur = 0;
409 }
410 even = !even;
411 }
412 return even ? OK : -EINVAL;
413}
414
415static const char* kLookup = "0123456789abcdef";
416
417status_t StrToHex(const std::string& str, std::string& hex) {
418 hex.clear();
419 for (size_t i = 0; i < str.size(); i++) {
Jeff Sharkeyef369752015-04-29 15:57:48 -0700420 hex.push_back(kLookup[(str[i] & 0xF0) >> 4]);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700421 hex.push_back(kLookup[str[i] & 0x0F]);
422 }
423 return OK;
424}
425
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700426status_t NormalizeHex(const std::string& in, std::string& out) {
427 std::string tmp;
428 if (HexToStr(in, tmp)) {
429 return -EINVAL;
430 }
431 return StrToHex(tmp, out);
432}
433
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700434uint64_t GetFreeBytes(const std::string& path) {
435 struct statvfs sb;
436 if (statvfs(path.c_str(), &sb) == 0) {
Jeff Sharkeya0220a52017-04-03 17:11:45 -0600437 return (uint64_t) sb.f_bavail * sb.f_frsize;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700438 } else {
439 return -1;
440 }
441}
442
443// TODO: borrowed from frameworks/native/libs/diskusage/ which should
444// eventually be migrated into system/
445static int64_t stat_size(struct stat *s) {
446 int64_t blksize = s->st_blksize;
447 // count actual blocks used instead of nominal file size
448 int64_t size = s->st_blocks * 512;
449
450 if (blksize) {
451 /* round up to filesystem block size */
452 size = (size + blksize - 1) & (~(blksize - 1));
453 }
454
455 return size;
456}
457
458// TODO: borrowed from frameworks/native/libs/diskusage/ which should
459// eventually be migrated into system/
460int64_t calculate_dir_size(int dfd) {
461 int64_t size = 0;
462 struct stat s;
463 DIR *d;
464 struct dirent *de;
465
466 d = fdopendir(dfd);
467 if (d == NULL) {
468 close(dfd);
469 return 0;
470 }
471
472 while ((de = readdir(d))) {
473 const char *name = de->d_name;
474 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
475 size += stat_size(&s);
476 }
477 if (de->d_type == DT_DIR) {
478 int subfd;
479
480 /* always skip "." and ".." */
481 if (name[0] == '.') {
482 if (name[1] == 0)
483 continue;
484 if ((name[1] == '.') && (name[2] == 0))
485 continue;
486 }
487
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600488 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700489 if (subfd >= 0) {
490 size += calculate_dir_size(subfd);
491 }
492 }
493 }
494 closedir(d);
495 return size;
496}
497
498uint64_t GetTreeBytes(const std::string& path) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600499 int dirfd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700500 if (dirfd < 0) {
501 PLOG(WARNING) << "Failed to open " << path;
502 return -1;
503 } else {
504 uint64_t res = calculate_dir_size(dirfd);
505 close(dirfd);
506 return res;
507 }
508}
509
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700510bool IsFilesystemSupported(const std::string& fsType) {
511 std::string supported;
512 if (!ReadFileToString(kProcFilesystems, &supported)) {
513 PLOG(ERROR) << "Failed to read supported filesystems";
514 return false;
515 }
516 return supported.find(fsType + "\n") != std::string::npos;
517}
518
519status_t WipeBlockDevice(const std::string& path) {
520 status_t res = -1;
521 const char* c_path = path.c_str();
522 unsigned long nr_sec = 0;
523 unsigned long long range[2];
524
525 int fd = TEMP_FAILURE_RETRY(open(c_path, O_RDWR | O_CLOEXEC));
526 if (fd == -1) {
527 PLOG(ERROR) << "Failed to open " << path;
528 goto done;
529 }
530
caozhiyuan9102b0b2015-10-29 16:39:00 +0800531 if ((ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700532 PLOG(ERROR) << "Failed to determine size of " << path;
533 goto done;
534 }
535
536 range[0] = 0;
537 range[1] = (unsigned long long) nr_sec * 512;
538
539 LOG(INFO) << "About to discard " << range[1] << " on " << path;
540 if (ioctl(fd, BLKDISCARD, &range) == 0) {
541 LOG(INFO) << "Discard success on " << path;
542 res = 0;
543 } else {
544 PLOG(ERROR) << "Discard failure on " << path;
545 }
546
547done:
548 close(fd);
549 return res;
550}
551
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800552static bool isValidFilename(const std::string& name) {
553 if (name.empty() || (name == ".") || (name == "..")
554 || (name.find('/') != std::string::npos)) {
555 return false;
556 } else {
557 return true;
558 }
559}
560
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700561std::string BuildKeyPath(const std::string& partGuid) {
562 return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str());
563}
564
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600565std::string BuildDataSystemLegacyPath(userid_t userId) {
566 return StringPrintf("%s/system/users/%u", BuildDataPath(nullptr).c_str(), userId);
567}
568
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800569std::string BuildDataSystemCePath(userid_t userId) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700570 return StringPrintf("%s/system_ce/%u", BuildDataPath(nullptr).c_str(), userId);
571}
572
573std::string BuildDataSystemDePath(userid_t userId) {
574 return StringPrintf("%s/system_de/%u", BuildDataPath(nullptr).c_str(), userId);
575}
576
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600577std::string BuildDataMiscLegacyPath(userid_t userId) {
578 return StringPrintf("%s/misc/user/%u", BuildDataPath(nullptr).c_str(), userId);
579}
580
Jeff Sharkey47695b22016-02-01 17:02:29 -0700581std::string BuildDataMiscCePath(userid_t userId) {
582 return StringPrintf("%s/misc_ce/%u", BuildDataPath(nullptr).c_str(), userId);
583}
584
585std::string BuildDataMiscDePath(userid_t userId) {
586 return StringPrintf("%s/misc_de/%u", BuildDataPath(nullptr).c_str(), userId);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800587}
588
Calin Juravle79f55a42016-02-17 20:14:46 +0000589// Keep in sync with installd (frameworks/native/cmds/installd/utils.h)
590std::string BuildDataProfilesDePath(userid_t userId) {
591 return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath(nullptr).c_str(), userId);
592}
593
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800594std::string BuildDataPath(const char* volumeUuid) {
595 // TODO: unify with installd path generation logic
596 if (volumeUuid == nullptr) {
597 return "/data";
598 } else {
599 CHECK(isValidFilename(volumeUuid));
600 return StringPrintf("/mnt/expand/%s", volumeUuid);
601 }
602}
603
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600604std::string BuildDataMediaCePath(const char* volumeUuid, userid_t userId) {
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700605 // TODO: unify with installd path generation logic
606 std::string data(BuildDataPath(volumeUuid));
607 return StringPrintf("%s/media/%u", data.c_str(), userId);
608}
609
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600610std::string BuildDataUserCePath(const char* volumeUuid, userid_t userId) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800611 // TODO: unify with installd path generation logic
612 std::string data(BuildDataPath(volumeUuid));
cjbaoeb501142017-04-12 00:09:00 +0800613 if (volumeUuid == nullptr && userId == 0) {
614 std::string legacy = StringPrintf("%s/data", data.c_str());
615 struct stat sb;
616 if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
617 /* /data/data is dir, return /data/data for legacy system */
618 return legacy;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800619 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800620 }
cjbaoeb501142017-04-12 00:09:00 +0800621 return StringPrintf("%s/user/%u", data.c_str(), userId);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800622}
623
624std::string BuildDataUserDePath(const char* volumeUuid, userid_t userId) {
625 // TODO: unify with installd path generation logic
626 std::string data(BuildDataPath(volumeUuid));
627 return StringPrintf("%s/user_de/%u", data.c_str(), userId);
628}
629
Jeff Sharkey66270a22015-06-24 11:49:24 -0700630dev_t GetDevice(const std::string& path) {
631 struct stat sb;
632 if (stat(path.c_str(), &sb)) {
633 PLOG(WARNING) << "Failed to stat " << path;
634 return 0;
635 } else {
636 return sb.st_dev;
637 }
638}
639
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600640status_t RestoreconRecursive(const std::string& path) {
641 LOG(VERBOSE) << "Starting restorecon of " << path;
642
643 // TODO: find a cleaner way of waiting for restorecon to finish
644 const char* cpath = path.c_str();
645 property_set("selinux.restorecon_recursive", "");
646 property_set("selinux.restorecon_recursive", cpath);
647
648 char value[PROPERTY_VALUE_MAX];
649 while (true) {
650 property_get("selinux.restorecon_recursive", value, "");
651 if (strcmp(cpath, value) == 0) {
652 break;
653 }
654 usleep(100000); // 100ms
655 }
656
657 LOG(VERBOSE) << "Finished restorecon of " << path;
658 return OK;
659}
660
Daichi Hirono10d34882016-01-29 14:33:51 +0900661status_t SaneReadLinkAt(int dirfd, const char* path, char* buf, size_t bufsiz) {
662 ssize_t len = readlinkat(dirfd, path, buf, bufsiz);
663 if (len < 0) {
664 return -1;
665 } else if (len == (ssize_t) bufsiz) {
666 return -1;
667 } else {
668 buf[len] = '\0';
669 return 0;
670 }
671}
672
Yu Ning942d4e82016-01-08 17:36:47 +0800673bool IsRunningInEmulator() {
674 return property_get_bool("ro.kernel.qemu", 0);
675}
676
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800677} // namespace vold
678} // namespace android