blob: 2c30ec7080d6aa6a8191bf753edd90dfe9aa2bde [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>
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050020#include <selinux/android.h>
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050021
Kenny Root86c95842011-03-31 13:16:12 -070022/* Directory records that are used in execution of commands. */
23dir_rec_t android_data_dir;
24dir_rec_t android_asec_dir;
25dir_rec_t android_app_dir;
26dir_rec_t android_app_private_dir;
Kenny Root9bbd70a2012-09-10 11:13:36 -070027dir_rec_t android_app_lib_dir;
Dianne Hackborn197a0c82012-07-12 14:46:04 -070028dir_rec_t android_media_dir;
Kenny Root86c95842011-03-31 13:16:12 -070029dir_rec_array_t android_system_dirs;
30
Kenny Root35ab3ad2011-02-02 16:42:14 -080031int install(const char *pkgname, uid_t uid, gid_t gid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032{
33 char pkgdir[PKG_PATH_MAX];
Kenny Root9bbd70a2012-09-10 11:13:36 -070034 char libsymlink[PKG_PATH_MAX];
35 char applibdir[PKG_PATH_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
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
Kenny Root9bbd70a2012-09-10 11:13:36 -070047 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
48 ALOGE("cannot create package lib symlink origin path\n");
49 return -1;
50 }
51
52 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
53 ALOGE("cannot create package lib symlink dest path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080054 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
David 'Digit' Turner0dd50e62010-02-09 19:02:38 -080057 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000058 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root9bbd70a2012-09-10 11:13:36 -070059 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070061 if (chmod(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000062 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070063 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070064 return -1;
Nick Kralevichf68327e2011-04-14 16:20:03 -070065 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050066
Kenny Root9bbd70a2012-09-10 11:13:36 -070067 if (symlink(applibdir, libsymlink) < 0) {
68 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
69 strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070071 return -1;
Kenny Root4503cf62012-06-14 13:05:18 -070072 }
Kenny Root33ef4ee2012-06-18 10:26:36 -070073
Kenny Rooted3ce512012-10-17 10:05:10 -070074 if (selinux_android_setfilecon(libsymlink, pkgname, uid) < 0) {
75 ALOGE("cannot setfilecon dir '%s': %s\n", libsymlink, strerror(errno));
Kenny Root9bbd70a2012-09-10 11:13:36 -070076 unlink(libsymlink);
Kenny Root33ef4ee2012-06-18 10:26:36 -070077 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070078 return -1;
Kenny Root33ef4ee2012-06-18 10:26:36 -070079 }
Kenny Root33ef4ee2012-06-18 10:26:36 -070080
Kenny Root9bbd70a2012-09-10 11:13:36 -070081 if (chown(pkgdir, uid, gid) < 0) {
82 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
83 unlink(libsymlink);
84 unlink(pkgdir);
85 return -1;
86 }
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 return 0;
89}
90
Amith Yamasani0b285492011-04-14 17:35:23 -070091int uninstall(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092{
93 char pkgdir[PKG_PATH_MAX];
94
Amith Yamasani0b285492011-04-14 17:35:23 -070095 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -080096 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Amith Yamasani0b285492011-04-14 17:35:23 -070098 /* delete contents AND directory, no exceptions */
99 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100}
101
Kenny Root35ab3ad2011-02-02 16:42:14 -0800102int renamepkg(const char *oldpkgname, const char *newpkgname)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800103{
104 char oldpkgdir[PKG_PATH_MAX];
105 char newpkgdir[PKG_PATH_MAX];
106
Kenny Root86c95842011-03-31 13:16:12 -0700107 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800108 return -1;
Kenny Root86c95842011-03-31 13:16:12 -0700109 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800110 return -1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800111
112 if (rename(oldpkgdir, newpkgdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000113 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800114 return -errno;
115 }
116 return 0;
117}
118
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700119int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
120{
121 char pkgdir[PKG_PATH_MAX];
122 struct stat s;
123 int rc = 0;
124
125 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
126 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
127 return -1;
128 }
129
130 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
131 ALOGE("cannot create package path\n");
132 return -1;
133 }
134
135 if (stat(pkgdir, &s) < 0) return -1;
136
137 if (s.st_uid != 0 || s.st_gid != 0) {
138 ALOGE("fixing uid of non-root pkg: %s %d %d\n", pkgdir, s.st_uid, s.st_gid);
139 return -1;
140 }
141
142 if (chmod(pkgdir, 0751) < 0) {
143 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
144 unlink(pkgdir);
145 return -errno;
146 }
147 if (chown(pkgdir, uid, gid) < 0) {
148 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
149 unlink(pkgdir);
150 return -errno;
151 }
152
153 return 0;
154}
155
Amith Yamasani0b285492011-04-14 17:35:23 -0700156int delete_user_data(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157{
158 char pkgdir[PKG_PATH_MAX];
159
Amith Yamasani0b285492011-04-14 17:35:23 -0700160 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800161 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Amith Yamasani0b285492011-04-14 17:35:23 -0700163 /* delete contents, excluding "lib", but not the directory itself */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 return delete_dir_contents(pkgdir, 0, "lib");
165}
166
Amith Yamasani0b285492011-04-14 17:35:23 -0700167int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
168{
169 char pkgdir[PKG_PATH_MAX];
Amith Yamasani0b285492011-04-14 17:35:23 -0700170
171 // Create the data dir for the package
172 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
173 return -1;
174 }
175 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000176 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700177 return -errno;
178 }
Amith Yamasani794d62f2012-08-24 12:58:27 -0700179 if (chmod(pkgdir, 0751) < 0) {
180 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
181 unlink(pkgdir);
182 return -errno;
183 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500184 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -0400185 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500186 unlink(pkgdir);
187 return -errno;
188 }
Kenny Rootc9a1aab2012-10-16 23:28:21 -0700189 if (chown(pkgdir, uid, uid) < 0) {
190 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
191 unlink(pkgdir);
192 return -errno;
193 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500194
Amith Yamasani0b285492011-04-14 17:35:23 -0700195 return 0;
196}
197
198int delete_persona(uid_t persona)
199{
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700200 char data_path[PKG_PATH_MAX];
201 if (create_persona_path(data_path, persona)) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700202 return -1;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700203 }
204 if (delete_dir_contents(data_path, 1, NULL)) {
205 return -1;
206 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700207
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700208 char media_path[PATH_MAX];
209 if (create_persona_media_path(media_path, (userid_t) persona) == -1) {
210 return -1;
211 }
212 if (delete_dir_contents(media_path, 1, NULL) == -1) {
213 return -1;
214 }
215
216 return 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700217}
218
Amith Yamasani742a6712011-05-04 14:49:28 -0700219int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy)
220{
221 char src_data_dir[PKG_PATH_MAX];
222 char pkg_path[PKG_PATH_MAX];
223 DIR *d;
224 struct dirent *de;
225 struct stat s;
226 uid_t uid;
227
228 if (create_persona_path(src_data_dir, src_persona)) {
229 return -1;
230 }
231
232 d = opendir(src_data_dir);
233 if (d != NULL) {
234 while ((de = readdir(d))) {
235 const char *name = de->d_name;
236
237 if (de->d_type == DT_DIR) {
238 int subfd;
239 /* always skip "." and ".." */
240 if (name[0] == '.') {
241 if (name[1] == 0) continue;
242 if ((name[1] == '.') && (name[2] == 0)) continue;
243 }
244 /* Create the full path to the package's data dir */
245 create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona);
246 /* Get the file stat */
247 if (stat(pkg_path, &s) < 0) continue;
248 /* Get the uid of the package */
249 ALOGI("Adding datadir for uid = %d\n", s.st_uid);
250 uid = (uid_t) s.st_uid % PER_USER_RANGE;
251 /* Create the directory for the target */
252 make_user_data(name, uid + target_persona * PER_USER_RANGE,
253 target_persona);
254 }
255 }
256 closedir(d);
257 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700258
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700259 if (ensure_media_user_dirs((userid_t) target_persona) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700260 return -1;
261 }
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700262
Amith Yamasani742a6712011-05-04 14:49:28 -0700263 return 0;
264}
265
Amith Yamasani54289b82012-10-01 10:39:14 -0700266int delete_cache(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267{
268 char cachedir[PKG_PATH_MAX];
269
Amith Yamasani54289b82012-10-01 10:39:14 -0700270 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800271 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272
273 /* delete contents, not the directory, no exceptions */
274 return delete_dir_contents(cachedir, 0, 0);
275}
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277/* Try to ensure free_size bytes of storage are available.
278 * Returns 0 on success.
279 * This is rather simple-minded because doing a full LRU would
280 * be potentially memory-intensive, and without atime it would
281 * also require that apps constantly modify file metadata even
282 * when just reading from the cache, which is pretty awful.
283 */
Kenny Root3e319a92010-09-07 13:58:28 -0700284int free_cache(int64_t free_size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285{
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700286 cache_t* cache;
287 int64_t avail;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 DIR *d;
289 struct dirent *de;
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700290 char tmpdir[PATH_MAX];
291 char *dirpos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700293 avail = data_disk_free();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 if (avail < 0) return -1;
295
Steve Block6215d3f2012-01-04 20:05:49 +0000296 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 if (avail >= free_size) return 0;
298
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700299 cache = start_cache_collection();
300
301 // Collect cache files for primary user.
302 if (create_persona_path(tmpdir, 0) == 0) {
303 //ALOGI("adding cache files from %s\n", tmpdir);
304 add_cache_files(cache, tmpdir, "cache");
Kenny Rootad757e92011-11-29 15:54:55 -0800305 }
306
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700307 // Search for other users and add any cache files from them.
308 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
309 SECONDARY_USER_PREFIX);
310 dirpos = tmpdir + strlen(tmpdir);
311 d = opendir(tmpdir);
312 if (d != NULL) {
313 while ((de = readdir(d))) {
314 if (de->d_type == DT_DIR) {
315 const char *name = de->d_name;
316 /* always skip "." and ".." */
317 if (name[0] == '.') {
318 if (name[1] == 0) continue;
319 if ((name[1] == '.') && (name[2] == 0)) continue;
320 }
321 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
322 strcpy(dirpos, name);
323 //ALOGI("adding cache files from %s\n", tmpdir);
324 add_cache_files(cache, tmpdir, "cache");
325 } else {
326 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
327 }
328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700330 closedir(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
Oscar Montemayora8529f62009-11-18 10:14:20 -0800332
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700333 // Collect cache files on external storage for all users (if it is mounted as part
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700334 // of the internal storage).
335 strcpy(tmpdir, android_media_dir.path);
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700336 dirpos = tmpdir + strlen(tmpdir);
337 d = opendir(tmpdir);
338 if (d != NULL) {
339 while ((de = readdir(d))) {
340 if (de->d_type == DT_DIR) {
341 const char *name = de->d_name;
342 /* skip any dir that doesn't start with a number, so not a user */
343 if (name[0] < '0' || name[0] > '9') {
344 continue;
345 }
346 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
347 strcpy(dirpos, name);
348 if (lookup_media_dir(tmpdir, "Android") == 0
349 && lookup_media_dir(tmpdir, "data") == 0) {
350 //ALOGI("adding cache files from %s\n", tmpdir);
351 add_cache_files(cache, tmpdir, "cache");
352 }
353 } else {
354 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
355 }
356 }
357 }
358 closedir(d);
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700359 }
360
361 clear_cache_files(cache, free_size);
362 finish_cache_collection(cache);
363
364 return data_disk_free() >= free_size ? 0 : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365}
366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367int move_dex(const char *src, const char *dst)
368{
369 char src_dex[PKG_PATH_MAX];
370 char dst_dex[PKG_PATH_MAX];
371
Kenny Root86c95842011-03-31 13:16:12 -0700372 if (validate_apk_path(src)) return -1;
373 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374
375 if (create_cache_path(src_dex, src)) return -1;
376 if (create_cache_path(dst_dex, dst)) return -1;
377
Steve Block71f2cf12011-10-20 11:56:00 +0100378 ALOGV("move %s -> %s\n", src_dex, dst_dex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 if (rename(src_dex, dst_dex) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000380 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 return -1;
382 } else {
383 return 0;
384 }
385}
386
387int rm_dex(const char *path)
388{
389 char dex_path[PKG_PATH_MAX];
390
Kenny Root86c95842011-03-31 13:16:12 -0700391 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 if (create_cache_path(dex_path, path)) return -1;
393
Steve Block71f2cf12011-10-20 11:56:00 +0100394 ALOGV("unlink %s\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 if (unlink(dex_path) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000396 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 return -1;
398 } else {
399 return 0;
400 }
401}
402
Dianne Hackborn0c380492012-08-20 17:23:30 -0700403int get_size(const char *pkgname, int persona, const char *apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700404 const char *fwdlock_apkpath, const char *asecpath,
405 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
406 int64_t* _asecsize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407{
408 DIR *d;
409 int dfd;
410 struct dirent *de;
411 struct stat s;
412 char path[PKG_PATH_MAX];
413
Kenny Root3e319a92010-09-07 13:58:28 -0700414 int64_t codesize = 0;
415 int64_t datasize = 0;
416 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700417 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418
419 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800420 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Kenny Root86c95842011-03-31 13:16:12 -0700422 if (validate_system_app_path(apkpath) &&
423 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 if (stat(apkpath, &s) == 0) {
425 codesize += stat_size(&s);
426 }
427 }
428 /* count the forward locked apk as code if it is given
429 */
430 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
431 if (stat(fwdlock_apkpath, &s) == 0) {
432 codesize += stat_size(&s);
433 }
434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 /* count the cached dexfile as code */
436 if (!create_cache_path(path, apkpath)) {
437 if (stat(path, &s) == 0) {
438 codesize += stat_size(&s);
439 }
440 }
441
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700442 /* add in size of any libraries */
443 if (!create_pkg_path_in_dir(path, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
444 d = opendir(path);
445 if (d != NULL) {
446 dfd = dirfd(d);
447 codesize += calculate_dir_size(dfd);
448 closedir(d);
449 }
450 }
451
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700452 /* compute asec size if it is given
453 */
454 if (asecpath != NULL && asecpath[0] != '!') {
455 if (stat(asecpath, &s) == 0) {
456 asecsize += stat_size(&s);
457 }
458 }
459
Dianne Hackborn0c380492012-08-20 17:23:30 -0700460 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800461 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463
464 d = opendir(path);
465 if (d == NULL) {
466 goto done;
467 }
468 dfd = dirfd(d);
469
Kenny Root86c95842011-03-31 13:16:12 -0700470 /* most stuff in the pkgdir is data, except for the "cache"
471 * directory and below, which is cache, and the "lib" directory
472 * and below, which is code...
473 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 while ((de = readdir(d))) {
475 const char *name = de->d_name;
476
477 if (de->d_type == DT_DIR) {
478 int subfd;
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700479 int64_t statsize = 0;
480 int64_t dirsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 /* always skip "." and ".." */
482 if (name[0] == '.') {
483 if (name[1] == 0) continue;
484 if ((name[1] == '.') && (name[2] == 0)) continue;
485 }
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700486 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
487 statsize = stat_size(&s);
488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
490 if (subfd >= 0) {
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700491 dirsize = calculate_dir_size(subfd);
492 }
493 if(!strcmp(name,"lib")) {
494 codesize += dirsize + statsize;
495 } else if(!strcmp(name,"cache")) {
496 cachesize += dirsize + statsize;
497 } else {
498 datasize += dirsize + statsize;
499 }
500 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
501 // This is the symbolic link to the application's library
502 // code. We'll count this as code instead of data, since
503 // it is not something that the app creates.
504 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
505 codesize += stat_size(&s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
507 } else {
508 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
509 datasize += stat_size(&s);
510 }
511 }
512 }
513 closedir(d);
514done:
515 *_codesize = codesize;
516 *_datasize = datasize;
517 *_cachesize = cachesize;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700518 *_asecsize = asecsize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 return 0;
520}
521
522
523/* a simpler version of dexOptGenerateCacheFileName() */
524int create_cache_path(char path[PKG_PATH_MAX], const char *src)
525{
526 char *tmp;
527 int srclen;
528 int dstlen;
529
530 srclen = strlen(src);
531
532 /* demand that we are an absolute path */
533 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
534 return -1;
535 }
536
537 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
538 return -1;
539 }
540
541 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
542 strlen(DALVIK_CACHE_POSTFIX) + 1;
543
544 if (dstlen > PKG_PATH_MAX) {
545 return -1;
546 }
547
548 sprintf(path,"%s%s%s",
549 DALVIK_CACHE_PREFIX,
550 src + 1, /* skip the leading / */
551 DALVIK_CACHE_POSTFIX);
552
553 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
554 if (*tmp == '/') {
555 *tmp = '@';
556 }
557 }
558
559 return 0;
560}
561
562static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
563 const char* dexopt_flags)
564{
565 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
566 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
567 char zip_num[MAX_INT_LEN];
568 char odex_num[MAX_INT_LEN];
569
570 sprintf(zip_num, "%d", zip_fd);
571 sprintf(odex_num, "%d", odex_fd);
572
573 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
574 dexopt_flags, (char*) NULL);
Steve Block3762c312012-01-06 19:20:56 +0000575 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576}
577
578static int wait_dexopt(pid_t pid, const char* apk_path)
579{
580 int status;
581 pid_t got_pid;
582
583 /*
584 * Wait for the optimization process to finish.
585 */
586 while (1) {
587 got_pid = waitpid(pid, &status, 0);
588 if (got_pid == -1 && errno == EINTR) {
589 printf("waitpid interrupted, retrying\n");
590 } else {
591 break;
592 }
593 }
594 if (got_pid != pid) {
Steve Block8564c8d2012-01-05 23:22:43 +0000595 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 (int) pid, (int) got_pid, strerror(errno));
597 return 1;
598 }
599
600 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100601 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 return 0;
603 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000604 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 apk_path, status);
606 return status; /* always nonzero */
607 }
608}
609
610int dexopt(const char *apk_path, uid_t uid, int is_public)
611{
612 struct utimbuf ut;
613 struct stat apk_stat, dex_stat;
614 char dex_path[PKG_PATH_MAX];
615 char dexopt_flags[PROPERTY_VALUE_MAX];
616 char *end;
617 int res, zip_fd=-1, odex_fd=-1;
618
619 /* Before anything else: is there a .odex file? If so, we have
620 * pre-optimized the apk and there is nothing to do here.
621 */
622 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
623 return -1;
624 }
625
626 /* platform-specific flags affecting optimization and verification */
627 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
628
629 strcpy(dex_path, apk_path);
630 end = strrchr(dex_path, '.');
631 if (end != NULL) {
632 strcpy(end, ".odex");
633 if (stat(dex_path, &dex_stat) == 0) {
634 return 0;
635 }
636 }
637
638 if (create_cache_path(dex_path, apk_path)) {
639 return -1;
640 }
641
642 memset(&apk_stat, 0, sizeof(apk_stat));
643 stat(apk_path, &apk_stat);
644
645 zip_fd = open(apk_path, O_RDONLY, 0);
646 if (zip_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000647 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 return -1;
649 }
650
651 unlink(dex_path);
652 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
653 if (odex_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000654 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 goto fail;
656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 if (fchmod(odex_fd,
658 S_IRUSR|S_IWUSR|S_IRGRP |
659 (is_public ? S_IROTH : 0)) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000660 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 goto fail;
662 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700663 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
664 ALOGE("dexopt cannot chown '%s'\n", dex_path);
665 goto fail;
666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667
Steve Block71f2cf12011-10-20 11:56:00 +0100668 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669
670 pid_t pid;
671 pid = fork();
672 if (pid == 0) {
673 /* child -- drop privileges before continuing */
674 if (setgid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000675 ALOGE("setgid(%d) failed during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 exit(64);
677 }
678 if (setuid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000679 ALOGE("setuid(%d) during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 exit(65);
681 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700682 // drop capabilities
683 struct __user_cap_header_struct capheader;
684 struct __user_cap_data_struct capdata[2];
685 memset(&capheader, 0, sizeof(capheader));
686 memset(&capdata, 0, sizeof(capdata));
687 capheader.version = _LINUX_CAPABILITY_VERSION_3;
688 if (capset(&capheader, &capdata[0]) < 0) {
689 ALOGE("capset failed: %s\n", strerror(errno));
690 exit(66);
691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 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));
Nick Kralevich812b19a2012-08-31 16:08:06 -0700694 exit(67);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 }
696
697 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
Nick Kralevich812b19a2012-08-31 16:08:06 -0700698 exit(68); /* only get here on exec failure */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 } 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
Nick Kralevich7de350a2012-09-07 15:48:11 -07001005 if (chown(dataDir, AID_INSTALL, AID_INSTALL) < 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) {
Nick Kralevich7de350a2012-09-07 15:48:11 -07001011 ALOGE("linklib() 1: 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) {
Nick Kralevich7de350a2012-09-07 15:48:11 -07001049 ALOGE("linklib() 2: 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}