blob: f178b1454c12651c041a5bc71974ccc56869b931 [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
Jeff Sharkeye3637242015-04-08 20:56:42 -070019#include <base/stringprintf.h>
20#include <base/logging.h>
21#include <cutils/sched_policy.h>
22#include <diskusage/dirsize.h>
23#include <logwrap/logwrap.h>
24#include <system/thread_defs.h>
25#include <selinux/android.h>
26
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070027#include <inttypes.h>
Nick Kralevichd7471292013-02-28 16:59:13 -080028#include <sys/capability.h>
Elliott Hughes2ead70c2015-02-16 10:44:22 -080029#include <sys/file.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070030#include <unistd.h>
Jeff Sharkey41ea4242015-04-09 11:34:03 -070031
32using android::base::StringPrintf;
Mike Lockwood94afecf2012-10-24 10:45:23 -070033
34/* Directory records that are used in execution of commands. */
35dir_rec_t android_data_dir;
36dir_rec_t android_asec_dir;
37dir_rec_t android_app_dir;
38dir_rec_t android_app_private_dir;
39dir_rec_t android_app_lib_dir;
40dir_rec_t android_media_dir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -070041dir_rec_t android_mnt_expand_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -070042dir_rec_array_t android_system_dirs;
43
Jeff Sharkeye3637242015-04-08 20:56:42 -070044static const char* kCpPath = "/system/bin/cp";
45
Jeff Sharkey63ec2d62015-11-09 13:10:36 -080046int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const char *seinfo) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070047 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
48 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
49 return -1;
50 }
51
Jeff Sharkeyebf728f2015-11-18 14:15:17 -070052 return make_user_data(uuid, pkgname, uid, 0, seinfo);
Mike Lockwood94afecf2012-10-24 10:45:23 -070053}
54
Jeff Sharkeyebf728f2015-11-18 14:15:17 -070055int uninstall(const char *uuid, const char *pkgname, userid_t userid) {
56 std::string ce_package_path(create_data_user_package_path(uuid, userid, pkgname));
57 std::string de_package_path(create_data_user_de_package_path(uuid, userid, pkgname));
Mike Lockwood94afecf2012-10-24 10:45:23 -070058
Jeff Sharkeyebf728f2015-11-18 14:15:17 -070059 int res = 0;
60 res |= delete_dir_contents_and_dir(ce_package_path);
61 res |= delete_dir_contents_and_dir(de_package_path);
62 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -070063}
64
Jeff Sharkeyc03de092015-04-07 18:14:05 -070065int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid)
Mike Lockwood94afecf2012-10-24 10:45:23 -070066{
Mike Lockwood94afecf2012-10-24 10:45:23 -070067 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -070068
69 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
70 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
71 return -1;
72 }
73
Jeff Sharkeyebf728f2015-11-18 14:15:17 -070074 // TODO: handle user_de paths
Jeff Sharkeyd7921182015-04-30 15:58:19 -070075 std::string _pkgdir(create_data_user_package_path(uuid, 0, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -070076 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070077
78 if (stat(pkgdir, &s) < 0) return -1;
79
80 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070081 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 -070082 return -1;
83 }
84
85 if (chmod(pkgdir, 0751) < 0) {
86 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
87 unlink(pkgdir);
88 return -errno;
89 }
90 if (chown(pkgdir, uid, gid) < 0) {
91 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
92 unlink(pkgdir);
93 return -errno;
94 }
95
96 return 0;
97}
98
Jeff Sharkeyebf728f2015-11-18 14:15:17 -070099int delete_user_data(const char *uuid, const char *pkgname, userid_t userid) {
100 std::string ce_package_path(create_data_user_package_path(uuid, userid, pkgname));
101 std::string de_package_path(create_data_user_de_package_path(uuid, userid, pkgname));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700102
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700103 int res = 0;
104 res |= delete_dir_contents(ce_package_path);
105 res |= delete_dir_contents(de_package_path);
106 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700107}
108
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700109int make_user_data(const char *uuid, const char *pkgname, uid_t uid, userid_t userid,
110 const char* seinfo) {
111 std::string ce_package_path(create_data_user_package_path(uuid, userid, pkgname));
112 std::string de_package_path(create_data_user_de_package_path(uuid, userid, pkgname));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700113
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700114 const char* c_ce_package_path = ce_package_path.c_str();
115 const char* c_de_package_path = de_package_path.c_str();
116
117 if (fs_prepare_dir(c_ce_package_path, 0751, uid, uid) == -1) {
118 PLOG(ERROR) << "Failed to prepare " << ce_package_path;
119 unlink(c_ce_package_path);
120 return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700121 }
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700122 if (selinux_android_setfilecon(c_ce_package_path, pkgname, seinfo, uid) < 0) {
123 PLOG(ERROR) << "Failed to setfilecon " << ce_package_path;
124 unlink(c_ce_package_path);
125 return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700126 }
127
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700128 if (fs_prepare_dir(c_de_package_path, 0751, uid, uid) == -1) {
129 PLOG(ERROR) << "Failed to prepare " << de_package_path;
130 unlink(c_de_package_path);
131 return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700132 }
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700133 if (selinux_android_setfilecon(c_de_package_path, pkgname, seinfo, uid) < 0) {
134 PLOG(ERROR) << "Failed to setfilecon " << de_package_path;
135 unlink(c_de_package_path);
136 return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700137 }
138
139 return 0;
140}
141
Jeff Sharkey31f08982015-07-07 13:31:37 -0700142int copy_complete_app(const char *from_uuid, const char *to_uuid,
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700143 const char *package_name, const char *data_app_name, appid_t appid,
144 const char* seinfo) {
Jeff Sharkeye3637242015-04-08 20:56:42 -0700145 std::vector<userid_t> users = get_known_users(from_uuid);
146
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700147 // Copy app
148 {
149 std::string from(create_data_app_package_path(from_uuid, data_app_name));
150 std::string to(create_data_app_package_path(to_uuid, data_app_name));
151 std::string to_parent(create_data_app_path(to_uuid));
152
153 char *argv[] = {
154 (char*) kCpPath,
155 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
156 (char*) "-p", /* preserve timestamps, ownership, and permissions */
157 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
158 (char*) "-P", /* Do not follow symlinks [default] */
159 (char*) "-d", /* don't dereference symlinks */
160 (char*) from.c_str(),
161 (char*) to_parent.c_str()
162 };
163
164 LOG(DEBUG) << "Copying " << from << " to " << to;
165 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
166
167 if (rc != 0) {
168 LOG(ERROR) << "Failed copying " << from << " to " << to
169 << ": status " << rc;
170 goto fail;
171 }
172
173 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
174 LOG(ERROR) << "Failed to restorecon " << to;
175 goto fail;
176 }
177 }
178
179 // Copy private data for all known users
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700180 // TODO: handle user_de paths
Jeff Sharkeye3637242015-04-08 20:56:42 -0700181 for (auto user : users) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700182 std::string from(create_data_user_package_path(from_uuid, user, package_name));
183 std::string to(create_data_user_package_path(to_uuid, user, package_name));
184 std::string to_parent(create_data_user_path(to_uuid, user));
Jeff Sharkeye3637242015-04-08 20:56:42 -0700185
186 // Data source may not exist for all users; that's okay
187 if (access(from.c_str(), F_OK) != 0) {
188 LOG(INFO) << "Missing source " << from;
189 continue;
190 }
191
192 std::string user_path(create_data_user_path(to_uuid, user));
193 if (fs_prepare_dir(user_path.c_str(), 0771, AID_SYSTEM, AID_SYSTEM) != 0) {
194 LOG(ERROR) << "Failed to prepare user target " << user_path;
195 goto fail;
196 }
197
198 uid_t uid = multiuser_get_uid(user, appid);
199 if (make_user_data(to_uuid, package_name, uid, user, seinfo) != 0) {
200 LOG(ERROR) << "Failed to create package target " << to;
201 goto fail;
202 }
203
204 char *argv[] = {
205 (char*) kCpPath,
206 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
207 (char*) "-p", /* preserve timestamps, ownership, and permissions */
208 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
209 (char*) "-P", /* Do not follow symlinks [default] */
210 (char*) "-d", /* don't dereference symlinks */
211 (char*) from.c_str(),
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700212 (char*) to_parent.c_str()
Jeff Sharkeye3637242015-04-08 20:56:42 -0700213 };
214
215 LOG(DEBUG) << "Copying " << from << " to " << to;
216 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
217
218 if (rc != 0) {
219 LOG(ERROR) << "Failed copying " << from << " to " << to
220 << ": status " << rc;
221 goto fail;
222 }
Jeff Sharkeya2307ae2015-07-20 16:26:19 -0700223 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700224
Jeff Sharkeya2307ae2015-07-20 16:26:19 -0700225 if (restorecon_data(to_uuid, package_name, seinfo, multiuser_get_uid(0, appid)) != 0) {
226 LOG(ERROR) << "Failed to restorecon";
227 goto fail;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700228 }
229
Jeff Sharkey31f08982015-07-07 13:31:37 -0700230 // We let the framework scan the new location and persist that before
231 // deleting the data in the old location; this ordering ensures that
232 // we can recover from things like battery pulls.
Jeff Sharkeye3637242015-04-08 20:56:42 -0700233 return 0;
234
235fail:
236 // Nuke everything we might have already copied
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700237 {
238 std::string to(create_data_app_package_path(to_uuid, data_app_name));
239 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
240 LOG(WARNING) << "Failed to rollback " << to;
241 }
242 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700243 for (auto user : users) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700244 std::string to(create_data_user_package_path(to_uuid, user, package_name));
Jeff Sharkeye3637242015-04-08 20:56:42 -0700245 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
246 LOG(WARNING) << "Failed to rollback " << to;
247 }
248 }
249 return -1;
250}
251
Robin Lee7c8bec02014-06-10 18:46:26 +0100252int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700253{
Robin Lee095c7632014-04-25 15:05:19 +0100254 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700255 return -1;
256 }
257
258 return 0;
259}
260
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700261int delete_user(const char *uuid, userid_t userid) {
262 int res = 0;
Robin Lee095c7632014-04-25 15:05:19 +0100263
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700264 std::string data_path(create_data_user_path(uuid, userid));
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700265 std::string data_de_path(create_data_user_de_path(uuid, userid));
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700266 std::string media_path(create_data_media_path(uuid, userid));
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700267
268 res |= delete_dir_contents_and_dir(data_path);
269 res |= delete_dir_contents_and_dir(data_de_path);
270 res |= delete_dir_contents_and_dir(media_path);
Robin Lee095c7632014-04-25 15:05:19 +0100271
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700272 // Config paths only exist on internal storage
273 if (uuid == nullptr) {
274 char config_path[PATH_MAX];
275 if ((create_user_config_path(config_path, userid) != 0)
276 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700277 res = -1;
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700278 }
Robin Lee095c7632014-04-25 15:05:19 +0100279 }
280
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700281 return res;
Robin Lee095c7632014-04-25 15:05:19 +0100282}
283
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700284int delete_cache(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700285{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700286 std::string _cachedir(
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700287 create_data_user_package_path(uuid, userid, pkgname) + CACHE_DIR_POSTFIX);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700288 const char* cachedir = _cachedir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700289
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700290 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100291 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700292}
293
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700294int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid)
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700295{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700296 std::string _codecachedir(
Daichi Hironoa2ccb9e2015-06-24 15:57:06 +0900297 create_data_user_package_path(uuid, userid, pkgname) + CODE_CACHE_DIR_POSTFIX);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700298 const char* codecachedir = _codecachedir.c_str();
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700299
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700300 struct stat s;
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700301
Jeff Sharkey770180a2014-09-08 17:14:26 -0700302 /* it's okay if code cache is missing */
303 if (lstat(codecachedir, &s) == -1 && errno == ENOENT) {
304 return 0;
305 }
306
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700307 /* delete contents, not the directory, no exceptions */
308 return delete_dir_contents(codecachedir, 0, NULL);
309}
310
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311/* Try to ensure free_size bytes of storage are available.
312 * Returns 0 on success.
313 * This is rather simple-minded because doing a full LRU would
314 * be potentially memory-intensive, and without atime it would
315 * also require that apps constantly modify file metadata even
316 * when just reading from the cache, which is pretty awful.
317 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700318int free_cache(const char *uuid, int64_t free_size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700319{
320 cache_t* cache;
321 int64_t avail;
322 DIR *d;
323 struct dirent *de;
324 char tmpdir[PATH_MAX];
325 char *dirpos;
326
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700327 std::string data_path(create_data_path(uuid));
328
329 avail = data_disk_free(data_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700330 if (avail < 0) return -1;
331
332 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
333 if (avail >= free_size) return 0;
334
335 cache = start_cache_collection();
336
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700337 // Special case for owner on internal storage
338 if (uuid == nullptr) {
339 std::string _tmpdir(create_data_user_path(nullptr, 0));
340 add_cache_files(cache, _tmpdir.c_str(), "cache");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700341 }
342
343 // Search for other users and add any cache files from them.
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700344 std::string _tmpdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX);
345 strcpy(tmpdir, _tmpdir.c_str());
346
Mike Lockwood94afecf2012-10-24 10:45:23 -0700347 dirpos = tmpdir + strlen(tmpdir);
348 d = opendir(tmpdir);
349 if (d != NULL) {
350 while ((de = readdir(d))) {
351 if (de->d_type == DT_DIR) {
352 const char *name = de->d_name;
353 /* always skip "." and ".." */
354 if (name[0] == '.') {
355 if (name[1] == 0) continue;
356 if ((name[1] == '.') && (name[2] == 0)) continue;
357 }
358 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
359 strcpy(dirpos, name);
360 //ALOGI("adding cache files from %s\n", tmpdir);
361 add_cache_files(cache, tmpdir, "cache");
362 } else {
363 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
364 }
365 }
366 }
367 closedir(d);
368 }
369
370 // Collect cache files on external storage for all users (if it is mounted as part
371 // of the internal storage).
372 strcpy(tmpdir, android_media_dir.path);
373 dirpos = tmpdir + strlen(tmpdir);
374 d = opendir(tmpdir);
375 if (d != NULL) {
376 while ((de = readdir(d))) {
377 if (de->d_type == DT_DIR) {
378 const char *name = de->d_name;
379 /* skip any dir that doesn't start with a number, so not a user */
380 if (name[0] < '0' || name[0] > '9') {
381 continue;
382 }
383 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
384 strcpy(dirpos, name);
385 if (lookup_media_dir(tmpdir, "Android") == 0
386 && lookup_media_dir(tmpdir, "data") == 0) {
387 //ALOGI("adding cache files from %s\n", tmpdir);
388 add_cache_files(cache, tmpdir, "cache");
389 }
390 } else {
391 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
392 }
393 }
394 }
395 closedir(d);
396 }
397
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700398 clear_cache_files(data_path, cache, free_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700399 finish_cache_collection(cache);
400
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700401 return data_disk_free(data_path) >= free_size ? 0 : -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700402}
403
Narayan Kamath1b400322014-04-11 13:17:00 +0100404int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700405{
406 char src_dex[PKG_PATH_MAX];
407 char dst_dex[PKG_PATH_MAX];
408
Jeff Sharkey770180a2014-09-08 17:14:26 -0700409 if (validate_apk_path(src)) {
410 ALOGE("invalid apk path '%s' (bad prefix)\n", src);
411 return -1;
412 }
413 if (validate_apk_path(dst)) {
414 ALOGE("invalid apk path '%s' (bad prefix)\n", dst);
415 return -1;
416 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700417
Narayan Kamath1b400322014-04-11 13:17:00 +0100418 if (create_cache_path(src_dex, src, instruction_set)) return -1;
419 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700420
421 ALOGV("move %s -> %s\n", src_dex, dst_dex);
422 if (rename(src_dex, dst_dex) < 0) {
423 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
424 return -1;
425 } else {
426 return 0;
427 }
428}
429
Narayan Kamath1b400322014-04-11 13:17:00 +0100430int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700431{
432 char dex_path[PKG_PATH_MAX];
433
Jeff Sharkey770180a2014-09-08 17:14:26 -0700434 if (validate_apk_path(path) && validate_system_app_path(path)) {
435 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
436 return -1;
437 }
438
Narayan Kamath1b400322014-04-11 13:17:00 +0100439 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700440
441 ALOGV("unlink %s\n", dex_path);
442 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700443 if (errno != ENOENT) {
444 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
445 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700446 return -1;
447 } else {
448 return 0;
449 }
450}
451
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700452int get_size(const char *uuid, const char *pkgname, int userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700453 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100454 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
455 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700456{
457 DIR *d;
458 int dfd;
459 struct dirent *de;
460 struct stat s;
461 char path[PKG_PATH_MAX];
462
463 int64_t codesize = 0;
464 int64_t datasize = 0;
465 int64_t cachesize = 0;
466 int64_t asecsize = 0;
467
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700468 /* count the source apk as code -- but only if it's not
469 * on the /system partition and its not on the sdcard. */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700470 if (validate_system_app_path(apkpath) &&
471 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
472 if (stat(apkpath, &s) == 0) {
473 codesize += stat_size(&s);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700474 if (S_ISDIR(s.st_mode)) {
475 d = opendir(apkpath);
476 if (d != NULL) {
477 dfd = dirfd(d);
478 codesize += calculate_dir_size(dfd);
479 closedir(d);
480 }
481 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700482 }
483 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700484
485 /* count the forward locked apk as code if it is given */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700486 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
487 if (stat(fwdlock_apkpath, &s) == 0) {
488 codesize += stat_size(&s);
489 }
490 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700491
492 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100493 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700494 if (stat(path, &s) == 0) {
495 codesize += stat_size(&s);
496 }
497 }
498
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700499 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700500 if (libdirpath != NULL && libdirpath[0] != '!') {
501 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700502 if (d != NULL) {
503 dfd = dirfd(d);
504 codesize += calculate_dir_size(dfd);
505 closedir(d);
506 }
507 }
508
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700509 /* compute asec size if it is given */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700510 if (asecpath != NULL && asecpath[0] != '!') {
511 if (stat(asecpath, &s) == 0) {
512 asecsize += stat_size(&s);
513 }
514 }
515
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700516 std::vector<userid_t> users;
517 if (userid == -1) {
518 users = get_known_users(uuid);
519 } else {
520 users.push_back(userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700521 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700522
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700523 for (auto user : users) {
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700524 // TODO: handle user_de directories
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700525 std::string _pkgdir(create_data_user_package_path(uuid, user, pkgname));
526 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700527
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700528 d = opendir(pkgdir);
529 if (d == NULL) {
530 PLOG(WARNING) << "Failed to open " << pkgdir;
531 continue;
532 }
533 dfd = dirfd(d);
534
535 /* most stuff in the pkgdir is data, except for the "cache"
536 * directory and below, which is cache, and the "lib" directory
537 * and below, which is code...
538 */
539 while ((de = readdir(d))) {
540 const char *name = de->d_name;
541
542 if (de->d_type == DT_DIR) {
543 int subfd;
544 int64_t statsize = 0;
545 int64_t dirsize = 0;
546 /* always skip "." and ".." */
547 if (name[0] == '.') {
548 if (name[1] == 0) continue;
549 if ((name[1] == '.') && (name[2] == 0)) continue;
550 }
551 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
552 statsize = stat_size(&s);
553 }
554 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
555 if (subfd >= 0) {
556 dirsize = calculate_dir_size(subfd);
557 }
558 if(!strcmp(name,"lib")) {
559 codesize += dirsize + statsize;
560 } else if(!strcmp(name,"cache")) {
561 cachesize += dirsize + statsize;
562 } else {
563 datasize += dirsize + statsize;
564 }
565 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
566 // This is the symbolic link to the application's library
567 // code. We'll count this as code instead of data, since
568 // it is not something that the app creates.
569 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
570 codesize += stat_size(&s);
571 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700572 } else {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700573 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
574 datasize += stat_size(&s);
575 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700576 }
577 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700578 closedir(d);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700579 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700580 *_codesize = codesize;
581 *_datasize = datasize;
582 *_cachesize = cachesize;
583 *_asecsize = asecsize;
584 return 0;
585}
586
Narayan Kamath1b400322014-04-11 13:17:00 +0100587int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700588{
589 char *tmp;
590 int srclen;
591 int dstlen;
592
593 srclen = strlen(src);
594
595 /* demand that we are an absolute path */
596 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
597 return -1;
598 }
599
600 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
601 return -1;
602 }
603
Dave Allisond9370732014-01-30 14:19:23 -0800604 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100605 strlen(instruction_set) +
606 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800607
Mike Lockwood94afecf2012-10-24 10:45:23 -0700608 if (dstlen > PKG_PATH_MAX) {
609 return -1;
610 }
611
Narayan Kamath1b400322014-04-11 13:17:00 +0100612 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700613 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100614 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700615 src + 1, /* skip the leading / */
616 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800617
Narayan Kamath1b400322014-04-11 13:17:00 +0100618 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700619 if (*tmp == '/') {
620 *tmp = '@';
621 }
622 }
623
624 return 0;
625}
626
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700627static int split_count(const char *str)
628{
629 char *ctx;
630 int count = 0;
631 char buf[PROPERTY_VALUE_MAX];
632
633 strncpy(buf, str, sizeof(buf));
634 char *pBuf = buf;
635
636 while(strtok_r(pBuf, " ", &ctx) != NULL) {
637 count++;
638 pBuf = NULL;
639 }
640
641 return count;
642}
643
neo.chae14e084d2015-01-07 18:46:13 +0900644static int split(char *buf, const char **argv)
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700645{
646 char *ctx;
647 int count = 0;
648 char *tok;
649 char *pBuf = buf;
650
651 while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
652 argv[count++] = tok;
653 pBuf = NULL;
654 }
655
656 return count;
657}
658
Alex Light7365a102014-07-21 12:23:48 -0700659static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700660 const char* output_file_name, const char *pkgname __unused, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700661{
662 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100663 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700664
665 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
666 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
667 ALOGE("Instruction set %s longer than max length of %d",
668 instruction_set, MAX_INSTRUCTION_SET_LEN);
669 return;
670 }
671
672 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
673 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
674 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
675 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
676 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
677 // The caller has already gotten all the locks we need.
678 const char* no_lock_arg = "--no-lock-output";
679 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
680 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
681 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700682 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700683 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
684
685 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
686 char* argv[7];
687 argv[0] = (char*) PATCHOAT_BIN;
688 argv[1] = (char*) patched_image_location_arg;
689 argv[2] = (char*) no_lock_arg;
690 argv[3] = instruction_set_arg;
691 argv[4] = output_oat_fd_arg;
692 argv[5] = input_oat_fd_arg;
693 argv[6] = NULL;
694
695 execv(PATCHOAT_BIN, (char* const *)argv);
696 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
697}
698
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700699static bool check_boolean_property(const char* property_name, bool default_value = false) {
700 char tmp_property_value[PROPERTY_VALUE_MAX];
701 bool have_property = property_get(property_name, tmp_property_value, nullptr) > 0;
702 if (!have_property) {
703 return default_value;
704 }
705 return strcmp(tmp_property_value, "true") == 0;
706}
707
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700708static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Calin Juravledf9dadd2015-11-04 14:47:37 +0000709 const char* output_file_name, int swap_fd, const char *instruction_set,
Todd Kennedy12434f82015-09-25 14:45:37 -0700710 bool vm_safe_mode, bool debuggable, bool post_bootcomplete, bool use_jit)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700711{
Calin Juravle8fc73152014-08-19 18:48:50 +0100712 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
713
714 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
715 ALOGE("Instruction set %s longer than max length of %d",
716 instruction_set, MAX_INSTRUCTION_SET_LEN);
717 return;
718 }
719
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700720 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
721 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
722
723 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
724 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
725
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700726 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
727 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
728 dex2oat_compiler_filter_flag, NULL) > 0;
729
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700730 char dex2oat_threads_buf[PROPERTY_VALUE_MAX];
Andreas Gampe919461c2015-09-28 08:55:01 -0700731 bool have_dex2oat_threads_flag = property_get(post_bootcomplete
732 ? "dalvik.vm.dex2oat-threads"
733 : "dalvik.vm.boot-dex2oat-threads",
734 dex2oat_threads_buf,
735 NULL) > 0;
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700736 char dex2oat_threads_arg[PROPERTY_VALUE_MAX + 2];
737 if (have_dex2oat_threads_flag) {
738 sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf);
739 }
740
Calin Juravle8fc73152014-08-19 18:48:50 +0100741 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
742 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
743 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
744 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
745 dex2oat_isa_features, NULL) > 0;
746
Ian Rogers16a95b22014-11-08 16:58:13 -0800747 char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
748 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
749 char dex2oat_isa_variant[PROPERTY_VALUE_MAX];
750 bool have_dex2oat_isa_variant = property_get(dex2oat_isa_variant_key,
751 dex2oat_isa_variant, NULL) > 0;
752
neo.chae14e084d2015-01-07 18:46:13 +0900753 const char *dex2oat_norelocation = "-Xnorelocate";
754 bool have_dex2oat_relocation_skip_flag = false;
755
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800756 char dex2oat_flags[PROPERTY_VALUE_MAX];
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700757 int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags",
758 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800759 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
760
Brian Carlstrom538998f2014-07-30 14:37:11 -0700761 // If we booting without the real /data, don't spend time compiling.
762 char vold_decrypt[PROPERTY_VALUE_MAX];
763 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
764 bool skip_compilation = (have_vold_decrypt &&
765 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
766 (strcmp(vold_decrypt, "1") == 0)));
767
David Srbecky528c8dd2015-05-28 16:55:50 +0100768 bool generate_debug_info = check_boolean_property("debug.generate-debug-info");
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700769
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700770 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700771
Brian Carlstrom53e07762014-06-27 14:15:19 -0700772 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700773
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700774 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100775
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700776 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
777 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
778 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700779 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100780 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Ian Rogers16a95b22014-11-08 16:58:13 -0800781 char instruction_set_variant_arg[strlen("--instruction-set-variant=") + PROPERTY_VALUE_MAX];
Calin Juravle8fc73152014-08-19 18:48:50 +0100782 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700783 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
784 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700785 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800786 bool have_dex2oat_swap_fd = false;
787 char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700788
789 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
790 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
791 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
792 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100793 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Ian Rogers16a95b22014-11-08 16:58:13 -0800794 sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
Calin Juravle8fc73152014-08-19 18:48:50 +0100795 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Andreas Gampee1c01352014-12-10 16:41:11 -0800796 if (swap_fd >= 0) {
797 have_dex2oat_swap_fd = true;
798 sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
799 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100800
Todd Kennedy12434f82015-09-25 14:45:37 -0700801 // use the JIT if either it's specified as a dexopt flag or if the property is set
802 use_jit = use_jit || check_boolean_property("debug.usejit");
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700803 if (have_dex2oat_Xms_flag) {
804 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
805 }
806 if (have_dex2oat_Xmx_flag) {
807 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
808 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700809 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700810 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700811 have_dex2oat_compiler_filter_flag = true;
neo.chae14e084d2015-01-07 18:46:13 +0900812 have_dex2oat_relocation_skip_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100813 } else if (vm_safe_mode) {
814 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100815 have_dex2oat_compiler_filter_flag = true;
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700816 } else if (use_jit) {
817 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-at-runtime");
818 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700819 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700820 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
821 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700822
Andreas Gampe598c25e2015-03-03 09:15:06 -0800823 // Check whether all apps should be compiled debuggable.
824 if (!debuggable) {
Calin Juravledf9dadd2015-11-04 14:47:37 +0000825 char prop_buf[PROPERTY_VALUE_MAX];
Andreas Gampe598c25e2015-03-03 09:15:06 -0800826 debuggable =
827 (property_get("dalvik.vm.always_debuggable", prop_buf, "0") > 0) &&
828 (prop_buf[0] == '1');
829 }
830
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700831 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100832
neo.chae14e084d2015-01-07 18:46:13 +0900833 const char* argv[7 // program name, mandatory arguments and the final NULL
834 + (have_dex2oat_isa_variant ? 1 : 0)
835 + (have_dex2oat_isa_features ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900836 + (have_dex2oat_Xms_flag ? 2 : 0)
837 + (have_dex2oat_Xmx_flag ? 2 : 0)
838 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700839 + (have_dex2oat_threads_flag ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900840 + (have_dex2oat_swap_fd ? 1 : 0)
841 + (have_dex2oat_relocation_skip_flag ? 2 : 0)
David Srbecky528c8dd2015-05-28 16:55:50 +0100842 + (generate_debug_info ? 1 : 0)
Andreas Gampe598c25e2015-03-03 09:15:06 -0800843 + (debuggable ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900844 + dex2oat_flags_count];
Calin Juravle4fdff462014-06-06 16:58:43 +0100845 int i = 0;
neo.chae14e084d2015-01-07 18:46:13 +0900846 argv[i++] = DEX2OAT_BIN;
Calin Juravle4fdff462014-06-06 16:58:43 +0100847 argv[i++] = zip_fd_arg;
848 argv[i++] = zip_location_arg;
849 argv[i++] = oat_fd_arg;
850 argv[i++] = oat_location_arg;
851 argv[i++] = instruction_set_arg;
Ian Rogers16a95b22014-11-08 16:58:13 -0800852 if (have_dex2oat_isa_variant) {
853 argv[i++] = instruction_set_variant_arg;
854 }
Calin Juravle8fc73152014-08-19 18:48:50 +0100855 if (have_dex2oat_isa_features) {
856 argv[i++] = instruction_set_features_arg;
857 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700858 if (have_dex2oat_Xms_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900859 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700860 argv[i++] = dex2oat_Xms_arg;
861 }
862 if (have_dex2oat_Xmx_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900863 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700864 argv[i++] = dex2oat_Xmx_arg;
865 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700866 if (have_dex2oat_compiler_filter_flag) {
867 argv[i++] = dex2oat_compiler_filter_arg;
868 }
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700869 if (have_dex2oat_threads_flag) {
870 argv[i++] = dex2oat_threads_arg;
871 }
Andreas Gampee1c01352014-12-10 16:41:11 -0800872 if (have_dex2oat_swap_fd) {
873 argv[i++] = dex2oat_swap_fd;
874 }
David Srbecky528c8dd2015-05-28 16:55:50 +0100875 if (generate_debug_info) {
876 argv[i++] = "--generate-debug-info";
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700877 }
Andreas Gampe598c25e2015-03-03 09:15:06 -0800878 if (debuggable) {
879 argv[i++] = "--debuggable";
880 }
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700881 if (dex2oat_flags_count) {
882 i += split(dex2oat_flags, argv + i);
Calin Juravle4fdff462014-06-06 16:58:43 +0100883 }
neo.chae14e084d2015-01-07 18:46:13 +0900884 if (have_dex2oat_relocation_skip_flag) {
885 argv[i++] = RUNTIME_ARG;
886 argv[i++] = dex2oat_norelocation;
887 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700888 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100889 argv[i] = NULL;
890
neo.chae14e084d2015-01-07 18:46:13 +0900891 execv(DEX2OAT_BIN, (char * const *)argv);
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700892 ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700893}
894
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100895static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700896{
897 int status;
898 pid_t got_pid;
899
Mike Lockwood94afecf2012-10-24 10:45:23 -0700900 while (1) {
901 got_pid = waitpid(pid, &status, 0);
902 if (got_pid == -1 && errno == EINTR) {
903 printf("waitpid interrupted, retrying\n");
904 } else {
905 break;
906 }
907 }
908 if (got_pid != pid) {
909 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
910 (int) pid, (int) got_pid, strerror(errno));
911 return 1;
912 }
913
914 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700915 return 0;
916 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700917 return status; /* always nonzero */
918 }
919}
920
Andreas Gampee1c01352014-12-10 16:41:11 -0800921/*
Andreas Gampec968c012015-07-16 15:55:41 -0700922 * Whether dexopt should use a swap file when compiling an APK.
923 *
924 * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision
925 * itself, anyways).
926 *
927 * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true".
928 *
929 * Otherwise, return true if this is a low-mem device.
930 *
931 * Otherwise, return default value.
Andreas Gampee1c01352014-12-10 16:41:11 -0800932 */
Andreas Gampec968c012015-07-16 15:55:41 -0700933static bool kAlwaysProvideSwapFile = false;
934static bool kDefaultProvideSwapFile = true;
Andreas Gampee1c01352014-12-10 16:41:11 -0800935
936static bool ShouldUseSwapFileForDexopt() {
937 if (kAlwaysProvideSwapFile) {
938 return true;
939 }
940
Andreas Gampec968c012015-07-16 15:55:41 -0700941 // Check the "override" property. If it exists, return value == "true".
942 char dex2oat_prop_buf[PROPERTY_VALUE_MAX];
943 if (property_get("dalvik.vm.dex2oat-swap", dex2oat_prop_buf, "") > 0) {
944 if (strcmp(dex2oat_prop_buf, "true") == 0) {
945 return true;
946 } else {
947 return false;
948 }
949 }
950
951 // Shortcut for default value. This is an implementation optimization for the process sketched
952 // above. If the default value is true, we can avoid to check whether this is a low-mem device,
953 // as low-mem is never returning false. The compiler will optimize this away if it can.
954 if (kDefaultProvideSwapFile) {
955 return true;
956 }
957
958 bool is_low_mem = check_boolean_property("ro.config.low_ram");
959 if (is_low_mem) {
960 return true;
961 }
962
963 // Default value must be false here.
964 return kDefaultProvideSwapFile;
Andreas Gampee1c01352014-12-10 16:41:11 -0800965}
966
Richard Uhler009b8772015-03-18 12:39:09 -0700967/*
968 * Computes the odex file for the given apk_path and instruction_set.
969 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
970 *
971 * Returns false if it failed to determine the odex file path.
972 */
973static bool calculate_odex_file_path(char path[PKG_PATH_MAX],
974 const char *apk_path,
975 const char *instruction_set)
976{
977 if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
978 + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
979 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
980 return false;
981 }
982
983 strcpy(path, apk_path);
984 char *end = strrchr(path, '/');
985 if (end == NULL) {
986 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
987 return false;
988 }
989 const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
990
991 strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0
992 strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
993 strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0
994 end = strrchr(path, '.');
995 if (end == NULL) {
996 ALOGE("apk_path '%s' has no extension.\n", apk_path);
997 return false;
998 }
999 strcpy(end + 1, "odex");
1000 return true;
1001}
1002
Andreas Gampe94dd3d32015-09-14 16:33:11 -07001003static void SetDex2OatAndPatchOatScheduling(bool set_to_bg) {
1004 if (set_to_bg) {
1005 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
1006 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
1007 exit(70);
1008 }
1009 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
1010 ALOGE("setpriority failed: %s\n", strerror(errno));
1011 exit(71);
1012 }
1013 }
1014}
1015
Todd Kennedy76e767c2015-09-25 07:47:47 -07001016int dexopt(const char *apk_path, uid_t uid, const char *pkgname, const char *instruction_set,
1017 int dexopt_needed, const char* oat_dir, int dexopt_flags)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001018{
1019 struct utimbuf ut;
Fyodor Kupolov26ff93c2015-04-02 16:59:10 -07001020 struct stat input_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001021 char out_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -08001022 char swap_file_name[PKG_PATH_MAX];
Alex Light7365a102014-07-21 12:23:48 -07001023 const char *input_file;
1024 char in_odex_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -08001025 int res, input_fd=-1, out_fd=-1, swap_fd=-1;
Todd Kennedy76e767c2015-09-25 07:47:47 -07001026 bool is_public = (dexopt_flags & DEXOPT_PUBLIC) != 0;
1027 bool vm_safe_mode = (dexopt_flags & DEXOPT_SAFEMODE) != 0;
1028 bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0;
1029 bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0;
Todd Kennedy12434f82015-09-25 14:45:37 -07001030 bool use_jit = (dexopt_flags & DEXOPT_USEJIT) != 0;
Todd Kennedy76e767c2015-09-25 07:47:47 -07001031
1032 if ((dexopt_flags & DEXOPT_MASK) != 0) {
1033 LOG_FATAL("dexopt flags contains unknown fields\n");
1034 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001035
Andreas Gampee1c01352014-12-10 16:41:11 -08001036 // Early best-effort check whether we can fit the the path into our buffers.
1037 // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
1038 // without a swap file, if necessary.
Mike Lockwood94afecf2012-10-24 10:45:23 -07001039 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001040 ALOGE("apk_path too long '%s'\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001041 return -1;
1042 }
1043
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001044 if (oat_dir != NULL && oat_dir[0] != '!') {
1045 if (validate_apk_path(oat_dir)) {
1046 ALOGE("invalid oat_dir '%s'\n", oat_dir);
1047 return -1;
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +08001048 }
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001049 if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) {
1050 return -1;
1051 }
1052 } else {
1053 if (create_cache_path(out_path, apk_path, instruction_set)) {
1054 return -1;
1055 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001056 }
1057
Richard Uhlerc92fb622015-03-26 15:47:38 -07001058 switch (dexopt_needed) {
1059 case DEXOPT_DEX2OAT_NEEDED:
1060 input_file = apk_path;
1061 break;
1062
1063 case DEXOPT_PATCHOAT_NEEDED:
1064 if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
1065 return -1;
1066 }
1067 input_file = in_odex_path;
1068 break;
1069
1070 case DEXOPT_SELF_PATCHOAT_NEEDED:
1071 input_file = out_path;
1072 break;
1073
1074 default:
1075 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1076 exit(72);
Alex Light7365a102014-07-21 12:23:48 -07001077 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001078
Alex Light7365a102014-07-21 12:23:48 -07001079 memset(&input_stat, 0, sizeof(input_stat));
1080 stat(input_file, &input_stat);
1081
1082 input_fd = open(input_file, O_RDONLY, 0);
1083 if (input_fd < 0) {
1084 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001085 return -1;
1086 }
1087
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001088 unlink(out_path);
1089 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1090 if (out_fd < 0) {
1091 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001092 goto fail;
1093 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001094 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -07001095 S_IRUSR|S_IWUSR|S_IRGRP |
1096 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001097 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001098 goto fail;
1099 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001100 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
1101 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001102 goto fail;
1103 }
1104
Andreas Gampee1c01352014-12-10 16:41:11 -08001105 // Create a swap file if necessary.
Richard Uhlerc92fb622015-03-26 15:47:38 -07001106 if (ShouldUseSwapFileForDexopt()) {
Andreas Gampee1c01352014-12-10 16:41:11 -08001107 // Make sure there really is enough space.
1108 size_t out_len = strlen(out_path);
1109 if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
1110 strcpy(swap_file_name, out_path);
1111 strcpy(swap_file_name + strlen(out_path), ".swap");
1112 unlink(swap_file_name);
1113 swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600);
1114 if (swap_fd < 0) {
1115 // Could not create swap file. Optimistically go on and hope that we can compile
1116 // without it.
1117 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
1118 } else {
1119 // Immediately unlink. We don't really want to hit flash.
1120 unlink(swap_file_name);
1121 }
1122 } else {
1123 // Swap file path is too long. Try to run without.
1124 ALOGE("installd could not create swap file for path %s during dexopt\n", out_path);
1125 }
1126 }
Dave Allisond9370732014-01-30 14:19:23 -08001127
Alex Light7365a102014-07-21 12:23:48 -07001128 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001129
1130 pid_t pid;
1131 pid = fork();
1132 if (pid == 0) {
1133 /* child -- drop privileges before continuing */
1134 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001135 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001136 exit(64);
1137 }
1138 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001139 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001140 exit(65);
1141 }
1142 // drop capabilities
1143 struct __user_cap_header_struct capheader;
1144 struct __user_cap_data_struct capdata[2];
1145 memset(&capheader, 0, sizeof(capheader));
1146 memset(&capdata, 0, sizeof(capdata));
1147 capheader.version = _LINUX_CAPABILITY_VERSION_3;
1148 if (capset(&capheader, &capdata[0]) < 0) {
1149 ALOGE("capset failed: %s\n", strerror(errno));
1150 exit(66);
1151 }
Andreas Gampe13f14192015-09-21 13:21:30 -07001152 SetDex2OatAndPatchOatScheduling(boot_complete);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001153 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
1154 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001155 exit(67);
1156 }
1157
Richard Uhlerc92fb622015-03-26 15:47:38 -07001158 if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED
1159 || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) {
Andreas Gampebd872e42014-12-15 11:41:11 -08001160 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001161 } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001162 const char *input_file_name = strrchr(input_file, '/');
1163 if (input_file_name == NULL) {
1164 input_file_name = input_file;
1165 } else {
1166 input_file_name++;
1167 }
Calin Juravledf9dadd2015-11-04 14:47:37 +00001168 run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd,
Todd Kennedy12434f82015-09-25 14:45:37 -07001169 instruction_set, vm_safe_mode, debuggable, boot_complete, use_jit);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001170 } else {
1171 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1172 exit(73);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001173 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001174 exit(68); /* only get here on exec failure */
1175 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001176 res = wait_child(pid);
1177 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001178 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001179 } else {
Alex Light7365a102014-07-21 12:23:48 -07001180 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001181 goto fail;
1182 }
1183 }
1184
Alex Light7365a102014-07-21 12:23:48 -07001185 ut.actime = input_stat.st_atime;
1186 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001187 utime(out_path, &ut);
1188
1189 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001190 close(input_fd);
Andreas Gampee1c01352014-12-10 16:41:11 -08001191 if (swap_fd != -1) {
1192 close(swap_fd);
1193 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001194 return 0;
1195
1196fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001197 if (out_fd >= 0) {
1198 close(out_fd);
1199 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001200 }
Alex Light7365a102014-07-21 12:23:48 -07001201 if (input_fd >= 0) {
1202 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001203 }
1204 return -1;
1205}
1206
Narayan Kamath091ea772014-11-10 15:03:46 +00001207int mark_boot_complete(const char* instruction_set)
1208{
1209 char boot_marker_path[PKG_PATH_MAX];
1210 sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set);
1211
1212 ALOGV("mark_boot_complete : %s", boot_marker_path);
1213 if (unlink(boot_marker_path) != 0) {
1214 ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1215 strerror(errno));
1216 return -1;
1217 }
1218
1219 return 0;
1220}
1221
Mike Lockwood94afecf2012-10-24 10:45:23 -07001222void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1223 struct stat* statbuf)
1224{
1225 while (path[basepos] != 0) {
1226 if (path[basepos] == '/') {
1227 path[basepos] = 0;
1228 if (lstat(path, statbuf) < 0) {
1229 ALOGV("Making directory: %s\n", path);
1230 if (mkdir(path, mode) == 0) {
1231 chown(path, uid, gid);
1232 } else {
1233 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1234 }
1235 }
1236 path[basepos] = '/';
1237 basepos++;
1238 }
1239 basepos++;
1240 }
1241}
1242
1243int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1244 int dstuid, int dstgid, struct stat* statbuf)
1245{
1246 DIR *d;
1247 struct dirent *de;
1248 int res;
1249
1250 int srcend = strlen(srcpath);
1251 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001252
Mike Lockwood94afecf2012-10-24 10:45:23 -07001253 if (lstat(srcpath, statbuf) < 0) {
1254 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1255 return 1;
1256 }
Dave Allisond9370732014-01-30 14:19:23 -08001257
Mike Lockwood94afecf2012-10-24 10:45:23 -07001258 if ((statbuf->st_mode&S_IFDIR) == 0) {
1259 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1260 dstuid, dstgid, statbuf);
1261 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1262 if (rename(srcpath, dstpath) >= 0) {
1263 if (chown(dstpath, dstuid, dstgid) < 0) {
1264 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1265 unlink(dstpath);
1266 return 1;
1267 }
1268 } else {
1269 ALOGW("Unable to rename %s to %s: %s\n",
1270 srcpath, dstpath, strerror(errno));
1271 return 1;
1272 }
1273 return 0;
1274 }
1275
1276 d = opendir(srcpath);
1277 if (d == NULL) {
1278 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1279 return 1;
1280 }
1281
1282 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001283
Mike Lockwood94afecf2012-10-24 10:45:23 -07001284 while ((de = readdir(d))) {
1285 const char *name = de->d_name;
1286 /* always skip "." and ".." */
1287 if (name[0] == '.') {
1288 if (name[1] == 0) continue;
1289 if ((name[1] == '.') && (name[2] == 0)) continue;
1290 }
Dave Allisond9370732014-01-30 14:19:23 -08001291
Mike Lockwood94afecf2012-10-24 10:45:23 -07001292 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1293 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1294 continue;
1295 }
Dave Allisond9370732014-01-30 14:19:23 -08001296
Mike Lockwood94afecf2012-10-24 10:45:23 -07001297 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1298 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1299 continue;
1300 }
Dave Allisond9370732014-01-30 14:19:23 -08001301
Mike Lockwood94afecf2012-10-24 10:45:23 -07001302 srcpath[srcend] = dstpath[dstend] = '/';
1303 strcpy(srcpath+srcend+1, name);
1304 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001305
Mike Lockwood94afecf2012-10-24 10:45:23 -07001306 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1307 res = 1;
1308 }
Dave Allisond9370732014-01-30 14:19:23 -08001309
Mike Lockwood94afecf2012-10-24 10:45:23 -07001310 // Note: we will be leaving empty directories behind in srcpath,
1311 // but that is okay, the package manager will be erasing all of the
1312 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001313
Mike Lockwood94afecf2012-10-24 10:45:23 -07001314 srcpath[srcend] = dstpath[dstend] = 0;
1315 }
Dave Allisond9370732014-01-30 14:19:23 -08001316
Mike Lockwood94afecf2012-10-24 10:45:23 -07001317 closedir(d);
1318 return res;
1319}
1320
1321int movefiles()
1322{
1323 DIR *d;
1324 int dfd, subfd;
1325 struct dirent *de;
1326 struct stat s;
1327 char buf[PKG_PATH_MAX+1];
1328 int bufp, bufe, bufi, readlen;
1329
1330 char srcpkg[PKG_NAME_MAX];
1331 char dstpkg[PKG_NAME_MAX];
1332 char srcpath[PKG_PATH_MAX];
1333 char dstpath[PKG_PATH_MAX];
1334 int dstuid=-1, dstgid=-1;
1335 int hasspace;
1336
1337 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1338 if (d == NULL) {
1339 goto done;
1340 }
1341 dfd = dirfd(d);
1342
1343 /* Iterate through all files in the directory, executing the
1344 * file movements requested there-in.
1345 */
1346 while ((de = readdir(d))) {
1347 const char *name = de->d_name;
1348
1349 if (de->d_type == DT_DIR) {
1350 continue;
1351 } else {
1352 subfd = openat(dfd, name, O_RDONLY);
1353 if (subfd < 0) {
1354 ALOGW("Unable to open update commands at %s%s\n",
1355 UPDATE_COMMANDS_DIR_PREFIX, name);
1356 continue;
1357 }
Dave Allisond9370732014-01-30 14:19:23 -08001358
Mike Lockwood94afecf2012-10-24 10:45:23 -07001359 bufp = 0;
1360 bufe = 0;
1361 buf[PKG_PATH_MAX] = 0;
1362 srcpkg[0] = dstpkg[0] = 0;
1363 while (1) {
1364 bufi = bufp;
1365 while (bufi < bufe && buf[bufi] != '\n') {
1366 bufi++;
1367 }
1368 if (bufi < bufe) {
1369 buf[bufi] = 0;
1370 ALOGV("Processing line: %s\n", buf+bufp);
1371 hasspace = 0;
1372 while (bufp < bufi && isspace(buf[bufp])) {
1373 hasspace = 1;
1374 bufp++;
1375 }
1376 if (buf[bufp] == '#' || bufp == bufi) {
1377 // skip comments and empty lines.
1378 } else if (hasspace) {
1379 if (dstpkg[0] == 0) {
1380 ALOGW("Path before package line in %s%s: %s\n",
1381 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1382 } else if (srcpkg[0] == 0) {
1383 // Skip -- source package no longer exists.
1384 } else {
1385 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1386 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1387 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1388 movefileordir(srcpath, dstpath,
1389 strlen(dstpath)-strlen(buf+bufp),
1390 dstuid, dstgid, &s);
1391 }
1392 }
1393 } else {
1394 char* div = strchr(buf+bufp, ':');
1395 if (div == NULL) {
1396 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1397 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1398 } else {
1399 *div = 0;
1400 div++;
1401 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1402 strcpy(dstpkg, buf+bufp);
1403 } else {
1404 srcpkg[0] = dstpkg[0] = 0;
1405 ALOGW("Package name too long in %s%s: %s\n",
1406 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1407 }
1408 if (strlen(div) < PKG_NAME_MAX) {
1409 strcpy(srcpkg, div);
1410 } else {
1411 srcpkg[0] = dstpkg[0] = 0;
1412 ALOGW("Package name too long in %s%s: %s\n",
1413 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1414 }
1415 if (srcpkg[0] != 0) {
1416 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1417 if (lstat(srcpath, &s) < 0) {
1418 // Package no longer exists -- skip.
1419 srcpkg[0] = 0;
1420 }
1421 } else {
1422 srcpkg[0] = 0;
1423 ALOGW("Can't create path %s in %s%s\n",
1424 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1425 }
1426 if (srcpkg[0] != 0) {
1427 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1428 if (lstat(dstpath, &s) == 0) {
1429 dstuid = s.st_uid;
1430 dstgid = s.st_gid;
1431 } else {
1432 // Destination package doesn't
1433 // exist... due to original-package,
1434 // this is normal, so don't be
1435 // noisy about it.
1436 srcpkg[0] = 0;
1437 }
1438 } else {
1439 srcpkg[0] = 0;
1440 ALOGW("Can't create path %s in %s%s\n",
1441 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1442 }
1443 }
1444 ALOGV("Transfering from %s to %s: uid=%d\n",
1445 srcpkg, dstpkg, dstuid);
1446 }
1447 }
1448 }
1449 bufp = bufi+1;
1450 } else {
1451 if (bufp == 0) {
1452 if (bufp < bufe) {
1453 ALOGW("Line too long in %s%s, skipping: %s\n",
1454 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1455 }
1456 } else if (bufp < bufe) {
1457 memcpy(buf, buf+bufp, bufe-bufp);
1458 bufe -= bufp;
1459 bufp = 0;
1460 }
1461 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1462 if (readlen < 0) {
1463 ALOGW("Failure reading update commands in %s%s: %s\n",
1464 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1465 break;
1466 } else if (readlen == 0) {
1467 break;
1468 }
1469 bufe += readlen;
1470 buf[bufe] = 0;
1471 ALOGV("Read buf: %s\n", buf);
1472 }
1473 }
1474 close(subfd);
1475 }
1476 }
1477 closedir(d);
1478done:
1479 return 0;
1480}
1481
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001482int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001483{
Mike Lockwood94afecf2012-10-24 10:45:23 -07001484 struct stat s, libStat;
1485 int rc = 0;
1486
Jeff Sharkeyd7921182015-04-30 15:58:19 -07001487 std::string _pkgdir(create_data_user_package_path(uuid, userId, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001488 std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
1489
1490 const char* pkgdir = _pkgdir.c_str();
1491 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001492
1493 if (stat(pkgdir, &s) < 0) return -1;
1494
1495 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1496 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1497 return -1;
1498 }
1499
1500 if (chmod(pkgdir, 0700) < 0) {
1501 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1502 rc = -1;
1503 goto out;
1504 }
1505
1506 if (lstat(libsymlink, &libStat) < 0) {
1507 if (errno != ENOENT) {
1508 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1509 rc = -1;
1510 goto out;
1511 }
1512 } else {
1513 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001514 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001515 rc = -1;
1516 goto out;
1517 }
1518 } else if (S_ISLNK(libStat.st_mode)) {
1519 if (unlink(libsymlink) < 0) {
1520 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1521 rc = -1;
1522 goto out;
1523 }
1524 }
1525 }
1526
1527 if (symlink(asecLibDir, libsymlink) < 0) {
1528 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1529 strerror(errno));
1530 rc = -errno;
1531 goto out;
1532 }
1533
1534out:
1535 if (chmod(pkgdir, s.st_mode) < 0) {
1536 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1537 rc = -errno;
1538 }
1539
1540 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1541 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1542 return -errno;
1543 }
1544
1545 return rc;
1546}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001547
1548static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1549{
1550 static const char *IDMAP_BIN = "/system/bin/idmap";
1551 static const size_t MAX_INT_LEN = 32;
1552 char idmap_str[MAX_INT_LEN];
1553
1554 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1555
1556 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1557 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1558}
1559
1560// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1561// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1562static int flatten_path(const char *prefix, const char *suffix,
1563 const char *overlay_path, char *idmap_path, size_t N)
1564{
1565 if (overlay_path == NULL || idmap_path == NULL) {
1566 return -1;
1567 }
1568 const size_t len_overlay_path = strlen(overlay_path);
1569 // will access overlay_path + 1 further below; requires absolute path
1570 if (len_overlay_path < 2 || *overlay_path != '/') {
1571 return -1;
1572 }
1573 const size_t len_idmap_root = strlen(prefix);
1574 const size_t len_suffix = strlen(suffix);
1575 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1576 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1577 // additions below would cause overflow
1578 return -1;
1579 }
1580 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1581 return -1;
1582 }
1583 memset(idmap_path, 0, N);
1584 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1585 char *ch = idmap_path + len_idmap_root;
1586 while (*ch != '\0') {
1587 if (*ch == '/') {
1588 *ch = '@';
1589 }
1590 ++ch;
1591 }
1592 return 0;
1593}
1594
1595int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1596{
1597 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1598
1599 int idmap_fd = -1;
1600 char idmap_path[PATH_MAX];
1601
1602 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1603 idmap_path, sizeof(idmap_path)) == -1) {
1604 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1605 goto fail;
1606 }
1607
1608 unlink(idmap_path);
1609 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1610 if (idmap_fd < 0) {
1611 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1612 goto fail;
1613 }
1614 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1615 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1616 goto fail;
1617 }
1618 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1619 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1620 goto fail;
1621 }
1622
1623 pid_t pid;
1624 pid = fork();
1625 if (pid == 0) {
1626 /* child -- drop privileges before continuing */
1627 if (setgid(uid) != 0) {
1628 ALOGE("setgid(%d) failed during idmap\n", uid);
1629 exit(1);
1630 }
1631 if (setuid(uid) != 0) {
1632 ALOGE("setuid(%d) failed during idmap\n", uid);
1633 exit(1);
1634 }
1635 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1636 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1637 exit(1);
1638 }
1639
1640 run_idmap(target_apk, overlay_apk, idmap_fd);
1641 exit(1); /* only if exec call to idmap failed */
1642 } else {
1643 int status = wait_child(pid);
1644 if (status != 0) {
1645 ALOGE("idmap failed, status=0x%04x\n", status);
1646 goto fail;
1647 }
1648 }
1649
1650 close(idmap_fd);
1651 return 0;
1652fail:
1653 if (idmap_fd >= 0) {
1654 close(idmap_fd);
1655 unlink(idmap_path);
1656 }
1657 return -1;
1658}
Robert Craige9887e42014-02-20 10:25:56 -05001659
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001660int restorecon_data(const char* uuid, const char* pkgName, const char* seinfo, appid_t appid) {
1661 int res = 0;
Robert Craige9887e42014-02-20 10:25:56 -05001662
Robert Craigda30dc72014-03-27 10:21:12 -04001663 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1664 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1665
1666 if (!pkgName || !seinfo) {
1667 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001668 return -1;
1669 }
1670
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001671 // Relabel package directory for all users
1672 std::vector<userid_t> users = get_known_users(uuid);
1673 for (auto user : users) {
1674 uid_t uid = multiuser_get_uid(user, appid);
Robert Craigda30dc72014-03-27 10:21:12 -04001675
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001676 std::string ce_package_path(create_data_user_package_path(uuid, user, pkgName));
1677 std::string de_package_path(create_data_user_de_package_path(uuid, user, pkgName));
1678
1679 if (selinux_android_restorecon_pkgdir(ce_package_path.c_str(), seinfo, uid, flags) < 0) {
1680 PLOG(ERROR) << "restorecon failed for " << ce_package_path;
1681 res = -1;
1682 }
1683 if (selinux_android_restorecon_pkgdir(de_package_path.c_str(), seinfo, uid, flags) < 0) {
1684 PLOG(ERROR) << "restorecon failed for " << de_package_path;
1685 res = -1;
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001686 }
Robert Craige9887e42014-02-20 10:25:56 -05001687 }
1688
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001689 return res;
Robert Craige9887e42014-02-20 10:25:56 -05001690}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001691
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001692int create_oat_dir(const char* oat_dir, const char* instruction_set)
1693{
1694 char oat_instr_dir[PKG_PATH_MAX];
1695
1696 if (validate_apk_path(oat_dir)) {
1697 ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
1698 return -1;
1699 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001700 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001701 return -1;
1702 }
1703 if (selinux_android_restorecon(oat_dir, 0)) {
1704 ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
1705 return -1;
1706 }
1707 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001708 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001709 return -1;
1710 }
1711 return 0;
1712}
1713
1714int rm_package_dir(const char* apk_path)
1715{
1716 if (validate_apk_path(apk_path)) {
1717 ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
1718 return -1;
1719 }
1720 return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */);
1721}
1722
Narayan Kamathd845c962015-06-04 13:20:27 +01001723int link_file(const char* relative_path, const char* from_base, const char* to_base) {
1724 char from_path[PKG_PATH_MAX];
1725 char to_path[PKG_PATH_MAX];
1726 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
1727 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
1728
1729 if (validate_apk_path_subdirs(from_path)) {
1730 ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path);
1731 return -1;
1732 }
1733
1734 if (validate_apk_path_subdirs(to_path)) {
1735 ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path);
1736 return -1;
1737 }
1738
1739 const int ret = link(from_path, to_path);
1740 if (ret < 0) {
1741 ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno));
1742 return -1;
1743 }
1744
1745 return 0;
1746}
1747
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001748int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
1749 const char *instruction_set) {
1750 char *file_name_start;
1751 char *file_name_end;
1752
1753 file_name_start = strrchr(apk_path, '/');
1754 if (file_name_start == NULL) {
1755 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
1756 return -1;
1757 }
1758 file_name_end = strrchr(apk_path, '.');
1759 if (file_name_end < file_name_start) {
1760 ALOGE("apk_path '%s' has no extension\n", apk_path);
1761 return -1;
1762 }
1763
1764 // Calculate file_name
1765 int file_name_len = file_name_end - file_name_start - 1;
1766 char file_name[file_name_len + 1];
1767 memcpy(file_name, file_name_start + 1, file_name_len);
1768 file_name[file_name_len] = '\0';
1769
1770 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
1771 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
1772 return 0;
1773}