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