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 | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 53 | std::string _pkgdir(create_data_user_package_path(uuid, 0, pkgname)); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 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 | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 83 | std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname)); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 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 | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 118 | std::string _pkgdir(create_data_user_package_path(uuid, 0, pkgname)); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 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 | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 144 | std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname)); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 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 | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 152 | std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname)); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 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 | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 180 | int 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 Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 183 | std::vector<userid_t> users = get_known_users(from_uuid); |
| 184 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 185 | // 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 Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 218 | for (auto user : users) { |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 219 | 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 Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 222 | |
| 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 Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 249 | (char*) to_parent.c_str() |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 250 | }; |
| 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 Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 267 | // 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 Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 274 | for (auto user : users) { |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 275 | std::string from(create_data_user_package_path(from_uuid, user, package_name)); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 276 | if (delete_dir_contents(from.c_str(), 1, NULL) != 0) { |
| 277 | LOG(WARNING) << "Failed to delete " << from; |
| 278 | } |
| 279 | } |
| 280 | return 0; |
| 281 | |
| 282 | fail: |
| 283 | // Nuke everything we might have already copied |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 284 | { |
| 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 Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 290 | for (auto user : users) { |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 291 | std::string to(create_data_user_package_path(to_uuid, user, package_name)); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 292 | 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 Lee | 7c8bec0 | 2014-06-10 18:46:26 +0100 | [diff] [blame] | 299 | int make_user_config(userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 300 | { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 301 | if (ensure_config_user_dirs(userid) == -1) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 308 | int delete_user(const char *uuid, userid_t userid) |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 309 | { |
| 310 | int status = 0; |
| 311 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 312 | std::string data_path(create_data_user_path(uuid, userid)); |
| 313 | if (delete_dir_contents(data_path.c_str(), 1, NULL) != 0) { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 314 | status = -1; |
| 315 | } |
| 316 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 317 | std::string media_path(create_data_media_path(uuid, userid)); |
| 318 | if (delete_dir_contents(media_path.c_str(), 1, NULL) != 0) { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 319 | status = -1; |
| 320 | } |
| 321 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 322 | // 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 Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | return status; |
| 332 | } |
| 333 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 334 | int delete_cache(const char *uuid, const char *pkgname, userid_t userid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 335 | { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 336 | std::string _cachedir( |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 337 | create_data_user_package_path(uuid, userid, pkgname) + CACHE_DIR_POSTFIX); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 338 | const char* cachedir = _cachedir.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 339 | |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 340 | /* delete contents, not the directory, no exceptions */ |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 341 | return delete_dir_contents(cachedir, 0, NULL); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 344 | 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] | 345 | { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 346 | std::string _codecachedir( |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 347 | create_data_user_package_path(uuid, userid, pkgname) + CACHE_DIR_POSTFIX); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 348 | const char* codecachedir = _codecachedir.c_str(); |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 349 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 350 | struct stat s; |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 351 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 352 | /* it's okay if code cache is missing */ |
| 353 | if (lstat(codecachedir, &s) == -1 && errno == ENOENT) { |
| 354 | return 0; |
| 355 | } |
| 356 | |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 357 | /* delete contents, not the directory, no exceptions */ |
| 358 | return delete_dir_contents(codecachedir, 0, NULL); |
| 359 | } |
| 360 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 361 | /* 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 Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 368 | int free_cache(const char *uuid, int64_t free_size) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 369 | { |
| 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 Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 377 | std::string data_path(create_data_path(uuid)); |
| 378 | |
| 379 | avail = data_disk_free(data_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 380 | 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 Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 387 | // 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // Search for other users and add any cache files from them. |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 394 | std::string _tmpdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX); |
| 395 | strcpy(tmpdir, _tmpdir.c_str()); |
| 396 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 397 | 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 Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 448 | clear_cache_files(data_path, cache, free_size); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 449 | finish_cache_collection(cache); |
| 450 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 451 | return data_disk_free(data_path) >= free_size ? 0 : -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 454 | 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] | 455 | { |
| 456 | char src_dex[PKG_PATH_MAX]; |
| 457 | char dst_dex[PKG_PATH_MAX]; |
| 458 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 459 | 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 467 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 468 | if (create_cache_path(src_dex, src, instruction_set)) return -1; |
| 469 | if (create_cache_path(dst_dex, dst, instruction_set)) return -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 470 | |
| 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 Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 480 | int rm_dex(const char *path, const char *instruction_set) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 481 | { |
| 482 | char dex_path[PKG_PATH_MAX]; |
| 483 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 484 | 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 Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 489 | if (create_cache_path(dex_path, path, instruction_set)) return -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 490 | |
| 491 | ALOGV("unlink %s\n", dex_path); |
| 492 | if (unlink(dex_path) < 0) { |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 493 | if (errno != ENOENT) { |
| 494 | ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno)); |
| 495 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 496 | return -1; |
| 497 | } else { |
| 498 | return 0; |
| 499 | } |
| 500 | } |
| 501 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 502 | int get_size(const char *uuid, const char *pkgname, int userid, const char *apkpath, |
Dianne Hackborn | 8b41780 | 2013-05-01 18:55:10 -0700 | [diff] [blame] | 503 | const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 504 | const char *instruction_set, int64_t *_codesize, int64_t *_datasize, |
| 505 | int64_t *_cachesize, int64_t* _asecsize) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 506 | { |
| 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 Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 518 | /* count the source apk as code -- but only if it's not |
| 519 | * on the /system partition and its not on the sdcard. */ |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 520 | 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 Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 524 | 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 534 | |
| 535 | /* count the forward locked apk as code if it is given */ |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 536 | if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') { |
| 537 | if (stat(fwdlock_apkpath, &s) == 0) { |
| 538 | codesize += stat_size(&s); |
| 539 | } |
| 540 | } |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 541 | |
| 542 | /* count the cached dexfile as code */ |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 543 | if (!create_cache_path(path, apkpath, instruction_set)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 544 | if (stat(path, &s) == 0) { |
| 545 | codesize += stat_size(&s); |
| 546 | } |
| 547 | } |
| 548 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 549 | /* add in size of any libraries */ |
Dianne Hackborn | 8b41780 | 2013-05-01 18:55:10 -0700 | [diff] [blame] | 550 | if (libdirpath != NULL && libdirpath[0] != '!') { |
| 551 | d = opendir(libdirpath); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 552 | if (d != NULL) { |
| 553 | dfd = dirfd(d); |
| 554 | codesize += calculate_dir_size(dfd); |
| 555 | closedir(d); |
| 556 | } |
| 557 | } |
| 558 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 559 | /* compute asec size if it is given */ |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 560 | if (asecpath != NULL && asecpath[0] != '!') { |
| 561 | if (stat(asecpath, &s) == 0) { |
| 562 | asecsize += stat_size(&s); |
| 563 | } |
| 564 | } |
| 565 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 566 | std::vector<userid_t> users; |
| 567 | if (userid == -1) { |
| 568 | users = get_known_users(uuid); |
| 569 | } else { |
| 570 | users.push_back(userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 571 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 572 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 573 | for (auto user : users) { |
| 574 | std::string _pkgdir(create_data_user_package_path(uuid, user, pkgname)); |
| 575 | const char* pkgdir = _pkgdir.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 576 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 577 | 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 Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 621 | } else { |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 622 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 623 | datasize += stat_size(&s); |
| 624 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 627 | closedir(d); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 628 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 629 | *_codesize = codesize; |
| 630 | *_datasize = datasize; |
| 631 | *_cachesize = cachesize; |
| 632 | *_asecsize = asecsize; |
| 633 | return 0; |
| 634 | } |
| 635 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 636 | 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] | 637 | { |
| 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 Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 653 | dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) + |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 654 | strlen(instruction_set) + |
| 655 | strlen(DALVIK_CACHE_POSTFIX) + 2; |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 656 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 657 | if (dstlen > PKG_PATH_MAX) { |
| 658 | return -1; |
| 659 | } |
| 660 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 661 | sprintf(path,"%s%s/%s%s", |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 662 | DALVIK_CACHE_PREFIX, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 663 | instruction_set, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 664 | src + 1, /* skip the leading / */ |
| 665 | DALVIK_CACHE_POSTFIX); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 666 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 667 | 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] | 668 | if (*tmp == '/') { |
| 669 | *tmp = '@'; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 676 | static 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.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 693 | static int split(char *buf, const char **argv) |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 694 | { |
| 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 Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 708 | 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] | 709 | 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] | 710 | { |
| 711 | static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 712 | static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 713 | |
| 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 Light | a7915d4 | 2014-08-11 10:07:02 -0700 | [diff] [blame] | 731 | 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] | 732 | 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 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 748 | 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] | 749 | 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] | 750 | bool vm_safe_mode, bool debuggable) |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 751 | { |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 752 | static const unsigned int MAX_INSTRUCTION_SET_LEN = 7; |
| 753 | |
| 754 | if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) { |
| 755 | ALOGE("Instruction set %s longer than max length of %d", |
| 756 | instruction_set, MAX_INSTRUCTION_SET_LEN); |
| 757 | return; |
| 758 | } |
| 759 | |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 760 | char prop_buf[PROPERTY_VALUE_MAX]; |
| 761 | bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1'); |
| 762 | |
| 763 | char dex2oat_Xms_flag[PROPERTY_VALUE_MAX]; |
| 764 | bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0; |
| 765 | |
| 766 | char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX]; |
| 767 | bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0; |
| 768 | |
Brian Carlstrom | cf51ba1 | 2014-07-28 19:13:28 -0700 | [diff] [blame] | 769 | char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX]; |
| 770 | bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter", |
| 771 | dex2oat_compiler_filter_flag, NULL) > 0; |
| 772 | |
Andreas Gampe | 8d7af8b | 2015-03-30 18:45:03 -0700 | [diff] [blame] | 773 | char dex2oat_threads_buf[PROPERTY_VALUE_MAX]; |
| 774 | bool have_dex2oat_threads_flag = property_get("dalvik.vm.dex2oat-threads", dex2oat_threads_buf, |
| 775 | NULL) > 0; |
| 776 | char dex2oat_threads_arg[PROPERTY_VALUE_MAX + 2]; |
| 777 | if (have_dex2oat_threads_flag) { |
| 778 | sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf); |
| 779 | } |
| 780 | |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 781 | char dex2oat_isa_features_key[PROPERTY_KEY_MAX]; |
| 782 | sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set); |
| 783 | char dex2oat_isa_features[PROPERTY_VALUE_MAX]; |
| 784 | bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key, |
| 785 | dex2oat_isa_features, NULL) > 0; |
| 786 | |
Ian Rogers | 16a95b2 | 2014-11-08 16:58:13 -0800 | [diff] [blame] | 787 | char dex2oat_isa_variant_key[PROPERTY_KEY_MAX]; |
| 788 | sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set); |
| 789 | char dex2oat_isa_variant[PROPERTY_VALUE_MAX]; |
| 790 | bool have_dex2oat_isa_variant = property_get(dex2oat_isa_variant_key, |
| 791 | dex2oat_isa_variant, NULL) > 0; |
| 792 | |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 793 | const char *dex2oat_norelocation = "-Xnorelocate"; |
| 794 | bool have_dex2oat_relocation_skip_flag = false; |
| 795 | |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 796 | char dex2oat_flags[PROPERTY_VALUE_MAX]; |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 797 | int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags", |
| 798 | dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags); |
Brian Carlstrom | 0ae8e39 | 2014-02-10 16:42:52 -0800 | [diff] [blame] | 799 | ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags); |
| 800 | |
Brian Carlstrom | 538998f | 2014-07-30 14:37:11 -0700 | [diff] [blame] | 801 | // If we booting without the real /data, don't spend time compiling. |
| 802 | char vold_decrypt[PROPERTY_VALUE_MAX]; |
| 803 | bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0; |
| 804 | bool skip_compilation = (have_vold_decrypt && |
| 805 | (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 || |
| 806 | (strcmp(vold_decrypt, "1") == 0))); |
| 807 | |
Mathieu Chartier | d4a7b45 | 2015-03-20 15:39:47 -0700 | [diff] [blame] | 808 | char use_jit_property[PROPERTY_VALUE_MAX]; |
| 809 | bool have_jit_property = property_get("debug.usejit", use_jit_property, NULL) > 0; |
| 810 | bool use_jit = have_jit_property && strcmp(use_jit_property, "true") == 0; |
| 811 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 812 | static const char* DEX2OAT_BIN = "/system/bin/dex2oat"; |
Brian Carlstrom | 53e0776 | 2014-06-27 14:15:19 -0700 | [diff] [blame] | 813 | |
Brian Carlstrom | 53e0776 | 2014-06-27 14:15:19 -0700 | [diff] [blame] | 814 | static const char* RUNTIME_ARG = "--runtime-arg"; |
Brian Carlstrom | 53e0776 | 2014-06-27 14:15:19 -0700 | [diff] [blame] | 815 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 816 | static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 817 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 818 | char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN]; |
| 819 | char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX]; |
| 820 | char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN]; |
Brian Carlstrom | 7195fcc | 2014-06-16 13:28:03 -0700 | [diff] [blame] | 821 | char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX]; |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 822 | char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN]; |
Ian Rogers | 16a95b2 | 2014-11-08 16:58:13 -0800 | [diff] [blame] | 823 | char instruction_set_variant_arg[strlen("--instruction-set-variant=") + PROPERTY_VALUE_MAX]; |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 824 | char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX]; |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 825 | char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX]; |
| 826 | 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] | 827 | char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX]; |
| 828 | char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX]; |
Brian Carlstrom | cf51ba1 | 2014-07-28 19:13:28 -0700 | [diff] [blame] | 829 | char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX]; |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 830 | bool have_dex2oat_swap_fd = false; |
| 831 | char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN]; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 832 | |
| 833 | sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd); |
| 834 | sprintf(zip_location_arg, "--zip-location=%s", input_file_name); |
| 835 | sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd); |
| 836 | sprintf(oat_location_arg, "--oat-location=%s", output_file_name); |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 837 | sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set); |
Ian Rogers | 16a95b2 | 2014-11-08 16:58:13 -0800 | [diff] [blame] | 838 | sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant); |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 839 | sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features); |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 840 | if (swap_fd >= 0) { |
| 841 | have_dex2oat_swap_fd = true; |
| 842 | sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd); |
| 843 | } |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 844 | |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 845 | bool have_profile_file = false; |
| 846 | bool have_top_k_profile_threshold = false; |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 847 | if (profiler && (strcmp(pkgname, "*") != 0)) { |
| 848 | char profile_file[PKG_PATH_MAX]; |
| 849 | snprintf(profile_file, sizeof(profile_file), "%s/%s", |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 850 | DALVIK_CACHE_PREFIX "profiles", pkgname); |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 851 | struct stat st; |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 852 | if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) { |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 853 | sprintf(profile_file_arg, "--profile-file=%s", profile_file); |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 854 | have_profile_file = true; |
| 855 | if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) { |
| 856 | snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg), |
| 857 | "--top-k-profile-threshold=%s", prop_buf); |
| 858 | have_top_k_profile_threshold = true; |
| 859 | } |
Calin Juravle | 57c69c3 | 2014-06-06 14:42:16 +0100 | [diff] [blame] | 860 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 861 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 862 | |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 863 | if (have_dex2oat_Xms_flag) { |
| 864 | sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag); |
| 865 | } |
| 866 | if (have_dex2oat_Xmx_flag) { |
| 867 | sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag); |
| 868 | } |
Brian Carlstrom | 538998f | 2014-07-30 14:37:11 -0700 | [diff] [blame] | 869 | if (skip_compilation) { |
Brian Carlstrom | e18987e | 2014-08-15 09:55:50 -0700 | [diff] [blame] | 870 | strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none"); |
Brian Carlstrom | 538998f | 2014-07-30 14:37:11 -0700 | [diff] [blame] | 871 | have_dex2oat_compiler_filter_flag = true; |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 872 | have_dex2oat_relocation_skip_flag = true; |
Calin Juravle | b1efac1 | 2014-08-21 19:05:20 +0100 | [diff] [blame] | 873 | } else if (vm_safe_mode) { |
| 874 | strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only"); |
Calin Juravle | 97477d2 | 2014-08-27 16:10:03 +0100 | [diff] [blame] | 875 | have_dex2oat_compiler_filter_flag = true; |
Mathieu Chartier | d4a7b45 | 2015-03-20 15:39:47 -0700 | [diff] [blame] | 876 | } else if (use_jit) { |
| 877 | strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-at-runtime"); |
| 878 | have_dex2oat_compiler_filter_flag = true; |
Brian Carlstrom | 538998f | 2014-07-30 14:37:11 -0700 | [diff] [blame] | 879 | } else if (have_dex2oat_compiler_filter_flag) { |
Brian Carlstrom | cf51ba1 | 2014-07-28 19:13:28 -0700 | [diff] [blame] | 880 | sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag); |
| 881 | } |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 882 | |
Andreas Gampe | 598c25e | 2015-03-03 09:15:06 -0800 | [diff] [blame] | 883 | // Check whether all apps should be compiled debuggable. |
| 884 | if (!debuggable) { |
| 885 | debuggable = |
| 886 | (property_get("dalvik.vm.always_debuggable", prop_buf, "0") > 0) && |
| 887 | (prop_buf[0] == '1'); |
| 888 | } |
| 889 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 890 | 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] | 891 | |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 892 | const char* argv[7 // program name, mandatory arguments and the final NULL |
| 893 | + (have_dex2oat_isa_variant ? 1 : 0) |
| 894 | + (have_dex2oat_isa_features ? 1 : 0) |
| 895 | + (have_profile_file ? 1 : 0) |
| 896 | + (have_top_k_profile_threshold ? 1 : 0) |
| 897 | + (have_dex2oat_Xms_flag ? 2 : 0) |
| 898 | + (have_dex2oat_Xmx_flag ? 2 : 0) |
| 899 | + (have_dex2oat_compiler_filter_flag ? 1 : 0) |
Andreas Gampe | 8d7af8b | 2015-03-30 18:45:03 -0700 | [diff] [blame] | 900 | + (have_dex2oat_threads_flag ? 1 : 0) |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 901 | + (have_dex2oat_swap_fd ? 1 : 0) |
| 902 | + (have_dex2oat_relocation_skip_flag ? 2 : 0) |
Andreas Gampe | 598c25e | 2015-03-03 09:15:06 -0800 | [diff] [blame] | 903 | + (debuggable ? 1 : 0) |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 904 | + dex2oat_flags_count]; |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 905 | int i = 0; |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 906 | argv[i++] = DEX2OAT_BIN; |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 907 | argv[i++] = zip_fd_arg; |
| 908 | argv[i++] = zip_location_arg; |
| 909 | argv[i++] = oat_fd_arg; |
| 910 | argv[i++] = oat_location_arg; |
| 911 | argv[i++] = instruction_set_arg; |
Ian Rogers | 16a95b2 | 2014-11-08 16:58:13 -0800 | [diff] [blame] | 912 | if (have_dex2oat_isa_variant) { |
| 913 | argv[i++] = instruction_set_variant_arg; |
| 914 | } |
Calin Juravle | 8fc7315 | 2014-08-19 18:48:50 +0100 | [diff] [blame] | 915 | if (have_dex2oat_isa_features) { |
| 916 | argv[i++] = instruction_set_features_arg; |
| 917 | } |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 918 | if (have_profile_file) { |
| 919 | argv[i++] = profile_file_arg; |
| 920 | } |
| 921 | if (have_top_k_profile_threshold) { |
| 922 | argv[i++] = top_k_profile_threshold_arg; |
| 923 | } |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 924 | if (have_dex2oat_Xms_flag) { |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 925 | argv[i++] = RUNTIME_ARG; |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 926 | argv[i++] = dex2oat_Xms_arg; |
| 927 | } |
| 928 | if (have_dex2oat_Xmx_flag) { |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 929 | argv[i++] = RUNTIME_ARG; |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 930 | argv[i++] = dex2oat_Xmx_arg; |
| 931 | } |
Brian Carlstrom | cf51ba1 | 2014-07-28 19:13:28 -0700 | [diff] [blame] | 932 | if (have_dex2oat_compiler_filter_flag) { |
| 933 | argv[i++] = dex2oat_compiler_filter_arg; |
| 934 | } |
Andreas Gampe | 8d7af8b | 2015-03-30 18:45:03 -0700 | [diff] [blame] | 935 | if (have_dex2oat_threads_flag) { |
| 936 | argv[i++] = dex2oat_threads_arg; |
| 937 | } |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 938 | if (have_dex2oat_swap_fd) { |
| 939 | argv[i++] = dex2oat_swap_fd; |
| 940 | } |
Andreas Gampe | 598c25e | 2015-03-03 09:15:06 -0800 | [diff] [blame] | 941 | if (debuggable) { |
| 942 | argv[i++] = "--debuggable"; |
| 943 | } |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 944 | if (dex2oat_flags_count) { |
| 945 | i += split(dex2oat_flags, argv + i); |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 946 | } |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 947 | if (have_dex2oat_relocation_skip_flag) { |
| 948 | argv[i++] = RUNTIME_ARG; |
| 949 | argv[i++] = dex2oat_norelocation; |
| 950 | } |
Brian Carlstrom | e46a75a | 2014-06-27 16:03:06 -0700 | [diff] [blame] | 951 | // Do not add after dex2oat_flags, they should override others for debugging. |
Calin Juravle | 4fdff46 | 2014-06-06 16:58:43 +0100 | [diff] [blame] | 952 | argv[i] = NULL; |
| 953 | |
neo.chae | 14e084d | 2015-01-07 18:46:13 +0900 | [diff] [blame] | 954 | execv(DEX2OAT_BIN, (char * const *)argv); |
Yevgeny Rouban | b0d8d00 | 2014-09-08 17:02:10 +0700 | [diff] [blame] | 955 | ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno)); |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 956 | } |
| 957 | |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 958 | static int wait_child(pid_t pid) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 959 | { |
| 960 | int status; |
| 961 | pid_t got_pid; |
| 962 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 963 | while (1) { |
| 964 | got_pid = waitpid(pid, &status, 0); |
| 965 | if (got_pid == -1 && errno == EINTR) { |
| 966 | printf("waitpid interrupted, retrying\n"); |
| 967 | } else { |
| 968 | break; |
| 969 | } |
| 970 | } |
| 971 | if (got_pid != pid) { |
| 972 | ALOGW("waitpid failed: wanted %d, got %d: %s\n", |
| 973 | (int) pid, (int) got_pid, strerror(errno)); |
| 974 | return 1; |
| 975 | } |
| 976 | |
| 977 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 978 | return 0; |
| 979 | } else { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 980 | return status; /* always nonzero */ |
| 981 | } |
| 982 | } |
| 983 | |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 984 | /* |
| 985 | * Whether dexopt should use a swap file when compiling an APK. If kAlwaysProvideSwapFile, do this |
| 986 | * on all devices (dex2oat will make a more informed decision itself, anyways). Otherwise, only do |
| 987 | * this on a low-mem device. |
| 988 | */ |
| 989 | static bool kAlwaysProvideSwapFile = true; |
| 990 | |
| 991 | static bool ShouldUseSwapFileForDexopt() { |
| 992 | if (kAlwaysProvideSwapFile) { |
| 993 | return true; |
| 994 | } |
| 995 | |
| 996 | char low_mem_buf[PROPERTY_VALUE_MAX]; |
| 997 | property_get("ro.config.low_ram", low_mem_buf, ""); |
| 998 | return (strcmp(low_mem_buf, "true") == 0); |
| 999 | } |
| 1000 | |
Richard Uhler | 009b877 | 2015-03-18 12:39:09 -0700 | [diff] [blame] | 1001 | /* |
| 1002 | * Computes the odex file for the given apk_path and instruction_set. |
| 1003 | * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex |
| 1004 | * |
| 1005 | * Returns false if it failed to determine the odex file path. |
| 1006 | */ |
| 1007 | static bool calculate_odex_file_path(char path[PKG_PATH_MAX], |
| 1008 | const char *apk_path, |
| 1009 | const char *instruction_set) |
| 1010 | { |
| 1011 | if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set) |
| 1012 | + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) { |
| 1013 | ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path); |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | strcpy(path, apk_path); |
| 1018 | char *end = strrchr(path, '/'); |
| 1019 | if (end == NULL) { |
| 1020 | ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path); |
| 1021 | return false; |
| 1022 | } |
| 1023 | const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/'); |
| 1024 | |
| 1025 | strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0 |
| 1026 | strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0 |
| 1027 | strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0 |
| 1028 | end = strrchr(path, '.'); |
| 1029 | if (end == NULL) { |
| 1030 | ALOGE("apk_path '%s' has no extension.\n", apk_path); |
| 1031 | return false; |
| 1032 | } |
| 1033 | strcpy(end + 1, "odex"); |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
Calin Juravle | b1efac1 | 2014-08-21 19:05:20 +0100 | [diff] [blame] | 1037 | int dexopt(const char *apk_path, uid_t uid, bool is_public, |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1038 | const char *pkgname, const char *instruction_set, int dexopt_needed, |
| 1039 | bool vm_safe_mode, bool debuggable, const char* oat_dir) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1040 | { |
| 1041 | struct utimbuf ut; |
Fyodor Kupolov | 26ff93c | 2015-04-02 16:59:10 -0700 | [diff] [blame] | 1042 | struct stat input_stat; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1043 | char out_path[PKG_PATH_MAX]; |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 1044 | char swap_file_name[PKG_PATH_MAX]; |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1045 | const char *input_file; |
| 1046 | char in_odex_path[PKG_PATH_MAX]; |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 1047 | int res, input_fd=-1, out_fd=-1, swap_fd=-1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1048 | |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 1049 | // Early best-effort check whether we can fit the the path into our buffers. |
| 1050 | // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run |
| 1051 | // without a swap file, if necessary. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1052 | if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) { |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1053 | ALOGE("apk_path too long '%s'\n", apk_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1054 | return -1; |
| 1055 | } |
| 1056 | |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1057 | if (oat_dir != NULL && oat_dir[0] != '!') { |
| 1058 | if (validate_apk_path(oat_dir)) { |
| 1059 | ALOGE("invalid oat_dir '%s'\n", oat_dir); |
| 1060 | return -1; |
Chih-Wei Huang | 0e8ae16 | 2014-04-28 15:47:45 +0800 | [diff] [blame] | 1061 | } |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1062 | if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) { |
| 1063 | return -1; |
| 1064 | } |
| 1065 | } else { |
| 1066 | if (create_cache_path(out_path, apk_path, instruction_set)) { |
| 1067 | return -1; |
| 1068 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1071 | switch (dexopt_needed) { |
| 1072 | case DEXOPT_DEX2OAT_NEEDED: |
| 1073 | input_file = apk_path; |
| 1074 | break; |
| 1075 | |
| 1076 | case DEXOPT_PATCHOAT_NEEDED: |
| 1077 | if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) { |
| 1078 | return -1; |
| 1079 | } |
| 1080 | input_file = in_odex_path; |
| 1081 | break; |
| 1082 | |
| 1083 | case DEXOPT_SELF_PATCHOAT_NEEDED: |
| 1084 | input_file = out_path; |
| 1085 | break; |
| 1086 | |
| 1087 | default: |
| 1088 | ALOGE("Invalid dexopt needed: %d\n", dexopt_needed); |
| 1089 | exit(72); |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1090 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1091 | |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1092 | memset(&input_stat, 0, sizeof(input_stat)); |
| 1093 | stat(input_file, &input_stat); |
| 1094 | |
| 1095 | input_fd = open(input_file, O_RDONLY, 0); |
| 1096 | if (input_fd < 0) { |
| 1097 | ALOGE("installd cannot open '%s' for input during dexopt\n", input_file); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1098 | return -1; |
| 1099 | } |
| 1100 | |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1101 | unlink(out_path); |
| 1102 | out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644); |
| 1103 | if (out_fd < 0) { |
| 1104 | ALOGE("installd cannot open '%s' for output during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1105 | goto fail; |
| 1106 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1107 | if (fchmod(out_fd, |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1108 | S_IRUSR|S_IWUSR|S_IRGRP | |
| 1109 | (is_public ? S_IROTH : 0)) < 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1110 | ALOGE("installd cannot chmod '%s' during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1111 | goto fail; |
| 1112 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1113 | if (fchown(out_fd, AID_SYSTEM, uid) < 0) { |
| 1114 | ALOGE("installd cannot chown '%s' during dexopt\n", out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1115 | goto fail; |
| 1116 | } |
| 1117 | |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1118 | // Create profile file if there is a package name present. |
| 1119 | if (strcmp(pkgname, "*") != 0) { |
| 1120 | create_profile_file(pkgname, uid); |
| 1121 | } |
| 1122 | |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 1123 | // Create a swap file if necessary. |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1124 | if (ShouldUseSwapFileForDexopt()) { |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 1125 | // Make sure there really is enough space. |
| 1126 | size_t out_len = strlen(out_path); |
| 1127 | if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) { |
| 1128 | strcpy(swap_file_name, out_path); |
| 1129 | strcpy(swap_file_name + strlen(out_path), ".swap"); |
| 1130 | unlink(swap_file_name); |
| 1131 | swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600); |
| 1132 | if (swap_fd < 0) { |
| 1133 | // Could not create swap file. Optimistically go on and hope that we can compile |
| 1134 | // without it. |
| 1135 | ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name); |
| 1136 | } else { |
| 1137 | // Immediately unlink. We don't really want to hit flash. |
| 1138 | unlink(swap_file_name); |
| 1139 | } |
| 1140 | } else { |
| 1141 | // Swap file path is too long. Try to run without. |
| 1142 | ALOGE("installd could not create swap file for path %s during dexopt\n", out_path); |
| 1143 | } |
| 1144 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1145 | |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1146 | ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1147 | |
| 1148 | pid_t pid; |
| 1149 | pid = fork(); |
| 1150 | if (pid == 0) { |
| 1151 | /* child -- drop privileges before continuing */ |
| 1152 | if (setgid(uid) != 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1153 | ALOGE("setgid(%d) failed in installd during dexopt\n", uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1154 | exit(64); |
| 1155 | } |
| 1156 | if (setuid(uid) != 0) { |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1157 | ALOGE("setuid(%d) failed in installd during dexopt\n", uid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1158 | exit(65); |
| 1159 | } |
| 1160 | // drop capabilities |
| 1161 | struct __user_cap_header_struct capheader; |
| 1162 | struct __user_cap_data_struct capdata[2]; |
| 1163 | memset(&capheader, 0, sizeof(capheader)); |
| 1164 | memset(&capdata, 0, sizeof(capdata)); |
| 1165 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 1166 | if (capset(&capheader, &capdata[0]) < 0) { |
| 1167 | ALOGE("capset failed: %s\n", strerror(errno)); |
| 1168 | exit(66); |
| 1169 | } |
Brian Carlstrom | 0378aaf | 2014-08-08 00:52:22 -0700 | [diff] [blame] | 1170 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 1171 | ALOGE("set_sched_policy failed: %s\n", strerror(errno)); |
| 1172 | exit(70); |
| 1173 | } |
Igor Murashkin | 9e87a80 | 2014-11-05 15:21:12 -0800 | [diff] [blame] | 1174 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { |
| 1175 | ALOGE("setpriority failed: %s\n", strerror(errno)); |
| 1176 | exit(71); |
| 1177 | } |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1178 | if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) { |
| 1179 | ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1180 | exit(67); |
| 1181 | } |
| 1182 | |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1183 | if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED |
| 1184 | || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) { |
Andreas Gampe | bd872e4 | 2014-12-15 11:41:11 -0800 | [diff] [blame] | 1185 | 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] | 1186 | } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) { |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1187 | const char *input_file_name = strrchr(input_file, '/'); |
| 1188 | if (input_file_name == NULL) { |
| 1189 | input_file_name = input_file; |
| 1190 | } else { |
| 1191 | input_file_name++; |
| 1192 | } |
| 1193 | run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname, |
| 1194 | instruction_set, vm_safe_mode, debuggable); |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 1195 | } else { |
| 1196 | ALOGE("Invalid dexopt needed: %d\n", dexopt_needed); |
| 1197 | exit(73); |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1198 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1199 | exit(68); /* only get here on exec failure */ |
| 1200 | } else { |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1201 | res = wait_child(pid); |
| 1202 | if (res == 0) { |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1203 | ALOGV("DexInv: --- END '%s' (success) ---\n", input_file); |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1204 | } else { |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1205 | 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] | 1206 | goto fail; |
| 1207 | } |
| 1208 | } |
| 1209 | |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1210 | ut.actime = input_stat.st_atime; |
| 1211 | ut.modtime = input_stat.st_mtime; |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1212 | utime(out_path, &ut); |
| 1213 | |
| 1214 | close(out_fd); |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1215 | close(input_fd); |
Andreas Gampe | e1c0135 | 2014-12-10 16:41:11 -0800 | [diff] [blame] | 1216 | if (swap_fd != -1) { |
| 1217 | close(swap_fd); |
| 1218 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1219 | return 0; |
| 1220 | |
| 1221 | fail: |
Brian Carlstrom | 1705fc4 | 2013-03-21 18:20:22 -0700 | [diff] [blame] | 1222 | if (out_fd >= 0) { |
| 1223 | close(out_fd); |
| 1224 | unlink(out_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1225 | } |
Alex Light | 7365a10 | 2014-07-21 12:23:48 -0700 | [diff] [blame] | 1226 | if (input_fd >= 0) { |
| 1227 | close(input_fd); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1228 | } |
| 1229 | return -1; |
| 1230 | } |
| 1231 | |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 1232 | int mark_boot_complete(const char* instruction_set) |
| 1233 | { |
| 1234 | char boot_marker_path[PKG_PATH_MAX]; |
| 1235 | sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set); |
| 1236 | |
| 1237 | ALOGV("mark_boot_complete : %s", boot_marker_path); |
| 1238 | if (unlink(boot_marker_path) != 0) { |
| 1239 | ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path, |
| 1240 | strerror(errno)); |
| 1241 | return -1; |
| 1242 | } |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1247 | void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, |
| 1248 | struct stat* statbuf) |
| 1249 | { |
| 1250 | while (path[basepos] != 0) { |
| 1251 | if (path[basepos] == '/') { |
| 1252 | path[basepos] = 0; |
| 1253 | if (lstat(path, statbuf) < 0) { |
| 1254 | ALOGV("Making directory: %s\n", path); |
| 1255 | if (mkdir(path, mode) == 0) { |
| 1256 | chown(path, uid, gid); |
| 1257 | } else { |
| 1258 | ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); |
| 1259 | } |
| 1260 | } |
| 1261 | path[basepos] = '/'; |
| 1262 | basepos++; |
| 1263 | } |
| 1264 | basepos++; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | int movefileordir(char* srcpath, char* dstpath, int dstbasepos, |
| 1269 | int dstuid, int dstgid, struct stat* statbuf) |
| 1270 | { |
| 1271 | DIR *d; |
| 1272 | struct dirent *de; |
| 1273 | int res; |
| 1274 | |
| 1275 | int srcend = strlen(srcpath); |
| 1276 | int dstend = strlen(dstpath); |
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 | if (lstat(srcpath, statbuf) < 0) { |
| 1279 | ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno)); |
| 1280 | return 1; |
| 1281 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1282 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1283 | if ((statbuf->st_mode&S_IFDIR) == 0) { |
| 1284 | mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH, |
| 1285 | dstuid, dstgid, statbuf); |
| 1286 | ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid); |
| 1287 | if (rename(srcpath, dstpath) >= 0) { |
| 1288 | if (chown(dstpath, dstuid, dstgid) < 0) { |
| 1289 | ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno)); |
| 1290 | unlink(dstpath); |
| 1291 | return 1; |
| 1292 | } |
| 1293 | } else { |
| 1294 | ALOGW("Unable to rename %s to %s: %s\n", |
| 1295 | srcpath, dstpath, strerror(errno)); |
| 1296 | return 1; |
| 1297 | } |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | d = opendir(srcpath); |
| 1302 | if (d == NULL) { |
| 1303 | ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno)); |
| 1304 | return 1; |
| 1305 | } |
| 1306 | |
| 1307 | res = 0; |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1308 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1309 | while ((de = readdir(d))) { |
| 1310 | const char *name = de->d_name; |
| 1311 | /* always skip "." and ".." */ |
| 1312 | if (name[0] == '.') { |
| 1313 | if (name[1] == 0) continue; |
| 1314 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 1315 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1316 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1317 | if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
| 1318 | ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name); |
| 1319 | continue; |
| 1320 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1321 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1322 | if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
| 1323 | ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name); |
| 1324 | continue; |
| 1325 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1326 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1327 | srcpath[srcend] = dstpath[dstend] = '/'; |
| 1328 | strcpy(srcpath+srcend+1, name); |
| 1329 | strcpy(dstpath+dstend+1, name); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1330 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1331 | if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) { |
| 1332 | res = 1; |
| 1333 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1334 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1335 | // Note: we will be leaving empty directories behind in srcpath, |
| 1336 | // but that is okay, the package manager will be erasing all of the |
| 1337 | // data associated with .apks that disappear. |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1338 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1339 | srcpath[srcend] = dstpath[dstend] = 0; |
| 1340 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1341 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1342 | closedir(d); |
| 1343 | return res; |
| 1344 | } |
| 1345 | |
| 1346 | int movefiles() |
| 1347 | { |
| 1348 | DIR *d; |
| 1349 | int dfd, subfd; |
| 1350 | struct dirent *de; |
| 1351 | struct stat s; |
| 1352 | char buf[PKG_PATH_MAX+1]; |
| 1353 | int bufp, bufe, bufi, readlen; |
| 1354 | |
| 1355 | char srcpkg[PKG_NAME_MAX]; |
| 1356 | char dstpkg[PKG_NAME_MAX]; |
| 1357 | char srcpath[PKG_PATH_MAX]; |
| 1358 | char dstpath[PKG_PATH_MAX]; |
| 1359 | int dstuid=-1, dstgid=-1; |
| 1360 | int hasspace; |
| 1361 | |
| 1362 | d = opendir(UPDATE_COMMANDS_DIR_PREFIX); |
| 1363 | if (d == NULL) { |
| 1364 | goto done; |
| 1365 | } |
| 1366 | dfd = dirfd(d); |
| 1367 | |
| 1368 | /* Iterate through all files in the directory, executing the |
| 1369 | * file movements requested there-in. |
| 1370 | */ |
| 1371 | while ((de = readdir(d))) { |
| 1372 | const char *name = de->d_name; |
| 1373 | |
| 1374 | if (de->d_type == DT_DIR) { |
| 1375 | continue; |
| 1376 | } else { |
| 1377 | subfd = openat(dfd, name, O_RDONLY); |
| 1378 | if (subfd < 0) { |
| 1379 | ALOGW("Unable to open update commands at %s%s\n", |
| 1380 | UPDATE_COMMANDS_DIR_PREFIX, name); |
| 1381 | continue; |
| 1382 | } |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 1383 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1384 | bufp = 0; |
| 1385 | bufe = 0; |
| 1386 | buf[PKG_PATH_MAX] = 0; |
| 1387 | srcpkg[0] = dstpkg[0] = 0; |
| 1388 | while (1) { |
| 1389 | bufi = bufp; |
| 1390 | while (bufi < bufe && buf[bufi] != '\n') { |
| 1391 | bufi++; |
| 1392 | } |
| 1393 | if (bufi < bufe) { |
| 1394 | buf[bufi] = 0; |
| 1395 | ALOGV("Processing line: %s\n", buf+bufp); |
| 1396 | hasspace = 0; |
| 1397 | while (bufp < bufi && isspace(buf[bufp])) { |
| 1398 | hasspace = 1; |
| 1399 | bufp++; |
| 1400 | } |
| 1401 | if (buf[bufp] == '#' || bufp == bufi) { |
| 1402 | // skip comments and empty lines. |
| 1403 | } else if (hasspace) { |
| 1404 | if (dstpkg[0] == 0) { |
| 1405 | ALOGW("Path before package line in %s%s: %s\n", |
| 1406 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 1407 | } else if (srcpkg[0] == 0) { |
| 1408 | // Skip -- source package no longer exists. |
| 1409 | } else { |
| 1410 | ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg); |
| 1411 | if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) && |
| 1412 | !create_move_path(dstpath, dstpkg, buf+bufp, 0)) { |
| 1413 | movefileordir(srcpath, dstpath, |
| 1414 | strlen(dstpath)-strlen(buf+bufp), |
| 1415 | dstuid, dstgid, &s); |
| 1416 | } |
| 1417 | } |
| 1418 | } else { |
| 1419 | char* div = strchr(buf+bufp, ':'); |
| 1420 | if (div == NULL) { |
| 1421 | ALOGW("Bad package spec in %s%s; no ':' sep: %s\n", |
| 1422 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 1423 | } else { |
| 1424 | *div = 0; |
| 1425 | div++; |
| 1426 | if (strlen(buf+bufp) < PKG_NAME_MAX) { |
| 1427 | strcpy(dstpkg, buf+bufp); |
| 1428 | } else { |
| 1429 | srcpkg[0] = dstpkg[0] = 0; |
| 1430 | ALOGW("Package name too long in %s%s: %s\n", |
| 1431 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 1432 | } |
| 1433 | if (strlen(div) < PKG_NAME_MAX) { |
| 1434 | strcpy(srcpkg, div); |
| 1435 | } else { |
| 1436 | srcpkg[0] = dstpkg[0] = 0; |
| 1437 | ALOGW("Package name too long in %s%s: %s\n", |
| 1438 | UPDATE_COMMANDS_DIR_PREFIX, name, div); |
| 1439 | } |
| 1440 | if (srcpkg[0] != 0) { |
| 1441 | if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) { |
| 1442 | if (lstat(srcpath, &s) < 0) { |
| 1443 | // Package no longer exists -- skip. |
| 1444 | srcpkg[0] = 0; |
| 1445 | } |
| 1446 | } else { |
| 1447 | srcpkg[0] = 0; |
| 1448 | ALOGW("Can't create path %s in %s%s\n", |
| 1449 | div, UPDATE_COMMANDS_DIR_PREFIX, name); |
| 1450 | } |
| 1451 | if (srcpkg[0] != 0) { |
| 1452 | if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) { |
| 1453 | if (lstat(dstpath, &s) == 0) { |
| 1454 | dstuid = s.st_uid; |
| 1455 | dstgid = s.st_gid; |
| 1456 | } else { |
| 1457 | // Destination package doesn't |
| 1458 | // exist... due to original-package, |
| 1459 | // this is normal, so don't be |
| 1460 | // noisy about it. |
| 1461 | srcpkg[0] = 0; |
| 1462 | } |
| 1463 | } else { |
| 1464 | srcpkg[0] = 0; |
| 1465 | ALOGW("Can't create path %s in %s%s\n", |
| 1466 | div, UPDATE_COMMANDS_DIR_PREFIX, name); |
| 1467 | } |
| 1468 | } |
| 1469 | ALOGV("Transfering from %s to %s: uid=%d\n", |
| 1470 | srcpkg, dstpkg, dstuid); |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | bufp = bufi+1; |
| 1475 | } else { |
| 1476 | if (bufp == 0) { |
| 1477 | if (bufp < bufe) { |
| 1478 | ALOGW("Line too long in %s%s, skipping: %s\n", |
| 1479 | UPDATE_COMMANDS_DIR_PREFIX, name, buf); |
| 1480 | } |
| 1481 | } else if (bufp < bufe) { |
| 1482 | memcpy(buf, buf+bufp, bufe-bufp); |
| 1483 | bufe -= bufp; |
| 1484 | bufp = 0; |
| 1485 | } |
| 1486 | readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe); |
| 1487 | if (readlen < 0) { |
| 1488 | ALOGW("Failure reading update commands in %s%s: %s\n", |
| 1489 | UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno)); |
| 1490 | break; |
| 1491 | } else if (readlen == 0) { |
| 1492 | break; |
| 1493 | } |
| 1494 | bufe += readlen; |
| 1495 | buf[bufe] = 0; |
| 1496 | ALOGV("Read buf: %s\n", buf); |
| 1497 | } |
| 1498 | } |
| 1499 | close(subfd); |
| 1500 | } |
| 1501 | } |
| 1502 | closedir(d); |
| 1503 | done: |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 1507 | 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] | 1508 | { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1509 | struct stat s, libStat; |
| 1510 | int rc = 0; |
| 1511 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1512 | std::string _pkgdir(create_data_user_package_path(uuid, userId, pkgname)); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 1513 | std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX); |
| 1514 | |
| 1515 | const char* pkgdir = _pkgdir.c_str(); |
| 1516 | const char* libsymlink = _libsymlink.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1517 | |
| 1518 | if (stat(pkgdir, &s) < 0) return -1; |
| 1519 | |
| 1520 | if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { |
| 1521 | ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno)); |
| 1522 | return -1; |
| 1523 | } |
| 1524 | |
| 1525 | if (chmod(pkgdir, 0700) < 0) { |
| 1526 | ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); |
| 1527 | rc = -1; |
| 1528 | goto out; |
| 1529 | } |
| 1530 | |
| 1531 | if (lstat(libsymlink, &libStat) < 0) { |
| 1532 | if (errno != ENOENT) { |
| 1533 | ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); |
| 1534 | rc = -1; |
| 1535 | goto out; |
| 1536 | } |
| 1537 | } else { |
| 1538 | if (S_ISDIR(libStat.st_mode)) { |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 1539 | if (delete_dir_contents(libsymlink, 1, NULL) < 0) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1540 | rc = -1; |
| 1541 | goto out; |
| 1542 | } |
| 1543 | } else if (S_ISLNK(libStat.st_mode)) { |
| 1544 | if (unlink(libsymlink) < 0) { |
| 1545 | ALOGE("couldn't unlink lib dir: %s\n", strerror(errno)); |
| 1546 | rc = -1; |
| 1547 | goto out; |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | if (symlink(asecLibDir, libsymlink) < 0) { |
| 1553 | ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir, |
| 1554 | strerror(errno)); |
| 1555 | rc = -errno; |
| 1556 | goto out; |
| 1557 | } |
| 1558 | |
| 1559 | out: |
| 1560 | if (chmod(pkgdir, s.st_mode) < 0) { |
| 1561 | ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); |
| 1562 | rc = -errno; |
| 1563 | } |
| 1564 | |
| 1565 | if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { |
| 1566 | ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno)); |
| 1567 | return -errno; |
| 1568 | } |
| 1569 | |
| 1570 | return rc; |
| 1571 | } |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1572 | |
| 1573 | static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) |
| 1574 | { |
| 1575 | static const char *IDMAP_BIN = "/system/bin/idmap"; |
| 1576 | static const size_t MAX_INT_LEN = 32; |
| 1577 | char idmap_str[MAX_INT_LEN]; |
| 1578 | |
| 1579 | snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); |
| 1580 | |
| 1581 | execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); |
| 1582 | ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); |
| 1583 | } |
| 1584 | |
| 1585 | // Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) |
| 1586 | // eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap |
| 1587 | static int flatten_path(const char *prefix, const char *suffix, |
| 1588 | const char *overlay_path, char *idmap_path, size_t N) |
| 1589 | { |
| 1590 | if (overlay_path == NULL || idmap_path == NULL) { |
| 1591 | return -1; |
| 1592 | } |
| 1593 | const size_t len_overlay_path = strlen(overlay_path); |
| 1594 | // will access overlay_path + 1 further below; requires absolute path |
| 1595 | if (len_overlay_path < 2 || *overlay_path != '/') { |
| 1596 | return -1; |
| 1597 | } |
| 1598 | const size_t len_idmap_root = strlen(prefix); |
| 1599 | const size_t len_suffix = strlen(suffix); |
| 1600 | if (SIZE_MAX - len_idmap_root < len_overlay_path || |
| 1601 | SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { |
| 1602 | // additions below would cause overflow |
| 1603 | return -1; |
| 1604 | } |
| 1605 | if (N < len_idmap_root + len_overlay_path + len_suffix) { |
| 1606 | return -1; |
| 1607 | } |
| 1608 | memset(idmap_path, 0, N); |
| 1609 | snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); |
| 1610 | char *ch = idmap_path + len_idmap_root; |
| 1611 | while (*ch != '\0') { |
| 1612 | if (*ch == '/') { |
| 1613 | *ch = '@'; |
| 1614 | } |
| 1615 | ++ch; |
| 1616 | } |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
| 1620 | int idmap(const char *target_apk, const char *overlay_apk, uid_t uid) |
| 1621 | { |
| 1622 | ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); |
| 1623 | |
| 1624 | int idmap_fd = -1; |
| 1625 | char idmap_path[PATH_MAX]; |
| 1626 | |
| 1627 | if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, |
| 1628 | idmap_path, sizeof(idmap_path)) == -1) { |
| 1629 | ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); |
| 1630 | goto fail; |
| 1631 | } |
| 1632 | |
| 1633 | unlink(idmap_path); |
| 1634 | idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); |
| 1635 | if (idmap_fd < 0) { |
| 1636 | ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); |
| 1637 | goto fail; |
| 1638 | } |
| 1639 | if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { |
| 1640 | ALOGE("idmap cannot chown '%s'\n", idmap_path); |
| 1641 | goto fail; |
| 1642 | } |
| 1643 | if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { |
| 1644 | ALOGE("idmap cannot chmod '%s'\n", idmap_path); |
| 1645 | goto fail; |
| 1646 | } |
| 1647 | |
| 1648 | pid_t pid; |
| 1649 | pid = fork(); |
| 1650 | if (pid == 0) { |
| 1651 | /* child -- drop privileges before continuing */ |
| 1652 | if (setgid(uid) != 0) { |
| 1653 | ALOGE("setgid(%d) failed during idmap\n", uid); |
| 1654 | exit(1); |
| 1655 | } |
| 1656 | if (setuid(uid) != 0) { |
| 1657 | ALOGE("setuid(%d) failed during idmap\n", uid); |
| 1658 | exit(1); |
| 1659 | } |
| 1660 | if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { |
| 1661 | ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); |
| 1662 | exit(1); |
| 1663 | } |
| 1664 | |
| 1665 | run_idmap(target_apk, overlay_apk, idmap_fd); |
| 1666 | exit(1); /* only if exec call to idmap failed */ |
| 1667 | } else { |
| 1668 | int status = wait_child(pid); |
| 1669 | if (status != 0) { |
| 1670 | ALOGE("idmap failed, status=0x%04x\n", status); |
| 1671 | goto fail; |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | close(idmap_fd); |
| 1676 | return 0; |
| 1677 | fail: |
| 1678 | if (idmap_fd >= 0) { |
| 1679 | close(idmap_fd); |
| 1680 | unlink(idmap_path); |
| 1681 | } |
| 1682 | return -1; |
| 1683 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1684 | |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 1685 | int restorecon_data(const char* uuid, const char* pkgName, |
Andreas Gampe | 0ad7a11 | 2015-04-09 09:52:45 -0700 | [diff] [blame] | 1686 | const char* seinfo, uid_t uid) |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1687 | { |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1688 | struct dirent *entry; |
| 1689 | DIR *d; |
| 1690 | struct stat s; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1691 | int ret = 0; |
| 1692 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1693 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
| 1694 | unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE; |
| 1695 | |
| 1696 | if (!pkgName || !seinfo) { |
| 1697 | ALOGE("Package name or seinfo tag is null when trying to restorecon."); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1698 | return -1; |
| 1699 | } |
| 1700 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1701 | // Special case for owner on internal storage |
| 1702 | if (uuid == nullptr) { |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1703 | std::string path(create_data_user_package_path(nullptr, 0, pkgName)); |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1704 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1705 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, flags) < 0) { |
| 1706 | PLOG(ERROR) << "restorecon failed for " << path; |
| 1707 | ret |= -1; |
| 1708 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1709 | } |
| 1710 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1711 | // Relabel package directory for all secondary users. |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1712 | std::string userdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX); |
| 1713 | d = opendir(userdir.c_str()); |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1714 | if (d == NULL) { |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1715 | return -1; |
| 1716 | } |
| 1717 | |
| 1718 | while ((entry = readdir(d))) { |
| 1719 | if (entry->d_type != DT_DIR) { |
| 1720 | continue; |
| 1721 | } |
| 1722 | |
| 1723 | const char *user = entry->d_name; |
| 1724 | // Ignore "." and ".." |
| 1725 | if (!strcmp(user, ".") || !strcmp(user, "..")) { |
| 1726 | continue; |
| 1727 | } |
| 1728 | |
| 1729 | // user directories start with a number |
| 1730 | if (user[0] < '0' || user[0] > '9') { |
| 1731 | ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user); |
| 1732 | continue; |
| 1733 | } |
| 1734 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1735 | std::string pkgdir(StringPrintf("%s%s/%s", userdir.c_str(), user, pkgName)); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1736 | if (stat(pkgdir.c_str(), &s) < 0) { |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1737 | continue; |
| 1738 | } |
| 1739 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1740 | if (selinux_android_restorecon_pkgdir(pkgdir.c_str(), seinfo, s.st_uid, flags) < 0) { |
| 1741 | PLOG(ERROR) << "restorecon failed for " << pkgdir; |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1742 | ret |= -1; |
| 1743 | } |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | closedir(d); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1747 | return ret; |
| 1748 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 1749 | |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1750 | int create_oat_dir(const char* oat_dir, const char* instruction_set) |
| 1751 | { |
| 1752 | char oat_instr_dir[PKG_PATH_MAX]; |
| 1753 | |
| 1754 | if (validate_apk_path(oat_dir)) { |
| 1755 | ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir); |
| 1756 | return -1; |
| 1757 | } |
Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 1758 | 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] | 1759 | return -1; |
| 1760 | } |
| 1761 | if (selinux_android_restorecon(oat_dir, 0)) { |
| 1762 | ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno)); |
| 1763 | return -1; |
| 1764 | } |
| 1765 | 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] | 1766 | 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] | 1767 | return -1; |
| 1768 | } |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | int rm_package_dir(const char* apk_path) |
| 1773 | { |
| 1774 | if (validate_apk_path(apk_path)) { |
| 1775 | ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path); |
| 1776 | return -1; |
| 1777 | } |
| 1778 | return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */); |
| 1779 | } |
| 1780 | |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame^] | 1781 | int link_file(const char* relative_path, const char* from_base, const char* to_base) { |
| 1782 | char from_path[PKG_PATH_MAX]; |
| 1783 | char to_path[PKG_PATH_MAX]; |
| 1784 | snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); |
| 1785 | snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); |
| 1786 | |
| 1787 | if (validate_apk_path_subdirs(from_path)) { |
| 1788 | ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path); |
| 1789 | return -1; |
| 1790 | } |
| 1791 | |
| 1792 | if (validate_apk_path_subdirs(to_path)) { |
| 1793 | ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path); |
| 1794 | return -1; |
| 1795 | } |
| 1796 | |
| 1797 | const int ret = link(from_path, to_path); |
| 1798 | if (ret < 0) { |
| 1799 | ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno)); |
| 1800 | return -1; |
| 1801 | } |
| 1802 | |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1806 | int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, |
| 1807 | const char *instruction_set) { |
| 1808 | char *file_name_start; |
| 1809 | char *file_name_end; |
| 1810 | |
| 1811 | file_name_start = strrchr(apk_path, '/'); |
| 1812 | if (file_name_start == NULL) { |
| 1813 | ALOGE("apk_path '%s' has no '/'s in it\n", apk_path); |
| 1814 | return -1; |
| 1815 | } |
| 1816 | file_name_end = strrchr(apk_path, '.'); |
| 1817 | if (file_name_end < file_name_start) { |
| 1818 | ALOGE("apk_path '%s' has no extension\n", apk_path); |
| 1819 | return -1; |
| 1820 | } |
| 1821 | |
| 1822 | // Calculate file_name |
| 1823 | int file_name_len = file_name_end - file_name_start - 1; |
| 1824 | char file_name[file_name_len + 1]; |
| 1825 | memcpy(file_name, file_name_start + 1, file_name_len); |
| 1826 | file_name[file_name_len] = '\0'; |
| 1827 | |
| 1828 | // <apk_parent_dir>/oat/<isa>/<file_name>.odex |
| 1829 | snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name); |
| 1830 | return 0; |
| 1831 | } |