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