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