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