blob: b48fbc1818aab430e2871b64d61efebb68fc0ba1 [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 Sharkey63ec2d62015-11-09 13:10:36 -080052 std::string ce_package_path(create_data_user_package_path(uuid, 0, pkgname));
53 std::string de_package_path(create_data_user_de_package_path(uuid, 0, pkgname));
Mike Lockwood94afecf2012-10-24 10:45:23 -070054
Jeff Sharkey63ec2d62015-11-09 13:10:36 -080055 const char* c_ce_package_path = ce_package_path.c_str();
56 const char* c_de_package_path = de_package_path.c_str();
57
58 if (fs_prepare_dir(c_ce_package_path, 0751, uid, gid) == -1) {
59 PLOG(ERROR) << "Failed to prepare " << ce_package_path;
60 unlink(c_ce_package_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -070061 return -1;
62 }
Jeff Sharkey63ec2d62015-11-09 13:10:36 -080063 if (selinux_android_setfilecon(c_ce_package_path, pkgname, seinfo, uid) < 0) {
64 PLOG(ERROR) << "Failed to setfilecon " << ce_package_path;
65 unlink(c_ce_package_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -070066 return -1;
67 }
68
Jeff Sharkey63ec2d62015-11-09 13:10:36 -080069 if (property_get_bool("vold.has_fbe", false)) {
70 if (fs_prepare_dir(c_de_package_path, 0751, uid, gid) == -1) {
71 PLOG(ERROR) << "Failed to prepare " << de_package_path;
72 unlink(c_de_package_path);
73 return -1;
74 }
75 if (selinux_android_setfilecon(c_de_package_path, pkgname, seinfo, uid) < 0) {
76 PLOG(ERROR) << "Failed to setfilecon " << de_package_path;
77 unlink(c_de_package_path);
78 return -1;
79 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070080 }
81
82 return 0;
83}
84
Jeff Sharkeyc03de092015-04-07 18:14:05 -070085int uninstall(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -070086{
Jeff Sharkeyd7921182015-04-30 15:58:19 -070087 std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -070088 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070089
Dave Allisond9370732014-01-30 14:19:23 -080090 remove_profile_file(pkgname);
91
Mike Lockwood94afecf2012-10-24 10:45:23 -070092 /* delete contents AND directory, no exceptions */
93 return delete_dir_contents(pkgdir, 1, NULL);
94}
95
Jeff Sharkeyc03de092015-04-07 18:14:05 -070096int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid)
Mike Lockwood94afecf2012-10-24 10:45:23 -070097{
Mike Lockwood94afecf2012-10-24 10:45:23 -070098 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -070099
100 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
101 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
102 return -1;
103 }
104
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700105 std::string _pkgdir(create_data_user_package_path(uuid, 0, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700106 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700107
108 if (stat(pkgdir, &s) < 0) return -1;
109
110 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700111 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 -0700112 return -1;
113 }
114
115 if (chmod(pkgdir, 0751) < 0) {
116 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
117 unlink(pkgdir);
118 return -errno;
119 }
120 if (chown(pkgdir, uid, gid) < 0) {
121 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
122 unlink(pkgdir);
123 return -errno;
124 }
125
126 return 0;
127}
128
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700129int delete_user_data(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700130{
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700131 std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700132 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700133
Jeff Sharkey3316fe42014-08-27 10:46:25 -0700134 return delete_dir_contents(pkgdir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700135}
136
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700137int 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 -0700138{
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700139 std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700140 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700141
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800142 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700143 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
144 return -errno;
145 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800146 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700147 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
148 unlink(pkgdir);
149 return -errno;
150 }
151
Stephen Smalley26288202014-02-07 09:16:46 -0500152 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700153 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700154 unlink(pkgdir);
155 return -errno;
156 }
157
158 if (chown(pkgdir, uid, uid) < 0) {
159 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700160 unlink(pkgdir);
161 return -errno;
162 }
163
164 return 0;
165}
166
Jeff Sharkey31f08982015-07-07 13:31:37 -0700167int copy_complete_app(const char *from_uuid, const char *to_uuid,
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700168 const char *package_name, const char *data_app_name, appid_t appid,
169 const char* seinfo) {
Jeff Sharkeye3637242015-04-08 20:56:42 -0700170 std::vector<userid_t> users = get_known_users(from_uuid);
171
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700172 // Copy app
173 {
174 std::string from(create_data_app_package_path(from_uuid, data_app_name));
175 std::string to(create_data_app_package_path(to_uuid, data_app_name));
176 std::string to_parent(create_data_app_path(to_uuid));
177
178 char *argv[] = {
179 (char*) kCpPath,
180 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
181 (char*) "-p", /* preserve timestamps, ownership, and permissions */
182 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
183 (char*) "-P", /* Do not follow symlinks [default] */
184 (char*) "-d", /* don't dereference symlinks */
185 (char*) from.c_str(),
186 (char*) to_parent.c_str()
187 };
188
189 LOG(DEBUG) << "Copying " << from << " to " << to;
190 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
191
192 if (rc != 0) {
193 LOG(ERROR) << "Failed copying " << from << " to " << to
194 << ": status " << rc;
195 goto fail;
196 }
197
198 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
199 LOG(ERROR) << "Failed to restorecon " << to;
200 goto fail;
201 }
202 }
203
204 // Copy private data for all known users
Jeff Sharkeye3637242015-04-08 20:56:42 -0700205 for (auto user : users) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700206 std::string from(create_data_user_package_path(from_uuid, user, package_name));
207 std::string to(create_data_user_package_path(to_uuid, user, package_name));
208 std::string to_parent(create_data_user_path(to_uuid, user));
Jeff Sharkeye3637242015-04-08 20:56:42 -0700209
210 // Data source may not exist for all users; that's okay
211 if (access(from.c_str(), F_OK) != 0) {
212 LOG(INFO) << "Missing source " << from;
213 continue;
214 }
215
216 std::string user_path(create_data_user_path(to_uuid, user));
217 if (fs_prepare_dir(user_path.c_str(), 0771, AID_SYSTEM, AID_SYSTEM) != 0) {
218 LOG(ERROR) << "Failed to prepare user target " << user_path;
219 goto fail;
220 }
221
222 uid_t uid = multiuser_get_uid(user, appid);
223 if (make_user_data(to_uuid, package_name, uid, user, seinfo) != 0) {
224 LOG(ERROR) << "Failed to create package target " << to;
225 goto fail;
226 }
227
228 char *argv[] = {
229 (char*) kCpPath,
230 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
231 (char*) "-p", /* preserve timestamps, ownership, and permissions */
232 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
233 (char*) "-P", /* Do not follow symlinks [default] */
234 (char*) "-d", /* don't dereference symlinks */
235 (char*) from.c_str(),
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700236 (char*) to_parent.c_str()
Jeff Sharkeye3637242015-04-08 20:56:42 -0700237 };
238
239 LOG(DEBUG) << "Copying " << from << " to " << to;
240 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
241
242 if (rc != 0) {
243 LOG(ERROR) << "Failed copying " << from << " to " << to
244 << ": status " << rc;
245 goto fail;
246 }
Jeff Sharkeya2307ae2015-07-20 16:26:19 -0700247 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700248
Jeff Sharkeya2307ae2015-07-20 16:26:19 -0700249 if (restorecon_data(to_uuid, package_name, seinfo, multiuser_get_uid(0, appid)) != 0) {
250 LOG(ERROR) << "Failed to restorecon";
251 goto fail;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700252 }
253
Jeff Sharkey31f08982015-07-07 13:31:37 -0700254 // We let the framework scan the new location and persist that before
255 // deleting the data in the old location; this ordering ensures that
256 // we can recover from things like battery pulls.
Jeff Sharkeye3637242015-04-08 20:56:42 -0700257 return 0;
258
259fail:
260 // Nuke everything we might have already copied
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700261 {
262 std::string to(create_data_app_package_path(to_uuid, data_app_name));
263 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
264 LOG(WARNING) << "Failed to rollback " << to;
265 }
266 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700267 for (auto user : users) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700268 std::string to(create_data_user_package_path(to_uuid, user, package_name));
Jeff Sharkeye3637242015-04-08 20:56:42 -0700269 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
270 LOG(WARNING) << "Failed to rollback " << to;
271 }
272 }
273 return -1;
274}
275
Robin Lee7c8bec02014-06-10 18:46:26 +0100276int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700277{
Robin Lee095c7632014-04-25 15:05:19 +0100278 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700279 return -1;
280 }
281
282 return 0;
283}
284
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700285int delete_user(const char *uuid, userid_t userid)
Robin Lee095c7632014-04-25 15:05:19 +0100286{
287 int status = 0;
288
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700289 std::string data_path(create_data_user_path(uuid, userid));
290 if (delete_dir_contents(data_path.c_str(), 1, NULL) != 0) {
Robin Lee095c7632014-04-25 15:05:19 +0100291 status = -1;
292 }
293
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700294 std::string media_path(create_data_media_path(uuid, userid));
295 if (delete_dir_contents(media_path.c_str(), 1, NULL) != 0) {
Robin Lee095c7632014-04-25 15:05:19 +0100296 status = -1;
297 }
298
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700299 // Config paths only exist on internal storage
300 if (uuid == nullptr) {
301 char config_path[PATH_MAX];
302 if ((create_user_config_path(config_path, userid) != 0)
303 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
304 status = -1;
305 }
Robin Lee095c7632014-04-25 15:05:19 +0100306 }
307
308 return status;
309}
310
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700311int delete_cache(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700312{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700313 std::string _cachedir(
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700314 create_data_user_package_path(uuid, userid, pkgname) + CACHE_DIR_POSTFIX);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700315 const char* cachedir = _cachedir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700316
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700317 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100318 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700319}
320
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700321int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid)
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700322{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700323 std::string _codecachedir(
Daichi Hironoa2ccb9e2015-06-24 15:57:06 +0900324 create_data_user_package_path(uuid, userid, pkgname) + CODE_CACHE_DIR_POSTFIX);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700325 const char* codecachedir = _codecachedir.c_str();
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700326
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700327 struct stat s;
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700328
Jeff Sharkey770180a2014-09-08 17:14:26 -0700329 /* it's okay if code cache is missing */
330 if (lstat(codecachedir, &s) == -1 && errno == ENOENT) {
331 return 0;
332 }
333
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700334 /* delete contents, not the directory, no exceptions */
335 return delete_dir_contents(codecachedir, 0, NULL);
336}
337
Mike Lockwood94afecf2012-10-24 10:45:23 -0700338/* Try to ensure free_size bytes of storage are available.
339 * Returns 0 on success.
340 * This is rather simple-minded because doing a full LRU would
341 * be potentially memory-intensive, and without atime it would
342 * also require that apps constantly modify file metadata even
343 * when just reading from the cache, which is pretty awful.
344 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700345int free_cache(const char *uuid, int64_t free_size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700346{
347 cache_t* cache;
348 int64_t avail;
349 DIR *d;
350 struct dirent *de;
351 char tmpdir[PATH_MAX];
352 char *dirpos;
353
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700354 std::string data_path(create_data_path(uuid));
355
356 avail = data_disk_free(data_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700357 if (avail < 0) return -1;
358
359 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
360 if (avail >= free_size) return 0;
361
362 cache = start_cache_collection();
363
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700364 // Special case for owner on internal storage
365 if (uuid == nullptr) {
366 std::string _tmpdir(create_data_user_path(nullptr, 0));
367 add_cache_files(cache, _tmpdir.c_str(), "cache");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700368 }
369
370 // Search for other users and add any cache files from them.
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700371 std::string _tmpdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX);
372 strcpy(tmpdir, _tmpdir.c_str());
373
Mike Lockwood94afecf2012-10-24 10:45:23 -0700374 dirpos = tmpdir + strlen(tmpdir);
375 d = opendir(tmpdir);
376 if (d != NULL) {
377 while ((de = readdir(d))) {
378 if (de->d_type == DT_DIR) {
379 const char *name = de->d_name;
380 /* always skip "." and ".." */
381 if (name[0] == '.') {
382 if (name[1] == 0) continue;
383 if ((name[1] == '.') && (name[2] == 0)) continue;
384 }
385 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
386 strcpy(dirpos, name);
387 //ALOGI("adding cache files from %s\n", tmpdir);
388 add_cache_files(cache, tmpdir, "cache");
389 } else {
390 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
391 }
392 }
393 }
394 closedir(d);
395 }
396
397 // Collect cache files on external storage for all users (if it is mounted as part
398 // of the internal storage).
399 strcpy(tmpdir, android_media_dir.path);
400 dirpos = tmpdir + strlen(tmpdir);
401 d = opendir(tmpdir);
402 if (d != NULL) {
403 while ((de = readdir(d))) {
404 if (de->d_type == DT_DIR) {
405 const char *name = de->d_name;
406 /* skip any dir that doesn't start with a number, so not a user */
407 if (name[0] < '0' || name[0] > '9') {
408 continue;
409 }
410 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
411 strcpy(dirpos, name);
412 if (lookup_media_dir(tmpdir, "Android") == 0
413 && lookup_media_dir(tmpdir, "data") == 0) {
414 //ALOGI("adding cache files from %s\n", tmpdir);
415 add_cache_files(cache, tmpdir, "cache");
416 }
417 } else {
418 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
419 }
420 }
421 }
422 closedir(d);
423 }
424
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700425 clear_cache_files(data_path, cache, free_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700426 finish_cache_collection(cache);
427
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700428 return data_disk_free(data_path) >= free_size ? 0 : -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700429}
430
Narayan Kamath1b400322014-04-11 13:17:00 +0100431int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700432{
433 char src_dex[PKG_PATH_MAX];
434 char dst_dex[PKG_PATH_MAX];
435
Jeff Sharkey770180a2014-09-08 17:14:26 -0700436 if (validate_apk_path(src)) {
437 ALOGE("invalid apk path '%s' (bad prefix)\n", src);
438 return -1;
439 }
440 if (validate_apk_path(dst)) {
441 ALOGE("invalid apk path '%s' (bad prefix)\n", dst);
442 return -1;
443 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700444
Narayan Kamath1b400322014-04-11 13:17:00 +0100445 if (create_cache_path(src_dex, src, instruction_set)) return -1;
446 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700447
448 ALOGV("move %s -> %s\n", src_dex, dst_dex);
449 if (rename(src_dex, dst_dex) < 0) {
450 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
451 return -1;
452 } else {
453 return 0;
454 }
455}
456
Narayan Kamath1b400322014-04-11 13:17:00 +0100457int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700458{
459 char dex_path[PKG_PATH_MAX];
460
Jeff Sharkey770180a2014-09-08 17:14:26 -0700461 if (validate_apk_path(path) && validate_system_app_path(path)) {
462 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
463 return -1;
464 }
465
Narayan Kamath1b400322014-04-11 13:17:00 +0100466 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700467
468 ALOGV("unlink %s\n", dex_path);
469 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700470 if (errno != ENOENT) {
471 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
472 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700473 return -1;
474 } else {
475 return 0;
476 }
477}
478
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700479int get_size(const char *uuid, const char *pkgname, int userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700480 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100481 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
482 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700483{
484 DIR *d;
485 int dfd;
486 struct dirent *de;
487 struct stat s;
488 char path[PKG_PATH_MAX];
489
490 int64_t codesize = 0;
491 int64_t datasize = 0;
492 int64_t cachesize = 0;
493 int64_t asecsize = 0;
494
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700495 /* count the source apk as code -- but only if it's not
496 * on the /system partition and its not on the sdcard. */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700497 if (validate_system_app_path(apkpath) &&
498 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
499 if (stat(apkpath, &s) == 0) {
500 codesize += stat_size(&s);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700501 if (S_ISDIR(s.st_mode)) {
502 d = opendir(apkpath);
503 if (d != NULL) {
504 dfd = dirfd(d);
505 codesize += calculate_dir_size(dfd);
506 closedir(d);
507 }
508 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700509 }
510 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700511
512 /* count the forward locked apk as code if it is given */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700513 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
514 if (stat(fwdlock_apkpath, &s) == 0) {
515 codesize += stat_size(&s);
516 }
517 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700518
519 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100520 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700521 if (stat(path, &s) == 0) {
522 codesize += stat_size(&s);
523 }
524 }
525
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700526 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700527 if (libdirpath != NULL && libdirpath[0] != '!') {
528 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700529 if (d != NULL) {
530 dfd = dirfd(d);
531 codesize += calculate_dir_size(dfd);
532 closedir(d);
533 }
534 }
535
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700536 /* compute asec size if it is given */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700537 if (asecpath != NULL && asecpath[0] != '!') {
538 if (stat(asecpath, &s) == 0) {
539 asecsize += stat_size(&s);
540 }
541 }
542
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700543 std::vector<userid_t> users;
544 if (userid == -1) {
545 users = get_known_users(uuid);
546 } else {
547 users.push_back(userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700548 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700549
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700550 for (auto user : users) {
551 std::string _pkgdir(create_data_user_package_path(uuid, user, pkgname));
552 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700553
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700554 d = opendir(pkgdir);
555 if (d == NULL) {
556 PLOG(WARNING) << "Failed to open " << pkgdir;
557 continue;
558 }
559 dfd = dirfd(d);
560
561 /* most stuff in the pkgdir is data, except for the "cache"
562 * directory and below, which is cache, and the "lib" directory
563 * and below, which is code...
564 */
565 while ((de = readdir(d))) {
566 const char *name = de->d_name;
567
568 if (de->d_type == DT_DIR) {
569 int subfd;
570 int64_t statsize = 0;
571 int64_t dirsize = 0;
572 /* always skip "." and ".." */
573 if (name[0] == '.') {
574 if (name[1] == 0) continue;
575 if ((name[1] == '.') && (name[2] == 0)) continue;
576 }
577 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
578 statsize = stat_size(&s);
579 }
580 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
581 if (subfd >= 0) {
582 dirsize = calculate_dir_size(subfd);
583 }
584 if(!strcmp(name,"lib")) {
585 codesize += dirsize + statsize;
586 } else if(!strcmp(name,"cache")) {
587 cachesize += dirsize + statsize;
588 } else {
589 datasize += dirsize + statsize;
590 }
591 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
592 // This is the symbolic link to the application's library
593 // code. We'll count this as code instead of data, since
594 // it is not something that the app creates.
595 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
596 codesize += stat_size(&s);
597 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700598 } else {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700599 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
600 datasize += stat_size(&s);
601 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700602 }
603 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700604 closedir(d);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700605 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700606 *_codesize = codesize;
607 *_datasize = datasize;
608 *_cachesize = cachesize;
609 *_asecsize = asecsize;
610 return 0;
611}
612
Narayan Kamath1b400322014-04-11 13:17:00 +0100613int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700614{
615 char *tmp;
616 int srclen;
617 int dstlen;
618
619 srclen = strlen(src);
620
621 /* demand that we are an absolute path */
622 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
623 return -1;
624 }
625
626 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
627 return -1;
628 }
629
Dave Allisond9370732014-01-30 14:19:23 -0800630 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100631 strlen(instruction_set) +
632 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800633
Mike Lockwood94afecf2012-10-24 10:45:23 -0700634 if (dstlen > PKG_PATH_MAX) {
635 return -1;
636 }
637
Narayan Kamath1b400322014-04-11 13:17:00 +0100638 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700639 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100640 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700641 src + 1, /* skip the leading / */
642 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800643
Narayan Kamath1b400322014-04-11 13:17:00 +0100644 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700645 if (*tmp == '/') {
646 *tmp = '@';
647 }
648 }
649
650 return 0;
651}
652
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700653static int split_count(const char *str)
654{
655 char *ctx;
656 int count = 0;
657 char buf[PROPERTY_VALUE_MAX];
658
659 strncpy(buf, str, sizeof(buf));
660 char *pBuf = buf;
661
662 while(strtok_r(pBuf, " ", &ctx) != NULL) {
663 count++;
664 pBuf = NULL;
665 }
666
667 return count;
668}
669
neo.chae14e084d2015-01-07 18:46:13 +0900670static int split(char *buf, const char **argv)
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700671{
672 char *ctx;
673 int count = 0;
674 char *tok;
675 char *pBuf = buf;
676
677 while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
678 argv[count++] = tok;
679 pBuf = NULL;
680 }
681
682 return count;
683}
684
Alex Light7365a102014-07-21 12:23:48 -0700685static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700686 const char* output_file_name, const char *pkgname __unused, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700687{
688 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100689 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700690
691 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
692 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
693 ALOGE("Instruction set %s longer than max length of %d",
694 instruction_set, MAX_INSTRUCTION_SET_LEN);
695 return;
696 }
697
698 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
699 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
700 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
701 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
702 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
703 // The caller has already gotten all the locks we need.
704 const char* no_lock_arg = "--no-lock-output";
705 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
706 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
707 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700708 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700709 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
710
711 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
712 char* argv[7];
713 argv[0] = (char*) PATCHOAT_BIN;
714 argv[1] = (char*) patched_image_location_arg;
715 argv[2] = (char*) no_lock_arg;
716 argv[3] = instruction_set_arg;
717 argv[4] = output_oat_fd_arg;
718 argv[5] = input_oat_fd_arg;
719 argv[6] = NULL;
720
721 execv(PATCHOAT_BIN, (char* const *)argv);
722 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
723}
724
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700725static bool check_boolean_property(const char* property_name, bool default_value = false) {
726 char tmp_property_value[PROPERTY_VALUE_MAX];
727 bool have_property = property_get(property_name, tmp_property_value, nullptr) > 0;
728 if (!have_property) {
729 return default_value;
730 }
731 return strcmp(tmp_property_value, "true") == 0;
732}
733
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700734static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Andreas Gampee1c01352014-12-10 16:41:11 -0800735 const char* output_file_name, int swap_fd, const char *pkgname, const char *instruction_set,
Todd Kennedy12434f82015-09-25 14:45:37 -0700736 bool vm_safe_mode, bool debuggable, bool post_bootcomplete, bool use_jit)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700737{
Calin Juravle8fc73152014-08-19 18:48:50 +0100738 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
739
740 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
741 ALOGE("Instruction set %s longer than max length of %d",
742 instruction_set, MAX_INSTRUCTION_SET_LEN);
743 return;
744 }
745
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700746 char prop_buf[PROPERTY_VALUE_MAX];
747 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
748
749 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
750 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
751
752 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
753 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
754
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700755 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
756 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
757 dex2oat_compiler_filter_flag, NULL) > 0;
758
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700759 char dex2oat_threads_buf[PROPERTY_VALUE_MAX];
Andreas Gampe919461c2015-09-28 08:55:01 -0700760 bool have_dex2oat_threads_flag = property_get(post_bootcomplete
761 ? "dalvik.vm.dex2oat-threads"
762 : "dalvik.vm.boot-dex2oat-threads",
763 dex2oat_threads_buf,
764 NULL) > 0;
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700765 char dex2oat_threads_arg[PROPERTY_VALUE_MAX + 2];
766 if (have_dex2oat_threads_flag) {
767 sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf);
768 }
769
Calin Juravle8fc73152014-08-19 18:48:50 +0100770 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
771 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
772 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
773 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
774 dex2oat_isa_features, NULL) > 0;
775
Ian Rogers16a95b22014-11-08 16:58:13 -0800776 char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
777 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
778 char dex2oat_isa_variant[PROPERTY_VALUE_MAX];
779 bool have_dex2oat_isa_variant = property_get(dex2oat_isa_variant_key,
780 dex2oat_isa_variant, NULL) > 0;
781
neo.chae14e084d2015-01-07 18:46:13 +0900782 const char *dex2oat_norelocation = "-Xnorelocate";
783 bool have_dex2oat_relocation_skip_flag = false;
784
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800785 char dex2oat_flags[PROPERTY_VALUE_MAX];
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700786 int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags",
787 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800788 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
789
Brian Carlstrom538998f2014-07-30 14:37:11 -0700790 // If we booting without the real /data, don't spend time compiling.
791 char vold_decrypt[PROPERTY_VALUE_MAX];
792 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
793 bool skip_compilation = (have_vold_decrypt &&
794 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
795 (strcmp(vold_decrypt, "1") == 0)));
796
David Srbecky528c8dd2015-05-28 16:55:50 +0100797 bool generate_debug_info = check_boolean_property("debug.generate-debug-info");
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700798
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700799 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700800
Brian Carlstrom53e07762014-06-27 14:15:19 -0700801 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700802
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700803 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100804
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700805 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
806 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
807 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700808 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100809 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Ian Rogers16a95b22014-11-08 16:58:13 -0800810 char instruction_set_variant_arg[strlen("--instruction-set-variant=") + PROPERTY_VALUE_MAX];
Calin Juravle8fc73152014-08-19 18:48:50 +0100811 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100812 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
813 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700814 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
815 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700816 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800817 bool have_dex2oat_swap_fd = false;
818 char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700819
820 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
821 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
822 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
823 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100824 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Ian Rogers16a95b22014-11-08 16:58:13 -0800825 sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
Calin Juravle8fc73152014-08-19 18:48:50 +0100826 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Andreas Gampee1c01352014-12-10 16:41:11 -0800827 if (swap_fd >= 0) {
828 have_dex2oat_swap_fd = true;
829 sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
830 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100831
Calin Juravle4fdff462014-06-06 16:58:43 +0100832 bool have_profile_file = false;
833 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100834 if (profiler && (strcmp(pkgname, "*") != 0)) {
835 char profile_file[PKG_PATH_MAX];
836 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800837 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100838 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100839 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100840 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100841 have_profile_file = true;
842 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
843 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
844 "--top-k-profile-threshold=%s", prop_buf);
845 have_top_k_profile_threshold = true;
846 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100847 }
Dave Allisond9370732014-01-30 14:19:23 -0800848 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700849
Todd Kennedy12434f82015-09-25 14:45:37 -0700850 // use the JIT if either it's specified as a dexopt flag or if the property is set
851 use_jit = use_jit || check_boolean_property("debug.usejit");
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700852 if (have_dex2oat_Xms_flag) {
853 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
854 }
855 if (have_dex2oat_Xmx_flag) {
856 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
857 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700858 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700859 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700860 have_dex2oat_compiler_filter_flag = true;
neo.chae14e084d2015-01-07 18:46:13 +0900861 have_dex2oat_relocation_skip_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100862 } else if (vm_safe_mode) {
863 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100864 have_dex2oat_compiler_filter_flag = true;
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700865 } else if (use_jit) {
866 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-at-runtime");
867 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700868 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700869 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
870 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700871
Andreas Gampe598c25e2015-03-03 09:15:06 -0800872 // Check whether all apps should be compiled debuggable.
873 if (!debuggable) {
874 debuggable =
875 (property_get("dalvik.vm.always_debuggable", prop_buf, "0") > 0) &&
876 (prop_buf[0] == '1');
877 }
878
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700879 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100880
neo.chae14e084d2015-01-07 18:46:13 +0900881 const char* argv[7 // program name, mandatory arguments and the final NULL
882 + (have_dex2oat_isa_variant ? 1 : 0)
883 + (have_dex2oat_isa_features ? 1 : 0)
884 + (have_profile_file ? 1 : 0)
885 + (have_top_k_profile_threshold ? 1 : 0)
886 + (have_dex2oat_Xms_flag ? 2 : 0)
887 + (have_dex2oat_Xmx_flag ? 2 : 0)
888 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700889 + (have_dex2oat_threads_flag ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900890 + (have_dex2oat_swap_fd ? 1 : 0)
891 + (have_dex2oat_relocation_skip_flag ? 2 : 0)
David Srbecky528c8dd2015-05-28 16:55:50 +0100892 + (generate_debug_info ? 1 : 0)
Andreas Gampe598c25e2015-03-03 09:15:06 -0800893 + (debuggable ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900894 + dex2oat_flags_count];
Calin Juravle4fdff462014-06-06 16:58:43 +0100895 int i = 0;
neo.chae14e084d2015-01-07 18:46:13 +0900896 argv[i++] = DEX2OAT_BIN;
Calin Juravle4fdff462014-06-06 16:58:43 +0100897 argv[i++] = zip_fd_arg;
898 argv[i++] = zip_location_arg;
899 argv[i++] = oat_fd_arg;
900 argv[i++] = oat_location_arg;
901 argv[i++] = instruction_set_arg;
Ian Rogers16a95b22014-11-08 16:58:13 -0800902 if (have_dex2oat_isa_variant) {
903 argv[i++] = instruction_set_variant_arg;
904 }
Calin Juravle8fc73152014-08-19 18:48:50 +0100905 if (have_dex2oat_isa_features) {
906 argv[i++] = instruction_set_features_arg;
907 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100908 if (have_profile_file) {
909 argv[i++] = profile_file_arg;
910 }
911 if (have_top_k_profile_threshold) {
912 argv[i++] = top_k_profile_threshold_arg;
913 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700914 if (have_dex2oat_Xms_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900915 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700916 argv[i++] = dex2oat_Xms_arg;
917 }
918 if (have_dex2oat_Xmx_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900919 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700920 argv[i++] = dex2oat_Xmx_arg;
921 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700922 if (have_dex2oat_compiler_filter_flag) {
923 argv[i++] = dex2oat_compiler_filter_arg;
924 }
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700925 if (have_dex2oat_threads_flag) {
926 argv[i++] = dex2oat_threads_arg;
927 }
Andreas Gampee1c01352014-12-10 16:41:11 -0800928 if (have_dex2oat_swap_fd) {
929 argv[i++] = dex2oat_swap_fd;
930 }
David Srbecky528c8dd2015-05-28 16:55:50 +0100931 if (generate_debug_info) {
932 argv[i++] = "--generate-debug-info";
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700933 }
Andreas Gampe598c25e2015-03-03 09:15:06 -0800934 if (debuggable) {
935 argv[i++] = "--debuggable";
936 }
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700937 if (dex2oat_flags_count) {
938 i += split(dex2oat_flags, argv + i);
Calin Juravle4fdff462014-06-06 16:58:43 +0100939 }
neo.chae14e084d2015-01-07 18:46:13 +0900940 if (have_dex2oat_relocation_skip_flag) {
941 argv[i++] = RUNTIME_ARG;
942 argv[i++] = dex2oat_norelocation;
943 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700944 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100945 argv[i] = NULL;
946
neo.chae14e084d2015-01-07 18:46:13 +0900947 execv(DEX2OAT_BIN, (char * const *)argv);
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700948 ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700949}
950
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100951static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700952{
953 int status;
954 pid_t got_pid;
955
Mike Lockwood94afecf2012-10-24 10:45:23 -0700956 while (1) {
957 got_pid = waitpid(pid, &status, 0);
958 if (got_pid == -1 && errno == EINTR) {
959 printf("waitpid interrupted, retrying\n");
960 } else {
961 break;
962 }
963 }
964 if (got_pid != pid) {
965 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
966 (int) pid, (int) got_pid, strerror(errno));
967 return 1;
968 }
969
970 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700971 return 0;
972 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700973 return status; /* always nonzero */
974 }
975}
976
Andreas Gampee1c01352014-12-10 16:41:11 -0800977/*
Andreas Gampec968c012015-07-16 15:55:41 -0700978 * Whether dexopt should use a swap file when compiling an APK.
979 *
980 * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision
981 * itself, anyways).
982 *
983 * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true".
984 *
985 * Otherwise, return true if this is a low-mem device.
986 *
987 * Otherwise, return default value.
Andreas Gampee1c01352014-12-10 16:41:11 -0800988 */
Andreas Gampec968c012015-07-16 15:55:41 -0700989static bool kAlwaysProvideSwapFile = false;
990static bool kDefaultProvideSwapFile = true;
Andreas Gampee1c01352014-12-10 16:41:11 -0800991
992static bool ShouldUseSwapFileForDexopt() {
993 if (kAlwaysProvideSwapFile) {
994 return true;
995 }
996
Andreas Gampec968c012015-07-16 15:55:41 -0700997 // Check the "override" property. If it exists, return value == "true".
998 char dex2oat_prop_buf[PROPERTY_VALUE_MAX];
999 if (property_get("dalvik.vm.dex2oat-swap", dex2oat_prop_buf, "") > 0) {
1000 if (strcmp(dex2oat_prop_buf, "true") == 0) {
1001 return true;
1002 } else {
1003 return false;
1004 }
1005 }
1006
1007 // Shortcut for default value. This is an implementation optimization for the process sketched
1008 // above. If the default value is true, we can avoid to check whether this is a low-mem device,
1009 // as low-mem is never returning false. The compiler will optimize this away if it can.
1010 if (kDefaultProvideSwapFile) {
1011 return true;
1012 }
1013
1014 bool is_low_mem = check_boolean_property("ro.config.low_ram");
1015 if (is_low_mem) {
1016 return true;
1017 }
1018
1019 // Default value must be false here.
1020 return kDefaultProvideSwapFile;
Andreas Gampee1c01352014-12-10 16:41:11 -08001021}
1022
Richard Uhler009b8772015-03-18 12:39:09 -07001023/*
1024 * Computes the odex file for the given apk_path and instruction_set.
1025 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
1026 *
1027 * Returns false if it failed to determine the odex file path.
1028 */
1029static bool calculate_odex_file_path(char path[PKG_PATH_MAX],
1030 const char *apk_path,
1031 const char *instruction_set)
1032{
1033 if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
1034 + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
1035 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
1036 return false;
1037 }
1038
1039 strcpy(path, apk_path);
1040 char *end = strrchr(path, '/');
1041 if (end == NULL) {
1042 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
1043 return false;
1044 }
1045 const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
1046
1047 strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0
1048 strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
1049 strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0
1050 end = strrchr(path, '.');
1051 if (end == NULL) {
1052 ALOGE("apk_path '%s' has no extension.\n", apk_path);
1053 return false;
1054 }
1055 strcpy(end + 1, "odex");
1056 return true;
1057}
1058
Andreas Gampe94dd3d32015-09-14 16:33:11 -07001059static void SetDex2OatAndPatchOatScheduling(bool set_to_bg) {
1060 if (set_to_bg) {
1061 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
1062 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
1063 exit(70);
1064 }
1065 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
1066 ALOGE("setpriority failed: %s\n", strerror(errno));
1067 exit(71);
1068 }
1069 }
1070}
1071
Todd Kennedy76e767c2015-09-25 07:47:47 -07001072int dexopt(const char *apk_path, uid_t uid, const char *pkgname, const char *instruction_set,
1073 int dexopt_needed, const char* oat_dir, int dexopt_flags)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001074{
1075 struct utimbuf ut;
Fyodor Kupolov26ff93c2015-04-02 16:59:10 -07001076 struct stat input_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001077 char out_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -08001078 char swap_file_name[PKG_PATH_MAX];
Alex Light7365a102014-07-21 12:23:48 -07001079 const char *input_file;
1080 char in_odex_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -08001081 int res, input_fd=-1, out_fd=-1, swap_fd=-1;
Todd Kennedy76e767c2015-09-25 07:47:47 -07001082 bool is_public = (dexopt_flags & DEXOPT_PUBLIC) != 0;
1083 bool vm_safe_mode = (dexopt_flags & DEXOPT_SAFEMODE) != 0;
1084 bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0;
1085 bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0;
Todd Kennedy12434f82015-09-25 14:45:37 -07001086 bool use_jit = (dexopt_flags & DEXOPT_USEJIT) != 0;
Todd Kennedy76e767c2015-09-25 07:47:47 -07001087
1088 if ((dexopt_flags & DEXOPT_MASK) != 0) {
1089 LOG_FATAL("dexopt flags contains unknown fields\n");
1090 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001091
Andreas Gampee1c01352014-12-10 16:41:11 -08001092 // Early best-effort check whether we can fit the the path into our buffers.
1093 // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
1094 // without a swap file, if necessary.
Mike Lockwood94afecf2012-10-24 10:45:23 -07001095 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001096 ALOGE("apk_path too long '%s'\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001097 return -1;
1098 }
1099
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001100 if (oat_dir != NULL && oat_dir[0] != '!') {
1101 if (validate_apk_path(oat_dir)) {
1102 ALOGE("invalid oat_dir '%s'\n", oat_dir);
1103 return -1;
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +08001104 }
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001105 if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) {
1106 return -1;
1107 }
1108 } else {
1109 if (create_cache_path(out_path, apk_path, instruction_set)) {
1110 return -1;
1111 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001112 }
1113
Richard Uhlerc92fb622015-03-26 15:47:38 -07001114 switch (dexopt_needed) {
1115 case DEXOPT_DEX2OAT_NEEDED:
1116 input_file = apk_path;
1117 break;
1118
1119 case DEXOPT_PATCHOAT_NEEDED:
1120 if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
1121 return -1;
1122 }
1123 input_file = in_odex_path;
1124 break;
1125
1126 case DEXOPT_SELF_PATCHOAT_NEEDED:
1127 input_file = out_path;
1128 break;
1129
1130 default:
1131 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1132 exit(72);
Alex Light7365a102014-07-21 12:23:48 -07001133 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001134
Alex Light7365a102014-07-21 12:23:48 -07001135 memset(&input_stat, 0, sizeof(input_stat));
1136 stat(input_file, &input_stat);
1137
1138 input_fd = open(input_file, O_RDONLY, 0);
1139 if (input_fd < 0) {
1140 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001141 return -1;
1142 }
1143
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001144 unlink(out_path);
1145 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1146 if (out_fd < 0) {
1147 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001148 goto fail;
1149 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001150 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -07001151 S_IRUSR|S_IWUSR|S_IRGRP |
1152 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001153 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001154 goto fail;
1155 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001156 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
1157 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001158 goto fail;
1159 }
1160
Dave Allisond9370732014-01-30 14:19:23 -08001161 // Create profile file if there is a package name present.
1162 if (strcmp(pkgname, "*") != 0) {
1163 create_profile_file(pkgname, uid);
1164 }
1165
Andreas Gampee1c01352014-12-10 16:41:11 -08001166 // Create a swap file if necessary.
Richard Uhlerc92fb622015-03-26 15:47:38 -07001167 if (ShouldUseSwapFileForDexopt()) {
Andreas Gampee1c01352014-12-10 16:41:11 -08001168 // Make sure there really is enough space.
1169 size_t out_len = strlen(out_path);
1170 if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
1171 strcpy(swap_file_name, out_path);
1172 strcpy(swap_file_name + strlen(out_path), ".swap");
1173 unlink(swap_file_name);
1174 swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600);
1175 if (swap_fd < 0) {
1176 // Could not create swap file. Optimistically go on and hope that we can compile
1177 // without it.
1178 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
1179 } else {
1180 // Immediately unlink. We don't really want to hit flash.
1181 unlink(swap_file_name);
1182 }
1183 } else {
1184 // Swap file path is too long. Try to run without.
1185 ALOGE("installd could not create swap file for path %s during dexopt\n", out_path);
1186 }
1187 }
Dave Allisond9370732014-01-30 14:19:23 -08001188
Alex Light7365a102014-07-21 12:23:48 -07001189 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001190
1191 pid_t pid;
1192 pid = fork();
1193 if (pid == 0) {
1194 /* child -- drop privileges before continuing */
1195 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001196 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001197 exit(64);
1198 }
1199 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001200 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001201 exit(65);
1202 }
1203 // drop capabilities
1204 struct __user_cap_header_struct capheader;
1205 struct __user_cap_data_struct capdata[2];
1206 memset(&capheader, 0, sizeof(capheader));
1207 memset(&capdata, 0, sizeof(capdata));
1208 capheader.version = _LINUX_CAPABILITY_VERSION_3;
1209 if (capset(&capheader, &capdata[0]) < 0) {
1210 ALOGE("capset failed: %s\n", strerror(errno));
1211 exit(66);
1212 }
Andreas Gampe13f14192015-09-21 13:21:30 -07001213 SetDex2OatAndPatchOatScheduling(boot_complete);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001214 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
1215 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001216 exit(67);
1217 }
1218
Richard Uhlerc92fb622015-03-26 15:47:38 -07001219 if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED
1220 || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) {
Andreas Gampebd872e42014-12-15 11:41:11 -08001221 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001222 } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001223 const char *input_file_name = strrchr(input_file, '/');
1224 if (input_file_name == NULL) {
1225 input_file_name = input_file;
1226 } else {
1227 input_file_name++;
1228 }
1229 run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname,
Todd Kennedy12434f82015-09-25 14:45:37 -07001230 instruction_set, vm_safe_mode, debuggable, boot_complete, use_jit);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001231 } else {
1232 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1233 exit(73);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001234 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001235 exit(68); /* only get here on exec failure */
1236 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001237 res = wait_child(pid);
1238 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001239 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001240 } else {
Alex Light7365a102014-07-21 12:23:48 -07001241 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001242 goto fail;
1243 }
1244 }
1245
Alex Light7365a102014-07-21 12:23:48 -07001246 ut.actime = input_stat.st_atime;
1247 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001248 utime(out_path, &ut);
1249
1250 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001251 close(input_fd);
Andreas Gampee1c01352014-12-10 16:41:11 -08001252 if (swap_fd != -1) {
1253 close(swap_fd);
1254 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001255 return 0;
1256
1257fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001258 if (out_fd >= 0) {
1259 close(out_fd);
1260 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001261 }
Alex Light7365a102014-07-21 12:23:48 -07001262 if (input_fd >= 0) {
1263 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001264 }
1265 return -1;
1266}
1267
Narayan Kamath091ea772014-11-10 15:03:46 +00001268int mark_boot_complete(const char* instruction_set)
1269{
1270 char boot_marker_path[PKG_PATH_MAX];
1271 sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set);
1272
1273 ALOGV("mark_boot_complete : %s", boot_marker_path);
1274 if (unlink(boot_marker_path) != 0) {
1275 ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1276 strerror(errno));
1277 return -1;
1278 }
1279
1280 return 0;
1281}
1282
Mike Lockwood94afecf2012-10-24 10:45:23 -07001283void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1284 struct stat* statbuf)
1285{
1286 while (path[basepos] != 0) {
1287 if (path[basepos] == '/') {
1288 path[basepos] = 0;
1289 if (lstat(path, statbuf) < 0) {
1290 ALOGV("Making directory: %s\n", path);
1291 if (mkdir(path, mode) == 0) {
1292 chown(path, uid, gid);
1293 } else {
1294 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1295 }
1296 }
1297 path[basepos] = '/';
1298 basepos++;
1299 }
1300 basepos++;
1301 }
1302}
1303
1304int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1305 int dstuid, int dstgid, struct stat* statbuf)
1306{
1307 DIR *d;
1308 struct dirent *de;
1309 int res;
1310
1311 int srcend = strlen(srcpath);
1312 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001313
Mike Lockwood94afecf2012-10-24 10:45:23 -07001314 if (lstat(srcpath, statbuf) < 0) {
1315 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1316 return 1;
1317 }
Dave Allisond9370732014-01-30 14:19:23 -08001318
Mike Lockwood94afecf2012-10-24 10:45:23 -07001319 if ((statbuf->st_mode&S_IFDIR) == 0) {
1320 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1321 dstuid, dstgid, statbuf);
1322 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1323 if (rename(srcpath, dstpath) >= 0) {
1324 if (chown(dstpath, dstuid, dstgid) < 0) {
1325 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1326 unlink(dstpath);
1327 return 1;
1328 }
1329 } else {
1330 ALOGW("Unable to rename %s to %s: %s\n",
1331 srcpath, dstpath, strerror(errno));
1332 return 1;
1333 }
1334 return 0;
1335 }
1336
1337 d = opendir(srcpath);
1338 if (d == NULL) {
1339 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1340 return 1;
1341 }
1342
1343 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001344
Mike Lockwood94afecf2012-10-24 10:45:23 -07001345 while ((de = readdir(d))) {
1346 const char *name = de->d_name;
1347 /* always skip "." and ".." */
1348 if (name[0] == '.') {
1349 if (name[1] == 0) continue;
1350 if ((name[1] == '.') && (name[2] == 0)) continue;
1351 }
Dave Allisond9370732014-01-30 14:19:23 -08001352
Mike Lockwood94afecf2012-10-24 10:45:23 -07001353 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1354 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1355 continue;
1356 }
Dave Allisond9370732014-01-30 14:19:23 -08001357
Mike Lockwood94afecf2012-10-24 10:45:23 -07001358 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1359 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1360 continue;
1361 }
Dave Allisond9370732014-01-30 14:19:23 -08001362
Mike Lockwood94afecf2012-10-24 10:45:23 -07001363 srcpath[srcend] = dstpath[dstend] = '/';
1364 strcpy(srcpath+srcend+1, name);
1365 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001366
Mike Lockwood94afecf2012-10-24 10:45:23 -07001367 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1368 res = 1;
1369 }
Dave Allisond9370732014-01-30 14:19:23 -08001370
Mike Lockwood94afecf2012-10-24 10:45:23 -07001371 // Note: we will be leaving empty directories behind in srcpath,
1372 // but that is okay, the package manager will be erasing all of the
1373 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001374
Mike Lockwood94afecf2012-10-24 10:45:23 -07001375 srcpath[srcend] = dstpath[dstend] = 0;
1376 }
Dave Allisond9370732014-01-30 14:19:23 -08001377
Mike Lockwood94afecf2012-10-24 10:45:23 -07001378 closedir(d);
1379 return res;
1380}
1381
1382int movefiles()
1383{
1384 DIR *d;
1385 int dfd, subfd;
1386 struct dirent *de;
1387 struct stat s;
1388 char buf[PKG_PATH_MAX+1];
1389 int bufp, bufe, bufi, readlen;
1390
1391 char srcpkg[PKG_NAME_MAX];
1392 char dstpkg[PKG_NAME_MAX];
1393 char srcpath[PKG_PATH_MAX];
1394 char dstpath[PKG_PATH_MAX];
1395 int dstuid=-1, dstgid=-1;
1396 int hasspace;
1397
1398 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1399 if (d == NULL) {
1400 goto done;
1401 }
1402 dfd = dirfd(d);
1403
1404 /* Iterate through all files in the directory, executing the
1405 * file movements requested there-in.
1406 */
1407 while ((de = readdir(d))) {
1408 const char *name = de->d_name;
1409
1410 if (de->d_type == DT_DIR) {
1411 continue;
1412 } else {
1413 subfd = openat(dfd, name, O_RDONLY);
1414 if (subfd < 0) {
1415 ALOGW("Unable to open update commands at %s%s\n",
1416 UPDATE_COMMANDS_DIR_PREFIX, name);
1417 continue;
1418 }
Dave Allisond9370732014-01-30 14:19:23 -08001419
Mike Lockwood94afecf2012-10-24 10:45:23 -07001420 bufp = 0;
1421 bufe = 0;
1422 buf[PKG_PATH_MAX] = 0;
1423 srcpkg[0] = dstpkg[0] = 0;
1424 while (1) {
1425 bufi = bufp;
1426 while (bufi < bufe && buf[bufi] != '\n') {
1427 bufi++;
1428 }
1429 if (bufi < bufe) {
1430 buf[bufi] = 0;
1431 ALOGV("Processing line: %s\n", buf+bufp);
1432 hasspace = 0;
1433 while (bufp < bufi && isspace(buf[bufp])) {
1434 hasspace = 1;
1435 bufp++;
1436 }
1437 if (buf[bufp] == '#' || bufp == bufi) {
1438 // skip comments and empty lines.
1439 } else if (hasspace) {
1440 if (dstpkg[0] == 0) {
1441 ALOGW("Path before package line in %s%s: %s\n",
1442 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1443 } else if (srcpkg[0] == 0) {
1444 // Skip -- source package no longer exists.
1445 } else {
1446 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1447 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1448 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1449 movefileordir(srcpath, dstpath,
1450 strlen(dstpath)-strlen(buf+bufp),
1451 dstuid, dstgid, &s);
1452 }
1453 }
1454 } else {
1455 char* div = strchr(buf+bufp, ':');
1456 if (div == NULL) {
1457 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1458 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1459 } else {
1460 *div = 0;
1461 div++;
1462 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1463 strcpy(dstpkg, buf+bufp);
1464 } else {
1465 srcpkg[0] = dstpkg[0] = 0;
1466 ALOGW("Package name too long in %s%s: %s\n",
1467 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1468 }
1469 if (strlen(div) < PKG_NAME_MAX) {
1470 strcpy(srcpkg, div);
1471 } else {
1472 srcpkg[0] = dstpkg[0] = 0;
1473 ALOGW("Package name too long in %s%s: %s\n",
1474 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1475 }
1476 if (srcpkg[0] != 0) {
1477 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1478 if (lstat(srcpath, &s) < 0) {
1479 // Package no longer exists -- skip.
1480 srcpkg[0] = 0;
1481 }
1482 } else {
1483 srcpkg[0] = 0;
1484 ALOGW("Can't create path %s in %s%s\n",
1485 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1486 }
1487 if (srcpkg[0] != 0) {
1488 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1489 if (lstat(dstpath, &s) == 0) {
1490 dstuid = s.st_uid;
1491 dstgid = s.st_gid;
1492 } else {
1493 // Destination package doesn't
1494 // exist... due to original-package,
1495 // this is normal, so don't be
1496 // noisy about it.
1497 srcpkg[0] = 0;
1498 }
1499 } else {
1500 srcpkg[0] = 0;
1501 ALOGW("Can't create path %s in %s%s\n",
1502 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1503 }
1504 }
1505 ALOGV("Transfering from %s to %s: uid=%d\n",
1506 srcpkg, dstpkg, dstuid);
1507 }
1508 }
1509 }
1510 bufp = bufi+1;
1511 } else {
1512 if (bufp == 0) {
1513 if (bufp < bufe) {
1514 ALOGW("Line too long in %s%s, skipping: %s\n",
1515 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1516 }
1517 } else if (bufp < bufe) {
1518 memcpy(buf, buf+bufp, bufe-bufp);
1519 bufe -= bufp;
1520 bufp = 0;
1521 }
1522 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1523 if (readlen < 0) {
1524 ALOGW("Failure reading update commands in %s%s: %s\n",
1525 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1526 break;
1527 } else if (readlen == 0) {
1528 break;
1529 }
1530 bufe += readlen;
1531 buf[bufe] = 0;
1532 ALOGV("Read buf: %s\n", buf);
1533 }
1534 }
1535 close(subfd);
1536 }
1537 }
1538 closedir(d);
1539done:
1540 return 0;
1541}
1542
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001543int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001544{
Mike Lockwood94afecf2012-10-24 10:45:23 -07001545 struct stat s, libStat;
1546 int rc = 0;
1547
Jeff Sharkeyd7921182015-04-30 15:58:19 -07001548 std::string _pkgdir(create_data_user_package_path(uuid, userId, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001549 std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
1550
1551 const char* pkgdir = _pkgdir.c_str();
1552 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001553
1554 if (stat(pkgdir, &s) < 0) return -1;
1555
1556 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1557 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1558 return -1;
1559 }
1560
1561 if (chmod(pkgdir, 0700) < 0) {
1562 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1563 rc = -1;
1564 goto out;
1565 }
1566
1567 if (lstat(libsymlink, &libStat) < 0) {
1568 if (errno != ENOENT) {
1569 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1570 rc = -1;
1571 goto out;
1572 }
1573 } else {
1574 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001575 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001576 rc = -1;
1577 goto out;
1578 }
1579 } else if (S_ISLNK(libStat.st_mode)) {
1580 if (unlink(libsymlink) < 0) {
1581 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1582 rc = -1;
1583 goto out;
1584 }
1585 }
1586 }
1587
1588 if (symlink(asecLibDir, libsymlink) < 0) {
1589 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1590 strerror(errno));
1591 rc = -errno;
1592 goto out;
1593 }
1594
1595out:
1596 if (chmod(pkgdir, s.st_mode) < 0) {
1597 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1598 rc = -errno;
1599 }
1600
1601 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1602 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1603 return -errno;
1604 }
1605
1606 return rc;
1607}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001608
1609static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1610{
1611 static const char *IDMAP_BIN = "/system/bin/idmap";
1612 static const size_t MAX_INT_LEN = 32;
1613 char idmap_str[MAX_INT_LEN];
1614
1615 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1616
1617 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1618 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1619}
1620
1621// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1622// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1623static int flatten_path(const char *prefix, const char *suffix,
1624 const char *overlay_path, char *idmap_path, size_t N)
1625{
1626 if (overlay_path == NULL || idmap_path == NULL) {
1627 return -1;
1628 }
1629 const size_t len_overlay_path = strlen(overlay_path);
1630 // will access overlay_path + 1 further below; requires absolute path
1631 if (len_overlay_path < 2 || *overlay_path != '/') {
1632 return -1;
1633 }
1634 const size_t len_idmap_root = strlen(prefix);
1635 const size_t len_suffix = strlen(suffix);
1636 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1637 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1638 // additions below would cause overflow
1639 return -1;
1640 }
1641 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1642 return -1;
1643 }
1644 memset(idmap_path, 0, N);
1645 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1646 char *ch = idmap_path + len_idmap_root;
1647 while (*ch != '\0') {
1648 if (*ch == '/') {
1649 *ch = '@';
1650 }
1651 ++ch;
1652 }
1653 return 0;
1654}
1655
1656int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1657{
1658 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1659
1660 int idmap_fd = -1;
1661 char idmap_path[PATH_MAX];
1662
1663 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1664 idmap_path, sizeof(idmap_path)) == -1) {
1665 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1666 goto fail;
1667 }
1668
1669 unlink(idmap_path);
1670 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1671 if (idmap_fd < 0) {
1672 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1673 goto fail;
1674 }
1675 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1676 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1677 goto fail;
1678 }
1679 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1680 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1681 goto fail;
1682 }
1683
1684 pid_t pid;
1685 pid = fork();
1686 if (pid == 0) {
1687 /* child -- drop privileges before continuing */
1688 if (setgid(uid) != 0) {
1689 ALOGE("setgid(%d) failed during idmap\n", uid);
1690 exit(1);
1691 }
1692 if (setuid(uid) != 0) {
1693 ALOGE("setuid(%d) failed during idmap\n", uid);
1694 exit(1);
1695 }
1696 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1697 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1698 exit(1);
1699 }
1700
1701 run_idmap(target_apk, overlay_apk, idmap_fd);
1702 exit(1); /* only if exec call to idmap failed */
1703 } else {
1704 int status = wait_child(pid);
1705 if (status != 0) {
1706 ALOGE("idmap failed, status=0x%04x\n", status);
1707 goto fail;
1708 }
1709 }
1710
1711 close(idmap_fd);
1712 return 0;
1713fail:
1714 if (idmap_fd >= 0) {
1715 close(idmap_fd);
1716 unlink(idmap_path);
1717 }
1718 return -1;
1719}
Robert Craige9887e42014-02-20 10:25:56 -05001720
Jeff Sharkey6fe28a02015-04-09 13:10:03 -07001721int restorecon_data(const char* uuid, const char* pkgName,
Andreas Gampe0ad7a112015-04-09 09:52:45 -07001722 const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001723{
Robert Craigda30dc72014-03-27 10:21:12 -04001724 struct dirent *entry;
1725 DIR *d;
1726 struct stat s;
Robert Craige9887e42014-02-20 10:25:56 -05001727 int ret = 0;
1728
Robert Craigda30dc72014-03-27 10:21:12 -04001729 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1730 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1731
1732 if (!pkgName || !seinfo) {
1733 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001734 return -1;
1735 }
1736
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001737 // Special case for owner on internal storage
1738 if (uuid == nullptr) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -07001739 std::string path(create_data_user_package_path(nullptr, 0, pkgName));
Robert Craigda30dc72014-03-27 10:21:12 -04001740
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001741 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, flags) < 0) {
1742 PLOG(ERROR) << "restorecon failed for " << path;
1743 ret |= -1;
1744 }
Robert Craige9887e42014-02-20 10:25:56 -05001745 }
1746
Robert Craigda30dc72014-03-27 10:21:12 -04001747 // Relabel package directory for all secondary users.
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001748 std::string userdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX);
1749 d = opendir(userdir.c_str());
Robert Craigda30dc72014-03-27 10:21:12 -04001750 if (d == NULL) {
Robert Craigda30dc72014-03-27 10:21:12 -04001751 return -1;
1752 }
1753
1754 while ((entry = readdir(d))) {
1755 if (entry->d_type != DT_DIR) {
1756 continue;
1757 }
1758
1759 const char *user = entry->d_name;
1760 // Ignore "." and ".."
1761 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1762 continue;
1763 }
1764
1765 // user directories start with a number
1766 if (user[0] < '0' || user[0] > '9') {
1767 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1768 continue;
1769 }
1770
Jeff Sharkeye3637242015-04-08 20:56:42 -07001771 std::string pkgdir(StringPrintf("%s%s/%s", userdir.c_str(), user, pkgName));
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001772 if (stat(pkgdir.c_str(), &s) < 0) {
Robert Craigda30dc72014-03-27 10:21:12 -04001773 continue;
1774 }
1775
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001776 if (selinux_android_restorecon_pkgdir(pkgdir.c_str(), seinfo, s.st_uid, flags) < 0) {
1777 PLOG(ERROR) << "restorecon failed for " << pkgdir;
Robert Craigda30dc72014-03-27 10:21:12 -04001778 ret |= -1;
1779 }
Robert Craigda30dc72014-03-27 10:21:12 -04001780 }
1781
1782 closedir(d);
Robert Craige9887e42014-02-20 10:25:56 -05001783 return ret;
1784}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001785
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001786int create_oat_dir(const char* oat_dir, const char* instruction_set)
1787{
1788 char oat_instr_dir[PKG_PATH_MAX];
1789
1790 if (validate_apk_path(oat_dir)) {
1791 ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
1792 return -1;
1793 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001794 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001795 return -1;
1796 }
1797 if (selinux_android_restorecon(oat_dir, 0)) {
1798 ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
1799 return -1;
1800 }
1801 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001802 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001803 return -1;
1804 }
1805 return 0;
1806}
1807
1808int rm_package_dir(const char* apk_path)
1809{
1810 if (validate_apk_path(apk_path)) {
1811 ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
1812 return -1;
1813 }
1814 return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */);
1815}
1816
Narayan Kamathd845c962015-06-04 13:20:27 +01001817int link_file(const char* relative_path, const char* from_base, const char* to_base) {
1818 char from_path[PKG_PATH_MAX];
1819 char to_path[PKG_PATH_MAX];
1820 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
1821 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
1822
1823 if (validate_apk_path_subdirs(from_path)) {
1824 ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path);
1825 return -1;
1826 }
1827
1828 if (validate_apk_path_subdirs(to_path)) {
1829 ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path);
1830 return -1;
1831 }
1832
1833 const int ret = link(from_path, to_path);
1834 if (ret < 0) {
1835 ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno));
1836 return -1;
1837 }
1838
1839 return 0;
1840}
1841
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001842int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
1843 const char *instruction_set) {
1844 char *file_name_start;
1845 char *file_name_end;
1846
1847 file_name_start = strrchr(apk_path, '/');
1848 if (file_name_start == NULL) {
1849 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
1850 return -1;
1851 }
1852 file_name_end = strrchr(apk_path, '.');
1853 if (file_name_end < file_name_start) {
1854 ALOGE("apk_path '%s' has no extension\n", apk_path);
1855 return -1;
1856 }
1857
1858 // Calculate file_name
1859 int file_name_len = file_name_end - file_name_start - 1;
1860 char file_name[file_name_len + 1];
1861 memcpy(file_name, file_name_start + 1, file_name_len);
1862 file_name[file_name_len] = '\0';
1863
1864 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
1865 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
1866 return 0;
1867}