blob: 9aa70a4582402c3e9c1bd7bb7660a90c209742be [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"
Kenny Root33b22642010-11-30 13:49:32 -080018#include <diskusage/dirsize.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
Kenny Root86c95842011-03-31 13:16:12 -070020/* Directory records that are used in execution of commands. */
21dir_rec_t android_data_dir;
22dir_rec_t android_asec_dir;
23dir_rec_t android_app_dir;
24dir_rec_t android_app_private_dir;
25dir_rec_array_t android_system_dirs;
26
Kenny Root35ab3ad2011-02-02 16:42:14 -080027int install(const char *pkgname, uid_t uid, gid_t gid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028{
29 char pkgdir[PKG_PATH_MAX];
30 char libdir[PKG_PATH_MAX];
31
32 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
33 LOGE("invalid uid/gid: %d %d\n", uid, gid);
34 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 }
Oscar Montemayora8529f62009-11-18 10:14:20 -080036
Kenny Root86c95842011-03-31 13:16:12 -070037 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
38 LOGE("cannot create package path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080039 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070040 }
41
42 if (create_pkg_path(libdir, pkgname, PKG_LIB_POSTFIX, 0)) {
43 LOGE("cannot create package lib path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080044 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
David 'Digit' Turner0dd50e62010-02-09 19:02:38 -080047 if (mkdir(pkgdir, 0751) < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 LOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
49 return -errno;
50 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070051 if (chmod(pkgdir, 0751) < 0) {
52 LOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
53 unlink(pkgdir);
54 return -errno;
55 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 if (chown(pkgdir, uid, gid) < 0) {
57 LOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
58 unlink(pkgdir);
59 return -errno;
60 }
61 if (mkdir(libdir, 0755) < 0) {
62 LOGE("cannot create dir '%s': %s\n", libdir, strerror(errno));
63 unlink(pkgdir);
64 return -errno;
65 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070066 if (chmod(libdir, 0755) < 0) {
67 LOGE("cannot chmod dir '%s': %s\n", libdir, strerror(errno));
68 unlink(libdir);
69 unlink(pkgdir);
70 return -errno;
71 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
73 LOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
74 unlink(libdir);
75 unlink(pkgdir);
76 return -errno;
77 }
78 return 0;
79}
80
Amith Yamasani0b285492011-04-14 17:35:23 -070081int uninstall(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082{
83 char pkgdir[PKG_PATH_MAX];
84
Amith Yamasani0b285492011-04-14 17:35:23 -070085 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -080086 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
Amith Yamasani0b285492011-04-14 17:35:23 -070088 /* delete contents AND directory, no exceptions */
89 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090}
91
Kenny Root35ab3ad2011-02-02 16:42:14 -080092int renamepkg(const char *oldpkgname, const char *newpkgname)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080093{
94 char oldpkgdir[PKG_PATH_MAX];
95 char newpkgdir[PKG_PATH_MAX];
96
Kenny Root86c95842011-03-31 13:16:12 -070097 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -080098 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070099 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800100 return -1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800101
102 if (rename(oldpkgdir, newpkgdir) < 0) {
103 LOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
104 return -errno;
105 }
106 return 0;
107}
108
Amith Yamasani0b285492011-04-14 17:35:23 -0700109int delete_user_data(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110{
111 char pkgdir[PKG_PATH_MAX];
112
Amith Yamasani0b285492011-04-14 17:35:23 -0700113 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800114 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Amith Yamasani0b285492011-04-14 17:35:23 -0700116 /* delete contents, excluding "lib", but not the directory itself */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 return delete_dir_contents(pkgdir, 0, "lib");
118}
119
Amith Yamasani0b285492011-04-14 17:35:23 -0700120int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
121{
122 char pkgdir[PKG_PATH_MAX];
123 char real_libdir[PKG_PATH_MAX];
124
125 // Create the data dir for the package
126 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
127 return -1;
128 }
129 if (mkdir(pkgdir, 0751) < 0) {
130 LOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
131 return -errno;
132 }
133 if (chown(pkgdir, uid, uid) < 0) {
134 LOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
135 unlink(pkgdir);
136 return -errno;
137 }
138 return 0;
139}
140
141int delete_persona(uid_t persona)
142{
143 char pkgdir[PKG_PATH_MAX];
144
145 if (create_persona_path(pkgdir, persona))
146 return -1;
147
148 return delete_dir_contents(pkgdir, 1, NULL);
149}
150
Kenny Root35ab3ad2011-02-02 16:42:14 -0800151int delete_cache(const char *pkgname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152{
153 char cachedir[PKG_PATH_MAX];
154
Kenny Root86c95842011-03-31 13:16:12 -0700155 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800156 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158 /* delete contents, not the directory, no exceptions */
159 return delete_dir_contents(cachedir, 0, 0);
160}
161
Kenny Root3e319a92010-09-07 13:58:28 -0700162static int64_t disk_free()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163{
164 struct statfs sfs;
Kenny Root86c95842011-03-31 13:16:12 -0700165 if (statfs(android_data_dir.path, &sfs) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 return sfs.f_bavail * sfs.f_bsize;
167 } else {
Kenny Root86c95842011-03-31 13:16:12 -0700168 LOGE("Couldn't statfs %s: %s\n", android_data_dir.path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 return -1;
170 }
171}
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173/* Try to ensure free_size bytes of storage are available.
174 * Returns 0 on success.
175 * This is rather simple-minded because doing a full LRU would
176 * be potentially memory-intensive, and without atime it would
177 * also require that apps constantly modify file metadata even
178 * when just reading from the cache, which is pretty awful.
179 */
Kenny Root3e319a92010-09-07 13:58:28 -0700180int free_cache(int64_t free_size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181{
182 const char *name;
183 int dfd, subfd;
184 DIR *d;
185 struct dirent *de;
Kenny Root3e319a92010-09-07 13:58:28 -0700186 int64_t avail;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
188 avail = disk_free();
189 if (avail < 0) return -1;
190
Kenny Root3e319a92010-09-07 13:58:28 -0700191 LOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 if (avail >= free_size) return 0;
193
Kenny Root86c95842011-03-31 13:16:12 -0700194 d = opendir(android_data_dir.path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 if (d == NULL) {
Kenny Root86c95842011-03-31 13:16:12 -0700196 LOGE("cannot open %s: %s\n", android_data_dir.path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 return -1;
198 }
199 dfd = dirfd(d);
200
201 while ((de = readdir(d))) {
202 if (de->d_type != DT_DIR) continue;
203 name = de->d_name;
204
Oscar Montemayora8529f62009-11-18 10:14:20 -0800205 /* always skip "." and ".." */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 if (name[0] == '.') {
207 if (name[1] == 0) continue;
208 if ((name[1] == '.') && (name[2] == 0)) continue;
209 }
210
211 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
212 if (subfd < 0) continue;
213
214 delete_dir_contents_fd(subfd, "cache");
215 close(subfd);
216
217 avail = disk_free();
218 if (avail >= free_size) {
219 closedir(d);
220 return 0;
221 }
222 }
223 closedir(d);
Oscar Montemayora8529f62009-11-18 10:14:20 -0800224
225 /* Fail case - not possible to free space */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 return -1;
227}
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229int move_dex(const char *src, const char *dst)
230{
231 char src_dex[PKG_PATH_MAX];
232 char dst_dex[PKG_PATH_MAX];
233
Kenny Root86c95842011-03-31 13:16:12 -0700234 if (validate_apk_path(src)) return -1;
235 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
237 if (create_cache_path(src_dex, src)) return -1;
238 if (create_cache_path(dst_dex, dst)) return -1;
239
240 LOGI("move %s -> %s\n", src_dex, dst_dex);
241 if (rename(src_dex, dst_dex) < 0) {
Kenny Root50871522010-08-04 09:14:01 -0700242 LOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 return -1;
244 } else {
245 return 0;
246 }
247}
248
249int rm_dex(const char *path)
250{
251 char dex_path[PKG_PATH_MAX];
252
Kenny Root86c95842011-03-31 13:16:12 -0700253 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 if (create_cache_path(dex_path, path)) return -1;
255
256 LOGI("unlink %s\n", dex_path);
257 if (unlink(dex_path) < 0) {
Kenny Root50871522010-08-04 09:14:01 -0700258 LOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 return -1;
260 } else {
261 return 0;
262 }
263}
264
265int protect(char *pkgname, gid_t gid)
266{
267 struct stat s;
268 char pkgpath[PKG_PATH_MAX];
269
270 if (gid < AID_SYSTEM) return -1;
271
Kenny Root86c95842011-03-31 13:16:12 -0700272 if (create_pkg_path_in_dir(pkgpath, &android_app_private_dir, pkgname, ".apk"))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 return -1;
274
275 if (stat(pkgpath, &s) < 0) return -1;
276
277 if (chown(pkgpath, s.st_uid, gid) < 0) {
278 LOGE("failed to chgrp '%s': %s\n", pkgpath, strerror(errno));
279 return -1;
280 }
281
282 if (chmod(pkgpath, S_IRUSR|S_IWUSR|S_IRGRP) < 0) {
283 LOGE("failed to chmod '%s': %s\n", pkgpath, strerror(errno));
284 return -1;
285 }
286
287 return 0;
288}
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290int get_size(const char *pkgname, const char *apkpath,
291 const char *fwdlock_apkpath,
Kenny Root35ab3ad2011-02-02 16:42:14 -0800292 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293{
294 DIR *d;
295 int dfd;
296 struct dirent *de;
297 struct stat s;
298 char path[PKG_PATH_MAX];
299
Kenny Root3e319a92010-09-07 13:58:28 -0700300 int64_t codesize = 0;
301 int64_t datasize = 0;
302 int64_t cachesize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
304 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800305 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 */
Kenny Root86c95842011-03-31 13:16:12 -0700307 if (validate_system_app_path(apkpath) &&
308 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 if (stat(apkpath, &s) == 0) {
310 codesize += stat_size(&s);
311 }
312 }
313 /* count the forward locked apk as code if it is given
314 */
315 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
316 if (stat(fwdlock_apkpath, &s) == 0) {
317 codesize += stat_size(&s);
318 }
319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /* count the cached dexfile as code */
321 if (!create_cache_path(path, apkpath)) {
322 if (stat(path, &s) == 0) {
323 codesize += stat_size(&s);
324 }
325 }
326
Kenny Root86c95842011-03-31 13:16:12 -0700327 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, 0)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800328 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
330
331 d = opendir(path);
332 if (d == NULL) {
333 goto done;
334 }
335 dfd = dirfd(d);
336
Kenny Root86c95842011-03-31 13:16:12 -0700337 /* most stuff in the pkgdir is data, except for the "cache"
338 * directory and below, which is cache, and the "lib" directory
339 * and below, which is code...
340 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 while ((de = readdir(d))) {
342 const char *name = de->d_name;
343
344 if (de->d_type == DT_DIR) {
345 int subfd;
346 /* always skip "." and ".." */
347 if (name[0] == '.') {
348 if (name[1] == 0) continue;
349 if ((name[1] == '.') && (name[2] == 0)) continue;
350 }
351 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
352 if (subfd >= 0) {
Kenny Root3e319a92010-09-07 13:58:28 -0700353 int64_t size = calculate_dir_size(subfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 if (!strcmp(name,"lib")) {
355 codesize += size;
356 } else if(!strcmp(name,"cache")) {
357 cachesize += size;
358 } else {
359 datasize += size;
360 }
361 }
362 } else {
363 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
364 datasize += stat_size(&s);
365 }
366 }
367 }
368 closedir(d);
369done:
370 *_codesize = codesize;
371 *_datasize = datasize;
372 *_cachesize = cachesize;
373 return 0;
374}
375
376
377/* a simpler version of dexOptGenerateCacheFileName() */
378int create_cache_path(char path[PKG_PATH_MAX], const char *src)
379{
380 char *tmp;
381 int srclen;
382 int dstlen;
383
384 srclen = strlen(src);
385
386 /* demand that we are an absolute path */
387 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
388 return -1;
389 }
390
391 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
392 return -1;
393 }
394
395 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
396 strlen(DALVIK_CACHE_POSTFIX) + 1;
397
398 if (dstlen > PKG_PATH_MAX) {
399 return -1;
400 }
401
402 sprintf(path,"%s%s%s",
403 DALVIK_CACHE_PREFIX,
404 src + 1, /* skip the leading / */
405 DALVIK_CACHE_POSTFIX);
406
407 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
408 if (*tmp == '/') {
409 *tmp = '@';
410 }
411 }
412
413 return 0;
414}
415
416static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
417 const char* dexopt_flags)
418{
419 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
420 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
421 char zip_num[MAX_INT_LEN];
422 char odex_num[MAX_INT_LEN];
423
424 sprintf(zip_num, "%d", zip_fd);
425 sprintf(odex_num, "%d", odex_fd);
426
427 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
428 dexopt_flags, (char*) NULL);
429 LOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
430}
431
432static int wait_dexopt(pid_t pid, const char* apk_path)
433{
434 int status;
435 pid_t got_pid;
436
437 /*
438 * Wait for the optimization process to finish.
439 */
440 while (1) {
441 got_pid = waitpid(pid, &status, 0);
442 if (got_pid == -1 && errno == EINTR) {
443 printf("waitpid interrupted, retrying\n");
444 } else {
445 break;
446 }
447 }
448 if (got_pid != pid) {
449 LOGW("waitpid failed: wanted %d, got %d: %s\n",
450 (int) pid, (int) got_pid, strerror(errno));
451 return 1;
452 }
453
454 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
455 LOGD("DexInv: --- END '%s' (success) ---\n", apk_path);
456 return 0;
457 } else {
458 LOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
459 apk_path, status);
460 return status; /* always nonzero */
461 }
462}
463
464int dexopt(const char *apk_path, uid_t uid, int is_public)
465{
466 struct utimbuf ut;
467 struct stat apk_stat, dex_stat;
468 char dex_path[PKG_PATH_MAX];
469 char dexopt_flags[PROPERTY_VALUE_MAX];
470 char *end;
471 int res, zip_fd=-1, odex_fd=-1;
472
473 /* Before anything else: is there a .odex file? If so, we have
474 * pre-optimized the apk and there is nothing to do here.
475 */
476 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
477 return -1;
478 }
479
480 /* platform-specific flags affecting optimization and verification */
481 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
482
483 strcpy(dex_path, apk_path);
484 end = strrchr(dex_path, '.');
485 if (end != NULL) {
486 strcpy(end, ".odex");
487 if (stat(dex_path, &dex_stat) == 0) {
488 return 0;
489 }
490 }
491
492 if (create_cache_path(dex_path, apk_path)) {
493 return -1;
494 }
495
496 memset(&apk_stat, 0, sizeof(apk_stat));
497 stat(apk_path, &apk_stat);
498
499 zip_fd = open(apk_path, O_RDONLY, 0);
500 if (zip_fd < 0) {
501 LOGE("dexopt cannot open '%s' for input\n", apk_path);
502 return -1;
503 }
504
505 unlink(dex_path);
506 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
507 if (odex_fd < 0) {
508 LOGE("dexopt cannot open '%s' for output\n", dex_path);
509 goto fail;
510 }
511 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
512 LOGE("dexopt cannot chown '%s'\n", dex_path);
513 goto fail;
514 }
515 if (fchmod(odex_fd,
516 S_IRUSR|S_IWUSR|S_IRGRP |
517 (is_public ? S_IROTH : 0)) < 0) {
518 LOGE("dexopt cannot chmod '%s'\n", dex_path);
519 goto fail;
520 }
521
522 LOGD("DexInv: --- BEGIN '%s' ---\n", apk_path);
523
524 pid_t pid;
525 pid = fork();
526 if (pid == 0) {
527 /* child -- drop privileges before continuing */
528 if (setgid(uid) != 0) {
529 LOGE("setgid(%d) failed during dexopt\n", uid);
530 exit(64);
531 }
532 if (setuid(uid) != 0) {
533 LOGE("setuid(%d) during dexopt\n", uid);
534 exit(65);
535 }
536 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
537 LOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
538 exit(66);
539 }
540
541 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
542 exit(67); /* only get here on exec failure */
543 } else {
544 res = wait_dexopt(pid, apk_path);
545 if (res != 0) {
546 LOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
547 goto fail;
548 }
549 }
550
551 ut.actime = apk_stat.st_atime;
552 ut.modtime = apk_stat.st_mtime;
553 utime(dex_path, &ut);
554
555 close(odex_fd);
556 close(zip_fd);
557 return 0;
558
559fail:
560 if (odex_fd >= 0) {
561 close(odex_fd);
562 unlink(dex_path);
563 }
564 if (zip_fd >= 0) {
565 close(zip_fd);
566 }
567 return -1;
568}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800569
570int create_move_path(char path[PKG_PATH_MAX],
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800571 const char* pkgname,
Kenny Root86c95842011-03-31 13:16:12 -0700572 const char* leaf,
573 uid_t persona)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800574{
Kenny Root86c95842011-03-31 13:16:12 -0700575 if ((android_data_dir.len + strlen(pkgname) + strlen(leaf) + 1) >= PKG_PATH_MAX) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800576 return -1;
577 }
578
Kenny Root86c95842011-03-31 13:16:12 -0700579 sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800580 return 0;
581}
582
Dianne Hackbornc1552392010-03-03 16:19:01 -0800583void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
584 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800585{
586 while (path[basepos] != 0) {
587 if (path[basepos] == '/') {
588 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800589 if (lstat(path, statbuf) < 0) {
590 LOGI("Making directory: %s\n", path);
591 if (mkdir(path, mode) == 0) {
592 chown(path, uid, gid);
593 } else {
594 LOGW("Unable to make directory %s: %s\n", path, strerror(errno));
595 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800596 }
597 path[basepos] = '/';
598 basepos++;
599 }
600 basepos++;
601 }
602}
603
Dianne Hackbornc1552392010-03-03 16:19:01 -0800604int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
605 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800606{
607 DIR *d;
608 struct dirent *de;
609 int res;
610
611 int srcend = strlen(srcpath);
612 int dstend = strlen(dstpath);
613
614 if (lstat(srcpath, statbuf) < 0) {
615 LOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
616 return 1;
617 }
618
619 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800620 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
621 dstuid, dstgid, statbuf);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800622 LOGI("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800623 if (rename(srcpath, dstpath) >= 0) {
624 if (chown(dstpath, dstuid, dstgid) < 0) {
625 LOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
626 unlink(dstpath);
627 return 1;
628 }
629 } else {
630 LOGW("Unable to rename %s to %s: %s\n",
631 srcpath, dstpath, strerror(errno));
632 return 1;
633 }
634 return 0;
635 }
636
637 d = opendir(srcpath);
638 if (d == NULL) {
639 LOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
640 return 1;
641 }
642
643 res = 0;
644
645 while ((de = readdir(d))) {
646 const char *name = de->d_name;
647 /* always skip "." and ".." */
648 if (name[0] == '.') {
649 if (name[1] == 0) continue;
650 if ((name[1] == '.') && (name[2] == 0)) continue;
651 }
652
653 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
654 LOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
655 continue;
656 }
657
658 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
659 LOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
660 continue;
661 }
662
663 srcpath[srcend] = dstpath[dstend] = '/';
664 strcpy(srcpath+srcend+1, name);
665 strcpy(dstpath+dstend+1, name);
666
Dianne Hackbornc1552392010-03-03 16:19:01 -0800667 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800668 res = 1;
669 }
670
671 // Note: we will be leaving empty directories behind in srcpath,
672 // but that is okay, the package manager will be erasing all of the
673 // data associated with .apks that disappear.
674
675 srcpath[srcend] = dstpath[dstend] = 0;
676 }
677
678 closedir(d);
679 return res;
680}
681
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800682int movefiles()
683{
684 DIR *d;
685 int dfd, subfd;
686 struct dirent *de;
687 struct stat s;
688 char buf[PKG_PATH_MAX+1];
689 int bufp, bufe, bufi, readlen;
690
691 char srcpkg[PKG_NAME_MAX];
692 char dstpkg[PKG_NAME_MAX];
693 char srcpath[PKG_PATH_MAX];
694 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800695 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800696 int hasspace;
697
698 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
699 if (d == NULL) {
700 goto done;
701 }
702 dfd = dirfd(d);
703
704 /* Iterate through all files in the directory, executing the
705 * file movements requested there-in.
706 */
707 while ((de = readdir(d))) {
708 const char *name = de->d_name;
709
710 if (de->d_type == DT_DIR) {
711 continue;
712 } else {
713 subfd = openat(dfd, name, O_RDONLY);
714 if (subfd < 0) {
715 LOGW("Unable to open update commands at %s%s\n",
716 UPDATE_COMMANDS_DIR_PREFIX, name);
717 continue;
718 }
719
720 bufp = 0;
721 bufe = 0;
722 buf[PKG_PATH_MAX] = 0;
723 srcpkg[0] = dstpkg[0] = 0;
724 while (1) {
725 bufi = bufp;
726 while (bufi < bufe && buf[bufi] != '\n') {
727 bufi++;
728 }
729 if (bufi < bufe) {
730 buf[bufi] = 0;
731 LOGV("Processing line: %s\n", buf+bufp);
732 hasspace = 0;
733 while (bufp < bufi && isspace(buf[bufp])) {
734 hasspace = 1;
735 bufp++;
736 }
737 if (buf[bufp] == '#' || bufp == bufi) {
738 // skip comments and empty lines.
739 } else if (hasspace) {
740 if (dstpkg[0] == 0) {
741 LOGW("Path before package line in %s%s: %s\n",
742 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
743 } else if (srcpkg[0] == 0) {
744 // Skip -- source package no longer exists.
745 } else {
746 LOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
Kenny Root86c95842011-03-31 13:16:12 -0700747 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
748 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800749 movefileordir(srcpath, dstpath,
750 strlen(dstpath)-strlen(buf+bufp),
751 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800752 }
753 }
754 } else {
755 char* div = strchr(buf+bufp, ':');
756 if (div == NULL) {
757 LOGW("Bad package spec in %s%s; no ':' sep: %s\n",
758 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
759 } else {
760 *div = 0;
761 div++;
762 if (strlen(buf+bufp) < PKG_NAME_MAX) {
763 strcpy(dstpkg, buf+bufp);
764 } else {
765 srcpkg[0] = dstpkg[0] = 0;
766 LOGW("Package name too long in %s%s: %s\n",
767 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
768 }
769 if (strlen(div) < PKG_NAME_MAX) {
770 strcpy(srcpkg, div);
771 } else {
772 srcpkg[0] = dstpkg[0] = 0;
773 LOGW("Package name too long in %s%s: %s\n",
774 UPDATE_COMMANDS_DIR_PREFIX, name, div);
775 }
776 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700777 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800778 if (lstat(srcpath, &s) < 0) {
779 // Package no longer exists -- skip.
780 srcpkg[0] = 0;
781 }
782 } else {
783 srcpkg[0] = 0;
784 LOGW("Can't create path %s in %s%s\n",
785 div, UPDATE_COMMANDS_DIR_PREFIX, name);
786 }
787 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700788 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800789 if (lstat(dstpath, &s) == 0) {
790 dstuid = s.st_uid;
791 dstgid = s.st_gid;
792 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800793 // Destination package doesn't
794 // exist... due to original-package,
795 // this is normal, so don't be
796 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800797 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800798 }
799 } else {
800 srcpkg[0] = 0;
801 LOGW("Can't create path %s in %s%s\n",
802 div, UPDATE_COMMANDS_DIR_PREFIX, name);
803 }
804 }
805 LOGV("Transfering from %s to %s: uid=%d\n",
806 srcpkg, dstpkg, dstuid);
807 }
808 }
809 }
810 bufp = bufi+1;
811 } else {
812 if (bufp == 0) {
813 if (bufp < bufe) {
814 LOGW("Line too long in %s%s, skipping: %s\n",
815 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
816 }
817 } else if (bufp < bufe) {
818 memcpy(buf, buf+bufp, bufe-bufp);
819 bufe -= bufp;
820 bufp = 0;
821 }
822 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
823 if (readlen < 0) {
824 LOGW("Failure reading update commands in %s%s: %s\n",
825 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
826 break;
827 } else if (readlen == 0) {
828 break;
829 }
830 bufe += readlen;
831 buf[bufe] = 0;
832 LOGV("Read buf: %s\n", buf);
833 }
834 }
835 close(subfd);
836 }
837 }
838 closedir(d);
839done:
840 return 0;
841}
Kenny Root6a6b0072010-10-07 16:46:10 -0700842
843int linklib(const char* dataDir, const char* asecLibDir)
844{
845 char libdir[PKG_PATH_MAX];
846 struct stat s, libStat;
847 int rc = 0;
848
849 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
850 if (libdirLen >= PKG_PATH_MAX) {
851 LOGE("library dir len too large");
Kenny Root0332d1c2010-10-21 16:14:06 -0700852 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -0700853 }
854
855 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
856 LOGE("library dir not written successfully: %s\n", strerror(errno));
Kenny Root0332d1c2010-10-21 16:14:06 -0700857 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -0700858 }
859
860 if (stat(dataDir, &s) < 0) return -1;
861
862 if (chown(dataDir, 0, 0) < 0) {
863 LOGE("failed to chown '%s': %s\n", dataDir, strerror(errno));
864 return -1;
865 }
866
867 if (chmod(dataDir, 0700) < 0) {
868 LOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
869 rc = -1;
870 goto out;
871 }
872
873 if (lstat(libdir, &libStat) < 0) {
874 LOGE("couldn't stat lib dir: %s\n", strerror(errno));
875 rc = -1;
876 goto out;
877 }
878
879 if (S_ISDIR(libStat.st_mode)) {
880 if (delete_dir_contents(libdir, 1, 0) < 0) {
881 rc = -1;
882 goto out;
883 }
884 } else if (S_ISLNK(libStat.st_mode)) {
885 if (unlink(libdir) < 0) {
886 rc = -1;
887 goto out;
888 }
889 }
890
891 if (symlink(asecLibDir, libdir) < 0) {
892 LOGE("couldn't symlink directory '%s' -> '%s': %s\n", libdir, asecLibDir, strerror(errno));
893 rc = -errno;
894 goto out;
895 }
896
897 if (lchown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
898 LOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
899 unlink(libdir);
900 rc = -errno;
901 goto out;
902 }
903
904out:
905 if (chmod(dataDir, s.st_mode) < 0) {
906 LOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
907 return -errno;
908 }
909
910 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
911 LOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
912 return -errno;
913 }
914
915 return rc;
916}
917
918int unlinklib(const char* dataDir)
919{
920 char libdir[PKG_PATH_MAX];
921 struct stat s, libStat;
922 int rc = 0;
923
924 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
925 if (libdirLen >= PKG_PATH_MAX) {
926 return -1;
927 }
928
929 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
930 LOGE("library dir not written successfully: %s\n", strerror(errno));
931 return -1;
932 }
933
934 if (stat(dataDir, &s) < 0) {
935 LOGE("couldn't state data dir");
936 return -1;
937 }
938
939 if (chown(dataDir, 0, 0) < 0) {
940 LOGE("failed to chown '%s': %s\n", dataDir, strerror(errno));
941 return -1;
942 }
943
944 if (chmod(dataDir, 0700) < 0) {
945 LOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
946 rc = -1;
947 goto out;
948 }
949
950 if (lstat(libdir, &libStat) < 0) {
951 LOGE("couldn't stat lib dir: %s\n", strerror(errno));
952 rc = -1;
953 goto out;
954 }
955
956 if (S_ISDIR(libStat.st_mode)) {
957 if (delete_dir_contents(libdir, 1, 0) < 0) {
958 rc = -1;
959 goto out;
960 }
961 } else if (S_ISLNK(libStat.st_mode)) {
962 if (unlink(libdir) < 0) {
963 rc = -1;
964 goto out;
965 }
966 }
967
968 if (mkdir(libdir, 0755) < 0) {
969 LOGE("cannot create dir '%s': %s\n", libdir, strerror(errno));
970 rc = -errno;
971 goto out;
972 }
973
974 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
975 LOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
976 unlink(libdir);
977 rc = -errno;
978 goto out;
979 }
980
981out:
982 if (chmod(dataDir, s.st_mode) < 0) {
983 LOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
984 return -1;
985 }
986
987 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
988 LOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
989 return -1;
990 }
991
992 return rc;
993}