blob: f0bf029b95e92920ac16f2ebb1790021fe1d23cf [file] [log] [blame]
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Utils.h"
18
19#include <android-base/file.h>
20#include <android-base/logging.h>
21#include <android-base/stringprintf.h>
22
23#include <fcntl.h>
24#include <linux/fs.h>
25#include <stdlib.h>
26#include <sys/mount.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <sys/wait.h>
30#include <sys/statvfs.h>
31
32#include <selinux/android.h>
33
34using android::base::ReadFileToString;
35using android::base::StringPrintf;
36
37namespace android {
38namespace vold {
39
40static const char* kKeyPath = "/data/misc/vold";
41
42status_t ForkExecvp(const std::vector<std::string>& args) {
43 return ForkExecvp(args, nullptr);
44}
45
46status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context) {
47 size_t argc = args.size();
48 char** argv = (char**) calloc(argc, sizeof(char*));
49 for (size_t i = 0; i < argc; i++) {
50 argv[i] = (char*) args[i].c_str();
51 if (i == 0) {
52 LOG(VERBOSE) << args[i];
53 } else {
54 LOG(VERBOSE) << " " << args[i];
55 }
56 }
57
58 if (setexeccon(context)) {
59 LOG(ERROR) << "Failed to setexeccon";
60 abort();
61 }
62 abort();
63 status_t res = 1;//android_fork_execvp(argc, argv, NULL, false, true);
64 if (setexeccon(nullptr)) {
65 LOG(ERROR) << "Failed to setexeccon";
66 abort();
67 }
68
69 free(argv);
70 return res;
71}
72
73status_t ForkExecvp(const std::vector<std::string>& args,
74 std::vector<std::string>& output) {
75 return ForkExecvp(args, output, nullptr);
76}
77
78status_t ForkExecvp(const std::vector<std::string>& args,
79 std::vector<std::string>& output, security_context_t context) {
80 std::string cmd;
81 for (size_t i = 0; i < args.size(); i++) {
82 cmd += args[i] + " ";
83 if (i == 0) {
84 LOG(VERBOSE) << args[i];
85 } else {
86 LOG(VERBOSE) << " " << args[i];
87 }
88 }
89 output.clear();
90
91 if (setexeccon(context)) {
92 LOG(ERROR) << "Failed to setexeccon";
93 abort();
94 }
95 FILE* fp = popen(cmd.c_str(), "r");
96 if (setexeccon(nullptr)) {
97 LOG(ERROR) << "Failed to setexeccon";
98 abort();
99 }
100
101 if (!fp) {
102 PLOG(ERROR) << "Failed to popen " << cmd;
103 return -errno;
104 }
105 char line[1024];
106 while (fgets(line, sizeof(line), fp) != nullptr) {
107 LOG(VERBOSE) << line;
108 output.push_back(std::string(line));
109 }
110 if (pclose(fp) != 0) {
111 PLOG(ERROR) << "Failed to pclose " << cmd;
112 return -errno;
113 }
114
115 return OK;
116}
117
118pid_t ForkExecvpAsync(const std::vector<std::string>& args) {
119 size_t argc = args.size();
120 char** argv = (char**) calloc(argc + 1, sizeof(char*));
121 for (size_t i = 0; i < argc; i++) {
122 argv[i] = (char*) args[i].c_str();
123 if (i == 0) {
124 LOG(VERBOSE) << args[i];
125 } else {
126 LOG(VERBOSE) << " " << args[i];
127 }
128 }
129
130 pid_t pid = fork();
131 if (pid == 0) {
132 close(STDIN_FILENO);
133 close(STDOUT_FILENO);
134 close(STDERR_FILENO);
135
136 if (execvp(argv[0], argv)) {
137 PLOG(ERROR) << "Failed to exec";
138 }
139
140 _exit(1);
141 }
142
143 if (pid == -1) {
144 PLOG(ERROR) << "Failed to exec";
145 }
146
147 free(argv);
148 return pid;
149}
150
151status_t ReadRandomBytes(size_t bytes, std::string& out) {
152 out.clear();
153
154 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
155 if (fd == -1) {
156 return -errno;
157 }
158
159 char buf[BUFSIZ];
160 size_t n;
161 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], std::min(sizeof(buf), bytes)))) > 0) {
162 out.append(buf, n);
163 bytes -= n;
164 }
165 close(fd);
166
167 if (bytes == 0) {
168 return OK;
169 } else {
170 return -EIO;
171 }
172}
173
174status_t HexToStr(const std::string& hex, std::string& str) {
175 str.clear();
176 bool even = true;
177 char cur = 0;
178 for (size_t i = 0; i < hex.size(); i++) {
179 int val = 0;
180 switch (hex[i]) {
181 case ' ': case '-': case ':': continue;
182 case 'f': case 'F': val = 15; break;
183 case 'e': case 'E': val = 14; break;
184 case 'd': case 'D': val = 13; break;
185 case 'c': case 'C': val = 12; break;
186 case 'b': case 'B': val = 11; break;
187 case 'a': case 'A': val = 10; break;
188 case '9': val = 9; break;
189 case '8': val = 8; break;
190 case '7': val = 7; break;
191 case '6': val = 6; break;
192 case '5': val = 5; break;
193 case '4': val = 4; break;
194 case '3': val = 3; break;
195 case '2': val = 2; break;
196 case '1': val = 1; break;
197 case '0': val = 0; break;
198 default: return -EINVAL;
199 }
200
201 if (even) {
202 cur = val << 4;
203 } else {
204 cur += val;
205 str.push_back(cur);
206 cur = 0;
207 }
208 even = !even;
209 }
210 return even ? OK : -EINVAL;
211}
212
213static bool isValidFilename(const std::string& name) {
214 if (name.empty() || (name == ".") || (name == "..")
215 || (name.find('/') != std::string::npos)) {
216 return false;
217 } else {
218 return true;
219 }
220}
221
222std::string BuildKeyPath(const std::string& partGuid) {
223 return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str());
224}
225
226std::string BuildDataSystemLegacyPath(userid_t userId) {
227 return StringPrintf("%s/system/users/%u", BuildDataPath(nullptr).c_str(), userId);
228}
229
230std::string BuildDataSystemCePath(userid_t userId) {
231 return StringPrintf("%s/system_ce/%u", BuildDataPath(nullptr).c_str(), userId);
232}
233
234std::string BuildDataSystemDePath(userid_t userId) {
235 return StringPrintf("%s/system_de/%u", BuildDataPath(nullptr).c_str(), userId);
236}
237
238std::string BuildDataMiscLegacyPath(userid_t userId) {
239 return StringPrintf("%s/misc/user/%u", BuildDataPath(nullptr).c_str(), userId);
240}
241
242std::string BuildDataMiscCePath(userid_t userId) {
243 return StringPrintf("%s/misc_ce/%u", BuildDataPath(nullptr).c_str(), userId);
244}
245
246std::string BuildDataMiscDePath(userid_t userId) {
247 return StringPrintf("%s/misc_de/%u", BuildDataPath(nullptr).c_str(), userId);
248}
249
250// Keep in sync with installd (frameworks/native/cmds/installd/utils.h)
251std::string BuildDataProfilesDePath(userid_t userId) {
252 return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath(nullptr).c_str(), userId);
253}
254
255std::string BuildDataProfilesForeignDexDePath(userid_t userId) {
256 std::string profiles_path = BuildDataProfilesDePath(userId);
257 return StringPrintf("%s/foreign-dex", profiles_path.c_str());
258}
259
260std::string BuildDataPath(const char* volumeUuid) {
261 // TODO: unify with installd path generation logic
262 if (volumeUuid == nullptr) {
263 return "/data";
264 } else {
265 CHECK(isValidFilename(volumeUuid));
266 return StringPrintf("/mnt/expand/%s", volumeUuid);
267 }
268}
269
270std::string BuildDataMediaCePath(const char* volumeUuid, userid_t userId) {
271 // TODO: unify with installd path generation logic
272 std::string data(BuildDataPath(volumeUuid));
273 return StringPrintf("%s/media/%u", data.c_str(), userId);
274}
275
276std::string BuildDataUserCePath(const char* volumeUuid, userid_t userId) {
277 // TODO: unify with installd path generation logic
278 std::string data(BuildDataPath(volumeUuid));
279 if (volumeUuid == nullptr) {
280 if (userId == 0) {
281 return StringPrintf("%s/data", data.c_str());
282 } else {
283 return StringPrintf("%s/user/%u", data.c_str(), userId);
284 }
285 } else {
286 return StringPrintf("%s/user/%u", data.c_str(), userId);
287 }
288}
289
290std::string BuildDataUserDePath(const char* volumeUuid, userid_t userId) {
291 // TODO: unify with installd path generation logic
292 std::string data(BuildDataPath(volumeUuid));
293 return StringPrintf("%s/user_de/%u", data.c_str(), userId);
294}
295
296} // namespace vold
297} // namespace android