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