blob: 9699777e4f9d46aab7650623c1ad49d6d0e70988 [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"
20
Elliott Hughes7e128fb2015-12-04 15:50:53 -080021#include <android-base/file.h>
22#include <android-base/logging.h>
Tom Cherryd6127ef2017-06-15 17:13:56 -070023#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080024#include <android-base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025#include <cutils/fs.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070026#include <logwrap/logwrap.h>
Tom Cherryd6127ef2017-06-15 17:13:56 -070027#include <private/android_filesystem_config.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070029#include <mutex>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070030#include <dirent.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031#include <fcntl.h>
32#include <linux/fs.h>
33#include <stdlib.h>
34#include <sys/mount.h>
35#include <sys/types.h>
36#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070037#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080038#include <sys/wait.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070039#include <sys/statvfs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040
41#ifndef UMOUNT_NOFOLLOW
42#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
43#endif
44
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070045using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070046using android::base::StringPrintf;
47
Jeff Sharkeydeb24052015-03-02 21:01:40 -080048namespace android {
49namespace vold {
50
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070051security_context_t sBlkidContext = nullptr;
52security_context_t sBlkidUntrustedContext = nullptr;
53security_context_t sFsckContext = nullptr;
54security_context_t sFsckUntrustedContext = nullptr;
55
Jeff Sharkey9c484982015-03-31 10:35:33 -070056static const char* kBlkidPath = "/system/bin/blkid";
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070057static const char* kKeyPath = "/data/misc/vold";
Jeff Sharkey9c484982015-03-31 10:35:33 -070058
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070059static const char* kProcFilesystems = "/proc/filesystems";
60
Jeff Sharkeydeb24052015-03-02 21:01:40 -080061status_t CreateDeviceNode(const std::string& path, dev_t dev) {
62 const char* cpath = path.c_str();
63 status_t res = 0;
64
65 char* secontext = nullptr;
66 if (sehandle) {
67 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) {
68 setfscreatecon(secontext);
69 }
70 }
71
72 mode_t mode = 0660 | S_IFBLK;
73 if (mknod(cpath, mode, dev) < 0) {
74 if (errno != EEXIST) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070075 PLOG(ERROR) << "Failed to create device node for " << major(dev)
76 << ":" << minor(dev) << " at " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080077 res = -errno;
78 }
79 }
80
81 if (secontext) {
82 setfscreatecon(nullptr);
83 freecon(secontext);
84 }
85
86 return res;
87}
88
89status_t DestroyDeviceNode(const std::string& path) {
90 const char* cpath = path.c_str();
91 if (TEMP_FAILURE_RETRY(unlink(cpath))) {
92 return -errno;
93 } else {
94 return OK;
95 }
96}
97
Jeff Sharkeyf0121c52015-04-06 14:08:45 -070098status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
99 const char* cpath = path.c_str();
100
101 char* secontext = nullptr;
102 if (sehandle) {
103 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFDIR)) {
104 setfscreatecon(secontext);
105 }
106 }
107
108 int res = fs_prepare_dir(cpath, mode, uid, gid);
109
110 if (secontext) {
111 setfscreatecon(nullptr);
112 freecon(secontext);
113 }
114
115 if (res == 0) {
116 return OK;
117 } else {
118 return -errno;
119 }
120}
121
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800122status_t ForceUnmount(const std::string& path) {
123 const char* cpath = path.c_str();
124 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
125 return OK;
126 }
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700127 // Apps might still be handling eject request, so wait before
128 // we start sending signals
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700129 sleep(5);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700130
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700131 Process::killProcessesWithOpenFiles(cpath, SIGINT);
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700132 sleep(5);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700133 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
134 return OK;
135 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700136
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800137 Process::killProcessesWithOpenFiles(cpath, SIGTERM);
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700138 sleep(5);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800139 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
140 return OK;
141 }
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700142
143 Process::killProcessesWithOpenFiles(cpath, SIGKILL);
144 sleep(5);
145 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
146 return OK;
147 }
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700148
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800149 return -errno;
150}
151
Jeff Sharkey89f74fb2015-10-21 12:16:12 -0700152status_t KillProcessesUsingPath(const std::string& path) {
153 const char* cpath = path.c_str();
154 if (Process::killProcessesWithOpenFiles(cpath, SIGINT) == 0) {
155 return OK;
156 }
157 sleep(5);
158
159 if (Process::killProcessesWithOpenFiles(cpath, SIGTERM) == 0) {
160 return OK;
161 }
162 sleep(5);
163
164 if (Process::killProcessesWithOpenFiles(cpath, SIGKILL) == 0) {
165 return OK;
166 }
167 sleep(5);
168
169 // Send SIGKILL a second time to determine if we've
170 // actually killed everyone with open files
171 if (Process::killProcessesWithOpenFiles(cpath, SIGKILL) == 0) {
172 return OK;
173 }
174 PLOG(ERROR) << "Failed to kill processes using " << path;
175 return -EBUSY;
176}
177
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700178status_t BindMount(const std::string& source, const std::string& target) {
179 if (::mount(source.c_str(), target.c_str(), "", MS_BIND, NULL)) {
180 PLOG(ERROR) << "Failed to bind mount " << source << " to " << target;
181 return -errno;
182 }
183 return OK;
184}
185
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700186static status_t readMetadata(const std::string& path, std::string& fsType,
187 std::string& fsUuid, std::string& fsLabel, bool untrusted) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700188 fsType.clear();
189 fsUuid.clear();
190 fsLabel.clear();
191
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700192 std::vector<std::string> cmd;
193 cmd.push_back(kBlkidPath);
194 cmd.push_back("-c");
195 cmd.push_back("/dev/null");
Jeff Sharkeyeddf9bd2015-08-12 16:04:35 -0700196 cmd.push_back("-s");
197 cmd.push_back("TYPE");
198 cmd.push_back("-s");
199 cmd.push_back("UUID");
200 cmd.push_back("-s");
201 cmd.push_back("LABEL");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700202 cmd.push_back(path);
203
204 std::vector<std::string> output;
205 status_t res = ForkExecvp(cmd, output, untrusted ? sBlkidUntrustedContext : sBlkidContext);
206 if (res != OK) {
207 LOG(WARNING) << "blkid failed to identify " << path;
208 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700209 }
210
Jeff Sharkey9c484982015-03-31 10:35:33 -0700211 char value[128];
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700212 for (const auto& line : output) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700213 // Extract values from blkid output, if defined
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700214 const char* cline = line.c_str();
Dan Austin49ab5f92016-03-24 12:26:39 -0700215 const char* start = strstr(cline, "TYPE=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700216 if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
217 fsType = value;
218 }
219
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700220 start = strstr(cline, "UUID=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700221 if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
222 fsUuid = value;
223 }
224
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700225 start = strstr(cline, "LABEL=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700226 if (start != nullptr && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) {
227 fsLabel = value;
228 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700229 }
230
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700231 return OK;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700232}
233
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700234status_t ReadMetadata(const std::string& path, std::string& fsType,
235 std::string& fsUuid, std::string& fsLabel) {
236 return readMetadata(path, fsType, fsUuid, fsLabel, false);
237}
238
239status_t ReadMetadataUntrusted(const std::string& path, std::string& fsType,
240 std::string& fsUuid, std::string& fsLabel) {
241 return readMetadata(path, fsType, fsUuid, fsLabel, true);
242}
243
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700244status_t ForkExecvp(const std::vector<std::string>& args) {
245 return ForkExecvp(args, nullptr);
246}
247
248status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context) {
249 size_t argc = args.size();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700250 char** argv = (char**) calloc(argc, sizeof(char*));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700251 for (size_t i = 0; i < argc; i++) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700252 argv[i] = (char*) args[i].c_str();
253 if (i == 0) {
254 LOG(VERBOSE) << args[i];
255 } else {
256 LOG(VERBOSE) << " " << args[i];
257 }
258 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700259
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700260 if (setexeccon(context)) {
261 LOG(ERROR) << "Failed to setexeccon";
262 abort();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700263 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700264 status_t res = android_fork_execvp(argc, argv, NULL, false, true);
265 if (setexeccon(nullptr)) {
266 LOG(ERROR) << "Failed to setexeccon";
267 abort();
268 }
269
Jeff Sharkey9c484982015-03-31 10:35:33 -0700270 free(argv);
271 return res;
272}
273
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700274status_t ForkExecvp(const std::vector<std::string>& args,
275 std::vector<std::string>& output) {
276 return ForkExecvp(args, output, nullptr);
277}
278
279status_t ForkExecvp(const std::vector<std::string>& args,
280 std::vector<std::string>& output, security_context_t context) {
281 std::string cmd;
282 for (size_t i = 0; i < args.size(); i++) {
283 cmd += args[i] + " ";
284 if (i == 0) {
285 LOG(VERBOSE) << args[i];
286 } else {
287 LOG(VERBOSE) << " " << args[i];
288 }
289 }
290 output.clear();
291
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700292 if (setexeccon(context)) {
293 LOG(ERROR) << "Failed to setexeccon";
294 abort();
295 }
Jeff Sharkey32ebb732017-03-27 16:18:50 -0600296 FILE* fp = popen(cmd.c_str(), "r"); // NOLINT
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700297 if (setexeccon(nullptr)) {
298 LOG(ERROR) << "Failed to setexeccon";
299 abort();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700300 }
301
302 if (!fp) {
303 PLOG(ERROR) << "Failed to popen " << cmd;
304 return -errno;
305 }
306 char line[1024];
307 while (fgets(line, sizeof(line), fp) != nullptr) {
308 LOG(VERBOSE) << line;
309 output.push_back(std::string(line));
310 }
311 if (pclose(fp) != 0) {
312 PLOG(ERROR) << "Failed to pclose " << cmd;
313 return -errno;
314 }
315
316 return OK;
317}
318
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700319pid_t ForkExecvpAsync(const std::vector<std::string>& args) {
320 size_t argc = args.size();
321 char** argv = (char**) calloc(argc + 1, sizeof(char*));
322 for (size_t i = 0; i < argc; i++) {
323 argv[i] = (char*) args[i].c_str();
324 if (i == 0) {
325 LOG(VERBOSE) << args[i];
326 } else {
327 LOG(VERBOSE) << " " << args[i];
328 }
329 }
330
331 pid_t pid = fork();
332 if (pid == 0) {
333 close(STDIN_FILENO);
334 close(STDOUT_FILENO);
335 close(STDERR_FILENO);
336
337 if (execvp(argv[0], argv)) {
338 PLOG(ERROR) << "Failed to exec";
339 }
340
341 _exit(1);
342 }
343
344 if (pid == -1) {
345 PLOG(ERROR) << "Failed to exec";
346 }
347
348 free(argv);
349 return pid;
350}
351
Jeff Sharkey9c484982015-03-31 10:35:33 -0700352status_t ReadRandomBytes(size_t bytes, std::string& out) {
353 out.clear();
354
355 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
356 if (fd == -1) {
357 return -errno;
358 }
359
360 char buf[BUFSIZ];
361 size_t n;
362 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], std::min(sizeof(buf), bytes)))) > 0) {
363 out.append(buf, n);
364 bytes -= n;
365 }
Elliott Hughesa6231082015-05-15 18:34:24 -0700366 close(fd);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700367
368 if (bytes == 0) {
369 return OK;
370 } else {
371 return -EIO;
372 }
373}
374
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600375status_t GenerateRandomUuid(std::string& out) {
376 status_t res = ReadRandomBytes(16, out);
377 if (res == OK) {
378 out[6] &= 0x0f; /* clear version */
379 out[6] |= 0x40; /* set to version 4 */
380 out[8] &= 0x3f; /* clear variant */
381 out[8] |= 0x80; /* set to IETF variant */
382 }
383 return res;
384}
385
Jeff Sharkey9c484982015-03-31 10:35:33 -0700386status_t HexToStr(const std::string& hex, std::string& str) {
387 str.clear();
388 bool even = true;
389 char cur = 0;
390 for (size_t i = 0; i < hex.size(); i++) {
391 int val = 0;
392 switch (hex[i]) {
393 case ' ': case '-': case ':': continue;
394 case 'f': case 'F': val = 15; break;
395 case 'e': case 'E': val = 14; break;
396 case 'd': case 'D': val = 13; break;
397 case 'c': case 'C': val = 12; break;
398 case 'b': case 'B': val = 11; break;
399 case 'a': case 'A': val = 10; break;
400 case '9': val = 9; break;
401 case '8': val = 8; break;
402 case '7': val = 7; break;
403 case '6': val = 6; break;
404 case '5': val = 5; break;
405 case '4': val = 4; break;
406 case '3': val = 3; break;
407 case '2': val = 2; break;
408 case '1': val = 1; break;
409 case '0': val = 0; break;
410 default: return -EINVAL;
411 }
412
413 if (even) {
414 cur = val << 4;
415 } else {
416 cur += val;
417 str.push_back(cur);
418 cur = 0;
419 }
420 even = !even;
421 }
422 return even ? OK : -EINVAL;
423}
424
425static const char* kLookup = "0123456789abcdef";
426
427status_t StrToHex(const std::string& str, std::string& hex) {
428 hex.clear();
429 for (size_t i = 0; i < str.size(); i++) {
Jeff Sharkeyef369752015-04-29 15:57:48 -0700430 hex.push_back(kLookup[(str[i] & 0xF0) >> 4]);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700431 hex.push_back(kLookup[str[i] & 0x0F]);
432 }
433 return OK;
434}
435
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700436status_t NormalizeHex(const std::string& in, std::string& out) {
437 std::string tmp;
438 if (HexToStr(in, tmp)) {
439 return -EINVAL;
440 }
441 return StrToHex(tmp, out);
442}
443
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700444uint64_t GetFreeBytes(const std::string& path) {
445 struct statvfs sb;
446 if (statvfs(path.c_str(), &sb) == 0) {
Jeff Sharkeya0220a52017-04-03 17:11:45 -0600447 return (uint64_t) sb.f_bavail * sb.f_frsize;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700448 } else {
449 return -1;
450 }
451}
452
453// TODO: borrowed from frameworks/native/libs/diskusage/ which should
454// eventually be migrated into system/
455static int64_t stat_size(struct stat *s) {
456 int64_t blksize = s->st_blksize;
457 // count actual blocks used instead of nominal file size
458 int64_t size = s->st_blocks * 512;
459
460 if (blksize) {
461 /* round up to filesystem block size */
462 size = (size + blksize - 1) & (~(blksize - 1));
463 }
464
465 return size;
466}
467
468// TODO: borrowed from frameworks/native/libs/diskusage/ which should
469// eventually be migrated into system/
470int64_t calculate_dir_size(int dfd) {
471 int64_t size = 0;
472 struct stat s;
473 DIR *d;
474 struct dirent *de;
475
476 d = fdopendir(dfd);
477 if (d == NULL) {
478 close(dfd);
479 return 0;
480 }
481
482 while ((de = readdir(d))) {
483 const char *name = de->d_name;
484 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
485 size += stat_size(&s);
486 }
487 if (de->d_type == DT_DIR) {
488 int subfd;
489
490 /* always skip "." and ".." */
491 if (name[0] == '.') {
492 if (name[1] == 0)
493 continue;
494 if ((name[1] == '.') && (name[2] == 0))
495 continue;
496 }
497
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600498 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700499 if (subfd >= 0) {
500 size += calculate_dir_size(subfd);
501 }
502 }
503 }
504 closedir(d);
505 return size;
506}
507
508uint64_t GetTreeBytes(const std::string& path) {
Jeff Sharkeyfd3dc3c2017-03-27 10:49:21 -0600509 int dirfd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700510 if (dirfd < 0) {
511 PLOG(WARNING) << "Failed to open " << path;
512 return -1;
513 } else {
514 uint64_t res = calculate_dir_size(dirfd);
515 close(dirfd);
516 return res;
517 }
518}
519
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700520bool IsFilesystemSupported(const std::string& fsType) {
521 std::string supported;
522 if (!ReadFileToString(kProcFilesystems, &supported)) {
523 PLOG(ERROR) << "Failed to read supported filesystems";
524 return false;
525 }
526 return supported.find(fsType + "\n") != std::string::npos;
527}
528
529status_t WipeBlockDevice(const std::string& path) {
530 status_t res = -1;
531 const char* c_path = path.c_str();
532 unsigned long nr_sec = 0;
533 unsigned long long range[2];
534
535 int fd = TEMP_FAILURE_RETRY(open(c_path, O_RDWR | O_CLOEXEC));
536 if (fd == -1) {
537 PLOG(ERROR) << "Failed to open " << path;
538 goto done;
539 }
540
caozhiyuan9102b0b2015-10-29 16:39:00 +0800541 if ((ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700542 PLOG(ERROR) << "Failed to determine size of " << path;
543 goto done;
544 }
545
546 range[0] = 0;
547 range[1] = (unsigned long long) nr_sec * 512;
548
549 LOG(INFO) << "About to discard " << range[1] << " on " << path;
550 if (ioctl(fd, BLKDISCARD, &range) == 0) {
551 LOG(INFO) << "Discard success on " << path;
552 res = 0;
553 } else {
554 PLOG(ERROR) << "Discard failure on " << path;
555 }
556
557done:
558 close(fd);
559 return res;
560}
561
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800562static bool isValidFilename(const std::string& name) {
563 if (name.empty() || (name == ".") || (name == "..")
564 || (name.find('/') != std::string::npos)) {
565 return false;
566 } else {
567 return true;
568 }
569}
570
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700571std::string BuildKeyPath(const std::string& partGuid) {
572 return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str());
573}
574
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600575std::string BuildDataSystemLegacyPath(userid_t userId) {
576 return StringPrintf("%s/system/users/%u", BuildDataPath(nullptr).c_str(), userId);
577}
578
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800579std::string BuildDataSystemCePath(userid_t userId) {
Jeff Sharkey47695b22016-02-01 17:02:29 -0700580 return StringPrintf("%s/system_ce/%u", BuildDataPath(nullptr).c_str(), userId);
581}
582
583std::string BuildDataSystemDePath(userid_t userId) {
584 return StringPrintf("%s/system_de/%u", BuildDataPath(nullptr).c_str(), userId);
585}
586
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600587std::string BuildDataMiscLegacyPath(userid_t userId) {
588 return StringPrintf("%s/misc/user/%u", BuildDataPath(nullptr).c_str(), userId);
589}
590
Jeff Sharkey47695b22016-02-01 17:02:29 -0700591std::string BuildDataMiscCePath(userid_t userId) {
592 return StringPrintf("%s/misc_ce/%u", BuildDataPath(nullptr).c_str(), userId);
593}
594
595std::string BuildDataMiscDePath(userid_t userId) {
596 return StringPrintf("%s/misc_de/%u", BuildDataPath(nullptr).c_str(), userId);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800597}
598
Calin Juravle79f55a42016-02-17 20:14:46 +0000599// Keep in sync with installd (frameworks/native/cmds/installd/utils.h)
600std::string BuildDataProfilesDePath(userid_t userId) {
601 return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath(nullptr).c_str(), userId);
602}
603
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800604std::string BuildDataPath(const char* volumeUuid) {
605 // TODO: unify with installd path generation logic
606 if (volumeUuid == nullptr) {
607 return "/data";
608 } else {
609 CHECK(isValidFilename(volumeUuid));
610 return StringPrintf("/mnt/expand/%s", volumeUuid);
611 }
612}
613
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600614std::string BuildDataMediaCePath(const char* volumeUuid, userid_t userId) {
Jeff Sharkeyfc505c32015-12-07 17:27:01 -0700615 // TODO: unify with installd path generation logic
616 std::string data(BuildDataPath(volumeUuid));
617 return StringPrintf("%s/media/%u", data.c_str(), userId);
618}
619
Jeff Sharkeybe70c9a2016-04-14 20:45:16 -0600620std::string BuildDataUserCePath(const char* volumeUuid, userid_t userId) {
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800621 // TODO: unify with installd path generation logic
622 std::string data(BuildDataPath(volumeUuid));
cjbaoeb501142017-04-12 00:09:00 +0800623 if (volumeUuid == nullptr && userId == 0) {
624 std::string legacy = StringPrintf("%s/data", data.c_str());
625 struct stat sb;
626 if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
627 /* /data/data is dir, return /data/data for legacy system */
628 return legacy;
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800629 }
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800630 }
cjbaoeb501142017-04-12 00:09:00 +0800631 return StringPrintf("%s/user/%u", data.c_str(), userId);
Jeff Sharkeyd2c96e72015-11-08 17:56:23 -0800632}
633
634std::string BuildDataUserDePath(const char* volumeUuid, userid_t userId) {
635 // TODO: unify with installd path generation logic
636 std::string data(BuildDataPath(volumeUuid));
637 return StringPrintf("%s/user_de/%u", data.c_str(), userId);
638}
639
Jeff Sharkey66270a22015-06-24 11:49:24 -0700640dev_t GetDevice(const std::string& path) {
641 struct stat sb;
642 if (stat(path.c_str(), &sb)) {
643 PLOG(WARNING) << "Failed to stat " << path;
644 return 0;
645 } else {
646 return sb.st_dev;
647 }
648}
649
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600650status_t RestoreconRecursive(const std::string& path) {
651 LOG(VERBOSE) << "Starting restorecon of " << path;
652
Tom Cherryd6127ef2017-06-15 17:13:56 -0700653 static constexpr const char* kRestoreconString = "selinux.restorecon_recursive";
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600654
Tom Cherryd6127ef2017-06-15 17:13:56 -0700655 android::base::SetProperty(kRestoreconString, "");
656 android::base::SetProperty(kRestoreconString, path);
657
658 android::base::WaitForProperty(kRestoreconString, path);
Jeff Sharkeyd24aeda2016-07-15 16:20:22 -0600659
660 LOG(VERBOSE) << "Finished restorecon of " << path;
661 return OK;
662}
663
Daichi Hirono10d34882016-01-29 14:33:51 +0900664status_t SaneReadLinkAt(int dirfd, const char* path, char* buf, size_t bufsiz) {
665 ssize_t len = readlinkat(dirfd, path, buf, bufsiz);
666 if (len < 0) {
667 return -1;
668 } else if (len == (ssize_t) bufsiz) {
669 return -1;
670 } else {
671 buf[len] = '\0';
672 return 0;
673 }
674}
675
Yu Ning942d4e82016-01-08 17:36:47 +0800676bool IsRunningInEmulator() {
Tom Cherryd6127ef2017-06-15 17:13:56 -0700677 return android::base::GetBoolProperty("ro.kernel.qemu", false);
Yu Ning942d4e82016-01-08 17:36:47 +0800678}
679
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800680} // namespace vold
681} // namespace android