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