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