blob: 09d6f89c4b8861ca6609a6569a6a1dc97aa140ab [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 Kralevich848b4c92013-02-28 16:43:23 -080017#include <sys/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
Robert Craigd3f8d032013-03-25 06:33:03 -040031int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
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
Robert Craigd3f8d032013-03-25 06:33:03 -040094 if (selinux_android_setfilecon2(pkgdir, pkgname, seinfo, uid) < 0) {
Kenny Root57c63d82012-10-17 09:50:35 -070095 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root36740042012-10-17 10:50:14 -070096 unlink(libsymlink);
Kenny Root57c63d82012-10-17 09:50:35 -070097 unlink(pkgdir);
98 return -errno;
99 }
100
Kenny Root9bbd70a2012-09-10 11:13:36 -0700101 if (chown(pkgdir, uid, gid) < 0) {
102 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
103 unlink(libsymlink);
104 unlink(pkgdir);
105 return -1;
106 }
107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 return 0;
109}
110
Amith Yamasani0b285492011-04-14 17:35:23 -0700111int uninstall(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112{
113 char pkgdir[PKG_PATH_MAX];
114
Amith Yamasani0b285492011-04-14 17:35:23 -0700115 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800116 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Amith Yamasani0b285492011-04-14 17:35:23 -0700118 /* delete contents AND directory, no exceptions */
119 return delete_dir_contents(pkgdir, 1, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120}
121
Kenny Root35ab3ad2011-02-02 16:42:14 -0800122int renamepkg(const char *oldpkgname, const char *newpkgname)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800123{
124 char oldpkgdir[PKG_PATH_MAX];
125 char newpkgdir[PKG_PATH_MAX];
126
Kenny Root86c95842011-03-31 13:16:12 -0700127 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800128 return -1;
Kenny Root86c95842011-03-31 13:16:12 -0700129 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800130 return -1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800131
132 if (rename(oldpkgdir, newpkgdir) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000133 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800134 return -errno;
135 }
136 return 0;
137}
138
Dave Burke383fa182012-10-23 23:12:19 -0700139int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700140{
141 char pkgdir[PKG_PATH_MAX];
142 struct stat s;
143 int rc = 0;
144
Dave Burke383fa182012-10-23 23:12:19 -0700145 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
146 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700147 return -1;
148 }
149
Dave Burke383fa182012-10-23 23:12:19 -0700150 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700151 ALOGE("cannot create package path\n");
152 return -1;
153 }
154
155 if (stat(pkgdir, &s) < 0) return -1;
156
Dave Burke383fa182012-10-23 23:12:19 -0700157 if (s.st_uid != 0 || s.st_gid != 0) {
158 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 -0700159 return -1;
160 }
161
162 if (chmod(pkgdir, 0751) < 0) {
163 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
164 unlink(pkgdir);
Dave Burke383fa182012-10-23 23:12:19 -0700165 return -errno;
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700166 }
Dave Burke383fa182012-10-23 23:12:19 -0700167 if (chown(pkgdir, uid, gid) < 0) {
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700168 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
169 unlink(pkgdir);
Dave Burke383fa182012-10-23 23:12:19 -0700170 return -errno;
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700171 }
172
173 return 0;
174}
175
Amith Yamasani0b285492011-04-14 17:35:23 -0700176int delete_user_data(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177{
178 char pkgdir[PKG_PATH_MAX];
179
Amith Yamasani0b285492011-04-14 17:35:23 -0700180 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800181 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
Kenny Root9157d3f2012-10-22 15:19:16 -0700183 /* delete contents, excluding "lib", but not the directory itself */
184 return delete_dir_contents(pkgdir, 0, "lib");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185}
186
rpcraig1e0c8e62012-11-28 08:37:06 -0500187int make_user_data(const char *pkgname, uid_t uid, uid_t persona, const char* seinfo)
Amith Yamasani0b285492011-04-14 17:35:23 -0700188{
189 char pkgdir[PKG_PATH_MAX];
Kenny Roota3e90792012-10-18 10:58:36 -0700190 char applibdir[PKG_PATH_MAX];
191 char libsymlink[PKG_PATH_MAX];
192 struct stat libStat;
Amith Yamasani0b285492011-04-14 17:35:23 -0700193
194 // Create the data dir for the package
195 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
196 return -1;
197 }
Kenny Roota3e90792012-10-18 10:58:36 -0700198 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, persona)) {
199 ALOGE("cannot create package lib symlink origin path\n");
200 return -1;
201 }
202 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
203 ALOGE("cannot create package lib symlink dest path\n");
204 return -1;
205 }
206
Amith Yamasani0b285492011-04-14 17:35:23 -0700207 if (mkdir(pkgdir, 0751) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000208 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
Amith Yamasani0b285492011-04-14 17:35:23 -0700209 return -errno;
210 }
Amith Yamasani794d62f2012-08-24 12:58:27 -0700211 if (chmod(pkgdir, 0751) < 0) {
212 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
213 unlink(pkgdir);
214 return -errno;
215 }
Kenny Roota3e90792012-10-18 10:58:36 -0700216
217 if (lstat(libsymlink, &libStat) < 0) {
218 if (errno != ENOENT) {
219 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
220 unlink(pkgdir);
221 return -1;
222 }
223 } else {
224 if (S_ISDIR(libStat.st_mode)) {
225 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
226 ALOGE("couldn't delete lib directory during install for non-primary: %s",
227 libsymlink);
228 unlink(pkgdir);
229 return -1;
230 }
231 } else if (S_ISLNK(libStat.st_mode)) {
232 if (unlink(libsymlink) < 0) {
233 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
234 libsymlink);
235 unlink(pkgdir);
236 return -1;
237 }
238 }
239 }
240
241 if (symlink(applibdir, libsymlink) < 0) {
242 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
243 applibdir, strerror(errno));
244 unlink(pkgdir);
245 return -1;
246 }
247
rpcraig1e0c8e62012-11-28 08:37:06 -0500248 if (selinux_android_setfilecon2(pkgdir, pkgname, seinfo, uid) < 0) {
Joshua Brindle365861e2012-07-10 10:22:36 -0400249 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Roota3e90792012-10-18 10:58:36 -0700250 unlink(libsymlink);
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500251 unlink(pkgdir);
252 return -errno;
253 }
Kenny Root9396f182012-10-19 17:16:41 -0700254
Kenny Rootc9a1aab2012-10-16 23:28:21 -0700255 if (chown(pkgdir, uid, uid) < 0) {
256 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Kenny Root9396f182012-10-19 17:16:41 -0700257 unlink(libsymlink);
Kenny Rootc9a1aab2012-10-16 23:28:21 -0700258 unlink(pkgdir);
259 return -errno;
260 }
Stephen Smalley0b58e6a2012-01-13 08:27:42 -0500261
Amith Yamasani0b285492011-04-14 17:35:23 -0700262 return 0;
263}
264
265int delete_persona(uid_t persona)
266{
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700267 char data_path[PKG_PATH_MAX];
268 if (create_persona_path(data_path, persona)) {
Amith Yamasani0b285492011-04-14 17:35:23 -0700269 return -1;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700270 }
271 if (delete_dir_contents(data_path, 1, NULL)) {
272 return -1;
273 }
Amith Yamasani0b285492011-04-14 17:35:23 -0700274
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700275 char media_path[PATH_MAX];
276 if (create_persona_media_path(media_path, (userid_t) persona) == -1) {
277 return -1;
278 }
279 if (delete_dir_contents(media_path, 1, NULL) == -1) {
280 return -1;
281 }
282
283 return 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700284}
285
Amith Yamasani742a6712011-05-04 14:49:28 -0700286int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy)
287{
288 char src_data_dir[PKG_PATH_MAX];
289 char pkg_path[PKG_PATH_MAX];
290 DIR *d;
291 struct dirent *de;
292 struct stat s;
293 uid_t uid;
294
295 if (create_persona_path(src_data_dir, src_persona)) {
296 return -1;
297 }
298
299 d = opendir(src_data_dir);
300 if (d != NULL) {
301 while ((de = readdir(d))) {
302 const char *name = de->d_name;
303
304 if (de->d_type == DT_DIR) {
305 int subfd;
306 /* always skip "." and ".." */
307 if (name[0] == '.') {
308 if (name[1] == 0) continue;
309 if ((name[1] == '.') && (name[2] == 0)) continue;
310 }
311 /* Create the full path to the package's data dir */
312 create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona);
313 /* Get the file stat */
314 if (stat(pkg_path, &s) < 0) continue;
315 /* Get the uid of the package */
Kenny Roota3e90792012-10-18 10:58:36 -0700316 ALOGI("Adding datadir for uid = %lu\n", s.st_uid);
Amith Yamasani742a6712011-05-04 14:49:28 -0700317 uid = (uid_t) s.st_uid % PER_USER_RANGE;
318 /* Create the directory for the target */
319 make_user_data(name, uid + target_persona * PER_USER_RANGE,
rpcraig1e0c8e62012-11-28 08:37:06 -0500320 target_persona, NULL);
Amith Yamasani742a6712011-05-04 14:49:28 -0700321 }
322 }
323 closedir(d);
324 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700325
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700326 if (ensure_media_user_dirs((userid_t) target_persona) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700327 return -1;
328 }
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700329
Amith Yamasani742a6712011-05-04 14:49:28 -0700330 return 0;
331}
332
Amith Yamasani54289b82012-10-01 10:39:14 -0700333int delete_cache(const char *pkgname, uid_t persona)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334{
335 char cachedir[PKG_PATH_MAX];
336
Amith Yamasani54289b82012-10-01 10:39:14 -0700337 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, persona))
Kenny Root35ab3ad2011-02-02 16:42:14 -0800338 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339
340 /* delete contents, not the directory, no exceptions */
341 return delete_dir_contents(cachedir, 0, 0);
342}
343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344/* Try to ensure free_size bytes of storage are available.
345 * Returns 0 on success.
346 * This is rather simple-minded because doing a full LRU would
347 * be potentially memory-intensive, and without atime it would
348 * also require that apps constantly modify file metadata even
349 * when just reading from the cache, which is pretty awful.
350 */
Kenny Root3e319a92010-09-07 13:58:28 -0700351int free_cache(int64_t free_size)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352{
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700353 cache_t* cache;
354 int64_t avail;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 DIR *d;
356 struct dirent *de;
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700357 char tmpdir[PATH_MAX];
358 char *dirpos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700360 avail = data_disk_free();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 if (avail < 0) return -1;
362
Steve Block6215d3f2012-01-04 20:05:49 +0000363 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 if (avail >= free_size) return 0;
365
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700366 cache = start_cache_collection();
367
368 // Collect cache files for primary user.
369 if (create_persona_path(tmpdir, 0) == 0) {
370 //ALOGI("adding cache files from %s\n", tmpdir);
371 add_cache_files(cache, tmpdir, "cache");
Kenny Rootad757e92011-11-29 15:54:55 -0800372 }
373
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700374 // Search for other users and add any cache files from them.
375 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
376 SECONDARY_USER_PREFIX);
377 dirpos = tmpdir + strlen(tmpdir);
378 d = opendir(tmpdir);
379 if (d != NULL) {
380 while ((de = readdir(d))) {
381 if (de->d_type == DT_DIR) {
382 const char *name = de->d_name;
383 /* always skip "." and ".." */
384 if (name[0] == '.') {
385 if (name[1] == 0) continue;
386 if ((name[1] == '.') && (name[2] == 0)) continue;
387 }
388 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
389 strcpy(dirpos, name);
390 //ALOGI("adding cache files from %s\n", tmpdir);
391 add_cache_files(cache, tmpdir, "cache");
392 } else {
393 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
394 }
395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700397 closedir(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 }
Oscar Montemayora8529f62009-11-18 10:14:20 -0800399
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700400 // Collect cache files on external storage for all users (if it is mounted as part
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700401 // of the internal storage).
402 strcpy(tmpdir, android_media_dir.path);
Dianne Hackborn556b09e2012-09-23 17:46:53 -0700403 dirpos = tmpdir + strlen(tmpdir);
404 d = opendir(tmpdir);
405 if (d != NULL) {
406 while ((de = readdir(d))) {
407 if (de->d_type == DT_DIR) {
408 const char *name = de->d_name;
409 /* skip any dir that doesn't start with a number, so not a user */
410 if (name[0] < '0' || name[0] > '9') {
411 continue;
412 }
413 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
414 strcpy(dirpos, name);
415 if (lookup_media_dir(tmpdir, "Android") == 0
416 && lookup_media_dir(tmpdir, "data") == 0) {
417 //ALOGI("adding cache files from %s\n", tmpdir);
418 add_cache_files(cache, tmpdir, "cache");
419 }
420 } else {
421 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
422 }
423 }
424 }
425 closedir(d);
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700426 }
427
428 clear_cache_files(cache, free_size);
429 finish_cache_collection(cache);
430
431 return data_disk_free() >= free_size ? 0 : -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432}
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434int move_dex(const char *src, const char *dst)
435{
436 char src_dex[PKG_PATH_MAX];
437 char dst_dex[PKG_PATH_MAX];
438
Kenny Root86c95842011-03-31 13:16:12 -0700439 if (validate_apk_path(src)) return -1;
440 if (validate_apk_path(dst)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441
442 if (create_cache_path(src_dex, src)) return -1;
443 if (create_cache_path(dst_dex, dst)) return -1;
444
Steve Block71f2cf12011-10-20 11:56:00 +0100445 ALOGV("move %s -> %s\n", src_dex, dst_dex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 if (rename(src_dex, dst_dex) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000447 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 return -1;
449 } else {
450 return 0;
451 }
452}
453
454int rm_dex(const char *path)
455{
456 char dex_path[PKG_PATH_MAX];
457
Kenny Root86c95842011-03-31 13:16:12 -0700458 if (validate_apk_path(path)) return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 if (create_cache_path(dex_path, path)) return -1;
460
Steve Block71f2cf12011-10-20 11:56:00 +0100461 ALOGV("unlink %s\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 if (unlink(dex_path) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000463 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 return -1;
465 } else {
466 return 0;
467 }
468}
469
Dianne Hackborn0c380492012-08-20 17:23:30 -0700470int get_size(const char *pkgname, int persona, const char *apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700471 const char *fwdlock_apkpath, const char *asecpath,
472 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
473 int64_t* _asecsize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474{
475 DIR *d;
476 int dfd;
477 struct dirent *de;
478 struct stat s;
479 char path[PKG_PATH_MAX];
480
Kenny Root3e319a92010-09-07 13:58:28 -0700481 int64_t codesize = 0;
482 int64_t datasize = 0;
483 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700484 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485
486 /* count the source apk as code -- but only if it's not
Suchi Amalapurapu8a9ab242010-03-11 16:49:16 -0800487 * on the /system partition and its not on the sdcard.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 */
Kenny Root86c95842011-03-31 13:16:12 -0700489 if (validate_system_app_path(apkpath) &&
490 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 if (stat(apkpath, &s) == 0) {
492 codesize += stat_size(&s);
493 }
494 }
495 /* count the forward locked apk as code if it is given
496 */
497 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
498 if (stat(fwdlock_apkpath, &s) == 0) {
499 codesize += stat_size(&s);
500 }
501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 /* count the cached dexfile as code */
503 if (!create_cache_path(path, apkpath)) {
504 if (stat(path, &s) == 0) {
505 codesize += stat_size(&s);
506 }
507 }
508
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700509 /* add in size of any libraries */
510 if (!create_pkg_path_in_dir(path, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
511 d = opendir(path);
512 if (d != NULL) {
513 dfd = dirfd(d);
514 codesize += calculate_dir_size(dfd);
515 closedir(d);
516 }
517 }
518
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700519 /* compute asec size if it is given
520 */
521 if (asecpath != NULL && asecpath[0] != '!') {
522 if (stat(asecpath, &s) == 0) {
523 asecsize += stat_size(&s);
524 }
525 }
526
Dianne Hackborn0c380492012-08-20 17:23:30 -0700527 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) {
Kenny Root35ab3ad2011-02-02 16:42:14 -0800528 goto done;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 }
530
531 d = opendir(path);
532 if (d == NULL) {
533 goto done;
534 }
535 dfd = dirfd(d);
536
Kenny Root86c95842011-03-31 13:16:12 -0700537 /* most stuff in the pkgdir is data, except for the "cache"
538 * directory and below, which is cache, and the "lib" directory
539 * and below, which is code...
540 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 while ((de = readdir(d))) {
542 const char *name = de->d_name;
543
544 if (de->d_type == DT_DIR) {
545 int subfd;
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700546 int64_t statsize = 0;
547 int64_t dirsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 /* always skip "." and ".." */
549 if (name[0] == '.') {
550 if (name[1] == 0) continue;
551 if ((name[1] == '.') && (name[2] == 0)) continue;
552 }
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700553 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
554 statsize = stat_size(&s);
555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
557 if (subfd >= 0) {
Dianne Hackbornf41496f2012-09-27 18:48:09 -0700558 dirsize = calculate_dir_size(subfd);
559 }
560 if(!strcmp(name,"lib")) {
561 codesize += dirsize + statsize;
562 } else if(!strcmp(name,"cache")) {
563 cachesize += dirsize + statsize;
564 } else {
565 datasize += dirsize + statsize;
566 }
567 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
568 // This is the symbolic link to the application's library
569 // code. We'll count this as code instead of data, since
570 // it is not something that the app creates.
571 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
572 codesize += stat_size(&s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 }
574 } else {
575 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
576 datasize += stat_size(&s);
577 }
578 }
579 }
580 closedir(d);
581done:
582 *_codesize = codesize;
583 *_datasize = datasize;
584 *_cachesize = cachesize;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700585 *_asecsize = asecsize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 return 0;
587}
588
589
590/* a simpler version of dexOptGenerateCacheFileName() */
591int create_cache_path(char path[PKG_PATH_MAX], const char *src)
592{
593 char *tmp;
594 int srclen;
595 int dstlen;
596
597 srclen = strlen(src);
598
599 /* demand that we are an absolute path */
600 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
601 return -1;
602 }
603
604 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
605 return -1;
606 }
607
608 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
609 strlen(DALVIK_CACHE_POSTFIX) + 1;
610
611 if (dstlen > PKG_PATH_MAX) {
612 return -1;
613 }
614
615 sprintf(path,"%s%s%s",
616 DALVIK_CACHE_PREFIX,
617 src + 1, /* skip the leading / */
618 DALVIK_CACHE_POSTFIX);
619
620 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
621 if (*tmp == '/') {
622 *tmp = '@';
623 }
624 }
625
626 return 0;
627}
628
629static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
630 const char* dexopt_flags)
631{
632 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
633 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
634 char zip_num[MAX_INT_LEN];
635 char odex_num[MAX_INT_LEN];
636
637 sprintf(zip_num, "%d", zip_fd);
638 sprintf(odex_num, "%d", odex_fd);
639
640 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
641 dexopt_flags, (char*) NULL);
Steve Block3762c312012-01-06 19:20:56 +0000642 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643}
644
645static int wait_dexopt(pid_t pid, const char* apk_path)
646{
647 int status;
648 pid_t got_pid;
649
650 /*
651 * Wait for the optimization process to finish.
652 */
653 while (1) {
654 got_pid = waitpid(pid, &status, 0);
655 if (got_pid == -1 && errno == EINTR) {
656 printf("waitpid interrupted, retrying\n");
657 } else {
658 break;
659 }
660 }
661 if (got_pid != pid) {
Steve Block8564c8d2012-01-05 23:22:43 +0000662 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 (int) pid, (int) got_pid, strerror(errno));
664 return 1;
665 }
666
667 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100668 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 return 0;
670 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000671 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 apk_path, status);
673 return status; /* always nonzero */
674 }
675}
676
677int dexopt(const char *apk_path, uid_t uid, int is_public)
678{
679 struct utimbuf ut;
680 struct stat apk_stat, dex_stat;
681 char dex_path[PKG_PATH_MAX];
682 char dexopt_flags[PROPERTY_VALUE_MAX];
683 char *end;
684 int res, zip_fd=-1, odex_fd=-1;
685
686 /* Before anything else: is there a .odex file? If so, we have
687 * pre-optimized the apk and there is nothing to do here.
688 */
689 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
690 return -1;
691 }
692
693 /* platform-specific flags affecting optimization and verification */
694 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
695
696 strcpy(dex_path, apk_path);
697 end = strrchr(dex_path, '.');
698 if (end != NULL) {
699 strcpy(end, ".odex");
700 if (stat(dex_path, &dex_stat) == 0) {
701 return 0;
702 }
703 }
704
705 if (create_cache_path(dex_path, apk_path)) {
706 return -1;
707 }
708
709 memset(&apk_stat, 0, sizeof(apk_stat));
710 stat(apk_path, &apk_stat);
711
712 zip_fd = open(apk_path, O_RDONLY, 0);
713 if (zip_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000714 ALOGE("dexopt cannot open '%s' for input\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 return -1;
716 }
717
718 unlink(dex_path);
719 odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644);
720 if (odex_fd < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000721 ALOGE("dexopt cannot open '%s' for output\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 goto fail;
723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 if (fchmod(odex_fd,
725 S_IRUSR|S_IWUSR|S_IRGRP |
726 (is_public ? S_IROTH : 0)) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000727 ALOGE("dexopt cannot chmod '%s'\n", dex_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 goto fail;
729 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700730 if (fchown(odex_fd, AID_SYSTEM, uid) < 0) {
731 ALOGE("dexopt cannot chown '%s'\n", dex_path);
732 goto fail;
733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734
Steve Block71f2cf12011-10-20 11:56:00 +0100735 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736
737 pid_t pid;
738 pid = fork();
739 if (pid == 0) {
740 /* child -- drop privileges before continuing */
741 if (setgid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000742 ALOGE("setgid(%d) failed during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 exit(64);
744 }
745 if (setuid(uid) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000746 ALOGE("setuid(%d) during dexopt\n", uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 exit(65);
748 }
Nick Kralevich812b19a2012-08-31 16:08:06 -0700749 // drop capabilities
750 struct __user_cap_header_struct capheader;
751 struct __user_cap_data_struct capdata[2];
752 memset(&capheader, 0, sizeof(capheader));
753 memset(&capdata, 0, sizeof(capdata));
754 capheader.version = _LINUX_CAPABILITY_VERSION_3;
755 if (capset(&capheader, &capdata[0]) < 0) {
756 ALOGE("capset failed: %s\n", strerror(errno));
757 exit(66);
758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000760 ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno));
Nick Kralevich812b19a2012-08-31 16:08:06 -0700761 exit(67);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763
764 run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags);
Nick Kralevich812b19a2012-08-31 16:08:06 -0700765 exit(68); /* only get here on exec failure */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 } else {
767 res = wait_dexopt(pid, apk_path);
768 if (res != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000769 ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 goto fail;
771 }
772 }
773
774 ut.actime = apk_stat.st_atime;
775 ut.modtime = apk_stat.st_mtime;
776 utime(dex_path, &ut);
777
778 close(odex_fd);
779 close(zip_fd);
780 return 0;
781
782fail:
783 if (odex_fd >= 0) {
784 close(odex_fd);
785 unlink(dex_path);
786 }
787 if (zip_fd >= 0) {
788 close(zip_fd);
789 }
790 return -1;
791}
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800792
Dianne Hackbornc1552392010-03-03 16:19:01 -0800793void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
794 struct stat* statbuf)
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800795{
796 while (path[basepos] != 0) {
797 if (path[basepos] == '/') {
798 path[basepos] = 0;
Dianne Hackbornc1552392010-03-03 16:19:01 -0800799 if (lstat(path, statbuf) < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100800 ALOGV("Making directory: %s\n", path);
Dianne Hackbornc1552392010-03-03 16:19:01 -0800801 if (mkdir(path, mode) == 0) {
802 chown(path, uid, gid);
803 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000804 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
Dianne Hackbornc1552392010-03-03 16:19:01 -0800805 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800806 }
807 path[basepos] = '/';
808 basepos++;
809 }
810 basepos++;
811 }
812}
813
Dianne Hackbornc1552392010-03-03 16:19:01 -0800814int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
815 int dstuid, int dstgid, struct stat* statbuf)
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800816{
817 DIR *d;
818 struct dirent *de;
819 int res;
820
821 int srcend = strlen(srcpath);
822 int dstend = strlen(dstpath);
823
824 if (lstat(srcpath, statbuf) < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000825 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800826 return 1;
827 }
828
829 if ((statbuf->st_mode&S_IFDIR) == 0) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800830 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
831 dstuid, dstgid, statbuf);
Steve Block71f2cf12011-10-20 11:56:00 +0100832 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800833 if (rename(srcpath, dstpath) >= 0) {
834 if (chown(dstpath, dstuid, dstgid) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000835 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800836 unlink(dstpath);
837 return 1;
838 }
839 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000840 ALOGW("Unable to rename %s to %s: %s\n",
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800841 srcpath, dstpath, strerror(errno));
842 return 1;
843 }
844 return 0;
845 }
846
847 d = opendir(srcpath);
848 if (d == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000849 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800850 return 1;
851 }
852
853 res = 0;
854
855 while ((de = readdir(d))) {
856 const char *name = de->d_name;
857 /* always skip "." and ".." */
858 if (name[0] == '.') {
859 if (name[1] == 0) continue;
860 if ((name[1] == '.') && (name[2] == 0)) continue;
861 }
862
863 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000864 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800865 continue;
866 }
867
868 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000869 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800870 continue;
871 }
872
873 srcpath[srcend] = dstpath[dstend] = '/';
874 strcpy(srcpath+srcend+1, name);
875 strcpy(dstpath+dstend+1, name);
876
Dianne Hackbornc1552392010-03-03 16:19:01 -0800877 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800878 res = 1;
879 }
880
881 // Note: we will be leaving empty directories behind in srcpath,
882 // but that is okay, the package manager will be erasing all of the
883 // data associated with .apks that disappear.
884
885 srcpath[srcend] = dstpath[dstend] = 0;
886 }
887
888 closedir(d);
889 return res;
890}
891
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800892int movefiles()
893{
894 DIR *d;
895 int dfd, subfd;
896 struct dirent *de;
897 struct stat s;
898 char buf[PKG_PATH_MAX+1];
899 int bufp, bufe, bufi, readlen;
900
901 char srcpkg[PKG_NAME_MAX];
902 char dstpkg[PKG_NAME_MAX];
903 char srcpath[PKG_PATH_MAX];
904 char dstpath[PKG_PATH_MAX];
Dianne Hackbornd705fd22010-02-12 14:58:04 -0800905 int dstuid=-1, dstgid=-1;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800906 int hasspace;
907
908 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
909 if (d == NULL) {
910 goto done;
911 }
912 dfd = dirfd(d);
913
914 /* Iterate through all files in the directory, executing the
915 * file movements requested there-in.
916 */
917 while ((de = readdir(d))) {
918 const char *name = de->d_name;
919
920 if (de->d_type == DT_DIR) {
921 continue;
922 } else {
923 subfd = openat(dfd, name, O_RDONLY);
924 if (subfd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000925 ALOGW("Unable to open update commands at %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800926 UPDATE_COMMANDS_DIR_PREFIX, name);
927 continue;
928 }
929
930 bufp = 0;
931 bufe = 0;
932 buf[PKG_PATH_MAX] = 0;
933 srcpkg[0] = dstpkg[0] = 0;
934 while (1) {
935 bufi = bufp;
936 while (bufi < bufe && buf[bufi] != '\n') {
937 bufi++;
938 }
939 if (bufi < bufe) {
940 buf[bufi] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +0100941 ALOGV("Processing line: %s\n", buf+bufp);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800942 hasspace = 0;
943 while (bufp < bufi && isspace(buf[bufp])) {
944 hasspace = 1;
945 bufp++;
946 }
947 if (buf[bufp] == '#' || bufp == bufi) {
948 // skip comments and empty lines.
949 } else if (hasspace) {
950 if (dstpkg[0] == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000951 ALOGW("Path before package line in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800952 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
953 } else if (srcpkg[0] == 0) {
954 // Skip -- source package no longer exists.
955 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100956 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
Kenny Root86c95842011-03-31 13:16:12 -0700957 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
958 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -0800959 movefileordir(srcpath, dstpath,
960 strlen(dstpath)-strlen(buf+bufp),
961 dstuid, dstgid, &s);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800962 }
963 }
964 } else {
965 char* div = strchr(buf+bufp, ':');
966 if (div == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000967 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800968 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
969 } else {
970 *div = 0;
971 div++;
972 if (strlen(buf+bufp) < PKG_NAME_MAX) {
973 strcpy(dstpkg, buf+bufp);
974 } else {
975 srcpkg[0] = dstpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000976 ALOGW("Package name too long in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800977 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
978 }
979 if (strlen(div) < PKG_NAME_MAX) {
980 strcpy(srcpkg, div);
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, div);
985 }
986 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700987 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800988 if (lstat(srcpath, &s) < 0) {
989 // Package no longer exists -- skip.
990 srcpkg[0] = 0;
991 }
992 } else {
993 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +0000994 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800995 div, UPDATE_COMMANDS_DIR_PREFIX, name);
996 }
997 if (srcpkg[0] != 0) {
Kenny Root86c95842011-03-31 13:16:12 -0700998 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800999 if (lstat(dstpath, &s) == 0) {
1000 dstuid = s.st_uid;
1001 dstgid = s.st_gid;
1002 } else {
Dianne Hackbornd705fd22010-02-12 14:58:04 -08001003 // Destination package doesn't
1004 // exist... due to original-package,
1005 // this is normal, so don't be
1006 // noisy about it.
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001007 srcpkg[0] = 0;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001008 }
1009 } else {
1010 srcpkg[0] = 0;
Steve Block8564c8d2012-01-05 23:22:43 +00001011 ALOGW("Can't create path %s in %s%s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001012 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1013 }
1014 }
Steve Block71f2cf12011-10-20 11:56:00 +01001015 ALOGV("Transfering from %s to %s: uid=%d\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001016 srcpkg, dstpkg, dstuid);
1017 }
1018 }
1019 }
1020 bufp = bufi+1;
1021 } else {
1022 if (bufp == 0) {
1023 if (bufp < bufe) {
Steve Block8564c8d2012-01-05 23:22:43 +00001024 ALOGW("Line too long in %s%s, skipping: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001025 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1026 }
1027 } else if (bufp < bufe) {
1028 memcpy(buf, buf+bufp, bufe-bufp);
1029 bufe -= bufp;
1030 bufp = 0;
1031 }
1032 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1033 if (readlen < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001034 ALOGW("Failure reading update commands in %s%s: %s\n",
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001035 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1036 break;
1037 } else if (readlen == 0) {
1038 break;
1039 }
1040 bufe += readlen;
1041 buf[bufe] = 0;
Steve Block71f2cf12011-10-20 11:56:00 +01001042 ALOGV("Read buf: %s\n", buf);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001043 }
1044 }
1045 close(subfd);
1046 }
1047 }
1048 closedir(d);
1049done:
1050 return 0;
1051}
Kenny Root6a6b0072010-10-07 16:46:10 -07001052
Kenny Roota3e90792012-10-18 10:58:36 -07001053int linklib(const char* pkgname, const char* asecLibDir, int userId)
Kenny Root6a6b0072010-10-07 16:46:10 -07001054{
Kenny Roota3e90792012-10-18 10:58:36 -07001055 char pkgdir[PKG_PATH_MAX];
1056 char libsymlink[PKG_PATH_MAX];
Kenny Root6a6b0072010-10-07 16:46:10 -07001057 struct stat s, libStat;
1058 int rc = 0;
1059
Kenny Roota3e90792012-10-18 10:58:36 -07001060 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1061 ALOGE("cannot create package path\n");
1062 return -1;
1063 }
1064 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1065 ALOGE("cannot create package lib symlink origin path\n");
Kenny Root0332d1c2010-10-21 16:14:06 -07001066 return -1;
Kenny Root6a6b0072010-10-07 16:46:10 -07001067 }
1068
Kenny Roota3e90792012-10-18 10:58:36 -07001069 if (stat(pkgdir, &s) < 0) return -1;
1070
1071 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1072 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
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 (chmod(pkgdir, 0700) < 0) {
1077 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001078 rc = -1;
1079 goto out;
1080 }
1081
Kenny Roota3e90792012-10-18 10:58:36 -07001082 if (lstat(libsymlink, &libStat) < 0) {
1083 if (errno != ENOENT) {
1084 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001085 rc = -1;
1086 goto out;
1087 }
Kenny Roota3e90792012-10-18 10:58:36 -07001088 } else {
1089 if (S_ISDIR(libStat.st_mode)) {
1090 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1091 rc = -1;
1092 goto out;
1093 }
1094 } else if (S_ISLNK(libStat.st_mode)) {
1095 if (unlink(libsymlink) < 0) {
1096 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1097 rc = -1;
1098 goto out;
1099 }
Kenny Root6a6b0072010-10-07 16:46:10 -07001100 }
1101 }
1102
Kenny Roota3e90792012-10-18 10:58:36 -07001103 if (symlink(asecLibDir, libsymlink) < 0) {
1104 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1105 strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001106 rc = -errno;
1107 goto out;
1108 }
1109
1110out:
Kenny Roota3e90792012-10-18 10:58:36 -07001111 if (chmod(pkgdir, s.st_mode) < 0) {
1112 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
Dianne Hackbornd0c5f512012-06-07 16:53:59 -07001113 rc = -errno;
Kenny Root6a6b0072010-10-07 16:46:10 -07001114 }
1115
Kenny Roota3e90792012-10-18 10:58:36 -07001116 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1117 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
Kenny Root6a6b0072010-10-07 16:46:10 -07001118 return -errno;
1119 }
1120
1121 return rc;
1122}