blob: 2a547102eb466229ff4e4ffee7f6f11a1e1af6ca [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
17#include <linux/capability.h>
18#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
31int install(const char *pkgname, uid_t uid, gid_t gid)
32{
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
58 if (mkdir(pkgdir, 0751) < 0) {
59 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
60 return -1;
61 }
62 if (chmod(pkgdir, 0751) < 0) {
63 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
Mike Lockwood94afecf2012-10-24 10:45:23 -070094 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
95 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
111int uninstall(const char *pkgname, uid_t persona)
112{
113 char pkgdir[PKG_PATH_MAX];
114
115 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
116 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
176int delete_user_data(const char *pkgname, uid_t persona)
177{
178 char pkgdir[PKG_PATH_MAX];
179
180 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
181 return -1;
182
183 /* delete contents, excluding "lib", but not the directory itself */
184 return delete_dir_contents(pkgdir, 0, "lib");
185}
186
187int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
188{
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
195 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
196 return -1;
197 }
198 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, persona)) {
199 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
207 if (mkdir(pkgdir, 0751) < 0) {
208 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
209 return -errno;
210 }
211 if (chmod(pkgdir, 0751) < 0) {
212 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
Mike Lockwood94afecf2012-10-24 10:45:23 -0700248 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
249 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
265int delete_persona(uid_t persona)
266{
267 char data_path[PKG_PATH_MAX];
268 if (create_persona_path(data_path, persona)) {
269 return -1;
270 }
271 if (delete_dir_contents(data_path, 1, NULL)) {
272 return -1;
273 }
274
275 char media_path[PATH_MAX];
276 if (create_persona_media_path(media_path, (userid_t) persona) == -1) {
277 return -1;
278 }
279 if (delete_dir_contents(media_path, 1, NULL) == -1) {
280 return -1;
281 }
282
283 return 0;
284}
285
286int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy)
287{
288 char src_data_dir[PKG_PATH_MAX];
289 char pkg_path[PKG_PATH_MAX];
290 DIR *d;
291 struct dirent *de;
292 struct stat s;
293 uid_t uid;
294
295 if (create_persona_path(src_data_dir, src_persona)) {
296 return -1;
297 }
298
299 d = opendir(src_data_dir);
300 if (d != NULL) {
301 while ((de = readdir(d))) {
302 const char *name = de->d_name;
303
304 if (de->d_type == DT_DIR) {
305 int subfd;
306 /* always skip "." and ".." */
307 if (name[0] == '.') {
308 if (name[1] == 0) continue;
309 if ((name[1] == '.') && (name[2] == 0)) continue;
310 }
311 /* Create the full path to the package's data dir */
312 create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona);
313 /* Get the file stat */
314 if (stat(pkg_path, &s) < 0) continue;
315 /* Get the uid of the package */
316 ALOGI("Adding datadir for uid = %lu\n", s.st_uid);
317 uid = (uid_t) s.st_uid % PER_USER_RANGE;
318 /* Create the directory for the target */
319 make_user_data(name, uid + target_persona * PER_USER_RANGE,
320 target_persona);
321 }
322 }
323 closedir(d);
324 }
325
326 if (ensure_media_user_dirs((userid_t) target_persona) == -1) {
327 return -1;
328 }
329
330 return 0;
331}
332
333int delete_cache(const char *pkgname, uid_t persona)
334{
335 char cachedir[PKG_PATH_MAX];
336
337 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, persona))
338 return -1;
339
340 /* delete contents, not the directory, no exceptions */
341 return delete_dir_contents(cachedir, 0, 0);
342}
343
344/* Try to ensure free_size bytes of storage are available.
345 * Returns 0 on success.
346 * This is rather simple-minded because doing a full LRU would
347 * be potentially memory-intensive, and without atime it would
348 * also require that apps constantly modify file metadata even
349 * when just reading from the cache, which is pretty awful.
350 */
351int free_cache(int64_t free_size)
352{
353 cache_t* cache;
354 int64_t avail;
355 DIR *d;
356 struct dirent *de;
357 char tmpdir[PATH_MAX];
358 char *dirpos;
359
360 avail = data_disk_free();
361 if (avail < 0) return -1;
362
363 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
364 if (avail >= free_size) return 0;
365
366 cache = start_cache_collection();
367
368 // Collect cache files for primary user.
369 if (create_persona_path(tmpdir, 0) == 0) {
370 //ALOGI("adding cache files from %s\n", tmpdir);
371 add_cache_files(cache, tmpdir, "cache");
372 }
373
374 // Search for other users and add any cache files from them.
375 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
376 SECONDARY_USER_PREFIX);
377 dirpos = tmpdir + strlen(tmpdir);
378 d = opendir(tmpdir);
379 if (d != NULL) {
380 while ((de = readdir(d))) {
381 if (de->d_type == DT_DIR) {
382 const char *name = de->d_name;
383 /* always skip "." and ".." */
384 if (name[0] == '.') {
385 if (name[1] == 0) continue;
386 if ((name[1] == '.') && (name[2] == 0)) continue;
387 }
388 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
389 strcpy(dirpos, name);
390 //ALOGI("adding cache files from %s\n", tmpdir);
391 add_cache_files(cache, tmpdir, "cache");
392 } else {
393 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
394 }
395 }
396 }
397 closedir(d);
398 }
399
400 // Collect cache files on external storage for all users (if it is mounted as part
401 // of the internal storage).
402 strcpy(tmpdir, android_media_dir.path);
403 dirpos = tmpdir + strlen(tmpdir);
404 d = opendir(tmpdir);
405 if (d != NULL) {
406 while ((de = readdir(d))) {
407 if (de->d_type == DT_DIR) {
408 const char *name = de->d_name;
409 /* skip any dir that doesn't start with a number, so not a user */
410 if (name[0] < '0' || name[0] > '9') {
411 continue;
412 }
413 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
414 strcpy(dirpos, name);
415 if (lookup_media_dir(tmpdir, "Android") == 0
416 && lookup_media_dir(tmpdir, "data") == 0) {
417 //ALOGI("adding cache files from %s\n", tmpdir);
418 add_cache_files(cache, tmpdir, "cache");
419 }
420 } else {
421 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
422 }
423 }
424 }
425 closedir(d);
426 }
427
428 clear_cache_files(cache, free_size);
429 finish_cache_collection(cache);
430
431 return data_disk_free() >= free_size ? 0 : -1;
432}
433
434int move_dex(const char *src, const char *dst)
435{
436 char src_dex[PKG_PATH_MAX];
437 char dst_dex[PKG_PATH_MAX];
438
439 if (validate_apk_path(src)) return -1;
440 if (validate_apk_path(dst)) return -1;
441
442 if (create_cache_path(src_dex, src)) return -1;
443 if (create_cache_path(dst_dex, dst)) return -1;
444
445 ALOGV("move %s -> %s\n", src_dex, dst_dex);
446 if (rename(src_dex, dst_dex) < 0) {
447 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
448 return -1;
449 } else {
450 return 0;
451 }
452}
453
454int rm_dex(const char *path)
455{
456 char dex_path[PKG_PATH_MAX];
457
458 if (validate_apk_path(path)) return -1;
459 if (create_cache_path(dex_path, path)) return -1;
460
461 ALOGV("unlink %s\n", dex_path);
462 if (unlink(dex_path) < 0) {
463 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
464 return -1;
465 } else {
466 return 0;
467 }
468}
469
470int get_size(const char *pkgname, int persona, const char *apkpath,
471 const char *fwdlock_apkpath, const char *asecpath,
472 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
473 int64_t* _asecsize)
474{
475 DIR *d;
476 int dfd;
477 struct dirent *de;
478 struct stat s;
479 char path[PKG_PATH_MAX];
480
481 int64_t codesize = 0;
482 int64_t datasize = 0;
483 int64_t cachesize = 0;
484 int64_t asecsize = 0;
485
486 /* count the source apk as code -- but only if it's not
487 * on the /system partition and its not on the sdcard.
488 */
489 if (validate_system_app_path(apkpath) &&
490 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
491 if (stat(apkpath, &s) == 0) {
492 codesize += stat_size(&s);
493 }
494 }
495 /* count the forward locked apk as code if it is given
496 */
497 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
498 if (stat(fwdlock_apkpath, &s) == 0) {
499 codesize += stat_size(&s);
500 }
501 }
502 /* count the cached dexfile as code */
503 if (!create_cache_path(path, apkpath)) {
504 if (stat(path, &s) == 0) {
505 codesize += stat_size(&s);
506 }
507 }
508
509 /* add in size of any libraries */
510 if (!create_pkg_path_in_dir(path, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
511 d = opendir(path);
512 if (d != NULL) {
513 dfd = dirfd(d);
514 codesize += calculate_dir_size(dfd);
515 closedir(d);
516 }
517 }
518
519 /* compute asec size if it is given
520 */
521 if (asecpath != NULL && asecpath[0] != '!') {
522 if (stat(asecpath, &s) == 0) {
523 asecsize += stat_size(&s);
524 }
525 }
526
527 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) {
528 goto done;
529 }
530
531 d = opendir(path);
532 if (d == NULL) {
533 goto done;
534 }
535 dfd = dirfd(d);
536
537 /* most stuff in the pkgdir is data, except for the "cache"
538 * directory and below, which is cache, and the "lib" directory
539 * and below, which is code...
540 */
541 while ((de = readdir(d))) {
542 const char *name = de->d_name;
543
544 if (de->d_type == DT_DIR) {
545 int subfd;
546 int64_t statsize = 0;
547 int64_t dirsize = 0;
548 /* always skip "." and ".." */
549 if (name[0] == '.') {
550 if (name[1] == 0) continue;
551 if ((name[1] == '.') && (name[2] == 0)) continue;
552 }
553 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
554 statsize = stat_size(&s);
555 }
556 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
557 if (subfd >= 0) {
558 dirsize = calculate_dir_size(subfd);
559 }
560 if(!strcmp(name,"lib")) {
561 codesize += dirsize + statsize;
562 } else if(!strcmp(name,"cache")) {
563 cachesize += dirsize + statsize;
564 } else {
565 datasize += dirsize + statsize;
566 }
567 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
568 // This is the symbolic link to the application's library
569 // code. We'll count this as code instead of data, since
570 // it is not something that the app creates.
571 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
572 codesize += stat_size(&s);
573 }
574 } else {
575 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
576 datasize += stat_size(&s);
577 }
578 }
579 }
580 closedir(d);
581done:
582 *_codesize = codesize;
583 *_datasize = datasize;
584 *_cachesize = cachesize;
585 *_asecsize = asecsize;
586 return 0;
587}
588
589
590/* a simpler version of dexOptGenerateCacheFileName() */
591int create_cache_path(char path[PKG_PATH_MAX], const char *src)
592{
593 char *tmp;
594 int srclen;
595 int dstlen;
596
597 srclen = strlen(src);
598
599 /* demand that we are an absolute path */
600 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
601 return -1;
602 }
603
604 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
605 return -1;
606 }
607
608 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
609 strlen(DALVIK_CACHE_POSTFIX) + 1;
610
611 if (dstlen > PKG_PATH_MAX) {
612 return -1;
613 }
614
615 sprintf(path,"%s%s%s",
616 DALVIK_CACHE_PREFIX,
617 src + 1, /* skip the leading / */
618 DALVIK_CACHE_POSTFIX);
619
620 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
621 if (*tmp == '/') {
622 *tmp = '@';
623 }
624 }
625
626 return 0;
627}
628
629static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
630 const char* dexopt_flags)
631{
632 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
633 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
634 char zip_num[MAX_INT_LEN];
635 char odex_num[MAX_INT_LEN];
636
637 sprintf(zip_num, "%d", zip_fd);
638 sprintf(odex_num, "%d", odex_fd);
639
640 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
641 dexopt_flags, (char*) NULL);
642 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
643}
644
645static int wait_dexopt(pid_t pid, const char* apk_path)
646{
647 int status;
648 pid_t got_pid;
649
650 /*
651 * Wait for the optimization process to finish.
652 */
653 while (1) {
654 got_pid = waitpid(pid, &status, 0);
655 if (got_pid == -1 && errno == EINTR) {
656 printf("waitpid interrupted, retrying\n");
657 } else {
658 break;
659 }
660 }
661 if (got_pid != pid) {
662 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
663 (int) pid, (int) got_pid, strerror(errno));
664 return 1;
665 }
666
667 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
668 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
669 return 0;
670 } else {
671 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
672 apk_path, status);
673 return status; /* always nonzero */
674 }
675}
676
677int dexopt(const char *apk_path, uid_t uid, int is_public)
678{
679 struct utimbuf ut;
680 struct stat apk_stat, dex_stat;
681 char dex_path[PKG_PATH_MAX];
682 char dexopt_flags[PROPERTY_VALUE_MAX];
683 char *end;
684 int res, zip_fd=-1, odex_fd=-1;
685
686 /* Before anything else: is there a .odex file? If so, we have
687 * pre-optimized the apk and there is nothing to do here.
688 */
689 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
690 return -1;
691 }
692
693 /* platform-specific flags affecting optimization and verification */
694 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
695
696 strcpy(dex_path, apk_path);
697 end = strrchr(dex_path, '.');
698 if (end != NULL) {
699 strcpy(end, ".odex");
700 if (stat(dex_path, &dex_stat) == 0) {
701 return 0;
702 }
703 }
704
705 if (create_cache_path(dex_path, apk_path)) {
706 return -1;
707 }
708
709 memset(&apk_stat, 0, sizeof(apk_stat));
710 stat(apk_path, &apk_stat);
711
712 zip_fd = open(apk_path, O_RDONLY, 0);
713 if (zip_fd < 0) {
714 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
715 return -1;
716 }
717
718 unlink(dex_path);
719 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
720 if (odex_fd < 0) {
721 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
722 goto fail;
723 }
724 if (fchmod(odex_fd,
725 S_IRUSR|S_IWUSR|S_IRGRP |
726 (is_public ? S_IROTH : 0)) < 0) {
727 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
728 goto fail;
729 }
730 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
731 ALOGE("dexopt cannot chown '%s'\n", dex_path);
732 goto fail;
733 }
734
735 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
736
737 pid_t pid;
738 pid = fork();
739 if (pid == 0) {
740 /* child -- drop privileges before continuing */
741 if (setgid(uid) != 0) {
742 ALOGE("setgid(%d) failed during dexopt\n", uid);
743 exit(64);
744 }
745 if (setuid(uid) != 0) {
746 ALOGE("setuid(%d) during dexopt\n", uid);
747 exit(65);
748 }
749 // drop capabilities
750 struct __user_cap_header_struct capheader;
751 struct __user_cap_data_struct capdata[2];
752 memset(&capheader, 0, sizeof(capheader));
753 memset(&capdata, 0, sizeof(capdata));
754 capheader.version = _LINUX_CAPABILITY_VERSION_3;
755 if (capset(&capheader, &capdata[0]) < 0) {
756 ALOGE("capset failed: %s\n", strerror(errno));
757 exit(66);
758 }
759 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
760 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
761 exit(67);
762 }
763
764 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
765 exit(68); /* only get here on exec failure */
766 } else {
767 res = wait_dexopt(pid, apk_path);
768 if (res != 0) {
769 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
770 goto fail;
771 }
772 }
773
774 ut.actime = apk_stat.st_atime;
775 ut.modtime = apk_stat.st_mtime;
776 utime(dex_path, &ut);
777
778 close(odex_fd);
779 close(zip_fd);
780 return 0;
781
782fail:
783 if (odex_fd >= 0) {
784 close(odex_fd);
785 unlink(dex_path);
786 }
787 if (zip_fd >= 0) {
788 close(zip_fd);
789 }
790 return -1;
791}
792
793void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
794 struct stat* statbuf)
795{
796 while (path[basepos] != 0) {
797 if (path[basepos] == '/') {
798 path[basepos] = 0;
799 if (lstat(path, statbuf) < 0) {
800 ALOGV("Making directory: %s\n", path);
801 if (mkdir(path, mode) == 0) {
802 chown(path, uid, gid);
803 } else {
804 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
805 }
806 }
807 path[basepos] = '/';
808 basepos++;
809 }
810 basepos++;
811 }
812}
813
814int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
815 int dstuid, int dstgid, struct stat* statbuf)
816{
817 DIR *d;
818 struct dirent *de;
819 int res;
820
821 int srcend = strlen(srcpath);
822 int dstend = strlen(dstpath);
823
824 if (lstat(srcpath, statbuf) < 0) {
825 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
826 return 1;
827 }
828
829 if ((statbuf->st_mode&S_IFDIR) == 0) {
830 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
831 dstuid, dstgid, statbuf);
832 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
833 if (rename(srcpath, dstpath) >= 0) {
834 if (chown(dstpath, dstuid, dstgid) < 0) {
835 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
836 unlink(dstpath);
837 return 1;
838 }
839 } else {
840 ALOGW("Unable to rename %s to %s: %s\n",
841 srcpath, dstpath, strerror(errno));
842 return 1;
843 }
844 return 0;
845 }
846
847 d = opendir(srcpath);
848 if (d == NULL) {
849 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
850 return 1;
851 }
852
853 res = 0;
854
855 while ((de = readdir(d))) {
856 const char *name = de->d_name;
857 /* always skip "." and ".." */
858 if (name[0] == '.') {
859 if (name[1] == 0) continue;
860 if ((name[1] == '.') && (name[2] == 0)) continue;
861 }
862
863 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
864 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
865 continue;
866 }
867
868 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
869 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
870 continue;
871 }
872
873 srcpath[srcend] = dstpath[dstend] = '/';
874 strcpy(srcpath+srcend+1, name);
875 strcpy(dstpath+dstend+1, name);
876
877 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
878 res = 1;
879 }
880
881 // Note: we will be leaving empty directories behind in srcpath,
882 // but that is okay, the package manager will be erasing all of the
883 // data associated with .apks that disappear.
884
885 srcpath[srcend] = dstpath[dstend] = 0;
886 }
887
888 closedir(d);
889 return res;
890}
891
892int movefiles()
893{
894 DIR *d;
895 int dfd, subfd;
896 struct dirent *de;
897 struct stat s;
898 char buf[PKG_PATH_MAX+1];
899 int bufp, bufe, bufi, readlen;
900
901 char srcpkg[PKG_NAME_MAX];
902 char dstpkg[PKG_NAME_MAX];
903 char srcpath[PKG_PATH_MAX];
904 char dstpath[PKG_PATH_MAX];
905 int dstuid=-1, dstgid=-1;
906 int hasspace;
907
908 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
909 if (d == NULL) {
910 goto done;
911 }
912 dfd = dirfd(d);
913
914 /* Iterate through all files in the directory, executing the
915 * file movements requested there-in.
916 */
917 while ((de = readdir(d))) {
918 const char *name = de->d_name;
919
920 if (de->d_type == DT_DIR) {
921 continue;
922 } else {
923 subfd = openat(dfd, name, O_RDONLY);
924 if (subfd < 0) {
925 ALOGW("Unable to open update commands at %s%s\n",
926 UPDATE_COMMANDS_DIR_PREFIX, name);
927 continue;
928 }
929
930 bufp = 0;
931 bufe = 0;
932 buf[PKG_PATH_MAX] = 0;
933 srcpkg[0] = dstpkg[0] = 0;
934 while (1) {
935 bufi = bufp;
936 while (bufi < bufe && buf[bufi] != '\n') {
937 bufi++;
938 }
939 if (bufi < bufe) {
940 buf[bufi] = 0;
941 ALOGV("Processing line: %s\n", buf+bufp);
942 hasspace = 0;
943 while (bufp < bufi && isspace(buf[bufp])) {
944 hasspace = 1;
945 bufp++;
946 }
947 if (buf[bufp] == '#' || bufp == bufi) {
948 // skip comments and empty lines.
949 } else if (hasspace) {
950 if (dstpkg[0] == 0) {
951 ALOGW("Path before package line in %s%s: %s\n",
952 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
953 } else if (srcpkg[0] == 0) {
954 // Skip -- source package no longer exists.
955 } else {
956 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
957 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
958 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
959 movefileordir(srcpath, dstpath,
960 strlen(dstpath)-strlen(buf+bufp),
961 dstuid, dstgid, &s);
962 }
963 }
964 } else {
965 char* div = strchr(buf+bufp, ':');
966 if (div == NULL) {
967 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
968 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
969 } else {
970 *div = 0;
971 div++;
972 if (strlen(buf+bufp) < PKG_NAME_MAX) {
973 strcpy(dstpkg, buf+bufp);
974 } else {
975 srcpkg[0] = dstpkg[0] = 0;
976 ALOGW("Package name too long in %s%s: %s\n",
977 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
978 }
979 if (strlen(div) < PKG_NAME_MAX) {
980 strcpy(srcpkg, div);
981 } else {
982 srcpkg[0] = dstpkg[0] = 0;
983 ALOGW("Package name too long in %s%s: %s\n",
984 UPDATE_COMMANDS_DIR_PREFIX, name, div);
985 }
986 if (srcpkg[0] != 0) {
987 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
988 if (lstat(srcpath, &s) < 0) {
989 // Package no longer exists -- skip.
990 srcpkg[0] = 0;
991 }
992 } else {
993 srcpkg[0] = 0;
994 ALOGW("Can't create path %s in %s%s\n",
995 div, UPDATE_COMMANDS_DIR_PREFIX, name);
996 }
997 if (srcpkg[0] != 0) {
998 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
999 if (lstat(dstpath, &s) == 0) {
1000 dstuid = s.st_uid;
1001 dstgid = s.st_gid;
1002 } else {
1003 // Destination package doesn't
1004 // exist... due to original-package,
1005 // this is normal, so don't be
1006 // noisy about it.
1007 srcpkg[0] = 0;
1008 }
1009 } else {
1010 srcpkg[0] = 0;
1011 ALOGW("Can't create path %s in %s%s\n",
1012 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1013 }
1014 }
1015 ALOGV("Transfering from %s to %s: uid=%d\n",
1016 srcpkg, dstpkg, dstuid);
1017 }
1018 }
1019 }
1020 bufp = bufi+1;
1021 } else {
1022 if (bufp == 0) {
1023 if (bufp < bufe) {
1024 ALOGW("Line too long in %s%s, skipping: %s\n",
1025 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1026 }
1027 } else if (bufp < bufe) {
1028 memcpy(buf, buf+bufp, bufe-bufp);
1029 bufe -= bufp;
1030 bufp = 0;
1031 }
1032 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1033 if (readlen < 0) {
1034 ALOGW("Failure reading update commands in %s%s: %s\n",
1035 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1036 break;
1037 } else if (readlen == 0) {
1038 break;
1039 }
1040 bufe += readlen;
1041 buf[bufe] = 0;
1042 ALOGV("Read buf: %s\n", buf);
1043 }
1044 }
1045 close(subfd);
1046 }
1047 }
1048 closedir(d);
1049done:
1050 return 0;
1051}
1052
1053int linklib(const char* pkgname, const char* asecLibDir, int userId)
1054{
1055 char pkgdir[PKG_PATH_MAX];
1056 char libsymlink[PKG_PATH_MAX];
1057 struct stat s, libStat;
1058 int rc = 0;
1059
1060 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1061 ALOGE("cannot create package path\n");
1062 return -1;
1063 }
1064 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1065 ALOGE("cannot create package lib symlink origin path\n");
1066 return -1;
1067 }
1068
1069 if (stat(pkgdir, &s) < 0) return -1;
1070
1071 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1072 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1073 return -1;
1074 }
1075
1076 if (chmod(pkgdir, 0700) < 0) {
1077 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1078 rc = -1;
1079 goto out;
1080 }
1081
1082 if (lstat(libsymlink, &libStat) < 0) {
1083 if (errno != ENOENT) {
1084 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1085 rc = -1;
1086 goto out;
1087 }
1088 } else {
1089 if (S_ISDIR(libStat.st_mode)) {
1090 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1091 rc = -1;
1092 goto out;
1093 }
1094 } else if (S_ISLNK(libStat.st_mode)) {
1095 if (unlink(libsymlink) < 0) {
1096 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1097 rc = -1;
1098 goto out;
1099 }
1100 }
1101 }
1102
1103 if (symlink(asecLibDir, libsymlink) < 0) {
1104 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1105 strerror(errno));
1106 rc = -errno;
1107 goto out;
1108 }
1109
1110out:
1111 if (chmod(pkgdir, s.st_mode) < 0) {
1112 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1113 rc = -errno;
1114 }
1115
1116 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1117 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1118 return -errno;
1119 }
1120
1121 return rc;
1122}