blob: 73f707a3c7fb7aed3871c99fc71a14118e23d395 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070017#include <inttypes.h>
Nick Kralevichd7471292013-02-28 16:59:13 -080018#include <sys/capability.h>
Elliott Hughes2ead70c2015-02-16 10:44:22 -080019#include <sys/file.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070020#include "installd.h"
Brian Carlstrom0378aaf2014-08-08 00:52:22 -070021#include <cutils/sched_policy.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070022#include <diskusage/dirsize.h>
23#include <selinux/android.h>
Igor Murashkin9e87a802014-11-05 15:21:12 -080024#include <system/thread_defs.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070025
26/* Directory records that are used in execution of commands. */
27dir_rec_t android_data_dir;
28dir_rec_t android_asec_dir;
29dir_rec_t android_app_dir;
30dir_rec_t android_app_private_dir;
31dir_rec_t android_app_lib_dir;
32dir_rec_t android_media_dir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -070033dir_rec_t android_mnt_expand_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -070034dir_rec_array_t android_system_dirs;
35
Jeff Sharkeyc03de092015-04-07 18:14:05 -070036int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070037{
Mike Lockwood94afecf2012-10-24 10:45:23 -070038 struct stat libStat;
39
40 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
41 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
42 return -1;
43 }
44
Jeff Sharkeyc03de092015-04-07 18:14:05 -070045 std::string _pkgdir(create_package_data_path(uuid, pkgname, 0));
46 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070047
Nick Kralevicha2d838a2013-01-09 16:00:35 -080048 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070049 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
50 return -1;
51 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080052 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070053 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
54 unlink(pkgdir);
55 return -1;
56 }
57
Stephen Smalley26288202014-02-07 09:16:46 -050058 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070059 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -070060 unlink(pkgdir);
61 return -errno;
62 }
63
64 if (chown(pkgdir, uid, gid) < 0) {
65 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -070066 unlink(pkgdir);
67 return -1;
68 }
69
70 return 0;
71}
72
Jeff Sharkeyc03de092015-04-07 18:14:05 -070073int uninstall(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -070074{
Jeff Sharkeyc03de092015-04-07 18:14:05 -070075 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
76 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070077
Dave Allisond9370732014-01-30 14:19:23 -080078 remove_profile_file(pkgname);
79
Mike Lockwood94afecf2012-10-24 10:45:23 -070080 /* delete contents AND directory, no exceptions */
81 return delete_dir_contents(pkgdir, 1, NULL);
82}
83
84int renamepkg(const char *oldpkgname, const char *newpkgname)
85{
86 char oldpkgdir[PKG_PATH_MAX];
87 char newpkgdir[PKG_PATH_MAX];
88
89 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
90 return -1;
91 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
92 return -1;
93
94 if (rename(oldpkgdir, newpkgdir) < 0) {
95 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
96 return -errno;
97 }
98 return 0;
99}
100
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700101int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700102{
Mike Lockwood94afecf2012-10-24 10:45:23 -0700103 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700104
105 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
106 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
107 return -1;
108 }
109
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700110 std::string _pkgdir(create_package_data_path(uuid, pkgname, 0));
111 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700112
113 if (stat(pkgdir, &s) < 0) return -1;
114
115 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700116 ALOGE("fixing uid of non-root pkg: %s %" PRIu32 " %" PRIu32 "\n", pkgdir, s.st_uid, s.st_gid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700117 return -1;
118 }
119
120 if (chmod(pkgdir, 0751) < 0) {
121 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
122 unlink(pkgdir);
123 return -errno;
124 }
125 if (chown(pkgdir, uid, gid) < 0) {
126 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
127 unlink(pkgdir);
128 return -errno;
129 }
130
131 return 0;
132}
133
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700134int delete_user_data(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700135{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700136 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
137 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138
Jeff Sharkey3316fe42014-08-27 10:46:25 -0700139 return delete_dir_contents(pkgdir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700140}
141
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700142int make_user_data(const char *uuid, const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700143{
Mike Lockwood94afecf2012-10-24 10:45:23 -0700144 struct stat libStat;
145
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700146 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
147 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800149 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
151 return -errno;
152 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800153 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700154 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
155 unlink(pkgdir);
156 return -errno;
157 }
158
Stephen Smalley26288202014-02-07 09:16:46 -0500159 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700160 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700161 unlink(pkgdir);
162 return -errno;
163 }
164
165 if (chown(pkgdir, uid, uid) < 0) {
166 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700167 unlink(pkgdir);
168 return -errno;
169 }
170
171 return 0;
172}
173
Robin Lee7c8bec02014-06-10 18:46:26 +0100174int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700175{
Robin Lee095c7632014-04-25 15:05:19 +0100176 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700177 return -1;
178 }
179
180 return 0;
181}
182
Robin Lee095c7632014-04-25 15:05:19 +0100183int delete_user(userid_t userid)
184{
185 int status = 0;
186
187 char data_path[PKG_PATH_MAX];
188 if ((create_user_path(data_path, userid) != 0)
189 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
190 status = -1;
191 }
192
193 char media_path[PATH_MAX];
194 if ((create_user_media_path(media_path, userid) != 0)
195 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
196 status = -1;
197 }
198
199 char config_path[PATH_MAX];
200 if ((create_user_config_path(config_path, userid) != 0)
201 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
202 status = -1;
203 }
204
205 return status;
206}
207
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700208int delete_cache(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700209{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700210 std::string _cachedir(
211 create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX);
212 const char* cachedir = _cachedir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700214 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100215 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700216}
217
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700218int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid)
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700219{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700220 std::string _codecachedir(
221 create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX);
222 const char* codecachedir = _codecachedir.c_str();
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700223
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700224 struct stat s;
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700225
Jeff Sharkey770180a2014-09-08 17:14:26 -0700226 /* it's okay if code cache is missing */
227 if (lstat(codecachedir, &s) == -1 && errno == ENOENT) {
228 return 0;
229 }
230
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700231 /* delete contents, not the directory, no exceptions */
232 return delete_dir_contents(codecachedir, 0, NULL);
233}
234
Mike Lockwood94afecf2012-10-24 10:45:23 -0700235/* Try to ensure free_size bytes of storage are available.
236 * Returns 0 on success.
237 * This is rather simple-minded because doing a full LRU would
238 * be potentially memory-intensive, and without atime it would
239 * also require that apps constantly modify file metadata even
240 * when just reading from the cache, which is pretty awful.
241 */
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700242// TODO: extend to know about other volumes
Mike Lockwood94afecf2012-10-24 10:45:23 -0700243int free_cache(int64_t free_size)
244{
245 cache_t* cache;
246 int64_t avail;
247 DIR *d;
248 struct dirent *de;
249 char tmpdir[PATH_MAX];
250 char *dirpos;
251
252 avail = data_disk_free();
253 if (avail < 0) return -1;
254
255 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
256 if (avail >= free_size) return 0;
257
258 cache = start_cache_collection();
259
260 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700261 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700262 //ALOGI("adding cache files from %s\n", tmpdir);
263 add_cache_files(cache, tmpdir, "cache");
264 }
265
266 // Search for other users and add any cache files from them.
267 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
268 SECONDARY_USER_PREFIX);
269 dirpos = tmpdir + strlen(tmpdir);
270 d = opendir(tmpdir);
271 if (d != NULL) {
272 while ((de = readdir(d))) {
273 if (de->d_type == DT_DIR) {
274 const char *name = de->d_name;
275 /* always skip "." and ".." */
276 if (name[0] == '.') {
277 if (name[1] == 0) continue;
278 if ((name[1] == '.') && (name[2] == 0)) continue;
279 }
280 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
281 strcpy(dirpos, name);
282 //ALOGI("adding cache files from %s\n", tmpdir);
283 add_cache_files(cache, tmpdir, "cache");
284 } else {
285 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
286 }
287 }
288 }
289 closedir(d);
290 }
291
292 // Collect cache files on external storage for all users (if it is mounted as part
293 // of the internal storage).
294 strcpy(tmpdir, android_media_dir.path);
295 dirpos = tmpdir + strlen(tmpdir);
296 d = opendir(tmpdir);
297 if (d != NULL) {
298 while ((de = readdir(d))) {
299 if (de->d_type == DT_DIR) {
300 const char *name = de->d_name;
301 /* skip any dir that doesn't start with a number, so not a user */
302 if (name[0] < '0' || name[0] > '9') {
303 continue;
304 }
305 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
306 strcpy(dirpos, name);
307 if (lookup_media_dir(tmpdir, "Android") == 0
308 && lookup_media_dir(tmpdir, "data") == 0) {
309 //ALOGI("adding cache files from %s\n", tmpdir);
310 add_cache_files(cache, tmpdir, "cache");
311 }
312 } else {
313 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
314 }
315 }
316 }
317 closedir(d);
318 }
319
320 clear_cache_files(cache, free_size);
321 finish_cache_collection(cache);
322
323 return data_disk_free() >= free_size ? 0 : -1;
324}
325
Narayan Kamath1b400322014-04-11 13:17:00 +0100326int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700327{
328 char src_dex[PKG_PATH_MAX];
329 char dst_dex[PKG_PATH_MAX];
330
Jeff Sharkey770180a2014-09-08 17:14:26 -0700331 if (validate_apk_path(src)) {
332 ALOGE("invalid apk path '%s' (bad prefix)\n", src);
333 return -1;
334 }
335 if (validate_apk_path(dst)) {
336 ALOGE("invalid apk path '%s' (bad prefix)\n", dst);
337 return -1;
338 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700339
Narayan Kamath1b400322014-04-11 13:17:00 +0100340 if (create_cache_path(src_dex, src, instruction_set)) return -1;
341 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700342
343 ALOGV("move %s -> %s\n", src_dex, dst_dex);
344 if (rename(src_dex, dst_dex) < 0) {
345 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
346 return -1;
347 } else {
348 return 0;
349 }
350}
351
Narayan Kamath1b400322014-04-11 13:17:00 +0100352int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700353{
354 char dex_path[PKG_PATH_MAX];
355
Jeff Sharkey770180a2014-09-08 17:14:26 -0700356 if (validate_apk_path(path) && validate_system_app_path(path)) {
357 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
358 return -1;
359 }
360
Narayan Kamath1b400322014-04-11 13:17:00 +0100361 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700362
363 ALOGV("unlink %s\n", dex_path);
364 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700365 if (errno != ENOENT) {
366 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
367 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700368 return -1;
369 } else {
370 return 0;
371 }
372}
373
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700374int get_size(const char *uuid, const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700375 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100376 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
377 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700378{
379 DIR *d;
380 int dfd;
381 struct dirent *de;
382 struct stat s;
383 char path[PKG_PATH_MAX];
384
385 int64_t codesize = 0;
386 int64_t datasize = 0;
387 int64_t cachesize = 0;
388 int64_t asecsize = 0;
389
390 /* count the source apk as code -- but only if it's not
391 * on the /system partition and its not on the sdcard.
392 */
393 if (validate_system_app_path(apkpath) &&
394 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
395 if (stat(apkpath, &s) == 0) {
396 codesize += stat_size(&s);
397 }
398 }
399 /* count the forward locked apk as code if it is given
400 */
401 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
402 if (stat(fwdlock_apkpath, &s) == 0) {
403 codesize += stat_size(&s);
404 }
405 }
406 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100407 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700408 if (stat(path, &s) == 0) {
409 codesize += stat_size(&s);
410 }
411 }
412
413 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700414 if (libdirpath != NULL && libdirpath[0] != '!') {
415 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700416 if (d != NULL) {
417 dfd = dirfd(d);
418 codesize += calculate_dir_size(dfd);
419 closedir(d);
420 }
421 }
422
423 /* compute asec size if it is given
424 */
425 if (asecpath != NULL && asecpath[0] != '!') {
426 if (stat(asecpath, &s) == 0) {
427 asecsize += stat_size(&s);
428 }
429 }
430
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700431 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
432 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700433
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700434 d = opendir(pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700435 if (d == NULL) {
436 goto done;
437 }
438 dfd = dirfd(d);
439
440 /* most stuff in the pkgdir is data, except for the "cache"
441 * directory and below, which is cache, and the "lib" directory
442 * and below, which is code...
443 */
444 while ((de = readdir(d))) {
445 const char *name = de->d_name;
446
447 if (de->d_type == DT_DIR) {
448 int subfd;
449 int64_t statsize = 0;
450 int64_t dirsize = 0;
451 /* always skip "." and ".." */
452 if (name[0] == '.') {
453 if (name[1] == 0) continue;
454 if ((name[1] == '.') && (name[2] == 0)) continue;
455 }
456 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
457 statsize = stat_size(&s);
458 }
459 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
460 if (subfd >= 0) {
461 dirsize = calculate_dir_size(subfd);
462 }
463 if(!strcmp(name,"lib")) {
464 codesize += dirsize + statsize;
465 } else if(!strcmp(name,"cache")) {
466 cachesize += dirsize + statsize;
467 } else {
468 datasize += dirsize + statsize;
469 }
470 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
471 // This is the symbolic link to the application's library
472 // code. We'll count this as code instead of data, since
473 // it is not something that the app creates.
474 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
475 codesize += stat_size(&s);
476 }
477 } else {
478 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
479 datasize += stat_size(&s);
480 }
481 }
482 }
483 closedir(d);
484done:
485 *_codesize = codesize;
486 *_datasize = datasize;
487 *_cachesize = cachesize;
488 *_asecsize = asecsize;
489 return 0;
490}
491
Narayan Kamath1b400322014-04-11 13:17:00 +0100492int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700493{
494 char *tmp;
495 int srclen;
496 int dstlen;
497
498 srclen = strlen(src);
499
500 /* demand that we are an absolute path */
501 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
502 return -1;
503 }
504
505 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
506 return -1;
507 }
508
Dave Allisond9370732014-01-30 14:19:23 -0800509 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100510 strlen(instruction_set) +
511 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800512
Mike Lockwood94afecf2012-10-24 10:45:23 -0700513 if (dstlen > PKG_PATH_MAX) {
514 return -1;
515 }
516
Narayan Kamath1b400322014-04-11 13:17:00 +0100517 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700518 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100519 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700520 src + 1, /* skip the leading / */
521 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800522
Narayan Kamath1b400322014-04-11 13:17:00 +0100523 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700524 if (*tmp == '/') {
525 *tmp = '@';
526 }
527 }
528
529 return 0;
530}
531
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700532static int split_count(const char *str)
533{
534 char *ctx;
535 int count = 0;
536 char buf[PROPERTY_VALUE_MAX];
537
538 strncpy(buf, str, sizeof(buf));
539 char *pBuf = buf;
540
541 while(strtok_r(pBuf, " ", &ctx) != NULL) {
542 count++;
543 pBuf = NULL;
544 }
545
546 return count;
547}
548
neo.chae14e084d2015-01-07 18:46:13 +0900549static int split(char *buf, const char **argv)
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700550{
551 char *ctx;
552 int count = 0;
553 char *tok;
554 char *pBuf = buf;
555
556 while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
557 argv[count++] = tok;
558 pBuf = NULL;
559 }
560
561 return count;
562}
563
Alex Light7365a102014-07-21 12:23:48 -0700564static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700565 const char* output_file_name, const char *pkgname __unused, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700566{
567 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100568 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700569
570 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
571 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
572 ALOGE("Instruction set %s longer than max length of %d",
573 instruction_set, MAX_INSTRUCTION_SET_LEN);
574 return;
575 }
576
577 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
578 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
579 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
580 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
581 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
582 // The caller has already gotten all the locks we need.
583 const char* no_lock_arg = "--no-lock-output";
584 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
585 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
586 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700587 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700588 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
589
590 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
591 char* argv[7];
592 argv[0] = (char*) PATCHOAT_BIN;
593 argv[1] = (char*) patched_image_location_arg;
594 argv[2] = (char*) no_lock_arg;
595 argv[3] = instruction_set_arg;
596 argv[4] = output_oat_fd_arg;
597 argv[5] = input_oat_fd_arg;
598 argv[6] = NULL;
599
600 execv(PATCHOAT_BIN, (char* const *)argv);
601 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
602}
603
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700604static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Andreas Gampee1c01352014-12-10 16:41:11 -0800605 const char* output_file_name, int swap_fd, const char *pkgname, const char *instruction_set,
Andreas Gampe598c25e2015-03-03 09:15:06 -0800606 bool vm_safe_mode, bool debuggable)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700607{
Calin Juravle8fc73152014-08-19 18:48:50 +0100608 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
609
610 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
611 ALOGE("Instruction set %s longer than max length of %d",
612 instruction_set, MAX_INSTRUCTION_SET_LEN);
613 return;
614 }
615
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700616 char prop_buf[PROPERTY_VALUE_MAX];
617 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
618
619 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
620 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
621
622 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
623 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
624
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700625 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
626 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
627 dex2oat_compiler_filter_flag, NULL) > 0;
628
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700629 char dex2oat_threads_buf[PROPERTY_VALUE_MAX];
630 bool have_dex2oat_threads_flag = property_get("dalvik.vm.dex2oat-threads", dex2oat_threads_buf,
631 NULL) > 0;
632 char dex2oat_threads_arg[PROPERTY_VALUE_MAX + 2];
633 if (have_dex2oat_threads_flag) {
634 sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf);
635 }
636
Calin Juravle8fc73152014-08-19 18:48:50 +0100637 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
638 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
639 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
640 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
641 dex2oat_isa_features, NULL) > 0;
642
Ian Rogers16a95b22014-11-08 16:58:13 -0800643 char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
644 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
645 char dex2oat_isa_variant[PROPERTY_VALUE_MAX];
646 bool have_dex2oat_isa_variant = property_get(dex2oat_isa_variant_key,
647 dex2oat_isa_variant, NULL) > 0;
648
neo.chae14e084d2015-01-07 18:46:13 +0900649 const char *dex2oat_norelocation = "-Xnorelocate";
650 bool have_dex2oat_relocation_skip_flag = false;
651
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800652 char dex2oat_flags[PROPERTY_VALUE_MAX];
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700653 int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags",
654 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800655 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
656
Brian Carlstrom538998f2014-07-30 14:37:11 -0700657 // If we booting without the real /data, don't spend time compiling.
658 char vold_decrypt[PROPERTY_VALUE_MAX];
659 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
660 bool skip_compilation = (have_vold_decrypt &&
661 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
662 (strcmp(vold_decrypt, "1") == 0)));
663
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700664 char use_jit_property[PROPERTY_VALUE_MAX];
665 bool have_jit_property = property_get("debug.usejit", use_jit_property, NULL) > 0;
666 bool use_jit = have_jit_property && strcmp(use_jit_property, "true") == 0;
667
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700668 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700669
Brian Carlstrom53e07762014-06-27 14:15:19 -0700670 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700671
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700672 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100673
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700674 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
675 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
676 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700677 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100678 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Ian Rogers16a95b22014-11-08 16:58:13 -0800679 char instruction_set_variant_arg[strlen("--instruction-set-variant=") + PROPERTY_VALUE_MAX];
Calin Juravle8fc73152014-08-19 18:48:50 +0100680 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100681 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
682 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700683 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
684 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700685 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800686 bool have_dex2oat_swap_fd = false;
687 char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700688
689 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
690 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
691 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
692 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100693 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Ian Rogers16a95b22014-11-08 16:58:13 -0800694 sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
Calin Juravle8fc73152014-08-19 18:48:50 +0100695 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Andreas Gampee1c01352014-12-10 16:41:11 -0800696 if (swap_fd >= 0) {
697 have_dex2oat_swap_fd = true;
698 sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
699 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100700
Calin Juravle4fdff462014-06-06 16:58:43 +0100701 bool have_profile_file = false;
702 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100703 if (profiler && (strcmp(pkgname, "*") != 0)) {
704 char profile_file[PKG_PATH_MAX];
705 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800706 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100707 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100708 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100709 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100710 have_profile_file = true;
711 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
712 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
713 "--top-k-profile-threshold=%s", prop_buf);
714 have_top_k_profile_threshold = true;
715 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100716 }
Dave Allisond9370732014-01-30 14:19:23 -0800717 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700718
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700719 if (have_dex2oat_Xms_flag) {
720 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
721 }
722 if (have_dex2oat_Xmx_flag) {
723 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
724 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700725 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700726 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700727 have_dex2oat_compiler_filter_flag = true;
neo.chae14e084d2015-01-07 18:46:13 +0900728 have_dex2oat_relocation_skip_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100729 } else if (vm_safe_mode) {
730 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100731 have_dex2oat_compiler_filter_flag = true;
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700732 } else if (use_jit) {
733 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-at-runtime");
734 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700735 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700736 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
737 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700738
Andreas Gampe598c25e2015-03-03 09:15:06 -0800739 // Check whether all apps should be compiled debuggable.
740 if (!debuggable) {
741 debuggable =
742 (property_get("dalvik.vm.always_debuggable", prop_buf, "0") > 0) &&
743 (prop_buf[0] == '1');
744 }
745
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700746 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100747
neo.chae14e084d2015-01-07 18:46:13 +0900748 const char* argv[7 // program name, mandatory arguments and the final NULL
749 + (have_dex2oat_isa_variant ? 1 : 0)
750 + (have_dex2oat_isa_features ? 1 : 0)
751 + (have_profile_file ? 1 : 0)
752 + (have_top_k_profile_threshold ? 1 : 0)
753 + (have_dex2oat_Xms_flag ? 2 : 0)
754 + (have_dex2oat_Xmx_flag ? 2 : 0)
755 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700756 + (have_dex2oat_threads_flag ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900757 + (have_dex2oat_swap_fd ? 1 : 0)
758 + (have_dex2oat_relocation_skip_flag ? 2 : 0)
Andreas Gampe598c25e2015-03-03 09:15:06 -0800759 + (debuggable ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900760 + dex2oat_flags_count];
Calin Juravle4fdff462014-06-06 16:58:43 +0100761 int i = 0;
neo.chae14e084d2015-01-07 18:46:13 +0900762 argv[i++] = DEX2OAT_BIN;
Calin Juravle4fdff462014-06-06 16:58:43 +0100763 argv[i++] = zip_fd_arg;
764 argv[i++] = zip_location_arg;
765 argv[i++] = oat_fd_arg;
766 argv[i++] = oat_location_arg;
767 argv[i++] = instruction_set_arg;
Ian Rogers16a95b22014-11-08 16:58:13 -0800768 if (have_dex2oat_isa_variant) {
769 argv[i++] = instruction_set_variant_arg;
770 }
Calin Juravle8fc73152014-08-19 18:48:50 +0100771 if (have_dex2oat_isa_features) {
772 argv[i++] = instruction_set_features_arg;
773 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100774 if (have_profile_file) {
775 argv[i++] = profile_file_arg;
776 }
777 if (have_top_k_profile_threshold) {
778 argv[i++] = top_k_profile_threshold_arg;
779 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700780 if (have_dex2oat_Xms_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900781 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700782 argv[i++] = dex2oat_Xms_arg;
783 }
784 if (have_dex2oat_Xmx_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900785 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700786 argv[i++] = dex2oat_Xmx_arg;
787 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700788 if (have_dex2oat_compiler_filter_flag) {
789 argv[i++] = dex2oat_compiler_filter_arg;
790 }
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700791 if (have_dex2oat_threads_flag) {
792 argv[i++] = dex2oat_threads_arg;
793 }
Andreas Gampee1c01352014-12-10 16:41:11 -0800794 if (have_dex2oat_swap_fd) {
795 argv[i++] = dex2oat_swap_fd;
796 }
Andreas Gampe598c25e2015-03-03 09:15:06 -0800797 if (debuggable) {
798 argv[i++] = "--debuggable";
799 }
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700800 if (dex2oat_flags_count) {
801 i += split(dex2oat_flags, argv + i);
Calin Juravle4fdff462014-06-06 16:58:43 +0100802 }
neo.chae14e084d2015-01-07 18:46:13 +0900803 if (have_dex2oat_relocation_skip_flag) {
804 argv[i++] = RUNTIME_ARG;
805 argv[i++] = dex2oat_norelocation;
806 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700807 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100808 argv[i] = NULL;
809
neo.chae14e084d2015-01-07 18:46:13 +0900810 execv(DEX2OAT_BIN, (char * const *)argv);
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700811 ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700812}
813
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100814static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700815{
816 int status;
817 pid_t got_pid;
818
Mike Lockwood94afecf2012-10-24 10:45:23 -0700819 while (1) {
820 got_pid = waitpid(pid, &status, 0);
821 if (got_pid == -1 && errno == EINTR) {
822 printf("waitpid interrupted, retrying\n");
823 } else {
824 break;
825 }
826 }
827 if (got_pid != pid) {
828 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
829 (int) pid, (int) got_pid, strerror(errno));
830 return 1;
831 }
832
833 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700834 return 0;
835 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700836 return status; /* always nonzero */
837 }
838}
839
Andreas Gampee1c01352014-12-10 16:41:11 -0800840/*
841 * Whether dexopt should use a swap file when compiling an APK. If kAlwaysProvideSwapFile, do this
842 * on all devices (dex2oat will make a more informed decision itself, anyways). Otherwise, only do
843 * this on a low-mem device.
844 */
845static bool kAlwaysProvideSwapFile = true;
846
847static bool ShouldUseSwapFileForDexopt() {
848 if (kAlwaysProvideSwapFile) {
849 return true;
850 }
851
852 char low_mem_buf[PROPERTY_VALUE_MAX];
853 property_get("ro.config.low_ram", low_mem_buf, "");
854 return (strcmp(low_mem_buf, "true") == 0);
855}
856
Richard Uhler009b8772015-03-18 12:39:09 -0700857/*
858 * Computes the odex file for the given apk_path and instruction_set.
859 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
860 *
861 * Returns false if it failed to determine the odex file path.
862 */
863static bool calculate_odex_file_path(char path[PKG_PATH_MAX],
864 const char *apk_path,
865 const char *instruction_set)
866{
867 if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
868 + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
869 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
870 return false;
871 }
872
873 strcpy(path, apk_path);
874 char *end = strrchr(path, '/');
875 if (end == NULL) {
876 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
877 return false;
878 }
879 const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
880
881 strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0
882 strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
883 strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0
884 end = strrchr(path, '.');
885 if (end == NULL) {
886 ALOGE("apk_path '%s' has no extension.\n", apk_path);
887 return false;
888 }
889 strcpy(end + 1, "odex");
890 return true;
891}
892
Calin Juravleb1efac12014-08-21 19:05:20 +0100893int dexopt(const char *apk_path, uid_t uid, bool is_public,
Richard Uhlerc92fb622015-03-26 15:47:38 -0700894 const char *pkgname, const char *instruction_set, int dexopt_needed,
895 bool vm_safe_mode, bool debuggable, const char* oat_dir)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700896{
897 struct utimbuf ut;
Fyodor Kupolov26ff93c2015-04-02 16:59:10 -0700898 struct stat input_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700899 char out_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800900 char swap_file_name[PKG_PATH_MAX];
Alex Light7365a102014-07-21 12:23:48 -0700901 const char *input_file;
902 char in_odex_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800903 int res, input_fd=-1, out_fd=-1, swap_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700904
Andreas Gampee1c01352014-12-10 16:41:11 -0800905 // Early best-effort check whether we can fit the the path into our buffers.
906 // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
907 // without a swap file, if necessary.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700908 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800909 ALOGE("apk_path too long '%s'\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700910 return -1;
911 }
912
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800913 if (oat_dir != NULL && oat_dir[0] != '!') {
914 if (validate_apk_path(oat_dir)) {
915 ALOGE("invalid oat_dir '%s'\n", oat_dir);
916 return -1;
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800917 }
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800918 if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) {
919 return -1;
920 }
921 } else {
922 if (create_cache_path(out_path, apk_path, instruction_set)) {
923 return -1;
924 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700925 }
926
Richard Uhlerc92fb622015-03-26 15:47:38 -0700927 switch (dexopt_needed) {
928 case DEXOPT_DEX2OAT_NEEDED:
929 input_file = apk_path;
930 break;
931
932 case DEXOPT_PATCHOAT_NEEDED:
933 if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
934 return -1;
935 }
936 input_file = in_odex_path;
937 break;
938
939 case DEXOPT_SELF_PATCHOAT_NEEDED:
940 input_file = out_path;
941 break;
942
943 default:
944 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
945 exit(72);
Alex Light7365a102014-07-21 12:23:48 -0700946 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700947
Alex Light7365a102014-07-21 12:23:48 -0700948 memset(&input_stat, 0, sizeof(input_stat));
949 stat(input_file, &input_stat);
950
951 input_fd = open(input_file, O_RDONLY, 0);
952 if (input_fd < 0) {
953 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700954 return -1;
955 }
956
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700957 unlink(out_path);
958 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
959 if (out_fd < 0) {
960 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700961 goto fail;
962 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700963 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700964 S_IRUSR|S_IWUSR|S_IRGRP |
965 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700966 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700967 goto fail;
968 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700969 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
970 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700971 goto fail;
972 }
973
Dave Allisond9370732014-01-30 14:19:23 -0800974 // Create profile file if there is a package name present.
975 if (strcmp(pkgname, "*") != 0) {
976 create_profile_file(pkgname, uid);
977 }
978
Andreas Gampee1c01352014-12-10 16:41:11 -0800979 // Create a swap file if necessary.
Richard Uhlerc92fb622015-03-26 15:47:38 -0700980 if (ShouldUseSwapFileForDexopt()) {
Andreas Gampee1c01352014-12-10 16:41:11 -0800981 // Make sure there really is enough space.
982 size_t out_len = strlen(out_path);
983 if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
984 strcpy(swap_file_name, out_path);
985 strcpy(swap_file_name + strlen(out_path), ".swap");
986 unlink(swap_file_name);
987 swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600);
988 if (swap_fd < 0) {
989 // Could not create swap file. Optimistically go on and hope that we can compile
990 // without it.
991 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
992 } else {
993 // Immediately unlink. We don't really want to hit flash.
994 unlink(swap_file_name);
995 }
996 } else {
997 // Swap file path is too long. Try to run without.
998 ALOGE("installd could not create swap file for path %s during dexopt\n", out_path);
999 }
1000 }
Dave Allisond9370732014-01-30 14:19:23 -08001001
Alex Light7365a102014-07-21 12:23:48 -07001002 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001003
1004 pid_t pid;
1005 pid = fork();
1006 if (pid == 0) {
1007 /* child -- drop privileges before continuing */
1008 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001009 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001010 exit(64);
1011 }
1012 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001013 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001014 exit(65);
1015 }
1016 // drop capabilities
1017 struct __user_cap_header_struct capheader;
1018 struct __user_cap_data_struct capdata[2];
1019 memset(&capheader, 0, sizeof(capheader));
1020 memset(&capdata, 0, sizeof(capdata));
1021 capheader.version = _LINUX_CAPABILITY_VERSION_3;
1022 if (capset(&capheader, &capdata[0]) < 0) {
1023 ALOGE("capset failed: %s\n", strerror(errno));
1024 exit(66);
1025 }
Brian Carlstrom0378aaf2014-08-08 00:52:22 -07001026 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
1027 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
1028 exit(70);
1029 }
Igor Murashkin9e87a802014-11-05 15:21:12 -08001030 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
1031 ALOGE("setpriority failed: %s\n", strerror(errno));
1032 exit(71);
1033 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001034 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
1035 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001036 exit(67);
1037 }
1038
Richard Uhlerc92fb622015-03-26 15:47:38 -07001039 if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED
1040 || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) {
Andreas Gampebd872e42014-12-15 11:41:11 -08001041 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001042 } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001043 const char *input_file_name = strrchr(input_file, '/');
1044 if (input_file_name == NULL) {
1045 input_file_name = input_file;
1046 } else {
1047 input_file_name++;
1048 }
1049 run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname,
1050 instruction_set, vm_safe_mode, debuggable);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001051 } else {
1052 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1053 exit(73);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001054 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001055 exit(68); /* only get here on exec failure */
1056 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001057 res = wait_child(pid);
1058 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001059 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001060 } else {
Alex Light7365a102014-07-21 12:23:48 -07001061 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001062 goto fail;
1063 }
1064 }
1065
Alex Light7365a102014-07-21 12:23:48 -07001066 ut.actime = input_stat.st_atime;
1067 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001068 utime(out_path, &ut);
1069
1070 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001071 close(input_fd);
Andreas Gampee1c01352014-12-10 16:41:11 -08001072 if (swap_fd != -1) {
1073 close(swap_fd);
1074 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001075 return 0;
1076
1077fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001078 if (out_fd >= 0) {
1079 close(out_fd);
1080 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001081 }
Alex Light7365a102014-07-21 12:23:48 -07001082 if (input_fd >= 0) {
1083 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001084 }
1085 return -1;
1086}
1087
Narayan Kamath091ea772014-11-10 15:03:46 +00001088int mark_boot_complete(const char* instruction_set)
1089{
1090 char boot_marker_path[PKG_PATH_MAX];
1091 sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set);
1092
1093 ALOGV("mark_boot_complete : %s", boot_marker_path);
1094 if (unlink(boot_marker_path) != 0) {
1095 ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1096 strerror(errno));
1097 return -1;
1098 }
1099
1100 return 0;
1101}
1102
Mike Lockwood94afecf2012-10-24 10:45:23 -07001103void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1104 struct stat* statbuf)
1105{
1106 while (path[basepos] != 0) {
1107 if (path[basepos] == '/') {
1108 path[basepos] = 0;
1109 if (lstat(path, statbuf) < 0) {
1110 ALOGV("Making directory: %s\n", path);
1111 if (mkdir(path, mode) == 0) {
1112 chown(path, uid, gid);
1113 } else {
1114 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1115 }
1116 }
1117 path[basepos] = '/';
1118 basepos++;
1119 }
1120 basepos++;
1121 }
1122}
1123
1124int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1125 int dstuid, int dstgid, struct stat* statbuf)
1126{
1127 DIR *d;
1128 struct dirent *de;
1129 int res;
1130
1131 int srcend = strlen(srcpath);
1132 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001133
Mike Lockwood94afecf2012-10-24 10:45:23 -07001134 if (lstat(srcpath, statbuf) < 0) {
1135 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1136 return 1;
1137 }
Dave Allisond9370732014-01-30 14:19:23 -08001138
Mike Lockwood94afecf2012-10-24 10:45:23 -07001139 if ((statbuf->st_mode&S_IFDIR) == 0) {
1140 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1141 dstuid, dstgid, statbuf);
1142 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1143 if (rename(srcpath, dstpath) >= 0) {
1144 if (chown(dstpath, dstuid, dstgid) < 0) {
1145 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1146 unlink(dstpath);
1147 return 1;
1148 }
1149 } else {
1150 ALOGW("Unable to rename %s to %s: %s\n",
1151 srcpath, dstpath, strerror(errno));
1152 return 1;
1153 }
1154 return 0;
1155 }
1156
1157 d = opendir(srcpath);
1158 if (d == NULL) {
1159 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1160 return 1;
1161 }
1162
1163 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001164
Mike Lockwood94afecf2012-10-24 10:45:23 -07001165 while ((de = readdir(d))) {
1166 const char *name = de->d_name;
1167 /* always skip "." and ".." */
1168 if (name[0] == '.') {
1169 if (name[1] == 0) continue;
1170 if ((name[1] == '.') && (name[2] == 0)) continue;
1171 }
Dave Allisond9370732014-01-30 14:19:23 -08001172
Mike Lockwood94afecf2012-10-24 10:45:23 -07001173 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1174 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1175 continue;
1176 }
Dave Allisond9370732014-01-30 14:19:23 -08001177
Mike Lockwood94afecf2012-10-24 10:45:23 -07001178 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1179 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1180 continue;
1181 }
Dave Allisond9370732014-01-30 14:19:23 -08001182
Mike Lockwood94afecf2012-10-24 10:45:23 -07001183 srcpath[srcend] = dstpath[dstend] = '/';
1184 strcpy(srcpath+srcend+1, name);
1185 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001186
Mike Lockwood94afecf2012-10-24 10:45:23 -07001187 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1188 res = 1;
1189 }
Dave Allisond9370732014-01-30 14:19:23 -08001190
Mike Lockwood94afecf2012-10-24 10:45:23 -07001191 // Note: we will be leaving empty directories behind in srcpath,
1192 // but that is okay, the package manager will be erasing all of the
1193 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001194
Mike Lockwood94afecf2012-10-24 10:45:23 -07001195 srcpath[srcend] = dstpath[dstend] = 0;
1196 }
Dave Allisond9370732014-01-30 14:19:23 -08001197
Mike Lockwood94afecf2012-10-24 10:45:23 -07001198 closedir(d);
1199 return res;
1200}
1201
1202int movefiles()
1203{
1204 DIR *d;
1205 int dfd, subfd;
1206 struct dirent *de;
1207 struct stat s;
1208 char buf[PKG_PATH_MAX+1];
1209 int bufp, bufe, bufi, readlen;
1210
1211 char srcpkg[PKG_NAME_MAX];
1212 char dstpkg[PKG_NAME_MAX];
1213 char srcpath[PKG_PATH_MAX];
1214 char dstpath[PKG_PATH_MAX];
1215 int dstuid=-1, dstgid=-1;
1216 int hasspace;
1217
1218 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1219 if (d == NULL) {
1220 goto done;
1221 }
1222 dfd = dirfd(d);
1223
1224 /* Iterate through all files in the directory, executing the
1225 * file movements requested there-in.
1226 */
1227 while ((de = readdir(d))) {
1228 const char *name = de->d_name;
1229
1230 if (de->d_type == DT_DIR) {
1231 continue;
1232 } else {
1233 subfd = openat(dfd, name, O_RDONLY);
1234 if (subfd < 0) {
1235 ALOGW("Unable to open update commands at %s%s\n",
1236 UPDATE_COMMANDS_DIR_PREFIX, name);
1237 continue;
1238 }
Dave Allisond9370732014-01-30 14:19:23 -08001239
Mike Lockwood94afecf2012-10-24 10:45:23 -07001240 bufp = 0;
1241 bufe = 0;
1242 buf[PKG_PATH_MAX] = 0;
1243 srcpkg[0] = dstpkg[0] = 0;
1244 while (1) {
1245 bufi = bufp;
1246 while (bufi < bufe && buf[bufi] != '\n') {
1247 bufi++;
1248 }
1249 if (bufi < bufe) {
1250 buf[bufi] = 0;
1251 ALOGV("Processing line: %s\n", buf+bufp);
1252 hasspace = 0;
1253 while (bufp < bufi && isspace(buf[bufp])) {
1254 hasspace = 1;
1255 bufp++;
1256 }
1257 if (buf[bufp] == '#' || bufp == bufi) {
1258 // skip comments and empty lines.
1259 } else if (hasspace) {
1260 if (dstpkg[0] == 0) {
1261 ALOGW("Path before package line in %s%s: %s\n",
1262 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1263 } else if (srcpkg[0] == 0) {
1264 // Skip -- source package no longer exists.
1265 } else {
1266 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1267 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1268 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1269 movefileordir(srcpath, dstpath,
1270 strlen(dstpath)-strlen(buf+bufp),
1271 dstuid, dstgid, &s);
1272 }
1273 }
1274 } else {
1275 char* div = strchr(buf+bufp, ':');
1276 if (div == NULL) {
1277 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1278 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1279 } else {
1280 *div = 0;
1281 div++;
1282 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1283 strcpy(dstpkg, buf+bufp);
1284 } else {
1285 srcpkg[0] = dstpkg[0] = 0;
1286 ALOGW("Package name too long in %s%s: %s\n",
1287 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1288 }
1289 if (strlen(div) < PKG_NAME_MAX) {
1290 strcpy(srcpkg, div);
1291 } else {
1292 srcpkg[0] = dstpkg[0] = 0;
1293 ALOGW("Package name too long in %s%s: %s\n",
1294 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1295 }
1296 if (srcpkg[0] != 0) {
1297 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1298 if (lstat(srcpath, &s) < 0) {
1299 // Package no longer exists -- skip.
1300 srcpkg[0] = 0;
1301 }
1302 } else {
1303 srcpkg[0] = 0;
1304 ALOGW("Can't create path %s in %s%s\n",
1305 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1306 }
1307 if (srcpkg[0] != 0) {
1308 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1309 if (lstat(dstpath, &s) == 0) {
1310 dstuid = s.st_uid;
1311 dstgid = s.st_gid;
1312 } else {
1313 // Destination package doesn't
1314 // exist... due to original-package,
1315 // this is normal, so don't be
1316 // noisy about it.
1317 srcpkg[0] = 0;
1318 }
1319 } else {
1320 srcpkg[0] = 0;
1321 ALOGW("Can't create path %s in %s%s\n",
1322 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1323 }
1324 }
1325 ALOGV("Transfering from %s to %s: uid=%d\n",
1326 srcpkg, dstpkg, dstuid);
1327 }
1328 }
1329 }
1330 bufp = bufi+1;
1331 } else {
1332 if (bufp == 0) {
1333 if (bufp < bufe) {
1334 ALOGW("Line too long in %s%s, skipping: %s\n",
1335 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1336 }
1337 } else if (bufp < bufe) {
1338 memcpy(buf, buf+bufp, bufe-bufp);
1339 bufe -= bufp;
1340 bufp = 0;
1341 }
1342 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1343 if (readlen < 0) {
1344 ALOGW("Failure reading update commands in %s%s: %s\n",
1345 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1346 break;
1347 } else if (readlen == 0) {
1348 break;
1349 }
1350 bufe += readlen;
1351 buf[bufe] = 0;
1352 ALOGV("Read buf: %s\n", buf);
1353 }
1354 }
1355 close(subfd);
1356 }
1357 }
1358 closedir(d);
1359done:
1360 return 0;
1361}
1362
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001363int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001364{
Mike Lockwood94afecf2012-10-24 10:45:23 -07001365 struct stat s, libStat;
1366 int rc = 0;
1367
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001368 std::string _pkgdir(create_package_data_path(uuid, pkgname, userId));
1369 std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
1370
1371 const char* pkgdir = _pkgdir.c_str();
1372 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001373
1374 if (stat(pkgdir, &s) < 0) return -1;
1375
1376 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1377 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1378 return -1;
1379 }
1380
1381 if (chmod(pkgdir, 0700) < 0) {
1382 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1383 rc = -1;
1384 goto out;
1385 }
1386
1387 if (lstat(libsymlink, &libStat) < 0) {
1388 if (errno != ENOENT) {
1389 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1390 rc = -1;
1391 goto out;
1392 }
1393 } else {
1394 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001395 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001396 rc = -1;
1397 goto out;
1398 }
1399 } else if (S_ISLNK(libStat.st_mode)) {
1400 if (unlink(libsymlink) < 0) {
1401 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1402 rc = -1;
1403 goto out;
1404 }
1405 }
1406 }
1407
1408 if (symlink(asecLibDir, libsymlink) < 0) {
1409 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1410 strerror(errno));
1411 rc = -errno;
1412 goto out;
1413 }
1414
1415out:
1416 if (chmod(pkgdir, s.st_mode) < 0) {
1417 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1418 rc = -errno;
1419 }
1420
1421 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1422 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1423 return -errno;
1424 }
1425
1426 return rc;
1427}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001428
1429static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1430{
1431 static const char *IDMAP_BIN = "/system/bin/idmap";
1432 static const size_t MAX_INT_LEN = 32;
1433 char idmap_str[MAX_INT_LEN];
1434
1435 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1436
1437 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1438 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1439}
1440
1441// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1442// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1443static int flatten_path(const char *prefix, const char *suffix,
1444 const char *overlay_path, char *idmap_path, size_t N)
1445{
1446 if (overlay_path == NULL || idmap_path == NULL) {
1447 return -1;
1448 }
1449 const size_t len_overlay_path = strlen(overlay_path);
1450 // will access overlay_path + 1 further below; requires absolute path
1451 if (len_overlay_path < 2 || *overlay_path != '/') {
1452 return -1;
1453 }
1454 const size_t len_idmap_root = strlen(prefix);
1455 const size_t len_suffix = strlen(suffix);
1456 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1457 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1458 // additions below would cause overflow
1459 return -1;
1460 }
1461 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1462 return -1;
1463 }
1464 memset(idmap_path, 0, N);
1465 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1466 char *ch = idmap_path + len_idmap_root;
1467 while (*ch != '\0') {
1468 if (*ch == '/') {
1469 *ch = '@';
1470 }
1471 ++ch;
1472 }
1473 return 0;
1474}
1475
1476int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1477{
1478 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1479
1480 int idmap_fd = -1;
1481 char idmap_path[PATH_MAX];
1482
1483 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1484 idmap_path, sizeof(idmap_path)) == -1) {
1485 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1486 goto fail;
1487 }
1488
1489 unlink(idmap_path);
1490 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1491 if (idmap_fd < 0) {
1492 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1493 goto fail;
1494 }
1495 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1496 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1497 goto fail;
1498 }
1499 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1500 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1501 goto fail;
1502 }
1503
1504 pid_t pid;
1505 pid = fork();
1506 if (pid == 0) {
1507 /* child -- drop privileges before continuing */
1508 if (setgid(uid) != 0) {
1509 ALOGE("setgid(%d) failed during idmap\n", uid);
1510 exit(1);
1511 }
1512 if (setuid(uid) != 0) {
1513 ALOGE("setuid(%d) failed during idmap\n", uid);
1514 exit(1);
1515 }
1516 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1517 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1518 exit(1);
1519 }
1520
1521 run_idmap(target_apk, overlay_apk, idmap_fd);
1522 exit(1); /* only if exec call to idmap failed */
1523 } else {
1524 int status = wait_child(pid);
1525 if (status != 0) {
1526 ALOGE("idmap failed, status=0x%04x\n", status);
1527 goto fail;
1528 }
1529 }
1530
1531 close(idmap_fd);
1532 return 0;
1533fail:
1534 if (idmap_fd >= 0) {
1535 close(idmap_fd);
1536 unlink(idmap_path);
1537 }
1538 return -1;
1539}
Robert Craige9887e42014-02-20 10:25:56 -05001540
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001541// TODO: extend to know about other volumes
1542int restorecon_data(const char *uuid, const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001543{
Robert Craigda30dc72014-03-27 10:21:12 -04001544 struct dirent *entry;
1545 DIR *d;
1546 struct stat s;
1547 char *userdir;
1548 char *primarydir;
1549 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001550 int ret = 0;
1551
Robert Craigda30dc72014-03-27 10:21:12 -04001552 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1553 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1554
1555 if (!pkgName || !seinfo) {
1556 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001557 return -1;
1558 }
1559
Robert Craigda30dc72014-03-27 10:21:12 -04001560 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1561 return -1;
1562 }
1563
1564 // Relabel for primary user.
1565 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1566 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001567 ret |= -1;
1568 }
1569
Robert Craigda30dc72014-03-27 10:21:12 -04001570 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1571 free(primarydir);
1572 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001573 }
1574
Robert Craigda30dc72014-03-27 10:21:12 -04001575 // Relabel package directory for all secondary users.
1576 d = opendir(userdir);
1577 if (d == NULL) {
1578 free(primarydir);
1579 free(userdir);
1580 return -1;
1581 }
1582
1583 while ((entry = readdir(d))) {
1584 if (entry->d_type != DT_DIR) {
1585 continue;
1586 }
1587
1588 const char *user = entry->d_name;
1589 // Ignore "." and ".."
1590 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1591 continue;
1592 }
1593
1594 // user directories start with a number
1595 if (user[0] < '0' || user[0] > '9') {
1596 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1597 continue;
1598 }
1599
1600 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1601 continue;
1602 }
1603
1604 if (stat(pkgdir, &s) < 0) {
1605 free(pkgdir);
1606 continue;
1607 }
1608
Stephen Smalley8ac2a642014-09-08 15:51:55 -04001609 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, s.st_uid, flags) < 0) {
Robert Craigda30dc72014-03-27 10:21:12 -04001610 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1611 ret |= -1;
1612 }
1613 free(pkgdir);
1614 }
1615
1616 closedir(d);
1617 free(primarydir);
1618 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001619 return ret;
1620}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001621
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001622int create_oat_dir(const char* oat_dir, const char* instruction_set)
1623{
1624 char oat_instr_dir[PKG_PATH_MAX];
1625
1626 if (validate_apk_path(oat_dir)) {
1627 ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
1628 return -1;
1629 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001630 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001631 return -1;
1632 }
1633 if (selinux_android_restorecon(oat_dir, 0)) {
1634 ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
1635 return -1;
1636 }
1637 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001638 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001639 return -1;
1640 }
1641 return 0;
1642}
1643
1644int rm_package_dir(const char* apk_path)
1645{
1646 if (validate_apk_path(apk_path)) {
1647 ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
1648 return -1;
1649 }
1650 return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */);
1651}
1652
1653int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
1654 const char *instruction_set) {
1655 char *file_name_start;
1656 char *file_name_end;
1657
1658 file_name_start = strrchr(apk_path, '/');
1659 if (file_name_start == NULL) {
1660 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
1661 return -1;
1662 }
1663 file_name_end = strrchr(apk_path, '.');
1664 if (file_name_end < file_name_start) {
1665 ALOGE("apk_path '%s' has no extension\n", apk_path);
1666 return -1;
1667 }
1668
1669 // Calculate file_name
1670 int file_name_len = file_name_end - file_name_start - 1;
1671 char file_name[file_name_len + 1];
1672 memcpy(file_name, file_name_start + 1, file_name_len);
1673 file_name[file_name_len] = '\0';
1674
1675 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
1676 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
1677 return 0;
1678}