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