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