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