blob: 387f33d1097f24553ba485bf677b46ed9f20f4f7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2** Copyright 2008, The Android Open Source Project
3**
4** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
7**
8** http://www.apache.org/licenses/LICENSE-2.0
9**
10** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
14** limitations under the License.
15*/
16
Nick Kralevich812b19a2012-08-31 16:08:06 -070017#include <linux/capability.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#include "installd.h"
Kenny Root33b22642010-11-30 13:49:32 -080019#include <diskusage/dirsize.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050021#ifdef HAVE_SELINUX
22#include <selinux/android.h>
23#endif
24
Kenny Root86c95842011-03-31 13:16:12 -070025/* Directory records that are used in execution of commands. */
26dir_rec_t android_data_dir;
27dir_rec_t android_asec_dir;
28dir_rec_t android_app_dir;
29dir_rec_t android_app_private_dir;
Kenny Root9bbd70a2012-09-10 11:13:36 -070030dir_rec_t android_app_lib_dir;
Dianne Hackborn197a0c82012-07-12 14:46:04 -070031dir_rec_t android_media_dir;
Kenny Root86c95842011-03-31 13:16:12 -070032dir_rec_array_t android_system_dirs;
33
Kenny Root35ab3ad2011-02-02 16:42:14 -080034int install(const char *pkgname, uid_t uid, gid_t gid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035{
36 char pkgdir[PKG_PATH_MAX];
Kenny Root9bbd70a2012-09-10 11:13:36 -070037 char libsymlink[PKG_PATH_MAX];
38 char applibdir[PKG_PATH_MAX];
Kenny Roota3e90792012-10-18 10:58:36 -070039 struct stat libStat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
Steve Block3762c312012-01-06 19:20:56 +000042 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 }
Oscar Montemayora8529f62009-11-18 10:14:20 -080045
Kenny Root86c95842011-03-31 13:16:12 -070046 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
Steve Block3762c312012-01-06 19:20:56 +000047 ALOGE("cannot create package path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080048 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070049 }
50
Kenny Root9bbd70a2012-09-10 11:13:36 -070051 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
52 ALOGE("cannot create package lib symlink origin path\n");
53 return -1;
54 }
55
56 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
57 ALOGE("cannot create package lib symlink dest path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080058 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
David 'Digit' Turner0dd50e62010-02-09 19:02:38 -080061 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000062 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root9bbd70a2012-09-10 11:13:36 -070063 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070065 if (chmod(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000066 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070067 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070068 return -1;
Nick Kralevichf68327e2011-04-14 16:20:03 -070069 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050070
Kenny Roota3e90792012-10-18 10:58:36 -070071 if (lstat(libsymlink, &libStat) < 0) {
72 if (errno != ENOENT) {
73 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
74 return -1;
75 }
76 } else {
77 if (S_ISDIR(libStat.st_mode)) {
78 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
79 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
80 return -1;
81 }
82 } else if (S_ISLNK(libStat.st_mode)) {
83 if (unlink(libsymlink) < 0) {
84 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
85 return -1;
86 }
87 }
88 }
89
Kenny Root9bbd70a2012-09-10 11:13:36 -070090 if (symlink(applibdir, libsymlink) < 0) {
91 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
92 strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070094 return -1;
Kenny Root4503cf62012-06-14 13:05:18 -070095 }
Kenny Root33ef4ee2012-06-18 10:26:36 -070096
97#ifdef HAVE_SELINUX
98 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -040099 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root9bbd70a2012-09-10 11:13:36 -0700100 unlink(libsymlink);
Kenny Root33ef4ee2012-06-18 10:26:36 -0700101 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -0700102 return -1;
Kenny Root33ef4ee2012-06-18 10:26:36 -0700103 }
104#endif
105
Kenny Root9bbd70a2012-09-10 11:13:36 -0700106 if (chown(pkgdir, uid, gid) < 0) {
107 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
108 unlink(libsymlink);
109 unlink(pkgdir);
110 return -1;
111 }
112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 return 0;
114}
115
Amith Yamasani0b285492011-04-14 17:35:23 -0700116int uninstall(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117{
118 char pkgdir[PKG_PATH_MAX];
119
Amith Yamasani0b285492011-04-14 17:35:23 -0700120 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800121 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
Amith Yamasani0b285492011-04-14 17:35:23 -0700123 /* delete contents AND directory, no exceptions */
124 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125}
126
Kenny Root35ab3ad2011-02-02 16:42:14 -0800127int renamepkg(const char *oldpkgname, const char *newpkgname)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800128{
129 char oldpkgdir[PKG_PATH_MAX];
130 char newpkgdir[PKG_PATH_MAX];
131
Kenny Root86c95842011-03-31 13:16:12 -0700132 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800133 return -1;
Kenny Root86c95842011-03-31 13:16:12 -0700134 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800135 return -1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800136
137 if (rename(oldpkgdir, newpkgdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000138 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800139 return -errno;
140 }
141 return 0;
142}
143
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700144int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
145{
146 char pkgdir[PKG_PATH_MAX];
147 struct stat s;
148 int rc = 0;
149
150 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
151 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
152 return -1;
153 }
154
155 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
156 ALOGE("cannot create package path\n");
157 return -1;
158 }
159
160 if (stat(pkgdir, &s) < 0) return -1;
161
162 if (s.st_uid != 0 || s.st_gid != 0) {
Kenny Roota3e90792012-10-18 10:58:36 -0700163 ALOGE("fixing uid of non-root pkg: %s %lu %lu\n", pkgdir, s.st_uid, s.st_gid);
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700164 return -1;
165 }
166
167 if (chmod(pkgdir, 0751) < 0) {
168 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
169 unlink(pkgdir);
170 return -errno;
171 }
172 if (chown(pkgdir, uid, gid) < 0) {
173 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
174 unlink(pkgdir);
175 return -errno;
176 }
177
178 return 0;
179}
180
Amith Yamasani0b285492011-04-14 17:35:23 -0700181int delete_user_data(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182{
183 char pkgdir[PKG_PATH_MAX];
184
Amith Yamasani0b285492011-04-14 17:35:23 -0700185 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800186 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Kenny Root9157d3f2012-10-22 15:19:16 -0700188 /* delete contents, excluding "lib", but not the directory itself */
189 return delete_dir_contents(pkgdir, 0, "lib");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190}
191
Amith Yamasani0b285492011-04-14 17:35:23 -0700192int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
193{
194 char pkgdir[PKG_PATH_MAX];
Kenny Roota3e90792012-10-18 10:58:36 -0700195 char applibdir[PKG_PATH_MAX];
196 char libsymlink[PKG_PATH_MAX];
197 struct stat libStat;
Amith Yamasani0b285492011-04-14 17:35:23 -0700198
199 // Create the data dir for the package
200 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
201 return -1;
202 }
Kenny Roota3e90792012-10-18 10:58:36 -0700203 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, persona)) {
204 ALOGE("cannot create package lib symlink origin path\n");
205 return -1;
206 }
207 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
208 ALOGE("cannot create package lib symlink dest path\n");
209 return -1;
210 }
211
Amith Yamasani0b285492011-04-14 17:35:23 -0700212 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000213 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700214 return -errno;
215 }
Amith Yamasani794d62f2012-08-24 12:58:27 -0700216 if (chmod(pkgdir, 0751) < 0) {
217 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
218 unlink(pkgdir);
219 return -errno;
220 }
Kenny Roota3e90792012-10-18 10:58:36 -0700221
222 if (lstat(libsymlink, &libStat) < 0) {
223 if (errno != ENOENT) {
224 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
225 unlink(pkgdir);
226 return -1;
227 }
228 } else {
229 if (S_ISDIR(libStat.st_mode)) {
230 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
231 ALOGE("couldn't delete lib directory during install for non-primary: %s",
232 libsymlink);
233 unlink(pkgdir);
234 return -1;
235 }
236 } else if (S_ISLNK(libStat.st_mode)) {
237 if (unlink(libsymlink) < 0) {
238 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
239 libsymlink);
240 unlink(pkgdir);
241 return -1;
242 }
243 }
244 }
245
246 if (symlink(applibdir, libsymlink) < 0) {
247 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
248 applibdir, strerror(errno));
249 unlink(pkgdir);
250 return -1;
251 }
252
Amith Yamasani0b285492011-04-14 17:35:23 -0700253 if (chown(pkgdir, uid, uid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000254 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Roota3e90792012-10-18 10:58:36 -0700255 unlink(libsymlink);
Amith Yamasani0b285492011-04-14 17:35:23 -0700256 unlink(pkgdir);
257 return -errno;
258 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500259
260#ifdef HAVE_SELINUX
261 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -0400262 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Roota3e90792012-10-18 10:58:36 -0700263 unlink(libsymlink);
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500264 unlink(pkgdir);
265 return -errno;
266 }
267#endif
268
Amith Yamasani0b285492011-04-14 17:35:23 -0700269 return 0;
270}
271
272int delete_persona(uid_t persona)
273{
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700274 char data_path[PKG_PATH_MAX];
275 if (create_persona_path(data_path, persona)) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700276 return -1;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700277 }
278 if (delete_dir_contents(data_path, 1, NULL)) {
279 return -1;
280 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700281
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700282 char media_path[PATH_MAX];
283 if (create_persona_media_path(media_path, (userid_t) persona) == -1) {
284 return -1;
285 }
286 if (delete_dir_contents(media_path, 1, NULL) == -1) {
287 return -1;
288 }
289
290 return 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700291}
292
Amith Yamasani742a6712011-05-04 14:49:28 -0700293int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy)
294{
295 char src_data_dir[PKG_PATH_MAX];
296 char pkg_path[PKG_PATH_MAX];
297 DIR *d;
298 struct dirent *de;
299 struct stat s;
300 uid_t uid;
301
302 if (create_persona_path(src_data_dir, src_persona)) {
303 return -1;
304 }
305
306 d = opendir(src_data_dir);
307 if (d != NULL) {
308 while ((de = readdir(d))) {
309 const char *name = de->d_name;
310
311 if (de->d_type == DT_DIR) {
312 int subfd;
313 /* always skip "." and ".." */
314 if (name[0] == '.') {
315 if (name[1] == 0) continue;
316 if ((name[1] == '.') && (name[2] == 0)) continue;
317 }
318 /* Create the full path to the package's data dir */
319 create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona);
320 /* Get the file stat */
321 if (stat(pkg_path, &s) < 0) continue;
322 /* Get the uid of the package */
Kenny Roota3e90792012-10-18 10:58:36 -0700323 ALOGI("Adding datadir for uid = %lu\n", s.st_uid);
Amith Yamasani742a6712011-05-04 14:49:28 -0700324 uid = (uid_t) s.st_uid % PER_USER_RANGE;
325 /* Create the directory for the target */
326 make_user_data(name, uid + target_persona * PER_USER_RANGE,
327 target_persona);
328 }
329 }
330 closedir(d);
331 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700332
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700333 if (ensure_media_user_dirs((userid_t) target_persona) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700334 return -1;
335 }
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700336
Amith Yamasani742a6712011-05-04 14:49:28 -0700337 return 0;
338}
339
Amith Yamasani54289b82012-10-01 10:39:14 -0700340int delete_cache(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341{
342 char cachedir[PKG_PATH_MAX];
343
Amith Yamasani54289b82012-10-01 10:39:14 -0700344 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800345 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346
347 /* delete contents, not the directory, no exceptions */
348 return delete_dir_contents(cachedir, 0, 0);
349}
350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351/* Try to ensure free_size bytes of storage are available.
352 * Returns 0 on success.
353 * This is rather simple-minded because doing a full LRU would
354 * be potentially memory-intensive, and without atime it would
355 * also require that apps constantly modify file metadata even
356 * when just reading from the cache, which is pretty awful.
357 */
Kenny Root3e319a92010-09-07 13:58:28 -0700358int free_cache(int64_t free_size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359{
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700360 cache_t* cache;
361 int64_t avail;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 DIR *d;
363 struct dirent *de;
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700364 char tmpdir[PATH_MAX];
365 char *dirpos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700367 avail = data_disk_free();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 if (avail < 0) return -1;
369
Steve Block6215d3f2012-01-04 20:05:49 +0000370 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 if (avail >= free_size) return 0;
372
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700373 cache = start_cache_collection();
374
375 // Collect cache files for primary user.
376 if (create_persona_path(tmpdir, 0) == 0) {
377 //ALOGI("adding cache files from %s\n", tmpdir);
378 add_cache_files(cache, tmpdir, "cache");
Kenny Rootad757e92011-11-29 15:54:55 -0800379 }
380
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700381 // Search for other users and add any cache files from them.
382 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
383 SECONDARY_USER_PREFIX);
384 dirpos = tmpdir + strlen(tmpdir);
385 d = opendir(tmpdir);
386 if (d != NULL) {
387 while ((de = readdir(d))) {
388 if (de->d_type == DT_DIR) {
389 const char *name = de->d_name;
390 /* always skip "." and ".." */
391 if (name[0] == '.') {
392 if (name[1] == 0) continue;
393 if ((name[1] == '.') && (name[2] == 0)) continue;
394 }
395 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
396 strcpy(dirpos, name);
397 //ALOGI("adding cache files from %s\n", tmpdir);
398 add_cache_files(cache, tmpdir, "cache");
399 } else {
400 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
401 }
402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700404 closedir(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
Oscar Montemayora8529f62009-11-18 10:14:20 -0800406
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700407 // Collect cache files on external storage for all users (if it is mounted as part
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700408 // of the internal storage).
409 strcpy(tmpdir, android_media_dir.path);
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700410 dirpos = tmpdir + strlen(tmpdir);
411 d = opendir(tmpdir);
412 if (d != NULL) {
413 while ((de = readdir(d))) {
414 if (de->d_type == DT_DIR) {
415 const char *name = de->d_name;
416 /* skip any dir that doesn't start with a number, so not a user */
417 if (name[0] < '0' || name[0] > '9') {
418 continue;
419 }
420 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
421 strcpy(dirpos, name);
422 if (lookup_media_dir(tmpdir, "Android") == 0
423 && lookup_media_dir(tmpdir, "data") == 0) {
424 //ALOGI("adding cache files from %s\n", tmpdir);
425 add_cache_files(cache, tmpdir, "cache");
426 }
427 } else {
428 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
429 }
430 }
431 }
432 closedir(d);
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700433 }
434
435 clear_cache_files(cache, free_size);
436 finish_cache_collection(cache);
437
438 return data_disk_free() >= free_size ? 0 : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439}
440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441int move_dex(const char *src, const char *dst)
442{
443 char src_dex[PKG_PATH_MAX];
444 char dst_dex[PKG_PATH_MAX];
445
Kenny Root86c95842011-03-31 13:16:12 -0700446 if (validate_apk_path(src)) return -1;
447 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448
449 if (create_cache_path(src_dex, src)) return -1;
450 if (create_cache_path(dst_dex, dst)) return -1;
451
Steve Block71f2cf12011-10-20 11:56:00 +0100452 ALOGV("move %s -> %s\n", src_dex, dst_dex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 if (rename(src_dex, dst_dex) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000454 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 return -1;
456 } else {
457 return 0;
458 }
459}
460
461int rm_dex(const char *path)
462{
463 char dex_path[PKG_PATH_MAX];
464
Kenny Root86c95842011-03-31 13:16:12 -0700465 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 if (create_cache_path(dex_path, path)) return -1;
467
Steve Block71f2cf12011-10-20 11:56:00 +0100468 ALOGV("unlink %s\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 if (unlink(dex_path) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000470 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 return -1;
472 } else {
473 return 0;
474 }
475}
476
Dianne Hackborn0c380492012-08-20 17:23:30 -0700477int get_size(const char *pkgname, int persona, const char *apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700478 const char *fwdlock_apkpath, const char *asecpath,
479 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
480 int64_t* _asecsize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481{
482 DIR *d;
483 int dfd;
484 struct dirent *de;
485 struct stat s;
486 char path[PKG_PATH_MAX];
487
Kenny Root3e319a92010-09-07 13:58:28 -0700488 int64_t codesize = 0;
489 int64_t datasize = 0;
490 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700491 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492
493 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800494 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 */
Kenny Root86c95842011-03-31 13:16:12 -0700496 if (validate_system_app_path(apkpath) &&
497 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 if (stat(apkpath, &s) == 0) {
499 codesize += stat_size(&s);
500 }
501 }
502 /* count the forward locked apk as code if it is given
503 */
504 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
505 if (stat(fwdlock_apkpath, &s) == 0) {
506 codesize += stat_size(&s);
507 }
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 /* count the cached dexfile as code */
510 if (!create_cache_path(path, apkpath)) {
511 if (stat(path, &s) == 0) {
512 codesize += stat_size(&s);
513 }
514 }
515
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700516 /* add in size of any libraries */
517 if (!create_pkg_path_in_dir(path, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
518 d = opendir(path);
519 if (d != NULL) {
520 dfd = dirfd(d);
521 codesize += calculate_dir_size(dfd);
522 closedir(d);
523 }
524 }
525
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700526 /* compute asec size if it is given
527 */
528 if (asecpath != NULL && asecpath[0] != '!') {
529 if (stat(asecpath, &s) == 0) {
530 asecsize += stat_size(&s);
531 }
532 }
533
Dianne Hackborn0c380492012-08-20 17:23:30 -0700534 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800535 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
537
538 d = opendir(path);
539 if (d == NULL) {
540 goto done;
541 }
542 dfd = dirfd(d);
543
Kenny Root86c95842011-03-31 13:16:12 -0700544 /* most stuff in the pkgdir is data, except for the "cache"
545 * directory and below, which is cache, and the "lib" directory
546 * and below, which is code...
547 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 while ((de = readdir(d))) {
549 const char *name = de->d_name;
550
551 if (de->d_type == DT_DIR) {
552 int subfd;
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700553 int64_t statsize = 0;
554 int64_t dirsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 /* always skip "." and ".." */
556 if (name[0] == '.') {
557 if (name[1] == 0) continue;
558 if ((name[1] == '.') && (name[2] == 0)) continue;
559 }
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700560 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
561 statsize = stat_size(&s);
562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
564 if (subfd >= 0) {
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700565 dirsize = calculate_dir_size(subfd);
566 }
567 if(!strcmp(name,"lib")) {
568 codesize += dirsize + statsize;
569 } else if(!strcmp(name,"cache")) {
570 cachesize += dirsize + statsize;
571 } else {
572 datasize += dirsize + statsize;
573 }
574 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
575 // This is the symbolic link to the application's library
576 // code. We'll count this as code instead of data, since
577 // it is not something that the app creates.
578 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
579 codesize += stat_size(&s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581 } else {
582 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
583 datasize += stat_size(&s);
584 }
585 }
586 }
587 closedir(d);
588done:
589 *_codesize = codesize;
590 *_datasize = datasize;
591 *_cachesize = cachesize;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700592 *_asecsize = asecsize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 return 0;
594}
595
596
597/* a simpler version of dexOptGenerateCacheFileName() */
598int create_cache_path(char path[PKG_PATH_MAX], const char *src)
599{
600 char *tmp;
601 int srclen;
602 int dstlen;
603
604 srclen = strlen(src);
605
606 /* demand that we are an absolute path */
607 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
608 return -1;
609 }
610
611 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
612 return -1;
613 }
614
615 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
616 strlen(DALVIK_CACHE_POSTFIX) + 1;
617
618 if (dstlen > PKG_PATH_MAX) {
619 return -1;
620 }
621
622 sprintf(path,"%s%s%s",
623 DALVIK_CACHE_PREFIX,
624 src + 1, /* skip the leading / */
625 DALVIK_CACHE_POSTFIX);
626
627 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
628 if (*tmp == '/') {
629 *tmp = '@';
630 }
631 }
632
633 return 0;
634}
635
636static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
637 const char* dexopt_flags)
638{
639 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
640 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
641 char zip_num[MAX_INT_LEN];
642 char odex_num[MAX_INT_LEN];
643
644 sprintf(zip_num, "%d", zip_fd);
645 sprintf(odex_num, "%d", odex_fd);
646
647 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
648 dexopt_flags, (char*) NULL);
Steve Block3762c312012-01-06 19:20:56 +0000649 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650}
651
652static int wait_dexopt(pid_t pid, const char* apk_path)
653{
654 int status;
655 pid_t got_pid;
656
657 /*
658 * Wait for the optimization process to finish.
659 */
660 while (1) {
661 got_pid = waitpid(pid, &status, 0);
662 if (got_pid == -1 && errno == EINTR) {
663 printf("waitpid interrupted, retrying\n");
664 } else {
665 break;
666 }
667 }
668 if (got_pid != pid) {
Steve Block8564c8d2012-01-05 23:22:43 +0000669 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 (int) pid, (int) got_pid, strerror(errno));
671 return 1;
672 }
673
674 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100675 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 return 0;
677 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000678 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 apk_path, status);
680 return status; /* always nonzero */
681 }
682}
683
684int dexopt(const char *apk_path, uid_t uid, int is_public)
685{
686 struct utimbuf ut;
687 struct stat apk_stat, dex_stat;
688 char dex_path[PKG_PATH_MAX];
689 char dexopt_flags[PROPERTY_VALUE_MAX];
690 char *end;
691 int res, zip_fd=-1, odex_fd=-1;
692
693 /* Before anything else: is there a .odex file? If so, we have
694 * pre-optimized the apk and there is nothing to do here.
695 */
696 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
697 return -1;
698 }
699
700 /* platform-specific flags affecting optimization and verification */
701 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
702
703 strcpy(dex_path, apk_path);
704 end = strrchr(dex_path, '.');
705 if (end != NULL) {
706 strcpy(end, ".odex");
707 if (stat(dex_path, &dex_stat) == 0) {
708 return 0;
709 }
710 }
711
712 if (create_cache_path(dex_path, apk_path)) {
713 return -1;
714 }
715
716 memset(&apk_stat, 0, sizeof(apk_stat));
717 stat(apk_path, &apk_stat);
718
719 zip_fd = open(apk_path, O_RDONLY, 0);
720 if (zip_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000721 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 return -1;
723 }
724
725 unlink(dex_path);
726 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
727 if (odex_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000728 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 goto fail;
730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 if (fchmod(odex_fd,
732 S_IRUSR|S_IWUSR|S_IRGRP |
733 (is_public ? S_IROTH : 0)) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000734 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 goto fail;
736 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700737 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
738 ALOGE("dexopt cannot chown '%s'\n", dex_path);
739 goto fail;
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741
Steve Block71f2cf12011-10-20 11:56:00 +0100742 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743
744 pid_t pid;
745 pid = fork();
746 if (pid == 0) {
747 /* child -- drop privileges before continuing */
748 if (setgid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000749 ALOGE("setgid(%d) failed during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 exit(64);
751 }
752 if (setuid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000753 ALOGE("setuid(%d) during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 exit(65);
755 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700756 // drop capabilities
757 struct __user_cap_header_struct capheader;
758 struct __user_cap_data_struct capdata[2];
759 memset(&capheader, 0, sizeof(capheader));
760 memset(&capdata, 0, sizeof(capdata));
761 capheader.version = _LINUX_CAPABILITY_VERSION_3;
762 if (capset(&capheader, &capdata[0]) < 0) {
763 ALOGE("capset failed: %s\n", strerror(errno));
764 exit(66);
765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000767 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
Nick Kralevich812b19a2012-08-31 16:08:06 -0700768 exit(67);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770
771 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
Nick Kralevich812b19a2012-08-31 16:08:06 -0700772 exit(68); /* only get here on exec failure */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 } else {
774 res = wait_dexopt(pid, apk_path);
775 if (res != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000776 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 goto fail;
778 }
779 }
780
781 ut.actime = apk_stat.st_atime;
782 ut.modtime = apk_stat.st_mtime;
783 utime(dex_path, &ut);
784
785 close(odex_fd);
786 close(zip_fd);
787 return 0;
788
789fail:
790 if (odex_fd >= 0) {
791 close(odex_fd);
792 unlink(dex_path);
793 }
794 if (zip_fd >= 0) {
795 close(zip_fd);
796 }
797 return -1;
798}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800799
Dianne Hackbornc1552392010-03-03 16:19:01 -0800800void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
801 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800802{
803 while (path[basepos] != 0) {
804 if (path[basepos] == '/') {
805 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800806 if (lstat(path, statbuf) < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100807 ALOGV("Making directory: %s\n", path);
Dianne Hackbornc1552392010-03-03 16:19:01 -0800808 if (mkdir(path, mode) == 0) {
809 chown(path, uid, gid);
810 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000811 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
Dianne Hackbornc1552392010-03-03 16:19:01 -0800812 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800813 }
814 path[basepos] = '/';
815 basepos++;
816 }
817 basepos++;
818 }
819}
820
Dianne Hackbornc1552392010-03-03 16:19:01 -0800821int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
822 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800823{
824 DIR *d;
825 struct dirent *de;
826 int res;
827
828 int srcend = strlen(srcpath);
829 int dstend = strlen(dstpath);
830
831 if (lstat(srcpath, statbuf) < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000832 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800833 return 1;
834 }
835
836 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800837 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
838 dstuid, dstgid, statbuf);
Steve Block71f2cf12011-10-20 11:56:00 +0100839 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800840 if (rename(srcpath, dstpath) >= 0) {
841 if (chown(dstpath, dstuid, dstgid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000842 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800843 unlink(dstpath);
844 return 1;
845 }
846 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000847 ALOGW("Unable to rename %s to %s: %s\n",
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800848 srcpath, dstpath, strerror(errno));
849 return 1;
850 }
851 return 0;
852 }
853
854 d = opendir(srcpath);
855 if (d == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000856 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800857 return 1;
858 }
859
860 res = 0;
861
862 while ((de = readdir(d))) {
863 const char *name = de->d_name;
864 /* always skip "." and ".." */
865 if (name[0] == '.') {
866 if (name[1] == 0) continue;
867 if ((name[1] == '.') && (name[2] == 0)) continue;
868 }
869
870 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000871 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800872 continue;
873 }
874
875 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000876 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800877 continue;
878 }
879
880 srcpath[srcend] = dstpath[dstend] = '/';
881 strcpy(srcpath+srcend+1, name);
882 strcpy(dstpath+dstend+1, name);
883
Dianne Hackbornc1552392010-03-03 16:19:01 -0800884 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800885 res = 1;
886 }
887
888 // Note: we will be leaving empty directories behind in srcpath,
889 // but that is okay, the package manager will be erasing all of the
890 // data associated with .apks that disappear.
891
892 srcpath[srcend] = dstpath[dstend] = 0;
893 }
894
895 closedir(d);
896 return res;
897}
898
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800899int movefiles()
900{
901 DIR *d;
902 int dfd, subfd;
903 struct dirent *de;
904 struct stat s;
905 char buf[PKG_PATH_MAX+1];
906 int bufp, bufe, bufi, readlen;
907
908 char srcpkg[PKG_NAME_MAX];
909 char dstpkg[PKG_NAME_MAX];
910 char srcpath[PKG_PATH_MAX];
911 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800912 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800913 int hasspace;
914
915 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
916 if (d == NULL) {
917 goto done;
918 }
919 dfd = dirfd(d);
920
921 /* Iterate through all files in the directory, executing the
922 * file movements requested there-in.
923 */
924 while ((de = readdir(d))) {
925 const char *name = de->d_name;
926
927 if (de->d_type == DT_DIR) {
928 continue;
929 } else {
930 subfd = openat(dfd, name, O_RDONLY);
931 if (subfd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000932 ALOGW("Unable to open update commands at %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800933 UPDATE_COMMANDS_DIR_PREFIX, name);
934 continue;
935 }
936
937 bufp = 0;
938 bufe = 0;
939 buf[PKG_PATH_MAX] = 0;
940 srcpkg[0] = dstpkg[0] = 0;
941 while (1) {
942 bufi = bufp;
943 while (bufi < bufe && buf[bufi] != '\n') {
944 bufi++;
945 }
946 if (bufi < bufe) {
947 buf[bufi] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100948 ALOGV("Processing line: %s\n", buf+bufp);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800949 hasspace = 0;
950 while (bufp < bufi && isspace(buf[bufp])) {
951 hasspace = 1;
952 bufp++;
953 }
954 if (buf[bufp] == '#' || bufp == bufi) {
955 // skip comments and empty lines.
956 } else if (hasspace) {
957 if (dstpkg[0] == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000958 ALOGW("Path before package line in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800959 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
960 } else if (srcpkg[0] == 0) {
961 // Skip -- source package no longer exists.
962 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100963 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
Kenny Root86c95842011-03-31 13:16:12 -0700964 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
965 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800966 movefileordir(srcpath, dstpath,
967 strlen(dstpath)-strlen(buf+bufp),
968 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800969 }
970 }
971 } else {
972 char* div = strchr(buf+bufp, ':');
973 if (div == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000974 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800975 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
976 } else {
977 *div = 0;
978 div++;
979 if (strlen(buf+bufp) < PKG_NAME_MAX) {
980 strcpy(dstpkg, buf+bufp);
981 } else {
982 srcpkg[0] = dstpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000983 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800984 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
985 }
986 if (strlen(div) < PKG_NAME_MAX) {
987 strcpy(srcpkg, div);
988 } else {
989 srcpkg[0] = dstpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000990 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800991 UPDATE_COMMANDS_DIR_PREFIX, name, div);
992 }
993 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700994 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800995 if (lstat(srcpath, &s) < 0) {
996 // Package no longer exists -- skip.
997 srcpkg[0] = 0;
998 }
999 } else {
1000 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001001 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001002 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1003 }
1004 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -07001005 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001006 if (lstat(dstpath, &s) == 0) {
1007 dstuid = s.st_uid;
1008 dstgid = s.st_gid;
1009 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -08001010 // Destination package doesn't
1011 // exist... due to original-package,
1012 // this is normal, so don't be
1013 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001014 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001015 }
1016 } else {
1017 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001018 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001019 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1020 }
1021 }
Steve Block71f2cf12011-10-20 11:56:00 +01001022 ALOGV("Transfering from %s to %s: uid=%d\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001023 srcpkg, dstpkg, dstuid);
1024 }
1025 }
1026 }
1027 bufp = bufi+1;
1028 } else {
1029 if (bufp == 0) {
1030 if (bufp < bufe) {
Steve Block8564c8d2012-01-05 23:22:43 +00001031 ALOGW("Line too long in %s%s, skipping: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001032 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1033 }
1034 } else if (bufp < bufe) {
1035 memcpy(buf, buf+bufp, bufe-bufp);
1036 bufe -= bufp;
1037 bufp = 0;
1038 }
1039 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1040 if (readlen < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001041 ALOGW("Failure reading update commands in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001042 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1043 break;
1044 } else if (readlen == 0) {
1045 break;
1046 }
1047 bufe += readlen;
1048 buf[bufe] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +01001049 ALOGV("Read buf: %s\n", buf);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001050 }
1051 }
1052 close(subfd);
1053 }
1054 }
1055 closedir(d);
1056done:
1057 return 0;
1058}
Kenny Root6a6b0072010-10-07 16:46:10 -07001059
Kenny Roota3e90792012-10-18 10:58:36 -07001060int linklib(const char* pkgname, const char* asecLibDir, int userId)
Kenny Root6a6b0072010-10-07 16:46:10 -07001061{
Kenny Roota3e90792012-10-18 10:58:36 -07001062 char pkgdir[PKG_PATH_MAX];
1063 char libsymlink[PKG_PATH_MAX];
Kenny Root6a6b0072010-10-07 16:46:10 -07001064 struct stat s, libStat;
1065 int rc = 0;
1066
Kenny Roota3e90792012-10-18 10:58:36 -07001067 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1068 ALOGE("cannot create package path\n");
1069 return -1;
1070 }
1071 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1072 ALOGE("cannot create package lib symlink origin path\n");
Kenny Root0332d1c2010-10-21 16:14:06 -07001073 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -07001074 }
1075
Kenny Roota3e90792012-10-18 10:58:36 -07001076 if (stat(pkgdir, &s) < 0) return -1;
1077
1078 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1079 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
Kenny Root0332d1c2010-10-21 16:14:06 -07001080 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -07001081 }
1082
Kenny Roota3e90792012-10-18 10:58:36 -07001083 if (chmod(pkgdir, 0700) < 0) {
1084 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001085 rc = -1;
1086 goto out;
1087 }
1088
Kenny Roota3e90792012-10-18 10:58:36 -07001089 if (lstat(libsymlink, &libStat) < 0) {
1090 if (errno != ENOENT) {
1091 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001092 rc = -1;
1093 goto out;
1094 }
Kenny Roota3e90792012-10-18 10:58:36 -07001095 } else {
1096 if (S_ISDIR(libStat.st_mode)) {
1097 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1098 rc = -1;
1099 goto out;
1100 }
1101 } else if (S_ISLNK(libStat.st_mode)) {
1102 if (unlink(libsymlink) < 0) {
1103 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1104 rc = -1;
1105 goto out;
1106 }
Kenny Root6a6b0072010-10-07 16:46:10 -07001107 }
1108 }
1109
Kenny Roota3e90792012-10-18 10:58:36 -07001110 if (symlink(asecLibDir, libsymlink) < 0) {
1111 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1112 strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001113 rc = -errno;
1114 goto out;
1115 }
1116
1117out:
Kenny Roota3e90792012-10-18 10:58:36 -07001118 if (chmod(pkgdir, s.st_mode) < 0) {
1119 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
Dianne Hackbornd0c5f512012-06-07 16:53:59 -07001120 rc = -errno;
Kenny Root6a6b0072010-10-07 16:46:10 -07001121 }
1122
Kenny Roota3e90792012-10-18 10:58:36 -07001123 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1124 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001125 return -errno;
1126 }
1127
1128 return rc;
1129}