blob: ea59f5b681eb1d71129da2bac3e67345ee0b377b [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
Jeff Sharkey41ea4242015-04-09 11:34:03 -070017#include "installd.h"
18
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070019#include <inttypes.h>
Nick Kralevichd7471292013-02-28 16:59:13 -080020#include <sys/capability.h>
Elliott Hughes2ead70c2015-02-16 10:44:22 -080021#include <sys/file.h>
Brian Carlstrom0378aaf2014-08-08 00:52:22 -070022#include <cutils/sched_policy.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070023#include <diskusage/dirsize.h>
24#include <selinux/android.h>
Igor Murashkin9e87a802014-11-05 15:21:12 -080025#include <system/thread_defs.h>
Jeff Sharkey41ea4242015-04-09 11:34:03 -070026#include <base/stringprintf.h>
27#include <base/logging.h>
28
29using android::base::StringPrintf;
Mike Lockwood94afecf2012-10-24 10:45:23 -070030
31/* Directory records that are used in execution of commands. */
32dir_rec_t android_data_dir;
33dir_rec_t android_asec_dir;
34dir_rec_t android_app_dir;
35dir_rec_t android_app_private_dir;
36dir_rec_t android_app_lib_dir;
37dir_rec_t android_media_dir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -070038dir_rec_t android_mnt_expand_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -070039dir_rec_array_t android_system_dirs;
40
Jeff Sharkeyc03de092015-04-07 18:14:05 -070041int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070042{
Mike Lockwood94afecf2012-10-24 10:45:23 -070043 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
44 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
45 return -1;
46 }
47
Jeff Sharkeyc03de092015-04-07 18:14:05 -070048 std::string _pkgdir(create_package_data_path(uuid, pkgname, 0));
49 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070050
Nick Kralevicha2d838a2013-01-09 16:00:35 -080051 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070052 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
53 return -1;
54 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080055 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070056 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
57 unlink(pkgdir);
58 return -1;
59 }
60
Stephen Smalley26288202014-02-07 09:16:46 -050061 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070062 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -070063 unlink(pkgdir);
64 return -errno;
65 }
66
67 if (chown(pkgdir, uid, gid) < 0) {
68 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -070069 unlink(pkgdir);
70 return -1;
71 }
72
73 return 0;
74}
75
Jeff Sharkeyc03de092015-04-07 18:14:05 -070076int uninstall(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -070077{
Jeff Sharkeyc03de092015-04-07 18:14:05 -070078 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
79 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070080
Dave Allisond9370732014-01-30 14:19:23 -080081 remove_profile_file(pkgname);
82
Mike Lockwood94afecf2012-10-24 10:45:23 -070083 /* delete contents AND directory, no exceptions */
84 return delete_dir_contents(pkgdir, 1, NULL);
85}
86
87int renamepkg(const char *oldpkgname, const char *newpkgname)
88{
89 char oldpkgdir[PKG_PATH_MAX];
90 char newpkgdir[PKG_PATH_MAX];
91
92 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
93 return -1;
94 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
95 return -1;
96
97 if (rename(oldpkgdir, newpkgdir) < 0) {
98 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
99 return -errno;
100 }
101 return 0;
102}
103
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700104int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700105{
Mike Lockwood94afecf2012-10-24 10:45:23 -0700106 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700107
108 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
109 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
110 return -1;
111 }
112
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700113 std::string _pkgdir(create_package_data_path(uuid, pkgname, 0));
114 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700115
116 if (stat(pkgdir, &s) < 0) return -1;
117
118 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700119 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 -0700120 return -1;
121 }
122
123 if (chmod(pkgdir, 0751) < 0) {
124 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
125 unlink(pkgdir);
126 return -errno;
127 }
128 if (chown(pkgdir, uid, gid) < 0) {
129 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
130 unlink(pkgdir);
131 return -errno;
132 }
133
134 return 0;
135}
136
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700137int delete_user_data(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700139 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
140 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700141
Jeff Sharkey3316fe42014-08-27 10:46:25 -0700142 return delete_dir_contents(pkgdir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700143}
144
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700145int 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 -0700146{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700147 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
148 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700149
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800150 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700151 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
152 return -errno;
153 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800154 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700155 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
156 unlink(pkgdir);
157 return -errno;
158 }
159
Stephen Smalley26288202014-02-07 09:16:46 -0500160 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700161 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700162 unlink(pkgdir);
163 return -errno;
164 }
165
166 if (chown(pkgdir, uid, uid) < 0) {
167 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700168 unlink(pkgdir);
169 return -errno;
170 }
171
172 return 0;
173}
174
Robin Lee7c8bec02014-06-10 18:46:26 +0100175int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700176{
Robin Lee095c7632014-04-25 15:05:19 +0100177 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700178 return -1;
179 }
180
181 return 0;
182}
183
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700184int delete_user(const char *uuid, userid_t userid)
Robin Lee095c7632014-04-25 15:05:19 +0100185{
186 int status = 0;
187
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700188 std::string data_path(create_data_user_path(uuid, userid));
189 if (delete_dir_contents(data_path.c_str(), 1, NULL) != 0) {
Robin Lee095c7632014-04-25 15:05:19 +0100190 status = -1;
191 }
192
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700193 std::string media_path(create_data_media_path(uuid, userid));
194 if (delete_dir_contents(media_path.c_str(), 1, NULL) != 0) {
Robin Lee095c7632014-04-25 15:05:19 +0100195 status = -1;
196 }
197
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700198 // Config paths only exist on internal storage
199 if (uuid == nullptr) {
200 char config_path[PATH_MAX];
201 if ((create_user_config_path(config_path, userid) != 0)
202 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
203 status = -1;
204 }
Robin Lee095c7632014-04-25 15:05:19 +0100205 }
206
207 return status;
208}
209
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700210int delete_cache(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700211{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700212 std::string _cachedir(
213 create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX);
214 const char* cachedir = _cachedir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700215
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700216 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100217 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700218}
219
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700220int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid)
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700221{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700222 std::string _codecachedir(
223 create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX);
224 const char* codecachedir = _codecachedir.c_str();
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700225
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700226 struct stat s;
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700227
Jeff Sharkey770180a2014-09-08 17:14:26 -0700228 /* it's okay if code cache is missing */
229 if (lstat(codecachedir, &s) == -1 && errno == ENOENT) {
230 return 0;
231 }
232
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700233 /* delete contents, not the directory, no exceptions */
234 return delete_dir_contents(codecachedir, 0, NULL);
235}
236
Mike Lockwood94afecf2012-10-24 10:45:23 -0700237/* Try to ensure free_size bytes of storage are available.
238 * Returns 0 on success.
239 * This is rather simple-minded because doing a full LRU would
240 * be potentially memory-intensive, and without atime it would
241 * also require that apps constantly modify file metadata even
242 * when just reading from the cache, which is pretty awful.
243 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700244int free_cache(const char *uuid, int64_t free_size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700245{
246 cache_t* cache;
247 int64_t avail;
248 DIR *d;
249 struct dirent *de;
250 char tmpdir[PATH_MAX];
251 char *dirpos;
252
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700253 std::string data_path(create_data_path(uuid));
254
255 avail = data_disk_free(data_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700256 if (avail < 0) return -1;
257
258 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
259 if (avail >= free_size) return 0;
260
261 cache = start_cache_collection();
262
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700263 // Special case for owner on internal storage
264 if (uuid == nullptr) {
265 std::string _tmpdir(create_data_user_path(nullptr, 0));
266 add_cache_files(cache, _tmpdir.c_str(), "cache");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700267 }
268
269 // Search for other users and add any cache files from them.
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700270 std::string _tmpdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX);
271 strcpy(tmpdir, _tmpdir.c_str());
272
Mike Lockwood94afecf2012-10-24 10:45:23 -0700273 dirpos = tmpdir + strlen(tmpdir);
274 d = opendir(tmpdir);
275 if (d != NULL) {
276 while ((de = readdir(d))) {
277 if (de->d_type == DT_DIR) {
278 const char *name = de->d_name;
279 /* always skip "." and ".." */
280 if (name[0] == '.') {
281 if (name[1] == 0) continue;
282 if ((name[1] == '.') && (name[2] == 0)) continue;
283 }
284 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
285 strcpy(dirpos, name);
286 //ALOGI("adding cache files from %s\n", tmpdir);
287 add_cache_files(cache, tmpdir, "cache");
288 } else {
289 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
290 }
291 }
292 }
293 closedir(d);
294 }
295
296 // Collect cache files on external storage for all users (if it is mounted as part
297 // of the internal storage).
298 strcpy(tmpdir, android_media_dir.path);
299 dirpos = tmpdir + strlen(tmpdir);
300 d = opendir(tmpdir);
301 if (d != NULL) {
302 while ((de = readdir(d))) {
303 if (de->d_type == DT_DIR) {
304 const char *name = de->d_name;
305 /* skip any dir that doesn't start with a number, so not a user */
306 if (name[0] < '0' || name[0] > '9') {
307 continue;
308 }
309 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
310 strcpy(dirpos, name);
311 if (lookup_media_dir(tmpdir, "Android") == 0
312 && lookup_media_dir(tmpdir, "data") == 0) {
313 //ALOGI("adding cache files from %s\n", tmpdir);
314 add_cache_files(cache, tmpdir, "cache");
315 }
316 } else {
317 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
318 }
319 }
320 }
321 closedir(d);
322 }
323
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700324 clear_cache_files(data_path, cache, free_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700325 finish_cache_collection(cache);
326
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700327 return data_disk_free(data_path) >= free_size ? 0 : -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700328}
329
Narayan Kamath1b400322014-04-11 13:17:00 +0100330int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700331{
332 char src_dex[PKG_PATH_MAX];
333 char dst_dex[PKG_PATH_MAX];
334
Jeff Sharkey770180a2014-09-08 17:14:26 -0700335 if (validate_apk_path(src)) {
336 ALOGE("invalid apk path '%s' (bad prefix)\n", src);
337 return -1;
338 }
339 if (validate_apk_path(dst)) {
340 ALOGE("invalid apk path '%s' (bad prefix)\n", dst);
341 return -1;
342 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700343
Narayan Kamath1b400322014-04-11 13:17:00 +0100344 if (create_cache_path(src_dex, src, instruction_set)) return -1;
345 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700346
347 ALOGV("move %s -> %s\n", src_dex, dst_dex);
348 if (rename(src_dex, dst_dex) < 0) {
349 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
350 return -1;
351 } else {
352 return 0;
353 }
354}
355
Narayan Kamath1b400322014-04-11 13:17:00 +0100356int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700357{
358 char dex_path[PKG_PATH_MAX];
359
Jeff Sharkey770180a2014-09-08 17:14:26 -0700360 if (validate_apk_path(path) && validate_system_app_path(path)) {
361 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
362 return -1;
363 }
364
Narayan Kamath1b400322014-04-11 13:17:00 +0100365 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700366
367 ALOGV("unlink %s\n", dex_path);
368 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700369 if (errno != ENOENT) {
370 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
371 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700372 return -1;
373 } else {
374 return 0;
375 }
376}
377
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700378int get_size(const char *uuid, const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700379 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100380 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
381 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700382{
383 DIR *d;
384 int dfd;
385 struct dirent *de;
386 struct stat s;
387 char path[PKG_PATH_MAX];
388
389 int64_t codesize = 0;
390 int64_t datasize = 0;
391 int64_t cachesize = 0;
392 int64_t asecsize = 0;
393
394 /* count the source apk as code -- but only if it's not
395 * on the /system partition and its not on the sdcard.
396 */
397 if (validate_system_app_path(apkpath) &&
398 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
399 if (stat(apkpath, &s) == 0) {
400 codesize += stat_size(&s);
401 }
402 }
403 /* count the forward locked apk as code if it is given
404 */
405 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
406 if (stat(fwdlock_apkpath, &s) == 0) {
407 codesize += stat_size(&s);
408 }
409 }
410 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100411 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700412 if (stat(path, &s) == 0) {
413 codesize += stat_size(&s);
414 }
415 }
416
417 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700418 if (libdirpath != NULL && libdirpath[0] != '!') {
419 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700420 if (d != NULL) {
421 dfd = dirfd(d);
422 codesize += calculate_dir_size(dfd);
423 closedir(d);
424 }
425 }
426
427 /* compute asec size if it is given
428 */
429 if (asecpath != NULL && asecpath[0] != '!') {
430 if (stat(asecpath, &s) == 0) {
431 asecsize += stat_size(&s);
432 }
433 }
434
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700435 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
436 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700437
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700438 d = opendir(pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700439 if (d == NULL) {
440 goto done;
441 }
442 dfd = dirfd(d);
443
444 /* most stuff in the pkgdir is data, except for the "cache"
445 * directory and below, which is cache, and the "lib" directory
446 * and below, which is code...
447 */
448 while ((de = readdir(d))) {
449 const char *name = de->d_name;
450
451 if (de->d_type == DT_DIR) {
452 int subfd;
453 int64_t statsize = 0;
454 int64_t dirsize = 0;
455 /* always skip "." and ".." */
456 if (name[0] == '.') {
457 if (name[1] == 0) continue;
458 if ((name[1] == '.') && (name[2] == 0)) continue;
459 }
460 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
461 statsize = stat_size(&s);
462 }
463 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
464 if (subfd >= 0) {
465 dirsize = calculate_dir_size(subfd);
466 }
467 if(!strcmp(name,"lib")) {
468 codesize += dirsize + statsize;
469 } else if(!strcmp(name,"cache")) {
470 cachesize += dirsize + statsize;
471 } else {
472 datasize += dirsize + statsize;
473 }
474 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
475 // This is the symbolic link to the application's library
476 // code. We'll count this as code instead of data, since
477 // it is not something that the app creates.
478 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
479 codesize += stat_size(&s);
480 }
481 } else {
482 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
483 datasize += stat_size(&s);
484 }
485 }
486 }
487 closedir(d);
488done:
489 *_codesize = codesize;
490 *_datasize = datasize;
491 *_cachesize = cachesize;
492 *_asecsize = asecsize;
493 return 0;
494}
495
Narayan Kamath1b400322014-04-11 13:17:00 +0100496int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700497{
498 char *tmp;
499 int srclen;
500 int dstlen;
501
502 srclen = strlen(src);
503
504 /* demand that we are an absolute path */
505 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
506 return -1;
507 }
508
509 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
510 return -1;
511 }
512
Dave Allisond9370732014-01-30 14:19:23 -0800513 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100514 strlen(instruction_set) +
515 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800516
Mike Lockwood94afecf2012-10-24 10:45:23 -0700517 if (dstlen > PKG_PATH_MAX) {
518 return -1;
519 }
520
Narayan Kamath1b400322014-04-11 13:17:00 +0100521 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700522 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100523 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700524 src + 1, /* skip the leading / */
525 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800526
Narayan Kamath1b400322014-04-11 13:17:00 +0100527 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700528 if (*tmp == '/') {
529 *tmp = '@';
530 }
531 }
532
533 return 0;
534}
535
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700536static int split_count(const char *str)
537{
538 char *ctx;
539 int count = 0;
540 char buf[PROPERTY_VALUE_MAX];
541
542 strncpy(buf, str, sizeof(buf));
543 char *pBuf = buf;
544
545 while(strtok_r(pBuf, " ", &ctx) != NULL) {
546 count++;
547 pBuf = NULL;
548 }
549
550 return count;
551}
552
neo.chae14e084d2015-01-07 18:46:13 +0900553static int split(char *buf, const char **argv)
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700554{
555 char *ctx;
556 int count = 0;
557 char *tok;
558 char *pBuf = buf;
559
560 while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
561 argv[count++] = tok;
562 pBuf = NULL;
563 }
564
565 return count;
566}
567
Alex Light7365a102014-07-21 12:23:48 -0700568static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700569 const char* output_file_name, const char *pkgname __unused, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700570{
571 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100572 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700573
574 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
575 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
576 ALOGE("Instruction set %s longer than max length of %d",
577 instruction_set, MAX_INSTRUCTION_SET_LEN);
578 return;
579 }
580
581 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
582 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
583 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
584 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
585 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
586 // The caller has already gotten all the locks we need.
587 const char* no_lock_arg = "--no-lock-output";
588 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
589 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
590 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700591 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700592 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
593
594 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
595 char* argv[7];
596 argv[0] = (char*) PATCHOAT_BIN;
597 argv[1] = (char*) patched_image_location_arg;
598 argv[2] = (char*) no_lock_arg;
599 argv[3] = instruction_set_arg;
600 argv[4] = output_oat_fd_arg;
601 argv[5] = input_oat_fd_arg;
602 argv[6] = NULL;
603
604 execv(PATCHOAT_BIN, (char* const *)argv);
605 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
606}
607
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700608static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Andreas Gampee1c01352014-12-10 16:41:11 -0800609 const char* output_file_name, int swap_fd, const char *pkgname, const char *instruction_set,
Andreas Gampe598c25e2015-03-03 09:15:06 -0800610 bool vm_safe_mode, bool debuggable)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700611{
Calin Juravle8fc73152014-08-19 18:48:50 +0100612 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
613
614 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
615 ALOGE("Instruction set %s longer than max length of %d",
616 instruction_set, MAX_INSTRUCTION_SET_LEN);
617 return;
618 }
619
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700620 char prop_buf[PROPERTY_VALUE_MAX];
621 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
622
623 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
624 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
625
626 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
627 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
628
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700629 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
630 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
631 dex2oat_compiler_filter_flag, NULL) > 0;
632
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700633 char dex2oat_threads_buf[PROPERTY_VALUE_MAX];
634 bool have_dex2oat_threads_flag = property_get("dalvik.vm.dex2oat-threads", dex2oat_threads_buf,
635 NULL) > 0;
636 char dex2oat_threads_arg[PROPERTY_VALUE_MAX + 2];
637 if (have_dex2oat_threads_flag) {
638 sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf);
639 }
640
Calin Juravle8fc73152014-08-19 18:48:50 +0100641 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
642 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
643 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
644 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
645 dex2oat_isa_features, NULL) > 0;
646
Ian Rogers16a95b22014-11-08 16:58:13 -0800647 char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
648 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
649 char dex2oat_isa_variant[PROPERTY_VALUE_MAX];
650 bool have_dex2oat_isa_variant = property_get(dex2oat_isa_variant_key,
651 dex2oat_isa_variant, NULL) > 0;
652
neo.chae14e084d2015-01-07 18:46:13 +0900653 const char *dex2oat_norelocation = "-Xnorelocate";
654 bool have_dex2oat_relocation_skip_flag = false;
655
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800656 char dex2oat_flags[PROPERTY_VALUE_MAX];
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700657 int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags",
658 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800659 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
660
Brian Carlstrom538998f2014-07-30 14:37:11 -0700661 // If we booting without the real /data, don't spend time compiling.
662 char vold_decrypt[PROPERTY_VALUE_MAX];
663 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
664 bool skip_compilation = (have_vold_decrypt &&
665 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
666 (strcmp(vold_decrypt, "1") == 0)));
667
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700668 char use_jit_property[PROPERTY_VALUE_MAX];
669 bool have_jit_property = property_get("debug.usejit", use_jit_property, NULL) > 0;
670 bool use_jit = have_jit_property && strcmp(use_jit_property, "true") == 0;
671
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700672 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700673
Brian Carlstrom53e07762014-06-27 14:15:19 -0700674 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700675
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700676 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100677
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700678 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
679 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
680 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700681 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100682 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Ian Rogers16a95b22014-11-08 16:58:13 -0800683 char instruction_set_variant_arg[strlen("--instruction-set-variant=") + PROPERTY_VALUE_MAX];
Calin Juravle8fc73152014-08-19 18:48:50 +0100684 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100685 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
686 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700687 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
688 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700689 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800690 bool have_dex2oat_swap_fd = false;
691 char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700692
693 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
694 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
695 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
696 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100697 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Ian Rogers16a95b22014-11-08 16:58:13 -0800698 sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
Calin Juravle8fc73152014-08-19 18:48:50 +0100699 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Andreas Gampee1c01352014-12-10 16:41:11 -0800700 if (swap_fd >= 0) {
701 have_dex2oat_swap_fd = true;
702 sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
703 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100704
Calin Juravle4fdff462014-06-06 16:58:43 +0100705 bool have_profile_file = false;
706 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100707 if (profiler && (strcmp(pkgname, "*") != 0)) {
708 char profile_file[PKG_PATH_MAX];
709 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800710 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100711 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100712 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100713 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100714 have_profile_file = true;
715 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
716 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
717 "--top-k-profile-threshold=%s", prop_buf);
718 have_top_k_profile_threshold = true;
719 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100720 }
Dave Allisond9370732014-01-30 14:19:23 -0800721 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700722
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700723 if (have_dex2oat_Xms_flag) {
724 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
725 }
726 if (have_dex2oat_Xmx_flag) {
727 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
728 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700729 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700730 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700731 have_dex2oat_compiler_filter_flag = true;
neo.chae14e084d2015-01-07 18:46:13 +0900732 have_dex2oat_relocation_skip_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100733 } else if (vm_safe_mode) {
734 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100735 have_dex2oat_compiler_filter_flag = true;
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700736 } else if (use_jit) {
737 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-at-runtime");
738 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700739 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700740 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
741 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700742
Andreas Gampe598c25e2015-03-03 09:15:06 -0800743 // Check whether all apps should be compiled debuggable.
744 if (!debuggable) {
745 debuggable =
746 (property_get("dalvik.vm.always_debuggable", prop_buf, "0") > 0) &&
747 (prop_buf[0] == '1');
748 }
749
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700750 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100751
neo.chae14e084d2015-01-07 18:46:13 +0900752 const char* argv[7 // program name, mandatory arguments and the final NULL
753 + (have_dex2oat_isa_variant ? 1 : 0)
754 + (have_dex2oat_isa_features ? 1 : 0)
755 + (have_profile_file ? 1 : 0)
756 + (have_top_k_profile_threshold ? 1 : 0)
757 + (have_dex2oat_Xms_flag ? 2 : 0)
758 + (have_dex2oat_Xmx_flag ? 2 : 0)
759 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700760 + (have_dex2oat_threads_flag ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900761 + (have_dex2oat_swap_fd ? 1 : 0)
762 + (have_dex2oat_relocation_skip_flag ? 2 : 0)
Andreas Gampe598c25e2015-03-03 09:15:06 -0800763 + (debuggable ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900764 + dex2oat_flags_count];
Calin Juravle4fdff462014-06-06 16:58:43 +0100765 int i = 0;
neo.chae14e084d2015-01-07 18:46:13 +0900766 argv[i++] = DEX2OAT_BIN;
Calin Juravle4fdff462014-06-06 16:58:43 +0100767 argv[i++] = zip_fd_arg;
768 argv[i++] = zip_location_arg;
769 argv[i++] = oat_fd_arg;
770 argv[i++] = oat_location_arg;
771 argv[i++] = instruction_set_arg;
Ian Rogers16a95b22014-11-08 16:58:13 -0800772 if (have_dex2oat_isa_variant) {
773 argv[i++] = instruction_set_variant_arg;
774 }
Calin Juravle8fc73152014-08-19 18:48:50 +0100775 if (have_dex2oat_isa_features) {
776 argv[i++] = instruction_set_features_arg;
777 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100778 if (have_profile_file) {
779 argv[i++] = profile_file_arg;
780 }
781 if (have_top_k_profile_threshold) {
782 argv[i++] = top_k_profile_threshold_arg;
783 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700784 if (have_dex2oat_Xms_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900785 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700786 argv[i++] = dex2oat_Xms_arg;
787 }
788 if (have_dex2oat_Xmx_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900789 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700790 argv[i++] = dex2oat_Xmx_arg;
791 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700792 if (have_dex2oat_compiler_filter_flag) {
793 argv[i++] = dex2oat_compiler_filter_arg;
794 }
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700795 if (have_dex2oat_threads_flag) {
796 argv[i++] = dex2oat_threads_arg;
797 }
Andreas Gampee1c01352014-12-10 16:41:11 -0800798 if (have_dex2oat_swap_fd) {
799 argv[i++] = dex2oat_swap_fd;
800 }
Andreas Gampe598c25e2015-03-03 09:15:06 -0800801 if (debuggable) {
802 argv[i++] = "--debuggable";
803 }
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700804 if (dex2oat_flags_count) {
805 i += split(dex2oat_flags, argv + i);
Calin Juravle4fdff462014-06-06 16:58:43 +0100806 }
neo.chae14e084d2015-01-07 18:46:13 +0900807 if (have_dex2oat_relocation_skip_flag) {
808 argv[i++] = RUNTIME_ARG;
809 argv[i++] = dex2oat_norelocation;
810 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700811 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100812 argv[i] = NULL;
813
neo.chae14e084d2015-01-07 18:46:13 +0900814 execv(DEX2OAT_BIN, (char * const *)argv);
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700815 ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700816}
817
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100818static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700819{
820 int status;
821 pid_t got_pid;
822
Mike Lockwood94afecf2012-10-24 10:45:23 -0700823 while (1) {
824 got_pid = waitpid(pid, &status, 0);
825 if (got_pid == -1 && errno == EINTR) {
826 printf("waitpid interrupted, retrying\n");
827 } else {
828 break;
829 }
830 }
831 if (got_pid != pid) {
832 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
833 (int) pid, (int) got_pid, strerror(errno));
834 return 1;
835 }
836
837 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700838 return 0;
839 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700840 return status; /* always nonzero */
841 }
842}
843
Andreas Gampee1c01352014-12-10 16:41:11 -0800844/*
845 * Whether dexopt should use a swap file when compiling an APK. If kAlwaysProvideSwapFile, do this
846 * on all devices (dex2oat will make a more informed decision itself, anyways). Otherwise, only do
847 * this on a low-mem device.
848 */
849static bool kAlwaysProvideSwapFile = true;
850
851static bool ShouldUseSwapFileForDexopt() {
852 if (kAlwaysProvideSwapFile) {
853 return true;
854 }
855
856 char low_mem_buf[PROPERTY_VALUE_MAX];
857 property_get("ro.config.low_ram", low_mem_buf, "");
858 return (strcmp(low_mem_buf, "true") == 0);
859}
860
Richard Uhler009b8772015-03-18 12:39:09 -0700861/*
862 * Computes the odex file for the given apk_path and instruction_set.
863 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
864 *
865 * Returns false if it failed to determine the odex file path.
866 */
867static bool calculate_odex_file_path(char path[PKG_PATH_MAX],
868 const char *apk_path,
869 const char *instruction_set)
870{
871 if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
872 + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
873 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
874 return false;
875 }
876
877 strcpy(path, apk_path);
878 char *end = strrchr(path, '/');
879 if (end == NULL) {
880 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
881 return false;
882 }
883 const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
884
885 strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0
886 strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
887 strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0
888 end = strrchr(path, '.');
889 if (end == NULL) {
890 ALOGE("apk_path '%s' has no extension.\n", apk_path);
891 return false;
892 }
893 strcpy(end + 1, "odex");
894 return true;
895}
896
Calin Juravleb1efac12014-08-21 19:05:20 +0100897int dexopt(const char *apk_path, uid_t uid, bool is_public,
Richard Uhlerc92fb622015-03-26 15:47:38 -0700898 const char *pkgname, const char *instruction_set, int dexopt_needed,
899 bool vm_safe_mode, bool debuggable, const char* oat_dir)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700900{
901 struct utimbuf ut;
Fyodor Kupolov26ff93c2015-04-02 16:59:10 -0700902 struct stat input_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700903 char out_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800904 char swap_file_name[PKG_PATH_MAX];
Alex Light7365a102014-07-21 12:23:48 -0700905 const char *input_file;
906 char in_odex_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800907 int res, input_fd=-1, out_fd=-1, swap_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700908
Andreas Gampee1c01352014-12-10 16:41:11 -0800909 // Early best-effort check whether we can fit the the path into our buffers.
910 // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
911 // without a swap file, if necessary.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700912 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800913 ALOGE("apk_path too long '%s'\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700914 return -1;
915 }
916
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800917 if (oat_dir != NULL && oat_dir[0] != '!') {
918 if (validate_apk_path(oat_dir)) {
919 ALOGE("invalid oat_dir '%s'\n", oat_dir);
920 return -1;
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800921 }
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800922 if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) {
923 return -1;
924 }
925 } else {
926 if (create_cache_path(out_path, apk_path, instruction_set)) {
927 return -1;
928 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700929 }
930
Richard Uhlerc92fb622015-03-26 15:47:38 -0700931 switch (dexopt_needed) {
932 case DEXOPT_DEX2OAT_NEEDED:
933 input_file = apk_path;
934 break;
935
936 case DEXOPT_PATCHOAT_NEEDED:
937 if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
938 return -1;
939 }
940 input_file = in_odex_path;
941 break;
942
943 case DEXOPT_SELF_PATCHOAT_NEEDED:
944 input_file = out_path;
945 break;
946
947 default:
948 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
949 exit(72);
Alex Light7365a102014-07-21 12:23:48 -0700950 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700951
Alex Light7365a102014-07-21 12:23:48 -0700952 memset(&input_stat, 0, sizeof(input_stat));
953 stat(input_file, &input_stat);
954
955 input_fd = open(input_file, O_RDONLY, 0);
956 if (input_fd < 0) {
957 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700958 return -1;
959 }
960
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700961 unlink(out_path);
962 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
963 if (out_fd < 0) {
964 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700965 goto fail;
966 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700967 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700968 S_IRUSR|S_IWUSR|S_IRGRP |
969 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700970 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700971 goto fail;
972 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700973 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
974 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700975 goto fail;
976 }
977
Dave Allisond9370732014-01-30 14:19:23 -0800978 // Create profile file if there is a package name present.
979 if (strcmp(pkgname, "*") != 0) {
980 create_profile_file(pkgname, uid);
981 }
982
Andreas Gampee1c01352014-12-10 16:41:11 -0800983 // Create a swap file if necessary.
Richard Uhlerc92fb622015-03-26 15:47:38 -0700984 if (ShouldUseSwapFileForDexopt()) {
Andreas Gampee1c01352014-12-10 16:41:11 -0800985 // Make sure there really is enough space.
986 size_t out_len = strlen(out_path);
987 if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
988 strcpy(swap_file_name, out_path);
989 strcpy(swap_file_name + strlen(out_path), ".swap");
990 unlink(swap_file_name);
991 swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600);
992 if (swap_fd < 0) {
993 // Could not create swap file. Optimistically go on and hope that we can compile
994 // without it.
995 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
996 } else {
997 // Immediately unlink. We don't really want to hit flash.
998 unlink(swap_file_name);
999 }
1000 } else {
1001 // Swap file path is too long. Try to run without.
1002 ALOGE("installd could not create swap file for path %s during dexopt\n", out_path);
1003 }
1004 }
Dave Allisond9370732014-01-30 14:19:23 -08001005
Alex Light7365a102014-07-21 12:23:48 -07001006 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001007
1008 pid_t pid;
1009 pid = fork();
1010 if (pid == 0) {
1011 /* child -- drop privileges before continuing */
1012 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001013 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001014 exit(64);
1015 }
1016 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001017 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001018 exit(65);
1019 }
1020 // drop capabilities
1021 struct __user_cap_header_struct capheader;
1022 struct __user_cap_data_struct capdata[2];
1023 memset(&capheader, 0, sizeof(capheader));
1024 memset(&capdata, 0, sizeof(capdata));
1025 capheader.version = _LINUX_CAPABILITY_VERSION_3;
1026 if (capset(&capheader, &capdata[0]) < 0) {
1027 ALOGE("capset failed: %s\n", strerror(errno));
1028 exit(66);
1029 }
Brian Carlstrom0378aaf2014-08-08 00:52:22 -07001030 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
1031 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
1032 exit(70);
1033 }
Igor Murashkin9e87a802014-11-05 15:21:12 -08001034 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
1035 ALOGE("setpriority failed: %s\n", strerror(errno));
1036 exit(71);
1037 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001038 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
1039 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001040 exit(67);
1041 }
1042
Richard Uhlerc92fb622015-03-26 15:47:38 -07001043 if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED
1044 || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) {
Andreas Gampebd872e42014-12-15 11:41:11 -08001045 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001046 } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001047 const char *input_file_name = strrchr(input_file, '/');
1048 if (input_file_name == NULL) {
1049 input_file_name = input_file;
1050 } else {
1051 input_file_name++;
1052 }
1053 run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname,
1054 instruction_set, vm_safe_mode, debuggable);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001055 } else {
1056 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1057 exit(73);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001058 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001059 exit(68); /* only get here on exec failure */
1060 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001061 res = wait_child(pid);
1062 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001063 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001064 } else {
Alex Light7365a102014-07-21 12:23:48 -07001065 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001066 goto fail;
1067 }
1068 }
1069
Alex Light7365a102014-07-21 12:23:48 -07001070 ut.actime = input_stat.st_atime;
1071 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001072 utime(out_path, &ut);
1073
1074 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001075 close(input_fd);
Andreas Gampee1c01352014-12-10 16:41:11 -08001076 if (swap_fd != -1) {
1077 close(swap_fd);
1078 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001079 return 0;
1080
1081fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001082 if (out_fd >= 0) {
1083 close(out_fd);
1084 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001085 }
Alex Light7365a102014-07-21 12:23:48 -07001086 if (input_fd >= 0) {
1087 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001088 }
1089 return -1;
1090}
1091
Narayan Kamath091ea772014-11-10 15:03:46 +00001092int mark_boot_complete(const char* instruction_set)
1093{
1094 char boot_marker_path[PKG_PATH_MAX];
1095 sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set);
1096
1097 ALOGV("mark_boot_complete : %s", boot_marker_path);
1098 if (unlink(boot_marker_path) != 0) {
1099 ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1100 strerror(errno));
1101 return -1;
1102 }
1103
1104 return 0;
1105}
1106
Mike Lockwood94afecf2012-10-24 10:45:23 -07001107void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1108 struct stat* statbuf)
1109{
1110 while (path[basepos] != 0) {
1111 if (path[basepos] == '/') {
1112 path[basepos] = 0;
1113 if (lstat(path, statbuf) < 0) {
1114 ALOGV("Making directory: %s\n", path);
1115 if (mkdir(path, mode) == 0) {
1116 chown(path, uid, gid);
1117 } else {
1118 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1119 }
1120 }
1121 path[basepos] = '/';
1122 basepos++;
1123 }
1124 basepos++;
1125 }
1126}
1127
1128int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1129 int dstuid, int dstgid, struct stat* statbuf)
1130{
1131 DIR *d;
1132 struct dirent *de;
1133 int res;
1134
1135 int srcend = strlen(srcpath);
1136 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001137
Mike Lockwood94afecf2012-10-24 10:45:23 -07001138 if (lstat(srcpath, statbuf) < 0) {
1139 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1140 return 1;
1141 }
Dave Allisond9370732014-01-30 14:19:23 -08001142
Mike Lockwood94afecf2012-10-24 10:45:23 -07001143 if ((statbuf->st_mode&S_IFDIR) == 0) {
1144 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1145 dstuid, dstgid, statbuf);
1146 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1147 if (rename(srcpath, dstpath) >= 0) {
1148 if (chown(dstpath, dstuid, dstgid) < 0) {
1149 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1150 unlink(dstpath);
1151 return 1;
1152 }
1153 } else {
1154 ALOGW("Unable to rename %s to %s: %s\n",
1155 srcpath, dstpath, strerror(errno));
1156 return 1;
1157 }
1158 return 0;
1159 }
1160
1161 d = opendir(srcpath);
1162 if (d == NULL) {
1163 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1164 return 1;
1165 }
1166
1167 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001168
Mike Lockwood94afecf2012-10-24 10:45:23 -07001169 while ((de = readdir(d))) {
1170 const char *name = de->d_name;
1171 /* always skip "." and ".." */
1172 if (name[0] == '.') {
1173 if (name[1] == 0) continue;
1174 if ((name[1] == '.') && (name[2] == 0)) continue;
1175 }
Dave Allisond9370732014-01-30 14:19:23 -08001176
Mike Lockwood94afecf2012-10-24 10:45:23 -07001177 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1178 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1179 continue;
1180 }
Dave Allisond9370732014-01-30 14:19:23 -08001181
Mike Lockwood94afecf2012-10-24 10:45:23 -07001182 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1183 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1184 continue;
1185 }
Dave Allisond9370732014-01-30 14:19:23 -08001186
Mike Lockwood94afecf2012-10-24 10:45:23 -07001187 srcpath[srcend] = dstpath[dstend] = '/';
1188 strcpy(srcpath+srcend+1, name);
1189 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001190
Mike Lockwood94afecf2012-10-24 10:45:23 -07001191 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1192 res = 1;
1193 }
Dave Allisond9370732014-01-30 14:19:23 -08001194
Mike Lockwood94afecf2012-10-24 10:45:23 -07001195 // Note: we will be leaving empty directories behind in srcpath,
1196 // but that is okay, the package manager will be erasing all of the
1197 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001198
Mike Lockwood94afecf2012-10-24 10:45:23 -07001199 srcpath[srcend] = dstpath[dstend] = 0;
1200 }
Dave Allisond9370732014-01-30 14:19:23 -08001201
Mike Lockwood94afecf2012-10-24 10:45:23 -07001202 closedir(d);
1203 return res;
1204}
1205
1206int movefiles()
1207{
1208 DIR *d;
1209 int dfd, subfd;
1210 struct dirent *de;
1211 struct stat s;
1212 char buf[PKG_PATH_MAX+1];
1213 int bufp, bufe, bufi, readlen;
1214
1215 char srcpkg[PKG_NAME_MAX];
1216 char dstpkg[PKG_NAME_MAX];
1217 char srcpath[PKG_PATH_MAX];
1218 char dstpath[PKG_PATH_MAX];
1219 int dstuid=-1, dstgid=-1;
1220 int hasspace;
1221
1222 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1223 if (d == NULL) {
1224 goto done;
1225 }
1226 dfd = dirfd(d);
1227
1228 /* Iterate through all files in the directory, executing the
1229 * file movements requested there-in.
1230 */
1231 while ((de = readdir(d))) {
1232 const char *name = de->d_name;
1233
1234 if (de->d_type == DT_DIR) {
1235 continue;
1236 } else {
1237 subfd = openat(dfd, name, O_RDONLY);
1238 if (subfd < 0) {
1239 ALOGW("Unable to open update commands at %s%s\n",
1240 UPDATE_COMMANDS_DIR_PREFIX, name);
1241 continue;
1242 }
Dave Allisond9370732014-01-30 14:19:23 -08001243
Mike Lockwood94afecf2012-10-24 10:45:23 -07001244 bufp = 0;
1245 bufe = 0;
1246 buf[PKG_PATH_MAX] = 0;
1247 srcpkg[0] = dstpkg[0] = 0;
1248 while (1) {
1249 bufi = bufp;
1250 while (bufi < bufe && buf[bufi] != '\n') {
1251 bufi++;
1252 }
1253 if (bufi < bufe) {
1254 buf[bufi] = 0;
1255 ALOGV("Processing line: %s\n", buf+bufp);
1256 hasspace = 0;
1257 while (bufp < bufi && isspace(buf[bufp])) {
1258 hasspace = 1;
1259 bufp++;
1260 }
1261 if (buf[bufp] == '#' || bufp == bufi) {
1262 // skip comments and empty lines.
1263 } else if (hasspace) {
1264 if (dstpkg[0] == 0) {
1265 ALOGW("Path before package line in %s%s: %s\n",
1266 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1267 } else if (srcpkg[0] == 0) {
1268 // Skip -- source package no longer exists.
1269 } else {
1270 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1271 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1272 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1273 movefileordir(srcpath, dstpath,
1274 strlen(dstpath)-strlen(buf+bufp),
1275 dstuid, dstgid, &s);
1276 }
1277 }
1278 } else {
1279 char* div = strchr(buf+bufp, ':');
1280 if (div == NULL) {
1281 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1282 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1283 } else {
1284 *div = 0;
1285 div++;
1286 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1287 strcpy(dstpkg, buf+bufp);
1288 } else {
1289 srcpkg[0] = dstpkg[0] = 0;
1290 ALOGW("Package name too long in %s%s: %s\n",
1291 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1292 }
1293 if (strlen(div) < PKG_NAME_MAX) {
1294 strcpy(srcpkg, div);
1295 } else {
1296 srcpkg[0] = dstpkg[0] = 0;
1297 ALOGW("Package name too long in %s%s: %s\n",
1298 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1299 }
1300 if (srcpkg[0] != 0) {
1301 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1302 if (lstat(srcpath, &s) < 0) {
1303 // Package no longer exists -- skip.
1304 srcpkg[0] = 0;
1305 }
1306 } else {
1307 srcpkg[0] = 0;
1308 ALOGW("Can't create path %s in %s%s\n",
1309 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1310 }
1311 if (srcpkg[0] != 0) {
1312 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1313 if (lstat(dstpath, &s) == 0) {
1314 dstuid = s.st_uid;
1315 dstgid = s.st_gid;
1316 } else {
1317 // Destination package doesn't
1318 // exist... due to original-package,
1319 // this is normal, so don't be
1320 // noisy about it.
1321 srcpkg[0] = 0;
1322 }
1323 } else {
1324 srcpkg[0] = 0;
1325 ALOGW("Can't create path %s in %s%s\n",
1326 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1327 }
1328 }
1329 ALOGV("Transfering from %s to %s: uid=%d\n",
1330 srcpkg, dstpkg, dstuid);
1331 }
1332 }
1333 }
1334 bufp = bufi+1;
1335 } else {
1336 if (bufp == 0) {
1337 if (bufp < bufe) {
1338 ALOGW("Line too long in %s%s, skipping: %s\n",
1339 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1340 }
1341 } else if (bufp < bufe) {
1342 memcpy(buf, buf+bufp, bufe-bufp);
1343 bufe -= bufp;
1344 bufp = 0;
1345 }
1346 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1347 if (readlen < 0) {
1348 ALOGW("Failure reading update commands in %s%s: %s\n",
1349 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1350 break;
1351 } else if (readlen == 0) {
1352 break;
1353 }
1354 bufe += readlen;
1355 buf[bufe] = 0;
1356 ALOGV("Read buf: %s\n", buf);
1357 }
1358 }
1359 close(subfd);
1360 }
1361 }
1362 closedir(d);
1363done:
1364 return 0;
1365}
1366
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001367int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001368{
Mike Lockwood94afecf2012-10-24 10:45:23 -07001369 struct stat s, libStat;
1370 int rc = 0;
1371
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001372 std::string _pkgdir(create_package_data_path(uuid, pkgname, userId));
1373 std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
1374
1375 const char* pkgdir = _pkgdir.c_str();
1376 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001377
1378 if (stat(pkgdir, &s) < 0) return -1;
1379
1380 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1381 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1382 return -1;
1383 }
1384
1385 if (chmod(pkgdir, 0700) < 0) {
1386 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1387 rc = -1;
1388 goto out;
1389 }
1390
1391 if (lstat(libsymlink, &libStat) < 0) {
1392 if (errno != ENOENT) {
1393 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1394 rc = -1;
1395 goto out;
1396 }
1397 } else {
1398 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001399 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001400 rc = -1;
1401 goto out;
1402 }
1403 } else if (S_ISLNK(libStat.st_mode)) {
1404 if (unlink(libsymlink) < 0) {
1405 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1406 rc = -1;
1407 goto out;
1408 }
1409 }
1410 }
1411
1412 if (symlink(asecLibDir, libsymlink) < 0) {
1413 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1414 strerror(errno));
1415 rc = -errno;
1416 goto out;
1417 }
1418
1419out:
1420 if (chmod(pkgdir, s.st_mode) < 0) {
1421 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1422 rc = -errno;
1423 }
1424
1425 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1426 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1427 return -errno;
1428 }
1429
1430 return rc;
1431}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001432
1433static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1434{
1435 static const char *IDMAP_BIN = "/system/bin/idmap";
1436 static const size_t MAX_INT_LEN = 32;
1437 char idmap_str[MAX_INT_LEN];
1438
1439 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1440
1441 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1442 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1443}
1444
1445// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1446// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1447static int flatten_path(const char *prefix, const char *suffix,
1448 const char *overlay_path, char *idmap_path, size_t N)
1449{
1450 if (overlay_path == NULL || idmap_path == NULL) {
1451 return -1;
1452 }
1453 const size_t len_overlay_path = strlen(overlay_path);
1454 // will access overlay_path + 1 further below; requires absolute path
1455 if (len_overlay_path < 2 || *overlay_path != '/') {
1456 return -1;
1457 }
1458 const size_t len_idmap_root = strlen(prefix);
1459 const size_t len_suffix = strlen(suffix);
1460 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1461 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1462 // additions below would cause overflow
1463 return -1;
1464 }
1465 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1466 return -1;
1467 }
1468 memset(idmap_path, 0, N);
1469 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1470 char *ch = idmap_path + len_idmap_root;
1471 while (*ch != '\0') {
1472 if (*ch == '/') {
1473 *ch = '@';
1474 }
1475 ++ch;
1476 }
1477 return 0;
1478}
1479
1480int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1481{
1482 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1483
1484 int idmap_fd = -1;
1485 char idmap_path[PATH_MAX];
1486
1487 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1488 idmap_path, sizeof(idmap_path)) == -1) {
1489 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1490 goto fail;
1491 }
1492
1493 unlink(idmap_path);
1494 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1495 if (idmap_fd < 0) {
1496 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1497 goto fail;
1498 }
1499 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1500 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1501 goto fail;
1502 }
1503 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1504 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1505 goto fail;
1506 }
1507
1508 pid_t pid;
1509 pid = fork();
1510 if (pid == 0) {
1511 /* child -- drop privileges before continuing */
1512 if (setgid(uid) != 0) {
1513 ALOGE("setgid(%d) failed during idmap\n", uid);
1514 exit(1);
1515 }
1516 if (setuid(uid) != 0) {
1517 ALOGE("setuid(%d) failed during idmap\n", uid);
1518 exit(1);
1519 }
1520 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1521 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1522 exit(1);
1523 }
1524
1525 run_idmap(target_apk, overlay_apk, idmap_fd);
1526 exit(1); /* only if exec call to idmap failed */
1527 } else {
1528 int status = wait_child(pid);
1529 if (status != 0) {
1530 ALOGE("idmap failed, status=0x%04x\n", status);
1531 goto fail;
1532 }
1533 }
1534
1535 close(idmap_fd);
1536 return 0;
1537fail:
1538 if (idmap_fd >= 0) {
1539 close(idmap_fd);
1540 unlink(idmap_path);
1541 }
1542 return -1;
1543}
Robert Craige9887e42014-02-20 10:25:56 -05001544
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001545// TODO: extend to know about other volumes
Andreas Gampe0ad7a112015-04-09 09:52:45 -07001546int restorecon_data(const char* uuid __attribute__((unused)), const char* pkgName,
1547 const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001548{
Robert Craigda30dc72014-03-27 10:21:12 -04001549 struct dirent *entry;
1550 DIR *d;
1551 struct stat s;
Robert Craige9887e42014-02-20 10:25:56 -05001552 int ret = 0;
1553
Robert Craigda30dc72014-03-27 10:21:12 -04001554 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1555 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1556
1557 if (!pkgName || !seinfo) {
1558 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001559 return -1;
1560 }
1561
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001562 // Special case for owner on internal storage
1563 if (uuid == nullptr) {
1564 std::string path(create_package_data_path(nullptr, pkgName, 0));
Robert Craigda30dc72014-03-27 10:21:12 -04001565
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001566 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, flags) < 0) {
1567 PLOG(ERROR) << "restorecon failed for " << path;
1568 ret |= -1;
1569 }
Robert Craige9887e42014-02-20 10:25:56 -05001570 }
1571
Robert Craigda30dc72014-03-27 10:21:12 -04001572 // Relabel package directory for all secondary users.
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001573 std::string userdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX);
1574 d = opendir(userdir.c_str());
Robert Craigda30dc72014-03-27 10:21:12 -04001575 if (d == NULL) {
Robert Craigda30dc72014-03-27 10:21:12 -04001576 return -1;
1577 }
1578
1579 while ((entry = readdir(d))) {
1580 if (entry->d_type != DT_DIR) {
1581 continue;
1582 }
1583
1584 const char *user = entry->d_name;
1585 // Ignore "." and ".."
1586 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1587 continue;
1588 }
1589
1590 // user directories start with a number
1591 if (user[0] < '0' || user[0] > '9') {
1592 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1593 continue;
1594 }
1595
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001596 std::string pkgdir(StringPrintf("%s/%s/%s", userdir.c_str(), user, pkgName));
1597 if (stat(pkgdir.c_str(), &s) < 0) {
Robert Craigda30dc72014-03-27 10:21:12 -04001598 continue;
1599 }
1600
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001601 if (selinux_android_restorecon_pkgdir(pkgdir.c_str(), seinfo, s.st_uid, flags) < 0) {
1602 PLOG(ERROR) << "restorecon failed for " << pkgdir;
Robert Craigda30dc72014-03-27 10:21:12 -04001603 ret |= -1;
1604 }
Robert Craigda30dc72014-03-27 10:21:12 -04001605 }
1606
1607 closedir(d);
Robert Craige9887e42014-02-20 10:25:56 -05001608 return ret;
1609}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001610
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001611int create_oat_dir(const char* oat_dir, const char* instruction_set)
1612{
1613 char oat_instr_dir[PKG_PATH_MAX];
1614
1615 if (validate_apk_path(oat_dir)) {
1616 ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
1617 return -1;
1618 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001619 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001620 return -1;
1621 }
1622 if (selinux_android_restorecon(oat_dir, 0)) {
1623 ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
1624 return -1;
1625 }
1626 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001627 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001628 return -1;
1629 }
1630 return 0;
1631}
1632
1633int rm_package_dir(const char* apk_path)
1634{
1635 if (validate_apk_path(apk_path)) {
1636 ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
1637 return -1;
1638 }
1639 return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */);
1640}
1641
1642int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
1643 const char *instruction_set) {
1644 char *file_name_start;
1645 char *file_name_end;
1646
1647 file_name_start = strrchr(apk_path, '/');
1648 if (file_name_start == NULL) {
1649 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
1650 return -1;
1651 }
1652 file_name_end = strrchr(apk_path, '.');
1653 if (file_name_end < file_name_start) {
1654 ALOGE("apk_path '%s' has no extension\n", apk_path);
1655 return -1;
1656 }
1657
1658 // Calculate file_name
1659 int file_name_len = file_name_end - file_name_start - 1;
1660 char file_name[file_name_len + 1];
1661 memcpy(file_name, file_name_start + 1, file_name_len);
1662 file_name[file_name_len] = '\0';
1663
1664 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
1665 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
1666 return 0;
1667}