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