blob: e0e2eb3b8c3c4fcef127cdc5df7d2e12d8d5d48f [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 Sharkeyc03de092015-04-07 18:14:05 -070046int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070047{
Mike Lockwood94afecf2012-10-24 10:45:23 -070048 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
49 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
50 return -1;
51 }
52
Jeff Sharkeyd7921182015-04-30 15:58:19 -070053 std::string _pkgdir(create_data_user_package_path(uuid, 0, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -070054 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070055
Nick Kralevicha2d838a2013-01-09 16:00:35 -080056 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070057 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
58 return -1;
59 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080060 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070061 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
62 unlink(pkgdir);
63 return -1;
64 }
65
Stephen Smalley26288202014-02-07 09:16:46 -050066 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070067 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -070068 unlink(pkgdir);
69 return -errno;
70 }
71
72 if (chown(pkgdir, uid, gid) < 0) {
73 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -070074 unlink(pkgdir);
75 return -1;
76 }
77
78 return 0;
79}
80
Jeff Sharkeyc03de092015-04-07 18:14:05 -070081int uninstall(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -070082{
Jeff Sharkeyd7921182015-04-30 15:58:19 -070083 std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -070084 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070085
Dave Allisond9370732014-01-30 14:19:23 -080086 remove_profile_file(pkgname);
87
Mike Lockwood94afecf2012-10-24 10:45:23 -070088 /* delete contents AND directory, no exceptions */
89 return delete_dir_contents(pkgdir, 1, NULL);
90}
91
92int renamepkg(const char *oldpkgname, const char *newpkgname)
93{
94 char oldpkgdir[PKG_PATH_MAX];
95 char newpkgdir[PKG_PATH_MAX];
96
97 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
98 return -1;
99 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
100 return -1;
101
102 if (rename(oldpkgdir, newpkgdir) < 0) {
103 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
104 return -errno;
105 }
106 return 0;
107}
108
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700109int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700110{
Mike Lockwood94afecf2012-10-24 10:45:23 -0700111 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700112
113 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
114 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
115 return -1;
116 }
117
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700118 std::string _pkgdir(create_data_user_package_path(uuid, 0, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700119 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700120
121 if (stat(pkgdir, &s) < 0) return -1;
122
123 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700124 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 -0700125 return -1;
126 }
127
128 if (chmod(pkgdir, 0751) < 0) {
129 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
130 unlink(pkgdir);
131 return -errno;
132 }
133 if (chown(pkgdir, uid, gid) < 0) {
134 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
135 unlink(pkgdir);
136 return -errno;
137 }
138
139 return 0;
140}
141
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700142int delete_user_data(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700143{
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700144 std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700145 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146
Jeff Sharkey3316fe42014-08-27 10:46:25 -0700147 return delete_dir_contents(pkgdir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148}
149
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700150int 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 -0700151{
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700152 std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700153 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700154
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800155 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700156 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
157 return -errno;
158 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800159 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700160 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
161 unlink(pkgdir);
162 return -errno;
163 }
164
Stephen Smalley26288202014-02-07 09:16:46 -0500165 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700166 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700167 unlink(pkgdir);
168 return -errno;
169 }
170
171 if (chown(pkgdir, uid, uid) < 0) {
172 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700173 unlink(pkgdir);
174 return -errno;
175 }
176
177 return 0;
178}
179
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700180int move_complete_app(const char *from_uuid, const char *to_uuid,
181 const char *package_name, const char *data_app_name, appid_t appid,
182 const char* seinfo) {
Jeff Sharkeye3637242015-04-08 20:56:42 -0700183 std::vector<userid_t> users = get_known_users(from_uuid);
184
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700185 // Copy app
186 {
187 std::string from(create_data_app_package_path(from_uuid, data_app_name));
188 std::string to(create_data_app_package_path(to_uuid, data_app_name));
189 std::string to_parent(create_data_app_path(to_uuid));
190
191 char *argv[] = {
192 (char*) kCpPath,
193 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
194 (char*) "-p", /* preserve timestamps, ownership, and permissions */
195 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
196 (char*) "-P", /* Do not follow symlinks [default] */
197 (char*) "-d", /* don't dereference symlinks */
198 (char*) from.c_str(),
199 (char*) to_parent.c_str()
200 };
201
202 LOG(DEBUG) << "Copying " << from << " to " << to;
203 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
204
205 if (rc != 0) {
206 LOG(ERROR) << "Failed copying " << from << " to " << to
207 << ": status " << rc;
208 goto fail;
209 }
210
211 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
212 LOG(ERROR) << "Failed to restorecon " << to;
213 goto fail;
214 }
215 }
216
217 // Copy private data for all known users
Jeff Sharkeye3637242015-04-08 20:56:42 -0700218 for (auto user : users) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700219 std::string from(create_data_user_package_path(from_uuid, user, package_name));
220 std::string to(create_data_user_package_path(to_uuid, user, package_name));
221 std::string to_parent(create_data_user_path(to_uuid, user));
Jeff Sharkeye3637242015-04-08 20:56:42 -0700222
223 // Data source may not exist for all users; that's okay
224 if (access(from.c_str(), F_OK) != 0) {
225 LOG(INFO) << "Missing source " << from;
226 continue;
227 }
228
229 std::string user_path(create_data_user_path(to_uuid, user));
230 if (fs_prepare_dir(user_path.c_str(), 0771, AID_SYSTEM, AID_SYSTEM) != 0) {
231 LOG(ERROR) << "Failed to prepare user target " << user_path;
232 goto fail;
233 }
234
235 uid_t uid = multiuser_get_uid(user, appid);
236 if (make_user_data(to_uuid, package_name, uid, user, seinfo) != 0) {
237 LOG(ERROR) << "Failed to create package target " << to;
238 goto fail;
239 }
240
241 char *argv[] = {
242 (char*) kCpPath,
243 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
244 (char*) "-p", /* preserve timestamps, ownership, and permissions */
245 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
246 (char*) "-P", /* Do not follow symlinks [default] */
247 (char*) "-d", /* don't dereference symlinks */
248 (char*) from.c_str(),
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700249 (char*) to_parent.c_str()
Jeff Sharkeye3637242015-04-08 20:56:42 -0700250 };
251
252 LOG(DEBUG) << "Copying " << from << " to " << to;
253 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
254
255 if (rc != 0) {
256 LOG(ERROR) << "Failed copying " << from << " to " << to
257 << ": status " << rc;
258 goto fail;
259 }
260
261 if (restorecon_data(to_uuid, package_name, seinfo, uid) != 0) {
262 LOG(ERROR) << "Failed to restorecon " << to;
263 goto fail;
264 }
265 }
266
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700267 // Delete old app and data
268 {
269 std::string from(create_data_app_package_path(from_uuid, data_app_name));
270 if (delete_dir_contents(from.c_str(), 1, NULL) != 0) {
271 LOG(WARNING) << "Failed to delete " << from;
272 }
273 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700274 for (auto user : users) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700275 std::string from(create_data_user_package_path(from_uuid, user, package_name));
Jeff Sharkeye3637242015-04-08 20:56:42 -0700276 if (delete_dir_contents(from.c_str(), 1, NULL) != 0) {
277 LOG(WARNING) << "Failed to delete " << from;
278 }
279 }
280 return 0;
281
282fail:
283 // Nuke everything we might have already copied
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700284 {
285 std::string to(create_data_app_package_path(to_uuid, data_app_name));
286 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
287 LOG(WARNING) << "Failed to rollback " << to;
288 }
289 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700290 for (auto user : users) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700291 std::string to(create_data_user_package_path(to_uuid, user, package_name));
Jeff Sharkeye3637242015-04-08 20:56:42 -0700292 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
293 LOG(WARNING) << "Failed to rollback " << to;
294 }
295 }
296 return -1;
297}
298
Robin Lee7c8bec02014-06-10 18:46:26 +0100299int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700300{
Robin Lee095c7632014-04-25 15:05:19 +0100301 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700302 return -1;
303 }
304
305 return 0;
306}
307
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700308int delete_user(const char *uuid, userid_t userid)
Robin Lee095c7632014-04-25 15:05:19 +0100309{
310 int status = 0;
311
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700312 std::string data_path(create_data_user_path(uuid, userid));
313 if (delete_dir_contents(data_path.c_str(), 1, NULL) != 0) {
Robin Lee095c7632014-04-25 15:05:19 +0100314 status = -1;
315 }
316
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700317 std::string media_path(create_data_media_path(uuid, userid));
318 if (delete_dir_contents(media_path.c_str(), 1, NULL) != 0) {
Robin Lee095c7632014-04-25 15:05:19 +0100319 status = -1;
320 }
321
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700322 // Config paths only exist on internal storage
323 if (uuid == nullptr) {
324 char config_path[PATH_MAX];
325 if ((create_user_config_path(config_path, userid) != 0)
326 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
327 status = -1;
328 }
Robin Lee095c7632014-04-25 15:05:19 +0100329 }
330
331 return status;
332}
333
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700334int delete_cache(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700335{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700336 std::string _cachedir(
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700337 create_data_user_package_path(uuid, userid, pkgname) + CACHE_DIR_POSTFIX);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700338 const char* cachedir = _cachedir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700339
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700340 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100341 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700342}
343
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700344int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid)
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700345{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700346 std::string _codecachedir(
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700347 create_data_user_package_path(uuid, userid, pkgname) + CACHE_DIR_POSTFIX);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700348 const char* codecachedir = _codecachedir.c_str();
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700349
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700350 struct stat s;
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700351
Jeff Sharkey770180a2014-09-08 17:14:26 -0700352 /* it's okay if code cache is missing */
353 if (lstat(codecachedir, &s) == -1 && errno == ENOENT) {
354 return 0;
355 }
356
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700357 /* delete contents, not the directory, no exceptions */
358 return delete_dir_contents(codecachedir, 0, NULL);
359}
360
Mike Lockwood94afecf2012-10-24 10:45:23 -0700361/* Try to ensure free_size bytes of storage are available.
362 * Returns 0 on success.
363 * This is rather simple-minded because doing a full LRU would
364 * be potentially memory-intensive, and without atime it would
365 * also require that apps constantly modify file metadata even
366 * when just reading from the cache, which is pretty awful.
367 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700368int free_cache(const char *uuid, int64_t free_size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700369{
370 cache_t* cache;
371 int64_t avail;
372 DIR *d;
373 struct dirent *de;
374 char tmpdir[PATH_MAX];
375 char *dirpos;
376
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700377 std::string data_path(create_data_path(uuid));
378
379 avail = data_disk_free(data_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700380 if (avail < 0) return -1;
381
382 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
383 if (avail >= free_size) return 0;
384
385 cache = start_cache_collection();
386
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700387 // Special case for owner on internal storage
388 if (uuid == nullptr) {
389 std::string _tmpdir(create_data_user_path(nullptr, 0));
390 add_cache_files(cache, _tmpdir.c_str(), "cache");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700391 }
392
393 // Search for other users and add any cache files from them.
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700394 std::string _tmpdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX);
395 strcpy(tmpdir, _tmpdir.c_str());
396
Mike Lockwood94afecf2012-10-24 10:45:23 -0700397 dirpos = tmpdir + strlen(tmpdir);
398 d = opendir(tmpdir);
399 if (d != NULL) {
400 while ((de = readdir(d))) {
401 if (de->d_type == DT_DIR) {
402 const char *name = de->d_name;
403 /* always skip "." and ".." */
404 if (name[0] == '.') {
405 if (name[1] == 0) continue;
406 if ((name[1] == '.') && (name[2] == 0)) continue;
407 }
408 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
409 strcpy(dirpos, name);
410 //ALOGI("adding cache files from %s\n", tmpdir);
411 add_cache_files(cache, tmpdir, "cache");
412 } else {
413 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
414 }
415 }
416 }
417 closedir(d);
418 }
419
420 // Collect cache files on external storage for all users (if it is mounted as part
421 // of the internal storage).
422 strcpy(tmpdir, android_media_dir.path);
423 dirpos = tmpdir + strlen(tmpdir);
424 d = opendir(tmpdir);
425 if (d != NULL) {
426 while ((de = readdir(d))) {
427 if (de->d_type == DT_DIR) {
428 const char *name = de->d_name;
429 /* skip any dir that doesn't start with a number, so not a user */
430 if (name[0] < '0' || name[0] > '9') {
431 continue;
432 }
433 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
434 strcpy(dirpos, name);
435 if (lookup_media_dir(tmpdir, "Android") == 0
436 && lookup_media_dir(tmpdir, "data") == 0) {
437 //ALOGI("adding cache files from %s\n", tmpdir);
438 add_cache_files(cache, tmpdir, "cache");
439 }
440 } else {
441 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
442 }
443 }
444 }
445 closedir(d);
446 }
447
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700448 clear_cache_files(data_path, cache, free_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700449 finish_cache_collection(cache);
450
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700451 return data_disk_free(data_path) >= free_size ? 0 : -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700452}
453
Narayan Kamath1b400322014-04-11 13:17:00 +0100454int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700455{
456 char src_dex[PKG_PATH_MAX];
457 char dst_dex[PKG_PATH_MAX];
458
Jeff Sharkey770180a2014-09-08 17:14:26 -0700459 if (validate_apk_path(src)) {
460 ALOGE("invalid apk path '%s' (bad prefix)\n", src);
461 return -1;
462 }
463 if (validate_apk_path(dst)) {
464 ALOGE("invalid apk path '%s' (bad prefix)\n", dst);
465 return -1;
466 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700467
Narayan Kamath1b400322014-04-11 13:17:00 +0100468 if (create_cache_path(src_dex, src, instruction_set)) return -1;
469 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700470
471 ALOGV("move %s -> %s\n", src_dex, dst_dex);
472 if (rename(src_dex, dst_dex) < 0) {
473 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
474 return -1;
475 } else {
476 return 0;
477 }
478}
479
Narayan Kamath1b400322014-04-11 13:17:00 +0100480int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700481{
482 char dex_path[PKG_PATH_MAX];
483
Jeff Sharkey770180a2014-09-08 17:14:26 -0700484 if (validate_apk_path(path) && validate_system_app_path(path)) {
485 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
486 return -1;
487 }
488
Narayan Kamath1b400322014-04-11 13:17:00 +0100489 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700490
491 ALOGV("unlink %s\n", dex_path);
492 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700493 if (errno != ENOENT) {
494 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
495 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700496 return -1;
497 } else {
498 return 0;
499 }
500}
501
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700502int get_size(const char *uuid, const char *pkgname, int userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700503 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100504 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
505 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700506{
507 DIR *d;
508 int dfd;
509 struct dirent *de;
510 struct stat s;
511 char path[PKG_PATH_MAX];
512
513 int64_t codesize = 0;
514 int64_t datasize = 0;
515 int64_t cachesize = 0;
516 int64_t asecsize = 0;
517
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700518 /* count the source apk as code -- but only if it's not
519 * on the /system partition and its not on the sdcard. */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700520 if (validate_system_app_path(apkpath) &&
521 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
522 if (stat(apkpath, &s) == 0) {
523 codesize += stat_size(&s);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700524 if (S_ISDIR(s.st_mode)) {
525 d = opendir(apkpath);
526 if (d != NULL) {
527 dfd = dirfd(d);
528 codesize += calculate_dir_size(dfd);
529 closedir(d);
530 }
531 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700532 }
533 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700534
535 /* count the forward locked apk as code if it is given */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700536 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
537 if (stat(fwdlock_apkpath, &s) == 0) {
538 codesize += stat_size(&s);
539 }
540 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700541
542 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100543 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700544 if (stat(path, &s) == 0) {
545 codesize += stat_size(&s);
546 }
547 }
548
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700549 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700550 if (libdirpath != NULL && libdirpath[0] != '!') {
551 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700552 if (d != NULL) {
553 dfd = dirfd(d);
554 codesize += calculate_dir_size(dfd);
555 closedir(d);
556 }
557 }
558
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700559 /* compute asec size if it is given */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700560 if (asecpath != NULL && asecpath[0] != '!') {
561 if (stat(asecpath, &s) == 0) {
562 asecsize += stat_size(&s);
563 }
564 }
565
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700566 std::vector<userid_t> users;
567 if (userid == -1) {
568 users = get_known_users(uuid);
569 } else {
570 users.push_back(userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700571 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700572
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700573 for (auto user : users) {
574 std::string _pkgdir(create_data_user_package_path(uuid, user, pkgname));
575 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700576
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700577 d = opendir(pkgdir);
578 if (d == NULL) {
579 PLOG(WARNING) << "Failed to open " << pkgdir;
580 continue;
581 }
582 dfd = dirfd(d);
583
584 /* most stuff in the pkgdir is data, except for the "cache"
585 * directory and below, which is cache, and the "lib" directory
586 * and below, which is code...
587 */
588 while ((de = readdir(d))) {
589 const char *name = de->d_name;
590
591 if (de->d_type == DT_DIR) {
592 int subfd;
593 int64_t statsize = 0;
594 int64_t dirsize = 0;
595 /* always skip "." and ".." */
596 if (name[0] == '.') {
597 if (name[1] == 0) continue;
598 if ((name[1] == '.') && (name[2] == 0)) continue;
599 }
600 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
601 statsize = stat_size(&s);
602 }
603 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
604 if (subfd >= 0) {
605 dirsize = calculate_dir_size(subfd);
606 }
607 if(!strcmp(name,"lib")) {
608 codesize += dirsize + statsize;
609 } else if(!strcmp(name,"cache")) {
610 cachesize += dirsize + statsize;
611 } else {
612 datasize += dirsize + statsize;
613 }
614 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
615 // This is the symbolic link to the application's library
616 // code. We'll count this as code instead of data, since
617 // it is not something that the app creates.
618 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
619 codesize += stat_size(&s);
620 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700621 } else {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700622 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
623 datasize += stat_size(&s);
624 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700625 }
626 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700627 closedir(d);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700628 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700629 *_codesize = codesize;
630 *_datasize = datasize;
631 *_cachesize = cachesize;
632 *_asecsize = asecsize;
633 return 0;
634}
635
Narayan Kamath1b400322014-04-11 13:17:00 +0100636int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700637{
638 char *tmp;
639 int srclen;
640 int dstlen;
641
642 srclen = strlen(src);
643
644 /* demand that we are an absolute path */
645 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
646 return -1;
647 }
648
649 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
650 return -1;
651 }
652
Dave Allisond9370732014-01-30 14:19:23 -0800653 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100654 strlen(instruction_set) +
655 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800656
Mike Lockwood94afecf2012-10-24 10:45:23 -0700657 if (dstlen > PKG_PATH_MAX) {
658 return -1;
659 }
660
Narayan Kamath1b400322014-04-11 13:17:00 +0100661 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700662 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100663 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700664 src + 1, /* skip the leading / */
665 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800666
Narayan Kamath1b400322014-04-11 13:17:00 +0100667 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700668 if (*tmp == '/') {
669 *tmp = '@';
670 }
671 }
672
673 return 0;
674}
675
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700676static int split_count(const char *str)
677{
678 char *ctx;
679 int count = 0;
680 char buf[PROPERTY_VALUE_MAX];
681
682 strncpy(buf, str, sizeof(buf));
683 char *pBuf = buf;
684
685 while(strtok_r(pBuf, " ", &ctx) != NULL) {
686 count++;
687 pBuf = NULL;
688 }
689
690 return count;
691}
692
neo.chae14e084d2015-01-07 18:46:13 +0900693static int split(char *buf, const char **argv)
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700694{
695 char *ctx;
696 int count = 0;
697 char *tok;
698 char *pBuf = buf;
699
700 while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
701 argv[count++] = tok;
702 pBuf = NULL;
703 }
704
705 return count;
706}
707
Alex Light7365a102014-07-21 12:23:48 -0700708static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700709 const char* output_file_name, const char *pkgname __unused, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700710{
711 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100712 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700713
714 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
715 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
716 ALOGE("Instruction set %s longer than max length of %d",
717 instruction_set, MAX_INSTRUCTION_SET_LEN);
718 return;
719 }
720
721 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
722 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
723 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
724 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
725 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
726 // The caller has already gotten all the locks we need.
727 const char* no_lock_arg = "--no-lock-output";
728 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
729 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
730 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700731 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700732 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
733
734 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
735 char* argv[7];
736 argv[0] = (char*) PATCHOAT_BIN;
737 argv[1] = (char*) patched_image_location_arg;
738 argv[2] = (char*) no_lock_arg;
739 argv[3] = instruction_set_arg;
740 argv[4] = output_oat_fd_arg;
741 argv[5] = input_oat_fd_arg;
742 argv[6] = NULL;
743
744 execv(PATCHOAT_BIN, (char* const *)argv);
745 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
746}
747
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700748static bool check_boolean_property(const char* property_name, bool default_value = false) {
749 char tmp_property_value[PROPERTY_VALUE_MAX];
750 bool have_property = property_get(property_name, tmp_property_value, nullptr) > 0;
751 if (!have_property) {
752 return default_value;
753 }
754 return strcmp(tmp_property_value, "true") == 0;
755}
756
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700757static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Andreas Gampee1c01352014-12-10 16:41:11 -0800758 const char* output_file_name, int swap_fd, const char *pkgname, const char *instruction_set,
Andreas Gampe598c25e2015-03-03 09:15:06 -0800759 bool vm_safe_mode, bool debuggable)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700760{
Calin Juravle8fc73152014-08-19 18:48:50 +0100761 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
762
763 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
764 ALOGE("Instruction set %s longer than max length of %d",
765 instruction_set, MAX_INSTRUCTION_SET_LEN);
766 return;
767 }
768
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700769 char prop_buf[PROPERTY_VALUE_MAX];
770 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
771
772 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
773 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
774
775 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
776 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
777
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700778 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
779 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
780 dex2oat_compiler_filter_flag, NULL) > 0;
781
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700782 char dex2oat_threads_buf[PROPERTY_VALUE_MAX];
783 bool have_dex2oat_threads_flag = property_get("dalvik.vm.dex2oat-threads", dex2oat_threads_buf,
784 NULL) > 0;
785 char dex2oat_threads_arg[PROPERTY_VALUE_MAX + 2];
786 if (have_dex2oat_threads_flag) {
787 sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf);
788 }
789
Calin Juravle8fc73152014-08-19 18:48:50 +0100790 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
791 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
792 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
793 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
794 dex2oat_isa_features, NULL) > 0;
795
Ian Rogers16a95b22014-11-08 16:58:13 -0800796 char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
797 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
798 char dex2oat_isa_variant[PROPERTY_VALUE_MAX];
799 bool have_dex2oat_isa_variant = property_get(dex2oat_isa_variant_key,
800 dex2oat_isa_variant, NULL) > 0;
801
neo.chae14e084d2015-01-07 18:46:13 +0900802 const char *dex2oat_norelocation = "-Xnorelocate";
803 bool have_dex2oat_relocation_skip_flag = false;
804
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800805 char dex2oat_flags[PROPERTY_VALUE_MAX];
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700806 int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags",
807 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800808 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
809
Brian Carlstrom538998f2014-07-30 14:37:11 -0700810 // If we booting without the real /data, don't spend time compiling.
811 char vold_decrypt[PROPERTY_VALUE_MAX];
812 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
813 bool skip_compilation = (have_vold_decrypt &&
814 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
815 (strcmp(vold_decrypt, "1") == 0)));
816
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700817 bool use_jit = check_boolean_property("debug.usejit");
818 bool gen_cfi = check_boolean_property("debug.gencfi");
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700819
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700820 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700821
Brian Carlstrom53e07762014-06-27 14:15:19 -0700822 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700823
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700824 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100825
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700826 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
827 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
828 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700829 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100830 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Ian Rogers16a95b22014-11-08 16:58:13 -0800831 char instruction_set_variant_arg[strlen("--instruction-set-variant=") + PROPERTY_VALUE_MAX];
Calin Juravle8fc73152014-08-19 18:48:50 +0100832 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100833 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
834 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700835 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
836 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700837 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800838 bool have_dex2oat_swap_fd = false;
839 char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700840
841 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
842 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
843 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
844 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100845 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Ian Rogers16a95b22014-11-08 16:58:13 -0800846 sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
Calin Juravle8fc73152014-08-19 18:48:50 +0100847 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Andreas Gampee1c01352014-12-10 16:41:11 -0800848 if (swap_fd >= 0) {
849 have_dex2oat_swap_fd = true;
850 sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
851 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100852
Calin Juravle4fdff462014-06-06 16:58:43 +0100853 bool have_profile_file = false;
854 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100855 if (profiler && (strcmp(pkgname, "*") != 0)) {
856 char profile_file[PKG_PATH_MAX];
857 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800858 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100859 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100860 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100861 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100862 have_profile_file = true;
863 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
864 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
865 "--top-k-profile-threshold=%s", prop_buf);
866 have_top_k_profile_threshold = true;
867 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100868 }
Dave Allisond9370732014-01-30 14:19:23 -0800869 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700870
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700871 if (have_dex2oat_Xms_flag) {
872 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
873 }
874 if (have_dex2oat_Xmx_flag) {
875 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
876 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700877 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700878 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700879 have_dex2oat_compiler_filter_flag = true;
neo.chae14e084d2015-01-07 18:46:13 +0900880 have_dex2oat_relocation_skip_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100881 } else if (vm_safe_mode) {
882 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100883 have_dex2oat_compiler_filter_flag = true;
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700884 } else if (use_jit) {
885 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-at-runtime");
886 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700887 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700888 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
889 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700890
Andreas Gampe598c25e2015-03-03 09:15:06 -0800891 // Check whether all apps should be compiled debuggable.
892 if (!debuggable) {
893 debuggable =
894 (property_get("dalvik.vm.always_debuggable", prop_buf, "0") > 0) &&
895 (prop_buf[0] == '1');
896 }
897
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700898 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100899
neo.chae14e084d2015-01-07 18:46:13 +0900900 const char* argv[7 // program name, mandatory arguments and the final NULL
901 + (have_dex2oat_isa_variant ? 1 : 0)
902 + (have_dex2oat_isa_features ? 1 : 0)
903 + (have_profile_file ? 1 : 0)
904 + (have_top_k_profile_threshold ? 1 : 0)
905 + (have_dex2oat_Xms_flag ? 2 : 0)
906 + (have_dex2oat_Xmx_flag ? 2 : 0)
907 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700908 + (have_dex2oat_threads_flag ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900909 + (have_dex2oat_swap_fd ? 1 : 0)
910 + (have_dex2oat_relocation_skip_flag ? 2 : 0)
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700911 + (gen_cfi ? 1 : 0)
Andreas Gampe598c25e2015-03-03 09:15:06 -0800912 + (debuggable ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900913 + dex2oat_flags_count];
Calin Juravle4fdff462014-06-06 16:58:43 +0100914 int i = 0;
neo.chae14e084d2015-01-07 18:46:13 +0900915 argv[i++] = DEX2OAT_BIN;
Calin Juravle4fdff462014-06-06 16:58:43 +0100916 argv[i++] = zip_fd_arg;
917 argv[i++] = zip_location_arg;
918 argv[i++] = oat_fd_arg;
919 argv[i++] = oat_location_arg;
920 argv[i++] = instruction_set_arg;
Ian Rogers16a95b22014-11-08 16:58:13 -0800921 if (have_dex2oat_isa_variant) {
922 argv[i++] = instruction_set_variant_arg;
923 }
Calin Juravle8fc73152014-08-19 18:48:50 +0100924 if (have_dex2oat_isa_features) {
925 argv[i++] = instruction_set_features_arg;
926 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100927 if (have_profile_file) {
928 argv[i++] = profile_file_arg;
929 }
930 if (have_top_k_profile_threshold) {
931 argv[i++] = top_k_profile_threshold_arg;
932 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700933 if (have_dex2oat_Xms_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900934 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700935 argv[i++] = dex2oat_Xms_arg;
936 }
937 if (have_dex2oat_Xmx_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900938 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700939 argv[i++] = dex2oat_Xmx_arg;
940 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700941 if (have_dex2oat_compiler_filter_flag) {
942 argv[i++] = dex2oat_compiler_filter_arg;
943 }
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700944 if (have_dex2oat_threads_flag) {
945 argv[i++] = dex2oat_threads_arg;
946 }
Andreas Gampee1c01352014-12-10 16:41:11 -0800947 if (have_dex2oat_swap_fd) {
948 argv[i++] = dex2oat_swap_fd;
949 }
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700950 if (gen_cfi) {
951 argv[i++] = "--include-cfi";
952 }
Andreas Gampe598c25e2015-03-03 09:15:06 -0800953 if (debuggable) {
954 argv[i++] = "--debuggable";
955 }
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700956 if (dex2oat_flags_count) {
957 i += split(dex2oat_flags, argv + i);
Calin Juravle4fdff462014-06-06 16:58:43 +0100958 }
neo.chae14e084d2015-01-07 18:46:13 +0900959 if (have_dex2oat_relocation_skip_flag) {
960 argv[i++] = RUNTIME_ARG;
961 argv[i++] = dex2oat_norelocation;
962 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700963 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100964 argv[i] = NULL;
965
neo.chae14e084d2015-01-07 18:46:13 +0900966 execv(DEX2OAT_BIN, (char * const *)argv);
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700967 ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700968}
969
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100970static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700971{
972 int status;
973 pid_t got_pid;
974
Mike Lockwood94afecf2012-10-24 10:45:23 -0700975 while (1) {
976 got_pid = waitpid(pid, &status, 0);
977 if (got_pid == -1 && errno == EINTR) {
978 printf("waitpid interrupted, retrying\n");
979 } else {
980 break;
981 }
982 }
983 if (got_pid != pid) {
984 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
985 (int) pid, (int) got_pid, strerror(errno));
986 return 1;
987 }
988
989 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700990 return 0;
991 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700992 return status; /* always nonzero */
993 }
994}
995
Andreas Gampee1c01352014-12-10 16:41:11 -0800996/*
997 * Whether dexopt should use a swap file when compiling an APK. If kAlwaysProvideSwapFile, do this
998 * on all devices (dex2oat will make a more informed decision itself, anyways). Otherwise, only do
999 * this on a low-mem device.
1000 */
1001static bool kAlwaysProvideSwapFile = true;
1002
1003static bool ShouldUseSwapFileForDexopt() {
1004 if (kAlwaysProvideSwapFile) {
1005 return true;
1006 }
1007
1008 char low_mem_buf[PROPERTY_VALUE_MAX];
1009 property_get("ro.config.low_ram", low_mem_buf, "");
1010 return (strcmp(low_mem_buf, "true") == 0);
1011}
1012
Richard Uhler009b8772015-03-18 12:39:09 -07001013/*
1014 * Computes the odex file for the given apk_path and instruction_set.
1015 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
1016 *
1017 * Returns false if it failed to determine the odex file path.
1018 */
1019static bool calculate_odex_file_path(char path[PKG_PATH_MAX],
1020 const char *apk_path,
1021 const char *instruction_set)
1022{
1023 if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
1024 + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
1025 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
1026 return false;
1027 }
1028
1029 strcpy(path, apk_path);
1030 char *end = strrchr(path, '/');
1031 if (end == NULL) {
1032 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
1033 return false;
1034 }
1035 const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
1036
1037 strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0
1038 strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
1039 strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0
1040 end = strrchr(path, '.');
1041 if (end == NULL) {
1042 ALOGE("apk_path '%s' has no extension.\n", apk_path);
1043 return false;
1044 }
1045 strcpy(end + 1, "odex");
1046 return true;
1047}
1048
Calin Juravleb1efac12014-08-21 19:05:20 +01001049int dexopt(const char *apk_path, uid_t uid, bool is_public,
Richard Uhlerc92fb622015-03-26 15:47:38 -07001050 const char *pkgname, const char *instruction_set, int dexopt_needed,
1051 bool vm_safe_mode, bool debuggable, const char* oat_dir)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001052{
1053 struct utimbuf ut;
Fyodor Kupolov26ff93c2015-04-02 16:59:10 -07001054 struct stat input_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001055 char out_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -08001056 char swap_file_name[PKG_PATH_MAX];
Alex Light7365a102014-07-21 12:23:48 -07001057 const char *input_file;
1058 char in_odex_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -08001059 int res, input_fd=-1, out_fd=-1, swap_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001060
Andreas Gampee1c01352014-12-10 16:41:11 -08001061 // Early best-effort check whether we can fit the the path into our buffers.
1062 // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
1063 // without a swap file, if necessary.
Mike Lockwood94afecf2012-10-24 10:45:23 -07001064 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001065 ALOGE("apk_path too long '%s'\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001066 return -1;
1067 }
1068
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001069 if (oat_dir != NULL && oat_dir[0] != '!') {
1070 if (validate_apk_path(oat_dir)) {
1071 ALOGE("invalid oat_dir '%s'\n", oat_dir);
1072 return -1;
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +08001073 }
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001074 if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) {
1075 return -1;
1076 }
1077 } else {
1078 if (create_cache_path(out_path, apk_path, instruction_set)) {
1079 return -1;
1080 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001081 }
1082
Richard Uhlerc92fb622015-03-26 15:47:38 -07001083 switch (dexopt_needed) {
1084 case DEXOPT_DEX2OAT_NEEDED:
1085 input_file = apk_path;
1086 break;
1087
1088 case DEXOPT_PATCHOAT_NEEDED:
1089 if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
1090 return -1;
1091 }
1092 input_file = in_odex_path;
1093 break;
1094
1095 case DEXOPT_SELF_PATCHOAT_NEEDED:
1096 input_file = out_path;
1097 break;
1098
1099 default:
1100 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1101 exit(72);
Alex Light7365a102014-07-21 12:23:48 -07001102 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001103
Alex Light7365a102014-07-21 12:23:48 -07001104 memset(&input_stat, 0, sizeof(input_stat));
1105 stat(input_file, &input_stat);
1106
1107 input_fd = open(input_file, O_RDONLY, 0);
1108 if (input_fd < 0) {
1109 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001110 return -1;
1111 }
1112
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001113 unlink(out_path);
1114 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1115 if (out_fd < 0) {
1116 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001117 goto fail;
1118 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001119 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -07001120 S_IRUSR|S_IWUSR|S_IRGRP |
1121 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001122 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001123 goto fail;
1124 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001125 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
1126 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001127 goto fail;
1128 }
1129
Dave Allisond9370732014-01-30 14:19:23 -08001130 // Create profile file if there is a package name present.
1131 if (strcmp(pkgname, "*") != 0) {
1132 create_profile_file(pkgname, uid);
1133 }
1134
Andreas Gampee1c01352014-12-10 16:41:11 -08001135 // Create a swap file if necessary.
Richard Uhlerc92fb622015-03-26 15:47:38 -07001136 if (ShouldUseSwapFileForDexopt()) {
Andreas Gampee1c01352014-12-10 16:41:11 -08001137 // Make sure there really is enough space.
1138 size_t out_len = strlen(out_path);
1139 if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
1140 strcpy(swap_file_name, out_path);
1141 strcpy(swap_file_name + strlen(out_path), ".swap");
1142 unlink(swap_file_name);
1143 swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600);
1144 if (swap_fd < 0) {
1145 // Could not create swap file. Optimistically go on and hope that we can compile
1146 // without it.
1147 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
1148 } else {
1149 // Immediately unlink. We don't really want to hit flash.
1150 unlink(swap_file_name);
1151 }
1152 } else {
1153 // Swap file path is too long. Try to run without.
1154 ALOGE("installd could not create swap file for path %s during dexopt\n", out_path);
1155 }
1156 }
Dave Allisond9370732014-01-30 14:19:23 -08001157
Alex Light7365a102014-07-21 12:23:48 -07001158 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001159
1160 pid_t pid;
1161 pid = fork();
1162 if (pid == 0) {
1163 /* child -- drop privileges before continuing */
1164 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001165 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001166 exit(64);
1167 }
1168 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001169 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001170 exit(65);
1171 }
1172 // drop capabilities
1173 struct __user_cap_header_struct capheader;
1174 struct __user_cap_data_struct capdata[2];
1175 memset(&capheader, 0, sizeof(capheader));
1176 memset(&capdata, 0, sizeof(capdata));
1177 capheader.version = _LINUX_CAPABILITY_VERSION_3;
1178 if (capset(&capheader, &capdata[0]) < 0) {
1179 ALOGE("capset failed: %s\n", strerror(errno));
1180 exit(66);
1181 }
Brian Carlstrom0378aaf2014-08-08 00:52:22 -07001182 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
1183 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
1184 exit(70);
1185 }
Igor Murashkin9e87a802014-11-05 15:21:12 -08001186 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
1187 ALOGE("setpriority failed: %s\n", strerror(errno));
1188 exit(71);
1189 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001190 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
1191 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001192 exit(67);
1193 }
1194
Richard Uhlerc92fb622015-03-26 15:47:38 -07001195 if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED
1196 || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) {
Andreas Gampebd872e42014-12-15 11:41:11 -08001197 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001198 } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001199 const char *input_file_name = strrchr(input_file, '/');
1200 if (input_file_name == NULL) {
1201 input_file_name = input_file;
1202 } else {
1203 input_file_name++;
1204 }
1205 run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname,
1206 instruction_set, vm_safe_mode, debuggable);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001207 } else {
1208 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1209 exit(73);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001210 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001211 exit(68); /* only get here on exec failure */
1212 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001213 res = wait_child(pid);
1214 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001215 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001216 } else {
Alex Light7365a102014-07-21 12:23:48 -07001217 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001218 goto fail;
1219 }
1220 }
1221
Alex Light7365a102014-07-21 12:23:48 -07001222 ut.actime = input_stat.st_atime;
1223 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001224 utime(out_path, &ut);
1225
1226 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001227 close(input_fd);
Andreas Gampee1c01352014-12-10 16:41:11 -08001228 if (swap_fd != -1) {
1229 close(swap_fd);
1230 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001231 return 0;
1232
1233fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001234 if (out_fd >= 0) {
1235 close(out_fd);
1236 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001237 }
Alex Light7365a102014-07-21 12:23:48 -07001238 if (input_fd >= 0) {
1239 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001240 }
1241 return -1;
1242}
1243
Narayan Kamath091ea772014-11-10 15:03:46 +00001244int mark_boot_complete(const char* instruction_set)
1245{
1246 char boot_marker_path[PKG_PATH_MAX];
1247 sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set);
1248
1249 ALOGV("mark_boot_complete : %s", boot_marker_path);
1250 if (unlink(boot_marker_path) != 0) {
1251 ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1252 strerror(errno));
1253 return -1;
1254 }
1255
1256 return 0;
1257}
1258
Mike Lockwood94afecf2012-10-24 10:45:23 -07001259void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1260 struct stat* statbuf)
1261{
1262 while (path[basepos] != 0) {
1263 if (path[basepos] == '/') {
1264 path[basepos] = 0;
1265 if (lstat(path, statbuf) < 0) {
1266 ALOGV("Making directory: %s\n", path);
1267 if (mkdir(path, mode) == 0) {
1268 chown(path, uid, gid);
1269 } else {
1270 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1271 }
1272 }
1273 path[basepos] = '/';
1274 basepos++;
1275 }
1276 basepos++;
1277 }
1278}
1279
1280int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1281 int dstuid, int dstgid, struct stat* statbuf)
1282{
1283 DIR *d;
1284 struct dirent *de;
1285 int res;
1286
1287 int srcend = strlen(srcpath);
1288 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001289
Mike Lockwood94afecf2012-10-24 10:45:23 -07001290 if (lstat(srcpath, statbuf) < 0) {
1291 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1292 return 1;
1293 }
Dave Allisond9370732014-01-30 14:19:23 -08001294
Mike Lockwood94afecf2012-10-24 10:45:23 -07001295 if ((statbuf->st_mode&S_IFDIR) == 0) {
1296 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1297 dstuid, dstgid, statbuf);
1298 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1299 if (rename(srcpath, dstpath) >= 0) {
1300 if (chown(dstpath, dstuid, dstgid) < 0) {
1301 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1302 unlink(dstpath);
1303 return 1;
1304 }
1305 } else {
1306 ALOGW("Unable to rename %s to %s: %s\n",
1307 srcpath, dstpath, strerror(errno));
1308 return 1;
1309 }
1310 return 0;
1311 }
1312
1313 d = opendir(srcpath);
1314 if (d == NULL) {
1315 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1316 return 1;
1317 }
1318
1319 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001320
Mike Lockwood94afecf2012-10-24 10:45:23 -07001321 while ((de = readdir(d))) {
1322 const char *name = de->d_name;
1323 /* always skip "." and ".." */
1324 if (name[0] == '.') {
1325 if (name[1] == 0) continue;
1326 if ((name[1] == '.') && (name[2] == 0)) continue;
1327 }
Dave Allisond9370732014-01-30 14:19:23 -08001328
Mike Lockwood94afecf2012-10-24 10:45:23 -07001329 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1330 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1331 continue;
1332 }
Dave Allisond9370732014-01-30 14:19:23 -08001333
Mike Lockwood94afecf2012-10-24 10:45:23 -07001334 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1335 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1336 continue;
1337 }
Dave Allisond9370732014-01-30 14:19:23 -08001338
Mike Lockwood94afecf2012-10-24 10:45:23 -07001339 srcpath[srcend] = dstpath[dstend] = '/';
1340 strcpy(srcpath+srcend+1, name);
1341 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001342
Mike Lockwood94afecf2012-10-24 10:45:23 -07001343 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1344 res = 1;
1345 }
Dave Allisond9370732014-01-30 14:19:23 -08001346
Mike Lockwood94afecf2012-10-24 10:45:23 -07001347 // Note: we will be leaving empty directories behind in srcpath,
1348 // but that is okay, the package manager will be erasing all of the
1349 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001350
Mike Lockwood94afecf2012-10-24 10:45:23 -07001351 srcpath[srcend] = dstpath[dstend] = 0;
1352 }
Dave Allisond9370732014-01-30 14:19:23 -08001353
Mike Lockwood94afecf2012-10-24 10:45:23 -07001354 closedir(d);
1355 return res;
1356}
1357
1358int movefiles()
1359{
1360 DIR *d;
1361 int dfd, subfd;
1362 struct dirent *de;
1363 struct stat s;
1364 char buf[PKG_PATH_MAX+1];
1365 int bufp, bufe, bufi, readlen;
1366
1367 char srcpkg[PKG_NAME_MAX];
1368 char dstpkg[PKG_NAME_MAX];
1369 char srcpath[PKG_PATH_MAX];
1370 char dstpath[PKG_PATH_MAX];
1371 int dstuid=-1, dstgid=-1;
1372 int hasspace;
1373
1374 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1375 if (d == NULL) {
1376 goto done;
1377 }
1378 dfd = dirfd(d);
1379
1380 /* Iterate through all files in the directory, executing the
1381 * file movements requested there-in.
1382 */
1383 while ((de = readdir(d))) {
1384 const char *name = de->d_name;
1385
1386 if (de->d_type == DT_DIR) {
1387 continue;
1388 } else {
1389 subfd = openat(dfd, name, O_RDONLY);
1390 if (subfd < 0) {
1391 ALOGW("Unable to open update commands at %s%s\n",
1392 UPDATE_COMMANDS_DIR_PREFIX, name);
1393 continue;
1394 }
Dave Allisond9370732014-01-30 14:19:23 -08001395
Mike Lockwood94afecf2012-10-24 10:45:23 -07001396 bufp = 0;
1397 bufe = 0;
1398 buf[PKG_PATH_MAX] = 0;
1399 srcpkg[0] = dstpkg[0] = 0;
1400 while (1) {
1401 bufi = bufp;
1402 while (bufi < bufe && buf[bufi] != '\n') {
1403 bufi++;
1404 }
1405 if (bufi < bufe) {
1406 buf[bufi] = 0;
1407 ALOGV("Processing line: %s\n", buf+bufp);
1408 hasspace = 0;
1409 while (bufp < bufi && isspace(buf[bufp])) {
1410 hasspace = 1;
1411 bufp++;
1412 }
1413 if (buf[bufp] == '#' || bufp == bufi) {
1414 // skip comments and empty lines.
1415 } else if (hasspace) {
1416 if (dstpkg[0] == 0) {
1417 ALOGW("Path before package line in %s%s: %s\n",
1418 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1419 } else if (srcpkg[0] == 0) {
1420 // Skip -- source package no longer exists.
1421 } else {
1422 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1423 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1424 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1425 movefileordir(srcpath, dstpath,
1426 strlen(dstpath)-strlen(buf+bufp),
1427 dstuid, dstgid, &s);
1428 }
1429 }
1430 } else {
1431 char* div = strchr(buf+bufp, ':');
1432 if (div == NULL) {
1433 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1434 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1435 } else {
1436 *div = 0;
1437 div++;
1438 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1439 strcpy(dstpkg, buf+bufp);
1440 } else {
1441 srcpkg[0] = dstpkg[0] = 0;
1442 ALOGW("Package name too long in %s%s: %s\n",
1443 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1444 }
1445 if (strlen(div) < PKG_NAME_MAX) {
1446 strcpy(srcpkg, div);
1447 } else {
1448 srcpkg[0] = dstpkg[0] = 0;
1449 ALOGW("Package name too long in %s%s: %s\n",
1450 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1451 }
1452 if (srcpkg[0] != 0) {
1453 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1454 if (lstat(srcpath, &s) < 0) {
1455 // Package no longer exists -- skip.
1456 srcpkg[0] = 0;
1457 }
1458 } else {
1459 srcpkg[0] = 0;
1460 ALOGW("Can't create path %s in %s%s\n",
1461 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1462 }
1463 if (srcpkg[0] != 0) {
1464 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1465 if (lstat(dstpath, &s) == 0) {
1466 dstuid = s.st_uid;
1467 dstgid = s.st_gid;
1468 } else {
1469 // Destination package doesn't
1470 // exist... due to original-package,
1471 // this is normal, so don't be
1472 // noisy about it.
1473 srcpkg[0] = 0;
1474 }
1475 } else {
1476 srcpkg[0] = 0;
1477 ALOGW("Can't create path %s in %s%s\n",
1478 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1479 }
1480 }
1481 ALOGV("Transfering from %s to %s: uid=%d\n",
1482 srcpkg, dstpkg, dstuid);
1483 }
1484 }
1485 }
1486 bufp = bufi+1;
1487 } else {
1488 if (bufp == 0) {
1489 if (bufp < bufe) {
1490 ALOGW("Line too long in %s%s, skipping: %s\n",
1491 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1492 }
1493 } else if (bufp < bufe) {
1494 memcpy(buf, buf+bufp, bufe-bufp);
1495 bufe -= bufp;
1496 bufp = 0;
1497 }
1498 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1499 if (readlen < 0) {
1500 ALOGW("Failure reading update commands in %s%s: %s\n",
1501 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1502 break;
1503 } else if (readlen == 0) {
1504 break;
1505 }
1506 bufe += readlen;
1507 buf[bufe] = 0;
1508 ALOGV("Read buf: %s\n", buf);
1509 }
1510 }
1511 close(subfd);
1512 }
1513 }
1514 closedir(d);
1515done:
1516 return 0;
1517}
1518
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001519int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001520{
Mike Lockwood94afecf2012-10-24 10:45:23 -07001521 struct stat s, libStat;
1522 int rc = 0;
1523
Jeff Sharkeyd7921182015-04-30 15:58:19 -07001524 std::string _pkgdir(create_data_user_package_path(uuid, userId, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001525 std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
1526
1527 const char* pkgdir = _pkgdir.c_str();
1528 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001529
1530 if (stat(pkgdir, &s) < 0) return -1;
1531
1532 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1533 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1534 return -1;
1535 }
1536
1537 if (chmod(pkgdir, 0700) < 0) {
1538 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1539 rc = -1;
1540 goto out;
1541 }
1542
1543 if (lstat(libsymlink, &libStat) < 0) {
1544 if (errno != ENOENT) {
1545 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1546 rc = -1;
1547 goto out;
1548 }
1549 } else {
1550 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001551 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001552 rc = -1;
1553 goto out;
1554 }
1555 } else if (S_ISLNK(libStat.st_mode)) {
1556 if (unlink(libsymlink) < 0) {
1557 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1558 rc = -1;
1559 goto out;
1560 }
1561 }
1562 }
1563
1564 if (symlink(asecLibDir, libsymlink) < 0) {
1565 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1566 strerror(errno));
1567 rc = -errno;
1568 goto out;
1569 }
1570
1571out:
1572 if (chmod(pkgdir, s.st_mode) < 0) {
1573 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1574 rc = -errno;
1575 }
1576
1577 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1578 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1579 return -errno;
1580 }
1581
1582 return rc;
1583}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001584
1585static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1586{
1587 static const char *IDMAP_BIN = "/system/bin/idmap";
1588 static const size_t MAX_INT_LEN = 32;
1589 char idmap_str[MAX_INT_LEN];
1590
1591 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1592
1593 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1594 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1595}
1596
1597// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1598// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1599static int flatten_path(const char *prefix, const char *suffix,
1600 const char *overlay_path, char *idmap_path, size_t N)
1601{
1602 if (overlay_path == NULL || idmap_path == NULL) {
1603 return -1;
1604 }
1605 const size_t len_overlay_path = strlen(overlay_path);
1606 // will access overlay_path + 1 further below; requires absolute path
1607 if (len_overlay_path < 2 || *overlay_path != '/') {
1608 return -1;
1609 }
1610 const size_t len_idmap_root = strlen(prefix);
1611 const size_t len_suffix = strlen(suffix);
1612 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1613 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1614 // additions below would cause overflow
1615 return -1;
1616 }
1617 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1618 return -1;
1619 }
1620 memset(idmap_path, 0, N);
1621 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1622 char *ch = idmap_path + len_idmap_root;
1623 while (*ch != '\0') {
1624 if (*ch == '/') {
1625 *ch = '@';
1626 }
1627 ++ch;
1628 }
1629 return 0;
1630}
1631
1632int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1633{
1634 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1635
1636 int idmap_fd = -1;
1637 char idmap_path[PATH_MAX];
1638
1639 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1640 idmap_path, sizeof(idmap_path)) == -1) {
1641 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1642 goto fail;
1643 }
1644
1645 unlink(idmap_path);
1646 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1647 if (idmap_fd < 0) {
1648 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1649 goto fail;
1650 }
1651 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1652 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1653 goto fail;
1654 }
1655 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1656 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1657 goto fail;
1658 }
1659
1660 pid_t pid;
1661 pid = fork();
1662 if (pid == 0) {
1663 /* child -- drop privileges before continuing */
1664 if (setgid(uid) != 0) {
1665 ALOGE("setgid(%d) failed during idmap\n", uid);
1666 exit(1);
1667 }
1668 if (setuid(uid) != 0) {
1669 ALOGE("setuid(%d) failed during idmap\n", uid);
1670 exit(1);
1671 }
1672 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1673 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1674 exit(1);
1675 }
1676
1677 run_idmap(target_apk, overlay_apk, idmap_fd);
1678 exit(1); /* only if exec call to idmap failed */
1679 } else {
1680 int status = wait_child(pid);
1681 if (status != 0) {
1682 ALOGE("idmap failed, status=0x%04x\n", status);
1683 goto fail;
1684 }
1685 }
1686
1687 close(idmap_fd);
1688 return 0;
1689fail:
1690 if (idmap_fd >= 0) {
1691 close(idmap_fd);
1692 unlink(idmap_path);
1693 }
1694 return -1;
1695}
Robert Craige9887e42014-02-20 10:25:56 -05001696
Jeff Sharkey6fe28a02015-04-09 13:10:03 -07001697int restorecon_data(const char* uuid, const char* pkgName,
Andreas Gampe0ad7a112015-04-09 09:52:45 -07001698 const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001699{
Robert Craigda30dc72014-03-27 10:21:12 -04001700 struct dirent *entry;
1701 DIR *d;
1702 struct stat s;
Robert Craige9887e42014-02-20 10:25:56 -05001703 int ret = 0;
1704
Robert Craigda30dc72014-03-27 10:21:12 -04001705 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1706 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1707
1708 if (!pkgName || !seinfo) {
1709 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001710 return -1;
1711 }
1712
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001713 // Special case for owner on internal storage
1714 if (uuid == nullptr) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -07001715 std::string path(create_data_user_package_path(nullptr, 0, pkgName));
Robert Craigda30dc72014-03-27 10:21:12 -04001716
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001717 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, flags) < 0) {
1718 PLOG(ERROR) << "restorecon failed for " << path;
1719 ret |= -1;
1720 }
Robert Craige9887e42014-02-20 10:25:56 -05001721 }
1722
Robert Craigda30dc72014-03-27 10:21:12 -04001723 // Relabel package directory for all secondary users.
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001724 std::string userdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX);
1725 d = opendir(userdir.c_str());
Robert Craigda30dc72014-03-27 10:21:12 -04001726 if (d == NULL) {
Robert Craigda30dc72014-03-27 10:21:12 -04001727 return -1;
1728 }
1729
1730 while ((entry = readdir(d))) {
1731 if (entry->d_type != DT_DIR) {
1732 continue;
1733 }
1734
1735 const char *user = entry->d_name;
1736 // Ignore "." and ".."
1737 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1738 continue;
1739 }
1740
1741 // user directories start with a number
1742 if (user[0] < '0' || user[0] > '9') {
1743 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1744 continue;
1745 }
1746
Jeff Sharkeye3637242015-04-08 20:56:42 -07001747 std::string pkgdir(StringPrintf("%s%s/%s", userdir.c_str(), user, pkgName));
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001748 if (stat(pkgdir.c_str(), &s) < 0) {
Robert Craigda30dc72014-03-27 10:21:12 -04001749 continue;
1750 }
1751
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001752 if (selinux_android_restorecon_pkgdir(pkgdir.c_str(), seinfo, s.st_uid, flags) < 0) {
1753 PLOG(ERROR) << "restorecon failed for " << pkgdir;
Robert Craigda30dc72014-03-27 10:21:12 -04001754 ret |= -1;
1755 }
Robert Craigda30dc72014-03-27 10:21:12 -04001756 }
1757
1758 closedir(d);
Robert Craige9887e42014-02-20 10:25:56 -05001759 return ret;
1760}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001761
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001762int create_oat_dir(const char* oat_dir, const char* instruction_set)
1763{
1764 char oat_instr_dir[PKG_PATH_MAX];
1765
1766 if (validate_apk_path(oat_dir)) {
1767 ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
1768 return -1;
1769 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001770 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001771 return -1;
1772 }
1773 if (selinux_android_restorecon(oat_dir, 0)) {
1774 ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
1775 return -1;
1776 }
1777 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001778 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001779 return -1;
1780 }
1781 return 0;
1782}
1783
1784int rm_package_dir(const char* apk_path)
1785{
1786 if (validate_apk_path(apk_path)) {
1787 ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
1788 return -1;
1789 }
1790 return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */);
1791}
1792
Narayan Kamathd845c962015-06-04 13:20:27 +01001793int link_file(const char* relative_path, const char* from_base, const char* to_base) {
1794 char from_path[PKG_PATH_MAX];
1795 char to_path[PKG_PATH_MAX];
1796 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
1797 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
1798
1799 if (validate_apk_path_subdirs(from_path)) {
1800 ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path);
1801 return -1;
1802 }
1803
1804 if (validate_apk_path_subdirs(to_path)) {
1805 ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path);
1806 return -1;
1807 }
1808
1809 const int ret = link(from_path, to_path);
1810 if (ret < 0) {
1811 ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno));
1812 return -1;
1813 }
1814
1815 return 0;
1816}
1817
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001818int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
1819 const char *instruction_set) {
1820 char *file_name_start;
1821 char *file_name_end;
1822
1823 file_name_start = strrchr(apk_path, '/');
1824 if (file_name_start == NULL) {
1825 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
1826 return -1;
1827 }
1828 file_name_end = strrchr(apk_path, '.');
1829 if (file_name_end < file_name_start) {
1830 ALOGE("apk_path '%s' has no extension\n", apk_path);
1831 return -1;
1832 }
1833
1834 // Calculate file_name
1835 int file_name_len = file_name_end - file_name_start - 1;
1836 char file_name[file_name_len + 1];
1837 memcpy(file_name, file_name_start + 1, file_name_len);
1838 file_name[file_name_len] = '\0';
1839
1840 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
1841 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
1842 return 0;
1843}