The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2008, The Android Open Source Project |
| 3 | ** |
| 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 |
| 7 | ** |
| 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | ** |
| 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 |
| 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Nick Kralevich | 812b19a | 2012-08-31 16:08:06 -0700 | [diff] [blame] | 17 | #include <linux/capability.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | #include "installd.h" |
Kenny Root | 33b2264 | 2010-11-30 13:49:32 -0800 | [diff] [blame] | 19 | #include <diskusage/dirsize.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Stephen Smalley | 0b58e6a | 2012-01-13 08:27:42 -0500 | [diff] [blame] | 21 | #ifdef HAVE_SELINUX |
| 22 | #include <selinux/android.h> |
| 23 | #endif |
| 24 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 25 | /* Directory records that are used in execution of commands. */ |
| 26 | dir_rec_t android_data_dir; |
| 27 | dir_rec_t android_asec_dir; |
| 28 | dir_rec_t android_app_dir; |
| 29 | dir_rec_t android_app_private_dir; |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 30 | dir_rec_t android_app_lib_dir; |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 31 | dir_rec_t android_media_dir; |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 32 | dir_rec_array_t android_system_dirs; |
| 33 | |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 34 | int install(const char *pkgname, uid_t uid, gid_t gid) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | { |
| 36 | char pkgdir[PKG_PATH_MAX]; |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 37 | char libsymlink[PKG_PATH_MAX]; |
| 38 | char applibdir[PKG_PATH_MAX]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 41 | ALOGE("invalid uid/gid: %d %d\n", uid, gid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 44 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 45 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 46 | ALOGE("cannot create package path\n"); |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 47 | return -1; |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 50 | if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) { |
| 51 | ALOGE("cannot create package lib symlink origin path\n"); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) { |
| 56 | ALOGE("cannot create package lib symlink dest path\n"); |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 57 | return -1; |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 58 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | |
David 'Digit' Turner | 0dd50e6 | 2010-02-09 19:02:38 -0800 | [diff] [blame] | 60 | if (mkdir(pkgdir, 0751) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 61 | ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 62 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | } |
Nick Kralevich | f68327e | 2011-04-14 16:20:03 -0700 | [diff] [blame] | 64 | if (chmod(pkgdir, 0751) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 65 | ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); |
Nick Kralevich | f68327e | 2011-04-14 16:20:03 -0700 | [diff] [blame] | 66 | unlink(pkgdir); |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 67 | return -1; |
Nick Kralevich | f68327e | 2011-04-14 16:20:03 -0700 | [diff] [blame] | 68 | } |
Stephen Smalley | 0b58e6a | 2012-01-13 08:27:42 -0500 | [diff] [blame] | 69 | |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 70 | if (symlink(applibdir, libsymlink) < 0) { |
| 71 | ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir, |
| 72 | strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | unlink(pkgdir); |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 74 | return -1; |
Kenny Root | 4503cf6 | 2012-06-14 13:05:18 -0700 | [diff] [blame] | 75 | } |
Kenny Root | 33ef4ee | 2012-06-18 10:26:36 -0700 | [diff] [blame] | 76 | |
| 77 | #ifdef HAVE_SELINUX |
| 78 | if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) { |
Joshua Brindle | 365861e | 2012-07-10 10:22:36 -0400 | [diff] [blame] | 79 | ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno)); |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 80 | unlink(libsymlink); |
Kenny Root | 33ef4ee | 2012-06-18 10:26:36 -0700 | [diff] [blame] | 81 | unlink(pkgdir); |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 82 | return -1; |
Kenny Root | 33ef4ee | 2012-06-18 10:26:36 -0700 | [diff] [blame] | 83 | } |
| 84 | #endif |
| 85 | |
Kenny Root | 9bbd70a | 2012-09-10 11:13:36 -0700 | [diff] [blame] | 86 | if (chown(pkgdir, uid, gid) < 0) { |
| 87 | ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); |
| 88 | unlink(libsymlink); |
| 89 | unlink(pkgdir); |
| 90 | return -1; |
| 91 | } |
| 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 96 | int uninstall(const char *pkgname, uid_t persona) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | { |
| 98 | char pkgdir[PKG_PATH_MAX]; |
| 99 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 100 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 101 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 103 | /* delete contents AND directory, no exceptions */ |
| 104 | return delete_dir_contents(pkgdir, 1, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 107 | int renamepkg(const char *oldpkgname, const char *newpkgname) |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 108 | { |
| 109 | char oldpkgdir[PKG_PATH_MAX]; |
| 110 | char newpkgdir[PKG_PATH_MAX]; |
| 111 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 112 | if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0)) |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 113 | return -1; |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 114 | if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0)) |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 115 | return -1; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 116 | |
| 117 | if (rename(oldpkgdir, newpkgdir) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 118 | ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno)); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 119 | return -errno; |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 124 | int fix_uid(const char *pkgname, uid_t uid, gid_t gid) |
| 125 | { |
| 126 | char pkgdir[PKG_PATH_MAX]; |
| 127 | struct stat s; |
| 128 | int rc = 0; |
| 129 | |
| 130 | if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { |
| 131 | ALOGE("invalid uid/gid: %d %d\n", uid, gid); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) { |
| 136 | ALOGE("cannot create package path\n"); |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | if (stat(pkgdir, &s) < 0) return -1; |
| 141 | |
| 142 | if (s.st_uid != 0 || s.st_gid != 0) { |
| 143 | ALOGE("fixing uid of non-root pkg: %s %d %d\n", pkgdir, s.st_uid, s.st_gid); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | if (chmod(pkgdir, 0751) < 0) { |
| 148 | ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); |
| 149 | unlink(pkgdir); |
| 150 | return -errno; |
| 151 | } |
| 152 | if (chown(pkgdir, uid, gid) < 0) { |
| 153 | ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); |
| 154 | unlink(pkgdir); |
| 155 | return -errno; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 161 | int delete_user_data(const char *pkgname, uid_t persona) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | { |
| 163 | char pkgdir[PKG_PATH_MAX]; |
| 164 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 165 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 166 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 168 | /* delete contents, excluding "lib", but not the directory itself */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | return delete_dir_contents(pkgdir, 0, "lib"); |
| 170 | } |
| 171 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 172 | int make_user_data(const char *pkgname, uid_t uid, uid_t persona) |
| 173 | { |
| 174 | char pkgdir[PKG_PATH_MAX]; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 175 | |
| 176 | // Create the data dir for the package |
| 177 | if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) { |
| 178 | return -1; |
| 179 | } |
| 180 | if (mkdir(pkgdir, 0751) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 181 | ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 182 | return -errno; |
| 183 | } |
Amith Yamasani | 794d62f | 2012-08-24 12:58:27 -0700 | [diff] [blame] | 184 | if (chmod(pkgdir, 0751) < 0) { |
| 185 | ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); |
| 186 | unlink(pkgdir); |
| 187 | return -errno; |
| 188 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 189 | if (chown(pkgdir, uid, uid) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 190 | ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 191 | unlink(pkgdir); |
| 192 | return -errno; |
| 193 | } |
Stephen Smalley | 0b58e6a | 2012-01-13 08:27:42 -0500 | [diff] [blame] | 194 | |
| 195 | #ifdef HAVE_SELINUX |
| 196 | if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) { |
Joshua Brindle | 365861e | 2012-07-10 10:22:36 -0400 | [diff] [blame] | 197 | ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno)); |
Stephen Smalley | 0b58e6a | 2012-01-13 08:27:42 -0500 | [diff] [blame] | 198 | unlink(pkgdir); |
| 199 | return -errno; |
| 200 | } |
| 201 | #endif |
| 202 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int delete_persona(uid_t persona) |
| 207 | { |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 208 | char data_path[PKG_PATH_MAX]; |
| 209 | if (create_persona_path(data_path, persona)) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 210 | return -1; |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 211 | } |
| 212 | if (delete_dir_contents(data_path, 1, NULL)) { |
| 213 | return -1; |
| 214 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 215 | |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 216 | char media_path[PATH_MAX]; |
| 217 | if (create_persona_media_path(media_path, (userid_t) persona) == -1) { |
| 218 | return -1; |
| 219 | } |
| 220 | if (delete_dir_contents(media_path, 1, NULL) == -1) { |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | return 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 227 | int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy) |
| 228 | { |
| 229 | char src_data_dir[PKG_PATH_MAX]; |
| 230 | char pkg_path[PKG_PATH_MAX]; |
| 231 | DIR *d; |
| 232 | struct dirent *de; |
| 233 | struct stat s; |
| 234 | uid_t uid; |
| 235 | |
| 236 | if (create_persona_path(src_data_dir, src_persona)) { |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | d = opendir(src_data_dir); |
| 241 | if (d != NULL) { |
| 242 | while ((de = readdir(d))) { |
| 243 | const char *name = de->d_name; |
| 244 | |
| 245 | if (de->d_type == DT_DIR) { |
| 246 | int subfd; |
| 247 | /* always skip "." and ".." */ |
| 248 | if (name[0] == '.') { |
| 249 | if (name[1] == 0) continue; |
| 250 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 251 | } |
| 252 | /* Create the full path to the package's data dir */ |
| 253 | create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona); |
| 254 | /* Get the file stat */ |
| 255 | if (stat(pkg_path, &s) < 0) continue; |
| 256 | /* Get the uid of the package */ |
| 257 | ALOGI("Adding datadir for uid = %d\n", s.st_uid); |
| 258 | uid = (uid_t) s.st_uid % PER_USER_RANGE; |
| 259 | /* Create the directory for the target */ |
| 260 | make_user_data(name, uid + target_persona * PER_USER_RANGE, |
| 261 | target_persona); |
| 262 | } |
| 263 | } |
| 264 | closedir(d); |
| 265 | } |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 266 | |
Jeff Sharkey | 8ea0dc6 | 2012-08-27 15:46:54 -0700 | [diff] [blame] | 267 | if (ensure_media_user_dirs((userid_t) target_persona) == -1) { |
Jeff Sharkey | 5b1ada2 | 2012-08-14 18:47:09 -0700 | [diff] [blame] | 268 | return -1; |
| 269 | } |
Jeff Sharkey | 8ea0dc6 | 2012-08-27 15:46:54 -0700 | [diff] [blame] | 270 | |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
Amith Yamasani | 54289b8 | 2012-10-01 10:39:14 -0700 | [diff] [blame^] | 274 | int delete_cache(const char *pkgname, uid_t persona) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | { |
| 276 | char cachedir[PKG_PATH_MAX]; |
| 277 | |
Amith Yamasani | 54289b8 | 2012-10-01 10:39:14 -0700 | [diff] [blame^] | 278 | if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, persona)) |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 279 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | |
| 281 | /* delete contents, not the directory, no exceptions */ |
| 282 | return delete_dir_contents(cachedir, 0, 0); |
| 283 | } |
| 284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | /* Try to ensure free_size bytes of storage are available. |
| 286 | * Returns 0 on success. |
| 287 | * This is rather simple-minded because doing a full LRU would |
| 288 | * be potentially memory-intensive, and without atime it would |
| 289 | * also require that apps constantly modify file metadata even |
| 290 | * when just reading from the cache, which is pretty awful. |
| 291 | */ |
Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 292 | int free_cache(int64_t free_size) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | { |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 294 | cache_t* cache; |
| 295 | int64_t avail; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | DIR *d; |
| 297 | struct dirent *de; |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 298 | char tmpdir[PATH_MAX]; |
| 299 | char *dirpos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 301 | avail = data_disk_free(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | if (avail < 0) return -1; |
| 303 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 304 | ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | if (avail >= free_size) return 0; |
| 306 | |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 307 | cache = start_cache_collection(); |
| 308 | |
| 309 | // Collect cache files for primary user. |
| 310 | if (create_persona_path(tmpdir, 0) == 0) { |
| 311 | //ALOGI("adding cache files from %s\n", tmpdir); |
| 312 | add_cache_files(cache, tmpdir, "cache"); |
Kenny Root | ad757e9 | 2011-11-29 15:54:55 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 315 | // Search for other users and add any cache files from them. |
| 316 | snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path, |
| 317 | SECONDARY_USER_PREFIX); |
| 318 | dirpos = tmpdir + strlen(tmpdir); |
| 319 | d = opendir(tmpdir); |
| 320 | if (d != NULL) { |
| 321 | while ((de = readdir(d))) { |
| 322 | if (de->d_type == DT_DIR) { |
| 323 | const char *name = de->d_name; |
| 324 | /* always skip "." and ".." */ |
| 325 | if (name[0] == '.') { |
| 326 | if (name[1] == 0) continue; |
| 327 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 328 | } |
| 329 | if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) { |
| 330 | strcpy(dirpos, name); |
| 331 | //ALOGI("adding cache files from %s\n", tmpdir); |
| 332 | add_cache_files(cache, tmpdir, "cache"); |
| 333 | } else { |
| 334 | ALOGW("Path exceeds limit: %s%s", tmpdir, name); |
| 335 | } |
| 336 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 338 | closedir(d); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | } |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 340 | |
Dianne Hackborn | 556b09e | 2012-09-23 17:46:53 -0700 | [diff] [blame] | 341 | // Collect cache files on external storage for all users (if it is mounted as part |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 342 | // of the internal storage). |
| 343 | strcpy(tmpdir, android_media_dir.path); |
Dianne Hackborn | 556b09e | 2012-09-23 17:46:53 -0700 | [diff] [blame] | 344 | dirpos = tmpdir + strlen(tmpdir); |
| 345 | d = opendir(tmpdir); |
| 346 | if (d != NULL) { |
| 347 | while ((de = readdir(d))) { |
| 348 | if (de->d_type == DT_DIR) { |
| 349 | const char *name = de->d_name; |
| 350 | /* skip any dir that doesn't start with a number, so not a user */ |
| 351 | if (name[0] < '0' || name[0] > '9') { |
| 352 | continue; |
| 353 | } |
| 354 | if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) { |
| 355 | strcpy(dirpos, name); |
| 356 | if (lookup_media_dir(tmpdir, "Android") == 0 |
| 357 | && lookup_media_dir(tmpdir, "data") == 0) { |
| 358 | //ALOGI("adding cache files from %s\n", tmpdir); |
| 359 | add_cache_files(cache, tmpdir, "cache"); |
| 360 | } |
| 361 | } else { |
| 362 | ALOGW("Path exceeds limit: %s%s", tmpdir, name); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | closedir(d); |
Dianne Hackborn | 197a0c8 | 2012-07-12 14:46:04 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | clear_cache_files(cache, free_size); |
| 370 | finish_cache_collection(cache); |
| 371 | |
| 372 | return data_disk_free() >= free_size ? 0 : -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | } |
| 374 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | int move_dex(const char *src, const char *dst) |
| 376 | { |
| 377 | char src_dex[PKG_PATH_MAX]; |
| 378 | char dst_dex[PKG_PATH_MAX]; |
| 379 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 380 | if (validate_apk_path(src)) return -1; |
| 381 | if (validate_apk_path(dst)) return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | |
| 383 | if (create_cache_path(src_dex, src)) return -1; |
| 384 | if (create_cache_path(dst_dex, dst)) return -1; |
| 385 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 386 | ALOGV("move %s -> %s\n", src_dex, dst_dex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | if (rename(src_dex, dst_dex) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 388 | ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | return -1; |
| 390 | } else { |
| 391 | return 0; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | int rm_dex(const char *path) |
| 396 | { |
| 397 | char dex_path[PKG_PATH_MAX]; |
| 398 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 399 | if (validate_apk_path(path)) return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | if (create_cache_path(dex_path, path)) return -1; |
| 401 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 402 | ALOGV("unlink %s\n", dex_path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | if (unlink(dex_path) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 404 | ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | return -1; |
| 406 | } else { |
| 407 | return 0; |
| 408 | } |
| 409 | } |
| 410 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 411 | int get_size(const char *pkgname, int persona, const char *apkpath, |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 412 | const char *fwdlock_apkpath, const char *asecpath, |
| 413 | int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize, |
| 414 | int64_t* _asecsize) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | { |
| 416 | DIR *d; |
| 417 | int dfd; |
| 418 | struct dirent *de; |
| 419 | struct stat s; |
| 420 | char path[PKG_PATH_MAX]; |
| 421 | |
Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 422 | int64_t codesize = 0; |
| 423 | int64_t datasize = 0; |
| 424 | int64_t cachesize = 0; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 425 | int64_t asecsize = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | |
| 427 | /* count the source apk as code -- but only if it's not |
Suchi Amalapurapu | 8a9ab24 | 2010-03-11 16:49:16 -0800 | [diff] [blame] | 428 | * on the /system partition and its not on the sdcard. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | */ |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 430 | if (validate_system_app_path(apkpath) && |
| 431 | strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | if (stat(apkpath, &s) == 0) { |
| 433 | codesize += stat_size(&s); |
| 434 | } |
| 435 | } |
| 436 | /* count the forward locked apk as code if it is given |
| 437 | */ |
| 438 | if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') { |
| 439 | if (stat(fwdlock_apkpath, &s) == 0) { |
| 440 | codesize += stat_size(&s); |
| 441 | } |
| 442 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | /* count the cached dexfile as code */ |
| 444 | if (!create_cache_path(path, apkpath)) { |
| 445 | if (stat(path, &s) == 0) { |
| 446 | codesize += stat_size(&s); |
| 447 | } |
| 448 | } |
| 449 | |
Dianne Hackborn | f41496f | 2012-09-27 18:48:09 -0700 | [diff] [blame] | 450 | /* add in size of any libraries */ |
| 451 | if (!create_pkg_path_in_dir(path, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) { |
| 452 | d = opendir(path); |
| 453 | if (d != NULL) { |
| 454 | dfd = dirfd(d); |
| 455 | codesize += calculate_dir_size(dfd); |
| 456 | closedir(d); |
| 457 | } |
| 458 | } |
| 459 | |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 460 | /* compute asec size if it is given |
| 461 | */ |
| 462 | if (asecpath != NULL && asecpath[0] != '!') { |
| 463 | if (stat(asecpath, &s) == 0) { |
| 464 | asecsize += stat_size(&s); |
| 465 | } |
| 466 | } |
| 467 | |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 468 | if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) { |
Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 469 | goto done; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | d = opendir(path); |
| 473 | if (d == NULL) { |
| 474 | goto done; |
| 475 | } |
| 476 | dfd = dirfd(d); |
| 477 | |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 478 | /* most stuff in the pkgdir is data, except for the "cache" |
| 479 | * directory and below, which is cache, and the "lib" directory |
| 480 | * and below, which is code... |
| 481 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | while ((de = readdir(d))) { |
| 483 | const char *name = de->d_name; |
| 484 | |
| 485 | if (de->d_type == DT_DIR) { |
| 486 | int subfd; |
Dianne Hackborn | f41496f | 2012-09-27 18:48:09 -0700 | [diff] [blame] | 487 | int64_t statsize = 0; |
| 488 | int64_t dirsize = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | /* always skip "." and ".." */ |
| 490 | if (name[0] == '.') { |
| 491 | if (name[1] == 0) continue; |
| 492 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 493 | } |
Dianne Hackborn | f41496f | 2012-09-27 18:48:09 -0700 | [diff] [blame] | 494 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 495 | statsize = stat_size(&s); |
| 496 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); |
| 498 | if (subfd >= 0) { |
Dianne Hackborn | f41496f | 2012-09-27 18:48:09 -0700 | [diff] [blame] | 499 | dirsize = calculate_dir_size(subfd); |
| 500 | } |
| 501 | if(!strcmp(name,"lib")) { |
| 502 | codesize += dirsize + statsize; |
| 503 | } else if(!strcmp(name,"cache")) { |
| 504 | cachesize += dirsize + statsize; |
| 505 | } else { |
| 506 | datasize += dirsize + statsize; |
| 507 | } |
| 508 | } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) { |
| 509 | // This is the symbolic link to the application's library |
| 510 | // code. We'll count this as code instead of data, since |
| 511 | // it is not something that the app creates. |
| 512 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 513 | codesize += stat_size(&s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | } |
| 515 | } else { |
| 516 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 517 | datasize += stat_size(&s); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | closedir(d); |
| 522 | done: |
| 523 | *_codesize = codesize; |
| 524 | *_datasize = datasize; |
| 525 | *_cachesize = cachesize; |
Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 526 | *_asecsize = asecsize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /* a simpler version of dexOptGenerateCacheFileName() */ |
| 532 | int create_cache_path(char path[PKG_PATH_MAX], const char *src) |
| 533 | { |
| 534 | char *tmp; |
| 535 | int srclen; |
| 536 | int dstlen; |
| 537 | |
| 538 | srclen = strlen(src); |
| 539 | |
| 540 | /* demand that we are an absolute path */ |
| 541 | if ((src == 0) || (src[0] != '/') || strstr(src,"..")) { |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX? |
| 546 | return -1; |
| 547 | } |
| 548 | |
| 549 | dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) + |
| 550 | strlen(DALVIK_CACHE_POSTFIX) + 1; |
| 551 | |
| 552 | if (dstlen > PKG_PATH_MAX) { |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | sprintf(path,"%s%s%s", |
| 557 | DALVIK_CACHE_PREFIX, |
| 558 | src + 1, /* skip the leading / */ |
| 559 | DALVIK_CACHE_POSTFIX); |
| 560 | |
| 561 | for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) { |
| 562 | if (*tmp == '/') { |
| 563 | *tmp = '@'; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name, |
| 571 | const char* dexopt_flags) |
| 572 | { |
| 573 | static const char* DEX_OPT_BIN = "/system/bin/dexopt"; |
| 574 | static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig |
| 575 | char zip_num[MAX_INT_LEN]; |
| 576 | char odex_num[MAX_INT_LEN]; |
| 577 | |
| 578 | sprintf(zip_num, "%d", zip_fd); |
| 579 | sprintf(odex_num, "%d", odex_fd); |
| 580 | |
| 581 | execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name, |
| 582 | dexopt_flags, (char*) NULL); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 583 | ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static int wait_dexopt(pid_t pid, const char* apk_path) |
| 587 | { |
| 588 | int status; |
| 589 | pid_t got_pid; |
| 590 | |
| 591 | /* |
| 592 | * Wait for the optimization process to finish. |
| 593 | */ |
| 594 | while (1) { |
| 595 | got_pid = waitpid(pid, &status, 0); |
| 596 | if (got_pid == -1 && errno == EINTR) { |
| 597 | printf("waitpid interrupted, retrying\n"); |
| 598 | } else { |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | if (got_pid != pid) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 603 | ALOGW("waitpid failed: wanted %d, got %d: %s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | (int) pid, (int) got_pid, strerror(errno)); |
| 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 609 | ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | return 0; |
| 611 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 612 | ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | apk_path, status); |
| 614 | return status; /* always nonzero */ |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | int dexopt(const char *apk_path, uid_t uid, int is_public) |
| 619 | { |
| 620 | struct utimbuf ut; |
| 621 | struct stat apk_stat, dex_stat; |
| 622 | char dex_path[PKG_PATH_MAX]; |
| 623 | char dexopt_flags[PROPERTY_VALUE_MAX]; |
| 624 | char *end; |
| 625 | int res, zip_fd=-1, odex_fd=-1; |
| 626 | |
| 627 | /* Before anything else: is there a .odex file? If so, we have |
| 628 | * pre-optimized the apk and there is nothing to do here. |
| 629 | */ |
| 630 | if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) { |
| 631 | return -1; |
| 632 | } |
| 633 | |
| 634 | /* platform-specific flags affecting optimization and verification */ |
| 635 | property_get("dalvik.vm.dexopt-flags", dexopt_flags, ""); |
| 636 | |
| 637 | strcpy(dex_path, apk_path); |
| 638 | end = strrchr(dex_path, '.'); |
| 639 | if (end != NULL) { |
| 640 | strcpy(end, ".odex"); |
| 641 | if (stat(dex_path, &dex_stat) == 0) { |
| 642 | return 0; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (create_cache_path(dex_path, apk_path)) { |
| 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | memset(&apk_stat, 0, sizeof(apk_stat)); |
| 651 | stat(apk_path, &apk_stat); |
| 652 | |
| 653 | zip_fd = open(apk_path, O_RDONLY, 0); |
| 654 | if (zip_fd < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 655 | ALOGE("dexopt cannot open '%s' for input\n", apk_path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | return -1; |
| 657 | } |
| 658 | |
| 659 | unlink(dex_path); |
| 660 | odex_fd = open(dex_path, O_RDWR | O_CREAT | O_EXCL, 0644); |
| 661 | if (odex_fd < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 662 | ALOGE("dexopt cannot open '%s' for output\n", dex_path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | goto fail; |
| 664 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | if (fchmod(odex_fd, |
| 666 | S_IRUSR|S_IWUSR|S_IRGRP | |
| 667 | (is_public ? S_IROTH : 0)) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 668 | ALOGE("dexopt cannot chmod '%s'\n", dex_path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | goto fail; |
| 670 | } |
Nick Kralevich | 812b19a | 2012-08-31 16:08:06 -0700 | [diff] [blame] | 671 | if (fchown(odex_fd, AID_SYSTEM, uid) < 0) { |
| 672 | ALOGE("dexopt cannot chown '%s'\n", dex_path); |
| 673 | goto fail; |
| 674 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 676 | ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | |
| 678 | pid_t pid; |
| 679 | pid = fork(); |
| 680 | if (pid == 0) { |
| 681 | /* child -- drop privileges before continuing */ |
| 682 | if (setgid(uid) != 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 683 | ALOGE("setgid(%d) failed during dexopt\n", uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | exit(64); |
| 685 | } |
| 686 | if (setuid(uid) != 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 687 | ALOGE("setuid(%d) during dexopt\n", uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | exit(65); |
| 689 | } |
Nick Kralevich | 812b19a | 2012-08-31 16:08:06 -0700 | [diff] [blame] | 690 | // drop capabilities |
| 691 | struct __user_cap_header_struct capheader; |
| 692 | struct __user_cap_data_struct capdata[2]; |
| 693 | memset(&capheader, 0, sizeof(capheader)); |
| 694 | memset(&capdata, 0, sizeof(capdata)); |
| 695 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 696 | if (capset(&capheader, &capdata[0]) < 0) { |
| 697 | ALOGE("capset failed: %s\n", strerror(errno)); |
| 698 | exit(66); |
| 699 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | if (flock(odex_fd, LOCK_EX | LOCK_NB) != 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 701 | ALOGE("flock(%s) failed: %s\n", dex_path, strerror(errno)); |
Nick Kralevich | 812b19a | 2012-08-31 16:08:06 -0700 | [diff] [blame] | 702 | exit(67); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | run_dexopt(zip_fd, odex_fd, apk_path, dexopt_flags); |
Nick Kralevich | 812b19a | 2012-08-31 16:08:06 -0700 | [diff] [blame] | 706 | exit(68); /* only get here on exec failure */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | } else { |
| 708 | res = wait_dexopt(pid, apk_path); |
| 709 | if (res != 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 710 | ALOGE("dexopt failed on '%s' res = %d\n", dex_path, res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | goto fail; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | ut.actime = apk_stat.st_atime; |
| 716 | ut.modtime = apk_stat.st_mtime; |
| 717 | utime(dex_path, &ut); |
| 718 | |
| 719 | close(odex_fd); |
| 720 | close(zip_fd); |
| 721 | return 0; |
| 722 | |
| 723 | fail: |
| 724 | if (odex_fd >= 0) { |
| 725 | close(odex_fd); |
| 726 | unlink(dex_path); |
| 727 | } |
| 728 | if (zip_fd >= 0) { |
| 729 | close(zip_fd); |
| 730 | } |
| 731 | return -1; |
| 732 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 733 | |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 734 | void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, |
| 735 | struct stat* statbuf) |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 736 | { |
| 737 | while (path[basepos] != 0) { |
| 738 | if (path[basepos] == '/') { |
| 739 | path[basepos] = 0; |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 740 | if (lstat(path, statbuf) < 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 741 | ALOGV("Making directory: %s\n", path); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 742 | if (mkdir(path, mode) == 0) { |
| 743 | chown(path, uid, gid); |
| 744 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 745 | ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 746 | } |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 747 | } |
| 748 | path[basepos] = '/'; |
| 749 | basepos++; |
| 750 | } |
| 751 | basepos++; |
| 752 | } |
| 753 | } |
| 754 | |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 755 | int movefileordir(char* srcpath, char* dstpath, int dstbasepos, |
| 756 | int dstuid, int dstgid, struct stat* statbuf) |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 757 | { |
| 758 | DIR *d; |
| 759 | struct dirent *de; |
| 760 | int res; |
| 761 | |
| 762 | int srcend = strlen(srcpath); |
| 763 | int dstend = strlen(dstpath); |
| 764 | |
| 765 | if (lstat(srcpath, statbuf) < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 766 | ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno)); |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 767 | return 1; |
| 768 | } |
| 769 | |
| 770 | if ((statbuf->st_mode&S_IFDIR) == 0) { |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 771 | mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH, |
| 772 | dstuid, dstgid, statbuf); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 773 | ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid); |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 774 | if (rename(srcpath, dstpath) >= 0) { |
| 775 | if (chown(dstpath, dstuid, dstgid) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 776 | ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno)); |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 777 | unlink(dstpath); |
| 778 | return 1; |
| 779 | } |
| 780 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 781 | ALOGW("Unable to rename %s to %s: %s\n", |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 782 | srcpath, dstpath, strerror(errno)); |
| 783 | return 1; |
| 784 | } |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | d = opendir(srcpath); |
| 789 | if (d == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 790 | ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno)); |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 791 | return 1; |
| 792 | } |
| 793 | |
| 794 | res = 0; |
| 795 | |
| 796 | while ((de = readdir(d))) { |
| 797 | const char *name = de->d_name; |
| 798 | /* always skip "." and ".." */ |
| 799 | if (name[0] == '.') { |
| 800 | if (name[1] == 0) continue; |
| 801 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 802 | } |
| 803 | |
| 804 | if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 805 | ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name); |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 806 | continue; |
| 807 | } |
| 808 | |
| 809 | if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 810 | ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name); |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 811 | continue; |
| 812 | } |
| 813 | |
| 814 | srcpath[srcend] = dstpath[dstend] = '/'; |
| 815 | strcpy(srcpath+srcend+1, name); |
| 816 | strcpy(dstpath+dstend+1, name); |
| 817 | |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 818 | if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) { |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 819 | res = 1; |
| 820 | } |
| 821 | |
| 822 | // Note: we will be leaving empty directories behind in srcpath, |
| 823 | // but that is okay, the package manager will be erasing all of the |
| 824 | // data associated with .apks that disappear. |
| 825 | |
| 826 | srcpath[srcend] = dstpath[dstend] = 0; |
| 827 | } |
| 828 | |
| 829 | closedir(d); |
| 830 | return res; |
| 831 | } |
| 832 | |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 833 | int movefiles() |
| 834 | { |
| 835 | DIR *d; |
| 836 | int dfd, subfd; |
| 837 | struct dirent *de; |
| 838 | struct stat s; |
| 839 | char buf[PKG_PATH_MAX+1]; |
| 840 | int bufp, bufe, bufi, readlen; |
| 841 | |
| 842 | char srcpkg[PKG_NAME_MAX]; |
| 843 | char dstpkg[PKG_NAME_MAX]; |
| 844 | char srcpath[PKG_PATH_MAX]; |
| 845 | char dstpath[PKG_PATH_MAX]; |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 846 | int dstuid=-1, dstgid=-1; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 847 | int hasspace; |
| 848 | |
| 849 | d = opendir(UPDATE_COMMANDS_DIR_PREFIX); |
| 850 | if (d == NULL) { |
| 851 | goto done; |
| 852 | } |
| 853 | dfd = dirfd(d); |
| 854 | |
| 855 | /* Iterate through all files in the directory, executing the |
| 856 | * file movements requested there-in. |
| 857 | */ |
| 858 | while ((de = readdir(d))) { |
| 859 | const char *name = de->d_name; |
| 860 | |
| 861 | if (de->d_type == DT_DIR) { |
| 862 | continue; |
| 863 | } else { |
| 864 | subfd = openat(dfd, name, O_RDONLY); |
| 865 | if (subfd < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 866 | ALOGW("Unable to open update commands at %s%s\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 867 | UPDATE_COMMANDS_DIR_PREFIX, name); |
| 868 | continue; |
| 869 | } |
| 870 | |
| 871 | bufp = 0; |
| 872 | bufe = 0; |
| 873 | buf[PKG_PATH_MAX] = 0; |
| 874 | srcpkg[0] = dstpkg[0] = 0; |
| 875 | while (1) { |
| 876 | bufi = bufp; |
| 877 | while (bufi < bufe && buf[bufi] != '\n') { |
| 878 | bufi++; |
| 879 | } |
| 880 | if (bufi < bufe) { |
| 881 | buf[bufi] = 0; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 882 | ALOGV("Processing line: %s\n", buf+bufp); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 883 | hasspace = 0; |
| 884 | while (bufp < bufi && isspace(buf[bufp])) { |
| 885 | hasspace = 1; |
| 886 | bufp++; |
| 887 | } |
| 888 | if (buf[bufp] == '#' || bufp == bufi) { |
| 889 | // skip comments and empty lines. |
| 890 | } else if (hasspace) { |
| 891 | if (dstpkg[0] == 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 892 | ALOGW("Path before package line in %s%s: %s\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 893 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 894 | } else if (srcpkg[0] == 0) { |
| 895 | // Skip -- source package no longer exists. |
| 896 | } else { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 897 | ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg); |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 898 | if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) && |
| 899 | !create_move_path(dstpath, dstpkg, buf+bufp, 0)) { |
Dianne Hackborn | c155239 | 2010-03-03 16:19:01 -0800 | [diff] [blame] | 900 | movefileordir(srcpath, dstpath, |
| 901 | strlen(dstpath)-strlen(buf+bufp), |
| 902 | dstuid, dstgid, &s); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | } else { |
| 906 | char* div = strchr(buf+bufp, ':'); |
| 907 | if (div == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 908 | ALOGW("Bad package spec in %s%s; no ':' sep: %s\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 909 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 910 | } else { |
| 911 | *div = 0; |
| 912 | div++; |
| 913 | if (strlen(buf+bufp) < PKG_NAME_MAX) { |
| 914 | strcpy(dstpkg, buf+bufp); |
| 915 | } else { |
| 916 | srcpkg[0] = dstpkg[0] = 0; |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 917 | ALOGW("Package name too long in %s%s: %s\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 918 | UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); |
| 919 | } |
| 920 | if (strlen(div) < PKG_NAME_MAX) { |
| 921 | strcpy(srcpkg, div); |
| 922 | } else { |
| 923 | srcpkg[0] = dstpkg[0] = 0; |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 924 | ALOGW("Package name too long in %s%s: %s\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 925 | UPDATE_COMMANDS_DIR_PREFIX, name, div); |
| 926 | } |
| 927 | if (srcpkg[0] != 0) { |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 928 | if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 929 | if (lstat(srcpath, &s) < 0) { |
| 930 | // Package no longer exists -- skip. |
| 931 | srcpkg[0] = 0; |
| 932 | } |
| 933 | } else { |
| 934 | srcpkg[0] = 0; |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 935 | ALOGW("Can't create path %s in %s%s\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 936 | div, UPDATE_COMMANDS_DIR_PREFIX, name); |
| 937 | } |
| 938 | if (srcpkg[0] != 0) { |
Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 939 | if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) { |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 940 | if (lstat(dstpath, &s) == 0) { |
| 941 | dstuid = s.st_uid; |
| 942 | dstgid = s.st_gid; |
| 943 | } else { |
Dianne Hackborn | d705fd2 | 2010-02-12 14:58:04 -0800 | [diff] [blame] | 944 | // Destination package doesn't |
| 945 | // exist... due to original-package, |
| 946 | // this is normal, so don't be |
| 947 | // noisy about it. |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 948 | srcpkg[0] = 0; |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 949 | } |
| 950 | } else { |
| 951 | srcpkg[0] = 0; |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 952 | ALOGW("Can't create path %s in %s%s\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 953 | div, UPDATE_COMMANDS_DIR_PREFIX, name); |
| 954 | } |
| 955 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 956 | ALOGV("Transfering from %s to %s: uid=%d\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 957 | srcpkg, dstpkg, dstuid); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | bufp = bufi+1; |
| 962 | } else { |
| 963 | if (bufp == 0) { |
| 964 | if (bufp < bufe) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 965 | ALOGW("Line too long in %s%s, skipping: %s\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 966 | UPDATE_COMMANDS_DIR_PREFIX, name, buf); |
| 967 | } |
| 968 | } else if (bufp < bufe) { |
| 969 | memcpy(buf, buf+bufp, bufe-bufp); |
| 970 | bufe -= bufp; |
| 971 | bufp = 0; |
| 972 | } |
| 973 | readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe); |
| 974 | if (readlen < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 975 | ALOGW("Failure reading update commands in %s%s: %s\n", |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 976 | UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno)); |
| 977 | break; |
| 978 | } else if (readlen == 0) { |
| 979 | break; |
| 980 | } |
| 981 | bufe += readlen; |
| 982 | buf[bufe] = 0; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 983 | ALOGV("Read buf: %s\n", buf); |
Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | close(subfd); |
| 987 | } |
| 988 | } |
| 989 | closedir(d); |
| 990 | done: |
| 991 | return 0; |
| 992 | } |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 993 | |
| 994 | int linklib(const char* dataDir, const char* asecLibDir) |
| 995 | { |
| 996 | char libdir[PKG_PATH_MAX]; |
| 997 | struct stat s, libStat; |
| 998 | int rc = 0; |
| 999 | |
| 1000 | const size_t libdirLen = strlen(dataDir) + strlen(PKG_LIB_POSTFIX); |
| 1001 | if (libdirLen >= PKG_PATH_MAX) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1002 | ALOGE("library dir len too large"); |
Kenny Root | 0332d1c | 2010-10-21 16:14:06 -0700 | [diff] [blame] | 1003 | return -1; |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | if (snprintf(libdir, sizeof(libdir), "%s%s", dataDir, PKG_LIB_POSTFIX) != (ssize_t)libdirLen) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1007 | ALOGE("library dir not written successfully: %s\n", strerror(errno)); |
Kenny Root | 0332d1c | 2010-10-21 16:14:06 -0700 | [diff] [blame] | 1008 | return -1; |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | if (stat(dataDir, &s) < 0) return -1; |
| 1012 | |
Nick Kralevich | 7de350a | 2012-09-07 15:48:11 -0700 | [diff] [blame] | 1013 | if (chown(dataDir, AID_INSTALL, AID_INSTALL) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1014 | ALOGE("failed to chown '%s': %s\n", dataDir, strerror(errno)); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1015 | return -1; |
| 1016 | } |
| 1017 | |
| 1018 | if (chmod(dataDir, 0700) < 0) { |
Nick Kralevich | 7de350a | 2012-09-07 15:48:11 -0700 | [diff] [blame] | 1019 | ALOGE("linklib() 1: failed to chmod '%s': %s\n", dataDir, strerror(errno)); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1020 | rc = -1; |
| 1021 | goto out; |
| 1022 | } |
| 1023 | |
| 1024 | if (lstat(libdir, &libStat) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1025 | ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1026 | rc = -1; |
| 1027 | goto out; |
| 1028 | } |
| 1029 | |
| 1030 | if (S_ISDIR(libStat.st_mode)) { |
| 1031 | if (delete_dir_contents(libdir, 1, 0) < 0) { |
| 1032 | rc = -1; |
| 1033 | goto out; |
| 1034 | } |
| 1035 | } else if (S_ISLNK(libStat.st_mode)) { |
| 1036 | if (unlink(libdir) < 0) { |
| 1037 | rc = -1; |
| 1038 | goto out; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | if (symlink(asecLibDir, libdir) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1043 | ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libdir, asecLibDir, strerror(errno)); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1044 | rc = -errno; |
| 1045 | goto out; |
| 1046 | } |
| 1047 | |
| 1048 | if (lchown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1049 | ALOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno)); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1050 | unlink(libdir); |
| 1051 | rc = -errno; |
| 1052 | goto out; |
| 1053 | } |
| 1054 | |
| 1055 | out: |
| 1056 | if (chmod(dataDir, s.st_mode) < 0) { |
Nick Kralevich | 7de350a | 2012-09-07 15:48:11 -0700 | [diff] [blame] | 1057 | ALOGE("linklib() 2: failed to chmod '%s': %s\n", dataDir, strerror(errno)); |
Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame] | 1058 | rc = -errno; |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | if (chown(dataDir, s.st_uid, s.st_gid) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1062 | ALOGE("failed to chown '%s' : %s\n", dataDir, strerror(errno)); |
Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 1063 | return -errno; |
| 1064 | } |
| 1065 | |
| 1066 | return rc; |
| 1067 | } |