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