blob: e586caa681c283664bafe2d34de2e68b07ef4361 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
17#include "installd.h"
18
Jeff Sharkeyc03de092015-04-07 18:14:05 -070019#include <base/stringprintf.h>
20#include <base/logging.h>
21
Mike Lockwood94afecf2012-10-24 10:45:23 -070022#define CACHE_NOISY(x) //x
23
Jeff Sharkeyc03de092015-04-07 18:14:05 -070024using android::base::StringPrintf;
Mike Lockwood94afecf2012-10-24 10:45:23 -070025
Jeff Sharkeyc03de092015-04-07 18:14:05 -070026/**
27 * Check that given string is valid filename, and that it attempts no
28 * parent or child directory traversal.
29 */
30static bool is_valid_filename(const std::string& name) {
31 if (name.empty() || (name == ".") || (name == "..")
32 || (name.find('/') != std::string::npos)) {
33 return false;
34 } else {
35 return true;
36 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070037}
38
39/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -070040 * Create the path name where package app contents should be stored for
41 * the given volume UUID and package name. An empty UUID is assumed to
42 * be internal storage.
43 */
44std::string create_data_app_package_path(const char* volume_uuid,
45 const char* package_name) {
46 CHECK(is_valid_filename(package_name));
47 CHECK(is_valid_package_name(package_name) == 0);
48
49 return StringPrintf("%s/%s",
50 create_data_app_path(volume_uuid).c_str(), package_name);
51}
52
53/**
Jeff Sharkeyc03de092015-04-07 18:14:05 -070054 * Create the path name where package data should be stored for the given
55 * volume UUID, package name, and user ID. An empty UUID is assumed to be
56 * internal storage.
Mike Lockwood94afecf2012-10-24 10:45:23 -070057 */
Jeff Sharkeyd7921182015-04-30 15:58:19 -070058std::string create_data_user_package_path(const char* volume_uuid,
59 userid_t user, const char* package_name) {
Jeff Sharkeyc03de092015-04-07 18:14:05 -070060 CHECK(is_valid_filename(package_name));
61 CHECK(is_valid_package_name(package_name) == 0);
62
Jeff Sharkeyd7921182015-04-30 15:58:19 -070063 return StringPrintf("%s/%s",
64 create_data_user_path(volume_uuid, user).c_str(), package_name);
Jeff Sharkeyc03de092015-04-07 18:14:05 -070065}
Mike Lockwood94afecf2012-10-24 10:45:23 -070066
Jeff Sharkey63ec2d62015-11-09 13:10:36 -080067std::string create_data_user_de_package_path(const char* volume_uuid,
68 userid_t user, const char* package_name) {
69 CHECK(is_valid_filename(package_name));
70 CHECK(is_valid_package_name(package_name) == 0);
71
72 return StringPrintf("%s/%s",
73 create_data_user_de_path(volume_uuid, user).c_str(), package_name);
74}
75
Jeff Sharkeyc03de092015-04-07 18:14:05 -070076int create_pkg_path(char path[PKG_PATH_MAX], const char *pkgname,
77 const char *postfix, userid_t userid) {
78 if (is_valid_package_name(pkgname) != 0) {
79 path[0] = '\0';
Mike Lockwood94afecf2012-10-24 10:45:23 -070080 return -1;
81 }
82
Jeff Sharkeyd7921182015-04-30 15:58:19 -070083 std::string _tmp(create_data_user_package_path(nullptr, userid, pkgname) + postfix);
Jeff Sharkeyc03de092015-04-07 18:14:05 -070084 const char* tmp = _tmp.c_str();
85 if (strlen(tmp) >= PKG_PATH_MAX) {
86 path[0] = '\0';
87 return -1;
88 } else {
89 strcpy(path, tmp);
90 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -070091 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070092}
93
Jeff Sharkey41ea4242015-04-09 11:34:03 -070094std::string create_data_path(const char* volume_uuid) {
95 if (volume_uuid == nullptr) {
96 return "/data";
97 } else {
98 CHECK(is_valid_filename(volume_uuid));
99 return StringPrintf("/mnt/expand/%s", volume_uuid);
100 }
101}
102
Mike Lockwood94afecf2012-10-24 10:45:23 -0700103/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700104 * Create the path name for app data.
105 */
106std::string create_data_app_path(const char* volume_uuid) {
107 return StringPrintf("%s/app", create_data_path(volume_uuid).c_str());
108}
109
110/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700111 * Create the path name for user data for a certain userid.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700112 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700113std::string create_data_user_path(const char* volume_uuid, userid_t userid) {
114 std::string data(create_data_path(volume_uuid));
115 if (volume_uuid == nullptr) {
116 if (userid == 0) {
117 return StringPrintf("%s/data", data.c_str());
118 } else {
119 return StringPrintf("%s/user/%u", data.c_str(), userid);
120 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700121 } else {
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700122 return StringPrintf("%s/user/%u", data.c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700123 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700124}
125
126/**
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800127 * Create the path name for device encrypted user data for a certain userid.
128 */
129std::string create_data_user_de_path(const char* volume_uuid, userid_t userid) {
130 std::string data(create_data_path(volume_uuid));
131 return StringPrintf("%s/user_de/%u", data.c_str(), userid);
132}
133
134/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700135 * Create the path name for media for a certain userid.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700136 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700137std::string create_data_media_path(const char* volume_uuid, userid_t userid) {
138 return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700139}
140
Jeff Sharkeye3637242015-04-08 20:56:42 -0700141std::vector<userid_t> get_known_users(const char* volume_uuid) {
142 std::vector<userid_t> users;
143
144 // We always have an owner
145 users.push_back(0);
146
147 std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX);
148 DIR* dir = opendir(path.c_str());
149 if (dir == NULL) {
150 // Unable to discover other users, but at least return owner
151 PLOG(ERROR) << "Failed to opendir " << path;
152 return users;
153 }
154
155 struct dirent* ent;
156 while ((ent = readdir(dir))) {
157 if (ent->d_type != DT_DIR) {
158 continue;
159 }
160
161 char* end;
162 userid_t user = strtol(ent->d_name, &end, 10);
163 if (*end == '\0' && user != 0) {
164 LOG(DEBUG) << "Found valid user " << user;
165 users.push_back(user);
166 }
167 }
168 closedir(dir);
169
170 return users;
171}
172
Robin Lee095c7632014-04-25 15:05:19 +0100173/**
174 * Create the path name for config for a certain userid.
175 * Returns 0 on success, and -1 on failure.
176 */
177int create_user_config_path(char path[PATH_MAX], userid_t userid) {
178 if (snprintf(path, PATH_MAX, "%s%d", "/data/misc/user/", userid) > PATH_MAX) {
179 return -1;
180 }
181 return 0;
182}
183
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184int create_move_path(char path[PKG_PATH_MAX],
185 const char* pkgname,
186 const char* leaf,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700187 userid_t userid __unused)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700188{
189 if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1)
190 >= PKG_PATH_MAX) {
191 return -1;
192 }
193
194 sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf);
195 return 0;
196}
197
198/**
199 * Checks whether the package name is valid. Returns -1 on error and
200 * 0 on success.
201 */
202int is_valid_package_name(const char* pkgname) {
203 const char *x = pkgname;
204 int alpha = -1;
205
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700206 if (strlen(pkgname) > PKG_NAME_MAX) {
207 return -1;
208 }
209
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210 while (*x) {
211 if (isalnum(*x) || (*x == '_')) {
212 /* alphanumeric or underscore are fine */
213 } else if (*x == '.') {
214 if ((x == pkgname) || (x[1] == '.') || (x[1] == 0)) {
215 /* periods must not be first, last, or doubled */
216 ALOGE("invalid package name '%s'\n", pkgname);
217 return -1;
218 }
219 } else if (*x == '-') {
220 /* Suffix -X is fine to let versioning of packages.
221 But whatever follows should be alphanumeric.*/
222 alpha = 1;
223 } else {
224 /* anything not A-Z, a-z, 0-9, _, or . is invalid */
225 ALOGE("invalid package name '%s'\n", pkgname);
226 return -1;
227 }
228
229 x++;
230 }
231
232 if (alpha == 1) {
233 // Skip current character
234 x++;
235 while (*x) {
236 if (!isalnum(*x)) {
237 ALOGE("invalid package name '%s' should include only numbers after -\n", pkgname);
238 return -1;
239 }
240 x++;
241 }
242 }
243
244 return 0;
245}
246
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100247static int _delete_dir_contents(DIR *d,
248 int (*exclusion_predicate)(const char *name, const int is_dir))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700249{
250 int result = 0;
251 struct dirent *de;
252 int dfd;
253
254 dfd = dirfd(d);
255
256 if (dfd < 0) return -1;
257
258 while ((de = readdir(d))) {
259 const char *name = de->d_name;
260
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100261 /* check using the exclusion predicate, if provided */
262 if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
263 continue;
264 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700265
266 if (de->d_type == DT_DIR) {
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700267 int subfd;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700268 DIR *subdir;
269
270 /* always skip "." and ".." */
271 if (name[0] == '.') {
272 if (name[1] == 0) continue;
273 if ((name[1] == '.') && (name[2] == 0)) continue;
274 }
275
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700276 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700277 if (subfd < 0) {
278 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
279 result = -1;
280 continue;
281 }
282 subdir = fdopendir(subfd);
283 if (subdir == NULL) {
284 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
285 close(subfd);
286 result = -1;
287 continue;
288 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100289 if (_delete_dir_contents(subdir, exclusion_predicate)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700290 result = -1;
291 }
292 closedir(subdir);
293 if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
294 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
295 result = -1;
296 }
297 } else {
298 if (unlinkat(dfd, name, 0) < 0) {
299 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
300 result = -1;
301 }
302 }
303 }
304
305 return result;
306}
307
308int delete_dir_contents(const char *pathname,
309 int also_delete_dir,
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100310 int (*exclusion_predicate)(const char*, const int))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311{
312 int res = 0;
313 DIR *d;
314
315 d = opendir(pathname);
316 if (d == NULL) {
317 ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
318 return -errno;
319 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100320 res = _delete_dir_contents(d, exclusion_predicate);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700321 closedir(d);
322 if (also_delete_dir) {
323 if (rmdir(pathname)) {
324 ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
325 res = -1;
326 }
327 }
328 return res;
329}
330
331int delete_dir_contents_fd(int dfd, const char *name)
332{
333 int fd, res;
334 DIR *d;
335
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700336 fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700337 if (fd < 0) {
338 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
339 return -1;
340 }
341 d = fdopendir(fd);
342 if (d == NULL) {
343 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
344 close(fd);
345 return -1;
346 }
347 res = _delete_dir_contents(d, 0);
348 closedir(d);
349 return res;
350}
351
Robin Lee60fd3fe2014-10-07 16:55:02 +0100352static int _copy_owner_permissions(int srcfd, int dstfd)
353{
354 struct stat st;
355 if (fstat(srcfd, &st) != 0) {
356 return -1;
357 }
358 if (fchmod(dstfd, st.st_mode) != 0) {
359 return -1;
360 }
361 return 0;
362}
363
364static int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
365{
366 int result = 0;
367 if (_copy_owner_permissions(sdfd, ddfd) != 0) {
368 ALOGE("_copy_dir_files failed to copy dir permissions\n");
369 }
370 if (fchown(ddfd, owner, group) != 0) {
371 ALOGE("_copy_dir_files failed to change dir owner\n");
372 }
373
374 DIR *ds = fdopendir(sdfd);
375 if (ds == NULL) {
376 ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
377 return -1;
378 }
379 struct dirent *de;
380 while ((de = readdir(ds))) {
381 if (de->d_type != DT_REG) {
382 continue;
383 }
384
385 const char *name = de->d_name;
386 int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
387 int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
388 if (fsfd == -1 || fdfd == -1) {
389 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
390 } else {
391 if (_copy_owner_permissions(fsfd, fdfd) != 0) {
392 ALOGE("Failed to change file permissions\n");
393 }
394 if (fchown(fdfd, owner, group) != 0) {
395 ALOGE("Failed to change file owner\n");
396 }
397
398 char buf[8192];
399 ssize_t size;
400 while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
401 write(fdfd, buf, size);
402 }
403 if (size < 0) {
404 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
405 result = -1;
406 }
407 }
408 close(fdfd);
409 close(fsfd);
410 }
411
412 return result;
413}
414
415int copy_dir_files(const char *srcname,
416 const char *dstname,
417 uid_t owner,
418 uid_t group)
419{
420 int res = 0;
421 DIR *ds = NULL;
422 DIR *dd = NULL;
423
424 ds = opendir(srcname);
425 if (ds == NULL) {
426 ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
427 return -errno;
428 }
429
430 mkdir(dstname, 0600);
431 dd = opendir(dstname);
432 if (dd == NULL) {
433 ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
434 closedir(ds);
435 return -errno;
436 }
437
438 int sdfd = dirfd(ds);
439 int ddfd = dirfd(dd);
440 if (sdfd != -1 && ddfd != -1) {
441 res = _copy_dir_files(sdfd, ddfd, owner, group);
442 } else {
443 res = -errno;
444 }
445 closedir(dd);
446 closedir(ds);
447 return res;
448}
449
Mike Lockwood94afecf2012-10-24 10:45:23 -0700450int lookup_media_dir(char basepath[PATH_MAX], const char *dir)
451{
452 DIR *d;
453 struct dirent *de;
454 struct stat s;
455 char* dirpos = basepath + strlen(basepath);
456
457 if ((*(dirpos-1)) != '/') {
458 *dirpos = '/';
459 dirpos++;
460 }
461
462 CACHE_NOISY(ALOGI("Looking up %s in %s\n", dir, basepath));
463 // Verify the path won't extend beyond our buffer, to avoid
464 // repeated checking later.
465 if ((dirpos-basepath+strlen(dir)) >= (PATH_MAX-1)) {
466 ALOGW("Path exceeds limit: %s%s", basepath, dir);
467 return -1;
468 }
469
470 // First, can we find this directory with the case that is given?
471 strcpy(dirpos, dir);
472 if (stat(basepath, &s) >= 0) {
473 CACHE_NOISY(ALOGI("Found direct: %s\n", basepath));
474 return 0;
475 }
476
477 // Not found with that case... search through all entries to find
478 // one that matches regardless of case.
479 *dirpos = 0;
480
481 d = opendir(basepath);
482 if (d == NULL) {
483 return -1;
484 }
485
486 while ((de = readdir(d))) {
487 if (strcasecmp(de->d_name, dir) == 0) {
488 strcpy(dirpos, de->d_name);
489 closedir(d);
490 CACHE_NOISY(ALOGI("Found search: %s\n", basepath));
491 return 0;
492 }
493 }
494
495 ALOGW("Couldn't find %s in %s", dir, basepath);
496 closedir(d);
497 return -1;
498}
499
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700500int64_t data_disk_free(const std::string& data_path)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700501{
502 struct statfs sfs;
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700503 if (statfs(data_path.c_str(), &sfs) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700504 return sfs.f_bavail * sfs.f_bsize;
505 } else {
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700506 PLOG(ERROR) << "Couldn't statfs " << data_path;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700507 return -1;
508 }
509}
510
511cache_t* start_cache_collection()
512{
513 cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t));
514 return cache;
515}
516
517#define CACHE_BLOCK_SIZE (512*1024)
518
519static void* _cache_malloc(cache_t* cache, size_t len)
520{
521 len = (len+3)&~3;
522 if (len > (CACHE_BLOCK_SIZE/2)) {
523 // It doesn't make sense to try to put this allocation into one
524 // of our blocks, because it is so big. Instead, make a new dedicated
525 // block for it.
526 int8_t* res = (int8_t*)malloc(len+sizeof(void*));
527 if (res == NULL) {
528 return NULL;
529 }
530 CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %d", res, len));
531 // Link it into our list of blocks, not disrupting the current one.
532 if (cache->memBlocks == NULL) {
533 *(void**)res = NULL;
534 cache->memBlocks = res;
535 } else {
536 *(void**)res = *(void**)cache->memBlocks;
537 *(void**)cache->memBlocks = res;
538 }
539 return res + sizeof(void*);
540 }
541 int8_t* res = cache->curMemBlockAvail;
542 int8_t* nextPos = res + len;
543 if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) {
Jeff Sharkey19803802015-04-07 12:44:51 -0700544 int8_t* newBlock = (int8_t*) malloc(CACHE_BLOCK_SIZE);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700545 if (newBlock == NULL) {
546 return NULL;
547 }
548 CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock));
549 *(void**)newBlock = cache->memBlocks;
550 cache->memBlocks = newBlock;
551 res = cache->curMemBlockAvail = newBlock + sizeof(void*);
552 cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE;
553 nextPos = res + len;
554 }
555 CACHE_NOISY(ALOGI("cache_malloc: ret %p size %d, block=%p, nextPos=%p",
556 res, len, cache->memBlocks, nextPos));
557 cache->curMemBlockAvail = nextPos;
558 return res;
559}
560
561static void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len)
562{
563 // This isn't really a realloc, but it is good enough for our purposes here.
564 void* alloc = _cache_malloc(cache, len);
565 if (alloc != NULL && cur != NULL) {
566 memcpy(alloc, cur, origLen < len ? origLen : len);
567 }
568 return alloc;
569}
570
571static void _inc_num_cache_collected(cache_t* cache)
572{
573 cache->numCollected++;
574 if ((cache->numCollected%20000) == 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700575 ALOGI("Collected cache so far: %zd directories, %zd files",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700576 cache->numDirs, cache->numFiles);
577 }
578}
579
580static cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name)
581{
582 size_t nameLen = strlen(name);
583 cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1);
584 if (dir != NULL) {
585 dir->parent = parent;
586 dir->childCount = 0;
587 dir->hiddenCount = 0;
588 dir->deleted = 0;
589 strcpy(dir->name, name);
590 if (cache->numDirs >= cache->availDirs) {
591 size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2;
592 cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs,
593 cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*));
594 if (newDirs == NULL) {
595 ALOGE("Failure growing cache dirs array for %s\n", name);
596 return NULL;
597 }
598 cache->availDirs = newAvail;
599 cache->dirs = newDirs;
600 }
601 cache->dirs[cache->numDirs] = dir;
602 cache->numDirs++;
603 if (parent != NULL) {
604 parent->childCount++;
605 }
606 _inc_num_cache_collected(cache);
607 } else {
608 ALOGE("Failure allocating cache_dir_t for %s\n", name);
609 }
610 return dir;
611}
612
613static cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime,
614 const char *name)
615{
616 size_t nameLen = strlen(name);
617 cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1);
618 if (file != NULL) {
619 file->dir = dir;
620 file->modTime = modTime;
621 strcpy(file->name, name);
622 if (cache->numFiles >= cache->availFiles) {
623 size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2;
624 cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files,
625 cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*));
626 if (newFiles == NULL) {
627 ALOGE("Failure growing cache file array for %s\n", name);
628 return NULL;
629 }
630 cache->availFiles = newAvail;
631 cache->files = newFiles;
632 }
633 CACHE_NOISY(ALOGI("Setting file %p at position %d in array %p", file,
634 cache->numFiles, cache->files));
635 cache->files[cache->numFiles] = file;
636 cache->numFiles++;
637 dir->childCount++;
638 _inc_num_cache_collected(cache);
639 } else {
640 ALOGE("Failure allocating cache_file_t for %s\n", name);
641 }
642 return file;
643}
644
645static int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName,
646 DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen)
647{
648 struct dirent *de;
649 cache_dir_t* cacheDir = NULL;
650 int dfd;
651
652 CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s",
653 parentDir, dirName, dir, pathBase));
654
655 dfd = dirfd(dir);
656
657 if (dfd < 0) return 0;
658
659 // Sub-directories always get added to the data structure, so if they
660 // are empty we will know about them to delete them later.
661 cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
662
663 while ((de = readdir(dir))) {
664 const char *name = de->d_name;
665
666 if (de->d_type == DT_DIR) {
667 int subfd;
668 DIR *subdir;
669
670 /* always skip "." and ".." */
671 if (name[0] == '.') {
672 if (name[1] == 0) continue;
673 if ((name[1] == '.') && (name[2] == 0)) continue;
674 }
675
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700676 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700677 if (subfd < 0) {
678 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
679 continue;
680 }
681 subdir = fdopendir(subfd);
682 if (subdir == NULL) {
683 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
684 close(subfd);
685 continue;
686 }
687 if (cacheDir == NULL) {
688 cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
689 }
690 if (cacheDir != NULL) {
691 // Update pathBase for the new path... this may change dirName
692 // if that is also pointing to the path, but we are done with it
693 // now.
694 size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
695 CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase));
696 if (finallen < pathAvailLen) {
697 _add_cache_files(cache, cacheDir, name, subdir, pathBase,
698 pathPos+finallen, pathAvailLen-finallen);
699 } else {
700 // Whoops, the final path is too long! We'll just delete
701 // this directory.
702 ALOGW("Cache dir %s truncated in path %s; deleting dir\n",
703 name, pathBase);
704 _delete_dir_contents(subdir, NULL);
705 if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
706 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
707 }
708 }
709 }
710 closedir(subdir);
711 } else if (de->d_type == DT_REG) {
712 // Skip files that start with '.'; they will be deleted if
713 // their entire directory is deleted. This allows for metadata
714 // like ".nomedia" to remain in the directory until the entire
715 // directory is deleted.
716 if (cacheDir == NULL) {
717 cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
718 }
719 if (name[0] == '.') {
720 cacheDir->hiddenCount++;
721 continue;
722 }
723 if (cacheDir != NULL) {
724 // Build final full path for file... this may change dirName
725 // if that is also pointing to the path, but we are done with it
726 // now.
727 size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
728 CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase));
729 if (finallen < pathAvailLen) {
730 struct stat s;
731 if (stat(pathBase, &s) >= 0) {
732 _add_cache_file_t(cache, cacheDir, s.st_mtime, name);
733 } else {
734 ALOGW("Unable to stat cache file %s; deleting\n", pathBase);
735 if (unlink(pathBase) < 0) {
736 ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno));
737 }
738 }
739 } else {
740 // Whoops, the final path is too long! We'll just delete
741 // this file.
742 ALOGW("Cache file %s truncated in path %s; deleting\n",
743 name, pathBase);
744 if (unlinkat(dfd, name, 0) < 0) {
745 *pathPos = 0;
746 ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase,
747 strerror(errno));
748 }
749 }
750 }
751 } else {
752 cacheDir->hiddenCount++;
753 }
754 }
755 return 0;
756}
757
758void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir)
759{
760 DIR *d;
761 struct dirent *de;
762 char dirname[PATH_MAX];
763
764 CACHE_NOISY(ALOGI("add_cache_files: base=%s cachedir=%s\n", basepath, cachedir));
765
766 d = opendir(basepath);
767 if (d == NULL) {
768 return;
769 }
770
771 while ((de = readdir(d))) {
772 if (de->d_type == DT_DIR) {
773 DIR* subdir;
774 const char *name = de->d_name;
775 char* pathpos;
776
777 /* always skip "." and ".." */
778 if (name[0] == '.') {
779 if (name[1] == 0) continue;
780 if ((name[1] == '.') && (name[2] == 0)) continue;
781 }
782
783 strcpy(dirname, basepath);
784 pathpos = dirname + strlen(dirname);
785 if ((*(pathpos-1)) != '/') {
786 *pathpos = '/';
787 pathpos++;
788 *pathpos = 0;
789 }
790 if (cachedir != NULL) {
791 snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s/%s", name, cachedir);
792 } else {
793 snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s", name);
794 }
795 CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname));
796 subdir = opendir(dirname);
797 if (subdir != NULL) {
798 size_t dirnameLen = strlen(dirname);
799 _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen,
800 PATH_MAX - dirnameLen);
801 closedir(subdir);
802 }
803 }
804 }
805
806 closedir(d);
807}
808
809static char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir)
810{
811 char *pos = path;
812 if (dir->parent != NULL) {
813 pos = create_dir_path(path, dir->parent);
814 }
815 // Note that we don't need to worry about going beyond the buffer,
816 // since when we were constructing the cache entries our maximum
817 // buffer size for full paths was PATH_MAX.
818 strcpy(pos, dir->name);
819 pos += strlen(pos);
820 *pos = '/';
821 pos++;
822 *pos = 0;
823 return pos;
824}
825
826static void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir)
827{
828 if (dir->parent != NULL) {
829 create_dir_path(path, dir);
830 ALOGI("DEL DIR %s\n", path);
831 if (dir->hiddenCount <= 0) {
832 if (rmdir(path)) {
833 ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno));
834 return;
835 }
836 } else {
837 // The directory contains hidden files so we need to delete
838 // them along with the directory itself.
839 if (delete_dir_contents(path, 1, NULL)) {
840 return;
841 }
842 }
843 dir->parent->childCount--;
844 dir->deleted = 1;
845 if (dir->parent->childCount <= 0) {
846 delete_cache_dir(path, dir->parent);
847 }
848 } else if (dir->hiddenCount > 0) {
849 // This is a root directory, but it has hidden files. Get rid of
850 // all of those files, but not the directory itself.
851 create_dir_path(path, dir);
852 ALOGI("DEL CONTENTS %s\n", path);
853 delete_dir_contents(path, 0, NULL);
854 }
855}
856
857static int cache_modtime_sort(const void *lhsP, const void *rhsP)
858{
859 const cache_file_t *lhs = *(const cache_file_t**)lhsP;
860 const cache_file_t *rhs = *(const cache_file_t**)rhsP;
861 return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0);
862}
863
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700864void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700865{
866 size_t i;
867 int skip = 0;
868 char path[PATH_MAX];
869
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700870 ALOGI("Collected cache files: %zd directories, %zd files",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700871 cache->numDirs, cache->numFiles);
872
873 CACHE_NOISY(ALOGI("Sorting files..."));
874 qsort(cache->files, cache->numFiles, sizeof(cache_file_t*),
875 cache_modtime_sort);
876
877 CACHE_NOISY(ALOGI("Cleaning empty directories..."));
878 for (i=cache->numDirs; i>0; i--) {
879 cache_dir_t* dir = cache->dirs[i-1];
880 if (dir->childCount <= 0 && !dir->deleted) {
881 delete_cache_dir(path, dir);
882 }
883 }
884
885 CACHE_NOISY(ALOGI("Trimming files..."));
886 for (i=0; i<cache->numFiles; i++) {
887 skip++;
888 if (skip > 10) {
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700889 if (data_disk_free(data_path) > free_size) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700890 return;
891 }
892 skip = 0;
893 }
894 cache_file_t* file = cache->files[i];
895 strcpy(create_dir_path(path, file->dir), file->name);
896 ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path);
897 if (unlink(path) < 0) {
898 ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno));
899 }
900 file->dir->childCount--;
901 if (file->dir->childCount <= 0) {
902 delete_cache_dir(path, file->dir);
903 }
904 }
905}
906
907void finish_cache_collection(cache_t* cache)
908{
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700909 CACHE_NOISY(size_t i;)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700910
911 CACHE_NOISY(ALOGI("clear_cache_files: %d dirs, %d files\n", cache->numDirs, cache->numFiles));
912 CACHE_NOISY(
913 for (i=0; i<cache->numDirs; i++) {
914 cache_dir_t* dir = cache->dirs[i];
915 ALOGI("dir #%d: %p %s parent=%p\n", i, dir, dir->name, dir->parent);
916 })
917 CACHE_NOISY(
918 for (i=0; i<cache->numFiles; i++) {
919 cache_file_t* file = cache->files[i];
920 ALOGI("file #%d: %p %s time=%d dir=%p\n", i, file, file->name,
921 (int)file->modTime, file->dir);
922 })
923 void* block = cache->memBlocks;
924 while (block != NULL) {
925 void* nextBlock = *(void**)block;
926 CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block));
927 free(block);
928 block = nextBlock;
929 }
930 free(cache);
931}
932
933/**
Calin Juravlec597b6d2014-08-19 17:43:05 +0100934 * Validate that the path is valid in the context of the provided directory.
935 * The path is allowed to have at most one subdirectory and no indirections
936 * to top level directories (i.e. have "..").
937 */
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700938static int validate_path(const dir_rec_t* dir, const char* path, int maxSubdirs) {
Calin Juravlec597b6d2014-08-19 17:43:05 +0100939 size_t dir_len = dir->len;
940 const char* subdir = strchr(path + dir_len, '/');
941
942 // Only allow the path to have at most one subdirectory.
943 if (subdir != NULL) {
944 ++subdir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700945 if ((--maxSubdirs == 0) && strchr(subdir, '/') != NULL) {
Calin Juravlec597b6d2014-08-19 17:43:05 +0100946 ALOGE("invalid apk path '%s' (subdir?)\n", path);
947 return -1;
948 }
949 }
950
951 // Directories can't have a period directly after the directory markers to prevent "..".
952 if ((path[dir_len] == '.') || ((subdir != NULL) && (*subdir == '.'))) {
953 ALOGE("invalid apk path '%s' (trickery)\n", path);
954 return -1;
955 }
956
957 return 0;
958}
959
960/**
Mike Lockwood94afecf2012-10-24 10:45:23 -0700961 * Checks whether a path points to a system app (.apk file). Returns 0
962 * if it is a system app or -1 if it is not.
963 */
964int validate_system_app_path(const char* path) {
965 size_t i;
966
967 for (i = 0; i < android_system_dirs.count; i++) {
968 const size_t dir_len = android_system_dirs.dirs[i].len;
969 if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) {
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700970 return validate_path(android_system_dirs.dirs + i, path, 1);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700971 }
972 }
973
974 return -1;
975}
976
977/**
978 * Get the contents of a environment variable that contains a path. Caller
979 * owns the string that is inserted into the directory record. Returns
980 * 0 on success and -1 on error.
981 */
982int get_path_from_env(dir_rec_t* rec, const char* var) {
983 const char* path = getenv(var);
984 int ret = get_path_from_string(rec, path);
985 if (ret < 0) {
986 ALOGW("Problem finding value for environment variable %s\n", var);
987 }
988 return ret;
989}
990
991/**
992 * Puts the string into the record as a directory. Appends '/' to the end
993 * of all paths. Caller owns the string that is inserted into the directory
994 * record. A null value will result in an error.
995 *
996 * Returns 0 on success and -1 on error.
997 */
998int get_path_from_string(dir_rec_t* rec, const char* path) {
999 if (path == NULL) {
1000 return -1;
1001 } else {
1002 const size_t path_len = strlen(path);
1003 if (path_len <= 0) {
1004 return -1;
1005 }
1006
1007 // Make sure path is absolute.
1008 if (path[0] != '/') {
1009 return -1;
1010 }
1011
1012 if (path[path_len - 1] == '/') {
1013 // Path ends with a forward slash. Make our own copy.
1014
1015 rec->path = strdup(path);
1016 if (rec->path == NULL) {
1017 return -1;
1018 }
1019
1020 rec->len = path_len;
1021 } else {
1022 // Path does not end with a slash. Generate a new string.
1023 char *dst;
1024
1025 // Add space for slash and terminating null.
1026 size_t dst_size = path_len + 2;
1027
Jeff Sharkey19803802015-04-07 12:44:51 -07001028 rec->path = (char*) malloc(dst_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001029 if (rec->path == NULL) {
1030 return -1;
1031 }
1032
1033 dst = rec->path;
1034
1035 if (append_and_increment(&dst, path, &dst_size) < 0
1036 || append_and_increment(&dst, "/", &dst_size)) {
1037 ALOGE("Error canonicalizing path");
1038 return -1;
1039 }
1040
1041 rec->len = dst - rec->path;
1042 }
1043 }
1044 return 0;
1045}
1046
1047int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) {
1048 dst->len = src->len + strlen(suffix);
1049 const size_t dstSize = dst->len + 1;
1050 dst->path = (char*) malloc(dstSize);
1051
1052 if (dst->path == NULL
1053 || snprintf(dst->path, dstSize, "%s%s", src->path, suffix)
1054 != (ssize_t) dst->len) {
1055 ALOGE("Could not allocate memory to hold appended path; aborting\n");
1056 return -1;
1057 }
1058
1059 return 0;
1060}
1061
1062/**
Narayan Kamathd845c962015-06-04 13:20:27 +01001063 * Check whether path points to a valid path for an APK file. The path must
1064 * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within
1065 * that path. Returns -1 when an invalid path is encountered and 0 when a valid path
1066 * is encountered.
Mike Lockwood94afecf2012-10-24 10:45:23 -07001067 */
Narayan Kamathd845c962015-06-04 13:20:27 +01001068static int validate_apk_path_internal(const char *path, int maxSubdirs) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001069 const dir_rec_t* dir = NULL;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001070 if (!strncmp(path, android_app_dir.path, android_app_dir.len)) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001071 dir = &android_app_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001072 } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001073 dir = &android_app_private_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001074 } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) {
Calin Juravlec597b6d2014-08-19 17:43:05 +01001075 dir = &android_asec_dir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -07001076 } else if (!strncmp(path, android_mnt_expand_dir.path, android_mnt_expand_dir.len)) {
1077 dir = &android_mnt_expand_dir;
Narayan Kamathd845c962015-06-04 13:20:27 +01001078 if (maxSubdirs < 2) {
1079 maxSubdirs = 2;
1080 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001081 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001082 return -1;
1083 }
1084
Jeff Sharkeye23a1322015-04-06 16:19:39 -07001085 return validate_path(dir, path, maxSubdirs);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001086}
1087
Narayan Kamathd845c962015-06-04 13:20:27 +01001088int validate_apk_path(const char* path) {
1089 return validate_apk_path_internal(path, 1 /* maxSubdirs */);
1090}
1091
1092int validate_apk_path_subdirs(const char* path) {
1093 return validate_apk_path_internal(path, 3 /* maxSubdirs */);
1094}
1095
Mike Lockwood94afecf2012-10-24 10:45:23 -07001096int append_and_increment(char** dst, const char* src, size_t* dst_size) {
1097 ssize_t ret = strlcpy(*dst, src, *dst_size);
1098 if (ret < 0 || (size_t) ret >= *dst_size) {
1099 return -1;
1100 }
1101 *dst += ret;
1102 *dst_size -= ret;
1103 return 0;
1104}
1105
Jeff Sharkey19803802015-04-07 12:44:51 -07001106char *build_string2(const char *s1, const char *s2) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001107 if (s1 == NULL || s2 == NULL) return NULL;
1108
1109 int len_s1 = strlen(s1);
1110 int len_s2 = strlen(s2);
1111 int len = len_s1 + len_s2 + 1;
Jeff Sharkey19803802015-04-07 12:44:51 -07001112 char *result = (char *) malloc(len);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001113 if (result == NULL) return NULL;
1114
1115 strcpy(result, s1);
1116 strcpy(result + len_s1, s2);
1117
1118 return result;
1119}
1120
Jeff Sharkey19803802015-04-07 12:44:51 -07001121char *build_string3(const char *s1, const char *s2, const char *s3) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001122 if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL;
1123
1124 int len_s1 = strlen(s1);
1125 int len_s2 = strlen(s2);
1126 int len_s3 = strlen(s3);
1127 int len = len_s1 + len_s2 + len_s3 + 1;
Jeff Sharkey19803802015-04-07 12:44:51 -07001128 char *result = (char *) malloc(len);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001129 if (result == NULL) return NULL;
1130
1131 strcpy(result, s1);
1132 strcpy(result + len_s1, s2);
1133 strcpy(result + len_s1 + len_s2, s3);
1134
1135 return result;
1136}
1137
1138/* Ensure that /data/media directories are prepared for given user. */
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001139int ensure_media_user_dirs(const char* uuid, userid_t userid) {
1140 std::string media_user_path(create_data_media_path(uuid, userid));
1141 if (fs_prepare_dir(media_user_path.c_str(), 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001142 return -1;
1143 }
1144
1145 return 0;
1146}
Dave Allisond9370732014-01-30 14:19:23 -08001147
Robin Lee095c7632014-04-25 15:05:19 +01001148int ensure_config_user_dirs(userid_t userid) {
1149 char config_user_path[PATH_MAX];
Robin Lee095c7632014-04-25 15:05:19 +01001150
1151 // writable by system, readable by any app within the same user
Robin Lee60fd3fe2014-10-07 16:55:02 +01001152 const int uid = multiuser_get_uid(userid, AID_SYSTEM);
1153 const int gid = multiuser_get_uid(userid, AID_EVERYBODY);
Robin Lee095c7632014-04-25 15:05:19 +01001154
1155 // Ensure /data/misc/user/<userid> exists
1156 create_user_config_path(config_user_path, userid);
1157 if (fs_prepare_dir(config_user_path, 0750, uid, gid) == -1) {
1158 return -1;
1159 }
1160
1161 return 0;
1162}
1163
Dave Allisond9370732014-01-30 14:19:23 -08001164int create_profile_file(const char *pkgname, gid_t gid) {
1165 const char *profile_dir = DALVIK_CACHE_PREFIX "profiles";
Dave Allisond9370732014-01-30 14:19:23 -08001166 char profile_file[PKG_PATH_MAX];
1167
Dave Allisond9370732014-01-30 14:19:23 -08001168 snprintf(profile_file, sizeof(profile_file), "%s/%s", profile_dir, pkgname);
1169
1170 // The 'system' user needs to be able to read the profile to determine if dex2oat
1171 // needs to be run. This is done in dalvik.system.DexFile.isDexOptNeededInternal(). So
Nick Kralevich0db0f972014-06-11 18:23:59 -07001172 // we assign ownership to AID_SYSTEM and ensure it's not world-readable.
Dave Allisond9370732014-01-30 14:19:23 -08001173
Nick Kralevich0db0f972014-06-11 18:23:59 -07001174 int fd = open(profile_file, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0660);
Dave Allisond9370732014-01-30 14:19:23 -08001175
Nick Kralevich0db0f972014-06-11 18:23:59 -07001176 // Always set the uid/gid/permissions. The file could have been previously created
1177 // with different permissions.
Dave Allisond9370732014-01-30 14:19:23 -08001178 if (fd >= 0) {
Nick Kralevich0db0f972014-06-11 18:23:59 -07001179 if (fchown(fd, AID_SYSTEM, gid) < 0) {
Dave Allisond9370732014-01-30 14:19:23 -08001180 ALOGE("cannot chown profile file '%s': %s\n", profile_file, strerror(errno));
1181 close(fd);
1182 unlink(profile_file);
1183 return -1;
1184 }
1185
Nick Kralevich0db0f972014-06-11 18:23:59 -07001186 if (fchmod(fd, 0660) < 0) {
Dave Allisond9370732014-01-30 14:19:23 -08001187 ALOGE("cannot chmod profile file '%s': %s\n", profile_file, strerror(errno));
1188 close(fd);
1189 unlink(profile_file);
1190 return -1;
1191 }
1192 close(fd);
1193 }
1194 return 0;
1195}
1196
1197void remove_profile_file(const char *pkgname) {
1198 char profile_file[PKG_PATH_MAX];
1199 snprintf(profile_file, sizeof(profile_file), "%s/%s", DALVIK_CACHE_PREFIX "profiles", pkgname);
1200 unlink(profile_file);
1201}