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