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