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> |
| 34 | |
| 35 | #include <cutils/fs.h> |
| 36 | #include <cutils/sockets.h> |
| 37 | #include <cutils/log.h> |
| 38 | #include <cutils/properties.h> |
| 39 | #include <cutils/multiuser.h> |
| 40 | |
| 41 | #include <private/android_filesystem_config.h> |
| 42 | |
Elliott Hughes | 9a4e7f4 | 2014-11-20 12:54:21 -0800 | [diff] [blame] | 43 | #if defined(__APPLE__) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 44 | #include <sys/mount.h> |
| 45 | #else |
| 46 | #include <sys/statfs.h> |
| 47 | #endif |
| 48 | |
| 49 | #define SOCKET_PATH "installd" |
| 50 | |
| 51 | |
| 52 | /* elements combined with a valid package name to form paths */ |
| 53 | |
| 54 | #define PRIMARY_USER_PREFIX "data/" |
| 55 | #define SECONDARY_USER_PREFIX "user/" |
| 56 | |
| 57 | #define PKG_DIR_POSTFIX "" |
| 58 | |
| 59 | #define PKG_LIB_POSTFIX "/lib" |
| 60 | |
| 61 | #define CACHE_DIR_POSTFIX "/cache" |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 62 | #define CODE_CACHE_DIR_POSTFIX "/code_cache" |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 63 | |
| 64 | #define APP_SUBDIR "app/" // sub-directory under ANDROID_DATA |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 65 | #define PRIV_APP_SUBDIR "priv-app/" // sub-directory under ANDROID_DATA |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 66 | |
| 67 | #define APP_LIB_SUBDIR "app-lib/" // sub-directory under ANDROID_DATA |
| 68 | |
| 69 | #define MEDIA_SUBDIR "media/" // sub-directory under ANDROID_DATA |
| 70 | |
| 71 | /* other handy constants */ |
| 72 | |
| 73 | #define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA |
| 74 | |
| 75 | #define DALVIK_CACHE_PREFIX "/data/dalvik-cache/" |
| 76 | #define DALVIK_CACHE_POSTFIX "/classes.dex" |
| 77 | |
| 78 | #define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/" |
| 79 | |
Mårten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 80 | #define IDMAP_PREFIX "/data/resource-cache/" |
| 81 | #define IDMAP_SUFFIX "@idmap" |
| 82 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 83 | #define PKG_NAME_MAX 128 /* largest allowed package name */ |
| 84 | #define PKG_PATH_MAX 256 /* max size of any path we use */ |
| 85 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 86 | /* data structures */ |
| 87 | |
| 88 | typedef struct { |
| 89 | char* path; |
| 90 | size_t len; |
| 91 | } dir_rec_t; |
| 92 | |
| 93 | typedef struct { |
| 94 | size_t count; |
| 95 | dir_rec_t* dirs; |
| 96 | } dir_rec_array_t; |
| 97 | |
| 98 | extern dir_rec_t android_app_dir; |
| 99 | extern dir_rec_t android_app_private_dir; |
| 100 | extern dir_rec_t android_app_lib_dir; |
| 101 | extern dir_rec_t android_data_dir; |
| 102 | extern dir_rec_t android_asec_dir; |
| 103 | extern dir_rec_t android_media_dir; |
Jeff Sharkey | e23a132 | 2015-04-06 16:19:39 -0700 | [diff] [blame^] | 104 | extern dir_rec_t android_mnt_expand_dir; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 105 | extern dir_rec_array_t android_system_dirs; |
| 106 | |
| 107 | typedef struct cache_dir_struct { |
| 108 | struct cache_dir_struct* parent; |
| 109 | int32_t childCount; |
| 110 | int32_t hiddenCount; |
| 111 | int32_t deleted; |
| 112 | char name[]; |
| 113 | } cache_dir_t; |
| 114 | |
| 115 | typedef struct { |
| 116 | cache_dir_t* dir; |
| 117 | time_t modTime; |
| 118 | char name[]; |
| 119 | } cache_file_t; |
| 120 | |
| 121 | typedef struct { |
| 122 | size_t numDirs; |
| 123 | size_t availDirs; |
| 124 | cache_dir_t** dirs; |
| 125 | size_t numFiles; |
| 126 | size_t availFiles; |
| 127 | cache_file_t** files; |
| 128 | size_t numCollected; |
| 129 | void* memBlocks; |
| 130 | int8_t* curMemBlockAvail; |
| 131 | int8_t* curMemBlockEnd; |
| 132 | } cache_t; |
| 133 | |
| 134 | /* util.c */ |
| 135 | |
| 136 | int create_pkg_path_in_dir(char path[PKG_PATH_MAX], |
| 137 | const dir_rec_t* dir, |
| 138 | const char* pkgname, |
| 139 | const char* postfix); |
| 140 | |
| 141 | int create_pkg_path(char path[PKG_PATH_MAX], |
| 142 | const char *pkgname, |
| 143 | const char *postfix, |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 144 | userid_t userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 145 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 146 | int create_user_path(char path[PKG_PATH_MAX], |
| 147 | userid_t userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 148 | |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 149 | int create_user_media_path(char path[PKG_PATH_MAX], userid_t userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 150 | |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 151 | int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid); |
| 152 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 153 | int create_move_path(char path[PKG_PATH_MAX], |
| 154 | const char* pkgname, |
| 155 | const char* leaf, |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 156 | userid_t userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 157 | |
| 158 | int is_valid_package_name(const char* pkgname); |
| 159 | |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 160 | int create_cache_path(char path[PKG_PATH_MAX], const char *src, |
| 161 | const char *instruction_set); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 162 | |
| 163 | int delete_dir_contents(const char *pathname, |
| 164 | int also_delete_dir, |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 165 | int (*exclusion_predicate)(const char *name, const int is_dir)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 166 | |
| 167 | int delete_dir_contents_fd(int dfd, const char *name); |
| 168 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 169 | int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group); |
| 170 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 171 | int lookup_media_dir(char basepath[PATH_MAX], const char *dir); |
| 172 | |
| 173 | int64_t data_disk_free(); |
| 174 | |
| 175 | cache_t* start_cache_collection(); |
| 176 | |
| 177 | void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir); |
| 178 | |
| 179 | void clear_cache_files(cache_t* cache, int64_t free_size); |
| 180 | |
| 181 | void finish_cache_collection(cache_t* cache); |
| 182 | |
| 183 | int validate_system_app_path(const char* path); |
| 184 | |
| 185 | int get_path_from_env(dir_rec_t* rec, const char* var); |
| 186 | |
| 187 | int get_path_from_string(dir_rec_t* rec, const char* path); |
| 188 | |
| 189 | int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix); |
| 190 | |
| 191 | int validate_apk_path(const char *path); |
| 192 | |
| 193 | int append_and_increment(char** dst, const char* src, size_t* dst_size); |
| 194 | |
| 195 | char *build_string2(char *s1, char *s2); |
| 196 | char *build_string3(char *s1, char *s2, char *s3); |
| 197 | |
| 198 | int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid); |
| 199 | int ensure_media_user_dirs(userid_t userid); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 200 | int ensure_config_user_dirs(userid_t userid); |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 201 | int create_profile_file(const char *pkgname, gid_t gid); |
| 202 | void remove_profile_file(const char *pkgname); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 203 | |
| 204 | /* commands.c */ |
| 205 | |
Robert Craig | 4d3fd4e | 2013-03-25 06:33:03 -0400 | [diff] [blame] | 206 | int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo); |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 207 | int uninstall(const char *pkgname, userid_t userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 208 | int renamepkg(const char *oldpkgname, const char *newpkgname); |
| 209 | int fix_uid(const char *pkgname, uid_t uid, gid_t gid); |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 210 | int delete_user_data(const char *pkgname, userid_t userid); |
Nick Kralevich | e4e91c4 | 2013-09-20 12:45:20 -0700 | [diff] [blame] | 211 | int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo); |
Robin Lee | 7c8bec0 | 2014-06-10 18:46:26 +0100 | [diff] [blame] | 212 | int make_user_config(userid_t userid); |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 213 | int delete_user(userid_t userid); |
| 214 | int delete_cache(const char *pkgname, userid_t userid); |
Jeff Sharkey | c796b68 | 2014-07-15 21:49:51 -0700 | [diff] [blame] | 215 | int delete_code_cache(const char *pkgname, userid_t userid); |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 216 | int move_dex(const char *src, const char *dst, const char *instruction_set); |
| 217 | int rm_dex(const char *path, const char *instruction_set); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 218 | int protect(char *pkgname, gid_t gid); |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 219 | int get_size(const char *pkgname, userid_t userid, const char *apkpath, const char *libdirpath, |
Narayan Kamath | 1b40032 | 2014-04-11 13:17:00 +0100 | [diff] [blame] | 220 | const char *fwdlock_apkpath, const char *asecpath, const char *instruction_set, |
| 221 | int64_t *codesize, int64_t *datasize, int64_t *cachesize, int64_t *asecsize); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 222 | int free_cache(int64_t free_size); |
Calin Juravle | b1efac1 | 2014-08-21 19:05:20 +0100 | [diff] [blame] | 223 | int dexopt(const char *apk_path, uid_t uid, bool is_public, const char *pkgName, |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 224 | const char *instruction_set, bool vm_safe_mode, bool should_relocate, bool debuggable, |
| 225 | const char* outputPath); |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 226 | int mark_boot_complete(const char *instruction_set); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 227 | int movefiles(); |
| 228 | int linklib(const char* target, const char* source, int userId); |
Mårten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 229 | int idmap(const char *target_path, const char *overlay_path, uid_t uid); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 230 | int restorecon_data(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 231 | int create_oat_dir(const char* oat_dir, const char *instruction_set); |
| 232 | int rm_package_dir(const char* apk_path); |
| 233 | 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^] | 234 | const char *instruction_set); |