Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "installd" |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdint.h> |
| 23 | #include <inttypes.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <dirent.h> |
| 26 | #include <unistd.h> |
| 27 | #include <ctype.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <errno.h> |
| 30 | #include <utime.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/wait.h> |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 34 | #include <string> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 35 | #include <vector> |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 36 | |
| 37 | #include <cutils/fs.h> |
| 38 | #include <cutils/sockets.h> |
| 39 | #include <cutils/log.h> |
| 40 | #include <cutils/properties.h> |
| 41 | #include <cutils/multiuser.h> |
| 42 | |
| 43 | #include <private/android_filesystem_config.h> |
| 44 | |
Elliott Hughes | 9a4e7f4 | 2014-11-20 12:54:21 -0800 | [diff] [blame] | 45 | #if defined(__APPLE__) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 46 | #include <sys/mount.h> |
| 47 | #else |
| 48 | #include <sys/statfs.h> |
| 49 | #endif |
| 50 | |
| 51 | #define SOCKET_PATH "installd" |
| 52 | |
| 53 | |
| 54 | /* elements combined with a valid package name to form paths */ |
| 55 | |
| 56 | #define PRIMARY_USER_PREFIX "data/" |
| 57 | #define SECONDARY_USER_PREFIX "user/" |
| 58 | |
| 59 | #define PKG_DIR_POSTFIX "" |
| 60 | |
| 61 | #define PKG_LIB_POSTFIX "/lib" |
| 62 | |
| 63 | #define CACHE_DIR_POSTFIX "/cache" |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 64 | #define CODE_CACHE_DIR_POSTFIX "/code_cache" |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 65 | |
| 66 | #define APP_SUBDIR "app/" // sub-directory under ANDROID_DATA |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 67 | #define PRIV_APP_SUBDIR "priv-app/" // sub-directory under ANDROID_DATA |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 68 | |
| 69 | #define APP_LIB_SUBDIR "app-lib/" // sub-directory under ANDROID_DATA |
| 70 | |
| 71 | #define MEDIA_SUBDIR "media/" // sub-directory under ANDROID_DATA |
| 72 | |
| 73 | /* other handy constants */ |
| 74 | |
| 75 | #define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA |
| 76 | |
| 77 | #define DALVIK_CACHE_PREFIX "/data/dalvik-cache/" |
| 78 | #define DALVIK_CACHE_POSTFIX "/classes.dex" |
| 79 | |
| 80 | #define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/" |
| 81 | |
Mårten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 82 | #define IDMAP_PREFIX "/data/resource-cache/" |
| 83 | #define IDMAP_SUFFIX "@idmap" |
| 84 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 85 | #define PKG_NAME_MAX 128 /* largest allowed package name */ |
| 86 | #define PKG_PATH_MAX 256 /* max size of any path we use */ |
| 87 | |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 88 | /* dexopt needed flags matching those in dalvik.system.DexFile */ |
| 89 | #define DEXOPT_DEX2OAT_NEEDED 1 |
| 90 | #define DEXOPT_PATCHOAT_NEEDED 2 |
| 91 | #define DEXOPT_SELF_PATCHOAT_NEEDED 3 |
| 92 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 93 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 94 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 95 | /* data structures */ |
| 96 | |
| 97 | typedef struct { |
| 98 | char* path; |
| 99 | size_t len; |
| 100 | } dir_rec_t; |
| 101 | |
| 102 | typedef struct { |
| 103 | size_t count; |
| 104 | dir_rec_t* dirs; |
| 105 | } dir_rec_array_t; |
| 106 | |
| 107 | extern dir_rec_t android_app_dir; |
| 108 | extern dir_rec_t android_app_private_dir; |
| 109 | extern dir_rec_t android_app_lib_dir; |
| 110 | extern dir_rec_t android_data_dir; |
| 111 | extern dir_rec_t android_asec_dir; |
| 112 | extern dir_rec_t android_media_dir; |
Jeff Sharkey | e23a132 | 2015-04-06 16:19:39 -0700 | [diff] [blame] | 113 | extern dir_rec_t android_mnt_expand_dir; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 114 | extern dir_rec_array_t android_system_dirs; |
| 115 | |
| 116 | typedef struct cache_dir_struct { |
| 117 | struct cache_dir_struct* parent; |
| 118 | int32_t childCount; |
| 119 | int32_t hiddenCount; |
| 120 | int32_t deleted; |
| 121 | char name[]; |
| 122 | } cache_dir_t; |
| 123 | |
| 124 | typedef struct { |
| 125 | cache_dir_t* dir; |
| 126 | time_t modTime; |
| 127 | char name[]; |
| 128 | } cache_file_t; |
| 129 | |
| 130 | typedef struct { |
| 131 | size_t numDirs; |
| 132 | size_t availDirs; |
| 133 | cache_dir_t** dirs; |
| 134 | size_t numFiles; |
| 135 | size_t availFiles; |
| 136 | cache_file_t** files; |
| 137 | size_t numCollected; |
| 138 | void* memBlocks; |
| 139 | int8_t* curMemBlockAvail; |
| 140 | int8_t* curMemBlockEnd; |
| 141 | } cache_t; |
| 142 | |
| 143 | /* util.c */ |
| 144 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 145 | int create_pkg_path(char path[PKG_PATH_MAX], |
| 146 | const char *pkgname, |
| 147 | const char *postfix, |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 148 | userid_t userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 149 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 150 | std::string create_data_path(const char* volume_uuid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 151 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 152 | std::string create_data_app_path(const char* volume_uuid); |
| 153 | |
| 154 | std::string create_data_app_package_path(const char* volume_uuid, const char* package_name); |
| 155 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 156 | std::string create_data_user_path(const char* volume_uuid, userid_t userid); |
| 157 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 158 | std::string create_data_user_package_path(const char* volume_uuid, |
| 159 | userid_t user, const char* package_name); |
| 160 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 161 | std::string create_data_media_path(const char* volume_uuid, userid_t userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 162 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 163 | std::vector<userid_t> get_known_users(const char* volume_uuid); |
| 164 | |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 165 | int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid); |
| 166 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 167 | int create_move_path(char path[PKG_PATH_MAX], |
| 168 | const char* pkgname, |
| 169 | const char* leaf, |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 170 | userid_t userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 171 | |
| 172 | int is_valid_package_name(const char* pkgname); |
| 173 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 174 | int create_cache_path(char path[PKG_PATH_MAX], const char *src, |
| 175 | const char *instruction_set); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 176 | |
| 177 | int delete_dir_contents(const char *pathname, |
| 178 | int also_delete_dir, |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 179 | int (*exclusion_predicate)(const char *name, const int is_dir)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 180 | |
| 181 | int delete_dir_contents_fd(int dfd, const char *name); |
| 182 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 183 | int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group); |
| 184 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 185 | int lookup_media_dir(char basepath[PATH_MAX], const char *dir); |
| 186 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 187 | int64_t data_disk_free(const std::string& data_path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 188 | |
| 189 | cache_t* start_cache_collection(); |
| 190 | |
| 191 | void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir); |
| 192 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 193 | void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 194 | |
| 195 | void finish_cache_collection(cache_t* cache); |
| 196 | |
| 197 | int validate_system_app_path(const char* path); |
| 198 | |
| 199 | int get_path_from_env(dir_rec_t* rec, const char* var); |
| 200 | |
| 201 | int get_path_from_string(dir_rec_t* rec, const char* path); |
| 202 | |
| 203 | int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix); |
| 204 | |
| 205 | int validate_apk_path(const char *path); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 206 | int validate_apk_path_subdirs(const char *path); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 207 | |
| 208 | int append_and_increment(char** dst, const char* src, size_t* dst_size); |
| 209 | |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 210 | char *build_string2(const char *s1, const char *s2); |
| 211 | char *build_string3(const char *s1, const char *s2, const char *s3); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 212 | |
| 213 | int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 214 | int ensure_media_user_dirs(const char* uuid, userid_t userid); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 215 | int ensure_config_user_dirs(userid_t userid); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 216 | int create_profile_file(const char *pkgname, gid_t gid); |
| 217 | void remove_profile_file(const char *pkgname); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 218 | |
| 219 | /* commands.c */ |
| 220 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 221 | int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const char *seinfo); |
| 222 | int uninstall(const char *uuid, const char *pkgname, userid_t userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 223 | int renamepkg(const char *oldpkgname, const char *newpkgname); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 224 | int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid); |
| 225 | int delete_user_data(const char *uuid, const char *pkgname, userid_t userid); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 226 | int make_user_data(const char *uuid, const char *pkgname, uid_t uid, |
| 227 | userid_t userid, const char* seinfo); |
Jeff Sharkey | 31f0898 | 2015-07-07 13:31:37 -0700 | [diff] [blame] | 228 | int copy_complete_app(const char* from_uuid, const char *to_uuid, |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 229 | const char *package_name, const char *data_app_name, appid_t appid, |
| 230 | const char* seinfo); |
Robin Lee | 7c8bec0 | 2014-06-10 18:46:26 +0100 | [diff] [blame] | 231 | int make_user_config(userid_t userid); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 232 | int delete_user(const char *uuid, userid_t userid); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 233 | int delete_cache(const char *uuid, const char *pkgname, userid_t userid); |
| 234 | int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid); |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 235 | int move_dex(const char *src, const char *dst, const char *instruction_set); |
| 236 | int rm_dex(const char *path, const char *instruction_set); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 237 | int protect(char *pkgname, gid_t gid); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 238 | int get_size(const char *uuid, const char *pkgname, int userid, |
| 239 | const char *apkpath, const char *libdirpath, |
| 240 | const char *fwdlock_apkpath, const char *asecpath, |
| 241 | const char *instruction_set, int64_t *codesize, int64_t *datasize, |
| 242 | int64_t *cachesize, int64_t *asecsize); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 243 | int free_cache(const char *uuid, int64_t free_size); |
Calin Juravle | b1efac1 | 2014-08-21 19:05:20 +0100 | [diff] [blame] | 244 | int dexopt(const char *apk_path, uid_t uid, bool is_public, const char *pkgName, |
Richard Uhler | c92fb62 | 2015-03-26 15:47:38 -0700 | [diff] [blame] | 245 | const char *instruction_set, int dexopt_needed, bool vm_safe_mode, |
Andreas Gampe | 72ebebe | 2015-09-21 13:21:30 -0700 | [diff] [blame] | 246 | bool debuggable, const char* oat_dir, bool boot_complete); |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 247 | int mark_boot_complete(const char *instruction_set); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 248 | int movefiles(); |
Jeff Sharkey | 6fe28a0 | 2015-04-09 13:10:03 -0700 | [diff] [blame] | 249 | int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId); |
Mårten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 250 | int idmap(const char *target_path, const char *overlay_path, uid_t uid); |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 251 | int restorecon_data(const char *uuid, const char* pkgName, const char* seinfo, uid_t uid); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 252 | int create_oat_dir(const char* oat_dir, const char *instruction_set); |
| 253 | int rm_package_dir(const char* apk_path); |
| 254 | int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, |
Jeff Sharkey | e23a132 | 2015-04-06 16:19:39 -0700 | [diff] [blame] | 255 | const char *instruction_set); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 256 | int move_package_dir(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, |
| 257 | const char *instruction_set); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 258 | int link_file(const char *relative_path, const char *from_base, const char *to_base); |