blob: b8ba3f6db55809975c0ae3752fd688f85ef59e8c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 "installd.h"
18
Oscar Montemayora8529f62009-11-18 10:14:20 -080019int install(const char *pkgname, int encrypted_fs_flag, uid_t uid, gid_t gid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020{
21 char pkgdir[PKG_PATH_MAX];
22 char libdir[PKG_PATH_MAX];
23
24 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
25 LOGE("invalid uid/gid: %d %d\n", uid, gid);
26 return -1;
27
28 }
Oscar Montemayora8529f62009-11-18 10:14:20 -080029
30 if (encrypted_fs_flag == USE_UNENCRYPTED_FS) {
31 if (create_pkg_path(pkgdir, PKG_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX))
32 return -1;
33 if (create_pkg_path(libdir, PKG_LIB_PREFIX, pkgname, PKG_LIB_POSTFIX))
34 return -1;
35 } else {
36 if (create_pkg_path(pkgdir, PKG_SEC_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX))
37 return -1;
38 if (create_pkg_path(libdir, PKG_SEC_LIB_PREFIX, pkgname, PKG_LIB_POSTFIX))
39 return -1;
40 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
David 'Digit' Turner0dd50e62010-02-09 19:02:38 -080042 if (mkdir(pkgdir, 0751) < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 LOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
44 return -errno;
45 }
46 if (chown(pkgdir, uid, gid) < 0) {
47 LOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
48 unlink(pkgdir);
49 return -errno;
50 }
51 if (mkdir(libdir, 0755) < 0) {
52 LOGE("cannot create dir '%s': %s\n", libdir, strerror(errno));
53 unlink(pkgdir);
54 return -errno;
55 }
56 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
57 LOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
58 unlink(libdir);
59 unlink(pkgdir);
60 return -errno;
61 }
62 return 0;
63}
64
Oscar Montemayora8529f62009-11-18 10:14:20 -080065int uninstall(const char *pkgname, int encrypted_fs_flag)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066{
67 char pkgdir[PKG_PATH_MAX];
68
Oscar Montemayora8529f62009-11-18 10:14:20 -080069 if (encrypted_fs_flag == USE_UNENCRYPTED_FS) {
70 if (create_pkg_path(pkgdir, PKG_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX))
71 return -1;
72 } else {
73 if (create_pkg_path(pkgdir, PKG_SEC_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX))
74 return -1;
75 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77 /* delete contents AND directory, no exceptions */
78 return delete_dir_contents(pkgdir, 1, 0);
79}
80
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080081int renamepkg(const char *oldpkgname, const char *newpkgname, int encrypted_fs_flag)
82{
83 char oldpkgdir[PKG_PATH_MAX];
84 char newpkgdir[PKG_PATH_MAX];
85
86 if (encrypted_fs_flag == USE_UNENCRYPTED_FS) {
87 if (create_pkg_path(oldpkgdir, PKG_DIR_PREFIX, oldpkgname, PKG_DIR_POSTFIX))
88 return -1;
89 if (create_pkg_path(newpkgdir, PKG_DIR_PREFIX, newpkgname, PKG_DIR_POSTFIX))
90 return -1;
91 } else {
92 if (create_pkg_path(oldpkgdir, PKG_SEC_DIR_PREFIX, oldpkgname, PKG_DIR_POSTFIX))
93 return -1;
94 if (create_pkg_path(newpkgdir, PKG_SEC_DIR_PREFIX, newpkgname, PKG_DIR_POSTFIX))
95 return -1;
96 }
97
98 if (rename(oldpkgdir, newpkgdir) < 0) {
99 LOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
100 return -errno;
101 }
102 return 0;
103}
104
Oscar Montemayora8529f62009-11-18 10:14:20 -0800105int delete_user_data(const char *pkgname, int encrypted_fs_flag)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106{
107 char pkgdir[PKG_PATH_MAX];
108
Oscar Montemayora8529f62009-11-18 10:14:20 -0800109 if (encrypted_fs_flag == USE_UNENCRYPTED_FS) {
110 if (create_pkg_path(pkgdir, PKG_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX))
111 return -1;
112 } else {
113 if (create_pkg_path(pkgdir, PKG_SEC_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX))
114 return -1;
115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
117 /* delete contents, excluding "lib", but not the directory itself */
118 return delete_dir_contents(pkgdir, 0, "lib");
119}
120
Oscar Montemayora8529f62009-11-18 10:14:20 -0800121int delete_cache(const char *pkgname, int encrypted_fs_flag)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122{
123 char cachedir[PKG_PATH_MAX];
124
Oscar Montemayora8529f62009-11-18 10:14:20 -0800125 if (encrypted_fs_flag == USE_UNENCRYPTED_FS) {
126 if (create_pkg_path(cachedir, CACHE_DIR_PREFIX, pkgname, CACHE_DIR_POSTFIX))
127 return -1;
128 } else {
129 if (create_pkg_path(cachedir, CACHE_SEC_DIR_PREFIX, pkgname, CACHE_DIR_POSTFIX))
130 return -1;
131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 /* delete contents, not the directory, no exceptions */
134 return delete_dir_contents(cachedir, 0, 0);
135}
136
Oscar Montemayora8529f62009-11-18 10:14:20 -0800137/* TODO(oam): depending on use case (ecryptfs or dmcrypt)
138 * change implementation
139 */
140static int disk_free()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141{
142 struct statfs sfs;
143 if (statfs(PKG_DIR_PREFIX, &sfs) == 0) {
144 return sfs.f_bavail * sfs.f_bsize;
145 } else {
146 return -1;
147 }
148}
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150/* Try to ensure free_size bytes of storage are available.
151 * Returns 0 on success.
152 * This is rather simple-minded because doing a full LRU would
153 * be potentially memory-intensive, and without atime it would
154 * also require that apps constantly modify file metadata even
155 * when just reading from the cache, which is pretty awful.
156 */
157int free_cache(int free_size)
158{
159 const char *name;
160 int dfd, subfd;
161 DIR *d;
162 struct dirent *de;
163 int avail;
164
165 avail = disk_free();
166 if (avail < 0) return -1;
167
168 LOGI("free_cache(%d) avail %d\n", free_size, avail);
169 if (avail >= free_size) return 0;
170
Oscar Montemayora8529f62009-11-18 10:14:20 -0800171 /* First try encrypted dir */
172 d = opendir(PKG_SEC_DIR_PREFIX);
173 if (d == NULL) {
174 LOGE("cannot open %s\n", PKG_SEC_DIR_PREFIX);
175 } else {
176 dfd = dirfd(d);
177
178 while ((de = readdir(d))) {
179 if (de->d_type != DT_DIR) continue;
180 name = de->d_name;
181
182 /* always skip "." and ".." */
183 if (name[0] == '.') {
184 if (name[1] == 0) continue;
185 if ((name[1] == '.') && (name[2] == 0)) continue;
186 }
187
188 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
189 if (subfd < 0) continue;
190
191 delete_dir_contents_fd(subfd, "cache");
192 close(subfd);
193
194 avail = disk_free();
195 if (avail >= free_size) {
196 closedir(d);
197 return 0;
198 }
199 }
200 closedir(d);
201 }
202
203 /* Next try unencrypted dir... */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 d = opendir(PKG_DIR_PREFIX);
205 if (d == NULL) {
206 LOGE("cannot open %s\n", PKG_DIR_PREFIX);
207 return -1;
208 }
209 dfd = dirfd(d);
210
211 while ((de = readdir(d))) {
212 if (de->d_type != DT_DIR) continue;
213 name = de->d_name;
214
Oscar Montemayora8529f62009-11-18 10:14:20 -0800215 /* always skip "." and ".." */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 if (name[0] == '.') {
217 if (name[1] == 0) continue;
218 if ((name[1] == '.') && (name[2] == 0)) continue;
219 }
220
221 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
222 if (subfd < 0) continue;
223
224 delete_dir_contents_fd(subfd, "cache");
225 close(subfd);
226
227 avail = disk_free();
228 if (avail >= free_size) {
229 closedir(d);
230 return 0;
231 }
232 }
233 closedir(d);
Oscar Montemayora8529f62009-11-18 10:14:20 -0800234
235 /* Fail case - not possible to free space */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 return -1;
237}
238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239/* used by move_dex, rm_dex, etc to ensure that the provided paths
240 * don't point anywhere other than at the APK_DIR_PREFIX
241 */
242static int is_valid_apk_path(const char *path)
243{
244 int len = strlen(APK_DIR_PREFIX);
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800245int nosubdircheck = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 if (strncmp(path, APK_DIR_PREFIX, len)) {
247 len = strlen(PROTECTED_DIR_PREFIX);
248 if (strncmp(path, PROTECTED_DIR_PREFIX, len)) {
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800249 len = strlen(SDCARD_DIR_PREFIX);
250 if (strncmp(path, SDCARD_DIR_PREFIX, len)) {
251 LOGE("invalid apk path '%s' (bad prefix)\n", path);
252 return 0;
253 } else {
254 nosubdircheck = 1;
255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
257 }
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800258 if ((nosubdircheck != 1) && strchr(path + len, '/')) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 LOGE("invalid apk path '%s' (subdir?)\n", path);
260 return 0;
261 }
262 if (path[len] == '.') {
263 LOGE("invalid apk path '%s' (trickery)\n", path);
264 return 0;
265 }
266 return 1;
267}
268
269int move_dex(const char *src, const char *dst)
270{
271 char src_dex[PKG_PATH_MAX];
272 char dst_dex[PKG_PATH_MAX];
273
274 if (!is_valid_apk_path(src)) return -1;
275 if (!is_valid_apk_path(dst)) return -1;
276
277 if (create_cache_path(src_dex, src)) return -1;
278 if (create_cache_path(dst_dex, dst)) return -1;
279
280 LOGI("move %s -> %s\n", src_dex, dst_dex);
281 if (rename(src_dex, dst_dex) < 0) {
282 return -1;
283 } else {
284 return 0;
285 }
286}
287
288int rm_dex(const char *path)
289{
290 char dex_path[PKG_PATH_MAX];
291
292 if (!is_valid_apk_path(path)) return -1;
293 if (create_cache_path(dex_path, path)) return -1;
294
295 LOGI("unlink %s\n", dex_path);
296 if (unlink(dex_path) < 0) {
297 return -1;
298 } else {
299 return 0;
300 }
301}
302
303int protect(char *pkgname, gid_t gid)
304{
305 struct stat s;
306 char pkgpath[PKG_PATH_MAX];
307
308 if (gid < AID_SYSTEM) return -1;
309
310 if (create_pkg_path(pkgpath, PROTECTED_DIR_PREFIX, pkgname, ".apk"))
311 return -1;
312
313 if (stat(pkgpath, &s) < 0) return -1;
314
315 if (chown(pkgpath, s.st_uid, gid) < 0) {
316 LOGE("failed to chgrp '%s': %s\n", pkgpath, strerror(errno));
317 return -1;
318 }
319
320 if (chmod(pkgpath, S_IRUSR|S_IWUSR|S_IRGRP) < 0) {
321 LOGE("failed to chmod '%s': %s\n", pkgpath, strerror(errno));
322 return -1;
323 }
324
325 return 0;
326}
327
328static int stat_size(struct stat *s)
329{
330 int blksize = s->st_blksize;
331 int size = s->st_size;
332
333 if (blksize) {
334 /* round up to filesystem block size */
335 size = (size + blksize - 1) & (~(blksize - 1));
336 }
337
338 return size;
339}
340
341static int calculate_dir_size(int dfd)
342{
343 int size = 0;
344 struct stat s;
345 DIR *d;
346 struct dirent *de;
347
348 d = fdopendir(dfd);
349 if (d == NULL) {
350 close(dfd);
351 return 0;
352 }
353
354 while ((de = readdir(d))) {
355 const char *name = de->d_name;
356 if (de->d_type == DT_DIR) {
357 int subfd;
358 /* always skip "." and ".." */
359 if (name[0] == '.') {
360 if (name[1] == 0) continue;
361 if ((name[1] == '.') && (name[2] == 0)) continue;
362 }
363 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
364 if (subfd >= 0) {
365 size += calculate_dir_size(subfd);
366 }
367 } else {
368 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
369 size += stat_size(&s);
370 }
371 }
372 }
373 closedir(d);
374 return size;
375}
376
377int get_size(const char *pkgname, const char *apkpath,
378 const char *fwdlock_apkpath,
Oscar Montemayora8529f62009-11-18 10:14:20 -0800379 int *_codesize, int *_datasize, int *_cachesize, int encrypted_fs_flag)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380{
381 DIR *d;
382 int dfd;
383 struct dirent *de;
384 struct stat s;
385 char path[PKG_PATH_MAX];
386
387 int codesize = 0;
388 int datasize = 0;
389 int cachesize = 0;
390
391 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800392 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 */
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800394 if (strncmp(apkpath, "/system", 7) != 0 &&
395 strncmp(apkpath, SDCARD_DIR_PREFIX, 7) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 if (stat(apkpath, &s) == 0) {
397 codesize += stat_size(&s);
398 }
399 }
400 /* count the forward locked apk as code if it is given
401 */
402 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
403 if (stat(fwdlock_apkpath, &s) == 0) {
404 codesize += stat_size(&s);
405 }
406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 /* count the cached dexfile as code */
408 if (!create_cache_path(path, apkpath)) {
409 if (stat(path, &s) == 0) {
410 codesize += stat_size(&s);
411 }
412 }
413
Oscar Montemayora8529f62009-11-18 10:14:20 -0800414 if (encrypted_fs_flag == 0) {
415 if (create_pkg_path(path, PKG_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX)) {
416 goto done;
417 }
418 } else {
419 if (create_pkg_path(path, PKG_SEC_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX)) {
420 goto done;
421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423
424 d = opendir(path);
425 if (d == NULL) {
426 goto done;
427 }
428 dfd = dirfd(d);
429
430 /* most stuff in the pkgdir is data, except for the "cache"
431 * directory and below, which is cache, and the "lib" directory
432 * and below, which is code...
433 */
434 while ((de = readdir(d))) {
435 const char *name = de->d_name;
436
437 if (de->d_type == DT_DIR) {
438 int subfd;
439 /* always skip "." and ".." */
440 if (name[0] == '.') {
441 if (name[1] == 0) continue;
442 if ((name[1] == '.') && (name[2] == 0)) continue;
443 }
444 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
445 if (subfd >= 0) {
446 int size = calculate_dir_size(subfd);
447 if (!strcmp(name,"lib")) {
448 codesize += size;
449 } else if(!strcmp(name,"cache")) {
450 cachesize += size;
451 } else {
452 datasize += size;
453 }
454 }
455 } else {
456 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
457 datasize += stat_size(&s);
458 }
459 }
460 }
461 closedir(d);
462done:
463 *_codesize = codesize;
464 *_datasize = datasize;
465 *_cachesize = cachesize;
466 return 0;
467}
468
469
470/* a simpler version of dexOptGenerateCacheFileName() */
471int create_cache_path(char path[PKG_PATH_MAX], const char *src)
472{
473 char *tmp;
474 int srclen;
475 int dstlen;
476
477 srclen = strlen(src);
478
479 /* demand that we are an absolute path */
480 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
481 return -1;
482 }
483
484 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
485 return -1;
486 }
487
488 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
489 strlen(DALVIK_CACHE_POSTFIX) + 1;
490
491 if (dstlen > PKG_PATH_MAX) {
492 return -1;
493 }
494
495 sprintf(path,"%s%s%s",
496 DALVIK_CACHE_PREFIX,
497 src + 1, /* skip the leading / */
498 DALVIK_CACHE_POSTFIX);
499
500 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
501 if (*tmp == '/') {
502 *tmp = '@';
503 }
504 }
505
506 return 0;
507}
508
509static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
510 const char* dexopt_flags)
511{
512 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
513 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
514 char zip_num[MAX_INT_LEN];
515 char odex_num[MAX_INT_LEN];
516
517 sprintf(zip_num, "%d", zip_fd);
518 sprintf(odex_num, "%d", odex_fd);
519
520 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
521 dexopt_flags, (char*) NULL);
522 LOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
523}
524
525static int wait_dexopt(pid_t pid, const char* apk_path)
526{
527 int status;
528 pid_t got_pid;
529
530 /*
531 * Wait for the optimization process to finish.
532 */
533 while (1) {
534 got_pid = waitpid(pid, &status, 0);
535 if (got_pid == -1 && errno == EINTR) {
536 printf("waitpid interrupted, retrying\n");
537 } else {
538 break;
539 }
540 }
541 if (got_pid != pid) {
542 LOGW("waitpid failed: wanted %d, got %d: %s\n",
543 (int) pid, (int) got_pid, strerror(errno));
544 return 1;
545 }
546
547 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
548 LOGD("DexInv: --- END '%s' (success) ---\n", apk_path);
549 return 0;
550 } else {
551 LOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
552 apk_path, status);
553 return status; /* always nonzero */
554 }
555}
556
557int dexopt(const char *apk_path, uid_t uid, int is_public)
558{
559 struct utimbuf ut;
560 struct stat apk_stat, dex_stat;
561 char dex_path[PKG_PATH_MAX];
562 char dexopt_flags[PROPERTY_VALUE_MAX];
563 char *end;
564 int res, zip_fd=-1, odex_fd=-1;
565
566 /* Before anything else: is there a .odex file? If so, we have
567 * pre-optimized the apk and there is nothing to do here.
568 */
569 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
570 return -1;
571 }
572
573 /* platform-specific flags affecting optimization and verification */
574 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
575
576 strcpy(dex_path, apk_path);
577 end = strrchr(dex_path, '.');
578 if (end != NULL) {
579 strcpy(end, ".odex");
580 if (stat(dex_path, &dex_stat) == 0) {
581 return 0;
582 }
583 }
584
585 if (create_cache_path(dex_path, apk_path)) {
586 return -1;
587 }
588
589 memset(&apk_stat, 0, sizeof(apk_stat));
590 stat(apk_path, &apk_stat);
591
592 zip_fd = open(apk_path, O_RDONLY, 0);
593 if (zip_fd < 0) {
594 LOGE("dexopt cannot open '%s' for input\n", apk_path);
595 return -1;
596 }
597
598 unlink(dex_path);
599 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
600 if (odex_fd < 0) {
601 LOGE("dexopt cannot open '%s' for output\n", dex_path);
602 goto fail;
603 }
604 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
605 LOGE("dexopt cannot chown '%s'\n", dex_path);
606 goto fail;
607 }
608 if (fchmod(odex_fd,
609 S_IRUSR|S_IWUSR|S_IRGRP |
610 (is_public ? S_IROTH : 0)) < 0) {
611 LOGE("dexopt cannot chmod '%s'\n", dex_path);
612 goto fail;
613 }
614
615 LOGD("DexInv: --- BEGIN '%s' ---\n", apk_path);
616
617 pid_t pid;
618 pid = fork();
619 if (pid == 0) {
620 /* child -- drop privileges before continuing */
621 if (setgid(uid) != 0) {
622 LOGE("setgid(%d) failed during dexopt\n", uid);
623 exit(64);
624 }
625 if (setuid(uid) != 0) {
626 LOGE("setuid(%d) during dexopt\n", uid);
627 exit(65);
628 }
629 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
630 LOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
631 exit(66);
632 }
633
634 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
635 exit(67); /* only get here on exec failure */
636 } else {
637 res = wait_dexopt(pid, apk_path);
638 if (res != 0) {
639 LOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
640 goto fail;
641 }
642 }
643
644 ut.actime = apk_stat.st_atime;
645 ut.modtime = apk_stat.st_mtime;
646 utime(dex_path, &ut);
647
648 close(odex_fd);
649 close(zip_fd);
650 return 0;
651
652fail:
653 if (odex_fd >= 0) {
654 close(odex_fd);
655 unlink(dex_path);
656 }
657 if (zip_fd >= 0) {
658 close(zip_fd);
659 }
660 return -1;
661}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800662
663int create_move_path(char path[PKG_PATH_MAX],
664 const char* prefix,
665 const char* pkgname,
666 const char* leaf)
667{
668 if ((strlen(prefix) + strlen(pkgname) + strlen(leaf) + 1) >= PKG_PATH_MAX) {
669 return -1;
670 }
671
672 sprintf(path, "%s%s/%s", prefix, pkgname, leaf);
673 return 0;
674}
675
Dianne Hackbornc1552392010-03-03 16:19:01 -0800676void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
677 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800678{
679 while (path[basepos] != 0) {
680 if (path[basepos] == '/') {
681 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800682 if (lstat(path, statbuf) < 0) {
683 LOGI("Making directory: %s\n", path);
684 if (mkdir(path, mode) == 0) {
685 chown(path, uid, gid);
686 } else {
687 LOGW("Unable to make directory %s: %s\n", path, strerror(errno));
688 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800689 }
690 path[basepos] = '/';
691 basepos++;
692 }
693 basepos++;
694 }
695}
696
Dianne Hackbornc1552392010-03-03 16:19:01 -0800697int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
698 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800699{
700 DIR *d;
701 struct dirent *de;
702 int res;
703
704 int srcend = strlen(srcpath);
705 int dstend = strlen(dstpath);
706
707 if (lstat(srcpath, statbuf) < 0) {
708 LOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
709 return 1;
710 }
711
712 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800713 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
714 dstuid, dstgid, statbuf);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800715 LOGI("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800716 if (rename(srcpath, dstpath) >= 0) {
717 if (chown(dstpath, dstuid, dstgid) < 0) {
718 LOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
719 unlink(dstpath);
720 return 1;
721 }
722 } else {
723 LOGW("Unable to rename %s to %s: %s\n",
724 srcpath, dstpath, strerror(errno));
725 return 1;
726 }
727 return 0;
728 }
729
730 d = opendir(srcpath);
731 if (d == NULL) {
732 LOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
733 return 1;
734 }
735
736 res = 0;
737
738 while ((de = readdir(d))) {
739 const char *name = de->d_name;
740 /* always skip "." and ".." */
741 if (name[0] == '.') {
742 if (name[1] == 0) continue;
743 if ((name[1] == '.') && (name[2] == 0)) continue;
744 }
745
746 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
747 LOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
748 continue;
749 }
750
751 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
752 LOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
753 continue;
754 }
755
756 srcpath[srcend] = dstpath[dstend] = '/';
757 strcpy(srcpath+srcend+1, name);
758 strcpy(dstpath+dstend+1, name);
759
Dianne Hackbornc1552392010-03-03 16:19:01 -0800760 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800761 res = 1;
762 }
763
764 // Note: we will be leaving empty directories behind in srcpath,
765 // but that is okay, the package manager will be erasing all of the
766 // data associated with .apks that disappear.
767
768 srcpath[srcend] = dstpath[dstend] = 0;
769 }
770
771 closedir(d);
772 return res;
773}
774
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800775int movefiles()
776{
777 DIR *d;
778 int dfd, subfd;
779 struct dirent *de;
780 struct stat s;
781 char buf[PKG_PATH_MAX+1];
782 int bufp, bufe, bufi, readlen;
783
784 char srcpkg[PKG_NAME_MAX];
785 char dstpkg[PKG_NAME_MAX];
786 char srcpath[PKG_PATH_MAX];
787 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800788 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800789 int hasspace;
790
791 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
792 if (d == NULL) {
793 goto done;
794 }
795 dfd = dirfd(d);
796
797 /* Iterate through all files in the directory, executing the
798 * file movements requested there-in.
799 */
800 while ((de = readdir(d))) {
801 const char *name = de->d_name;
802
803 if (de->d_type == DT_DIR) {
804 continue;
805 } else {
806 subfd = openat(dfd, name, O_RDONLY);
807 if (subfd < 0) {
808 LOGW("Unable to open update commands at %s%s\n",
809 UPDATE_COMMANDS_DIR_PREFIX, name);
810 continue;
811 }
812
813 bufp = 0;
814 bufe = 0;
815 buf[PKG_PATH_MAX] = 0;
816 srcpkg[0] = dstpkg[0] = 0;
817 while (1) {
818 bufi = bufp;
819 while (bufi < bufe && buf[bufi] != '\n') {
820 bufi++;
821 }
822 if (bufi < bufe) {
823 buf[bufi] = 0;
824 LOGV("Processing line: %s\n", buf+bufp);
825 hasspace = 0;
826 while (bufp < bufi && isspace(buf[bufp])) {
827 hasspace = 1;
828 bufp++;
829 }
830 if (buf[bufp] == '#' || bufp == bufi) {
831 // skip comments and empty lines.
832 } else if (hasspace) {
833 if (dstpkg[0] == 0) {
834 LOGW("Path before package line in %s%s: %s\n",
835 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
836 } else if (srcpkg[0] == 0) {
837 // Skip -- source package no longer exists.
838 } else {
839 LOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
840 if (!create_move_path(srcpath, PKG_DIR_PREFIX, srcpkg, buf+bufp) &&
841 !create_move_path(dstpath, PKG_DIR_PREFIX, dstpkg, buf+bufp)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800842 movefileordir(srcpath, dstpath,
843 strlen(dstpath)-strlen(buf+bufp),
844 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800845 }
846 }
847 } else {
848 char* div = strchr(buf+bufp, ':');
849 if (div == NULL) {
850 LOGW("Bad package spec in %s%s; no ':' sep: %s\n",
851 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
852 } else {
853 *div = 0;
854 div++;
855 if (strlen(buf+bufp) < PKG_NAME_MAX) {
856 strcpy(dstpkg, buf+bufp);
857 } else {
858 srcpkg[0] = dstpkg[0] = 0;
859 LOGW("Package name too long in %s%s: %s\n",
860 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
861 }
862 if (strlen(div) < PKG_NAME_MAX) {
863 strcpy(srcpkg, div);
864 } else {
865 srcpkg[0] = dstpkg[0] = 0;
866 LOGW("Package name too long in %s%s: %s\n",
867 UPDATE_COMMANDS_DIR_PREFIX, name, div);
868 }
869 if (srcpkg[0] != 0) {
870 if (!create_pkg_path(srcpath, PKG_DIR_PREFIX, srcpkg,
871 PKG_DIR_POSTFIX)) {
872 if (lstat(srcpath, &s) < 0) {
873 // Package no longer exists -- skip.
874 srcpkg[0] = 0;
875 }
876 } else {
877 srcpkg[0] = 0;
878 LOGW("Can't create path %s in %s%s\n",
879 div, UPDATE_COMMANDS_DIR_PREFIX, name);
880 }
881 if (srcpkg[0] != 0) {
882 if (!create_pkg_path(dstpath, PKG_DIR_PREFIX, dstpkg,
883 PKG_DIR_POSTFIX)) {
884 if (lstat(dstpath, &s) == 0) {
885 dstuid = s.st_uid;
886 dstgid = s.st_gid;
887 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800888 // Destination package doesn't
889 // exist... due to original-package,
890 // this is normal, so don't be
891 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800892 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800893 }
894 } else {
895 srcpkg[0] = 0;
896 LOGW("Can't create path %s in %s%s\n",
897 div, UPDATE_COMMANDS_DIR_PREFIX, name);
898 }
899 }
900 LOGV("Transfering from %s to %s: uid=%d\n",
901 srcpkg, dstpkg, dstuid);
902 }
903 }
904 }
905 bufp = bufi+1;
906 } else {
907 if (bufp == 0) {
908 if (bufp < bufe) {
909 LOGW("Line too long in %s%s, skipping: %s\n",
910 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
911 }
912 } else if (bufp < bufe) {
913 memcpy(buf, buf+bufp, bufe-bufp);
914 bufe -= bufp;
915 bufp = 0;
916 }
917 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
918 if (readlen < 0) {
919 LOGW("Failure reading update commands in %s%s: %s\n",
920 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
921 break;
922 } else if (readlen == 0) {
923 break;
924 }
925 bufe += readlen;
926 buf[bufe] = 0;
927 LOGV("Read buf: %s\n", buf);
928 }
929 }
930 close(subfd);
931 }
932 }
933 closedir(d);
934done:
935 return 0;
936}