blob: a9945b7d7b78c05be67ccd5db17de9b699d42845 [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
Nick Kralevich812b19a2012-08-31 16:08:06 -070017#include <linux/capability.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#include "installd.h"
Kenny Root33b22642010-11-30 13:49:32 -080019#include <diskusage/dirsize.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050021#ifdef HAVE_SELINUX
22#include <selinux/android.h>
23#endif
24
Kenny Root86c95842011-03-31 13:16:12 -070025/* Directory records that are used in execution of commands. */
26dir_rec_t android_data_dir;
27dir_rec_t android_asec_dir;
28dir_rec_t android_app_dir;
29dir_rec_t android_app_private_dir;
Kenny Root9bbd70a2012-09-10 11:13:36 -070030dir_rec_t android_app_lib_dir;
Dianne Hackborn197a0c82012-07-12 14:46:04 -070031dir_rec_t android_media_dir;
Kenny Root86c95842011-03-31 13:16:12 -070032dir_rec_array_t android_system_dirs;
33
Kenny Root35ab3ad2011-02-02 16:42:14 -080034int install(const char *pkgname, uid_t uid, gid_t gid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035{
36 char pkgdir[PKG_PATH_MAX];
Kenny Root9bbd70a2012-09-10 11:13:36 -070037 char libsymlink[PKG_PATH_MAX];
38 char applibdir[PKG_PATH_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
Steve Block3762c312012-01-06 19:20:56 +000041 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 }
Oscar Montemayora8529f62009-11-18 10:14:20 -080044
Kenny Root86c95842011-03-31 13:16:12 -070045 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
Steve Block3762c312012-01-06 19:20:56 +000046 ALOGE("cannot create package path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080047 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070048 }
49
Kenny Root9bbd70a2012-09-10 11:13:36 -070050 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
51 ALOGE("cannot create package lib symlink origin path\n");
52 return -1;
53 }
54
55 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
56 ALOGE("cannot create package lib symlink dest path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080057 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
David 'Digit' Turner0dd50e62010-02-09 19:02:38 -080060 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000061 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root9bbd70a2012-09-10 11:13:36 -070062 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070064 if (chmod(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000065 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070066 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070067 return -1;
Nick Kralevichf68327e2011-04-14 16:20:03 -070068 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050069
Kenny Root9bbd70a2012-09-10 11:13:36 -070070 if (symlink(applibdir, libsymlink) < 0) {
71 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
72 strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070074 return -1;
Kenny Root4503cf62012-06-14 13:05:18 -070075 }
Kenny Root33ef4ee2012-06-18 10:26:36 -070076
77#ifdef HAVE_SELINUX
78 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -040079 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root9bbd70a2012-09-10 11:13:36 -070080 unlink(libsymlink);
Kenny Root33ef4ee2012-06-18 10:26:36 -070081 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070082 return -1;
Kenny Root33ef4ee2012-06-18 10:26:36 -070083 }
84#endif
85
Kenny Root9bbd70a2012-09-10 11:13:36 -070086 if (chown(pkgdir, uid, gid) < 0) {
87 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
88 unlink(libsymlink);
89 unlink(pkgdir);
90 return -1;
91 }
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 return 0;
94}
95
Amith Yamasani0b285492011-04-14 17:35:23 -070096int uninstall(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097{
98 char pkgdir[PKG_PATH_MAX];
99
Amith Yamasani0b285492011-04-14 17:35:23 -0700100 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800101 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Amith Yamasani0b285492011-04-14 17:35:23 -0700103 /* delete contents AND directory, no exceptions */
104 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105}
106
Kenny Root35ab3ad2011-02-02 16:42:14 -0800107int renamepkg(const char *oldpkgname, const char *newpkgname)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800108{
109 char oldpkgdir[PKG_PATH_MAX];
110 char newpkgdir[PKG_PATH_MAX];
111
Kenny Root86c95842011-03-31 13:16:12 -0700112 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800113 return -1;
Kenny Root86c95842011-03-31 13:16:12 -0700114 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800115 return -1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800116
117 if (rename(oldpkgdir, newpkgdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000118 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800119 return -errno;
120 }
121 return 0;
122}
123
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700124int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
125{
126 char pkgdir[PKG_PATH_MAX];
127 struct stat s;
128 int rc = 0;
129
130 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
131 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
132 return -1;
133 }
134
135 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
136 ALOGE("cannot create package path\n");
137 return -1;
138 }
139
140 if (stat(pkgdir, &s) < 0) return -1;
141
142 if (s.st_uid != 0 || s.st_gid != 0) {
143 ALOGE("fixing uid of non-root pkg: %s %d %d\n", pkgdir, s.st_uid, s.st_gid);
144 return -1;
145 }
146
147 if (chmod(pkgdir, 0751) < 0) {
148 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
149 unlink(pkgdir);
150 return -errno;
151 }
152 if (chown(pkgdir, uid, gid) < 0) {
153 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
154 unlink(pkgdir);
155 return -errno;
156 }
157
158 return 0;
159}
160
Amith Yamasani0b285492011-04-14 17:35:23 -0700161int delete_user_data(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162{
163 char pkgdir[PKG_PATH_MAX];
164
Amith Yamasani0b285492011-04-14 17:35:23 -0700165 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800166 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
Amith Yamasani0b285492011-04-14 17:35:23 -0700168 /* delete contents, excluding "lib", but not the directory itself */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 return delete_dir_contents(pkgdir, 0, "lib");
170}
171
Amith Yamasani0b285492011-04-14 17:35:23 -0700172int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
173{
174 char pkgdir[PKG_PATH_MAX];
Amith Yamasani0b285492011-04-14 17:35:23 -0700175
176 // Create the data dir for the package
177 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
178 return -1;
179 }
180 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000181 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700182 return -errno;
183 }
Amith Yamasani794d62f2012-08-24 12:58:27 -0700184 if (chmod(pkgdir, 0751) < 0) {
185 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
186 unlink(pkgdir);
187 return -errno;
188 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700189 if (chown(pkgdir, uid, uid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000190 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700191 unlink(pkgdir);
192 return -errno;
193 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500194
195#ifdef HAVE_SELINUX
196 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -0400197 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500198 unlink(pkgdir);
199 return -errno;
200 }
201#endif
202
Amith Yamasani0b285492011-04-14 17:35:23 -0700203 return 0;
204}
205
206int delete_persona(uid_t persona)
207{
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700208 char data_path[PKG_PATH_MAX];
209 if (create_persona_path(data_path, persona)) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700210 return -1;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700211 }
212 if (delete_dir_contents(data_path, 1, NULL)) {
213 return -1;
214 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700215
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700216 char media_path[PATH_MAX];
217 if (create_persona_media_path(media_path, (userid_t) persona) == -1) {
218 return -1;
219 }
220 if (delete_dir_contents(media_path, 1, NULL) == -1) {
221 return -1;
222 }
223
224 return 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700225}
226
Amith Yamasani742a6712011-05-04 14:49:28 -0700227int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy)
228{
229 char src_data_dir[PKG_PATH_MAX];
230 char pkg_path[PKG_PATH_MAX];
231 DIR *d;
232 struct dirent *de;
233 struct stat s;
234 uid_t uid;
235
236 if (create_persona_path(src_data_dir, src_persona)) {
237 return -1;
238 }
239
240 d = opendir(src_data_dir);
241 if (d != NULL) {
242 while ((de = readdir(d))) {
243 const char *name = de->d_name;
244
245 if (de->d_type == DT_DIR) {
246 int subfd;
247 /* always skip "." and ".." */
248 if (name[0] == '.') {
249 if (name[1] == 0) continue;
250 if ((name[1] == '.') && (name[2] == 0)) continue;
251 }
252 /* Create the full path to the package's data dir */
253 create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona);
254 /* Get the file stat */
255 if (stat(pkg_path, &s) < 0) continue;
256 /* Get the uid of the package */
257 ALOGI("Adding datadir for uid = %d\n", s.st_uid);
258 uid = (uid_t) s.st_uid % PER_USER_RANGE;
259 /* Create the directory for the target */
260 make_user_data(name, uid + target_persona * PER_USER_RANGE,
261 target_persona);
262 }
263 }
264 closedir(d);
265 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700266
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700267 if (ensure_media_user_dirs((userid_t) target_persona) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700268 return -1;
269 }
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700270
Amith Yamasani742a6712011-05-04 14:49:28 -0700271 return 0;
272}
273
Kenny Root35ab3ad2011-02-02 16:42:14 -0800274int delete_cache(const char *pkgname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275{
276 char cachedir[PKG_PATH_MAX];
277
Kenny Root86c95842011-03-31 13:16:12 -0700278 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800279 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
281 /* delete contents, not the directory, no exceptions */
282 return delete_dir_contents(cachedir, 0, 0);
283}
284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285/* Try to ensure free_size bytes of storage are available.
286 * Returns 0 on success.
287 * This is rather simple-minded because doing a full LRU would
288 * be potentially memory-intensive, and without atime it would
289 * also require that apps constantly modify file metadata even
290 * when just reading from the cache, which is pretty awful.
291 */
Kenny Root3e319a92010-09-07 13:58:28 -0700292int free_cache(int64_t free_size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293{
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700294 cache_t* cache;
295 int64_t avail;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 DIR *d;
297 struct dirent *de;
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700298 char tmpdir[PATH_MAX];
299 char *dirpos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700301 avail = data_disk_free();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 if (avail < 0) return -1;
303
Steve Block6215d3f2012-01-04 20:05:49 +0000304 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 if (avail >= free_size) return 0;
306
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700307 cache = start_cache_collection();
308
309 // Collect cache files for primary user.
310 if (create_persona_path(tmpdir, 0) == 0) {
311 //ALOGI("adding cache files from %s\n", tmpdir);
312 add_cache_files(cache, tmpdir, "cache");
Kenny Rootad757e92011-11-29 15:54:55 -0800313 }
314
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700315 // Search for other users and add any cache files from them.
316 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
317 SECONDARY_USER_PREFIX);
318 dirpos = tmpdir + strlen(tmpdir);
319 d = opendir(tmpdir);
320 if (d != NULL) {
321 while ((de = readdir(d))) {
322 if (de->d_type == DT_DIR) {
323 const char *name = de->d_name;
324 /* always skip "." and ".." */
325 if (name[0] == '.') {
326 if (name[1] == 0) continue;
327 if ((name[1] == '.') && (name[2] == 0)) continue;
328 }
329 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
330 strcpy(dirpos, name);
331 //ALOGI("adding cache files from %s\n", tmpdir);
332 add_cache_files(cache, tmpdir, "cache");
333 } else {
334 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
335 }
336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700338 closedir(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
Oscar Montemayora8529f62009-11-18 10:14:20 -0800340
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700341 // Collect cache files on external storage for all users (if it is mounted as part
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700342 // of the internal storage).
343 strcpy(tmpdir, android_media_dir.path);
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700344 dirpos = tmpdir + strlen(tmpdir);
345 d = opendir(tmpdir);
346 if (d != NULL) {
347 while ((de = readdir(d))) {
348 if (de->d_type == DT_DIR) {
349 const char *name = de->d_name;
350 /* skip any dir that doesn't start with a number, so not a user */
351 if (name[0] < '0' || name[0] > '9') {
352 continue;
353 }
354 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
355 strcpy(dirpos, name);
356 if (lookup_media_dir(tmpdir, "Android") == 0
357 && lookup_media_dir(tmpdir, "data") == 0) {
358 //ALOGI("adding cache files from %s\n", tmpdir);
359 add_cache_files(cache, tmpdir, "cache");
360 }
361 } else {
362 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
363 }
364 }
365 }
366 closedir(d);
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700367 }
368
369 clear_cache_files(cache, free_size);
370 finish_cache_collection(cache);
371
372 return data_disk_free() >= free_size ? 0 : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373}
374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375int move_dex(const char *src, const char *dst)
376{
377 char src_dex[PKG_PATH_MAX];
378 char dst_dex[PKG_PATH_MAX];
379
Kenny Root86c95842011-03-31 13:16:12 -0700380 if (validate_apk_path(src)) return -1;
381 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382
383 if (create_cache_path(src_dex, src)) return -1;
384 if (create_cache_path(dst_dex, dst)) return -1;
385
Steve Block71f2cf12011-10-20 11:56:00 +0100386 ALOGV("move %s -> %s\n", src_dex, dst_dex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 if (rename(src_dex, dst_dex) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000388 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 return -1;
390 } else {
391 return 0;
392 }
393}
394
395int rm_dex(const char *path)
396{
397 char dex_path[PKG_PATH_MAX];
398
Kenny Root86c95842011-03-31 13:16:12 -0700399 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 if (create_cache_path(dex_path, path)) return -1;
401
Steve Block71f2cf12011-10-20 11:56:00 +0100402 ALOGV("unlink %s\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 if (unlink(dex_path) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000404 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 return -1;
406 } else {
407 return 0;
408 }
409}
410
Dianne Hackborn0c380492012-08-20 17:23:30 -0700411int get_size(const char *pkgname, int persona, const char *apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700412 const char *fwdlock_apkpath, const char *asecpath,
413 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
414 int64_t* _asecsize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415{
416 DIR *d;
417 int dfd;
418 struct dirent *de;
419 struct stat s;
420 char path[PKG_PATH_MAX];
421
Kenny Root3e319a92010-09-07 13:58:28 -0700422 int64_t codesize = 0;
423 int64_t datasize = 0;
424 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700425 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
427 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800428 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 */
Kenny Root86c95842011-03-31 13:16:12 -0700430 if (validate_system_app_path(apkpath) &&
431 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 if (stat(apkpath, &s) == 0) {
433 codesize += stat_size(&s);
434 }
435 }
436 /* count the forward locked apk as code if it is given
437 */
438 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
439 if (stat(fwdlock_apkpath, &s) == 0) {
440 codesize += stat_size(&s);
441 }
442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 /* count the cached dexfile as code */
444 if (!create_cache_path(path, apkpath)) {
445 if (stat(path, &s) == 0) {
446 codesize += stat_size(&s);
447 }
448 }
449
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700450 /* compute asec size if it is given
451 */
452 if (asecpath != NULL && asecpath[0] != '!') {
453 if (stat(asecpath, &s) == 0) {
454 asecsize += stat_size(&s);
455 }
456 }
457
Dianne Hackborn0c380492012-08-20 17:23:30 -0700458 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800459 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 }
461
462 d = opendir(path);
463 if (d == NULL) {
464 goto done;
465 }
466 dfd = dirfd(d);
467
Kenny Root86c95842011-03-31 13:16:12 -0700468 /* most stuff in the pkgdir is data, except for the "cache"
469 * directory and below, which is cache, and the "lib" directory
470 * and below, which is code...
471 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 while ((de = readdir(d))) {
473 const char *name = de->d_name;
474
475 if (de->d_type == DT_DIR) {
476 int subfd;
477 /* always skip "." and ".." */
478 if (name[0] == '.') {
479 if (name[1] == 0) continue;
480 if ((name[1] == '.') && (name[2] == 0)) continue;
481 }
482 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
483 if (subfd >= 0) {
Kenny Root3e319a92010-09-07 13:58:28 -0700484 int64_t size = calculate_dir_size(subfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 if (!strcmp(name,"lib")) {
486 codesize += size;
487 } else if(!strcmp(name,"cache")) {
488 cachesize += size;
489 } else {
490 datasize += size;
491 }
492 }
493 } else {
494 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
495 datasize += stat_size(&s);
496 }
497 }
498 }
499 closedir(d);
500done:
501 *_codesize = codesize;
502 *_datasize = datasize;
503 *_cachesize = cachesize;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700504 *_asecsize = asecsize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 return 0;
506}
507
508
509/* a simpler version of dexOptGenerateCacheFileName() */
510int create_cache_path(char path[PKG_PATH_MAX], const char *src)
511{
512 char *tmp;
513 int srclen;
514 int dstlen;
515
516 srclen = strlen(src);
517
518 /* demand that we are an absolute path */
519 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
520 return -1;
521 }
522
523 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
524 return -1;
525 }
526
527 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
528 strlen(DALVIK_CACHE_POSTFIX) + 1;
529
530 if (dstlen > PKG_PATH_MAX) {
531 return -1;
532 }
533
534 sprintf(path,"%s%s%s",
535 DALVIK_CACHE_PREFIX,
536 src + 1, /* skip the leading / */
537 DALVIK_CACHE_POSTFIX);
538
539 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
540 if (*tmp == '/') {
541 *tmp = '@';
542 }
543 }
544
545 return 0;
546}
547
548static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
549 const char* dexopt_flags)
550{
551 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
552 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
553 char zip_num[MAX_INT_LEN];
554 char odex_num[MAX_INT_LEN];
555
556 sprintf(zip_num, "%d", zip_fd);
557 sprintf(odex_num, "%d", odex_fd);
558
559 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
560 dexopt_flags, (char*) NULL);
Steve Block3762c312012-01-06 19:20:56 +0000561 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562}
563
564static int wait_dexopt(pid_t pid, const char* apk_path)
565{
566 int status;
567 pid_t got_pid;
568
569 /*
570 * Wait for the optimization process to finish.
571 */
572 while (1) {
573 got_pid = waitpid(pid, &status, 0);
574 if (got_pid == -1 && errno == EINTR) {
575 printf("waitpid interrupted, retrying\n");
576 } else {
577 break;
578 }
579 }
580 if (got_pid != pid) {
Steve Block8564c8d2012-01-05 23:22:43 +0000581 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 (int) pid, (int) got_pid, strerror(errno));
583 return 1;
584 }
585
586 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100587 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 return 0;
589 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000590 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 apk_path, status);
592 return status; /* always nonzero */
593 }
594}
595
596int dexopt(const char *apk_path, uid_t uid, int is_public)
597{
598 struct utimbuf ut;
599 struct stat apk_stat, dex_stat;
600 char dex_path[PKG_PATH_MAX];
601 char dexopt_flags[PROPERTY_VALUE_MAX];
602 char *end;
603 int res, zip_fd=-1, odex_fd=-1;
604
605 /* Before anything else: is there a .odex file? If so, we have
606 * pre-optimized the apk and there is nothing to do here.
607 */
608 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
609 return -1;
610 }
611
612 /* platform-specific flags affecting optimization and verification */
613 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
614
615 strcpy(dex_path, apk_path);
616 end = strrchr(dex_path, '.');
617 if (end != NULL) {
618 strcpy(end, ".odex");
619 if (stat(dex_path, &dex_stat) == 0) {
620 return 0;
621 }
622 }
623
624 if (create_cache_path(dex_path, apk_path)) {
625 return -1;
626 }
627
628 memset(&apk_stat, 0, sizeof(apk_stat));
629 stat(apk_path, &apk_stat);
630
631 zip_fd = open(apk_path, O_RDONLY, 0);
632 if (zip_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000633 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 return -1;
635 }
636
637 unlink(dex_path);
638 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
639 if (odex_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000640 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 goto fail;
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 if (fchmod(odex_fd,
644 S_IRUSR|S_IWUSR|S_IRGRP |
645 (is_public ? S_IROTH : 0)) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000646 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 goto fail;
648 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700649 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
650 ALOGE("dexopt cannot chown '%s'\n", dex_path);
651 goto fail;
652 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
Steve Block71f2cf12011-10-20 11:56:00 +0100654 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655
656 pid_t pid;
657 pid = fork();
658 if (pid == 0) {
659 /* child -- drop privileges before continuing */
660 if (setgid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000661 ALOGE("setgid(%d) failed during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 exit(64);
663 }
664 if (setuid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000665 ALOGE("setuid(%d) during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 exit(65);
667 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700668 // drop capabilities
669 struct __user_cap_header_struct capheader;
670 struct __user_cap_data_struct capdata[2];
671 memset(&capheader, 0, sizeof(capheader));
672 memset(&capdata, 0, sizeof(capdata));
673 capheader.version = _LINUX_CAPABILITY_VERSION_3;
674 if (capset(&capheader, &capdata[0]) < 0) {
675 ALOGE("capset failed: %s\n", strerror(errno));
676 exit(66);
677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000679 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
Nick Kralevich812b19a2012-08-31 16:08:06 -0700680 exit(67);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682
683 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
Nick Kralevich812b19a2012-08-31 16:08:06 -0700684 exit(68); /* only get here on exec failure */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 } else {
686 res = wait_dexopt(pid, apk_path);
687 if (res != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000688 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 goto fail;
690 }
691 }
692
693 ut.actime = apk_stat.st_atime;
694 ut.modtime = apk_stat.st_mtime;
695 utime(dex_path, &ut);
696
697 close(odex_fd);
698 close(zip_fd);
699 return 0;
700
701fail:
702 if (odex_fd >= 0) {
703 close(odex_fd);
704 unlink(dex_path);
705 }
706 if (zip_fd >= 0) {
707 close(zip_fd);
708 }
709 return -1;
710}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800711
Dianne Hackbornc1552392010-03-03 16:19:01 -0800712void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
713 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800714{
715 while (path[basepos] != 0) {
716 if (path[basepos] == '/') {
717 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800718 if (lstat(path, statbuf) < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100719 ALOGV("Making directory: %s\n", path);
Dianne Hackbornc1552392010-03-03 16:19:01 -0800720 if (mkdir(path, mode) == 0) {
721 chown(path, uid, gid);
722 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000723 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
Dianne Hackbornc1552392010-03-03 16:19:01 -0800724 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800725 }
726 path[basepos] = '/';
727 basepos++;
728 }
729 basepos++;
730 }
731}
732
Dianne Hackbornc1552392010-03-03 16:19:01 -0800733int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
734 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800735{
736 DIR *d;
737 struct dirent *de;
738 int res;
739
740 int srcend = strlen(srcpath);
741 int dstend = strlen(dstpath);
742
743 if (lstat(srcpath, statbuf) < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000744 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800745 return 1;
746 }
747
748 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800749 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
750 dstuid, dstgid, statbuf);
Steve Block71f2cf12011-10-20 11:56:00 +0100751 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800752 if (rename(srcpath, dstpath) >= 0) {
753 if (chown(dstpath, dstuid, dstgid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000754 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800755 unlink(dstpath);
756 return 1;
757 }
758 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000759 ALOGW("Unable to rename %s to %s: %s\n",
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800760 srcpath, dstpath, strerror(errno));
761 return 1;
762 }
763 return 0;
764 }
765
766 d = opendir(srcpath);
767 if (d == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000768 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800769 return 1;
770 }
771
772 res = 0;
773
774 while ((de = readdir(d))) {
775 const char *name = de->d_name;
776 /* always skip "." and ".." */
777 if (name[0] == '.') {
778 if (name[1] == 0) continue;
779 if ((name[1] == '.') && (name[2] == 0)) continue;
780 }
781
782 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000783 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800784 continue;
785 }
786
787 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000788 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800789 continue;
790 }
791
792 srcpath[srcend] = dstpath[dstend] = '/';
793 strcpy(srcpath+srcend+1, name);
794 strcpy(dstpath+dstend+1, name);
795
Dianne Hackbornc1552392010-03-03 16:19:01 -0800796 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800797 res = 1;
798 }
799
800 // Note: we will be leaving empty directories behind in srcpath,
801 // but that is okay, the package manager will be erasing all of the
802 // data associated with .apks that disappear.
803
804 srcpath[srcend] = dstpath[dstend] = 0;
805 }
806
807 closedir(d);
808 return res;
809}
810
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800811int movefiles()
812{
813 DIR *d;
814 int dfd, subfd;
815 struct dirent *de;
816 struct stat s;
817 char buf[PKG_PATH_MAX+1];
818 int bufp, bufe, bufi, readlen;
819
820 char srcpkg[PKG_NAME_MAX];
821 char dstpkg[PKG_NAME_MAX];
822 char srcpath[PKG_PATH_MAX];
823 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800824 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800825 int hasspace;
826
827 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
828 if (d == NULL) {
829 goto done;
830 }
831 dfd = dirfd(d);
832
833 /* Iterate through all files in the directory, executing the
834 * file movements requested there-in.
835 */
836 while ((de = readdir(d))) {
837 const char *name = de->d_name;
838
839 if (de->d_type == DT_DIR) {
840 continue;
841 } else {
842 subfd = openat(dfd, name, O_RDONLY);
843 if (subfd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000844 ALOGW("Unable to open update commands at %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800845 UPDATE_COMMANDS_DIR_PREFIX, name);
846 continue;
847 }
848
849 bufp = 0;
850 bufe = 0;
851 buf[PKG_PATH_MAX] = 0;
852 srcpkg[0] = dstpkg[0] = 0;
853 while (1) {
854 bufi = bufp;
855 while (bufi < bufe && buf[bufi] != '\n') {
856 bufi++;
857 }
858 if (bufi < bufe) {
859 buf[bufi] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100860 ALOGV("Processing line: %s\n", buf+bufp);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800861 hasspace = 0;
862 while (bufp < bufi && isspace(buf[bufp])) {
863 hasspace = 1;
864 bufp++;
865 }
866 if (buf[bufp] == '#' || bufp == bufi) {
867 // skip comments and empty lines.
868 } else if (hasspace) {
869 if (dstpkg[0] == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000870 ALOGW("Path before package line in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800871 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
872 } else if (srcpkg[0] == 0) {
873 // Skip -- source package no longer exists.
874 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100875 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
Kenny Root86c95842011-03-31 13:16:12 -0700876 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
877 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800878 movefileordir(srcpath, dstpath,
879 strlen(dstpath)-strlen(buf+bufp),
880 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800881 }
882 }
883 } else {
884 char* div = strchr(buf+bufp, ':');
885 if (div == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000886 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800887 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
888 } else {
889 *div = 0;
890 div++;
891 if (strlen(buf+bufp) < PKG_NAME_MAX) {
892 strcpy(dstpkg, buf+bufp);
893 } else {
894 srcpkg[0] = dstpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000895 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800896 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
897 }
898 if (strlen(div) < PKG_NAME_MAX) {
899 strcpy(srcpkg, div);
900 } else {
901 srcpkg[0] = dstpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000902 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800903 UPDATE_COMMANDS_DIR_PREFIX, name, div);
904 }
905 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700906 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800907 if (lstat(srcpath, &s) < 0) {
908 // Package no longer exists -- skip.
909 srcpkg[0] = 0;
910 }
911 } else {
912 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000913 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800914 div, UPDATE_COMMANDS_DIR_PREFIX, name);
915 }
916 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700917 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800918 if (lstat(dstpath, &s) == 0) {
919 dstuid = s.st_uid;
920 dstgid = s.st_gid;
921 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800922 // Destination package doesn't
923 // exist... due to original-package,
924 // this is normal, so don't be
925 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800926 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800927 }
928 } else {
929 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000930 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800931 div, UPDATE_COMMANDS_DIR_PREFIX, name);
932 }
933 }
Steve Block71f2cf12011-10-20 11:56:00 +0100934 ALOGV("Transfering from %s to %s: uid=%d\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800935 srcpkg, dstpkg, dstuid);
936 }
937 }
938 }
939 bufp = bufi+1;
940 } else {
941 if (bufp == 0) {
942 if (bufp < bufe) {
Steve Block8564c8d2012-01-05 23:22:43 +0000943 ALOGW("Line too long in %s%s, skipping: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800944 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
945 }
946 } else if (bufp < bufe) {
947 memcpy(buf, buf+bufp, bufe-bufp);
948 bufe -= bufp;
949 bufp = 0;
950 }
951 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
952 if (readlen < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000953 ALOGW("Failure reading update commands in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800954 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
955 break;
956 } else if (readlen == 0) {
957 break;
958 }
959 bufe += readlen;
960 buf[bufe] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100961 ALOGV("Read buf: %s\n", buf);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800962 }
963 }
964 close(subfd);
965 }
966 }
967 closedir(d);
968done:
969 return 0;
970}
Kenny Root6a6b0072010-10-07 16:46:10 -0700971
972int linklib(const char* dataDir, const char* asecLibDir)
973{
974 char libdir[PKG_PATH_MAX];
975 struct stat s, libStat;
976 int rc = 0;
977
978 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
979 if (libdirLen >= PKG_PATH_MAX) {
Steve Block3762c312012-01-06 19:20:56 +0000980 ALOGE("library dir len too large");
Kenny Root0332d1c2010-10-21 16:14:06 -0700981 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -0700982 }
983
984 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
Steve Block3762c312012-01-06 19:20:56 +0000985 ALOGE("library dir not written successfully: %s\n", strerror(errno));
Kenny Root0332d1c2010-10-21 16:14:06 -0700986 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -0700987 }
988
989 if (stat(dataDir, &s) < 0) return -1;
990
Nick Kralevich7de350a2012-09-07 15:48:11 -0700991 if (chown(dataDir, AID_INSTALL, AID_INSTALL) < 0) {
Steve Block3762c312012-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 if (chmod(dataDir, 0700) < 0) {
Nick Kralevich7de350a2012-09-07 15:48:11 -0700997 ALOGE("linklib() 1: failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -0700998 rc = -1;
999 goto out;
1000 }
1001
1002 if (lstat(libdir, &libStat) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001003 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001004 rc = -1;
1005 goto out;
1006 }
1007
1008 if (S_ISDIR(libStat.st_mode)) {
1009 if (delete_dir_contents(libdir, 1, 0) < 0) {
1010 rc = -1;
1011 goto out;
1012 }
1013 } else if (S_ISLNK(libStat.st_mode)) {
1014 if (unlink(libdir) < 0) {
1015 rc = -1;
1016 goto out;
1017 }
1018 }
1019
1020 if (symlink(asecLibDir, libdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001021 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libdir, asecLibDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001022 rc = -errno;
1023 goto out;
1024 }
1025
1026 if (lchown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001027 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001028 unlink(libdir);
1029 rc = -errno;
1030 goto out;
1031 }
1032
1033out:
1034 if (chmod(dataDir, s.st_mode) < 0) {
Nick Kralevich7de350a2012-09-07 15:48:11 -07001035 ALOGE("linklib() 2: failed to chmod '%s': %s\n", dataDir, strerror(errno));
Dianne Hackbornd0c5f512012-06-07 16:53:59 -07001036 rc = -errno;
Kenny Root6a6b0072010-10-07 16:46:10 -07001037 }
1038
1039 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001040 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001041 return -errno;
1042 }
1043
1044 return rc;
1045}