blob: c16e6fba5a76bbf49265987ae65998aab6cf03d3 [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;
Dianne Hackborn197a0c82012-07-12 14:46:04 -070029dir_rec_t android_media_dir;
Kenny Root86c95842011-03-31 13:16:12 -070030dir_rec_array_t android_system_dirs;
31
Kenny Root35ab3ad2011-02-02 16:42:14 -080032int install(const char *pkgname, uid_t uid, gid_t gid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033{
34 char pkgdir[PKG_PATH_MAX];
35 char libdir[PKG_PATH_MAX];
36
37 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
Steve Block3762c312012-01-06 19:20:56 +000038 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 }
Oscar Montemayora8529f62009-11-18 10:14:20 -080041
Kenny Root86c95842011-03-31 13:16:12 -070042 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
Steve Block3762c312012-01-06 19:20:56 +000043 ALOGE("cannot create package path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080044 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070045 }
46
47 if (create_pkg_path(libdir, pkgname, PKG_LIB_POSTFIX, 0)) {
Steve Block3762c312012-01-06 19:20:56 +000048 ALOGE("cannot create package lib path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080049 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
David 'Digit' Turner0dd50e62010-02-09 19:02:38 -080052 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000053 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 return -errno;
55 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070056 if (chmod(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000057 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070058 unlink(pkgdir);
59 return -errno;
60 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 if (mkdir(libdir, 0755) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000063 ALOGE("cannot create dir '%s': %s\n", libdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 unlink(pkgdir);
65 return -errno;
66 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070067 if (chmod(libdir, 0755) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000068 ALOGE("cannot chmod dir '%s': %s\n", libdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070069 unlink(libdir);
70 unlink(pkgdir);
71 return -errno;
72 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000074 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 unlink(libdir);
76 unlink(pkgdir);
77 return -errno;
78 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050079
80#ifdef HAVE_SELINUX
81 if (selinux_android_setfilecon(libdir, pkgname, AID_SYSTEM) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -040082 ALOGE("cannot setfilecon dir '%s': %s\n", libdir, strerror(errno));
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050083 unlink(libdir);
84 unlink(pkgdir);
85 return -errno;
86 }
87#endif
88
Kenny Root4503cf62012-06-14 13:05:18 -070089 if (chown(pkgdir, uid, gid) < 0) {
90 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
91 unlink(libdir);
92 unlink(pkgdir);
93 return -errno;
94 }
Kenny Root33ef4ee2012-06-18 10:26:36 -070095
96#ifdef HAVE_SELINUX
97 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -040098 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root33ef4ee2012-06-18 10:26:36 -070099 unlink(libdir);
100 unlink(pkgdir);
101 return -errno;
102 }
103#endif
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 return 0;
106}
107
Amith Yamasani0b285492011-04-14 17:35:23 -0700108int uninstall(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109{
110 char pkgdir[PKG_PATH_MAX];
111
Amith Yamasani0b285492011-04-14 17:35:23 -0700112 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800113 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
Amith Yamasani0b285492011-04-14 17:35:23 -0700115 /* delete contents AND directory, no exceptions */
116 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117}
118
Kenny Root35ab3ad2011-02-02 16:42:14 -0800119int renamepkg(const char *oldpkgname, const char *newpkgname)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800120{
121 char oldpkgdir[PKG_PATH_MAX];
122 char newpkgdir[PKG_PATH_MAX];
123
Kenny Root86c95842011-03-31 13:16:12 -0700124 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800125 return -1;
Kenny Root86c95842011-03-31 13:16:12 -0700126 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800127 return -1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800128
129 if (rename(oldpkgdir, newpkgdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000130 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800131 return -errno;
132 }
133 return 0;
134}
135
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700136int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
137{
138 char pkgdir[PKG_PATH_MAX];
139 struct stat s;
140 int rc = 0;
141
142 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
143 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
144 return -1;
145 }
146
147 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
148 ALOGE("cannot create package path\n");
149 return -1;
150 }
151
152 if (stat(pkgdir, &s) < 0) return -1;
153
154 if (s.st_uid != 0 || s.st_gid != 0) {
155 ALOGE("fixing uid of non-root pkg: %s %d %d\n", pkgdir, s.st_uid, s.st_gid);
156 return -1;
157 }
158
159 if (chmod(pkgdir, 0751) < 0) {
160 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
161 unlink(pkgdir);
162 return -errno;
163 }
164 if (chown(pkgdir, uid, gid) < 0) {
165 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
166 unlink(pkgdir);
167 return -errno;
168 }
169
170 return 0;
171}
172
Amith Yamasani0b285492011-04-14 17:35:23 -0700173int delete_user_data(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174{
175 char pkgdir[PKG_PATH_MAX];
176
Amith Yamasani0b285492011-04-14 17:35:23 -0700177 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800178 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Amith Yamasani0b285492011-04-14 17:35:23 -0700180 /* delete contents, excluding "lib", but not the directory itself */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 return delete_dir_contents(pkgdir, 0, "lib");
182}
183
Amith Yamasani0b285492011-04-14 17:35:23 -0700184int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
185{
186 char pkgdir[PKG_PATH_MAX];
187 char real_libdir[PKG_PATH_MAX];
188
189 // Create the data dir for the package
190 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
191 return -1;
192 }
193 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000194 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700195 return -errno;
196 }
197 if (chown(pkgdir, uid, uid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000198 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700199 unlink(pkgdir);
200 return -errno;
201 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500202
203#ifdef HAVE_SELINUX
204 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -0400205 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500206 unlink(pkgdir);
207 return -errno;
208 }
209#endif
210
Amith Yamasani0b285492011-04-14 17:35:23 -0700211 return 0;
212}
213
214int delete_persona(uid_t persona)
215{
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700216 char data_path[PKG_PATH_MAX];
217 if (create_persona_path(data_path, persona)) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700218 return -1;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700219 }
220 if (delete_dir_contents(data_path, 1, NULL)) {
221 return -1;
222 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700223
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700224 char media_path[PATH_MAX];
225 if (create_persona_media_path(media_path, (userid_t) persona) == -1) {
226 return -1;
227 }
228 if (delete_dir_contents(media_path, 1, NULL) == -1) {
229 return -1;
230 }
231
232 return 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700233}
234
Amith Yamasani742a6712011-05-04 14:49:28 -0700235int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy)
236{
237 char src_data_dir[PKG_PATH_MAX];
238 char pkg_path[PKG_PATH_MAX];
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700239 char media_path[PATH_MAX];
Amith Yamasani742a6712011-05-04 14:49:28 -0700240 DIR *d;
241 struct dirent *de;
242 struct stat s;
243 uid_t uid;
244
245 if (create_persona_path(src_data_dir, src_persona)) {
246 return -1;
247 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700248 if (create_persona_media_path(media_path, (userid_t) target_persona) == -1) {
249 return -1;
250 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700251
252 d = opendir(src_data_dir);
253 if (d != NULL) {
254 while ((de = readdir(d))) {
255 const char *name = de->d_name;
256
257 if (de->d_type == DT_DIR) {
258 int subfd;
259 /* always skip "." and ".." */
260 if (name[0] == '.') {
261 if (name[1] == 0) continue;
262 if ((name[1] == '.') && (name[2] == 0)) continue;
263 }
264 /* Create the full path to the package's data dir */
265 create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona);
266 /* Get the file stat */
267 if (stat(pkg_path, &s) < 0) continue;
268 /* Get the uid of the package */
269 ALOGI("Adding datadir for uid = %d\n", s.st_uid);
270 uid = (uid_t) s.st_uid % PER_USER_RANGE;
271 /* Create the directory for the target */
272 make_user_data(name, uid + target_persona * PER_USER_RANGE,
273 target_persona);
274 }
275 }
276 closedir(d);
277 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700278
279 // ensure /data/media/<user_id> exists
280 if (ensure_dir(media_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
281 return -1;
282 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700283 return 0;
284}
285
Kenny Root35ab3ad2011-02-02 16:42:14 -0800286int delete_cache(const char *pkgname)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287{
288 char cachedir[PKG_PATH_MAX];
289
Kenny Root86c95842011-03-31 13:16:12 -0700290 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800291 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
293 /* delete contents, not the directory, no exceptions */
294 return delete_dir_contents(cachedir, 0, 0);
295}
296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297/* Try to ensure free_size bytes of storage are available.
298 * Returns 0 on success.
299 * This is rather simple-minded because doing a full LRU would
300 * be potentially memory-intensive, and without atime it would
301 * also require that apps constantly modify file metadata even
302 * when just reading from the cache, which is pretty awful.
303 */
Kenny Root3e319a92010-09-07 13:58:28 -0700304int free_cache(int64_t free_size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305{
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700306 cache_t* cache;
307 int64_t avail;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 DIR *d;
309 struct dirent *de;
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700310 char tmpdir[PATH_MAX];
311 char *dirpos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700313 avail = data_disk_free();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 if (avail < 0) return -1;
315
Steve Block6215d3f2012-01-04 20:05:49 +0000316 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 if (avail >= free_size) return 0;
318
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700319 cache = start_cache_collection();
320
321 // Collect cache files for primary user.
322 if (create_persona_path(tmpdir, 0) == 0) {
323 //ALOGI("adding cache files from %s\n", tmpdir);
324 add_cache_files(cache, tmpdir, "cache");
Kenny Rootad757e92011-11-29 15:54:55 -0800325 }
326
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700327 // Search for other users and add any cache files from them.
328 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
329 SECONDARY_USER_PREFIX);
330 dirpos = tmpdir + strlen(tmpdir);
331 d = opendir(tmpdir);
332 if (d != NULL) {
333 while ((de = readdir(d))) {
334 if (de->d_type == DT_DIR) {
335 const char *name = de->d_name;
336 /* always skip "." and ".." */
337 if (name[0] == '.') {
338 if (name[1] == 0) continue;
339 if ((name[1] == '.') && (name[2] == 0)) continue;
340 }
341 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
342 strcpy(dirpos, name);
343 //ALOGI("adding cache files from %s\n", tmpdir);
344 add_cache_files(cache, tmpdir, "cache");
345 } else {
346 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
347 }
348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700350 closedir(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 }
Oscar Montemayora8529f62009-11-18 10:14:20 -0800352
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700353 // Collect cache files on external storage (if it is mounted as part
354 // of the internal storage).
355 strcpy(tmpdir, android_media_dir.path);
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
362 clear_cache_files(cache, free_size);
363 finish_cache_collection(cache);
364
365 return data_disk_free() >= free_size ? 0 : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366}
367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368int move_dex(const char *src, const char *dst)
369{
370 char src_dex[PKG_PATH_MAX];
371 char dst_dex[PKG_PATH_MAX];
372
Kenny Root86c95842011-03-31 13:16:12 -0700373 if (validate_apk_path(src)) return -1;
374 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375
376 if (create_cache_path(src_dex, src)) return -1;
377 if (create_cache_path(dst_dex, dst)) return -1;
378
Steve Block71f2cf12011-10-20 11:56:00 +0100379 ALOGV("move %s -> %s\n", src_dex, dst_dex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 if (rename(src_dex, dst_dex) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000381 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 return -1;
383 } else {
384 return 0;
385 }
386}
387
388int rm_dex(const char *path)
389{
390 char dex_path[PKG_PATH_MAX];
391
Kenny Root86c95842011-03-31 13:16:12 -0700392 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 if (create_cache_path(dex_path, path)) return -1;
394
Steve Block71f2cf12011-10-20 11:56:00 +0100395 ALOGV("unlink %s\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 if (unlink(dex_path) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000397 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 return -1;
399 } else {
400 return 0;
401 }
402}
403
404int protect(char *pkgname, gid_t gid)
405{
406 struct stat s;
407 char pkgpath[PKG_PATH_MAX];
408
409 if (gid < AID_SYSTEM) return -1;
410
Kenny Root86c95842011-03-31 13:16:12 -0700411 if (create_pkg_path_in_dir(pkgpath, &android_app_private_dir, pkgname, ".apk"))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 return -1;
413
414 if (stat(pkgpath, &s) < 0) return -1;
415
416 if (chown(pkgpath, s.st_uid, gid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000417 ALOGE("failed to chgrp '%s': %s\n", pkgpath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 return -1;
419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 if (chmod(pkgpath, S_IRUSR|S_IWUSR|S_IRGRP) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000421 ALOGE("failed to chmod '%s': %s\n", pkgpath, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 return -1;
423 }
424
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500425#ifdef HAVE_SELINUX
426 if (selinux_android_setfilecon(pkgpath, pkgname, s.st_uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -0400427 ALOGE("cannot setfilecon dir '%s': %s\n", pkgpath, strerror(errno));
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500428 return -1;
429 }
430#endif
431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 return 0;
433}
434
Dianne Hackborn0c380492012-08-20 17:23:30 -0700435int get_size(const char *pkgname, int persona, const char *apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700436 const char *fwdlock_apkpath, const char *asecpath,
437 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
438 int64_t* _asecsize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439{
440 DIR *d;
441 int dfd;
442 struct dirent *de;
443 struct stat s;
444 char path[PKG_PATH_MAX];
445
Kenny Root3e319a92010-09-07 13:58:28 -0700446 int64_t codesize = 0;
447 int64_t datasize = 0;
448 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700449 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450
451 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800452 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 */
Kenny Root86c95842011-03-31 13:16:12 -0700454 if (validate_system_app_path(apkpath) &&
455 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 if (stat(apkpath, &s) == 0) {
457 codesize += stat_size(&s);
458 }
459 }
460 /* count the forward locked apk as code if it is given
461 */
462 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
463 if (stat(fwdlock_apkpath, &s) == 0) {
464 codesize += stat_size(&s);
465 }
466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 /* count the cached dexfile as code */
468 if (!create_cache_path(path, apkpath)) {
469 if (stat(path, &s) == 0) {
470 codesize += stat_size(&s);
471 }
472 }
473
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700474 /* compute asec size if it is given
475 */
476 if (asecpath != NULL && asecpath[0] != '!') {
477 if (stat(asecpath, &s) == 0) {
478 asecsize += stat_size(&s);
479 }
480 }
481
Dianne Hackborn0c380492012-08-20 17:23:30 -0700482 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800483 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
485
486 d = opendir(path);
487 if (d == NULL) {
488 goto done;
489 }
490 dfd = dirfd(d);
491
Kenny Root86c95842011-03-31 13:16:12 -0700492 /* most stuff in the pkgdir is data, except for the "cache"
493 * directory and below, which is cache, and the "lib" directory
494 * and below, which is code...
495 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 while ((de = readdir(d))) {
497 const char *name = de->d_name;
498
499 if (de->d_type == DT_DIR) {
500 int subfd;
501 /* always skip "." and ".." */
502 if (name[0] == '.') {
503 if (name[1] == 0) continue;
504 if ((name[1] == '.') && (name[2] == 0)) continue;
505 }
506 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
507 if (subfd >= 0) {
Kenny Root3e319a92010-09-07 13:58:28 -0700508 int64_t size = calculate_dir_size(subfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 if (!strcmp(name,"lib")) {
510 codesize += size;
511 } else if(!strcmp(name,"cache")) {
512 cachesize += size;
513 } else {
514 datasize += size;
515 }
516 }
517 } else {
518 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
519 datasize += stat_size(&s);
520 }
521 }
522 }
523 closedir(d);
524done:
525 *_codesize = codesize;
526 *_datasize = datasize;
527 *_cachesize = cachesize;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700528 *_asecsize = asecsize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 return 0;
530}
531
532
533/* a simpler version of dexOptGenerateCacheFileName() */
534int create_cache_path(char path[PKG_PATH_MAX], const char *src)
535{
536 char *tmp;
537 int srclen;
538 int dstlen;
539
540 srclen = strlen(src);
541
542 /* demand that we are an absolute path */
543 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
544 return -1;
545 }
546
547 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
548 return -1;
549 }
550
551 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
552 strlen(DALVIK_CACHE_POSTFIX) + 1;
553
554 if (dstlen > PKG_PATH_MAX) {
555 return -1;
556 }
557
558 sprintf(path,"%s%s%s",
559 DALVIK_CACHE_PREFIX,
560 src + 1, /* skip the leading / */
561 DALVIK_CACHE_POSTFIX);
562
563 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
564 if (*tmp == '/') {
565 *tmp = '@';
566 }
567 }
568
569 return 0;
570}
571
572static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
573 const char* dexopt_flags)
574{
575 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
576 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
577 char zip_num[MAX_INT_LEN];
578 char odex_num[MAX_INT_LEN];
579
580 sprintf(zip_num, "%d", zip_fd);
581 sprintf(odex_num, "%d", odex_fd);
582
583 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
584 dexopt_flags, (char*) NULL);
Steve Block3762c312012-01-06 19:20:56 +0000585 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586}
587
588static int wait_dexopt(pid_t pid, const char* apk_path)
589{
590 int status;
591 pid_t got_pid;
592
593 /*
594 * Wait for the optimization process to finish.
595 */
596 while (1) {
597 got_pid = waitpid(pid, &status, 0);
598 if (got_pid == -1 && errno == EINTR) {
599 printf("waitpid interrupted, retrying\n");
600 } else {
601 break;
602 }
603 }
604 if (got_pid != pid) {
Steve Block8564c8d2012-01-05 23:22:43 +0000605 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 (int) pid, (int) got_pid, strerror(errno));
607 return 1;
608 }
609
610 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100611 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 return 0;
613 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000614 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 apk_path, status);
616 return status; /* always nonzero */
617 }
618}
619
620int dexopt(const char *apk_path, uid_t uid, int is_public)
621{
622 struct utimbuf ut;
623 struct stat apk_stat, dex_stat;
624 char dex_path[PKG_PATH_MAX];
625 char dexopt_flags[PROPERTY_VALUE_MAX];
626 char *end;
627 int res, zip_fd=-1, odex_fd=-1;
628
629 /* Before anything else: is there a .odex file? If so, we have
630 * pre-optimized the apk and there is nothing to do here.
631 */
632 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
633 return -1;
634 }
635
636 /* platform-specific flags affecting optimization and verification */
637 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
638
639 strcpy(dex_path, apk_path);
640 end = strrchr(dex_path, '.');
641 if (end != NULL) {
642 strcpy(end, ".odex");
643 if (stat(dex_path, &dex_stat) == 0) {
644 return 0;
645 }
646 }
647
648 if (create_cache_path(dex_path, apk_path)) {
649 return -1;
650 }
651
652 memset(&apk_stat, 0, sizeof(apk_stat));
653 stat(apk_path, &apk_stat);
654
655 zip_fd = open(apk_path, O_RDONLY, 0);
656 if (zip_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000657 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 return -1;
659 }
660
661 unlink(dex_path);
662 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
663 if (odex_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000664 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 goto fail;
666 }
667 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000668 ALOGE("dexopt cannot chown '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 goto fail;
670 }
671 if (fchmod(odex_fd,
672 S_IRUSR|S_IWUSR|S_IRGRP |
673 (is_public ? S_IROTH : 0)) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000674 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 goto fail;
676 }
677
Steve Block71f2cf12011-10-20 11:56:00 +0100678 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679
680 pid_t pid;
681 pid = fork();
682 if (pid == 0) {
683 /* child -- drop privileges before continuing */
684 if (setgid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000685 ALOGE("setgid(%d) failed during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 exit(64);
687 }
688 if (setuid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000689 ALOGE("setuid(%d) during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 exit(65);
691 }
692 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000693 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 exit(66);
695 }
696
697 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
698 exit(67); /* only get here on exec failure */
699 } else {
700 res = wait_dexopt(pid, apk_path);
701 if (res != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000702 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 goto fail;
704 }
705 }
706
707 ut.actime = apk_stat.st_atime;
708 ut.modtime = apk_stat.st_mtime;
709 utime(dex_path, &ut);
710
711 close(odex_fd);
712 close(zip_fd);
713 return 0;
714
715fail:
716 if (odex_fd >= 0) {
717 close(odex_fd);
718 unlink(dex_path);
719 }
720 if (zip_fd >= 0) {
721 close(zip_fd);
722 }
723 return -1;
724}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800725
Dianne Hackbornc1552392010-03-03 16:19:01 -0800726void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
727 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800728{
729 while (path[basepos] != 0) {
730 if (path[basepos] == '/') {
731 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800732 if (lstat(path, statbuf) < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100733 ALOGV("Making directory: %s\n", path);
Dianne Hackbornc1552392010-03-03 16:19:01 -0800734 if (mkdir(path, mode) == 0) {
735 chown(path, uid, gid);
736 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000737 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
Dianne Hackbornc1552392010-03-03 16:19:01 -0800738 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800739 }
740 path[basepos] = '/';
741 basepos++;
742 }
743 basepos++;
744 }
745}
746
Dianne Hackbornc1552392010-03-03 16:19:01 -0800747int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
748 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800749{
750 DIR *d;
751 struct dirent *de;
752 int res;
753
754 int srcend = strlen(srcpath);
755 int dstend = strlen(dstpath);
756
757 if (lstat(srcpath, statbuf) < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000758 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800759 return 1;
760 }
761
762 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800763 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
764 dstuid, dstgid, statbuf);
Steve Block71f2cf12011-10-20 11:56:00 +0100765 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800766 if (rename(srcpath, dstpath) >= 0) {
767 if (chown(dstpath, dstuid, dstgid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000768 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800769 unlink(dstpath);
770 return 1;
771 }
772 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000773 ALOGW("Unable to rename %s to %s: %s\n",
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800774 srcpath, dstpath, strerror(errno));
775 return 1;
776 }
777 return 0;
778 }
779
780 d = opendir(srcpath);
781 if (d == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000782 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800783 return 1;
784 }
785
786 res = 0;
787
788 while ((de = readdir(d))) {
789 const char *name = de->d_name;
790 /* always skip "." and ".." */
791 if (name[0] == '.') {
792 if (name[1] == 0) continue;
793 if ((name[1] == '.') && (name[2] == 0)) continue;
794 }
795
796 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000797 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800798 continue;
799 }
800
801 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000802 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800803 continue;
804 }
805
806 srcpath[srcend] = dstpath[dstend] = '/';
807 strcpy(srcpath+srcend+1, name);
808 strcpy(dstpath+dstend+1, name);
809
Dianne Hackbornc1552392010-03-03 16:19:01 -0800810 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800811 res = 1;
812 }
813
814 // Note: we will be leaving empty directories behind in srcpath,
815 // but that is okay, the package manager will be erasing all of the
816 // data associated with .apks that disappear.
817
818 srcpath[srcend] = dstpath[dstend] = 0;
819 }
820
821 closedir(d);
822 return res;
823}
824
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800825int movefiles()
826{
827 DIR *d;
828 int dfd, subfd;
829 struct dirent *de;
830 struct stat s;
831 char buf[PKG_PATH_MAX+1];
832 int bufp, bufe, bufi, readlen;
833
834 char srcpkg[PKG_NAME_MAX];
835 char dstpkg[PKG_NAME_MAX];
836 char srcpath[PKG_PATH_MAX];
837 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800838 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800839 int hasspace;
840
841 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
842 if (d == NULL) {
843 goto done;
844 }
845 dfd = dirfd(d);
846
847 /* Iterate through all files in the directory, executing the
848 * file movements requested there-in.
849 */
850 while ((de = readdir(d))) {
851 const char *name = de->d_name;
852
853 if (de->d_type == DT_DIR) {
854 continue;
855 } else {
856 subfd = openat(dfd, name, O_RDONLY);
857 if (subfd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000858 ALOGW("Unable to open update commands at %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800859 UPDATE_COMMANDS_DIR_PREFIX, name);
860 continue;
861 }
862
863 bufp = 0;
864 bufe = 0;
865 buf[PKG_PATH_MAX] = 0;
866 srcpkg[0] = dstpkg[0] = 0;
867 while (1) {
868 bufi = bufp;
869 while (bufi < bufe && buf[bufi] != '\n') {
870 bufi++;
871 }
872 if (bufi < bufe) {
873 buf[bufi] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100874 ALOGV("Processing line: %s\n", buf+bufp);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800875 hasspace = 0;
876 while (bufp < bufi && isspace(buf[bufp])) {
877 hasspace = 1;
878 bufp++;
879 }
880 if (buf[bufp] == '#' || bufp == bufi) {
881 // skip comments and empty lines.
882 } else if (hasspace) {
883 if (dstpkg[0] == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000884 ALOGW("Path before package line in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800885 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
886 } else if (srcpkg[0] == 0) {
887 // Skip -- source package no longer exists.
888 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100889 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
Kenny Root86c95842011-03-31 13:16:12 -0700890 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
891 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800892 movefileordir(srcpath, dstpath,
893 strlen(dstpath)-strlen(buf+bufp),
894 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800895 }
896 }
897 } else {
898 char* div = strchr(buf+bufp, ':');
899 if (div == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000900 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800901 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
902 } else {
903 *div = 0;
904 div++;
905 if (strlen(buf+bufp) < PKG_NAME_MAX) {
906 strcpy(dstpkg, buf+bufp);
907 } else {
908 srcpkg[0] = dstpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000909 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800910 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
911 }
912 if (strlen(div) < PKG_NAME_MAX) {
913 strcpy(srcpkg, div);
914 } else {
915 srcpkg[0] = dstpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000916 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800917 UPDATE_COMMANDS_DIR_PREFIX, name, div);
918 }
919 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700920 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800921 if (lstat(srcpath, &s) < 0) {
922 // Package no longer exists -- skip.
923 srcpkg[0] = 0;
924 }
925 } else {
926 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000927 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800928 div, UPDATE_COMMANDS_DIR_PREFIX, name);
929 }
930 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700931 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800932 if (lstat(dstpath, &s) == 0) {
933 dstuid = s.st_uid;
934 dstgid = s.st_gid;
935 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800936 // Destination package doesn't
937 // exist... due to original-package,
938 // this is normal, so don't be
939 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800940 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800941 }
942 } else {
943 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000944 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800945 div, UPDATE_COMMANDS_DIR_PREFIX, name);
946 }
947 }
Steve Block71f2cf12011-10-20 11:56:00 +0100948 ALOGV("Transfering from %s to %s: uid=%d\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800949 srcpkg, dstpkg, dstuid);
950 }
951 }
952 }
953 bufp = bufi+1;
954 } else {
955 if (bufp == 0) {
956 if (bufp < bufe) {
Steve Block8564c8d2012-01-05 23:22:43 +0000957 ALOGW("Line too long in %s%s, skipping: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800958 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
959 }
960 } else if (bufp < bufe) {
961 memcpy(buf, buf+bufp, bufe-bufp);
962 bufe -= bufp;
963 bufp = 0;
964 }
965 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
966 if (readlen < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000967 ALOGW("Failure reading update commands in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800968 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
969 break;
970 } else if (readlen == 0) {
971 break;
972 }
973 bufe += readlen;
974 buf[bufe] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100975 ALOGV("Read buf: %s\n", buf);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800976 }
977 }
978 close(subfd);
979 }
980 }
981 closedir(d);
982done:
983 return 0;
984}
Kenny Root6a6b0072010-10-07 16:46:10 -0700985
986int linklib(const char* dataDir, const char* asecLibDir)
987{
988 char libdir[PKG_PATH_MAX];
989 struct stat s, libStat;
990 int rc = 0;
991
992 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
993 if (libdirLen >= PKG_PATH_MAX) {
Steve Block3762c312012-01-06 19:20:56 +0000994 ALOGE("library dir len too large");
Kenny Root0332d1c2010-10-21 16:14:06 -0700995 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -0700996 }
997
998 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
Steve Block3762c312012-01-06 19:20:56 +0000999 ALOGE("library dir not written successfully: %s\n", strerror(errno));
Kenny Root0332d1c2010-10-21 16:14:06 -07001000 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -07001001 }
1002
1003 if (stat(dataDir, &s) < 0) return -1;
1004
1005 if (chown(dataDir, 0, 0) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001006 ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001007 return -1;
1008 }
1009
1010 if (chmod(dataDir, 0700) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001011 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001012 rc = -1;
1013 goto out;
1014 }
1015
1016 if (lstat(libdir, &libStat) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001017 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001018 rc = -1;
1019 goto out;
1020 }
1021
1022 if (S_ISDIR(libStat.st_mode)) {
1023 if (delete_dir_contents(libdir, 1, 0) < 0) {
1024 rc = -1;
1025 goto out;
1026 }
1027 } else if (S_ISLNK(libStat.st_mode)) {
1028 if (unlink(libdir) < 0) {
1029 rc = -1;
1030 goto out;
1031 }
1032 }
1033
1034 if (symlink(asecLibDir, libdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001035 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libdir, asecLibDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001036 rc = -errno;
1037 goto out;
1038 }
1039
1040 if (lchown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001041 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001042 unlink(libdir);
1043 rc = -errno;
1044 goto out;
1045 }
1046
1047out:
1048 if (chmod(dataDir, s.st_mode) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001049 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Dianne Hackbornd0c5f512012-06-07 16:53:59 -07001050 rc = -errno;
Kenny Root6a6b0072010-10-07 16:46:10 -07001051 }
1052
1053 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001054 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001055 return -errno;
1056 }
1057
1058 return rc;
1059}
1060
1061int unlinklib(const char* dataDir)
1062{
1063 char libdir[PKG_PATH_MAX];
1064 struct stat s, libStat;
1065 int rc = 0;
1066
1067 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
1068 if (libdirLen >= PKG_PATH_MAX) {
1069 return -1;
1070 }
1071
1072 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
Steve Block3762c312012-01-06 19:20:56 +00001073 ALOGE("library dir not written successfully: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001074 return -1;
1075 }
1076
1077 if (stat(dataDir, &s) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001078 ALOGE("couldn't state data dir");
Kenny Root6a6b0072010-10-07 16:46:10 -07001079 return -1;
1080 }
1081
1082 if (chown(dataDir, 0, 0) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001083 ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001084 return -1;
1085 }
1086
1087 if (chmod(dataDir, 0700) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001088 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001089 rc = -1;
1090 goto out;
1091 }
1092
1093 if (lstat(libdir, &libStat) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001094 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001095 rc = -1;
1096 goto out;
1097 }
1098
1099 if (S_ISDIR(libStat.st_mode)) {
1100 if (delete_dir_contents(libdir, 1, 0) < 0) {
1101 rc = -1;
1102 goto out;
1103 }
1104 } else if (S_ISLNK(libStat.st_mode)) {
1105 if (unlink(libdir) < 0) {
1106 rc = -1;
1107 goto out;
1108 }
1109 }
1110
1111 if (mkdir(libdir, 0755) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001112 ALOGE("cannot create dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001113 rc = -errno;
1114 goto out;
1115 }
Kenny Root515087d2012-07-30 15:00:16 -07001116 if (chmod(libdir, 0755) < 0) {
1117 ALOGE("cannot chmod dir '%s': %s\n", libdir, strerror(errno));
1118 unlink(libdir);
1119 rc = -errno;
1120 goto out;
1121 }
Kenny Root6a6b0072010-10-07 16:46:10 -07001122 if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001123 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001124 unlink(libdir);
1125 rc = -errno;
1126 goto out;
1127 }
1128
1129out:
1130 if (chmod(dataDir, s.st_mode) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001131 ALOGE("failed to chmod '%s': %s\n", dataDir, strerror(errno));
Dianne Hackbornd0c5f512012-06-07 16:53:59 -07001132 rc = -1;
Kenny Root6a6b0072010-10-07 16:46:10 -07001133 }
1134
1135 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001136 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001137 return -1;
1138 }
1139
1140 return rc;
1141}