blob: 158a61eb80c042951c3bb117faeff454d39ce403 [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
Jeff Sharkey36801cc2015-03-13 16:09:20 -070021#include <base/logging.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070022#include <base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023#include <cutils/fs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024#include <private/android_filesystem_config.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070025#include <logwrap/logwrap.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080026
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070027#include <mutex>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070028#include <dirent.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029#include <fcntl.h>
30#include <linux/fs.h>
31#include <stdlib.h>
32#include <sys/mount.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/wait.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070036#include <sys/statvfs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080037
38#ifndef UMOUNT_NOFOLLOW
39#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
40#endif
41
Jeff Sharkey9c484982015-03-31 10:35:33 -070042using android::base::StringPrintf;
43
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044namespace android {
45namespace vold {
46
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070047security_context_t sBlkidContext = nullptr;
48security_context_t sBlkidUntrustedContext = nullptr;
49security_context_t sFsckContext = nullptr;
50security_context_t sFsckUntrustedContext = nullptr;
51
Jeff Sharkey9c484982015-03-31 10:35:33 -070052static const char* kBlkidPath = "/system/bin/blkid";
53
Jeff Sharkeydeb24052015-03-02 21:01:40 -080054status_t CreateDeviceNode(const std::string& path, dev_t dev) {
55 const char* cpath = path.c_str();
56 status_t res = 0;
57
58 char* secontext = nullptr;
59 if (sehandle) {
60 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) {
61 setfscreatecon(secontext);
62 }
63 }
64
65 mode_t mode = 0660 | S_IFBLK;
66 if (mknod(cpath, mode, dev) < 0) {
67 if (errno != EEXIST) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070068 PLOG(ERROR) << "Failed to create device node for " << major(dev)
69 << ":" << minor(dev) << " at " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080070 res = -errno;
71 }
72 }
73
74 if (secontext) {
75 setfscreatecon(nullptr);
76 freecon(secontext);
77 }
78
79 return res;
80}
81
82status_t DestroyDeviceNode(const std::string& path) {
83 const char* cpath = path.c_str();
84 if (TEMP_FAILURE_RETRY(unlink(cpath))) {
85 return -errno;
86 } else {
87 return OK;
88 }
89}
90
Jeff Sharkeyf0121c52015-04-06 14:08:45 -070091status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
92 const char* cpath = path.c_str();
93
94 char* secontext = nullptr;
95 if (sehandle) {
96 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFDIR)) {
97 setfscreatecon(secontext);
98 }
99 }
100
101 int res = fs_prepare_dir(cpath, mode, uid, gid);
102
103 if (secontext) {
104 setfscreatecon(nullptr);
105 freecon(secontext);
106 }
107
108 if (res == 0) {
109 return OK;
110 } else {
111 return -errno;
112 }
113}
114
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800115status_t ForceUnmount(const std::string& path) {
116 const char* cpath = path.c_str();
117 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
118 return OK;
119 }
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700120 PLOG(WARNING) << "Failed to unmount " << path;
121
122 sleep(5);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700123 Process::killProcessesWithOpenFiles(cpath, SIGINT);
124
125 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
126 return OK;
127 }
128 PLOG(WARNING) << "Failed to unmount " << path;
129
130 sleep(5);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800131 Process::killProcessesWithOpenFiles(cpath, SIGTERM);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800132
133 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
134 return OK;
135 }
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700136 PLOG(WARNING) << "Failed to unmount " << path;
137
138 sleep(5);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800139 Process::killProcessesWithOpenFiles(cpath, SIGKILL);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800140
141 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
142 return OK;
143 }
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700144 PLOG(ERROR) << "Failed to unmount " << path;
145
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800146 return -errno;
147}
148
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700149status_t BindMount(const std::string& source, const std::string& target) {
150 if (::mount(source.c_str(), target.c_str(), "", MS_BIND, NULL)) {
151 PLOG(ERROR) << "Failed to bind mount " << source << " to " << target;
152 return -errno;
153 }
154 return OK;
155}
156
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700157static status_t readMetadata(const std::string& path, std::string& fsType,
158 std::string& fsUuid, std::string& fsLabel, bool untrusted) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700159 fsType.clear();
160 fsUuid.clear();
161 fsLabel.clear();
162
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700163 std::vector<std::string> cmd;
164 cmd.push_back(kBlkidPath);
165 cmd.push_back("-c");
166 cmd.push_back("/dev/null");
167 cmd.push_back(path);
168
169 std::vector<std::string> output;
170 status_t res = ForkExecvp(cmd, output, untrusted ? sBlkidUntrustedContext : sBlkidContext);
171 if (res != OK) {
172 LOG(WARNING) << "blkid failed to identify " << path;
173 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700174 }
175
Jeff Sharkey9c484982015-03-31 10:35:33 -0700176 char value[128];
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700177 for (auto line : output) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700178 // Extract values from blkid output, if defined
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700179 const char* cline = line.c_str();
180 char* start = strstr(cline, "TYPE=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700181 if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
182 fsType = value;
183 }
184
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700185 start = strstr(cline, "UUID=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700186 if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
187 fsUuid = value;
188 }
189
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700190 start = strstr(cline, "LABEL=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700191 if (start != nullptr && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) {
192 fsLabel = value;
193 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700194 }
195
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700196 return OK;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700197}
198
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700199status_t ReadMetadata(const std::string& path, std::string& fsType,
200 std::string& fsUuid, std::string& fsLabel) {
201 return readMetadata(path, fsType, fsUuid, fsLabel, false);
202}
203
204status_t ReadMetadataUntrusted(const std::string& path, std::string& fsType,
205 std::string& fsUuid, std::string& fsLabel) {
206 return readMetadata(path, fsType, fsUuid, fsLabel, true);
207}
208
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700209status_t ForkExecvp(const std::vector<std::string>& args) {
210 return ForkExecvp(args, nullptr);
211}
212
213status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context) {
214 size_t argc = args.size();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700215 char** argv = (char**) calloc(argc, sizeof(char*));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700216 for (size_t i = 0; i < argc; i++) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700217 argv[i] = (char*) args[i].c_str();
218 if (i == 0) {
219 LOG(VERBOSE) << args[i];
220 } else {
221 LOG(VERBOSE) << " " << args[i];
222 }
223 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700224
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700225 if (setexeccon(context)) {
226 LOG(ERROR) << "Failed to setexeccon";
227 abort();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700228 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700229 status_t res = android_fork_execvp(argc, argv, NULL, false, true);
230 if (setexeccon(nullptr)) {
231 LOG(ERROR) << "Failed to setexeccon";
232 abort();
233 }
234
Jeff Sharkey9c484982015-03-31 10:35:33 -0700235 free(argv);
236 return res;
237}
238
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700239status_t ForkExecvp(const std::vector<std::string>& args,
240 std::vector<std::string>& output) {
241 return ForkExecvp(args, output, nullptr);
242}
243
244status_t ForkExecvp(const std::vector<std::string>& args,
245 std::vector<std::string>& output, security_context_t context) {
246 std::string cmd;
247 for (size_t i = 0; i < args.size(); i++) {
248 cmd += args[i] + " ";
249 if (i == 0) {
250 LOG(VERBOSE) << args[i];
251 } else {
252 LOG(VERBOSE) << " " << args[i];
253 }
254 }
255 output.clear();
256
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700257 if (setexeccon(context)) {
258 LOG(ERROR) << "Failed to setexeccon";
259 abort();
260 }
261 FILE* fp = popen(cmd.c_str(), "r");
262 if (setexeccon(nullptr)) {
263 LOG(ERROR) << "Failed to setexeccon";
264 abort();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700265 }
266
267 if (!fp) {
268 PLOG(ERROR) << "Failed to popen " << cmd;
269 return -errno;
270 }
271 char line[1024];
272 while (fgets(line, sizeof(line), fp) != nullptr) {
273 LOG(VERBOSE) << line;
274 output.push_back(std::string(line));
275 }
276 if (pclose(fp) != 0) {
277 PLOG(ERROR) << "Failed to pclose " << cmd;
278 return -errno;
279 }
280
281 return OK;
282}
283
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700284pid_t ForkExecvpAsync(const std::vector<std::string>& args) {
285 size_t argc = args.size();
286 char** argv = (char**) calloc(argc + 1, sizeof(char*));
287 for (size_t i = 0; i < argc; i++) {
288 argv[i] = (char*) args[i].c_str();
289 if (i == 0) {
290 LOG(VERBOSE) << args[i];
291 } else {
292 LOG(VERBOSE) << " " << args[i];
293 }
294 }
295
296 pid_t pid = fork();
297 if (pid == 0) {
298 close(STDIN_FILENO);
299 close(STDOUT_FILENO);
300 close(STDERR_FILENO);
301
302 if (execvp(argv[0], argv)) {
303 PLOG(ERROR) << "Failed to exec";
304 }
305
306 _exit(1);
307 }
308
309 if (pid == -1) {
310 PLOG(ERROR) << "Failed to exec";
311 }
312
313 free(argv);
314 return pid;
315}
316
Jeff Sharkey9c484982015-03-31 10:35:33 -0700317status_t ReadRandomBytes(size_t bytes, std::string& out) {
318 out.clear();
319
320 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
321 if (fd == -1) {
322 return -errno;
323 }
324
325 char buf[BUFSIZ];
326 size_t n;
327 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], std::min(sizeof(buf), bytes)))) > 0) {
328 out.append(buf, n);
329 bytes -= n;
330 }
331 TEMP_FAILURE_RETRY(close(fd));
332
333 if (bytes == 0) {
334 return OK;
335 } else {
336 return -EIO;
337 }
338}
339
340status_t HexToStr(const std::string& hex, std::string& str) {
341 str.clear();
342 bool even = true;
343 char cur = 0;
344 for (size_t i = 0; i < hex.size(); i++) {
345 int val = 0;
346 switch (hex[i]) {
347 case ' ': case '-': case ':': continue;
348 case 'f': case 'F': val = 15; break;
349 case 'e': case 'E': val = 14; break;
350 case 'd': case 'D': val = 13; break;
351 case 'c': case 'C': val = 12; break;
352 case 'b': case 'B': val = 11; break;
353 case 'a': case 'A': val = 10; break;
354 case '9': val = 9; break;
355 case '8': val = 8; break;
356 case '7': val = 7; break;
357 case '6': val = 6; break;
358 case '5': val = 5; break;
359 case '4': val = 4; break;
360 case '3': val = 3; break;
361 case '2': val = 2; break;
362 case '1': val = 1; break;
363 case '0': val = 0; break;
364 default: return -EINVAL;
365 }
366
367 if (even) {
368 cur = val << 4;
369 } else {
370 cur += val;
371 str.push_back(cur);
372 cur = 0;
373 }
374 even = !even;
375 }
376 return even ? OK : -EINVAL;
377}
378
379static const char* kLookup = "0123456789abcdef";
380
381status_t StrToHex(const std::string& str, std::string& hex) {
382 hex.clear();
383 for (size_t i = 0; i < str.size(); i++) {
384 hex.push_back(kLookup[str[i] >> 4]);
385 hex.push_back(kLookup[str[i] & 0x0F]);
386 }
387 return OK;
388}
389
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700390uint64_t GetFreeBytes(const std::string& path) {
391 struct statvfs sb;
392 if (statvfs(path.c_str(), &sb) == 0) {
393 return sb.f_bfree * sb.f_bsize;
394 } else {
395 return -1;
396 }
397}
398
399// TODO: borrowed from frameworks/native/libs/diskusage/ which should
400// eventually be migrated into system/
401static int64_t stat_size(struct stat *s) {
402 int64_t blksize = s->st_blksize;
403 // count actual blocks used instead of nominal file size
404 int64_t size = s->st_blocks * 512;
405
406 if (blksize) {
407 /* round up to filesystem block size */
408 size = (size + blksize - 1) & (~(blksize - 1));
409 }
410
411 return size;
412}
413
414// TODO: borrowed from frameworks/native/libs/diskusage/ which should
415// eventually be migrated into system/
416int64_t calculate_dir_size(int dfd) {
417 int64_t size = 0;
418 struct stat s;
419 DIR *d;
420 struct dirent *de;
421
422 d = fdopendir(dfd);
423 if (d == NULL) {
424 close(dfd);
425 return 0;
426 }
427
428 while ((de = readdir(d))) {
429 const char *name = de->d_name;
430 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
431 size += stat_size(&s);
432 }
433 if (de->d_type == DT_DIR) {
434 int subfd;
435
436 /* always skip "." and ".." */
437 if (name[0] == '.') {
438 if (name[1] == 0)
439 continue;
440 if ((name[1] == '.') && (name[2] == 0))
441 continue;
442 }
443
444 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
445 if (subfd >= 0) {
446 size += calculate_dir_size(subfd);
447 }
448 }
449 }
450 closedir(d);
451 return size;
452}
453
454uint64_t GetTreeBytes(const std::string& path) {
455 int dirfd = open(path.c_str(), O_DIRECTORY, O_RDONLY);
456 if (dirfd < 0) {
457 PLOG(WARNING) << "Failed to open " << path;
458 return -1;
459 } else {
460 uint64_t res = calculate_dir_size(dirfd);
461 close(dirfd);
462 return res;
463 }
464}
465
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800466} // namespace vold
467} // namespace android