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