blob: 0dd8156ebf5e7c8fe031ff816f34311d2d26681d [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
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050020#ifdef HAVE_SELINUX
21#include <selinux/android.h>
22#endif
23
Kenny Root86c95842011-03-31 13:16:12 -070024/* Directory records that are used in execution of commands. */
25dir_rec_t android_data_dir;
26dir_rec_t android_asec_dir;
27dir_rec_t android_app_dir;
28dir_rec_t android_app_private_dir;
29dir_rec_array_t android_system_dirs;
30
Kenny Root35ab3ad2011-02-02 16:42:14 -080031int install(const char *pkgname, uid_t uid, gid_t gid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032{
33 char pkgdir[PKG_PATH_MAX];
34 char libdir[PKG_PATH_MAX];
35
36 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
Steve Blockc6aacce2012-01-06 19:20:56 +000037 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 }
Oscar Montemayora8529f62009-11-18 10:14:20 -080040
Kenny Root86c95842011-03-31 13:16:12 -070041 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
Steve Blockc6aacce2012-01-06 19:20:56 +000042 ALOGE("cannot create package path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080043 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070044 }
45
46 if (create_pkg_path(libdir, pkgname, PKG_LIB_POSTFIX, 0)) {
Steve Blockc6aacce2012-01-06 19:20:56 +000047 ALOGE("cannot create package lib path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080048 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
David 'Digit' Turner0dd50e62010-02-09 19:02:38 -080051 if (mkdir(pkgdir, 0751) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000052 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 return -errno;
54 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070055 if (chmod(pkgdir, 0751) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000056 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070057 unlink(pkgdir);
58 return -errno;
59 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 if (chown(pkgdir, uid, gid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000061 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 unlink(pkgdir);
63 return -errno;
64 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050065
66#ifdef HAVE_SELINUX
67 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
68 LOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
69 unlink(pkgdir);
70 return -errno;
71 }
72#endif
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 if (mkdir(libdir, 0755) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000075 ALOGE("cannot create dir '%s': %s\n", libdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 unlink(pkgdir);
77 return -errno;
78 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070079 if (chmod(libdir, 0755) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000080 ALOGE("cannot chmod dir '%s': %s\n", libdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070081 unlink(libdir);
82 unlink(pkgdir);
83 return -errno;
84 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000086 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 unlink(libdir);
88 unlink(pkgdir);
89 return -errno;
90 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050091
92#ifdef HAVE_SELINUX
93 if (selinux_android_setfilecon(libdir, pkgname, AID_SYSTEM) < 0) {
94 LOGE("cannot setfilecon dir '%s': %s\n", libdir, strerror(errno));
95 unlink(libdir);
96 unlink(pkgdir);
97 return -errno;
98 }
99#endif
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 return 0;
102}
103
Amith Yamasani0b285492011-04-14 17:35:23 -0700104int uninstall(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105{
106 char pkgdir[PKG_PATH_MAX];
107
Amith Yamasani0b285492011-04-14 17:35:23 -0700108 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800109 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Amith Yamasani0b285492011-04-14 17:35:23 -0700111 /* delete contents AND directory, no exceptions */
112 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113}
114
Kenny Root35ab3ad2011-02-02 16:42:14 -0800115int renamepkg(const char *oldpkgname, const char *newpkgname)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800116{
117 char oldpkgdir[PKG_PATH_MAX];
118 char newpkgdir[PKG_PATH_MAX];
119
Kenny Root86c95842011-03-31 13:16:12 -0700120 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800121 return -1;
Kenny Root86c95842011-03-31 13:16:12 -0700122 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800123 return -1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800124
125 if (rename(oldpkgdir, newpkgdir) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000126 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800127 return -errno;
128 }
129 return 0;
130}
131
Amith Yamasani0b285492011-04-14 17:35:23 -0700132int delete_user_data(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133{
134 char pkgdir[PKG_PATH_MAX];
135
Amith Yamasani0b285492011-04-14 17:35:23 -0700136 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800137 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
Amith Yamasani0b285492011-04-14 17:35:23 -0700139 /* delete contents, excluding "lib", but not the directory itself */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 return delete_dir_contents(pkgdir, 0, "lib");
141}
142
Amith Yamasani0b285492011-04-14 17:35:23 -0700143int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
144{
145 char pkgdir[PKG_PATH_MAX];
146 char real_libdir[PKG_PATH_MAX];
147
148 // Create the data dir for the package
149 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
150 return -1;
151 }
152 if (mkdir(pkgdir, 0751) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000153 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700154 return -errno;
155 }
156 if (chown(pkgdir, uid, uid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000157 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700158 unlink(pkgdir);
159 return -errno;
160 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500161
162#ifdef HAVE_SELINUX
163 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
164 LOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
165 unlink(pkgdir);
166 return -errno;
167 }
168#endif
169
Amith Yamasani0b285492011-04-14 17:35:23 -0700170 return 0;
171}
172
173int delete_persona(uid_t persona)
174{
175 char pkgdir[PKG_PATH_MAX];
176
177 if (create_persona_path(pkgdir, persona))
178 return -1;
179
180 return delete_dir_contents(pkgdir, 1, NULL);
181}
182
Kenny Root35ab3ad2011-02-02 16:42:14 -0800183int delete_cache(const char *pkgname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184{
185 char cachedir[PKG_PATH_MAX];
186
Kenny Root86c95842011-03-31 13:16:12 -0700187 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800188 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
190 /* delete contents, not the directory, no exceptions */
191 return delete_dir_contents(cachedir, 0, 0);
192}
193
Kenny Root3e319a92010-09-07 13:58:28 -0700194static int64_t disk_free()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195{
196 struct statfs sfs;
Kenny Root86c95842011-03-31 13:16:12 -0700197 if (statfs(android_data_dir.path, &sfs) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 return sfs.f_bavail * sfs.f_bsize;
199 } else {
Steve Blockc6aacce2012-01-06 19:20:56 +0000200 ALOGE("Couldn't statfs %s: %s\n", android_data_dir.path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 return -1;
202 }
203}
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205/* Try to ensure free_size bytes of storage are available.
206 * Returns 0 on success.
207 * This is rather simple-minded because doing a full LRU would
208 * be potentially memory-intensive, and without atime it would
209 * also require that apps constantly modify file metadata even
210 * when just reading from the cache, which is pretty awful.
211 */
Kenny Root3e319a92010-09-07 13:58:28 -0700212int free_cache(int64_t free_size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213{
214 const char *name;
215 int dfd, subfd;
216 DIR *d;
217 struct dirent *de;
Kenny Root3e319a92010-09-07 13:58:28 -0700218 int64_t avail;
Kenny Rootad757e92011-11-29 15:54:55 -0800219 char datadir[PKG_PATH_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
221 avail = disk_free();
222 if (avail < 0) return -1;
223
Steve Block933e8562012-01-04 20:05:49 +0000224 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 if (avail >= free_size) return 0;
226
Kenny Rootad757e92011-11-29 15:54:55 -0800227 if (create_persona_path(datadir, 0)) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000228 ALOGE("couldn't get directory for persona 0");
Kenny Rootad757e92011-11-29 15:54:55 -0800229 return -1;
230 }
231
232 d = opendir(datadir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 if (d == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000234 ALOGE("cannot open %s: %s\n", datadir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 return -1;
236 }
237 dfd = dirfd(d);
238
239 while ((de = readdir(d))) {
240 if (de->d_type != DT_DIR) continue;
241 name = de->d_name;
242
Oscar Montemayora8529f62009-11-18 10:14:20 -0800243 /* always skip "." and ".." */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 if (name[0] == '.') {
245 if (name[1] == 0) continue;
246 if ((name[1] == '.') && (name[2] == 0)) continue;
247 }
248
249 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
250 if (subfd < 0) continue;
251
252 delete_dir_contents_fd(subfd, "cache");
253 close(subfd);
254
255 avail = disk_free();
256 if (avail >= free_size) {
257 closedir(d);
258 return 0;
259 }
260 }
261 closedir(d);
Oscar Montemayora8529f62009-11-18 10:14:20 -0800262
263 /* Fail case - not possible to free space */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 return -1;
265}
266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267int move_dex(const char *src, const char *dst)
268{
269 char src_dex[PKG_PATH_MAX];
270 char dst_dex[PKG_PATH_MAX];
271
Kenny Root86c95842011-03-31 13:16:12 -0700272 if (validate_apk_path(src)) return -1;
273 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
275 if (create_cache_path(src_dex, src)) return -1;
276 if (create_cache_path(dst_dex, dst)) return -1;
277
Steve Block06ade6a2011-10-20 11:56:00 +0100278 ALOGV("move %s -> %s\n", src_dex, dst_dex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 if (rename(src_dex, dst_dex) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000280 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 return -1;
282 } else {
283 return 0;
284 }
285}
286
287int rm_dex(const char *path)
288{
289 char dex_path[PKG_PATH_MAX];
290
Kenny Root86c95842011-03-31 13:16:12 -0700291 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 if (create_cache_path(dex_path, path)) return -1;
293
Steve Block06ade6a2011-10-20 11:56:00 +0100294 ALOGV("unlink %s\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 if (unlink(dex_path) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000296 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 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
Kenny Root86c95842011-03-31 13:16:12 -0700310 if (create_pkg_path_in_dir(pkgpath, &android_app_private_dir, pkgname, ".apk"))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 return -1;
312
313 if (stat(pkgpath, &s) < 0) return -1;
314
315 if (chown(pkgpath, s.st_uid, gid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000316 ALOGE("failed to chgrp '%s': %s\n", pkgpath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 return -1;
318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 if (chmod(pkgpath, S_IRUSR|S_IWUSR|S_IRGRP) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000320 ALOGE("failed to chmod '%s': %s\n", pkgpath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 return -1;
322 }
323
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500324#ifdef HAVE_SELINUX
325 if (selinux_android_setfilecon(pkgpath, pkgname, s.st_uid) < 0) {
326 LOGE("cannot setfilecon dir '%s': %s\n", pkgpath, strerror(errno));
327 return -1;
328 }
329#endif
330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 return 0;
332}
333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334int get_size(const char *pkgname, const char *apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700335 const char *fwdlock_apkpath, const char *asecpath,
336 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
337 int64_t* _asecsize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338{
339 DIR *d;
340 int dfd;
341 struct dirent *de;
342 struct stat s;
343 char path[PKG_PATH_MAX];
344
Kenny Root3e319a92010-09-07 13:58:28 -0700345 int64_t codesize = 0;
346 int64_t datasize = 0;
347 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700348 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349
350 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800351 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 */
Kenny Root86c95842011-03-31 13:16:12 -0700353 if (validate_system_app_path(apkpath) &&
354 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 if (stat(apkpath, &s) == 0) {
356 codesize += stat_size(&s);
357 }
358 }
359 /* count the forward locked apk as code if it is given
360 */
361 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
362 if (stat(fwdlock_apkpath, &s) == 0) {
363 codesize += stat_size(&s);
364 }
365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 /* count the cached dexfile as code */
367 if (!create_cache_path(path, apkpath)) {
368 if (stat(path, &s) == 0) {
369 codesize += stat_size(&s);
370 }
371 }
372
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700373 /* compute asec size if it is given
374 */
375 if (asecpath != NULL && asecpath[0] != '!') {
376 if (stat(asecpath, &s) == 0) {
377 asecsize += stat_size(&s);
378 }
379 }
380
Kenny Root86c95842011-03-31 13:16:12 -0700381 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, 0)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800382 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
384
385 d = opendir(path);
386 if (d == NULL) {
387 goto done;
388 }
389 dfd = dirfd(d);
390
Kenny Root86c95842011-03-31 13:16:12 -0700391 /* most stuff in the pkgdir is data, except for the "cache"
392 * directory and below, which is cache, and the "lib" directory
393 * and below, which is code...
394 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 while ((de = readdir(d))) {
396 const char *name = de->d_name;
397
398 if (de->d_type == DT_DIR) {
399 int subfd;
400 /* always skip "." and ".." */
401 if (name[0] == '.') {
402 if (name[1] == 0) continue;
403 if ((name[1] == '.') && (name[2] == 0)) continue;
404 }
405 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
406 if (subfd >= 0) {
Kenny Root3e319a92010-09-07 13:58:28 -0700407 int64_t size = calculate_dir_size(subfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 if (!strcmp(name,"lib")) {
409 codesize += size;
410 } else if(!strcmp(name,"cache")) {
411 cachesize += size;
412 } else {
413 datasize += size;
414 }
415 }
416 } else {
417 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
418 datasize += stat_size(&s);
419 }
420 }
421 }
422 closedir(d);
423done:
424 *_codesize = codesize;
425 *_datasize = datasize;
426 *_cachesize = cachesize;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700427 *_asecsize = asecsize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 return 0;
429}
430
431
432/* a simpler version of dexOptGenerateCacheFileName() */
433int create_cache_path(char path[PKG_PATH_MAX], const char *src)
434{
435 char *tmp;
436 int srclen;
437 int dstlen;
438
439 srclen = strlen(src);
440
441 /* demand that we are an absolute path */
442 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
443 return -1;
444 }
445
446 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
447 return -1;
448 }
449
450 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
451 strlen(DALVIK_CACHE_POSTFIX) + 1;
452
453 if (dstlen > PKG_PATH_MAX) {
454 return -1;
455 }
456
457 sprintf(path,"%s%s%s",
458 DALVIK_CACHE_PREFIX,
459 src + 1, /* skip the leading / */
460 DALVIK_CACHE_POSTFIX);
461
462 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
463 if (*tmp == '/') {
464 *tmp = '@';
465 }
466 }
467
468 return 0;
469}
470
471static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
472 const char* dexopt_flags)
473{
474 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
475 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
476 char zip_num[MAX_INT_LEN];
477 char odex_num[MAX_INT_LEN];
478
479 sprintf(zip_num, "%d", zip_fd);
480 sprintf(odex_num, "%d", odex_fd);
481
482 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
483 dexopt_flags, (char*) NULL);
Steve Blockc6aacce2012-01-06 19:20:56 +0000484 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485}
486
487static int wait_dexopt(pid_t pid, const char* apk_path)
488{
489 int status;
490 pid_t got_pid;
491
492 /*
493 * Wait for the optimization process to finish.
494 */
495 while (1) {
496 got_pid = waitpid(pid, &status, 0);
497 if (got_pid == -1 && errno == EINTR) {
498 printf("waitpid interrupted, retrying\n");
499 } else {
500 break;
501 }
502 }
503 if (got_pid != pid) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000504 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 (int) pid, (int) got_pid, strerror(errno));
506 return 1;
507 }
508
509 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Steve Block06ade6a2011-10-20 11:56:00 +0100510 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 return 0;
512 } else {
Steve Blocka51f0e72012-01-05 23:22:43 +0000513 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 apk_path, status);
515 return status; /* always nonzero */
516 }
517}
518
519int dexopt(const char *apk_path, uid_t uid, int is_public)
520{
521 struct utimbuf ut;
522 struct stat apk_stat, dex_stat;
523 char dex_path[PKG_PATH_MAX];
524 char dexopt_flags[PROPERTY_VALUE_MAX];
525 char *end;
526 int res, zip_fd=-1, odex_fd=-1;
527
528 /* Before anything else: is there a .odex file? If so, we have
529 * pre-optimized the apk and there is nothing to do here.
530 */
531 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
532 return -1;
533 }
534
535 /* platform-specific flags affecting optimization and verification */
536 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
537
538 strcpy(dex_path, apk_path);
539 end = strrchr(dex_path, '.');
540 if (end != NULL) {
541 strcpy(end, ".odex");
542 if (stat(dex_path, &dex_stat) == 0) {
543 return 0;
544 }
545 }
546
547 if (create_cache_path(dex_path, apk_path)) {
548 return -1;
549 }
550
551 memset(&apk_stat, 0, sizeof(apk_stat));
552 stat(apk_path, &apk_stat);
553
554 zip_fd = open(apk_path, O_RDONLY, 0);
555 if (zip_fd < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000556 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 return -1;
558 }
559
560 unlink(dex_path);
561 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
562 if (odex_fd < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000563 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 goto fail;
565 }
566 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000567 ALOGE("dexopt cannot chown '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 goto fail;
569 }
570 if (fchmod(odex_fd,
571 S_IRUSR|S_IWUSR|S_IRGRP |
572 (is_public ? S_IROTH : 0)) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000573 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 goto fail;
575 }
576
Steve Block06ade6a2011-10-20 11:56:00 +0100577 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578
579 pid_t pid;
580 pid = fork();
581 if (pid == 0) {
582 /* child -- drop privileges before continuing */
583 if (setgid(uid) != 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000584 ALOGE("setgid(%d) failed during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 exit(64);
586 }
587 if (setuid(uid) != 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000588 ALOGE("setuid(%d) during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 exit(65);
590 }
591 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000592 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 exit(66);
594 }
595
596 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
597 exit(67); /* only get here on exec failure */
598 } else {
599 res = wait_dexopt(pid, apk_path);
600 if (res != 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000601 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 goto fail;
603 }
604 }
605
606 ut.actime = apk_stat.st_atime;
607 ut.modtime = apk_stat.st_mtime;
608 utime(dex_path, &ut);
609
610 close(odex_fd);
611 close(zip_fd);
612 return 0;
613
614fail:
615 if (odex_fd >= 0) {
616 close(odex_fd);
617 unlink(dex_path);
618 }
619 if (zip_fd >= 0) {
620 close(zip_fd);
621 }
622 return -1;
623}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800624
Dianne Hackbornc1552392010-03-03 16:19:01 -0800625void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
626 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800627{
628 while (path[basepos] != 0) {
629 if (path[basepos] == '/') {
630 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800631 if (lstat(path, statbuf) < 0) {
Steve Block06ade6a2011-10-20 11:56:00 +0100632 ALOGV("Making directory: %s\n", path);
Dianne Hackbornc1552392010-03-03 16:19:01 -0800633 if (mkdir(path, mode) == 0) {
634 chown(path, uid, gid);
635 } else {
Steve Blocka51f0e72012-01-05 23:22:43 +0000636 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
Dianne Hackbornc1552392010-03-03 16:19:01 -0800637 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800638 }
639 path[basepos] = '/';
640 basepos++;
641 }
642 basepos++;
643 }
644}
645
Dianne Hackbornc1552392010-03-03 16:19:01 -0800646int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
647 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800648{
649 DIR *d;
650 struct dirent *de;
651 int res;
652
653 int srcend = strlen(srcpath);
654 int dstend = strlen(dstpath);
655
656 if (lstat(srcpath, statbuf) < 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000657 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800658 return 1;
659 }
660
661 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800662 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
663 dstuid, dstgid, statbuf);
Steve Block06ade6a2011-10-20 11:56:00 +0100664 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800665 if (rename(srcpath, dstpath) >= 0) {
666 if (chown(dstpath, dstuid, dstgid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000667 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800668 unlink(dstpath);
669 return 1;
670 }
671 } else {
Steve Blocka51f0e72012-01-05 23:22:43 +0000672 ALOGW("Unable to rename %s to %s: %s\n",
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800673 srcpath, dstpath, strerror(errno));
674 return 1;
675 }
676 return 0;
677 }
678
679 d = opendir(srcpath);
680 if (d == NULL) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000681 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800682 return 1;
683 }
684
685 res = 0;
686
687 while ((de = readdir(d))) {
688 const char *name = de->d_name;
689 /* always skip "." and ".." */
690 if (name[0] == '.') {
691 if (name[1] == 0) continue;
692 if ((name[1] == '.') && (name[2] == 0)) continue;
693 }
694
695 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000696 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800697 continue;
698 }
699
700 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000701 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800702 continue;
703 }
704
705 srcpath[srcend] = dstpath[dstend] = '/';
706 strcpy(srcpath+srcend+1, name);
707 strcpy(dstpath+dstend+1, name);
708
Dianne Hackbornc1552392010-03-03 16:19:01 -0800709 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800710 res = 1;
711 }
712
713 // Note: we will be leaving empty directories behind in srcpath,
714 // but that is okay, the package manager will be erasing all of the
715 // data associated with .apks that disappear.
716
717 srcpath[srcend] = dstpath[dstend] = 0;
718 }
719
720 closedir(d);
721 return res;
722}
723
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800724int movefiles()
725{
726 DIR *d;
727 int dfd, subfd;
728 struct dirent *de;
729 struct stat s;
730 char buf[PKG_PATH_MAX+1];
731 int bufp, bufe, bufi, readlen;
732
733 char srcpkg[PKG_NAME_MAX];
734 char dstpkg[PKG_NAME_MAX];
735 char srcpath[PKG_PATH_MAX];
736 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800737 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800738 int hasspace;
739
740 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
741 if (d == NULL) {
742 goto done;
743 }
744 dfd = dirfd(d);
745
746 /* Iterate through all files in the directory, executing the
747 * file movements requested there-in.
748 */
749 while ((de = readdir(d))) {
750 const char *name = de->d_name;
751
752 if (de->d_type == DT_DIR) {
753 continue;
754 } else {
755 subfd = openat(dfd, name, O_RDONLY);
756 if (subfd < 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000757 ALOGW("Unable to open update commands at %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800758 UPDATE_COMMANDS_DIR_PREFIX, name);
759 continue;
760 }
761
762 bufp = 0;
763 bufe = 0;
764 buf[PKG_PATH_MAX] = 0;
765 srcpkg[0] = dstpkg[0] = 0;
766 while (1) {
767 bufi = bufp;
768 while (bufi < bufe && buf[bufi] != '\n') {
769 bufi++;
770 }
771 if (bufi < bufe) {
772 buf[bufi] = 0;
Steve Block06ade6a2011-10-20 11:56:00 +0100773 ALOGV("Processing line: %s\n", buf+bufp);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800774 hasspace = 0;
775 while (bufp < bufi && isspace(buf[bufp])) {
776 hasspace = 1;
777 bufp++;
778 }
779 if (buf[bufp] == '#' || bufp == bufi) {
780 // skip comments and empty lines.
781 } else if (hasspace) {
782 if (dstpkg[0] == 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000783 ALOGW("Path before package line in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800784 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
785 } else if (srcpkg[0] == 0) {
786 // Skip -- source package no longer exists.
787 } else {
Steve Block06ade6a2011-10-20 11:56:00 +0100788 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
Kenny Root86c95842011-03-31 13:16:12 -0700789 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
790 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800791 movefileordir(srcpath, dstpath,
792 strlen(dstpath)-strlen(buf+bufp),
793 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800794 }
795 }
796 } else {
797 char* div = strchr(buf+bufp, ':');
798 if (div == NULL) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000799 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800800 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
801 } else {
802 *div = 0;
803 div++;
804 if (strlen(buf+bufp) < PKG_NAME_MAX) {
805 strcpy(dstpkg, buf+bufp);
806 } else {
807 srcpkg[0] = dstpkg[0] = 0;
Steve Blocka51f0e72012-01-05 23:22:43 +0000808 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800809 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
810 }
811 if (strlen(div) < PKG_NAME_MAX) {
812 strcpy(srcpkg, div);
813 } else {
814 srcpkg[0] = dstpkg[0] = 0;
Steve Blocka51f0e72012-01-05 23:22:43 +0000815 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800816 UPDATE_COMMANDS_DIR_PREFIX, name, div);
817 }
818 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700819 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800820 if (lstat(srcpath, &s) < 0) {
821 // Package no longer exists -- skip.
822 srcpkg[0] = 0;
823 }
824 } else {
825 srcpkg[0] = 0;
Steve Blocka51f0e72012-01-05 23:22:43 +0000826 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800827 div, UPDATE_COMMANDS_DIR_PREFIX, name);
828 }
829 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700830 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800831 if (lstat(dstpath, &s) == 0) {
832 dstuid = s.st_uid;
833 dstgid = s.st_gid;
834 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800835 // Destination package doesn't
836 // exist... due to original-package,
837 // this is normal, so don't be
838 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800839 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800840 }
841 } else {
842 srcpkg[0] = 0;
Steve Blocka51f0e72012-01-05 23:22:43 +0000843 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800844 div, UPDATE_COMMANDS_DIR_PREFIX, name);
845 }
846 }
Steve Block06ade6a2011-10-20 11:56:00 +0100847 ALOGV("Transfering from %s to %s: uid=%d\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800848 srcpkg, dstpkg, dstuid);
849 }
850 }
851 }
852 bufp = bufi+1;
853 } else {
854 if (bufp == 0) {
855 if (bufp < bufe) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000856 ALOGW("Line too long in %s%s, skipping: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800857 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
858 }
859 } else if (bufp < bufe) {
860 memcpy(buf, buf+bufp, bufe-bufp);
861 bufe -= bufp;
862 bufp = 0;
863 }
864 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
865 if (readlen < 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000866 ALOGW("Failure reading update commands in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800867 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
868 break;
869 } else if (readlen == 0) {
870 break;
871 }
872 bufe += readlen;
873 buf[bufe] = 0;
Steve Block06ade6a2011-10-20 11:56:00 +0100874 ALOGV("Read buf: %s\n", buf);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800875 }
876 }
877 close(subfd);
878 }
879 }
880 closedir(d);
881done:
882 return 0;
883}
Kenny Root6a6b0072010-10-07 16:46:10 -0700884
885int linklib(const char* dataDir, const char* asecLibDir)
886{
887 char libdir[PKG_PATH_MAX];
888 struct stat s, libStat;
889 int rc = 0;
890
891 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
892 if (libdirLen >= PKG_PATH_MAX) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000893 ALOGE("library dir len too large");
Kenny Root0332d1c2010-10-21 16:14:06 -0700894 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -0700895 }
896
897 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000898 ALOGE("library dir not written successfully: %s\n", strerror(errno));
Kenny Root0332d1c2010-10-21 16:14:06 -0700899 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -0700900 }
901
902 if (stat(dataDir, &s) < 0) return -1;
903
904 if (chown(dataDir, 0, 0) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000905 ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700906 return -1;
907 }
908
909 if (chmod(dataDir, 0700) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000910 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700911 rc = -1;
912 goto out;
913 }
914
915 if (lstat(libdir, &libStat) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000916 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700917 rc = -1;
918 goto out;
919 }
920
921 if (S_ISDIR(libStat.st_mode)) {
922 if (delete_dir_contents(libdir, 1, 0) < 0) {
923 rc = -1;
924 goto out;
925 }
926 } else if (S_ISLNK(libStat.st_mode)) {
927 if (unlink(libdir) < 0) {
928 rc = -1;
929 goto out;
930 }
931 }
932
933 if (symlink(asecLibDir, libdir) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000934 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libdir, asecLibDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700935 rc = -errno;
936 goto out;
937 }
938
939 if (lchown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000940 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700941 unlink(libdir);
942 rc = -errno;
943 goto out;
944 }
945
946out:
947 if (chmod(dataDir, s.st_mode) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000948 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700949 return -errno;
950 }
951
952 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000953 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700954 return -errno;
955 }
956
957 return rc;
958}
959
960int unlinklib(const char* dataDir)
961{
962 char libdir[PKG_PATH_MAX];
963 struct stat s, libStat;
964 int rc = 0;
965
966 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
967 if (libdirLen >= PKG_PATH_MAX) {
968 return -1;
969 }
970
971 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000972 ALOGE("library dir not written successfully: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700973 return -1;
974 }
975
976 if (stat(dataDir, &s) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000977 ALOGE("couldn't state data dir");
Kenny Root6a6b0072010-10-07 16:46:10 -0700978 return -1;
979 }
980
981 if (chown(dataDir, 0, 0) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000982 ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700983 return -1;
984 }
985
986 if (chmod(dataDir, 0700) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000987 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700988 rc = -1;
989 goto out;
990 }
991
992 if (lstat(libdir, &libStat) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000993 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700994 rc = -1;
995 goto out;
996 }
997
998 if (S_ISDIR(libStat.st_mode)) {
999 if (delete_dir_contents(libdir, 1, 0) < 0) {
1000 rc = -1;
1001 goto out;
1002 }
1003 } else if (S_ISLNK(libStat.st_mode)) {
1004 if (unlink(libdir) < 0) {
1005 rc = -1;
1006 goto out;
1007 }
1008 }
1009
1010 if (mkdir(libdir, 0755) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +00001011 ALOGE("cannot create dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001012 rc = -errno;
1013 goto out;
1014 }
1015
1016 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +00001017 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001018 unlink(libdir);
1019 rc = -errno;
1020 goto out;
1021 }
1022
1023out:
1024 if (chmod(dataDir, s.st_mode) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +00001025 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001026 return -1;
1027 }
1028
1029 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +00001030 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001031 return -1;
1032 }
1033
1034 return rc;
1035}