blob: 4da772a0d7e6f5f71791f5b04f0141114770dbd6 [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 Root36740042012-10-17 10:50:14 -070074 if (selinux_android_setfilecon(libsymlink, pkgname, AID_SYSTEM) < 0) {
Kenny Rooted3ce512012-10-17 10:05:10 -070075 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 Root57c63d82012-10-17 09:50:35 -070081 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
82 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root36740042012-10-17 10:50:14 -070083 unlink(libsymlink);
Kenny Root57c63d82012-10-17 09:50:35 -070084 unlink(pkgdir);
85 return -errno;
86 }
87
Kenny Root9bbd70a2012-09-10 11:13:36 -070088 if (chown(pkgdir, uid, gid) < 0) {
89 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
90 unlink(libsymlink);
91 unlink(pkgdir);
92 return -1;
93 }
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 return 0;
96}
97
Amith Yamasani0b285492011-04-14 17:35:23 -070098int uninstall(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099{
100 char pkgdir[PKG_PATH_MAX];
101
Amith Yamasani0b285492011-04-14 17:35:23 -0700102 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800103 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
Amith Yamasani0b285492011-04-14 17:35:23 -0700105 /* delete contents AND directory, no exceptions */
106 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107}
108
Kenny Root35ab3ad2011-02-02 16:42:14 -0800109int renamepkg(const char *oldpkgname, const char *newpkgname)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800110{
111 char oldpkgdir[PKG_PATH_MAX];
112 char newpkgdir[PKG_PATH_MAX];
113
Kenny Root86c95842011-03-31 13:16:12 -0700114 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800115 return -1;
Kenny Root86c95842011-03-31 13:16:12 -0700116 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800117 return -1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800118
119 if (rename(oldpkgdir, newpkgdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000120 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800121 return -errno;
122 }
123 return 0;
124}
125
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700126int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
127{
128 char pkgdir[PKG_PATH_MAX];
129 struct stat s;
130 int rc = 0;
131
132 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
133 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
134 return -1;
135 }
136
137 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
138 ALOGE("cannot create package path\n");
139 return -1;
140 }
141
142 if (stat(pkgdir, &s) < 0) return -1;
143
144 if (s.st_uid != 0 || s.st_gid != 0) {
145 ALOGE("fixing uid of non-root pkg: %s %d %d\n", pkgdir, s.st_uid, s.st_gid);
146 return -1;
147 }
148
149 if (chmod(pkgdir, 0751) < 0) {
150 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
151 unlink(pkgdir);
152 return -errno;
153 }
154 if (chown(pkgdir, uid, gid) < 0) {
155 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
156 unlink(pkgdir);
157 return -errno;
158 }
159
160 return 0;
161}
162
Amith Yamasani0b285492011-04-14 17:35:23 -0700163int delete_user_data(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164{
165 char pkgdir[PKG_PATH_MAX];
166
Amith Yamasani0b285492011-04-14 17:35:23 -0700167 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800168 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
Amith Yamasani0b285492011-04-14 17:35:23 -0700170 /* delete contents, excluding "lib", but not the directory itself */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 return delete_dir_contents(pkgdir, 0, "lib");
172}
173
Amith Yamasani0b285492011-04-14 17:35:23 -0700174int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
175{
176 char pkgdir[PKG_PATH_MAX];
Amith Yamasani0b285492011-04-14 17:35:23 -0700177
178 // Create the data dir for the package
179 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
180 return -1;
181 }
182 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000183 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700184 return -errno;
185 }
Amith Yamasani794d62f2012-08-24 12:58:27 -0700186 if (chmod(pkgdir, 0751) < 0) {
187 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
188 unlink(pkgdir);
189 return -errno;
190 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500191 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -0400192 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500193 unlink(pkgdir);
194 return -errno;
195 }
Kenny Rootc9a1aab2012-10-16 23:28:21 -0700196 if (chown(pkgdir, uid, uid) < 0) {
197 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
198 unlink(pkgdir);
199 return -errno;
200 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500201
Amith Yamasani0b285492011-04-14 17:35:23 -0700202 return 0;
203}
204
205int delete_persona(uid_t persona)
206{
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700207 char data_path[PKG_PATH_MAX];
208 if (create_persona_path(data_path, persona)) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700209 return -1;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700210 }
211 if (delete_dir_contents(data_path, 1, NULL)) {
212 return -1;
213 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700214
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700215 char media_path[PATH_MAX];
216 if (create_persona_media_path(media_path, (userid_t) persona) == -1) {
217 return -1;
218 }
219 if (delete_dir_contents(media_path, 1, NULL) == -1) {
220 return -1;
221 }
222
223 return 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700224}
225
Amith Yamasani742a6712011-05-04 14:49:28 -0700226int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy)
227{
228 char src_data_dir[PKG_PATH_MAX];
229 char pkg_path[PKG_PATH_MAX];
230 DIR *d;
231 struct dirent *de;
232 struct stat s;
233 uid_t uid;
234
235 if (create_persona_path(src_data_dir, src_persona)) {
236 return -1;
237 }
238
239 d = opendir(src_data_dir);
240 if (d != NULL) {
241 while ((de = readdir(d))) {
242 const char *name = de->d_name;
243
244 if (de->d_type == DT_DIR) {
245 int subfd;
246 /* always skip "." and ".." */
247 if (name[0] == '.') {
248 if (name[1] == 0) continue;
249 if ((name[1] == '.') && (name[2] == 0)) continue;
250 }
251 /* Create the full path to the package's data dir */
252 create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona);
253 /* Get the file stat */
254 if (stat(pkg_path, &s) < 0) continue;
255 /* Get the uid of the package */
256 ALOGI("Adding datadir for uid = %d\n", s.st_uid);
257 uid = (uid_t) s.st_uid % PER_USER_RANGE;
258 /* Create the directory for the target */
259 make_user_data(name, uid + target_persona * PER_USER_RANGE,
260 target_persona);
261 }
262 }
263 closedir(d);
264 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700265
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700266 if (ensure_media_user_dirs((userid_t) target_persona) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700267 return -1;
268 }
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700269
Amith Yamasani742a6712011-05-04 14:49:28 -0700270 return 0;
271}
272
Amith Yamasani54289b82012-10-01 10:39:14 -0700273int delete_cache(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274{
275 char cachedir[PKG_PATH_MAX];
276
Amith Yamasani54289b82012-10-01 10:39:14 -0700277 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800278 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279
280 /* delete contents, not the directory, no exceptions */
281 return delete_dir_contents(cachedir, 0, 0);
282}
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284/* Try to ensure free_size bytes of storage are available.
285 * Returns 0 on success.
286 * This is rather simple-minded because doing a full LRU would
287 * be potentially memory-intensive, and without atime it would
288 * also require that apps constantly modify file metadata even
289 * when just reading from the cache, which is pretty awful.
290 */
Kenny Root3e319a92010-09-07 13:58:28 -0700291int free_cache(int64_t free_size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292{
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700293 cache_t* cache;
294 int64_t avail;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 DIR *d;
296 struct dirent *de;
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700297 char tmpdir[PATH_MAX];
298 char *dirpos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700300 avail = data_disk_free();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 if (avail < 0) return -1;
302
Steve Block6215d3f2012-01-04 20:05:49 +0000303 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 if (avail >= free_size) return 0;
305
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700306 cache = start_cache_collection();
307
308 // Collect cache files for primary user.
309 if (create_persona_path(tmpdir, 0) == 0) {
310 //ALOGI("adding cache files from %s\n", tmpdir);
311 add_cache_files(cache, tmpdir, "cache");
Kenny Rootad757e92011-11-29 15:54:55 -0800312 }
313
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700314 // Search for other users and add any cache files from them.
315 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
316 SECONDARY_USER_PREFIX);
317 dirpos = tmpdir + strlen(tmpdir);
318 d = opendir(tmpdir);
319 if (d != NULL) {
320 while ((de = readdir(d))) {
321 if (de->d_type == DT_DIR) {
322 const char *name = de->d_name;
323 /* always skip "." and ".." */
324 if (name[0] == '.') {
325 if (name[1] == 0) continue;
326 if ((name[1] == '.') && (name[2] == 0)) continue;
327 }
328 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
329 strcpy(dirpos, name);
330 //ALOGI("adding cache files from %s\n", tmpdir);
331 add_cache_files(cache, tmpdir, "cache");
332 } else {
333 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
334 }
335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700337 closedir(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
Oscar Montemayora8529f62009-11-18 10:14:20 -0800339
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700340 // Collect cache files on external storage for all users (if it is mounted as part
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700341 // of the internal storage).
342 strcpy(tmpdir, android_media_dir.path);
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700343 dirpos = tmpdir + strlen(tmpdir);
344 d = opendir(tmpdir);
345 if (d != NULL) {
346 while ((de = readdir(d))) {
347 if (de->d_type == DT_DIR) {
348 const char *name = de->d_name;
349 /* skip any dir that doesn't start with a number, so not a user */
350 if (name[0] < '0' || name[0] > '9') {
351 continue;
352 }
353 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
354 strcpy(dirpos, name);
355 if (lookup_media_dir(tmpdir, "Android") == 0
356 && lookup_media_dir(tmpdir, "data") == 0) {
357 //ALOGI("adding cache files from %s\n", tmpdir);
358 add_cache_files(cache, tmpdir, "cache");
359 }
360 } else {
361 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
362 }
363 }
364 }
365 closedir(d);
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700366 }
367
368 clear_cache_files(cache, free_size);
369 finish_cache_collection(cache);
370
371 return data_disk_free() >= free_size ? 0 : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372}
373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374int move_dex(const char *src, const char *dst)
375{
376 char src_dex[PKG_PATH_MAX];
377 char dst_dex[PKG_PATH_MAX];
378
Kenny Root86c95842011-03-31 13:16:12 -0700379 if (validate_apk_path(src)) return -1;
380 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381
382 if (create_cache_path(src_dex, src)) return -1;
383 if (create_cache_path(dst_dex, dst)) return -1;
384
Steve Block71f2cf12011-10-20 11:56:00 +0100385 ALOGV("move %s -> %s\n", src_dex, dst_dex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 if (rename(src_dex, dst_dex) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000387 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 return -1;
389 } else {
390 return 0;
391 }
392}
393
394int rm_dex(const char *path)
395{
396 char dex_path[PKG_PATH_MAX];
397
Kenny Root86c95842011-03-31 13:16:12 -0700398 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 if (create_cache_path(dex_path, path)) return -1;
400
Steve Block71f2cf12011-10-20 11:56:00 +0100401 ALOGV("unlink %s\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 if (unlink(dex_path) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000403 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 return -1;
405 } else {
406 return 0;
407 }
408}
409
Dianne Hackborn0c380492012-08-20 17:23:30 -0700410int get_size(const char *pkgname, int persona, const char *apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700411 const char *fwdlock_apkpath, const char *asecpath,
412 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
413 int64_t* _asecsize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414{
415 DIR *d;
416 int dfd;
417 struct dirent *de;
418 struct stat s;
419 char path[PKG_PATH_MAX];
420
Kenny Root3e319a92010-09-07 13:58:28 -0700421 int64_t codesize = 0;
422 int64_t datasize = 0;
423 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700424 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425
426 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800427 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 */
Kenny Root86c95842011-03-31 13:16:12 -0700429 if (validate_system_app_path(apkpath) &&
430 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 if (stat(apkpath, &s) == 0) {
432 codesize += stat_size(&s);
433 }
434 }
435 /* count the forward locked apk as code if it is given
436 */
437 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
438 if (stat(fwdlock_apkpath, &s) == 0) {
439 codesize += stat_size(&s);
440 }
441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 /* count the cached dexfile as code */
443 if (!create_cache_path(path, apkpath)) {
444 if (stat(path, &s) == 0) {
445 codesize += stat_size(&s);
446 }
447 }
448
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700449 /* add in size of any libraries */
450 if (!create_pkg_path_in_dir(path, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
451 d = opendir(path);
452 if (d != NULL) {
453 dfd = dirfd(d);
454 codesize += calculate_dir_size(dfd);
455 closedir(d);
456 }
457 }
458
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700459 /* compute asec size if it is given
460 */
461 if (asecpath != NULL && asecpath[0] != '!') {
462 if (stat(asecpath, &s) == 0) {
463 asecsize += stat_size(&s);
464 }
465 }
466
Dianne Hackborn0c380492012-08-20 17:23:30 -0700467 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800468 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 }
470
471 d = opendir(path);
472 if (d == NULL) {
473 goto done;
474 }
475 dfd = dirfd(d);
476
Kenny Root86c95842011-03-31 13:16:12 -0700477 /* most stuff in the pkgdir is data, except for the "cache"
478 * directory and below, which is cache, and the "lib" directory
479 * and below, which is code...
480 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 while ((de = readdir(d))) {
482 const char *name = de->d_name;
483
484 if (de->d_type == DT_DIR) {
485 int subfd;
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700486 int64_t statsize = 0;
487 int64_t dirsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 /* always skip "." and ".." */
489 if (name[0] == '.') {
490 if (name[1] == 0) continue;
491 if ((name[1] == '.') && (name[2] == 0)) continue;
492 }
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700493 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
494 statsize = stat_size(&s);
495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
497 if (subfd >= 0) {
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700498 dirsize = calculate_dir_size(subfd);
499 }
500 if(!strcmp(name,"lib")) {
501 codesize += dirsize + statsize;
502 } else if(!strcmp(name,"cache")) {
503 cachesize += dirsize + statsize;
504 } else {
505 datasize += dirsize + statsize;
506 }
507 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
508 // This is the symbolic link to the application's library
509 // code. We'll count this as code instead of data, since
510 // it is not something that the app creates.
511 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
512 codesize += stat_size(&s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514 } else {
515 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
516 datasize += stat_size(&s);
517 }
518 }
519 }
520 closedir(d);
521done:
522 *_codesize = codesize;
523 *_datasize = datasize;
524 *_cachesize = cachesize;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700525 *_asecsize = asecsize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 return 0;
527}
528
529
530/* a simpler version of dexOptGenerateCacheFileName() */
531int create_cache_path(char path[PKG_PATH_MAX], const char *src)
532{
533 char *tmp;
534 int srclen;
535 int dstlen;
536
537 srclen = strlen(src);
538
539 /* demand that we are an absolute path */
540 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
541 return -1;
542 }
543
544 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
545 return -1;
546 }
547
548 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
549 strlen(DALVIK_CACHE_POSTFIX) + 1;
550
551 if (dstlen > PKG_PATH_MAX) {
552 return -1;
553 }
554
555 sprintf(path,"%s%s%s",
556 DALVIK_CACHE_PREFIX,
557 src + 1, /* skip the leading / */
558 DALVIK_CACHE_POSTFIX);
559
560 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
561 if (*tmp == '/') {
562 *tmp = '@';
563 }
564 }
565
566 return 0;
567}
568
569static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
570 const char* dexopt_flags)
571{
572 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
573 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
574 char zip_num[MAX_INT_LEN];
575 char odex_num[MAX_INT_LEN];
576
577 sprintf(zip_num, "%d", zip_fd);
578 sprintf(odex_num, "%d", odex_fd);
579
580 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
581 dexopt_flags, (char*) NULL);
Steve Block3762c312012-01-06 19:20:56 +0000582 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583}
584
585static int wait_dexopt(pid_t pid, const char* apk_path)
586{
587 int status;
588 pid_t got_pid;
589
590 /*
591 * Wait for the optimization process to finish.
592 */
593 while (1) {
594 got_pid = waitpid(pid, &status, 0);
595 if (got_pid == -1 && errno == EINTR) {
596 printf("waitpid interrupted, retrying\n");
597 } else {
598 break;
599 }
600 }
601 if (got_pid != pid) {
Steve Block8564c8d2012-01-05 23:22:43 +0000602 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 (int) pid, (int) got_pid, strerror(errno));
604 return 1;
605 }
606
607 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100608 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 return 0;
610 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000611 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 apk_path, status);
613 return status; /* always nonzero */
614 }
615}
616
617int dexopt(const char *apk_path, uid_t uid, int is_public)
618{
619 struct utimbuf ut;
620 struct stat apk_stat, dex_stat;
621 char dex_path[PKG_PATH_MAX];
622 char dexopt_flags[PROPERTY_VALUE_MAX];
623 char *end;
624 int res, zip_fd=-1, odex_fd=-1;
625
626 /* Before anything else: is there a .odex file? If so, we have
627 * pre-optimized the apk and there is nothing to do here.
628 */
629 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
630 return -1;
631 }
632
633 /* platform-specific flags affecting optimization and verification */
634 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
635
636 strcpy(dex_path, apk_path);
637 end = strrchr(dex_path, '.');
638 if (end != NULL) {
639 strcpy(end, ".odex");
640 if (stat(dex_path, &dex_stat) == 0) {
641 return 0;
642 }
643 }
644
645 if (create_cache_path(dex_path, apk_path)) {
646 return -1;
647 }
648
649 memset(&apk_stat, 0, sizeof(apk_stat));
650 stat(apk_path, &apk_stat);
651
652 zip_fd = open(apk_path, O_RDONLY, 0);
653 if (zip_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000654 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 return -1;
656 }
657
658 unlink(dex_path);
659 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
660 if (odex_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000661 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 goto fail;
663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 if (fchmod(odex_fd,
665 S_IRUSR|S_IWUSR|S_IRGRP |
666 (is_public ? S_IROTH : 0)) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000667 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 goto fail;
669 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700670 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
671 ALOGE("dexopt cannot chown '%s'\n", dex_path);
672 goto fail;
673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674
Steve Block71f2cf12011-10-20 11:56:00 +0100675 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676
677 pid_t pid;
678 pid = fork();
679 if (pid == 0) {
680 /* child -- drop privileges before continuing */
681 if (setgid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000682 ALOGE("setgid(%d) failed during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 exit(64);
684 }
685 if (setuid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000686 ALOGE("setuid(%d) during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 exit(65);
688 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700689 // drop capabilities
690 struct __user_cap_header_struct capheader;
691 struct __user_cap_data_struct capdata[2];
692 memset(&capheader, 0, sizeof(capheader));
693 memset(&capdata, 0, sizeof(capdata));
694 capheader.version = _LINUX_CAPABILITY_VERSION_3;
695 if (capset(&capheader, &capdata[0]) < 0) {
696 ALOGE("capset failed: %s\n", strerror(errno));
697 exit(66);
698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000700 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
Nick Kralevich812b19a2012-08-31 16:08:06 -0700701 exit(67);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
704 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
Nick Kralevich812b19a2012-08-31 16:08:06 -0700705 exit(68); /* only get here on exec failure */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 } else {
707 res = wait_dexopt(pid, apk_path);
708 if (res != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000709 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 goto fail;
711 }
712 }
713
714 ut.actime = apk_stat.st_atime;
715 ut.modtime = apk_stat.st_mtime;
716 utime(dex_path, &ut);
717
718 close(odex_fd);
719 close(zip_fd);
720 return 0;
721
722fail:
723 if (odex_fd >= 0) {
724 close(odex_fd);
725 unlink(dex_path);
726 }
727 if (zip_fd >= 0) {
728 close(zip_fd);
729 }
730 return -1;
731}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800732
Dianne Hackbornc1552392010-03-03 16:19:01 -0800733void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
734 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800735{
736 while (path[basepos] != 0) {
737 if (path[basepos] == '/') {
738 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800739 if (lstat(path, statbuf) < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100740 ALOGV("Making directory: %s\n", path);
Dianne Hackbornc1552392010-03-03 16:19:01 -0800741 if (mkdir(path, mode) == 0) {
742 chown(path, uid, gid);
743 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000744 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
Dianne Hackbornc1552392010-03-03 16:19:01 -0800745 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800746 }
747 path[basepos] = '/';
748 basepos++;
749 }
750 basepos++;
751 }
752}
753
Dianne Hackbornc1552392010-03-03 16:19:01 -0800754int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
755 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800756{
757 DIR *d;
758 struct dirent *de;
759 int res;
760
761 int srcend = strlen(srcpath);
762 int dstend = strlen(dstpath);
763
764 if (lstat(srcpath, statbuf) < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000765 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800766 return 1;
767 }
768
769 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800770 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
771 dstuid, dstgid, statbuf);
Steve Block71f2cf12011-10-20 11:56:00 +0100772 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800773 if (rename(srcpath, dstpath) >= 0) {
774 if (chown(dstpath, dstuid, dstgid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000775 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800776 unlink(dstpath);
777 return 1;
778 }
779 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000780 ALOGW("Unable to rename %s to %s: %s\n",
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800781 srcpath, dstpath, strerror(errno));
782 return 1;
783 }
784 return 0;
785 }
786
787 d = opendir(srcpath);
788 if (d == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000789 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800790 return 1;
791 }
792
793 res = 0;
794
795 while ((de = readdir(d))) {
796 const char *name = de->d_name;
797 /* always skip "." and ".." */
798 if (name[0] == '.') {
799 if (name[1] == 0) continue;
800 if ((name[1] == '.') && (name[2] == 0)) continue;
801 }
802
803 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000804 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800805 continue;
806 }
807
808 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000809 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800810 continue;
811 }
812
813 srcpath[srcend] = dstpath[dstend] = '/';
814 strcpy(srcpath+srcend+1, name);
815 strcpy(dstpath+dstend+1, name);
816
Dianne Hackbornc1552392010-03-03 16:19:01 -0800817 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800818 res = 1;
819 }
820
821 // Note: we will be leaving empty directories behind in srcpath,
822 // but that is okay, the package manager will be erasing all of the
823 // data associated with .apks that disappear.
824
825 srcpath[srcend] = dstpath[dstend] = 0;
826 }
827
828 closedir(d);
829 return res;
830}
831
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800832int movefiles()
833{
834 DIR *d;
835 int dfd, subfd;
836 struct dirent *de;
837 struct stat s;
838 char buf[PKG_PATH_MAX+1];
839 int bufp, bufe, bufi, readlen;
840
841 char srcpkg[PKG_NAME_MAX];
842 char dstpkg[PKG_NAME_MAX];
843 char srcpath[PKG_PATH_MAX];
844 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800845 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800846 int hasspace;
847
848 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
849 if (d == NULL) {
850 goto done;
851 }
852 dfd = dirfd(d);
853
854 /* Iterate through all files in the directory, executing the
855 * file movements requested there-in.
856 */
857 while ((de = readdir(d))) {
858 const char *name = de->d_name;
859
860 if (de->d_type == DT_DIR) {
861 continue;
862 } else {
863 subfd = openat(dfd, name, O_RDONLY);
864 if (subfd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000865 ALOGW("Unable to open update commands at %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800866 UPDATE_COMMANDS_DIR_PREFIX, name);
867 continue;
868 }
869
870 bufp = 0;
871 bufe = 0;
872 buf[PKG_PATH_MAX] = 0;
873 srcpkg[0] = dstpkg[0] = 0;
874 while (1) {
875 bufi = bufp;
876 while (bufi < bufe && buf[bufi] != '\n') {
877 bufi++;
878 }
879 if (bufi < bufe) {
880 buf[bufi] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100881 ALOGV("Processing line: %s\n", buf+bufp);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800882 hasspace = 0;
883 while (bufp < bufi && isspace(buf[bufp])) {
884 hasspace = 1;
885 bufp++;
886 }
887 if (buf[bufp] == '#' || bufp == bufi) {
888 // skip comments and empty lines.
889 } else if (hasspace) {
890 if (dstpkg[0] == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000891 ALOGW("Path before package line in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800892 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
893 } else if (srcpkg[0] == 0) {
894 // Skip -- source package no longer exists.
895 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100896 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
Kenny Root86c95842011-03-31 13:16:12 -0700897 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
898 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800899 movefileordir(srcpath, dstpath,
900 strlen(dstpath)-strlen(buf+bufp),
901 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800902 }
903 }
904 } else {
905 char* div = strchr(buf+bufp, ':');
906 if (div == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000907 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800908 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
909 } else {
910 *div = 0;
911 div++;
912 if (strlen(buf+bufp) < PKG_NAME_MAX) {
913 strcpy(dstpkg, buf+bufp);
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, buf+bufp);
918 }
919 if (strlen(div) < PKG_NAME_MAX) {
920 strcpy(srcpkg, div);
921 } else {
922 srcpkg[0] = dstpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000923 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800924 UPDATE_COMMANDS_DIR_PREFIX, name, div);
925 }
926 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700927 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800928 if (lstat(srcpath, &s) < 0) {
929 // Package no longer exists -- skip.
930 srcpkg[0] = 0;
931 }
932 } else {
933 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000934 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800935 div, UPDATE_COMMANDS_DIR_PREFIX, name);
936 }
937 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700938 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800939 if (lstat(dstpath, &s) == 0) {
940 dstuid = s.st_uid;
941 dstgid = s.st_gid;
942 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800943 // Destination package doesn't
944 // exist... due to original-package,
945 // this is normal, so don't be
946 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800947 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800948 }
949 } else {
950 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000951 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800952 div, UPDATE_COMMANDS_DIR_PREFIX, name);
953 }
954 }
Steve Block71f2cf12011-10-20 11:56:00 +0100955 ALOGV("Transfering from %s to %s: uid=%d\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800956 srcpkg, dstpkg, dstuid);
957 }
958 }
959 }
960 bufp = bufi+1;
961 } else {
962 if (bufp == 0) {
963 if (bufp < bufe) {
Steve Block8564c8d2012-01-05 23:22:43 +0000964 ALOGW("Line too long in %s%s, skipping: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800965 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
966 }
967 } else if (bufp < bufe) {
968 memcpy(buf, buf+bufp, bufe-bufp);
969 bufe -= bufp;
970 bufp = 0;
971 }
972 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
973 if (readlen < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000974 ALOGW("Failure reading update commands in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800975 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
976 break;
977 } else if (readlen == 0) {
978 break;
979 }
980 bufe += readlen;
981 buf[bufe] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100982 ALOGV("Read buf: %s\n", buf);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800983 }
984 }
985 close(subfd);
986 }
987 }
988 closedir(d);
989done:
990 return 0;
991}
Kenny Root6a6b0072010-10-07 16:46:10 -0700992
993int linklib(const char* dataDir, const char* asecLibDir)
994{
995 char libdir[PKG_PATH_MAX];
996 struct stat s, libStat;
997 int rc = 0;
998
999 const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX);
1000 if (libdirLen >= PKG_PATH_MAX) {
Steve Block3762c312012-01-06 19:20:56 +00001001 ALOGE("library dir len too large");
Kenny Root0332d1c2010-10-21 16:14:06 -07001002 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -07001003 }
1004
1005 if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) {
Steve Block3762c312012-01-06 19:20:56 +00001006 ALOGE("library dir not written successfully: %s\n", strerror(errno));
Kenny Root0332d1c2010-10-21 16:14:06 -07001007 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -07001008 }
1009
1010 if (stat(dataDir, &s) < 0) return -1;
1011
Nick Kralevich7de350a2012-09-07 15:48:11 -07001012 if (chown(dataDir, AID_INSTALL, AID_INSTALL) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001013 ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001014 return -1;
1015 }
1016
1017 if (chmod(dataDir, 0700) < 0) {
Nick Kralevich7de350a2012-09-07 15:48:11 -07001018 ALOGE("linklib() 1: failed to chmod '%s': %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001019 rc = -1;
1020 goto out;
1021 }
1022
1023 if (lstat(libdir, &libStat) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001024 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001025 rc = -1;
1026 goto out;
1027 }
1028
1029 if (S_ISDIR(libStat.st_mode)) {
1030 if (delete_dir_contents(libdir, 1, 0) < 0) {
1031 rc = -1;
1032 goto out;
1033 }
1034 } else if (S_ISLNK(libStat.st_mode)) {
1035 if (unlink(libdir) < 0) {
1036 rc = -1;
1037 goto out;
1038 }
1039 }
1040
1041 if (symlink(asecLibDir, libdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001042 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libdir, asecLibDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001043 rc = -errno;
1044 goto out;
1045 }
1046
1047 if (lchown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001048 ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001049 unlink(libdir);
1050 rc = -errno;
1051 goto out;
1052 }
1053
1054out:
1055 if (chmod(dataDir, s.st_mode) < 0) {
Nick Kralevich7de350a2012-09-07 15:48:11 -07001056 ALOGE("linklib() 2: failed to chmod '%s': %s\n", dataDir, strerror(errno));
Dianne Hackbornd0c5f512012-06-07 16:53:59 -07001057 rc = -errno;
Kenny Root6a6b0072010-10-07 16:46:10 -07001058 }
1059
1060 if (chown(dataDir, s.st_uid, s.st_gid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001061 ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001062 return -errno;
1063 }
1064
1065 return rc;
1066}