Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2008, The Android Open Source Project |
| 3 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 4 | ** 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 10 | ** 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 17 | #include "installd.h" |
| 18 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame^] | 19 | #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 Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 27 | #include <inttypes.h> |
Nick Kralevich | d747129 | 2013-02-28 16:59:13 -0800 | [diff] [blame] | 28 | #include <sys/capability.h> |
Elliott Hughes | 2ead70c | 2015-02-16 10:44:22 -0800 | [diff] [blame] | 29 | #include <sys/file.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame^] | 30 | #include <unistd.h> |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 31 | |
| 32 | using android::base::StringPrintf; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 33 | |
| 34 | /* Directory records that are used in execution of commands. */ |
| 35 | dir_rec_t android_data_dir; |
| 36 | dir_rec_t android_asec_dir; |
| 37 | dir_rec_t android_app_dir; |
| 38 | dir_rec_t android_app_private_dir; |
| 39 | dir_rec_t android_app_lib_dir; |
| 40 | dir_rec_t android_media_dir; |
Jeff Sharkey | e23a132 | 2015-04-06 16:19:39 -0700 | [diff] [blame] | 41 | dir_rec_t android_mnt_expand_dir; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 42 | dir_rec_array_t android_system_dirs; |
| 43 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame^] | 44 | static const char* kCpPath = "/system/bin/cp"; |
| 45 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 46 | int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const char *seinfo) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 47 | { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 48 | if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { |
| 49 | ALOGE("invalid uid/gid: %d %d\n", uid, gid); |
| 50 | return -1; |
| 51 | } |
| 52 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 53 | std::string _pkgdir(create_package_data_path(uuid, pkgname, 0)); |
| 54 | const char* pkgdir = _pkgdir.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 55 | |
Nick Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 56 | if (mkdir(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 57 | ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); |
| 58 | return -1; |
| 59 | } |
Nick Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 60 | if (chmod(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 61 | ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); |
| 62 | unlink(pkgdir); |
| 63 | return -1; |
| 64 | } |
| 65 | |
Stephen Smalley | 2628820 | 2014-02-07 09:16:46 -0500 | [diff] [blame] | 66 | if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 67 | ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 68 | 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 74 | unlink(pkgdir); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 81 | int uninstall(const char *uuid, const char *pkgname, userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 82 | { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 83 | std::string _pkgdir(create_package_data_path(uuid, pkgname, userid)); |
| 84 | const char* pkgdir = _pkgdir.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 85 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 86 | remove_profile_file(pkgname); |
| 87 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 88 | /* delete contents AND directory, no exceptions */ |
| 89 | return delete_dir_contents(pkgdir, 1, NULL); |
| 90 | } |
| 91 | |
| 92 | int 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 Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 109 | int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 110 | { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 111 | struct stat s; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 112 | |
| 113 | if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { |
| 114 | ALOGE("invalid uid/gid: %d %d\n", uid, gid); |
| 115 | return -1; |
| 116 | } |
| 117 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 118 | std::string _pkgdir(create_package_data_path(uuid, pkgname, 0)); |
| 119 | const char* pkgdir = _pkgdir.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 120 | |
| 121 | if (stat(pkgdir, &s) < 0) return -1; |
| 122 | |
| 123 | if (s.st_uid != 0 || s.st_gid != 0) { |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 124 | ALOGE("fixing uid of non-root pkg: %s %" PRIu32 " %" PRIu32 "\n", pkgdir, s.st_uid, s.st_gid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 125 | 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 Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 142 | int delete_user_data(const char *uuid, const char *pkgname, userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 143 | { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 144 | std::string _pkgdir(create_package_data_path(uuid, pkgname, userid)); |
| 145 | const char* pkgdir = _pkgdir.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 146 | |
Jeff Sharkey | 3316fe4 | 2014-08-27 10:46:25 -0700 | [diff] [blame] | 147 | return delete_dir_contents(pkgdir, 0, NULL); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 150 | int make_user_data(const char *uuid, const char *pkgname, uid_t uid, userid_t userid, const char* seinfo) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 151 | { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 152 | std::string _pkgdir(create_package_data_path(uuid, pkgname, userid)); |
| 153 | const char* pkgdir = _pkgdir.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 154 | |
Nick Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 155 | if (mkdir(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 156 | ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); |
| 157 | return -errno; |
| 158 | } |
Nick Kralevich | a2d838a | 2013-01-09 16:00:35 -0800 | [diff] [blame] | 159 | if (chmod(pkgdir, 0751) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 160 | ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); |
| 161 | unlink(pkgdir); |
| 162 | return -errno; |
| 163 | } |
| 164 | |
Stephen Smalley | 2628820 | 2014-02-07 09:16:46 -0500 | [diff] [blame] | 165 | if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 166 | ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 167 | 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 173 | unlink(pkgdir); |
| 174 | return -errno; |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame^] | 180 | int move_user_data(const char *from_uuid, const char *to_uuid, |
| 181 | const char *package_name, appid_t appid, const char* seinfo) { |
| 182 | std::vector<userid_t> users = get_known_users(from_uuid); |
| 183 | |
| 184 | // Copy package private data for all known users |
| 185 | for (auto user : users) { |
| 186 | std::string from(create_package_data_path(from_uuid, package_name, user)); |
| 187 | std::string to(create_package_data_path(to_uuid, package_name, user)); |
| 188 | std::string to_user(create_data_user_path(to_uuid, user)); |
| 189 | |
| 190 | // Data source may not exist for all users; that's okay |
| 191 | if (access(from.c_str(), F_OK) != 0) { |
| 192 | LOG(INFO) << "Missing source " << from; |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | std::string user_path(create_data_user_path(to_uuid, user)); |
| 197 | if (fs_prepare_dir(user_path.c_str(), 0771, AID_SYSTEM, AID_SYSTEM) != 0) { |
| 198 | LOG(ERROR) << "Failed to prepare user target " << user_path; |
| 199 | goto fail; |
| 200 | } |
| 201 | |
| 202 | uid_t uid = multiuser_get_uid(user, appid); |
| 203 | if (make_user_data(to_uuid, package_name, uid, user, seinfo) != 0) { |
| 204 | LOG(ERROR) << "Failed to create package target " << to; |
| 205 | goto fail; |
| 206 | } |
| 207 | |
| 208 | char *argv[] = { |
| 209 | (char*) kCpPath, |
| 210 | (char*) "-F", /* delete any existing destination file first (--remove-destination) */ |
| 211 | (char*) "-p", /* preserve timestamps, ownership, and permissions */ |
| 212 | (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ |
| 213 | (char*) "-P", /* Do not follow symlinks [default] */ |
| 214 | (char*) "-d", /* don't dereference symlinks */ |
| 215 | (char*) from.c_str(), |
| 216 | (char*) to_user.c_str() |
| 217 | }; |
| 218 | |
| 219 | LOG(DEBUG) << "Copying " << from << " to " << to; |
| 220 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); |
| 221 | |
| 222 | if (rc != 0) { |
| 223 | LOG(ERROR) << "Failed copying " << from << " to " << to |
| 224 | << ": status " << rc; |
| 225 | goto fail; |
| 226 | } |
| 227 | |
| 228 | if (restorecon_data(to_uuid, package_name, seinfo, uid) != 0) { |
| 229 | LOG(ERROR) << "Failed to restorecon " << to; |
| 230 | goto fail; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Copy successful, so delete old data |
| 235 | for (auto user : users) { |
| 236 | std::string from(create_package_data_path(from_uuid, package_name, user)); |
| 237 | if (delete_dir_contents(from.c_str(), 1, NULL) != 0) { |
| 238 | LOG(WARNING) << "Failed to delete " << from; |
| 239 | } |
| 240 | } |
| 241 | return 0; |
| 242 | |
| 243 | fail: |
| 244 | // Nuke everything we might have already copied |
| 245 | for (auto user : users) { |
| 246 | std::string to(create_package_data_path(to_uuid, package_name, user)); |
| 247 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { |
| 248 | LOG(WARNING) << "Failed to rollback " << to; |
| 249 | } |
| 250 | } |
| 251 | return -1; |
| 252 | } |
| 253 | |
Robin Lee | 7c8bec0 | 2014-06-10 18:46:26 +0100 | [diff] [blame] | 254 | int make_user_config(userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 255 | { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 256 | if (ensure_config_user_dirs(userid) == -1) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 257 | return -1; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 263 | int delete_user(const char *uuid, userid_t userid) |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 264 | { |
| 265 | int status = 0; |
| 266 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 267 | std::string data_path(create_data_user_path(uuid, userid)); |
| 268 | if (delete_dir_contents(data_path.c_str(), 1, NULL) != 0) { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 269 | status = -1; |
| 270 | } |
| 271 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 272 | std::string media_path(create_data_media_path(uuid, userid)); |
| 273 | if (delete_dir_contents(media_path.c_str(), 1, NULL) != 0) { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 274 | status = -1; |
| 275 | } |
| 276 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 277 | // Config paths only exist on internal storage |
| 278 | if (uuid == nullptr) { |
| 279 | char config_path[PATH_MAX]; |
| 280 | if ((create_user_config_path(config_path, userid) != 0) |
| 281 | || (delete_dir_contents(config_path, 1, NULL) != 0)) { |
| 282 | status = -1; |
| 283 | } |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return status; |
| 287 | } |
| 288 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 289 | int delete_cache(const char *uuid, const char *pkgname, userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 290 | { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 291 | std::string _cachedir( |
| 292 | create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX); |
| 293 | const char* cachedir = _cachedir.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 294 | |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 295 | /* delete contents, not the directory, no exceptions */ |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 296 | return delete_dir_contents(cachedir, 0, NULL); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 299 | int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid) |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 300 | { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 301 | std::string _codecachedir( |
| 302 | create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX); |
| 303 | const char* codecachedir = _codecachedir.c_str(); |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 304 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 305 | struct stat s; |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 306 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 307 | /* it's okay if code cache is missing */ |
| 308 | if (lstat(codecachedir, &s) == -1 && errno == ENOENT) { |
| 309 | return 0; |
| 310 | } |
| 311 | |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 312 | /* delete contents, not the directory, no exceptions */ |
| 313 | return delete_dir_contents(codecachedir, 0, NULL); |
| 314 | } |
| 315 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 316 | /* Try to ensure free_size bytes of storage are available. |
| 317 | * Returns 0 on success. |
| 318 | * This is rather simple-minded because doing a full LRU would |
| 319 | * be potentially memory-intensive, and without atime it would |
| 320 | * also require that apps constantly modify file metadata even |
| 321 | * when just reading from the cache, which is pretty awful. |
| 322 | */ |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 323 | int free_cache(const char *uuid, int64_t free_size) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 324 | { |
| 325 | cache_t* cache; |
| 326 | int64_t avail; |
| 327 | DIR *d; |
| 328 | struct dirent *de; |
| 329 | char tmpdir[PATH_MAX]; |
| 330 | char *dirpos; |
| 331 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 332 | std::string data_path(create_data_path(uuid)); |
| 333 | |
| 334 | avail = data_disk_free(data_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 335 | if (avail < 0) return -1; |
| 336 | |
| 337 | ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail); |
| 338 | if (avail >= free_size) return 0; |
| 339 | |
| 340 | cache = start_cache_collection(); |
| 341 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 342 | // Special case for owner on internal storage |
| 343 | if (uuid == nullptr) { |
| 344 | std::string _tmpdir(create_data_user_path(nullptr, 0)); |
| 345 | add_cache_files(cache, _tmpdir.c_str(), "cache"); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // Search for other users and add any cache files from them. |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 349 | std::string _tmpdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX); |
| 350 | strcpy(tmpdir, _tmpdir.c_str()); |
| 351 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 352 | dirpos = tmpdir + strlen(tmpdir); |
| 353 | d = opendir(tmpdir); |
| 354 | if (d != NULL) { |
| 355 | while ((de = readdir(d))) { |
| 356 | if (de->d_type == DT_DIR) { |
| 357 | const char *name = de->d_name; |
| 358 | /* always skip "." and ".." */ |
| 359 | if (name[0] == '.') { |
| 360 | if (name[1] == 0) continue; |
| 361 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 362 | } |
| 363 | if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) { |
| 364 | strcpy(dirpos, name); |
| 365 | //ALOGI("adding cache files from %s\n", tmpdir); |
| 366 | add_cache_files(cache, tmpdir, "cache"); |
| 367 | } else { |
| 368 | ALOGW("Path exceeds limit: %s%s", tmpdir, name); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | closedir(d); |
| 373 | } |
| 374 | |
| 375 | // Collect cache files on external storage for all users (if it is mounted as part |
| 376 | // of the internal storage). |
| 377 | strcpy(tmpdir, android_media_dir.path); |
| 378 | dirpos = tmpdir + strlen(tmpdir); |
| 379 | d = opendir(tmpdir); |
| 380 | if (d != NULL) { |
| 381 | while ((de = readdir(d))) { |
| 382 | if (de->d_type == DT_DIR) { |
| 383 | const char *name = de->d_name; |
| 384 | /* skip any dir that doesn't start with a number, so not a user */ |
| 385 | if (name[0] < '0' || name[0] > '9') { |
| 386 | continue; |
| 387 | } |
| 388 | if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) { |
| 389 | strcpy(dirpos, name); |
| 390 | if (lookup_media_dir(tmpdir, "Android") == 0 |
| 391 | && lookup_media_dir(tmpdir, "data") == 0) { |
| 392 | //ALOGI("adding cache files from %s\n", tmpdir); |
| 393 | add_cache_files(cache, tmpdir, "cache"); |
| 394 | } |
| 395 | } else { |
| 396 | ALOGW("Path exceeds limit: %s%s", tmpdir, name); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | closedir(d); |
| 401 | } |
| 402 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 403 | clear_cache_files(data_path, cache, free_size); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 404 | finish_cache_collection(cache); |
| 405 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 406 | return data_disk_free(data_path) >= free_size ? 0 : -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 409 | int move_dex(const char *src, const char *dst, const char *instruction_set) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 410 | { |
| 411 | char src_dex[PKG_PATH_MAX]; |
| 412 | char dst_dex[PKG_PATH_MAX]; |
| 413 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 414 | if (validate_apk_path(src)) { |
| 415 | ALOGE("invalid apk path '%s' (bad prefix)\n", src); |
| 416 | return -1; |
| 417 | } |
| 418 | if (validate_apk_path(dst)) { |
| 419 | ALOGE("invalid apk path '%s' (bad prefix)\n", dst); |
| 420 | return -1; |
| 421 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 422 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 423 | if (create_cache_path(src_dex, src, instruction_set)) return -1; |
| 424 | if (create_cache_path(dst_dex, dst, instruction_set)) return -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 425 | |
| 426 | ALOGV("move %s -> %s\n", src_dex, dst_dex); |
| 427 | if (rename(src_dex, dst_dex) < 0) { |
| 428 | ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno)); |
| 429 | return -1; |
| 430 | } else { |
| 431 | return 0; |
| 432 | } |
| 433 | } |
| 434 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 435 | int rm_dex(const char *path, const char *instruction_set) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 436 | { |
| 437 | char dex_path[PKG_PATH_MAX]; |
| 438 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 439 | if (validate_apk_path(path) && validate_system_app_path(path)) { |
| 440 | ALOGE("invalid apk path '%s' (bad prefix)\n", path); |
| 441 | return -1; |
| 442 | } |
| 443 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 444 | if (create_cache_path(dex_path, path, instruction_set)) return -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 445 | |
| 446 | ALOGV("unlink %s\n", dex_path); |
| 447 | if (unlink(dex_path) < 0) { |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 448 | if (errno != ENOENT) { |
| 449 | ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno)); |
| 450 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 451 | return -1; |
| 452 | } else { |
| 453 | return 0; |
| 454 | } |
| 455 | } |
| 456 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 457 | int get_size(const char *uuid, const char *pkgname, userid_t userid, const char *apkpath, |
Dianne Hackborn | 8b41780 | 2013-05-01 18:55:10 -0700 | [diff] [blame] | 458 | const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 459 | const char *instruction_set, int64_t *_codesize, int64_t *_datasize, |
| 460 | int64_t *_cachesize, int64_t* _asecsize) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 461 | { |
| 462 | DIR *d; |
| 463 | int dfd; |
| 464 | struct dirent *de; |
| 465 | struct stat s; |
| 466 | char path[PKG_PATH_MAX]; |
| 467 | |
| 468 | int64_t codesize = 0; |
| 469 | int64_t datasize = 0; |
| 470 | int64_t cachesize = 0; |
| 471 | int64_t asecsize = 0; |
| 472 | |
| 473 | /* count the source apk as code -- but only if it's not |
| 474 | * on the /system partition and its not on the sdcard. |
| 475 | */ |
| 476 | if (validate_system_app_path(apkpath) && |
| 477 | strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) { |
| 478 | if (stat(apkpath, &s) == 0) { |
| 479 | codesize += stat_size(&s); |
| 480 | } |
| 481 | } |
| 482 | /* count the forward locked apk as code if it is given |
| 483 | */ |
| 484 | if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') { |
| 485 | if (stat(fwdlock_apkpath, &s) == 0) { |
| 486 | codesize += stat_size(&s); |
| 487 | } |
| 488 | } |
| 489 | /* count the cached dexfile as code */ |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 490 | if (!create_cache_path(path, apkpath, instruction_set)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 491 | if (stat(path, &s) == 0) { |
| 492 | codesize += stat_size(&s); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /* add in size of any libraries */ |
Dianne Hackborn | 8b41780 | 2013-05-01 18:55:10 -0700 | [diff] [blame] | 497 | if (libdirpath != NULL && libdirpath[0] != '!') { |
| 498 | d = opendir(libdirpath); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 499 | if (d != NULL) { |
| 500 | dfd = dirfd(d); |
| 501 | codesize += calculate_dir_size(dfd); |
| 502 | closedir(d); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* compute asec size if it is given |
| 507 | */ |
| 508 | if (asecpath != NULL && asecpath[0] != '!') { |
| 509 | if (stat(asecpath, &s) == 0) { |
| 510 | asecsize += stat_size(&s); |
| 511 | } |
| 512 | } |
| 513 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 514 | std::string _pkgdir(create_package_data_path(uuid, pkgname, userid)); |
| 515 | const char* pkgdir = _pkgdir.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 516 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 517 | d = opendir(pkgdir); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 518 | if (d == NULL) { |
| 519 | goto done; |
| 520 | } |
| 521 | dfd = dirfd(d); |
| 522 | |
| 523 | /* most stuff in the pkgdir is data, except for the "cache" |
| 524 | * directory and below, which is cache, and the "lib" directory |
| 525 | * and below, which is code... |
| 526 | */ |
| 527 | while ((de = readdir(d))) { |
| 528 | const char *name = de->d_name; |
| 529 | |
| 530 | if (de->d_type == DT_DIR) { |
| 531 | int subfd; |
| 532 | int64_t statsize = 0; |
| 533 | int64_t dirsize = 0; |
| 534 | /* always skip "." and ".." */ |
| 535 | if (name[0] == '.') { |
| 536 | if (name[1] == 0) continue; |
| 537 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 538 | } |
| 539 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 540 | statsize = stat_size(&s); |
| 541 | } |
| 542 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); |
| 543 | if (subfd >= 0) { |
| 544 | dirsize = calculate_dir_size(subfd); |
| 545 | } |
| 546 | if(!strcmp(name,"lib")) { |
| 547 | codesize += dirsize + statsize; |
| 548 | } else if(!strcmp(name,"cache")) { |
| 549 | cachesize += dirsize + statsize; |
| 550 | } else { |
| 551 | datasize += dirsize + statsize; |
| 552 | } |
| 553 | } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) { |
| 554 | // This is the symbolic link to the application's library |
| 555 | // code. We'll count this as code instead of data, since |
| 556 | // it is not something that the app creates. |
| 557 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 558 | codesize += stat_size(&s); |
| 559 | } |
| 560 | } else { |
| 561 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 562 | datasize += stat_size(&s); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | closedir(d); |
| 567 | done: |
| 568 | *_codesize = codesize; |
| 569 | *_datasize = datasize; |
| 570 | *_cachesize = cachesize; |
| 571 | *_asecsize = asecsize; |
| 572 | return 0; |
| 573 | } |
| 574 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 575 | int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 576 | { |
| 577 | char *tmp; |
| 578 | int srclen; |
| 579 | int dstlen; |
| 580 | |
| 581 | srclen = strlen(src); |
| 582 | |
| 583 | /* demand that we are an absolute path */ |
| 584 | if ((src == 0) || (src[0] != '/') || strstr(src,"..")) { |
| 585 | return -1; |
| 586 | } |
| 587 | |
| 588 | if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX? |
| 589 | return -1; |
| 590 | } |
| 591 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 592 | dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) + |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 593 | strlen(instruction_set) + |
| 594 | strlen(DALVIK_CACHE_POSTFIX) + 2; |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 595 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 596 | if (dstlen > PKG_PATH_MAX) { |
| 597 | return -1; |
| 598 | } |
| 599 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 600 | sprintf(path,"%s%s/%s%s", |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 601 | DALVIK_CACHE_PREFIX, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 602 | instruction_set, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 603 | src + 1, /* skip the leading / */ |
| 604 | DALVIK_CACHE_POSTFIX); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 605 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 606 | for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 607 | if (*tmp == '/') { |
| 608 | *tmp = '@'; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 615 | static int split_count(const char *str) |
| 616 | { |
| 617 | char *ctx; |
| 618 | int count = 0; |
| 619 | char buf[PROPERTY_VALUE_MAX]; |
| 620 | |
| 621 | strncpy(buf, str, sizeof(buf)); |
| 622 | char *pBuf = buf; |
| 623 | |
| 624 | while(strtok_r(pBuf, " ", &ctx) != NULL) { |
| 625 | count++; |
| 626 | pBuf = NULL; |
| 627 | } |
| 628 | |
| 629 | return count; |
| 630 | } |
| 631 | |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 632 | static int split(char *buf, const char **argv) |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 633 | { |
| 634 | char *ctx; |
| 635 | int count = 0; |
| 636 | char *tok; |
| 637 | char *pBuf = buf; |
| 638 | |
| 639 | while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) { |
| 640 | argv[count++] = tok; |
| 641 | pBuf = NULL; |
| 642 | } |
| 643 | |
| 644 | return count; |
| 645 | } |
| 646 | |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 647 | static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name, |
Chih-Hung Hsieh | 99d9fb1 | 2014-09-11 14:44:46 -0700 | [diff] [blame] | 648 | const char* output_file_name, const char *pkgname __unused, const char *instruction_set) |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 649 | { |
| 650 | static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 651 | static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 652 | |
| 653 | static const char* PATCHOAT_BIN = "/system/bin/patchoat"; |
| 654 | if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) { |
| 655 | ALOGE("Instruction set %s longer than max length of %d", |
| 656 | instruction_set, MAX_INSTRUCTION_SET_LEN); |
| 657 | return; |
| 658 | } |
| 659 | |
| 660 | /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/ |
| 661 | char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN]; |
| 662 | char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN]; |
| 663 | char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN]; |
| 664 | const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art"; |
| 665 | // The caller has already gotten all the locks we need. |
| 666 | const char* no_lock_arg = "--no-lock-output"; |
| 667 | sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set); |
| 668 | sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd); |
| 669 | sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd); |
Alex Light | a7915d4 | 2014-08-11 10:07:02 -0700 | [diff] [blame] | 670 | ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n", |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 671 | PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name); |
| 672 | |
| 673 | /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */ |
| 674 | char* argv[7]; |
| 675 | argv[0] = (char*) PATCHOAT_BIN; |
| 676 | argv[1] = (char*) patched_image_location_arg; |
| 677 | argv[2] = (char*) no_lock_arg; |
| 678 | argv[3] = instruction_set_arg; |
| 679 | argv[4] = output_oat_fd_arg; |
| 680 | argv[5] = input_oat_fd_arg; |
| 681 | argv[6] = NULL; |
| 682 | |
| 683 | execv(PATCHOAT_BIN, (char* const *)argv); |
| 684 | ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno)); |
| 685 | } |
| 686 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 687 | static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name, |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 688 | const char* output_file_name, int swap_fd, const char *pkgname, const char *instruction_set, |
Andreas Gampe | 598c25e | 2015-03-03 09:15:06 -0800 | [diff] [blame] | 689 | bool vm_safe_mode, bool debuggable) |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 690 | { |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 691 | static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; |
| 692 | |
| 693 | if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) { |
| 694 | ALOGE("Instruction set %s longer than max length of %d", |
| 695 | instruction_set, MAX_INSTRUCTION_SET_LEN); |
| 696 | return; |
| 697 | } |
| 698 | |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 699 | char prop_buf[PROPERTY_VALUE_MAX]; |
| 700 | bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1'); |
| 701 | |
| 702 | char dex2oat_Xms_flag[PROPERTY_VALUE_MAX]; |
| 703 | bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0; |
| 704 | |
| 705 | char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX]; |
| 706 | bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0; |
| 707 | |
Brian Carlstrom | cf51ba1 | 2014-07-28 19:13:28 -0700 | [diff] [blame] | 708 | char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX]; |
| 709 | bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter", |
| 710 | dex2oat_compiler_filter_flag, NULL) > 0; |
| 711 | |
Andreas Gampe | 8d7af8b | 2015-03-30 18:45:03 -0700 | [diff] [blame] | 712 | char dex2oat_threads_buf[PROPERTY_VALUE_MAX]; |
| 713 | bool have_dex2oat_threads_flag = property_get("dalvik.vm.dex2oat-threads", dex2oat_threads_buf, |
| 714 | NULL) > 0; |
| 715 | char dex2oat_threads_arg[PROPERTY_VALUE_MAX + 2]; |
| 716 | if (have_dex2oat_threads_flag) { |
| 717 | sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf); |
| 718 | } |
| 719 | |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 720 | char dex2oat_isa_features_key[PROPERTY_KEY_MAX]; |
| 721 | sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set); |
| 722 | char dex2oat_isa_features[PROPERTY_VALUE_MAX]; |
| 723 | bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key, |
| 724 | dex2oat_isa_features, NULL) > 0; |
| 725 | |
Ian Rogers | 16a95b2 | 2014-11-08 16:58:13 -0800 | [diff] [blame] | 726 | char dex2oat_isa_variant_key[PROPERTY_KEY_MAX]; |
| 727 | sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set); |
| 728 | char dex2oat_isa_variant[PROPERTY_VALUE_MAX]; |
| 729 | bool have_dex2oat_isa_variant = property_get(dex2oat_isa_variant_key, |
| 730 | dex2oat_isa_variant, NULL) > 0; |
| 731 | |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 732 | const char *dex2oat_norelocation = "-Xnorelocate"; |
| 733 | bool have_dex2oat_relocation_skip_flag = false; |
| 734 | |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 735 | char dex2oat_flags[PROPERTY_VALUE_MAX]; |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 736 | int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags", |
| 737 | dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags); |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 738 | ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags); |
| 739 | |
Brian Carlstrom | 538998f | 2014-07-30 14:37:11 -0700 | [diff] [blame] | 740 | // If we booting without the real /data, don't spend time compiling. |
| 741 | char vold_decrypt[PROPERTY_VALUE_MAX]; |
| 742 | bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0; |
| 743 | bool skip_compilation = (have_vold_decrypt && |
| 744 | (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 || |
| 745 | (strcmp(vold_decrypt, "1") == 0))); |
| 746 | |
Mathieu Chartier | d4a7b45 | 2015-03-20 15:39:47 -0700 | [diff] [blame] | 747 | char use_jit_property[PROPERTY_VALUE_MAX]; |
| 748 | bool have_jit_property = property_get("debug.usejit", use_jit_property, NULL) > 0; |
| 749 | bool use_jit = have_jit_property && strcmp(use_jit_property, "true") == 0; |
| 750 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 751 | static const char* DEX2OAT_BIN = "/system/bin/dex2oat"; |
Brian Carlstrom | 53e0776 | 2014-06-27 14:15:19 -0700 | [diff] [blame] | 752 | |
Brian Carlstrom | 53e0776 | 2014-06-27 14:15:19 -0700 | [diff] [blame] | 753 | static const char* RUNTIME_ARG = "--runtime-arg"; |
Brian Carlstrom | 53e0776 | 2014-06-27 14:15:19 -0700 | [diff] [blame] | 754 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 755 | static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 756 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 757 | char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN]; |
| 758 | char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX]; |
| 759 | char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN]; |
Brian Carlstrom | 7195fcc | 2014-06-16 13:28:03 -0700 | [diff] [blame] | 760 | char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX]; |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 761 | char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN]; |
Ian Rogers | 16a95b2 | 2014-11-08 16:58:13 -0800 | [diff] [blame] | 762 | char instruction_set_variant_arg[strlen("--instruction-set-variant=") + PROPERTY_VALUE_MAX]; |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 763 | char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX]; |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 764 | char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX]; |
| 765 | char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX]; |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 766 | char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX]; |
| 767 | char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX]; |
Brian Carlstrom | cf51ba1 | 2014-07-28 19:13:28 -0700 | [diff] [blame] | 768 | char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX]; |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 769 | bool have_dex2oat_swap_fd = false; |
| 770 | char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN]; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 771 | |
| 772 | sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd); |
| 773 | sprintf(zip_location_arg, "--zip-location=%s", input_file_name); |
| 774 | sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd); |
| 775 | sprintf(oat_location_arg, "--oat-location=%s", output_file_name); |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 776 | sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set); |
Ian Rogers | 16a95b2 | 2014-11-08 16:58:13 -0800 | [diff] [blame] | 777 | sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant); |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 778 | sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features); |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 779 | if (swap_fd >= 0) { |
| 780 | have_dex2oat_swap_fd = true; |
| 781 | sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd); |
| 782 | } |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 783 | |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 784 | bool have_profile_file = false; |
| 785 | bool have_top_k_profile_threshold = false; |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 786 | if (profiler && (strcmp(pkgname, "*") != 0)) { |
| 787 | char profile_file[PKG_PATH_MAX]; |
| 788 | snprintf(profile_file, sizeof(profile_file), "%s/%s", |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 789 | DALVIK_CACHE_PREFIX "profiles", pkgname); |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 790 | struct stat st; |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 791 | if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) { |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 792 | sprintf(profile_file_arg, "--profile-file=%s", profile_file); |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 793 | have_profile_file = true; |
| 794 | if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) { |
| 795 | snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg), |
| 796 | "--top-k-profile-threshold=%s", prop_buf); |
| 797 | have_top_k_profile_threshold = true; |
| 798 | } |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 799 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 800 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 801 | |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 802 | if (have_dex2oat_Xms_flag) { |
| 803 | sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag); |
| 804 | } |
| 805 | if (have_dex2oat_Xmx_flag) { |
| 806 | sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag); |
| 807 | } |
Brian Carlstrom | 538998f | 2014-07-30 14:37:11 -0700 | [diff] [blame] | 808 | if (skip_compilation) { |
Brian Carlstrom | e18987e | 2014-08-15 09:55:50 -0700 | [diff] [blame] | 809 | strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none"); |
Brian Carlstrom | 538998f | 2014-07-30 14:37:11 -0700 | [diff] [blame] | 810 | have_dex2oat_compiler_filter_flag = true; |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 811 | have_dex2oat_relocation_skip_flag = true; |
Calin Juravle | b1efac1 | 2014-08-21 19:05:20 +0100 | [diff] [blame] | 812 | } else if (vm_safe_mode) { |
| 813 | strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only"); |
Calin Juravle | 97477d2 | 2014-08-27 16:10:03 +0100 | [diff] [blame] | 814 | have_dex2oat_compiler_filter_flag = true; |
Mathieu Chartier | d4a7b45 | 2015-03-20 15:39:47 -0700 | [diff] [blame] | 815 | } else if (use_jit) { |
| 816 | strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-at-runtime"); |
| 817 | have_dex2oat_compiler_filter_flag = true; |
Brian Carlstrom | 538998f | 2014-07-30 14:37:11 -0700 | [diff] [blame] | 818 | } else if (have_dex2oat_compiler_filter_flag) { |
Brian Carlstrom | cf51ba1 | 2014-07-28 19:13:28 -0700 | [diff] [blame] | 819 | sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag); |
| 820 | } |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 821 | |
Andreas Gampe | 598c25e | 2015-03-03 09:15:06 -0800 | [diff] [blame] | 822 | // Check whether all apps should be compiled debuggable. |
| 823 | if (!debuggable) { |
| 824 | debuggable = |
| 825 | (property_get("dalvik.vm.always_debuggable", prop_buf, "0") > 0) && |
| 826 | (prop_buf[0] == '1'); |
| 827 | } |
| 828 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 829 | ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name); |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 830 | |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 831 | const char* argv[7 // program name, mandatory arguments and the final NULL |
| 832 | + (have_dex2oat_isa_variant ? 1 : 0) |
| 833 | + (have_dex2oat_isa_features ? 1 : 0) |
| 834 | + (have_profile_file ? 1 : 0) |
| 835 | + (have_top_k_profile_threshold ? 1 : 0) |
| 836 | + (have_dex2oat_Xms_flag ? 2 : 0) |
| 837 | + (have_dex2oat_Xmx_flag ? 2 : 0) |
| 838 | + (have_dex2oat_compiler_filter_flag ? 1 : 0) |
Andreas Gampe | 8d7af8b | 2015-03-30 18:45:03 -0700 | [diff] [blame] | 839 | + (have_dex2oat_threads_flag ? 1 : 0) |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 840 | + (have_dex2oat_swap_fd ? 1 : 0) |
| 841 | + (have_dex2oat_relocation_skip_flag ? 2 : 0) |
Andreas Gampe | 598c25e | 2015-03-03 09:15:06 -0800 | [diff] [blame] | 842 | + (debuggable ? 1 : 0) |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 843 | + dex2oat_flags_count]; |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 844 | int i = 0; |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 845 | argv[i++] = DEX2OAT_BIN; |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 846 | argv[i++] = zip_fd_arg; |
| 847 | argv[i++] = zip_location_arg; |
| 848 | argv[i++] = oat_fd_arg; |
| 849 | argv[i++] = oat_location_arg; |
| 850 | argv[i++] = instruction_set_arg; |
Ian Rogers | 16a95b2 | 2014-11-08 16:58:13 -0800 | [diff] [blame] | 851 | if (have_dex2oat_isa_variant) { |
| 852 | argv[i++] = instruction_set_variant_arg; |
| 853 | } |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 854 | if (have_dex2oat_isa_features) { |
| 855 | argv[i++] = instruction_set_features_arg; |
| 856 | } |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 857 | if (have_profile_file) { |
| 858 | argv[i++] = profile_file_arg; |
| 859 | } |
| 860 | if (have_top_k_profile_threshold) { |
| 861 | argv[i++] = top_k_profile_threshold_arg; |
| 862 | } |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 863 | if (have_dex2oat_Xms_flag) { |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 864 | argv[i++] = RUNTIME_ARG; |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 865 | argv[i++] = dex2oat_Xms_arg; |
| 866 | } |
| 867 | if (have_dex2oat_Xmx_flag) { |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 868 | argv[i++] = RUNTIME_ARG; |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 869 | argv[i++] = dex2oat_Xmx_arg; |
| 870 | } |
Brian Carlstrom | cf51ba1 | 2014-07-28 19:13:28 -0700 | [diff] [blame] | 871 | if (have_dex2oat_compiler_filter_flag) { |
| 872 | argv[i++] = dex2oat_compiler_filter_arg; |
| 873 | } |
Andreas Gampe | 8d7af8b | 2015-03-30 18:45:03 -0700 | [diff] [blame] | 874 | if (have_dex2oat_threads_flag) { |
| 875 | argv[i++] = dex2oat_threads_arg; |
| 876 | } |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 877 | if (have_dex2oat_swap_fd) { |
| 878 | argv[i++] = dex2oat_swap_fd; |
| 879 | } |
Andreas Gampe | 598c25e | 2015-03-03 09:15:06 -0800 | [diff] [blame] | 880 | if (debuggable) { |
| 881 | argv[i++] = "--debuggable"; |
| 882 | } |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 883 | if (dex2oat_flags_count) { |
| 884 | i += split(dex2oat_flags, argv + i); |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 885 | } |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 886 | if (have_dex2oat_relocation_skip_flag) { |
| 887 | argv[i++] = RUNTIME_ARG; |
| 888 | argv[i++] = dex2oat_norelocation; |
| 889 | } |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 890 | // Do not add after dex2oat_flags, they should override others for debugging. |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 891 | argv[i] = NULL; |
| 892 | |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 893 | execv(DEX2OAT_BIN, (char * const *)argv); |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 894 | ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno)); |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 895 | } |
| 896 | |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 897 | static int wait_child(pid_t pid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 898 | { |
| 899 | int status; |
| 900 | pid_t got_pid; |
| 901 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 902 | while (1) { |
| 903 | got_pid = waitpid(pid, &status, 0); |
| 904 | if (got_pid == -1 && errno == EINTR) { |
| 905 | printf("waitpid interrupted, retrying\n"); |
| 906 | } else { |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | if (got_pid != pid) { |
| 911 | ALOGW("waitpid failed: wanted %d, got %d: %s\n", |
| 912 | (int) pid, (int) got_pid, strerror(errno)); |
| 913 | return 1; |
| 914 | } |
| 915 | |
| 916 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 917 | return 0; |
| 918 | } else { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 919 | return status; /* always nonzero */ |
| 920 | } |
| 921 | } |
| 922 | |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 923 | /* |
| 924 | * Whether dexopt should use a swap file when compiling an APK. If kAlwaysProvideSwapFile, do this |
| 925 | * on all devices (dex2oat will make a more informed decision itself, anyways). Otherwise, only do |
| 926 | * this on a low-mem device. |
| 927 | */ |
| 928 | static bool kAlwaysProvideSwapFile = true; |
| 929 | |
| 930 | static bool ShouldUseSwapFileForDexopt() { |
| 931 | if (kAlwaysProvideSwapFile) { |
| 932 | return true; |
| 933 | } |
| 934 | |
| 935 | char low_mem_buf[PROPERTY_VALUE_MAX]; |
| 936 | property_get("ro.config.low_ram", low_mem_buf, ""); |
| 937 | return (strcmp(low_mem_buf, "true") == 0); |
| 938 | } |
| 939 | |
Richard Uhler | 009b877 | 2015-03-18 12:39:09 -0700 | [diff] [blame] | 940 | /* |
| 941 | * Computes the odex file for the given apk_path and instruction_set. |
| 942 | * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex |
| 943 | * |
| 944 | * Returns false if it failed to determine the odex file path. |
| 945 | */ |
| 946 | static bool calculate_odex_file_path(char path[PKG_PATH_MAX], |
| 947 | const char *apk_path, |
| 948 | const char *instruction_set) |
| 949 | { |
| 950 | if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set) |
| 951 | + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) { |
| 952 | ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path); |
| 953 | return false; |
| 954 | } |
| 955 | |
| 956 | strcpy(path, apk_path); |
| 957 | char *end = strrchr(path, '/'); |
| 958 | if (end == NULL) { |
| 959 | ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path); |
| 960 | return false; |
| 961 | } |
| 962 | const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/'); |
| 963 | |
| 964 | strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0 |
| 965 | strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0 |
| 966 | strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0 |
| 967 | end = strrchr(path, '.'); |
| 968 | if (end == NULL) { |
| 969 | ALOGE("apk_path '%s' has no extension.\n", apk_path); |
| 970 | return false; |
| 971 | } |
| 972 | strcpy(end + 1, "odex"); |
| 973 | return true; |
| 974 | } |
| 975 | |
Calin Juravle | b1efac1 | 2014-08-21 19:05:20 +0100 | [diff] [blame] | 976 | int dexopt(const char *apk_path, uid_t uid, bool is_public, |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 977 | const char *pkgname, const char *instruction_set, int dexopt_needed, |
| 978 | bool vm_safe_mode, bool debuggable, const char* oat_dir) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 979 | { |
| 980 | struct utimbuf ut; |
Fyodor Kupolov | 26ff93c | 2015-04-02 16:59:10 -0700 | [diff] [blame] | 981 | struct stat input_stat; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 982 | char out_path[PKG_PATH_MAX]; |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 983 | char swap_file_name[PKG_PATH_MAX]; |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 984 | const char *input_file; |
| 985 | char in_odex_path[PKG_PATH_MAX]; |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 986 | int res, input_fd=-1, out_fd=-1, swap_fd=-1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 987 | |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 988 | // Early best-effort check whether we can fit the the path into our buffers. |
| 989 | // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run |
| 990 | // without a swap file, if necessary. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 991 | if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) { |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 992 | ALOGE("apk_path too long '%s'\n", apk_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 993 | return -1; |
| 994 | } |
| 995 | |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 996 | if (oat_dir != NULL && oat_dir[0] != '!') { |
| 997 | if (validate_apk_path(oat_dir)) { |
| 998 | ALOGE("invalid oat_dir '%s'\n", oat_dir); |
| 999 | return -1; |
Chih-Wei Huang | 0e8ae16 | 2014-04-28 15:47:45 +0800 | [diff] [blame] | 1000 | } |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1001 | if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) { |
| 1002 | return -1; |
| 1003 | } |
| 1004 | } else { |
| 1005 | if (create_cache_path(out_path, apk_path, instruction_set)) { |
| 1006 | return -1; |
| 1007 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1010 | switch (dexopt_needed) { |
| 1011 | case DEXOPT_DEX2OAT_NEEDED: |
| 1012 | input_file = apk_path; |
| 1013 | break; |
| 1014 | |
| 1015 | case DEXOPT_PATCHOAT_NEEDED: |
| 1016 | if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) { |
| 1017 | return -1; |
| 1018 | } |
| 1019 | input_file = in_odex_path; |
| 1020 | break; |
| 1021 | |
| 1022 | case DEXOPT_SELF_PATCHOAT_NEEDED: |
| 1023 | input_file = out_path; |
| 1024 | break; |
| 1025 | |
| 1026 | default: |
| 1027 | ALOGE("Invalid dexopt needed: %d\n", dexopt_needed); |
| 1028 | exit(72); |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1029 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1030 | |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1031 | memset(&input_stat, 0, sizeof(input_stat)); |
| 1032 | stat(input_file, &input_stat); |
| 1033 | |
| 1034 | input_fd = open(input_file, O_RDONLY, 0); |
| 1035 | if (input_fd < 0) { |
| 1036 | ALOGE("installd cannot open '%s' for input during dexopt\n", input_file); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1037 | return -1; |
| 1038 | } |
| 1039 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1040 | unlink(out_path); |
| 1041 | out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644); |
| 1042 | if (out_fd < 0) { |
| 1043 | ALOGE("installd cannot open '%s' for output during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1044 | goto fail; |
| 1045 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1046 | if (fchmod(out_fd, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1047 | S_IRUSR|S_IWUSR|S_IRGRP | |
| 1048 | (is_public ? S_IROTH : 0)) < 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1049 | ALOGE("installd cannot chmod '%s' during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1050 | goto fail; |
| 1051 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1052 | if (fchown(out_fd, AID_SYSTEM, uid) < 0) { |
| 1053 | ALOGE("installd cannot chown '%s' during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1054 | goto fail; |
| 1055 | } |
| 1056 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1057 | // Create profile file if there is a package name present. |
| 1058 | if (strcmp(pkgname, "*") != 0) { |
| 1059 | create_profile_file(pkgname, uid); |
| 1060 | } |
| 1061 | |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 1062 | // Create a swap file if necessary. |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1063 | if (ShouldUseSwapFileForDexopt()) { |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 1064 | // Make sure there really is enough space. |
| 1065 | size_t out_len = strlen(out_path); |
| 1066 | if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) { |
| 1067 | strcpy(swap_file_name, out_path); |
| 1068 | strcpy(swap_file_name + strlen(out_path), ".swap"); |
| 1069 | unlink(swap_file_name); |
| 1070 | swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600); |
| 1071 | if (swap_fd < 0) { |
| 1072 | // Could not create swap file. Optimistically go on and hope that we can compile |
| 1073 | // without it. |
| 1074 | ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name); |
| 1075 | } else { |
| 1076 | // Immediately unlink. We don't really want to hit flash. |
| 1077 | unlink(swap_file_name); |
| 1078 | } |
| 1079 | } else { |
| 1080 | // Swap file path is too long. Try to run without. |
| 1081 | ALOGE("installd could not create swap file for path %s during dexopt\n", out_path); |
| 1082 | } |
| 1083 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1084 | |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1085 | ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1086 | |
| 1087 | pid_t pid; |
| 1088 | pid = fork(); |
| 1089 | if (pid == 0) { |
| 1090 | /* child -- drop privileges before continuing */ |
| 1091 | if (setgid(uid) != 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1092 | ALOGE("setgid(%d) failed in installd during dexopt\n", uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1093 | exit(64); |
| 1094 | } |
| 1095 | if (setuid(uid) != 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1096 | ALOGE("setuid(%d) failed in installd during dexopt\n", uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1097 | exit(65); |
| 1098 | } |
| 1099 | // drop capabilities |
| 1100 | struct __user_cap_header_struct capheader; |
| 1101 | struct __user_cap_data_struct capdata[2]; |
| 1102 | memset(&capheader, 0, sizeof(capheader)); |
| 1103 | memset(&capdata, 0, sizeof(capdata)); |
| 1104 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 1105 | if (capset(&capheader, &capdata[0]) < 0) { |
| 1106 | ALOGE("capset failed: %s\n", strerror(errno)); |
| 1107 | exit(66); |
| 1108 | } |
Brian Carlstrom | 0378aaf | 2014-08-08 00:52:22 -0700 | [diff] [blame] | 1109 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 1110 | ALOGE("set_sched_policy failed: %s\n", strerror(errno)); |
| 1111 | exit(70); |
| 1112 | } |
Igor Murashkin | 9e87a80 | 2014-11-05 15:21:12 -0800 | [diff] [blame] | 1113 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { |
| 1114 | ALOGE("setpriority failed: %s\n", strerror(errno)); |
| 1115 | exit(71); |
| 1116 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1117 | if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) { |
| 1118 | ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1119 | exit(67); |
| 1120 | } |
| 1121 | |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1122 | if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED |
| 1123 | || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) { |
Andreas Gampe | bd872e4 | 2014-12-15 11:41:11 -0800 | [diff] [blame] | 1124 | run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set); |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1125 | } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) { |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1126 | const char *input_file_name = strrchr(input_file, '/'); |
| 1127 | if (input_file_name == NULL) { |
| 1128 | input_file_name = input_file; |
| 1129 | } else { |
| 1130 | input_file_name++; |
| 1131 | } |
| 1132 | run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname, |
| 1133 | instruction_set, vm_safe_mode, debuggable); |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1134 | } else { |
| 1135 | ALOGE("Invalid dexopt needed: %d\n", dexopt_needed); |
| 1136 | exit(73); |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1137 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1138 | exit(68); /* only get here on exec failure */ |
| 1139 | } else { |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1140 | res = wait_child(pid); |
| 1141 | if (res == 0) { |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1142 | ALOGV("DexInv: --- END '%s' (success) ---\n", input_file); |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1143 | } else { |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1144 | ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1145 | goto fail; |
| 1146 | } |
| 1147 | } |
| 1148 | |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1149 | ut.actime = input_stat.st_atime; |
| 1150 | ut.modtime = input_stat.st_mtime; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1151 | utime(out_path, &ut); |
| 1152 | |
| 1153 | close(out_fd); |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1154 | close(input_fd); |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 1155 | if (swap_fd != -1) { |
| 1156 | close(swap_fd); |
| 1157 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1158 | return 0; |
| 1159 | |
| 1160 | fail: |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1161 | if (out_fd >= 0) { |
| 1162 | close(out_fd); |
| 1163 | unlink(out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1164 | } |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1165 | if (input_fd >= 0) { |
| 1166 | close(input_fd); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1167 | } |
| 1168 | return -1; |
| 1169 | } |
| 1170 | |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 1171 | int mark_boot_complete(const char* instruction_set) |
| 1172 | { |
| 1173 | char boot_marker_path[PKG_PATH_MAX]; |
| 1174 | sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set); |
| 1175 | |
| 1176 | ALOGV("mark_boot_complete : %s", boot_marker_path); |
| 1177 | if (unlink(boot_marker_path) != 0) { |
| 1178 | ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path, |
| 1179 | strerror(errno)); |
| 1180 | return -1; |
| 1181 | } |
| 1182 | |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1186 | void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, |
| 1187 | struct stat* statbuf) |
| 1188 | { |
| 1189 | while (path[basepos] != 0) { |
| 1190 | if (path[basepos] == '/') { |
| 1191 | path[basepos] = 0; |
| 1192 | if (lstat(path, statbuf) < 0) { |
| 1193 | ALOGV("Making directory: %s\n", path); |
| 1194 | if (mkdir(path, mode) == 0) { |
| 1195 | chown(path, uid, gid); |
| 1196 | } else { |
| 1197 | ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); |
| 1198 | } |
| 1199 | } |
| 1200 | path[basepos] = '/'; |
| 1201 | basepos++; |
| 1202 | } |
| 1203 | basepos++; |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | int movefileordir(char* srcpath, char* dstpath, int dstbasepos, |
| 1208 | int dstuid, int dstgid, struct stat* statbuf) |
| 1209 | { |
| 1210 | DIR *d; |
| 1211 | struct dirent *de; |
| 1212 | int res; |
| 1213 | |
| 1214 | int srcend = strlen(srcpath); |
| 1215 | int dstend = strlen(dstpath); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1216 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1217 | if (lstat(srcpath, statbuf) < 0) { |
| 1218 | ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno)); |
| 1219 | return 1; |
| 1220 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1221 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1222 | if ((statbuf->st_mode&S_IFDIR) == 0) { |
| 1223 | mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH, |
| 1224 | dstuid, dstgid, statbuf); |
| 1225 | ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid); |
| 1226 | if (rename(srcpath, dstpath) >= 0) { |
| 1227 | if (chown(dstpath, dstuid, dstgid) < 0) { |
| 1228 | ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno)); |
| 1229 | unlink(dstpath); |
| 1230 | return 1; |
| 1231 | } |
| 1232 | } else { |
| 1233 | ALOGW("Unable to rename %s to %s: %s\n", |
| 1234 | srcpath, dstpath, strerror(errno)); |
| 1235 | return 1; |
| 1236 | } |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | d = opendir(srcpath); |
| 1241 | if (d == NULL) { |
| 1242 | ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno)); |
| 1243 | return 1; |
| 1244 | } |
| 1245 | |
| 1246 | res = 0; |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1247 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1248 | while ((de = readdir(d))) { |
| 1249 | const char *name = de->d_name; |
| 1250 | /* always skip "." and ".." */ |
| 1251 | if (name[0] == '.') { |
| 1252 | if (name[1] == 0) continue; |
| 1253 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 1254 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1255 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1256 | if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
| 1257 | ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name); |
| 1258 | continue; |
| 1259 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1260 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1261 | if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
| 1262 | ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name); |
| 1263 | continue; |
| 1264 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1265 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1266 | srcpath[srcend] = dstpath[dstend] = '/'; |
| 1267 | strcpy(srcpath+srcend+1, name); |
| 1268 | strcpy(dstpath+dstend+1, name); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1269 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1270 | if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) { |
| 1271 | res = 1; |
| 1272 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1273 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1274 | // Note: we will be leaving empty directories behind in srcpath, |
| 1275 | // but that is okay, the package manager will be erasing all of the |
| 1276 | // data associated with .apks that disappear. |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1277 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1278 | srcpath[srcend] = dstpath[dstend] = 0; |
| 1279 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1280 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1281 | closedir(d); |
| 1282 | return res; |
| 1283 | } |
| 1284 | |
| 1285 | int movefiles() |
| 1286 | { |
| 1287 | DIR *d; |
| 1288 | int dfd, subfd; |
| 1289 | struct dirent *de; |
| 1290 | struct stat s; |
| 1291 | char buf[PKG_PATH_MAX+1]; |
| 1292 | int bufp, bufe, bufi, readlen; |
| 1293 | |
| 1294 | char srcpkg[PKG_NAME_MAX]; |
| 1295 | char dstpkg[PKG_NAME_MAX]; |
| 1296 | char srcpath[PKG_PATH_MAX]; |
| 1297 | char dstpath[PKG_PATH_MAX]; |
| 1298 | int dstuid=-1, dstgid=-1; |
| 1299 | int hasspace; |
| 1300 | |
| 1301 | d = opendir(UPDATE_COMMANDS_DIR_PREFIX); |
| 1302 | if (d == NULL) { |
| 1303 | goto done; |
| 1304 | } |
| 1305 | dfd = dirfd(d); |
| 1306 | |
| 1307 | /* Iterate through all files in the directory, executing the |
| 1308 | * file movements requested there-in. |
| 1309 | */ |
| 1310 | while ((de = readdir(d))) { |
| 1311 | const char *name = de->d_name; |
| 1312 | |
| 1313 | if (de->d_type == DT_DIR) { |
| 1314 | continue; |
| 1315 | } else { |
| 1316 | subfd = openat(dfd, name, O_RDONLY); |
| 1317 | if (subfd < 0) { |
| 1318 | ALOGW("Unable to open update commands at %s%s\n", |
| 1319 | UPDATE_COMMANDS_DIR_PREFIX, name); |
| 1320 | continue; |
| 1321 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1322 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1323 | bufp = 0; |
| 1324 | bufe = 0; |
| 1325 | buf[PKG_PATH_MAX] = 0; |
| 1326 | srcpkg[0] = dstpkg[0] = 0; |
| 1327 | while (1) { |
| 1328 | bufi = bufp; |
| 1329 | while (bufi < bufe && buf[bufi] != '\n') { |
| 1330 | bufi++; |
| 1331 | } |
| 1332 | if (bufi < bufe) { |
| 1333 | buf[bufi] = 0; |
| 1334 | ALOGV("Processing line: %s\n", buf+bufp); |
| 1335 | hasspace = 0; |
| 1336 | while (bufp < bufi && isspace(buf[bufp])) { |
| 1337 | hasspace = 1; |
| 1338 | bufp++; |
| 1339 | } |
| 1340 | if (buf[bufp] == '#' || bufp == bufi) { |
| 1341 | // skip comments and empty lines. |
| 1342 | } else if (hasspace) { |
| 1343 | if (dstpkg[0] == 0) { |
| 1344 | ALOGW("Path before package line in %s%s: %s\n", |
| 1345 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 1346 | } else if (srcpkg[0] == 0) { |
| 1347 | // Skip -- source package no longer exists. |
| 1348 | } else { |
| 1349 | ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg); |
| 1350 | if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) && |
| 1351 | !create_move_path(dstpath, dstpkg, buf+bufp, 0)) { |
| 1352 | movefileordir(srcpath, dstpath, |
| 1353 | strlen(dstpath)-strlen(buf+bufp), |
| 1354 | dstuid, dstgid, &s); |
| 1355 | } |
| 1356 | } |
| 1357 | } else { |
| 1358 | char* div = strchr(buf+bufp, ':'); |
| 1359 | if (div == NULL) { |
| 1360 | ALOGW("Bad package spec in %s%s; no ':' sep: %s\n", |
| 1361 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 1362 | } else { |
| 1363 | *div = 0; |
| 1364 | div++; |
| 1365 | if (strlen(buf+bufp) < PKG_NAME_MAX) { |
| 1366 | strcpy(dstpkg, buf+bufp); |
| 1367 | } else { |
| 1368 | srcpkg[0] = dstpkg[0] = 0; |
| 1369 | ALOGW("Package name too long in %s%s: %s\n", |
| 1370 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 1371 | } |
| 1372 | if (strlen(div) < PKG_NAME_MAX) { |
| 1373 | strcpy(srcpkg, div); |
| 1374 | } else { |
| 1375 | srcpkg[0] = dstpkg[0] = 0; |
| 1376 | ALOGW("Package name too long in %s%s: %s\n", |
| 1377 | UPDATE_COMMANDS_DIR_PREFIX, name, div); |
| 1378 | } |
| 1379 | if (srcpkg[0] != 0) { |
| 1380 | if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) { |
| 1381 | if (lstat(srcpath, &s) < 0) { |
| 1382 | // Package no longer exists -- skip. |
| 1383 | srcpkg[0] = 0; |
| 1384 | } |
| 1385 | } else { |
| 1386 | srcpkg[0] = 0; |
| 1387 | ALOGW("Can't create path %s in %s%s\n", |
| 1388 | div, UPDATE_COMMANDS_DIR_PREFIX, name); |
| 1389 | } |
| 1390 | if (srcpkg[0] != 0) { |
| 1391 | if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) { |
| 1392 | if (lstat(dstpath, &s) == 0) { |
| 1393 | dstuid = s.st_uid; |
| 1394 | dstgid = s.st_gid; |
| 1395 | } else { |
| 1396 | // Destination package doesn't |
| 1397 | // exist... due to original-package, |
| 1398 | // this is normal, so don't be |
| 1399 | // noisy about it. |
| 1400 | srcpkg[0] = 0; |
| 1401 | } |
| 1402 | } else { |
| 1403 | srcpkg[0] = 0; |
| 1404 | ALOGW("Can't create path %s in %s%s\n", |
| 1405 | div, UPDATE_COMMANDS_DIR_PREFIX, name); |
| 1406 | } |
| 1407 | } |
| 1408 | ALOGV("Transfering from %s to %s: uid=%d\n", |
| 1409 | srcpkg, dstpkg, dstuid); |
| 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | bufp = bufi+1; |
| 1414 | } else { |
| 1415 | if (bufp == 0) { |
| 1416 | if (bufp < bufe) { |
| 1417 | ALOGW("Line too long in %s%s, skipping: %s\n", |
| 1418 | UPDATE_COMMANDS_DIR_PREFIX, name, buf); |
| 1419 | } |
| 1420 | } else if (bufp < bufe) { |
| 1421 | memcpy(buf, buf+bufp, bufe-bufp); |
| 1422 | bufe -= bufp; |
| 1423 | bufp = 0; |
| 1424 | } |
| 1425 | readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe); |
| 1426 | if (readlen < 0) { |
| 1427 | ALOGW("Failure reading update commands in %s%s: %s\n", |
| 1428 | UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno)); |
| 1429 | break; |
| 1430 | } else if (readlen == 0) { |
| 1431 | break; |
| 1432 | } |
| 1433 | bufe += readlen; |
| 1434 | buf[bufe] = 0; |
| 1435 | ALOGV("Read buf: %s\n", buf); |
| 1436 | } |
| 1437 | } |
| 1438 | close(subfd); |
| 1439 | } |
| 1440 | } |
| 1441 | closedir(d); |
| 1442 | done: |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 1446 | int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1447 | { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1448 | struct stat s, libStat; |
| 1449 | int rc = 0; |
| 1450 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 1451 | std::string _pkgdir(create_package_data_path(uuid, pkgname, userId)); |
| 1452 | std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX); |
| 1453 | |
| 1454 | const char* pkgdir = _pkgdir.c_str(); |
| 1455 | const char* libsymlink = _libsymlink.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1456 | |
| 1457 | if (stat(pkgdir, &s) < 0) return -1; |
| 1458 | |
| 1459 | if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { |
| 1460 | ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno)); |
| 1461 | return -1; |
| 1462 | } |
| 1463 | |
| 1464 | if (chmod(pkgdir, 0700) < 0) { |
| 1465 | ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); |
| 1466 | rc = -1; |
| 1467 | goto out; |
| 1468 | } |
| 1469 | |
| 1470 | if (lstat(libsymlink, &libStat) < 0) { |
| 1471 | if (errno != ENOENT) { |
| 1472 | ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); |
| 1473 | rc = -1; |
| 1474 | goto out; |
| 1475 | } |
| 1476 | } else { |
| 1477 | if (S_ISDIR(libStat.st_mode)) { |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 1478 | if (delete_dir_contents(libsymlink, 1, NULL) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1479 | rc = -1; |
| 1480 | goto out; |
| 1481 | } |
| 1482 | } else if (S_ISLNK(libStat.st_mode)) { |
| 1483 | if (unlink(libsymlink) < 0) { |
| 1484 | ALOGE("couldn't unlink lib dir: %s\n", strerror(errno)); |
| 1485 | rc = -1; |
| 1486 | goto out; |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | if (symlink(asecLibDir, libsymlink) < 0) { |
| 1492 | ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir, |
| 1493 | strerror(errno)); |
| 1494 | rc = -errno; |
| 1495 | goto out; |
| 1496 | } |
| 1497 | |
| 1498 | out: |
| 1499 | if (chmod(pkgdir, s.st_mode) < 0) { |
| 1500 | ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); |
| 1501 | rc = -errno; |
| 1502 | } |
| 1503 | |
| 1504 | if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { |
| 1505 | ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno)); |
| 1506 | return -errno; |
| 1507 | } |
| 1508 | |
| 1509 | return rc; |
| 1510 | } |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1511 | |
| 1512 | static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) |
| 1513 | { |
| 1514 | static const char *IDMAP_BIN = "/system/bin/idmap"; |
| 1515 | static const size_t MAX_INT_LEN = 32; |
| 1516 | char idmap_str[MAX_INT_LEN]; |
| 1517 | |
| 1518 | snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); |
| 1519 | |
| 1520 | execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); |
| 1521 | ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); |
| 1522 | } |
| 1523 | |
| 1524 | // Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) |
| 1525 | // eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap |
| 1526 | static int flatten_path(const char *prefix, const char *suffix, |
| 1527 | const char *overlay_path, char *idmap_path, size_t N) |
| 1528 | { |
| 1529 | if (overlay_path == NULL || idmap_path == NULL) { |
| 1530 | return -1; |
| 1531 | } |
| 1532 | const size_t len_overlay_path = strlen(overlay_path); |
| 1533 | // will access overlay_path + 1 further below; requires absolute path |
| 1534 | if (len_overlay_path < 2 || *overlay_path != '/') { |
| 1535 | return -1; |
| 1536 | } |
| 1537 | const size_t len_idmap_root = strlen(prefix); |
| 1538 | const size_t len_suffix = strlen(suffix); |
| 1539 | if (SIZE_MAX - len_idmap_root < len_overlay_path || |
| 1540 | SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { |
| 1541 | // additions below would cause overflow |
| 1542 | return -1; |
| 1543 | } |
| 1544 | if (N < len_idmap_root + len_overlay_path + len_suffix) { |
| 1545 | return -1; |
| 1546 | } |
| 1547 | memset(idmap_path, 0, N); |
| 1548 | snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); |
| 1549 | char *ch = idmap_path + len_idmap_root; |
| 1550 | while (*ch != '\0') { |
| 1551 | if (*ch == '/') { |
| 1552 | *ch = '@'; |
| 1553 | } |
| 1554 | ++ch; |
| 1555 | } |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | int idmap(const char *target_apk, const char *overlay_apk, uid_t uid) |
| 1560 | { |
| 1561 | ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); |
| 1562 | |
| 1563 | int idmap_fd = -1; |
| 1564 | char idmap_path[PATH_MAX]; |
| 1565 | |
| 1566 | if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, |
| 1567 | idmap_path, sizeof(idmap_path)) == -1) { |
| 1568 | ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); |
| 1569 | goto fail; |
| 1570 | } |
| 1571 | |
| 1572 | unlink(idmap_path); |
| 1573 | idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); |
| 1574 | if (idmap_fd < 0) { |
| 1575 | ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); |
| 1576 | goto fail; |
| 1577 | } |
| 1578 | if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { |
| 1579 | ALOGE("idmap cannot chown '%s'\n", idmap_path); |
| 1580 | goto fail; |
| 1581 | } |
| 1582 | if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { |
| 1583 | ALOGE("idmap cannot chmod '%s'\n", idmap_path); |
| 1584 | goto fail; |
| 1585 | } |
| 1586 | |
| 1587 | pid_t pid; |
| 1588 | pid = fork(); |
| 1589 | if (pid == 0) { |
| 1590 | /* child -- drop privileges before continuing */ |
| 1591 | if (setgid(uid) != 0) { |
| 1592 | ALOGE("setgid(%d) failed during idmap\n", uid); |
| 1593 | exit(1); |
| 1594 | } |
| 1595 | if (setuid(uid) != 0) { |
| 1596 | ALOGE("setuid(%d) failed during idmap\n", uid); |
| 1597 | exit(1); |
| 1598 | } |
| 1599 | if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { |
| 1600 | ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); |
| 1601 | exit(1); |
| 1602 | } |
| 1603 | |
| 1604 | run_idmap(target_apk, overlay_apk, idmap_fd); |
| 1605 | exit(1); /* only if exec call to idmap failed */ |
| 1606 | } else { |
| 1607 | int status = wait_child(pid); |
| 1608 | if (status != 0) { |
| 1609 | ALOGE("idmap failed, status=0x%04x\n", status); |
| 1610 | goto fail; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | close(idmap_fd); |
| 1615 | return 0; |
| 1616 | fail: |
| 1617 | if (idmap_fd >= 0) { |
| 1618 | close(idmap_fd); |
| 1619 | unlink(idmap_path); |
| 1620 | } |
| 1621 | return -1; |
| 1622 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1623 | |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 1624 | int restorecon_data(const char* uuid, const char* pkgName, |
Andreas Gampe | 0ad7a11 | 2015-04-09 09:52:45 -0700 | [diff] [blame] | 1625 | const char* seinfo, uid_t uid) |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1626 | { |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1627 | struct dirent *entry; |
| 1628 | DIR *d; |
| 1629 | struct stat s; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1630 | int ret = 0; |
| 1631 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1632 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
| 1633 | unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE; |
| 1634 | |
| 1635 | if (!pkgName || !seinfo) { |
| 1636 | ALOGE("Package name or seinfo tag is null when trying to restorecon."); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1637 | return -1; |
| 1638 | } |
| 1639 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1640 | // Special case for owner on internal storage |
| 1641 | if (uuid == nullptr) { |
| 1642 | std::string path(create_package_data_path(nullptr, pkgName, 0)); |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1643 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1644 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, flags) < 0) { |
| 1645 | PLOG(ERROR) << "restorecon failed for " << path; |
| 1646 | ret |= -1; |
| 1647 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1648 | } |
| 1649 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1650 | // Relabel package directory for all secondary users. |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1651 | std::string userdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX); |
| 1652 | d = opendir(userdir.c_str()); |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1653 | if (d == NULL) { |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1654 | return -1; |
| 1655 | } |
| 1656 | |
| 1657 | while ((entry = readdir(d))) { |
| 1658 | if (entry->d_type != DT_DIR) { |
| 1659 | continue; |
| 1660 | } |
| 1661 | |
| 1662 | const char *user = entry->d_name; |
| 1663 | // Ignore "." and ".." |
| 1664 | if (!strcmp(user, ".") || !strcmp(user, "..")) { |
| 1665 | continue; |
| 1666 | } |
| 1667 | |
| 1668 | // user directories start with a number |
| 1669 | if (user[0] < '0' || user[0] > '9') { |
| 1670 | ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user); |
| 1671 | continue; |
| 1672 | } |
| 1673 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame^] | 1674 | std::string pkgdir(StringPrintf("%s%s/%s", userdir.c_str(), user, pkgName)); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1675 | if (stat(pkgdir.c_str(), &s) < 0) { |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1676 | continue; |
| 1677 | } |
| 1678 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1679 | if (selinux_android_restorecon_pkgdir(pkgdir.c_str(), seinfo, s.st_uid, flags) < 0) { |
| 1680 | PLOG(ERROR) << "restorecon failed for " << pkgdir; |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1681 | ret |= -1; |
| 1682 | } |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | closedir(d); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1686 | return ret; |
| 1687 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 1688 | |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1689 | int create_oat_dir(const char* oat_dir, const char* instruction_set) |
| 1690 | { |
| 1691 | char oat_instr_dir[PKG_PATH_MAX]; |
| 1692 | |
| 1693 | if (validate_apk_path(oat_dir)) { |
| 1694 | ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir); |
| 1695 | return -1; |
| 1696 | } |
Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 1697 | if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1698 | return -1; |
| 1699 | } |
| 1700 | if (selinux_android_restorecon(oat_dir, 0)) { |
| 1701 | ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno)); |
| 1702 | return -1; |
| 1703 | } |
| 1704 | snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); |
Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 1705 | if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1706 | return -1; |
| 1707 | } |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | int rm_package_dir(const char* apk_path) |
| 1712 | { |
| 1713 | if (validate_apk_path(apk_path)) { |
| 1714 | ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path); |
| 1715 | return -1; |
| 1716 | } |
| 1717 | return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */); |
| 1718 | } |
| 1719 | |
| 1720 | int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, |
| 1721 | const char *instruction_set) { |
| 1722 | char *file_name_start; |
| 1723 | char *file_name_end; |
| 1724 | |
| 1725 | file_name_start = strrchr(apk_path, '/'); |
| 1726 | if (file_name_start == NULL) { |
| 1727 | ALOGE("apk_path '%s' has no '/'s in it\n", apk_path); |
| 1728 | return -1; |
| 1729 | } |
| 1730 | file_name_end = strrchr(apk_path, '.'); |
| 1731 | if (file_name_end < file_name_start) { |
| 1732 | ALOGE("apk_path '%s' has no extension\n", apk_path); |
| 1733 | return -1; |
| 1734 | } |
| 1735 | |
| 1736 | // Calculate file_name |
| 1737 | int file_name_len = file_name_end - file_name_start - 1; |
| 1738 | char file_name[file_name_len + 1]; |
| 1739 | memcpy(file_name, file_name_start + 1, file_name_len); |
| 1740 | file_name[file_name_len] = '\0'; |
| 1741 | |
| 1742 | // <apk_parent_dir>/oat/<isa>/<file_name>.odex |
| 1743 | snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name); |
| 1744 | return 0; |
| 1745 | } |