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