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