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