blob: e4f63e21fd3a8f73ce5d5e8b9317f4b05ba8a1a5 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, 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
Nick Kralevichd7471292013-02-28 16:59:13 -080017#include <sys/capability.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070018#include "installd.h"
19#include <diskusage/dirsize.h>
20#include <selinux/android.h>
21
22/* Directory records that are used in execution of commands. */
23dir_rec_t android_data_dir;
24dir_rec_t android_asec_dir;
25dir_rec_t android_app_dir;
26dir_rec_t android_app_private_dir;
27dir_rec_t android_app_lib_dir;
28dir_rec_t android_media_dir;
29dir_rec_array_t android_system_dirs;
30
Robert Craig4d3fd4e2013-03-25 06:33:03 -040031int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070032{
33 char pkgdir[PKG_PATH_MAX];
34 char libsymlink[PKG_PATH_MAX];
35 char applibdir[PKG_PATH_MAX];
36 struct stat libStat;
37
38 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
39 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
40 return -1;
41 }
42
43 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
44 ALOGE("cannot create package path\n");
45 return -1;
46 }
47
48 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
49 ALOGE("cannot create package lib symlink origin path\n");
50 return -1;
51 }
52
53 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
54 ALOGE("cannot create package lib symlink dest path\n");
55 return -1;
56 }
57
Nick Kralevicha2d838a2013-01-09 16:00:35 -080058 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070059 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
60 return -1;
61 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080062 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070063 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
64 unlink(pkgdir);
65 return -1;
66 }
67
68 if (lstat(libsymlink, &libStat) < 0) {
69 if (errno != ENOENT) {
70 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
71 return -1;
72 }
73 } else {
74 if (S_ISDIR(libStat.st_mode)) {
75 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
76 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
77 return -1;
78 }
79 } else if (S_ISLNK(libStat.st_mode)) {
80 if (unlink(libsymlink) < 0) {
81 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
82 return -1;
83 }
84 }
85 }
86
87 if (symlink(applibdir, libsymlink) < 0) {
88 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
89 strerror(errno));
90 unlink(pkgdir);
91 return -1;
92 }
93
Robert Craig4d3fd4e2013-03-25 06:33:03 -040094 if (selinux_android_setfilecon2(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070095 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
96 unlink(libsymlink);
97 unlink(pkgdir);
98 return -errno;
99 }
100
101 if (chown(pkgdir, uid, gid) < 0) {
102 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
103 unlink(libsymlink);
104 unlink(pkgdir);
105 return -1;
106 }
107
108 return 0;
109}
110
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700111int uninstall(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700112{
113 char pkgdir[PKG_PATH_MAX];
114
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700115 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700116 return -1;
117
118 /* delete contents AND directory, no exceptions */
119 return delete_dir_contents(pkgdir, 1, NULL);
120}
121
122int renamepkg(const char *oldpkgname, const char *newpkgname)
123{
124 char oldpkgdir[PKG_PATH_MAX];
125 char newpkgdir[PKG_PATH_MAX];
126
127 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
128 return -1;
129 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
130 return -1;
131
132 if (rename(oldpkgdir, newpkgdir) < 0) {
133 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
134 return -errno;
135 }
136 return 0;
137}
138
139int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
140{
141 char pkgdir[PKG_PATH_MAX];
142 struct stat s;
143 int rc = 0;
144
145 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
146 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
147 return -1;
148 }
149
150 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
151 ALOGE("cannot create package path\n");
152 return -1;
153 }
154
155 if (stat(pkgdir, &s) < 0) return -1;
156
157 if (s.st_uid != 0 || s.st_gid != 0) {
158 ALOGE("fixing uid of non-root pkg: %s %lu %lu\n", pkgdir, s.st_uid, s.st_gid);
159 return -1;
160 }
161
162 if (chmod(pkgdir, 0751) < 0) {
163 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
164 unlink(pkgdir);
165 return -errno;
166 }
167 if (chown(pkgdir, uid, gid) < 0) {
168 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
169 unlink(pkgdir);
170 return -errno;
171 }
172
173 return 0;
174}
175
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700176int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700177{
178 char pkgdir[PKG_PATH_MAX];
179
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700180 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181 return -1;
182
183 /* delete contents, excluding "lib", but not the directory itself */
184 return delete_dir_contents(pkgdir, 0, "lib");
185}
186
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700187int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700188{
189 char pkgdir[PKG_PATH_MAX];
190 char applibdir[PKG_PATH_MAX];
191 char libsymlink[PKG_PATH_MAX];
192 struct stat libStat;
193
194 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700195 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700196 return -1;
197 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700198 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700199 ALOGE("cannot create package lib symlink origin path\n");
200 return -1;
201 }
202 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
203 ALOGE("cannot create package lib symlink dest path\n");
204 return -1;
205 }
206
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800207 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700208 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
209 return -errno;
210 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800211 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700212 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
213 unlink(pkgdir);
214 return -errno;
215 }
216
217 if (lstat(libsymlink, &libStat) < 0) {
218 if (errno != ENOENT) {
219 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
220 unlink(pkgdir);
221 return -1;
222 }
223 } else {
224 if (S_ISDIR(libStat.st_mode)) {
225 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
226 ALOGE("couldn't delete lib directory during install for non-primary: %s",
227 libsymlink);
228 unlink(pkgdir);
229 return -1;
230 }
231 } else if (S_ISLNK(libStat.st_mode)) {
232 if (unlink(libsymlink) < 0) {
233 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
234 libsymlink);
235 unlink(pkgdir);
236 return -1;
237 }
238 }
239 }
240
241 if (symlink(applibdir, libsymlink) < 0) {
242 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
243 applibdir, strerror(errno));
244 unlink(pkgdir);
245 return -1;
246 }
247
Robert Craig880d1a92013-07-29 09:18:09 -0400248 if (selinux_android_setfilecon2(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700249 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
250 unlink(libsymlink);
251 unlink(pkgdir);
252 return -errno;
253 }
254
255 if (chown(pkgdir, uid, uid) < 0) {
256 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
257 unlink(libsymlink);
258 unlink(pkgdir);
259 return -errno;
260 }
261
262 return 0;
263}
264
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700265int delete_user(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700266{
267 char data_path[PKG_PATH_MAX];
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700268 if (create_user_path(data_path, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700269 return -1;
270 }
271 if (delete_dir_contents(data_path, 1, NULL)) {
272 return -1;
273 }
274
275 char media_path[PATH_MAX];
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700276 if (create_user_media_path(media_path, userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700277 return -1;
278 }
279 if (delete_dir_contents(media_path, 1, NULL) == -1) {
280 return -1;
281 }
282
283 return 0;
284}
285
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700286int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700287{
288 char cachedir[PKG_PATH_MAX];
289
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700290 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700291 return -1;
292
293 /* delete contents, not the directory, no exceptions */
294 return delete_dir_contents(cachedir, 0, 0);
295}
296
297/* Try to ensure free_size bytes of storage are available.
298 * Returns 0 on success.
299 * This is rather simple-minded because doing a full LRU would
300 * be potentially memory-intensive, and without atime it would
301 * also require that apps constantly modify file metadata even
302 * when just reading from the cache, which is pretty awful.
303 */
304int free_cache(int64_t free_size)
305{
306 cache_t* cache;
307 int64_t avail;
308 DIR *d;
309 struct dirent *de;
310 char tmpdir[PATH_MAX];
311 char *dirpos;
312
313 avail = data_disk_free();
314 if (avail < 0) return -1;
315
316 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
317 if (avail >= free_size) return 0;
318
319 cache = start_cache_collection();
320
321 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700322 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700323 //ALOGI("adding cache files from %s\n", tmpdir);
324 add_cache_files(cache, tmpdir, "cache");
325 }
326
327 // Search for other users and add any cache files from them.
328 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
329 SECONDARY_USER_PREFIX);
330 dirpos = tmpdir + strlen(tmpdir);
331 d = opendir(tmpdir);
332 if (d != NULL) {
333 while ((de = readdir(d))) {
334 if (de->d_type == DT_DIR) {
335 const char *name = de->d_name;
336 /* always skip "." and ".." */
337 if (name[0] == '.') {
338 if (name[1] == 0) continue;
339 if ((name[1] == '.') && (name[2] == 0)) continue;
340 }
341 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
342 strcpy(dirpos, name);
343 //ALOGI("adding cache files from %s\n", tmpdir);
344 add_cache_files(cache, tmpdir, "cache");
345 } else {
346 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
347 }
348 }
349 }
350 closedir(d);
351 }
352
353 // Collect cache files on external storage for all users (if it is mounted as part
354 // of the internal storage).
355 strcpy(tmpdir, android_media_dir.path);
356 dirpos = tmpdir + strlen(tmpdir);
357 d = opendir(tmpdir);
358 if (d != NULL) {
359 while ((de = readdir(d))) {
360 if (de->d_type == DT_DIR) {
361 const char *name = de->d_name;
362 /* skip any dir that doesn't start with a number, so not a user */
363 if (name[0] < '0' || name[0] > '9') {
364 continue;
365 }
366 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
367 strcpy(dirpos, name);
368 if (lookup_media_dir(tmpdir, "Android") == 0
369 && lookup_media_dir(tmpdir, "data") == 0) {
370 //ALOGI("adding cache files from %s\n", tmpdir);
371 add_cache_files(cache, tmpdir, "cache");
372 }
373 } else {
374 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
375 }
376 }
377 }
378 closedir(d);
379 }
380
381 clear_cache_files(cache, free_size);
382 finish_cache_collection(cache);
383
384 return data_disk_free() >= free_size ? 0 : -1;
385}
386
387int move_dex(const char *src, const char *dst)
388{
389 char src_dex[PKG_PATH_MAX];
390 char dst_dex[PKG_PATH_MAX];
391
392 if (validate_apk_path(src)) return -1;
393 if (validate_apk_path(dst)) return -1;
394
395 if (create_cache_path(src_dex, src)) return -1;
396 if (create_cache_path(dst_dex, dst)) return -1;
397
398 ALOGV("move %s -> %s\n", src_dex, dst_dex);
399 if (rename(src_dex, dst_dex) < 0) {
400 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
401 return -1;
402 } else {
403 return 0;
404 }
405}
406
407int rm_dex(const char *path)
408{
409 char dex_path[PKG_PATH_MAX];
410
411 if (validate_apk_path(path)) return -1;
412 if (create_cache_path(dex_path, path)) return -1;
413
414 ALOGV("unlink %s\n", dex_path);
415 if (unlink(dex_path) < 0) {
416 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
417 return -1;
418 } else {
419 return 0;
420 }
421}
422
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700423int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700424 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700425 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
426 int64_t* _asecsize)
427{
428 DIR *d;
429 int dfd;
430 struct dirent *de;
431 struct stat s;
432 char path[PKG_PATH_MAX];
433
434 int64_t codesize = 0;
435 int64_t datasize = 0;
436 int64_t cachesize = 0;
437 int64_t asecsize = 0;
438
439 /* count the source apk as code -- but only if it's not
440 * on the /system partition and its not on the sdcard.
441 */
442 if (validate_system_app_path(apkpath) &&
443 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
444 if (stat(apkpath, &s) == 0) {
445 codesize += stat_size(&s);
446 }
447 }
448 /* count the forward locked apk as code if it is given
449 */
450 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
451 if (stat(fwdlock_apkpath, &s) == 0) {
452 codesize += stat_size(&s);
453 }
454 }
455 /* count the cached dexfile as code */
456 if (!create_cache_path(path, apkpath)) {
457 if (stat(path, &s) == 0) {
458 codesize += stat_size(&s);
459 }
460 }
461
462 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700463 if (libdirpath != NULL && libdirpath[0] != '!') {
464 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700465 if (d != NULL) {
466 dfd = dirfd(d);
467 codesize += calculate_dir_size(dfd);
468 closedir(d);
469 }
470 }
471
472 /* compute asec size if it is given
473 */
474 if (asecpath != NULL && asecpath[0] != '!') {
475 if (stat(asecpath, &s) == 0) {
476 asecsize += stat_size(&s);
477 }
478 }
479
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700480 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700481 goto done;
482 }
483
484 d = opendir(path);
485 if (d == NULL) {
486 goto done;
487 }
488 dfd = dirfd(d);
489
490 /* most stuff in the pkgdir is data, except for the "cache"
491 * directory and below, which is cache, and the "lib" directory
492 * and below, which is code...
493 */
494 while ((de = readdir(d))) {
495 const char *name = de->d_name;
496
497 if (de->d_type == DT_DIR) {
498 int subfd;
499 int64_t statsize = 0;
500 int64_t dirsize = 0;
501 /* always skip "." and ".." */
502 if (name[0] == '.') {
503 if (name[1] == 0) continue;
504 if ((name[1] == '.') && (name[2] == 0)) continue;
505 }
506 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
507 statsize = stat_size(&s);
508 }
509 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
510 if (subfd >= 0) {
511 dirsize = calculate_dir_size(subfd);
512 }
513 if(!strcmp(name,"lib")) {
514 codesize += dirsize + statsize;
515 } else if(!strcmp(name,"cache")) {
516 cachesize += dirsize + statsize;
517 } else {
518 datasize += dirsize + statsize;
519 }
520 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
521 // This is the symbolic link to the application's library
522 // code. We'll count this as code instead of data, since
523 // it is not something that the app creates.
524 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
525 codesize += stat_size(&s);
526 }
527 } else {
528 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
529 datasize += stat_size(&s);
530 }
531 }
532 }
533 closedir(d);
534done:
535 *_codesize = codesize;
536 *_datasize = datasize;
537 *_cachesize = cachesize;
538 *_asecsize = asecsize;
539 return 0;
540}
541
542
Mike Lockwood94afecf2012-10-24 10:45:23 -0700543int create_cache_path(char path[PKG_PATH_MAX], const char *src)
544{
545 char *tmp;
546 int srclen;
547 int dstlen;
548
549 srclen = strlen(src);
550
551 /* demand that we are an absolute path */
552 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
553 return -1;
554 }
555
556 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
557 return -1;
558 }
559
560 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
561 strlen(DALVIK_CACHE_POSTFIX) + 1;
562
563 if (dstlen > PKG_PATH_MAX) {
564 return -1;
565 }
566
567 sprintf(path,"%s%s%s",
568 DALVIK_CACHE_PREFIX,
569 src + 1, /* skip the leading / */
570 DALVIK_CACHE_POSTFIX);
571
572 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
573 if (*tmp == '/') {
574 *tmp = '@';
575 }
576 }
577
578 return 0;
579}
580
581static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700582 const char* output_file_name, const char* dexopt_flags)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700583{
584 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
585 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
586 char zip_num[MAX_INT_LEN];
587 char odex_num[MAX_INT_LEN];
588
589 sprintf(zip_num, "%d", zip_fd);
590 sprintf(odex_num, "%d", odex_fd);
591
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700592 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700593 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
594 dexopt_flags, (char*) NULL);
595 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
596}
597
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700598static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
599 const char* output_file_name, const char* dexopt_flags)
600{
601 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
602 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
603 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
604 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
605 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
606 char oat_location_arg[strlen("--oat-name=") + PKG_PATH_MAX];
607
608 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
609 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
610 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
611 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
612
613 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
614 execl(DEX2OAT_BIN, DEX2OAT_BIN,
615 zip_fd_arg, zip_location_arg,
616 oat_fd_arg, oat_location_arg,
617 (char*) NULL);
618 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
619}
620
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100621static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700622{
623 int status;
624 pid_t got_pid;
625
Mike Lockwood94afecf2012-10-24 10:45:23 -0700626 while (1) {
627 got_pid = waitpid(pid, &status, 0);
628 if (got_pid == -1 && errno == EINTR) {
629 printf("waitpid interrupted, retrying\n");
630 } else {
631 break;
632 }
633 }
634 if (got_pid != pid) {
635 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
636 (int) pid, (int) got_pid, strerror(errno));
637 return 1;
638 }
639
640 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700641 return 0;
642 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700643 return status; /* always nonzero */
644 }
645}
646
647int dexopt(const char *apk_path, uid_t uid, int is_public)
648{
649 struct utimbuf ut;
650 struct stat apk_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700651 char out_path[PKG_PATH_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700652 char dexopt_flags[PROPERTY_VALUE_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700653 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700654 char *end;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700655 int res, zip_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700656
Mike Lockwood94afecf2012-10-24 10:45:23 -0700657 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
658 return -1;
659 }
660
661 /* platform-specific flags affecting optimization and verification */
662 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700663 ALOGV("dalvik.vm.dexopt_flags=%s\n", dexopt_flags);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700664
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700665 /* The command to run depend ones the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom0c05d3a2014-01-30 13:14:01 -0800666 property_get("persist.sys.dalvik.vm.lib.1", persist_sys_dalvik_vm_lib, "libdvm.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700667
668 /* Before anything else: is there a .odex file? If so, we have
669 * precompiled the apk and there is nothing to do here.
670 */
671 sprintf(out_path, "%s%s", apk_path, ".odex");
672 if (stat(out_path, &dex_stat) == 0) {
673 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700674 }
675
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700676 if (create_cache_path(out_path, apk_path)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700677 return -1;
678 }
679
680 memset(&apk_stat, 0, sizeof(apk_stat));
681 stat(apk_path, &apk_stat);
682
683 zip_fd = open(apk_path, O_RDONLY, 0);
684 if (zip_fd < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700685 ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700686 return -1;
687 }
688
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700689 unlink(out_path);
690 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
691 if (out_fd < 0) {
692 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700693 goto fail;
694 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700695 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700696 S_IRUSR|S_IWUSR|S_IRGRP |
697 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700698 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700699 goto fail;
700 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700701 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
702 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700703 goto fail;
704 }
705
706 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
707
708 pid_t pid;
709 pid = fork();
710 if (pid == 0) {
711 /* child -- drop privileges before continuing */
712 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700713 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700714 exit(64);
715 }
716 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700717 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700718 exit(65);
719 }
720 // drop capabilities
721 struct __user_cap_header_struct capheader;
722 struct __user_cap_data_struct capdata[2];
723 memset(&capheader, 0, sizeof(capheader));
724 memset(&capdata, 0, sizeof(capdata));
725 capheader.version = _LINUX_CAPABILITY_VERSION_3;
726 if (capset(&capheader, &capdata[0]) < 0) {
727 ALOGE("capset failed: %s\n", strerror(errno));
728 exit(66);
729 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700730 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
731 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700732 exit(67);
733 }
734
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700735 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700736 run_dexopt(zip_fd, out_fd, apk_path, out_path, dexopt_flags);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700737 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700738 run_dex2oat(zip_fd, out_fd, apk_path, out_path, dexopt_flags);
739 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700740 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700741 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700742 exit(68); /* only get here on exec failure */
743 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100744 res = wait_child(pid);
745 if (res == 0) {
746 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
747 } else {
748 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", apk_path, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700749 goto fail;
750 }
751 }
752
753 ut.actime = apk_stat.st_atime;
754 ut.modtime = apk_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700755 utime(out_path, &ut);
756
757 close(out_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700758 close(zip_fd);
759 return 0;
760
761fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700762 if (out_fd >= 0) {
763 close(out_fd);
764 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700765 }
766 if (zip_fd >= 0) {
767 close(zip_fd);
768 }
769 return -1;
770}
771
772void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
773 struct stat* statbuf)
774{
775 while (path[basepos] != 0) {
776 if (path[basepos] == '/') {
777 path[basepos] = 0;
778 if (lstat(path, statbuf) < 0) {
779 ALOGV("Making directory: %s\n", path);
780 if (mkdir(path, mode) == 0) {
781 chown(path, uid, gid);
782 } else {
783 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
784 }
785 }
786 path[basepos] = '/';
787 basepos++;
788 }
789 basepos++;
790 }
791}
792
793int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
794 int dstuid, int dstgid, struct stat* statbuf)
795{
796 DIR *d;
797 struct dirent *de;
798 int res;
799
800 int srcend = strlen(srcpath);
801 int dstend = strlen(dstpath);
802
803 if (lstat(srcpath, statbuf) < 0) {
804 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
805 return 1;
806 }
807
808 if ((statbuf->st_mode&S_IFDIR) == 0) {
809 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
810 dstuid, dstgid, statbuf);
811 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
812 if (rename(srcpath, dstpath) >= 0) {
813 if (chown(dstpath, dstuid, dstgid) < 0) {
814 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
815 unlink(dstpath);
816 return 1;
817 }
818 } else {
819 ALOGW("Unable to rename %s to %s: %s\n",
820 srcpath, dstpath, strerror(errno));
821 return 1;
822 }
823 return 0;
824 }
825
826 d = opendir(srcpath);
827 if (d == NULL) {
828 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
829 return 1;
830 }
831
832 res = 0;
833
834 while ((de = readdir(d))) {
835 const char *name = de->d_name;
836 /* always skip "." and ".." */
837 if (name[0] == '.') {
838 if (name[1] == 0) continue;
839 if ((name[1] == '.') && (name[2] == 0)) continue;
840 }
841
842 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
843 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
844 continue;
845 }
846
847 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
848 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
849 continue;
850 }
851
852 srcpath[srcend] = dstpath[dstend] = '/';
853 strcpy(srcpath+srcend+1, name);
854 strcpy(dstpath+dstend+1, name);
855
856 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
857 res = 1;
858 }
859
860 // Note: we will be leaving empty directories behind in srcpath,
861 // but that is okay, the package manager will be erasing all of the
862 // data associated with .apks that disappear.
863
864 srcpath[srcend] = dstpath[dstend] = 0;
865 }
866
867 closedir(d);
868 return res;
869}
870
871int movefiles()
872{
873 DIR *d;
874 int dfd, subfd;
875 struct dirent *de;
876 struct stat s;
877 char buf[PKG_PATH_MAX+1];
878 int bufp, bufe, bufi, readlen;
879
880 char srcpkg[PKG_NAME_MAX];
881 char dstpkg[PKG_NAME_MAX];
882 char srcpath[PKG_PATH_MAX];
883 char dstpath[PKG_PATH_MAX];
884 int dstuid=-1, dstgid=-1;
885 int hasspace;
886
887 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
888 if (d == NULL) {
889 goto done;
890 }
891 dfd = dirfd(d);
892
893 /* Iterate through all files in the directory, executing the
894 * file movements requested there-in.
895 */
896 while ((de = readdir(d))) {
897 const char *name = de->d_name;
898
899 if (de->d_type == DT_DIR) {
900 continue;
901 } else {
902 subfd = openat(dfd, name, O_RDONLY);
903 if (subfd < 0) {
904 ALOGW("Unable to open update commands at %s%s\n",
905 UPDATE_COMMANDS_DIR_PREFIX, name);
906 continue;
907 }
908
909 bufp = 0;
910 bufe = 0;
911 buf[PKG_PATH_MAX] = 0;
912 srcpkg[0] = dstpkg[0] = 0;
913 while (1) {
914 bufi = bufp;
915 while (bufi < bufe && buf[bufi] != '\n') {
916 bufi++;
917 }
918 if (bufi < bufe) {
919 buf[bufi] = 0;
920 ALOGV("Processing line: %s\n", buf+bufp);
921 hasspace = 0;
922 while (bufp < bufi && isspace(buf[bufp])) {
923 hasspace = 1;
924 bufp++;
925 }
926 if (buf[bufp] == '#' || bufp == bufi) {
927 // skip comments and empty lines.
928 } else if (hasspace) {
929 if (dstpkg[0] == 0) {
930 ALOGW("Path before package line in %s%s: %s\n",
931 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
932 } else if (srcpkg[0] == 0) {
933 // Skip -- source package no longer exists.
934 } else {
935 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
936 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
937 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
938 movefileordir(srcpath, dstpath,
939 strlen(dstpath)-strlen(buf+bufp),
940 dstuid, dstgid, &s);
941 }
942 }
943 } else {
944 char* div = strchr(buf+bufp, ':');
945 if (div == NULL) {
946 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
947 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
948 } else {
949 *div = 0;
950 div++;
951 if (strlen(buf+bufp) < PKG_NAME_MAX) {
952 strcpy(dstpkg, buf+bufp);
953 } else {
954 srcpkg[0] = dstpkg[0] = 0;
955 ALOGW("Package name too long in %s%s: %s\n",
956 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
957 }
958 if (strlen(div) < PKG_NAME_MAX) {
959 strcpy(srcpkg, div);
960 } else {
961 srcpkg[0] = dstpkg[0] = 0;
962 ALOGW("Package name too long in %s%s: %s\n",
963 UPDATE_COMMANDS_DIR_PREFIX, name, div);
964 }
965 if (srcpkg[0] != 0) {
966 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
967 if (lstat(srcpath, &s) < 0) {
968 // Package no longer exists -- skip.
969 srcpkg[0] = 0;
970 }
971 } else {
972 srcpkg[0] = 0;
973 ALOGW("Can't create path %s in %s%s\n",
974 div, UPDATE_COMMANDS_DIR_PREFIX, name);
975 }
976 if (srcpkg[0] != 0) {
977 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
978 if (lstat(dstpath, &s) == 0) {
979 dstuid = s.st_uid;
980 dstgid = s.st_gid;
981 } else {
982 // Destination package doesn't
983 // exist... due to original-package,
984 // this is normal, so don't be
985 // noisy about it.
986 srcpkg[0] = 0;
987 }
988 } else {
989 srcpkg[0] = 0;
990 ALOGW("Can't create path %s in %s%s\n",
991 div, UPDATE_COMMANDS_DIR_PREFIX, name);
992 }
993 }
994 ALOGV("Transfering from %s to %s: uid=%d\n",
995 srcpkg, dstpkg, dstuid);
996 }
997 }
998 }
999 bufp = bufi+1;
1000 } else {
1001 if (bufp == 0) {
1002 if (bufp < bufe) {
1003 ALOGW("Line too long in %s%s, skipping: %s\n",
1004 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1005 }
1006 } else if (bufp < bufe) {
1007 memcpy(buf, buf+bufp, bufe-bufp);
1008 bufe -= bufp;
1009 bufp = 0;
1010 }
1011 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1012 if (readlen < 0) {
1013 ALOGW("Failure reading update commands in %s%s: %s\n",
1014 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1015 break;
1016 } else if (readlen == 0) {
1017 break;
1018 }
1019 bufe += readlen;
1020 buf[bufe] = 0;
1021 ALOGV("Read buf: %s\n", buf);
1022 }
1023 }
1024 close(subfd);
1025 }
1026 }
1027 closedir(d);
1028done:
1029 return 0;
1030}
1031
1032int linklib(const char* pkgname, const char* asecLibDir, int userId)
1033{
1034 char pkgdir[PKG_PATH_MAX];
1035 char libsymlink[PKG_PATH_MAX];
1036 struct stat s, libStat;
1037 int rc = 0;
1038
1039 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1040 ALOGE("cannot create package path\n");
1041 return -1;
1042 }
1043 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1044 ALOGE("cannot create package lib symlink origin path\n");
1045 return -1;
1046 }
1047
1048 if (stat(pkgdir, &s) < 0) return -1;
1049
1050 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1051 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1052 return -1;
1053 }
1054
1055 if (chmod(pkgdir, 0700) < 0) {
1056 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1057 rc = -1;
1058 goto out;
1059 }
1060
1061 if (lstat(libsymlink, &libStat) < 0) {
1062 if (errno != ENOENT) {
1063 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1064 rc = -1;
1065 goto out;
1066 }
1067 } else {
1068 if (S_ISDIR(libStat.st_mode)) {
1069 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1070 rc = -1;
1071 goto out;
1072 }
1073 } else if (S_ISLNK(libStat.st_mode)) {
1074 if (unlink(libsymlink) < 0) {
1075 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1076 rc = -1;
1077 goto out;
1078 }
1079 }
1080 }
1081
1082 if (symlink(asecLibDir, libsymlink) < 0) {
1083 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1084 strerror(errno));
1085 rc = -errno;
1086 goto out;
1087 }
1088
1089out:
1090 if (chmod(pkgdir, s.st_mode) < 0) {
1091 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1092 rc = -errno;
1093 }
1094
1095 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1096 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1097 return -errno;
1098 }
1099
1100 return rc;
1101}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001102
1103static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1104{
1105 static const char *IDMAP_BIN = "/system/bin/idmap";
1106 static const size_t MAX_INT_LEN = 32;
1107 char idmap_str[MAX_INT_LEN];
1108
1109 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1110
1111 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1112 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1113}
1114
1115// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1116// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1117static int flatten_path(const char *prefix, const char *suffix,
1118 const char *overlay_path, char *idmap_path, size_t N)
1119{
1120 if (overlay_path == NULL || idmap_path == NULL) {
1121 return -1;
1122 }
1123 const size_t len_overlay_path = strlen(overlay_path);
1124 // will access overlay_path + 1 further below; requires absolute path
1125 if (len_overlay_path < 2 || *overlay_path != '/') {
1126 return -1;
1127 }
1128 const size_t len_idmap_root = strlen(prefix);
1129 const size_t len_suffix = strlen(suffix);
1130 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1131 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1132 // additions below would cause overflow
1133 return -1;
1134 }
1135 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1136 return -1;
1137 }
1138 memset(idmap_path, 0, N);
1139 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1140 char *ch = idmap_path + len_idmap_root;
1141 while (*ch != '\0') {
1142 if (*ch == '/') {
1143 *ch = '@';
1144 }
1145 ++ch;
1146 }
1147 return 0;
1148}
1149
1150int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1151{
1152 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1153
1154 int idmap_fd = -1;
1155 char idmap_path[PATH_MAX];
1156
1157 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1158 idmap_path, sizeof(idmap_path)) == -1) {
1159 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1160 goto fail;
1161 }
1162
1163 unlink(idmap_path);
1164 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1165 if (idmap_fd < 0) {
1166 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1167 goto fail;
1168 }
1169 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1170 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1171 goto fail;
1172 }
1173 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1174 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1175 goto fail;
1176 }
1177
1178 pid_t pid;
1179 pid = fork();
1180 if (pid == 0) {
1181 /* child -- drop privileges before continuing */
1182 if (setgid(uid) != 0) {
1183 ALOGE("setgid(%d) failed during idmap\n", uid);
1184 exit(1);
1185 }
1186 if (setuid(uid) != 0) {
1187 ALOGE("setuid(%d) failed during idmap\n", uid);
1188 exit(1);
1189 }
1190 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1191 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1192 exit(1);
1193 }
1194
1195 run_idmap(target_apk, overlay_apk, idmap_fd);
1196 exit(1); /* only if exec call to idmap failed */
1197 } else {
1198 int status = wait_child(pid);
1199 if (status != 0) {
1200 ALOGE("idmap failed, status=0x%04x\n", status);
1201 goto fail;
1202 }
1203 }
1204
1205 close(idmap_fd);
1206 return 0;
1207fail:
1208 if (idmap_fd >= 0) {
1209 close(idmap_fd);
1210 unlink(idmap_path);
1211 }
1212 return -1;
1213}