blob: 4d49c8adf36ff89624296d87c2ad7263bb6fbfe6 [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];
Kenny Roota3e90792012-10-18 10:58:36 -070036 struct stat libStat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
Steve Block3762c312012-01-06 19:20:56 +000039 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 }
Oscar Montemayora8529f62009-11-18 10:14:20 -080042
Kenny Root86c95842011-03-31 13:16:12 -070043 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
Steve Block3762c312012-01-06 19:20:56 +000044 ALOGE("cannot create package path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080045 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070046 }
47
Kenny Root9bbd70a2012-09-10 11:13:36 -070048 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
49 ALOGE("cannot create package lib symlink origin path\n");
50 return -1;
51 }
52
53 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
54 ALOGE("cannot create package lib symlink dest path\n");
Kenny Root35ab3ad2011-02-02 16:42:14 -080055 return -1;
Kenny Root86c95842011-03-31 13:16:12 -070056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
David 'Digit' Turner0dd50e62010-02-09 19:02:38 -080058 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000059 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root9bbd70a2012-09-10 11:13:36 -070060 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 }
Nick Kralevichf68327e2011-04-14 16:20:03 -070062 if (chmod(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000063 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
Nick Kralevichf68327e2011-04-14 16:20:03 -070064 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070065 return -1;
Nick Kralevichf68327e2011-04-14 16:20:03 -070066 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -050067
Kenny Roota3e90792012-10-18 10:58:36 -070068 if (lstat(libsymlink, &libStat) < 0) {
69 if (errno != ENOENT) {
70 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
71 return -1;
72 }
73 } else {
74 if (S_ISDIR(libStat.st_mode)) {
75 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
76 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
77 return -1;
78 }
79 } else if (S_ISLNK(libStat.st_mode)) {
80 if (unlink(libsymlink) < 0) {
81 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
82 return -1;
83 }
84 }
85 }
86
Kenny Root9bbd70a2012-09-10 11:13:36 -070087 if (symlink(applibdir, libsymlink) < 0) {
88 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
89 strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070091 return -1;
Kenny Root4503cf62012-06-14 13:05:18 -070092 }
Kenny Root33ef4ee2012-06-18 10:26:36 -070093
Kenny Root36740042012-10-17 10:50:14 -070094 if (selinux_android_setfilecon(libsymlink, pkgname, AID_SYSTEM) < 0) {
Kenny Rooted3ce512012-10-17 10:05:10 -070095 ALOGE("cannot setfilecon dir '%s': %s\n", libsymlink, strerror(errno));
Kenny Root9bbd70a2012-09-10 11:13:36 -070096 unlink(libsymlink);
Kenny Root33ef4ee2012-06-18 10:26:36 -070097 unlink(pkgdir);
Kenny Root9bbd70a2012-09-10 11:13:36 -070098 return -1;
Kenny Root33ef4ee2012-06-18 10:26:36 -070099 }
Kenny Root33ef4ee2012-06-18 10:26:36 -0700100
Kenny Root57c63d82012-10-17 09:50:35 -0700101 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
102 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root36740042012-10-17 10:50:14 -0700103 unlink(libsymlink);
Kenny Root57c63d82012-10-17 09:50:35 -0700104 unlink(pkgdir);
105 return -errno;
106 }
107
Kenny Root9bbd70a2012-09-10 11:13:36 -0700108 if (chown(pkgdir, uid, gid) < 0) {
109 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
110 unlink(libsymlink);
111 unlink(pkgdir);
112 return -1;
113 }
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 return 0;
116}
117
Amith Yamasani0b285492011-04-14 17:35:23 -0700118int uninstall(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119{
120 char pkgdir[PKG_PATH_MAX];
121
Amith Yamasani0b285492011-04-14 17:35:23 -0700122 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800123 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
Amith Yamasani0b285492011-04-14 17:35:23 -0700125 /* delete contents AND directory, no exceptions */
126 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127}
128
Kenny Root35ab3ad2011-02-02 16:42:14 -0800129int renamepkg(const char *oldpkgname, const char *newpkgname)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800130{
131 char oldpkgdir[PKG_PATH_MAX];
132 char newpkgdir[PKG_PATH_MAX];
133
Kenny Root86c95842011-03-31 13:16:12 -0700134 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800135 return -1;
Kenny Root86c95842011-03-31 13:16:12 -0700136 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800137 return -1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800138
139 if (rename(oldpkgdir, newpkgdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000140 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800141 return -errno;
142 }
143 return 0;
144}
145
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700146int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
147{
148 char pkgdir[PKG_PATH_MAX];
149 struct stat s;
150 int rc = 0;
151
152 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
153 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
154 return -1;
155 }
156
157 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
158 ALOGE("cannot create package path\n");
159 return -1;
160 }
161
162 if (stat(pkgdir, &s) < 0) return -1;
163
164 if (s.st_uid != 0 || s.st_gid != 0) {
Kenny Roota3e90792012-10-18 10:58:36 -0700165 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 -0700166 return -1;
167 }
168
169 if (chmod(pkgdir, 0751) < 0) {
170 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
171 unlink(pkgdir);
172 return -errno;
173 }
174 if (chown(pkgdir, uid, gid) < 0) {
175 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
176 unlink(pkgdir);
177 return -errno;
178 }
179
180 return 0;
181}
182
Amith Yamasani0b285492011-04-14 17:35:23 -0700183int delete_user_data(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184{
185 char pkgdir[PKG_PATH_MAX];
186
Amith Yamasani0b285492011-04-14 17:35:23 -0700187 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800188 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
Kenny Roota3e90792012-10-18 10:58:36 -0700190 /* delete contents AND directory, no exceptions */
191 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192}
193
Amith Yamasani0b285492011-04-14 17:35:23 -0700194int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
195{
196 char pkgdir[PKG_PATH_MAX];
Kenny Roota3e90792012-10-18 10:58:36 -0700197 char applibdir[PKG_PATH_MAX];
198 char libsymlink[PKG_PATH_MAX];
199 struct stat libStat;
Amith Yamasani0b285492011-04-14 17:35:23 -0700200
201 // Create the data dir for the package
202 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
203 return -1;
204 }
Kenny Roota3e90792012-10-18 10:58:36 -0700205 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, persona)) {
206 ALOGE("cannot create package lib symlink origin path\n");
207 return -1;
208 }
209 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
210 ALOGE("cannot create package lib symlink dest path\n");
211 return -1;
212 }
213
Amith Yamasani0b285492011-04-14 17:35:23 -0700214 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000215 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700216 return -errno;
217 }
Amith Yamasani794d62f2012-08-24 12:58:27 -0700218 if (chmod(pkgdir, 0751) < 0) {
219 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
220 unlink(pkgdir);
221 return -errno;
222 }
Kenny Roota3e90792012-10-18 10:58:36 -0700223
224 if (lstat(libsymlink, &libStat) < 0) {
225 if (errno != ENOENT) {
226 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
227 unlink(pkgdir);
228 return -1;
229 }
230 } else {
231 if (S_ISDIR(libStat.st_mode)) {
232 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
233 ALOGE("couldn't delete lib directory during install for non-primary: %s",
234 libsymlink);
235 unlink(pkgdir);
236 return -1;
237 }
238 } else if (S_ISLNK(libStat.st_mode)) {
239 if (unlink(libsymlink) < 0) {
240 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
241 libsymlink);
242 unlink(pkgdir);
243 return -1;
244 }
245 }
246 }
247
248 if (symlink(applibdir, libsymlink) < 0) {
249 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
250 applibdir, strerror(errno));
251 unlink(pkgdir);
252 return -1;
253 }
254
Kenny Root9396f182012-10-19 17:16:41 -0700255 if (selinux_android_setfilecon(libsymlink, pkgname, AID_SYSTEM) < 0) {
256 ALOGE("cannot setfilecon dir '%s': %s\n", libsymlink, strerror(errno));
Kenny Roota3e90792012-10-18 10:58:36 -0700257 unlink(libsymlink);
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500258 unlink(pkgdir);
259 return -errno;
260 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500261
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500262 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
263 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Roota3e90792012-10-18 10:58:36 -0700264 unlink(libsymlink);
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500265 unlink(pkgdir);
266 return -errno;
267 }
Kenny Root9396f182012-10-19 17:16:41 -0700268
Kenny Rootc9a1aab2012-10-16 23:28:21 -0700269 if (chown(pkgdir, uid, uid) < 0) {
270 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root9396f182012-10-19 17:16:41 -0700271 unlink(libsymlink);
Kenny Rootc9a1aab2012-10-16 23:28:21 -0700272 unlink(pkgdir);
273 return -errno;
274 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500275
Amith Yamasani0b285492011-04-14 17:35:23 -0700276 return 0;
277}
278
279int delete_persona(uid_t persona)
280{
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700281 char data_path[PKG_PATH_MAX];
282 if (create_persona_path(data_path, persona)) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700283 return -1;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700284 }
285 if (delete_dir_contents(data_path, 1, NULL)) {
286 return -1;
287 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700288
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700289 char media_path[PATH_MAX];
290 if (create_persona_media_path(media_path, (userid_t) persona) == -1) {
291 return -1;
292 }
293 if (delete_dir_contents(media_path, 1, NULL) == -1) {
294 return -1;
295 }
296
297 return 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700298}
299
Amith Yamasani742a6712011-05-04 14:49:28 -0700300int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy)
301{
302 char src_data_dir[PKG_PATH_MAX];
303 char pkg_path[PKG_PATH_MAX];
304 DIR *d;
305 struct dirent *de;
306 struct stat s;
307 uid_t uid;
308
309 if (create_persona_path(src_data_dir, src_persona)) {
310 return -1;
311 }
312
313 d = opendir(src_data_dir);
314 if (d != NULL) {
315 while ((de = readdir(d))) {
316 const char *name = de->d_name;
317
318 if (de->d_type == DT_DIR) {
319 int subfd;
320 /* always skip "." and ".." */
321 if (name[0] == '.') {
322 if (name[1] == 0) continue;
323 if ((name[1] == '.') && (name[2] == 0)) continue;
324 }
325 /* Create the full path to the package's data dir */
326 create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona);
327 /* Get the file stat */
328 if (stat(pkg_path, &s) < 0) continue;
329 /* Get the uid of the package */
Kenny Roota3e90792012-10-18 10:58:36 -0700330 ALOGI("Adding datadir for uid = %lu\n", s.st_uid);
Amith Yamasani742a6712011-05-04 14:49:28 -0700331 uid = (uid_t) s.st_uid % PER_USER_RANGE;
332 /* Create the directory for the target */
333 make_user_data(name, uid + target_persona * PER_USER_RANGE,
334 target_persona);
335 }
336 }
337 closedir(d);
338 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700339
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700340 if (ensure_media_user_dirs((userid_t) target_persona) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700341 return -1;
342 }
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700343
Amith Yamasani742a6712011-05-04 14:49:28 -0700344 return 0;
345}
346
Amith Yamasani54289b82012-10-01 10:39:14 -0700347int delete_cache(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348{
349 char cachedir[PKG_PATH_MAX];
350
Amith Yamasani54289b82012-10-01 10:39:14 -0700351 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800352 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353
354 /* delete contents, not the directory, no exceptions */
355 return delete_dir_contents(cachedir, 0, 0);
356}
357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358/* Try to ensure free_size bytes of storage are available.
359 * Returns 0 on success.
360 * This is rather simple-minded because doing a full LRU would
361 * be potentially memory-intensive, and without atime it would
362 * also require that apps constantly modify file metadata even
363 * when just reading from the cache, which is pretty awful.
364 */
Kenny Root3e319a92010-09-07 13:58:28 -0700365int free_cache(int64_t free_size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366{
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700367 cache_t* cache;
368 int64_t avail;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 DIR *d;
370 struct dirent *de;
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700371 char tmpdir[PATH_MAX];
372 char *dirpos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700374 avail = data_disk_free();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 if (avail < 0) return -1;
376
Steve Block6215d3f2012-01-04 20:05:49 +0000377 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 if (avail >= free_size) return 0;
379
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700380 cache = start_cache_collection();
381
382 // Collect cache files for primary user.
383 if (create_persona_path(tmpdir, 0) == 0) {
384 //ALOGI("adding cache files from %s\n", tmpdir);
385 add_cache_files(cache, tmpdir, "cache");
Kenny Rootad757e92011-11-29 15:54:55 -0800386 }
387
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700388 // Search for other users and add any cache files from them.
389 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
390 SECONDARY_USER_PREFIX);
391 dirpos = tmpdir + strlen(tmpdir);
392 d = opendir(tmpdir);
393 if (d != NULL) {
394 while ((de = readdir(d))) {
395 if (de->d_type == DT_DIR) {
396 const char *name = de->d_name;
397 /* always skip "." and ".." */
398 if (name[0] == '.') {
399 if (name[1] == 0) continue;
400 if ((name[1] == '.') && (name[2] == 0)) continue;
401 }
402 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
403 strcpy(dirpos, name);
404 //ALOGI("adding cache files from %s\n", tmpdir);
405 add_cache_files(cache, tmpdir, "cache");
406 } else {
407 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
408 }
409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700411 closedir(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 }
Oscar Montemayora8529f62009-11-18 10:14:20 -0800413
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700414 // Collect cache files on external storage for all users (if it is mounted as part
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700415 // of the internal storage).
416 strcpy(tmpdir, android_media_dir.path);
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700417 dirpos = tmpdir + strlen(tmpdir);
418 d = opendir(tmpdir);
419 if (d != NULL) {
420 while ((de = readdir(d))) {
421 if (de->d_type == DT_DIR) {
422 const char *name = de->d_name;
423 /* skip any dir that doesn't start with a number, so not a user */
424 if (name[0] < '0' || name[0] > '9') {
425 continue;
426 }
427 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
428 strcpy(dirpos, name);
429 if (lookup_media_dir(tmpdir, "Android") == 0
430 && lookup_media_dir(tmpdir, "data") == 0) {
431 //ALOGI("adding cache files from %s\n", tmpdir);
432 add_cache_files(cache, tmpdir, "cache");
433 }
434 } else {
435 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
436 }
437 }
438 }
439 closedir(d);
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700440 }
441
442 clear_cache_files(cache, free_size);
443 finish_cache_collection(cache);
444
445 return data_disk_free() >= free_size ? 0 : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446}
447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448int move_dex(const char *src, const char *dst)
449{
450 char src_dex[PKG_PATH_MAX];
451 char dst_dex[PKG_PATH_MAX];
452
Kenny Root86c95842011-03-31 13:16:12 -0700453 if (validate_apk_path(src)) return -1;
454 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455
456 if (create_cache_path(src_dex, src)) return -1;
457 if (create_cache_path(dst_dex, dst)) return -1;
458
Steve Block71f2cf12011-10-20 11:56:00 +0100459 ALOGV("move %s -> %s\n", src_dex, dst_dex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 if (rename(src_dex, dst_dex) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000461 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 return -1;
463 } else {
464 return 0;
465 }
466}
467
468int rm_dex(const char *path)
469{
470 char dex_path[PKG_PATH_MAX];
471
Kenny Root86c95842011-03-31 13:16:12 -0700472 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 if (create_cache_path(dex_path, path)) return -1;
474
Steve Block71f2cf12011-10-20 11:56:00 +0100475 ALOGV("unlink %s\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 if (unlink(dex_path) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000477 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 return -1;
479 } else {
480 return 0;
481 }
482}
483
Dianne Hackborn0c380492012-08-20 17:23:30 -0700484int get_size(const char *pkgname, int persona, const char *apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700485 const char *fwdlock_apkpath, const char *asecpath,
486 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
487 int64_t* _asecsize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488{
489 DIR *d;
490 int dfd;
491 struct dirent *de;
492 struct stat s;
493 char path[PKG_PATH_MAX];
494
Kenny Root3e319a92010-09-07 13:58:28 -0700495 int64_t codesize = 0;
496 int64_t datasize = 0;
497 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700498 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499
500 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800501 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 */
Kenny Root86c95842011-03-31 13:16:12 -0700503 if (validate_system_app_path(apkpath) &&
504 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 if (stat(apkpath, &s) == 0) {
506 codesize += stat_size(&s);
507 }
508 }
509 /* count the forward locked apk as code if it is given
510 */
511 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
512 if (stat(fwdlock_apkpath, &s) == 0) {
513 codesize += stat_size(&s);
514 }
515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 /* count the cached dexfile as code */
517 if (!create_cache_path(path, apkpath)) {
518 if (stat(path, &s) == 0) {
519 codesize += stat_size(&s);
520 }
521 }
522
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700523 /* add in size of any libraries */
524 if (!create_pkg_path_in_dir(path, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
525 d = opendir(path);
526 if (d != NULL) {
527 dfd = dirfd(d);
528 codesize += calculate_dir_size(dfd);
529 closedir(d);
530 }
531 }
532
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700533 /* compute asec size if it is given
534 */
535 if (asecpath != NULL && asecpath[0] != '!') {
536 if (stat(asecpath, &s) == 0) {
537 asecsize += stat_size(&s);
538 }
539 }
540
Dianne Hackborn0c380492012-08-20 17:23:30 -0700541 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800542 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544
545 d = opendir(path);
546 if (d == NULL) {
547 goto done;
548 }
549 dfd = dirfd(d);
550
Kenny Root86c95842011-03-31 13:16:12 -0700551 /* most stuff in the pkgdir is data, except for the "cache"
552 * directory and below, which is cache, and the "lib" directory
553 * and below, which is code...
554 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 while ((de = readdir(d))) {
556 const char *name = de->d_name;
557
558 if (de->d_type == DT_DIR) {
559 int subfd;
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700560 int64_t statsize = 0;
561 int64_t dirsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 /* always skip "." and ".." */
563 if (name[0] == '.') {
564 if (name[1] == 0) continue;
565 if ((name[1] == '.') && (name[2] == 0)) continue;
566 }
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700567 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
568 statsize = stat_size(&s);
569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
571 if (subfd >= 0) {
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700572 dirsize = calculate_dir_size(subfd);
573 }
574 if(!strcmp(name,"lib")) {
575 codesize += dirsize + statsize;
576 } else if(!strcmp(name,"cache")) {
577 cachesize += dirsize + statsize;
578 } else {
579 datasize += dirsize + statsize;
580 }
581 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
582 // This is the symbolic link to the application's library
583 // code. We'll count this as code instead of data, since
584 // it is not something that the app creates.
585 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
586 codesize += stat_size(&s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 }
588 } else {
589 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
590 datasize += stat_size(&s);
591 }
592 }
593 }
594 closedir(d);
595done:
596 *_codesize = codesize;
597 *_datasize = datasize;
598 *_cachesize = cachesize;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700599 *_asecsize = asecsize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 return 0;
601}
602
603
604/* a simpler version of dexOptGenerateCacheFileName() */
605int create_cache_path(char path[PKG_PATH_MAX], const char *src)
606{
607 char *tmp;
608 int srclen;
609 int dstlen;
610
611 srclen = strlen(src);
612
613 /* demand that we are an absolute path */
614 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
615 return -1;
616 }
617
618 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
619 return -1;
620 }
621
622 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
623 strlen(DALVIK_CACHE_POSTFIX) + 1;
624
625 if (dstlen > PKG_PATH_MAX) {
626 return -1;
627 }
628
629 sprintf(path,"%s%s%s",
630 DALVIK_CACHE_PREFIX,
631 src + 1, /* skip the leading / */
632 DALVIK_CACHE_POSTFIX);
633
634 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
635 if (*tmp == '/') {
636 *tmp = '@';
637 }
638 }
639
640 return 0;
641}
642
643static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
644 const char* dexopt_flags)
645{
646 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
647 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
648 char zip_num[MAX_INT_LEN];
649 char odex_num[MAX_INT_LEN];
650
651 sprintf(zip_num, "%d", zip_fd);
652 sprintf(odex_num, "%d", odex_fd);
653
654 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
655 dexopt_flags, (char*) NULL);
Steve Block3762c312012-01-06 19:20:56 +0000656 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657}
658
659static int wait_dexopt(pid_t pid, const char* apk_path)
660{
661 int status;
662 pid_t got_pid;
663
664 /*
665 * Wait for the optimization process to finish.
666 */
667 while (1) {
668 got_pid = waitpid(pid, &status, 0);
669 if (got_pid == -1 && errno == EINTR) {
670 printf("waitpid interrupted, retrying\n");
671 } else {
672 break;
673 }
674 }
675 if (got_pid != pid) {
Steve Block8564c8d2012-01-05 23:22:43 +0000676 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 (int) pid, (int) got_pid, strerror(errno));
678 return 1;
679 }
680
681 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100682 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 return 0;
684 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000685 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 apk_path, status);
687 return status; /* always nonzero */
688 }
689}
690
691int dexopt(const char *apk_path, uid_t uid, int is_public)
692{
693 struct utimbuf ut;
694 struct stat apk_stat, dex_stat;
695 char dex_path[PKG_PATH_MAX];
696 char dexopt_flags[PROPERTY_VALUE_MAX];
697 char *end;
698 int res, zip_fd=-1, odex_fd=-1;
699
700 /* Before anything else: is there a .odex file? If so, we have
701 * pre-optimized the apk and there is nothing to do here.
702 */
703 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
704 return -1;
705 }
706
707 /* platform-specific flags affecting optimization and verification */
708 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
709
710 strcpy(dex_path, apk_path);
711 end = strrchr(dex_path, '.');
712 if (end != NULL) {
713 strcpy(end, ".odex");
714 if (stat(dex_path, &dex_stat) == 0) {
715 return 0;
716 }
717 }
718
719 if (create_cache_path(dex_path, apk_path)) {
720 return -1;
721 }
722
723 memset(&apk_stat, 0, sizeof(apk_stat));
724 stat(apk_path, &apk_stat);
725
726 zip_fd = open(apk_path, O_RDONLY, 0);
727 if (zip_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000728 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 return -1;
730 }
731
732 unlink(dex_path);
733 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
734 if (odex_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000735 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 goto fail;
737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 if (fchmod(odex_fd,
739 S_IRUSR|S_IWUSR|S_IRGRP |
740 (is_public ? S_IROTH : 0)) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000741 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 goto fail;
743 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700744 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
745 ALOGE("dexopt cannot chown '%s'\n", dex_path);
746 goto fail;
747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748
Steve Block71f2cf12011-10-20 11:56:00 +0100749 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750
751 pid_t pid;
752 pid = fork();
753 if (pid == 0) {
754 /* child -- drop privileges before continuing */
755 if (setgid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000756 ALOGE("setgid(%d) failed during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 exit(64);
758 }
759 if (setuid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000760 ALOGE("setuid(%d) during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 exit(65);
762 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700763 // drop capabilities
764 struct __user_cap_header_struct capheader;
765 struct __user_cap_data_struct capdata[2];
766 memset(&capheader, 0, sizeof(capheader));
767 memset(&capdata, 0, sizeof(capdata));
768 capheader.version = _LINUX_CAPABILITY_VERSION_3;
769 if (capset(&capheader, &capdata[0]) < 0) {
770 ALOGE("capset failed: %s\n", strerror(errno));
771 exit(66);
772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000774 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
Nick Kralevich812b19a2012-08-31 16:08:06 -0700775 exit(67);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 }
777
778 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
Nick Kralevich812b19a2012-08-31 16:08:06 -0700779 exit(68); /* only get here on exec failure */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 } else {
781 res = wait_dexopt(pid, apk_path);
782 if (res != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000783 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 goto fail;
785 }
786 }
787
788 ut.actime = apk_stat.st_atime;
789 ut.modtime = apk_stat.st_mtime;
790 utime(dex_path, &ut);
791
792 close(odex_fd);
793 close(zip_fd);
794 return 0;
795
796fail:
797 if (odex_fd >= 0) {
798 close(odex_fd);
799 unlink(dex_path);
800 }
801 if (zip_fd >= 0) {
802 close(zip_fd);
803 }
804 return -1;
805}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800806
Dianne Hackbornc1552392010-03-03 16:19:01 -0800807void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
808 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800809{
810 while (path[basepos] != 0) {
811 if (path[basepos] == '/') {
812 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800813 if (lstat(path, statbuf) < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100814 ALOGV("Making directory: %s\n", path);
Dianne Hackbornc1552392010-03-03 16:19:01 -0800815 if (mkdir(path, mode) == 0) {
816 chown(path, uid, gid);
817 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000818 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
Dianne Hackbornc1552392010-03-03 16:19:01 -0800819 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800820 }
821 path[basepos] = '/';
822 basepos++;
823 }
824 basepos++;
825 }
826}
827
Dianne Hackbornc1552392010-03-03 16:19:01 -0800828int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
829 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800830{
831 DIR *d;
832 struct dirent *de;
833 int res;
834
835 int srcend = strlen(srcpath);
836 int dstend = strlen(dstpath);
837
838 if (lstat(srcpath, statbuf) < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000839 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800840 return 1;
841 }
842
843 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800844 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
845 dstuid, dstgid, statbuf);
Steve Block71f2cf12011-10-20 11:56:00 +0100846 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800847 if (rename(srcpath, dstpath) >= 0) {
848 if (chown(dstpath, dstuid, dstgid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000849 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800850 unlink(dstpath);
851 return 1;
852 }
853 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000854 ALOGW("Unable to rename %s to %s: %s\n",
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800855 srcpath, dstpath, strerror(errno));
856 return 1;
857 }
858 return 0;
859 }
860
861 d = opendir(srcpath);
862 if (d == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000863 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800864 return 1;
865 }
866
867 res = 0;
868
869 while ((de = readdir(d))) {
870 const char *name = de->d_name;
871 /* always skip "." and ".." */
872 if (name[0] == '.') {
873 if (name[1] == 0) continue;
874 if ((name[1] == '.') && (name[2] == 0)) continue;
875 }
876
877 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000878 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800879 continue;
880 }
881
882 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000883 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800884 continue;
885 }
886
887 srcpath[srcend] = dstpath[dstend] = '/';
888 strcpy(srcpath+srcend+1, name);
889 strcpy(dstpath+dstend+1, name);
890
Dianne Hackbornc1552392010-03-03 16:19:01 -0800891 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800892 res = 1;
893 }
894
895 // Note: we will be leaving empty directories behind in srcpath,
896 // but that is okay, the package manager will be erasing all of the
897 // data associated with .apks that disappear.
898
899 srcpath[srcend] = dstpath[dstend] = 0;
900 }
901
902 closedir(d);
903 return res;
904}
905
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800906int movefiles()
907{
908 DIR *d;
909 int dfd, subfd;
910 struct dirent *de;
911 struct stat s;
912 char buf[PKG_PATH_MAX+1];
913 int bufp, bufe, bufi, readlen;
914
915 char srcpkg[PKG_NAME_MAX];
916 char dstpkg[PKG_NAME_MAX];
917 char srcpath[PKG_PATH_MAX];
918 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800919 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800920 int hasspace;
921
922 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
923 if (d == NULL) {
924 goto done;
925 }
926 dfd = dirfd(d);
927
928 /* Iterate through all files in the directory, executing the
929 * file movements requested there-in.
930 */
931 while ((de = readdir(d))) {
932 const char *name = de->d_name;
933
934 if (de->d_type == DT_DIR) {
935 continue;
936 } else {
937 subfd = openat(dfd, name, O_RDONLY);
938 if (subfd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000939 ALOGW("Unable to open update commands at %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800940 UPDATE_COMMANDS_DIR_PREFIX, name);
941 continue;
942 }
943
944 bufp = 0;
945 bufe = 0;
946 buf[PKG_PATH_MAX] = 0;
947 srcpkg[0] = dstpkg[0] = 0;
948 while (1) {
949 bufi = bufp;
950 while (bufi < bufe && buf[bufi] != '\n') {
951 bufi++;
952 }
953 if (bufi < bufe) {
954 buf[bufi] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100955 ALOGV("Processing line: %s\n", buf+bufp);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800956 hasspace = 0;
957 while (bufp < bufi && isspace(buf[bufp])) {
958 hasspace = 1;
959 bufp++;
960 }
961 if (buf[bufp] == '#' || bufp == bufi) {
962 // skip comments and empty lines.
963 } else if (hasspace) {
964 if (dstpkg[0] == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000965 ALOGW("Path before package line in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800966 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
967 } else if (srcpkg[0] == 0) {
968 // Skip -- source package no longer exists.
969 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100970 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
Kenny Root86c95842011-03-31 13:16:12 -0700971 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
972 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800973 movefileordir(srcpath, dstpath,
974 strlen(dstpath)-strlen(buf+bufp),
975 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800976 }
977 }
978 } else {
979 char* div = strchr(buf+bufp, ':');
980 if (div == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000981 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800982 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
983 } else {
984 *div = 0;
985 div++;
986 if (strlen(buf+bufp) < PKG_NAME_MAX) {
987 strcpy(dstpkg, buf+bufp);
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, buf+bufp);
992 }
993 if (strlen(div) < PKG_NAME_MAX) {
994 strcpy(srcpkg, div);
995 } else {
996 srcpkg[0] = dstpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000997 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800998 UPDATE_COMMANDS_DIR_PREFIX, name, div);
999 }
1000 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -07001001 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001002 if (lstat(srcpath, &s) < 0) {
1003 // Package no longer exists -- skip.
1004 srcpkg[0] = 0;
1005 }
1006 } else {
1007 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001008 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001009 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1010 }
1011 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -07001012 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001013 if (lstat(dstpath, &s) == 0) {
1014 dstuid = s.st_uid;
1015 dstgid = s.st_gid;
1016 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -08001017 // Destination package doesn't
1018 // exist... due to original-package,
1019 // this is normal, so don't be
1020 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001021 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001022 }
1023 } else {
1024 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001025 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001026 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1027 }
1028 }
Steve Block71f2cf12011-10-20 11:56:00 +01001029 ALOGV("Transfering from %s to %s: uid=%d\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001030 srcpkg, dstpkg, dstuid);
1031 }
1032 }
1033 }
1034 bufp = bufi+1;
1035 } else {
1036 if (bufp == 0) {
1037 if (bufp < bufe) {
Steve Block8564c8d2012-01-05 23:22:43 +00001038 ALOGW("Line too long in %s%s, skipping: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001039 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1040 }
1041 } else if (bufp < bufe) {
1042 memcpy(buf, buf+bufp, bufe-bufp);
1043 bufe -= bufp;
1044 bufp = 0;
1045 }
1046 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1047 if (readlen < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001048 ALOGW("Failure reading update commands in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001049 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1050 break;
1051 } else if (readlen == 0) {
1052 break;
1053 }
1054 bufe += readlen;
1055 buf[bufe] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +01001056 ALOGV("Read buf: %s\n", buf);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001057 }
1058 }
1059 close(subfd);
1060 }
1061 }
1062 closedir(d);
1063done:
1064 return 0;
1065}
Kenny Root6a6b0072010-10-07 16:46:10 -07001066
Kenny Roota3e90792012-10-18 10:58:36 -07001067int linklib(const char* pkgname, const char* asecLibDir, int userId)
Kenny Root6a6b0072010-10-07 16:46:10 -07001068{
Kenny Roota3e90792012-10-18 10:58:36 -07001069 char pkgdir[PKG_PATH_MAX];
1070 char libsymlink[PKG_PATH_MAX];
Kenny Root6a6b0072010-10-07 16:46:10 -07001071 struct stat s, libStat;
1072 int rc = 0;
1073
Kenny Roota3e90792012-10-18 10:58:36 -07001074 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1075 ALOGE("cannot create package path\n");
1076 return -1;
1077 }
1078 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1079 ALOGE("cannot create package lib symlink origin path\n");
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 (stat(pkgdir, &s) < 0) return -1;
1084
1085 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1086 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
Kenny Root0332d1c2010-10-21 16:14:06 -07001087 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -07001088 }
1089
Kenny Roota3e90792012-10-18 10:58:36 -07001090 if (chmod(pkgdir, 0700) < 0) {
1091 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001092 rc = -1;
1093 goto out;
1094 }
1095
Kenny Roota3e90792012-10-18 10:58:36 -07001096 if (lstat(libsymlink, &libStat) < 0) {
1097 if (errno != ENOENT) {
1098 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001099 rc = -1;
1100 goto out;
1101 }
Kenny Roota3e90792012-10-18 10:58:36 -07001102 } else {
1103 if (S_ISDIR(libStat.st_mode)) {
1104 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1105 rc = -1;
1106 goto out;
1107 }
1108 } else if (S_ISLNK(libStat.st_mode)) {
1109 if (unlink(libsymlink) < 0) {
1110 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1111 rc = -1;
1112 goto out;
1113 }
Kenny Root6a6b0072010-10-07 16:46:10 -07001114 }
1115 }
1116
Kenny Roota3e90792012-10-18 10:58:36 -07001117 if (symlink(asecLibDir, libsymlink) < 0) {
1118 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1119 strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001120 rc = -errno;
1121 goto out;
1122 }
1123
1124out:
Kenny Roota3e90792012-10-18 10:58:36 -07001125 if (chmod(pkgdir, s.st_mode) < 0) {
1126 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
Dianne Hackbornd0c5f512012-06-07 16:53:59 -07001127 rc = -errno;
Kenny Root6a6b0072010-10-07 16:46:10 -07001128 }
1129
Kenny Roota3e90792012-10-18 10:58:36 -07001130 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1131 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001132 return -errno;
1133 }
1134
1135 return rc;
1136}