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