blob: dd92bbe499bc3e3afc1250650db4685e2a44c3ff [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)) {
Steve Blockc6aacce2012-01-06 19:20:56 +000033 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034 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)) {
Steve Blockc6aacce2012-01-06 19:20:56 +000038 ALOGE("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)) {
Steve Blockc6aacce2012-01-06 19:20:56 +000043 ALOGE("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) {
Steve Blockc6aacce2012-01-06 19:20:56 +000048 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 return -errno;
50 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070051 if (chmod(pkgdir, 0751) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000052 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070053 unlink(pkgdir);
54 return -errno;
55 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 if (chown(pkgdir, uid, gid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000057 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 unlink(pkgdir);
59 return -errno;
60 }
61 if (mkdir(libdir, 0755) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000062 ALOGE("cannot create dir '%s': %s\n", libdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 unlink(pkgdir);
64 return -errno;
65 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070066 if (chmod(libdir, 0755) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +000067 ALOGE("cannot chmod dir '%s': %s\n", libdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070068 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) {
Steve Blockc6aacce2012-01-06 19:20:56 +000073 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 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) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000103 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800104 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) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000130 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700131 return -errno;
132 }
133 if (chown(pkgdir, uid, uid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000134 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700135 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 {
Steve Blockc6aacce2012-01-06 19:20:56 +0000168 ALOGE("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;
Kenny Rootad757e92011-11-29 15:54:55 -0800187 char datadir[PKG_PATH_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
189 avail = disk_free();
190 if (avail < 0) return -1;
191
Steve Block933e8562012-01-04 20:05:49 +0000192 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 if (avail >= free_size) return 0;
194
Kenny Rootad757e92011-11-29 15:54:55 -0800195 if (create_persona_path(datadir, 0)) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000196 ALOGE("couldn't get directory for persona 0");
Kenny Rootad757e92011-11-29 15:54:55 -0800197 return -1;
198 }
199
200 d = opendir(datadir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 if (d == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000202 ALOGE("cannot open %s: %s\n", datadir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 return -1;
204 }
205 dfd = dirfd(d);
206
207 while ((de = readdir(d))) {
208 if (de->d_type != DT_DIR) continue;
209 name = de->d_name;
210
Oscar Montemayora8529f62009-11-18 10:14:20 -0800211 /* always skip "." and ".." */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 if (name[0] == '.') {
213 if (name[1] == 0) continue;
214 if ((name[1] == '.') && (name[2] == 0)) continue;
215 }
216
217 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
218 if (subfd < 0) continue;
219
220 delete_dir_contents_fd(subfd, "cache");
221 close(subfd);
222
223 avail = disk_free();
224 if (avail >= free_size) {
225 closedir(d);
226 return 0;
227 }
228 }
229 closedir(d);
Oscar Montemayora8529f62009-11-18 10:14:20 -0800230
231 /* Fail case - not possible to free space */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 return -1;
233}
234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235int move_dex(const char *src, const char *dst)
236{
237 char src_dex[PKG_PATH_MAX];
238 char dst_dex[PKG_PATH_MAX];
239
Kenny Root86c95842011-03-31 13:16:12 -0700240 if (validate_apk_path(src)) return -1;
241 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
243 if (create_cache_path(src_dex, src)) return -1;
244 if (create_cache_path(dst_dex, dst)) return -1;
245
Steve Block06ade6a2011-10-20 11:56:00 +0100246 ALOGV("move %s -> %s\n", src_dex, dst_dex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 if (rename(src_dex, dst_dex) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000248 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 return -1;
250 } else {
251 return 0;
252 }
253}
254
255int rm_dex(const char *path)
256{
257 char dex_path[PKG_PATH_MAX];
258
Kenny Root86c95842011-03-31 13:16:12 -0700259 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 if (create_cache_path(dex_path, path)) return -1;
261
Steve Block06ade6a2011-10-20 11:56:00 +0100262 ALOGV("unlink %s\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 if (unlink(dex_path) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000264 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 return -1;
266 } else {
267 return 0;
268 }
269}
270
271int protect(char *pkgname, gid_t gid)
272{
273 struct stat s;
274 char pkgpath[PKG_PATH_MAX];
275
276 if (gid < AID_SYSTEM) return -1;
277
Kenny Root86c95842011-03-31 13:16:12 -0700278 if (create_pkg_path_in_dir(pkgpath, &android_app_private_dir, pkgname, ".apk"))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 return -1;
280
281 if (stat(pkgpath, &s) < 0) return -1;
282
283 if (chown(pkgpath, s.st_uid, gid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000284 ALOGE("failed to chgrp '%s': %s\n", pkgpath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 return -1;
286 }
287
288 if (chmod(pkgpath, S_IRUSR|S_IWUSR|S_IRGRP) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000289 ALOGE("failed to chmod '%s': %s\n", pkgpath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 return -1;
291 }
292
293 return 0;
294}
295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296int get_size(const char *pkgname, const char *apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700297 const char *fwdlock_apkpath, const char *asecpath,
298 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
299 int64_t* _asecsize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300{
301 DIR *d;
302 int dfd;
303 struct dirent *de;
304 struct stat s;
305 char path[PKG_PATH_MAX];
306
Kenny Root3e319a92010-09-07 13:58:28 -0700307 int64_t codesize = 0;
308 int64_t datasize = 0;
309 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700310 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311
312 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800313 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 */
Kenny Root86c95842011-03-31 13:16:12 -0700315 if (validate_system_app_path(apkpath) &&
316 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 if (stat(apkpath, &s) == 0) {
318 codesize += stat_size(&s);
319 }
320 }
321 /* count the forward locked apk as code if it is given
322 */
323 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
324 if (stat(fwdlock_apkpath, &s) == 0) {
325 codesize += stat_size(&s);
326 }
327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 /* count the cached dexfile as code */
329 if (!create_cache_path(path, apkpath)) {
330 if (stat(path, &s) == 0) {
331 codesize += stat_size(&s);
332 }
333 }
334
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700335 /* compute asec size if it is given
336 */
337 if (asecpath != NULL && asecpath[0] != '!') {
338 if (stat(asecpath, &s) == 0) {
339 asecsize += stat_size(&s);
340 }
341 }
342
Kenny Root86c95842011-03-31 13:16:12 -0700343 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, 0)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800344 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346
347 d = opendir(path);
348 if (d == NULL) {
349 goto done;
350 }
351 dfd = dirfd(d);
352
Kenny Root86c95842011-03-31 13:16:12 -0700353 /* most stuff in the pkgdir is data, except for the "cache"
354 * directory and below, which is cache, and the "lib" directory
355 * and below, which is code...
356 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 while ((de = readdir(d))) {
358 const char *name = de->d_name;
359
360 if (de->d_type == DT_DIR) {
361 int subfd;
362 /* always skip "." and ".." */
363 if (name[0] == '.') {
364 if (name[1] == 0) continue;
365 if ((name[1] == '.') && (name[2] == 0)) continue;
366 }
367 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
368 if (subfd >= 0) {
Kenny Root3e319a92010-09-07 13:58:28 -0700369 int64_t size = calculate_dir_size(subfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 if (!strcmp(name,"lib")) {
371 codesize += size;
372 } else if(!strcmp(name,"cache")) {
373 cachesize += size;
374 } else {
375 datasize += size;
376 }
377 }
378 } else {
379 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
380 datasize += stat_size(&s);
381 }
382 }
383 }
384 closedir(d);
385done:
386 *_codesize = codesize;
387 *_datasize = datasize;
388 *_cachesize = cachesize;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700389 *_asecsize = asecsize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 return 0;
391}
392
393
394/* a simpler version of dexOptGenerateCacheFileName() */
395int create_cache_path(char path[PKG_PATH_MAX], const char *src)
396{
397 char *tmp;
398 int srclen;
399 int dstlen;
400
401 srclen = strlen(src);
402
403 /* demand that we are an absolute path */
404 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
405 return -1;
406 }
407
408 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
409 return -1;
410 }
411
412 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
413 strlen(DALVIK_CACHE_POSTFIX) + 1;
414
415 if (dstlen > PKG_PATH_MAX) {
416 return -1;
417 }
418
419 sprintf(path,"%s%s%s",
420 DALVIK_CACHE_PREFIX,
421 src + 1, /* skip the leading / */
422 DALVIK_CACHE_POSTFIX);
423
424 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
425 if (*tmp == '/') {
426 *tmp = '@';
427 }
428 }
429
430 return 0;
431}
432
433static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
434 const char* dexopt_flags)
435{
436 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
437 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
438 char zip_num[MAX_INT_LEN];
439 char odex_num[MAX_INT_LEN];
440
441 sprintf(zip_num, "%d", zip_fd);
442 sprintf(odex_num, "%d", odex_fd);
443
444 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
445 dexopt_flags, (char*) NULL);
Steve Blockc6aacce2012-01-06 19:20:56 +0000446 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447}
448
449static int wait_dexopt(pid_t pid, const char* apk_path)
450{
451 int status;
452 pid_t got_pid;
453
454 /*
455 * Wait for the optimization process to finish.
456 */
457 while (1) {
458 got_pid = waitpid(pid, &status, 0);
459 if (got_pid == -1 && errno == EINTR) {
460 printf("waitpid interrupted, retrying\n");
461 } else {
462 break;
463 }
464 }
465 if (got_pid != pid) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000466 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 (int) pid, (int) got_pid, strerror(errno));
468 return 1;
469 }
470
471 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Steve Block06ade6a2011-10-20 11:56:00 +0100472 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 return 0;
474 } else {
Steve Blocka51f0e72012-01-05 23:22:43 +0000475 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 apk_path, status);
477 return status; /* always nonzero */
478 }
479}
480
481int dexopt(const char *apk_path, uid_t uid, int is_public)
482{
483 struct utimbuf ut;
484 struct stat apk_stat, dex_stat;
485 char dex_path[PKG_PATH_MAX];
486 char dexopt_flags[PROPERTY_VALUE_MAX];
487 char *end;
488 int res, zip_fd=-1, odex_fd=-1;
489
490 /* Before anything else: is there a .odex file? If so, we have
491 * pre-optimized the apk and there is nothing to do here.
492 */
493 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
494 return -1;
495 }
496
497 /* platform-specific flags affecting optimization and verification */
498 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
499
500 strcpy(dex_path, apk_path);
501 end = strrchr(dex_path, '.');
502 if (end != NULL) {
503 strcpy(end, ".odex");
504 if (stat(dex_path, &dex_stat) == 0) {
505 return 0;
506 }
507 }
508
509 if (create_cache_path(dex_path, apk_path)) {
510 return -1;
511 }
512
513 memset(&apk_stat, 0, sizeof(apk_stat));
514 stat(apk_path, &apk_stat);
515
516 zip_fd = open(apk_path, O_RDONLY, 0);
517 if (zip_fd < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000518 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 return -1;
520 }
521
522 unlink(dex_path);
523 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
524 if (odex_fd < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000525 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 goto fail;
527 }
528 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000529 ALOGE("dexopt cannot chown '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 goto fail;
531 }
532 if (fchmod(odex_fd,
533 S_IRUSR|S_IWUSR|S_IRGRP |
534 (is_public ? S_IROTH : 0)) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000535 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 goto fail;
537 }
538
Steve Block06ade6a2011-10-20 11:56:00 +0100539 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540
541 pid_t pid;
542 pid = fork();
543 if (pid == 0) {
544 /* child -- drop privileges before continuing */
545 if (setgid(uid) != 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000546 ALOGE("setgid(%d) failed during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 exit(64);
548 }
549 if (setuid(uid) != 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000550 ALOGE("setuid(%d) during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 exit(65);
552 }
553 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000554 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 exit(66);
556 }
557
558 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
559 exit(67); /* only get here on exec failure */
560 } else {
561 res = wait_dexopt(pid, apk_path);
562 if (res != 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000563 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 goto fail;
565 }
566 }
567
568 ut.actime = apk_stat.st_atime;
569 ut.modtime = apk_stat.st_mtime;
570 utime(dex_path, &ut);
571
572 close(odex_fd);
573 close(zip_fd);
574 return 0;
575
576fail:
577 if (odex_fd >= 0) {
578 close(odex_fd);
579 unlink(dex_path);
580 }
581 if (zip_fd >= 0) {
582 close(zip_fd);
583 }
584 return -1;
585}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800586
Dianne Hackbornc1552392010-03-03 16:19:01 -0800587void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
588 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800589{
590 while (path[basepos] != 0) {
591 if (path[basepos] == '/') {
592 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800593 if (lstat(path, statbuf) < 0) {
Steve Block06ade6a2011-10-20 11:56:00 +0100594 ALOGV("Making directory: %s\n", path);
Dianne Hackbornc1552392010-03-03 16:19:01 -0800595 if (mkdir(path, mode) == 0) {
596 chown(path, uid, gid);
597 } else {
Steve Blocka51f0e72012-01-05 23:22:43 +0000598 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
Dianne Hackbornc1552392010-03-03 16:19:01 -0800599 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800600 }
601 path[basepos] = '/';
602 basepos++;
603 }
604 basepos++;
605 }
606}
607
Dianne Hackbornc1552392010-03-03 16:19:01 -0800608int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
609 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800610{
611 DIR *d;
612 struct dirent *de;
613 int res;
614
615 int srcend = strlen(srcpath);
616 int dstend = strlen(dstpath);
617
618 if (lstat(srcpath, statbuf) < 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000619 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800620 return 1;
621 }
622
623 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800624 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
625 dstuid, dstgid, statbuf);
Steve Block06ade6a2011-10-20 11:56:00 +0100626 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800627 if (rename(srcpath, dstpath) >= 0) {
628 if (chown(dstpath, dstuid, dstgid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000629 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800630 unlink(dstpath);
631 return 1;
632 }
633 } else {
Steve Blocka51f0e72012-01-05 23:22:43 +0000634 ALOGW("Unable to rename %s to %s: %s\n",
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800635 srcpath, dstpath, strerror(errno));
636 return 1;
637 }
638 return 0;
639 }
640
641 d = opendir(srcpath);
642 if (d == NULL) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000643 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800644 return 1;
645 }
646
647 res = 0;
648
649 while ((de = readdir(d))) {
650 const char *name = de->d_name;
651 /* always skip "." and ".." */
652 if (name[0] == '.') {
653 if (name[1] == 0) continue;
654 if ((name[1] == '.') && (name[2] == 0)) continue;
655 }
656
657 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000658 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800659 continue;
660 }
661
662 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000663 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800664 continue;
665 }
666
667 srcpath[srcend] = dstpath[dstend] = '/';
668 strcpy(srcpath+srcend+1, name);
669 strcpy(dstpath+dstend+1, name);
670
Dianne Hackbornc1552392010-03-03 16:19:01 -0800671 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800672 res = 1;
673 }
674
675 // Note: we will be leaving empty directories behind in srcpath,
676 // but that is okay, the package manager will be erasing all of the
677 // data associated with .apks that disappear.
678
679 srcpath[srcend] = dstpath[dstend] = 0;
680 }
681
682 closedir(d);
683 return res;
684}
685
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800686int movefiles()
687{
688 DIR *d;
689 int dfd, subfd;
690 struct dirent *de;
691 struct stat s;
692 char buf[PKG_PATH_MAX+1];
693 int bufp, bufe, bufi, readlen;
694
695 char srcpkg[PKG_NAME_MAX];
696 char dstpkg[PKG_NAME_MAX];
697 char srcpath[PKG_PATH_MAX];
698 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800699 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800700 int hasspace;
701
702 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
703 if (d == NULL) {
704 goto done;
705 }
706 dfd = dirfd(d);
707
708 /* Iterate through all files in the directory, executing the
709 * file movements requested there-in.
710 */
711 while ((de = readdir(d))) {
712 const char *name = de->d_name;
713
714 if (de->d_type == DT_DIR) {
715 continue;
716 } else {
717 subfd = openat(dfd, name, O_RDONLY);
718 if (subfd < 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000719 ALOGW("Unable to open update commands at %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800720 UPDATE_COMMANDS_DIR_PREFIX, name);
721 continue;
722 }
723
724 bufp = 0;
725 bufe = 0;
726 buf[PKG_PATH_MAX] = 0;
727 srcpkg[0] = dstpkg[0] = 0;
728 while (1) {
729 bufi = bufp;
730 while (bufi < bufe && buf[bufi] != '\n') {
731 bufi++;
732 }
733 if (bufi < bufe) {
734 buf[bufi] = 0;
Steve Block06ade6a2011-10-20 11:56:00 +0100735 ALOGV("Processing line: %s\n", buf+bufp);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800736 hasspace = 0;
737 while (bufp < bufi && isspace(buf[bufp])) {
738 hasspace = 1;
739 bufp++;
740 }
741 if (buf[bufp] == '#' || bufp == bufi) {
742 // skip comments and empty lines.
743 } else if (hasspace) {
744 if (dstpkg[0] == 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000745 ALOGW("Path before package line in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800746 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
747 } else if (srcpkg[0] == 0) {
748 // Skip -- source package no longer exists.
749 } else {
Steve Block06ade6a2011-10-20 11:56:00 +0100750 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
Kenny Root86c95842011-03-31 13:16:12 -0700751 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
752 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800753 movefileordir(srcpath, dstpath,
754 strlen(dstpath)-strlen(buf+bufp),
755 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800756 }
757 }
758 } else {
759 char* div = strchr(buf+bufp, ':');
760 if (div == NULL) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000761 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800762 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
763 } else {
764 *div = 0;
765 div++;
766 if (strlen(buf+bufp) < PKG_NAME_MAX) {
767 strcpy(dstpkg, buf+bufp);
768 } else {
769 srcpkg[0] = dstpkg[0] = 0;
Steve Blocka51f0e72012-01-05 23:22:43 +0000770 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800771 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
772 }
773 if (strlen(div) < PKG_NAME_MAX) {
774 strcpy(srcpkg, div);
775 } else {
776 srcpkg[0] = dstpkg[0] = 0;
Steve Blocka51f0e72012-01-05 23:22:43 +0000777 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800778 UPDATE_COMMANDS_DIR_PREFIX, name, div);
779 }
780 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700781 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800782 if (lstat(srcpath, &s) < 0) {
783 // Package no longer exists -- skip.
784 srcpkg[0] = 0;
785 }
786 } else {
787 srcpkg[0] = 0;
Steve Blocka51f0e72012-01-05 23:22:43 +0000788 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800789 div, UPDATE_COMMANDS_DIR_PREFIX, name);
790 }
791 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700792 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800793 if (lstat(dstpath, &s) == 0) {
794 dstuid = s.st_uid;
795 dstgid = s.st_gid;
796 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800797 // Destination package doesn't
798 // exist... due to original-package,
799 // this is normal, so don't be
800 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800801 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800802 }
803 } else {
804 srcpkg[0] = 0;
Steve Blocka51f0e72012-01-05 23:22:43 +0000805 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800806 div, UPDATE_COMMANDS_DIR_PREFIX, name);
807 }
808 }
Steve Block06ade6a2011-10-20 11:56:00 +0100809 ALOGV("Transfering from %s to %s: uid=%d\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800810 srcpkg, dstpkg, dstuid);
811 }
812 }
813 }
814 bufp = bufi+1;
815 } else {
816 if (bufp == 0) {
817 if (bufp < bufe) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000818 ALOGW("Line too long in %s%s, skipping: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800819 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
820 }
821 } else if (bufp < bufe) {
822 memcpy(buf, buf+bufp, bufe-bufp);
823 bufe -= bufp;
824 bufp = 0;
825 }
826 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
827 if (readlen < 0) {
Steve Blocka51f0e72012-01-05 23:22:43 +0000828 ALOGW("Failure reading update commands in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800829 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
830 break;
831 } else if (readlen == 0) {
832 break;
833 }
834 bufe += readlen;
835 buf[bufe] = 0;
Steve Block06ade6a2011-10-20 11:56:00 +0100836 ALOGV("Read buf: %s\n", buf);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800837 }
838 }
839 close(subfd);
840 }
841 }
842 closedir(d);
843done:
844 return 0;
845}
Kenny Root6a6b0072010-10-07 16:46:10 -0700846
847int linklib(const char* dataDir, const char* asecLibDir)
848{
849 char libdir[PKG_PATH_MAX];
850 struct stat s, libStat;
851 int rc = 0;
852
853 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
854 if (libdirLen >= PKG_PATH_MAX) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000855 ALOGE("library dir len too large");
Kenny Root0332d1c2010-10-21 16:14:06 -0700856 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -0700857 }
858
859 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000860 ALOGE("library dir not written successfully: %s\n", strerror(errno));
Kenny Root0332d1c2010-10-21 16:14:06 -0700861 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -0700862 }
863
864 if (stat(dataDir, &s) < 0) return -1;
865
866 if (chown(dataDir, 0, 0) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000867 ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700868 return -1;
869 }
870
871 if (chmod(dataDir, 0700) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000872 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700873 rc = -1;
874 goto out;
875 }
876
877 if (lstat(libdir, &libStat) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000878 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700879 rc = -1;
880 goto out;
881 }
882
883 if (S_ISDIR(libStat.st_mode)) {
884 if (delete_dir_contents(libdir, 1, 0) < 0) {
885 rc = -1;
886 goto out;
887 }
888 } else if (S_ISLNK(libStat.st_mode)) {
889 if (unlink(libdir) < 0) {
890 rc = -1;
891 goto out;
892 }
893 }
894
895 if (symlink(asecLibDir, libdir) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000896 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libdir, asecLibDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700897 rc = -errno;
898 goto out;
899 }
900
901 if (lchown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000902 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700903 unlink(libdir);
904 rc = -errno;
905 goto out;
906 }
907
908out:
909 if (chmod(dataDir, s.st_mode) < 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 return -errno;
912 }
913
914 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000915 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700916 return -errno;
917 }
918
919 return rc;
920}
921
922int unlinklib(const char* dataDir)
923{
924 char libdir[PKG_PATH_MAX];
925 struct stat s, libStat;
926 int rc = 0;
927
928 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
929 if (libdirLen >= PKG_PATH_MAX) {
930 return -1;
931 }
932
933 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000934 ALOGE("library dir not written successfully: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700935 return -1;
936 }
937
938 if (stat(dataDir, &s) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000939 ALOGE("couldn't state data dir");
Kenny Root6a6b0072010-10-07 16:46:10 -0700940 return -1;
941 }
942
943 if (chown(dataDir, 0, 0) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000944 ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700945 return -1;
946 }
947
948 if (chmod(dataDir, 0700) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000949 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700950 rc = -1;
951 goto out;
952 }
953
954 if (lstat(libdir, &libStat) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000955 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700956 rc = -1;
957 goto out;
958 }
959
960 if (S_ISDIR(libStat.st_mode)) {
961 if (delete_dir_contents(libdir, 1, 0) < 0) {
962 rc = -1;
963 goto out;
964 }
965 } else if (S_ISLNK(libStat.st_mode)) {
966 if (unlink(libdir) < 0) {
967 rc = -1;
968 goto out;
969 }
970 }
971
972 if (mkdir(libdir, 0755) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000973 ALOGE("cannot create dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700974 rc = -errno;
975 goto out;
976 }
977
978 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000979 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700980 unlink(libdir);
981 rc = -errno;
982 goto out;
983 }
984
985out:
986 if (chmod(dataDir, s.st_mode) < 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 return -1;
989 }
990
991 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000992 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700993 return -1;
994 }
995
996 return rc;
997}