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