blob: 0bc1393733c8c5d174cda1ae1f9362caf403d71e [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,
Alex Light7365a102014-07-21 12:23:48 -0700894 const char *pkgname, const char *instruction_set,
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800895 bool vm_safe_mode, bool is_patchoat, 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
Alex Light7365a102014-07-21 12:23:48 -0700927 if (is_patchoat) {
Richard Uhler009b8772015-03-18 12:39:09 -0700928 if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
929 return -1;
Alex Light7365a102014-07-21 12:23:48 -0700930 }
Alex Light7365a102014-07-21 12:23:48 -0700931 input_file = in_odex_path;
932 } else {
933 input_file = apk_path;
934 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700935
Alex Light7365a102014-07-21 12:23:48 -0700936 memset(&input_stat, 0, sizeof(input_stat));
937 stat(input_file, &input_stat);
938
939 input_fd = open(input_file, O_RDONLY, 0);
940 if (input_fd < 0) {
941 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700942 return -1;
943 }
944
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700945 unlink(out_path);
946 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
947 if (out_fd < 0) {
948 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700949 goto fail;
950 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700951 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700952 S_IRUSR|S_IWUSR|S_IRGRP |
953 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700954 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700955 goto fail;
956 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700957 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
958 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700959 goto fail;
960 }
961
Dave Allisond9370732014-01-30 14:19:23 -0800962 // Create profile file if there is a package name present.
963 if (strcmp(pkgname, "*") != 0) {
964 create_profile_file(pkgname, uid);
965 }
966
Andreas Gampee1c01352014-12-10 16:41:11 -0800967 // Create a swap file if necessary.
968 if (!is_patchoat && ShouldUseSwapFileForDexopt()) {
969 // Make sure there really is enough space.
970 size_t out_len = strlen(out_path);
971 if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
972 strcpy(swap_file_name, out_path);
973 strcpy(swap_file_name + strlen(out_path), ".swap");
974 unlink(swap_file_name);
975 swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600);
976 if (swap_fd < 0) {
977 // Could not create swap file. Optimistically go on and hope that we can compile
978 // without it.
979 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
980 } else {
981 // Immediately unlink. We don't really want to hit flash.
982 unlink(swap_file_name);
983 }
984 } else {
985 // Swap file path is too long. Try to run without.
986 ALOGE("installd could not create swap file for path %s during dexopt\n", out_path);
987 }
988 }
Dave Allisond9370732014-01-30 14:19:23 -0800989
Alex Light7365a102014-07-21 12:23:48 -0700990 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700991
992 pid_t pid;
993 pid = fork();
994 if (pid == 0) {
995 /* child -- drop privileges before continuing */
996 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700997 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700998 exit(64);
999 }
1000 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001001 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001002 exit(65);
1003 }
1004 // drop capabilities
1005 struct __user_cap_header_struct capheader;
1006 struct __user_cap_data_struct capdata[2];
1007 memset(&capheader, 0, sizeof(capheader));
1008 memset(&capdata, 0, sizeof(capdata));
1009 capheader.version = _LINUX_CAPABILITY_VERSION_3;
1010 if (capset(&capheader, &capdata[0]) < 0) {
1011 ALOGE("capset failed: %s\n", strerror(errno));
1012 exit(66);
1013 }
Brian Carlstrom0378aaf2014-08-08 00:52:22 -07001014 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
1015 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
1016 exit(70);
1017 }
Igor Murashkin9e87a802014-11-05 15:21:12 -08001018 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
1019 ALOGE("setpriority failed: %s\n", strerror(errno));
1020 exit(71);
1021 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001022 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
1023 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001024 exit(67);
1025 }
1026
Andreas Gampebd872e42014-12-15 11:41:11 -08001027 if (is_patchoat) {
1028 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001029 } else {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001030 const char *input_file_name = strrchr(input_file, '/');
1031 if (input_file_name == NULL) {
1032 input_file_name = input_file;
1033 } else {
1034 input_file_name++;
1035 }
1036 run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname,
1037 instruction_set, vm_safe_mode, debuggable);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001038 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001039 exit(68); /* only get here on exec failure */
1040 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001041 res = wait_child(pid);
1042 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001043 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001044 } else {
Alex Light7365a102014-07-21 12:23:48 -07001045 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001046 goto fail;
1047 }
1048 }
1049
Alex Light7365a102014-07-21 12:23:48 -07001050 ut.actime = input_stat.st_atime;
1051 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001052 utime(out_path, &ut);
1053
1054 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001055 close(input_fd);
Andreas Gampee1c01352014-12-10 16:41:11 -08001056 if (swap_fd != -1) {
1057 close(swap_fd);
1058 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001059 return 0;
1060
1061fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001062 if (out_fd >= 0) {
1063 close(out_fd);
1064 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001065 }
Alex Light7365a102014-07-21 12:23:48 -07001066 if (input_fd >= 0) {
1067 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001068 }
1069 return -1;
1070}
1071
Narayan Kamath091ea772014-11-10 15:03:46 +00001072int mark_boot_complete(const char* instruction_set)
1073{
1074 char boot_marker_path[PKG_PATH_MAX];
1075 sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set);
1076
1077 ALOGV("mark_boot_complete : %s", boot_marker_path);
1078 if (unlink(boot_marker_path) != 0) {
1079 ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1080 strerror(errno));
1081 return -1;
1082 }
1083
1084 return 0;
1085}
1086
Mike Lockwood94afecf2012-10-24 10:45:23 -07001087void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1088 struct stat* statbuf)
1089{
1090 while (path[basepos] != 0) {
1091 if (path[basepos] == '/') {
1092 path[basepos] = 0;
1093 if (lstat(path, statbuf) < 0) {
1094 ALOGV("Making directory: %s\n", path);
1095 if (mkdir(path, mode) == 0) {
1096 chown(path, uid, gid);
1097 } else {
1098 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1099 }
1100 }
1101 path[basepos] = '/';
1102 basepos++;
1103 }
1104 basepos++;
1105 }
1106}
1107
1108int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1109 int dstuid, int dstgid, struct stat* statbuf)
1110{
1111 DIR *d;
1112 struct dirent *de;
1113 int res;
1114
1115 int srcend = strlen(srcpath);
1116 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001117
Mike Lockwood94afecf2012-10-24 10:45:23 -07001118 if (lstat(srcpath, statbuf) < 0) {
1119 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1120 return 1;
1121 }
Dave Allisond9370732014-01-30 14:19:23 -08001122
Mike Lockwood94afecf2012-10-24 10:45:23 -07001123 if ((statbuf->st_mode&S_IFDIR) == 0) {
1124 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1125 dstuid, dstgid, statbuf);
1126 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1127 if (rename(srcpath, dstpath) >= 0) {
1128 if (chown(dstpath, dstuid, dstgid) < 0) {
1129 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1130 unlink(dstpath);
1131 return 1;
1132 }
1133 } else {
1134 ALOGW("Unable to rename %s to %s: %s\n",
1135 srcpath, dstpath, strerror(errno));
1136 return 1;
1137 }
1138 return 0;
1139 }
1140
1141 d = opendir(srcpath);
1142 if (d == NULL) {
1143 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1144 return 1;
1145 }
1146
1147 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001148
Mike Lockwood94afecf2012-10-24 10:45:23 -07001149 while ((de = readdir(d))) {
1150 const char *name = de->d_name;
1151 /* always skip "." and ".." */
1152 if (name[0] == '.') {
1153 if (name[1] == 0) continue;
1154 if ((name[1] == '.') && (name[2] == 0)) continue;
1155 }
Dave Allisond9370732014-01-30 14:19:23 -08001156
Mike Lockwood94afecf2012-10-24 10:45:23 -07001157 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1158 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1159 continue;
1160 }
Dave Allisond9370732014-01-30 14:19:23 -08001161
Mike Lockwood94afecf2012-10-24 10:45:23 -07001162 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1163 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1164 continue;
1165 }
Dave Allisond9370732014-01-30 14:19:23 -08001166
Mike Lockwood94afecf2012-10-24 10:45:23 -07001167 srcpath[srcend] = dstpath[dstend] = '/';
1168 strcpy(srcpath+srcend+1, name);
1169 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001170
Mike Lockwood94afecf2012-10-24 10:45:23 -07001171 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1172 res = 1;
1173 }
Dave Allisond9370732014-01-30 14:19:23 -08001174
Mike Lockwood94afecf2012-10-24 10:45:23 -07001175 // Note: we will be leaving empty directories behind in srcpath,
1176 // but that is okay, the package manager will be erasing all of the
1177 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001178
Mike Lockwood94afecf2012-10-24 10:45:23 -07001179 srcpath[srcend] = dstpath[dstend] = 0;
1180 }
Dave Allisond9370732014-01-30 14:19:23 -08001181
Mike Lockwood94afecf2012-10-24 10:45:23 -07001182 closedir(d);
1183 return res;
1184}
1185
1186int movefiles()
1187{
1188 DIR *d;
1189 int dfd, subfd;
1190 struct dirent *de;
1191 struct stat s;
1192 char buf[PKG_PATH_MAX+1];
1193 int bufp, bufe, bufi, readlen;
1194
1195 char srcpkg[PKG_NAME_MAX];
1196 char dstpkg[PKG_NAME_MAX];
1197 char srcpath[PKG_PATH_MAX];
1198 char dstpath[PKG_PATH_MAX];
1199 int dstuid=-1, dstgid=-1;
1200 int hasspace;
1201
1202 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1203 if (d == NULL) {
1204 goto done;
1205 }
1206 dfd = dirfd(d);
1207
1208 /* Iterate through all files in the directory, executing the
1209 * file movements requested there-in.
1210 */
1211 while ((de = readdir(d))) {
1212 const char *name = de->d_name;
1213
1214 if (de->d_type == DT_DIR) {
1215 continue;
1216 } else {
1217 subfd = openat(dfd, name, O_RDONLY);
1218 if (subfd < 0) {
1219 ALOGW("Unable to open update commands at %s%s\n",
1220 UPDATE_COMMANDS_DIR_PREFIX, name);
1221 continue;
1222 }
Dave Allisond9370732014-01-30 14:19:23 -08001223
Mike Lockwood94afecf2012-10-24 10:45:23 -07001224 bufp = 0;
1225 bufe = 0;
1226 buf[PKG_PATH_MAX] = 0;
1227 srcpkg[0] = dstpkg[0] = 0;
1228 while (1) {
1229 bufi = bufp;
1230 while (bufi < bufe && buf[bufi] != '\n') {
1231 bufi++;
1232 }
1233 if (bufi < bufe) {
1234 buf[bufi] = 0;
1235 ALOGV("Processing line: %s\n", buf+bufp);
1236 hasspace = 0;
1237 while (bufp < bufi && isspace(buf[bufp])) {
1238 hasspace = 1;
1239 bufp++;
1240 }
1241 if (buf[bufp] == '#' || bufp == bufi) {
1242 // skip comments and empty lines.
1243 } else if (hasspace) {
1244 if (dstpkg[0] == 0) {
1245 ALOGW("Path before package line in %s%s: %s\n",
1246 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1247 } else if (srcpkg[0] == 0) {
1248 // Skip -- source package no longer exists.
1249 } else {
1250 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1251 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1252 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1253 movefileordir(srcpath, dstpath,
1254 strlen(dstpath)-strlen(buf+bufp),
1255 dstuid, dstgid, &s);
1256 }
1257 }
1258 } else {
1259 char* div = strchr(buf+bufp, ':');
1260 if (div == NULL) {
1261 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1262 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1263 } else {
1264 *div = 0;
1265 div++;
1266 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1267 strcpy(dstpkg, buf+bufp);
1268 } else {
1269 srcpkg[0] = dstpkg[0] = 0;
1270 ALOGW("Package name too long in %s%s: %s\n",
1271 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1272 }
1273 if (strlen(div) < PKG_NAME_MAX) {
1274 strcpy(srcpkg, div);
1275 } else {
1276 srcpkg[0] = dstpkg[0] = 0;
1277 ALOGW("Package name too long in %s%s: %s\n",
1278 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1279 }
1280 if (srcpkg[0] != 0) {
1281 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1282 if (lstat(srcpath, &s) < 0) {
1283 // Package no longer exists -- skip.
1284 srcpkg[0] = 0;
1285 }
1286 } else {
1287 srcpkg[0] = 0;
1288 ALOGW("Can't create path %s in %s%s\n",
1289 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1290 }
1291 if (srcpkg[0] != 0) {
1292 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1293 if (lstat(dstpath, &s) == 0) {
1294 dstuid = s.st_uid;
1295 dstgid = s.st_gid;
1296 } else {
1297 // Destination package doesn't
1298 // exist... due to original-package,
1299 // this is normal, so don't be
1300 // noisy about it.
1301 srcpkg[0] = 0;
1302 }
1303 } else {
1304 srcpkg[0] = 0;
1305 ALOGW("Can't create path %s in %s%s\n",
1306 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1307 }
1308 }
1309 ALOGV("Transfering from %s to %s: uid=%d\n",
1310 srcpkg, dstpkg, dstuid);
1311 }
1312 }
1313 }
1314 bufp = bufi+1;
1315 } else {
1316 if (bufp == 0) {
1317 if (bufp < bufe) {
1318 ALOGW("Line too long in %s%s, skipping: %s\n",
1319 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1320 }
1321 } else if (bufp < bufe) {
1322 memcpy(buf, buf+bufp, bufe-bufp);
1323 bufe -= bufp;
1324 bufp = 0;
1325 }
1326 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1327 if (readlen < 0) {
1328 ALOGW("Failure reading update commands in %s%s: %s\n",
1329 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1330 break;
1331 } else if (readlen == 0) {
1332 break;
1333 }
1334 bufe += readlen;
1335 buf[bufe] = 0;
1336 ALOGV("Read buf: %s\n", buf);
1337 }
1338 }
1339 close(subfd);
1340 }
1341 }
1342 closedir(d);
1343done:
1344 return 0;
1345}
1346
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001347int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001348{
Mike Lockwood94afecf2012-10-24 10:45:23 -07001349 struct stat s, libStat;
1350 int rc = 0;
1351
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001352 std::string _pkgdir(create_package_data_path(uuid, pkgname, userId));
1353 std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
1354
1355 const char* pkgdir = _pkgdir.c_str();
1356 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001357
1358 if (stat(pkgdir, &s) < 0) return -1;
1359
1360 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1361 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1362 return -1;
1363 }
1364
1365 if (chmod(pkgdir, 0700) < 0) {
1366 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1367 rc = -1;
1368 goto out;
1369 }
1370
1371 if (lstat(libsymlink, &libStat) < 0) {
1372 if (errno != ENOENT) {
1373 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1374 rc = -1;
1375 goto out;
1376 }
1377 } else {
1378 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001379 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001380 rc = -1;
1381 goto out;
1382 }
1383 } else if (S_ISLNK(libStat.st_mode)) {
1384 if (unlink(libsymlink) < 0) {
1385 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1386 rc = -1;
1387 goto out;
1388 }
1389 }
1390 }
1391
1392 if (symlink(asecLibDir, libsymlink) < 0) {
1393 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1394 strerror(errno));
1395 rc = -errno;
1396 goto out;
1397 }
1398
1399out:
1400 if (chmod(pkgdir, s.st_mode) < 0) {
1401 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1402 rc = -errno;
1403 }
1404
1405 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1406 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1407 return -errno;
1408 }
1409
1410 return rc;
1411}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001412
1413static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1414{
1415 static const char *IDMAP_BIN = "/system/bin/idmap";
1416 static const size_t MAX_INT_LEN = 32;
1417 char idmap_str[MAX_INT_LEN];
1418
1419 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1420
1421 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1422 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1423}
1424
1425// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1426// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1427static int flatten_path(const char *prefix, const char *suffix,
1428 const char *overlay_path, char *idmap_path, size_t N)
1429{
1430 if (overlay_path == NULL || idmap_path == NULL) {
1431 return -1;
1432 }
1433 const size_t len_overlay_path = strlen(overlay_path);
1434 // will access overlay_path + 1 further below; requires absolute path
1435 if (len_overlay_path < 2 || *overlay_path != '/') {
1436 return -1;
1437 }
1438 const size_t len_idmap_root = strlen(prefix);
1439 const size_t len_suffix = strlen(suffix);
1440 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1441 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1442 // additions below would cause overflow
1443 return -1;
1444 }
1445 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1446 return -1;
1447 }
1448 memset(idmap_path, 0, N);
1449 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1450 char *ch = idmap_path + len_idmap_root;
1451 while (*ch != '\0') {
1452 if (*ch == '/') {
1453 *ch = '@';
1454 }
1455 ++ch;
1456 }
1457 return 0;
1458}
1459
1460int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1461{
1462 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1463
1464 int idmap_fd = -1;
1465 char idmap_path[PATH_MAX];
1466
1467 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1468 idmap_path, sizeof(idmap_path)) == -1) {
1469 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1470 goto fail;
1471 }
1472
1473 unlink(idmap_path);
1474 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1475 if (idmap_fd < 0) {
1476 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1477 goto fail;
1478 }
1479 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1480 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1481 goto fail;
1482 }
1483 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1484 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1485 goto fail;
1486 }
1487
1488 pid_t pid;
1489 pid = fork();
1490 if (pid == 0) {
1491 /* child -- drop privileges before continuing */
1492 if (setgid(uid) != 0) {
1493 ALOGE("setgid(%d) failed during idmap\n", uid);
1494 exit(1);
1495 }
1496 if (setuid(uid) != 0) {
1497 ALOGE("setuid(%d) failed during idmap\n", uid);
1498 exit(1);
1499 }
1500 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1501 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1502 exit(1);
1503 }
1504
1505 run_idmap(target_apk, overlay_apk, idmap_fd);
1506 exit(1); /* only if exec call to idmap failed */
1507 } else {
1508 int status = wait_child(pid);
1509 if (status != 0) {
1510 ALOGE("idmap failed, status=0x%04x\n", status);
1511 goto fail;
1512 }
1513 }
1514
1515 close(idmap_fd);
1516 return 0;
1517fail:
1518 if (idmap_fd >= 0) {
1519 close(idmap_fd);
1520 unlink(idmap_path);
1521 }
1522 return -1;
1523}
Robert Craige9887e42014-02-20 10:25:56 -05001524
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001525// TODO: extend to know about other volumes
1526int restorecon_data(const char *uuid, const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001527{
Robert Craigda30dc72014-03-27 10:21:12 -04001528 struct dirent *entry;
1529 DIR *d;
1530 struct stat s;
1531 char *userdir;
1532 char *primarydir;
1533 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001534 int ret = 0;
1535
Robert Craigda30dc72014-03-27 10:21:12 -04001536 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1537 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1538
1539 if (!pkgName || !seinfo) {
1540 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001541 return -1;
1542 }
1543
Robert Craigda30dc72014-03-27 10:21:12 -04001544 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1545 return -1;
1546 }
1547
1548 // Relabel for primary user.
1549 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1550 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001551 ret |= -1;
1552 }
1553
Robert Craigda30dc72014-03-27 10:21:12 -04001554 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1555 free(primarydir);
1556 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001557 }
1558
Robert Craigda30dc72014-03-27 10:21:12 -04001559 // Relabel package directory for all secondary users.
1560 d = opendir(userdir);
1561 if (d == NULL) {
1562 free(primarydir);
1563 free(userdir);
1564 return -1;
1565 }
1566
1567 while ((entry = readdir(d))) {
1568 if (entry->d_type != DT_DIR) {
1569 continue;
1570 }
1571
1572 const char *user = entry->d_name;
1573 // Ignore "." and ".."
1574 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1575 continue;
1576 }
1577
1578 // user directories start with a number
1579 if (user[0] < '0' || user[0] > '9') {
1580 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1581 continue;
1582 }
1583
1584 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1585 continue;
1586 }
1587
1588 if (stat(pkgdir, &s) < 0) {
1589 free(pkgdir);
1590 continue;
1591 }
1592
Stephen Smalley8ac2a642014-09-08 15:51:55 -04001593 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, s.st_uid, flags) < 0) {
Robert Craigda30dc72014-03-27 10:21:12 -04001594 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1595 ret |= -1;
1596 }
1597 free(pkgdir);
1598 }
1599
1600 closedir(d);
1601 free(primarydir);
1602 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001603 return ret;
1604}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001605
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001606int create_oat_dir(const char* oat_dir, const char* instruction_set)
1607{
1608 char oat_instr_dir[PKG_PATH_MAX];
1609
1610 if (validate_apk_path(oat_dir)) {
1611 ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
1612 return -1;
1613 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001614 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001615 return -1;
1616 }
1617 if (selinux_android_restorecon(oat_dir, 0)) {
1618 ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
1619 return -1;
1620 }
1621 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001622 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001623 return -1;
1624 }
1625 return 0;
1626}
1627
1628int rm_package_dir(const char* apk_path)
1629{
1630 if (validate_apk_path(apk_path)) {
1631 ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
1632 return -1;
1633 }
1634 return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */);
1635}
1636
1637int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
1638 const char *instruction_set) {
1639 char *file_name_start;
1640 char *file_name_end;
1641
1642 file_name_start = strrchr(apk_path, '/');
1643 if (file_name_start == NULL) {
1644 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
1645 return -1;
1646 }
1647 file_name_end = strrchr(apk_path, '.');
1648 if (file_name_end < file_name_start) {
1649 ALOGE("apk_path '%s' has no extension\n", apk_path);
1650 return -1;
1651 }
1652
1653 // Calculate file_name
1654 int file_name_len = file_name_end - file_name_start - 1;
1655 char file_name[file_name_len + 1];
1656 memcpy(file_name, file_name_start + 1, file_name_len);
1657 file_name[file_name_len] = '\0';
1658
1659 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
1660 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
1661 return 0;
1662}