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 43 #if INCLUDE_SYS_MOUNT_FOR_STATFS 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" 62 63 #define APP_SUBDIR "app/" // sub-directory under ANDROID_DATA 64 65 #define APP_LIB_SUBDIR "app-lib/" // sub-directory under ANDROID_DATA 66 67 #define MEDIA_SUBDIR "media/" // sub-directory under ANDROID_DATA 68 69 /* other handy constants */ 70 71 #define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA 72 73 #define DALVIK_CACHE_PREFIX "/data/dalvik-cache/" 74 #define DALVIK_CACHE_POSTFIX "/classes.dex" 75 76 #define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/" 77 78 #define PKG_NAME_MAX 128 /* largest allowed package name */ 79 #define PKG_PATH_MAX 256 /* max size of any path we use */ 80 81 /* data structures */ 82 83 typedef struct { 84 char* path; 85 size_t len; 86 } dir_rec_t; 87 88 typedef struct { 89 size_t count; 90 dir_rec_t* dirs; 91 } dir_rec_array_t; 92 93 extern dir_rec_t android_app_dir; 94 extern dir_rec_t android_app_private_dir; 95 extern dir_rec_t android_app_lib_dir; 96 extern dir_rec_t android_data_dir; 97 extern dir_rec_t android_asec_dir; 98 extern dir_rec_t android_media_dir; 99 extern dir_rec_array_t android_system_dirs; 100 101 typedef struct cache_dir_struct { 102 struct cache_dir_struct* parent; 103 int32_t childCount; 104 int32_t hiddenCount; 105 int32_t deleted; 106 char name[]; 107 } cache_dir_t; 108 109 typedef struct { 110 cache_dir_t* dir; 111 time_t modTime; 112 char name[]; 113 } cache_file_t; 114 115 typedef struct { 116 size_t numDirs; 117 size_t availDirs; 118 cache_dir_t** dirs; 119 size_t numFiles; 120 size_t availFiles; 121 cache_file_t** files; 122 size_t numCollected; 123 void* memBlocks; 124 int8_t* curMemBlockAvail; 125 int8_t* curMemBlockEnd; 126 } cache_t; 127 128 /* util.c */ 129 130 int create_pkg_path_in_dir(char path[PKG_PATH_MAX], 131 const dir_rec_t* dir, 132 const char* pkgname, 133 const char* postfix); 134 135 int create_pkg_path(char path[PKG_PATH_MAX], 136 const char *pkgname, 137 const char *postfix, 138 userid_t userid); 139 140 int create_user_path(char path[PKG_PATH_MAX], 141 userid_t userid); 142 143 int create_user_media_path(char path[PKG_PATH_MAX], userid_t userid); 144 145 int create_move_path(char path[PKG_PATH_MAX], 146 const char* pkgname, 147 const char* leaf, 148 userid_t userid); 149 150 int is_valid_package_name(const char* pkgname); 151 152 int create_cache_path(char path[PKG_PATH_MAX], const char *src); 153 154 int delete_dir_contents(const char *pathname, 155 int also_delete_dir, 156 const char *ignore); 157 158 int delete_dir_contents_fd(int dfd, const char *name); 159 160 int lookup_media_dir(char basepath[PATH_MAX], const char *dir); 161 162 int64_t data_disk_free(); 163 164 cache_t* start_cache_collection(); 165 166 void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir); 167 168 void clear_cache_files(cache_t* cache, int64_t free_size); 169 170 void finish_cache_collection(cache_t* cache); 171 172 int validate_system_app_path(const char* path); 173 174 int get_path_from_env(dir_rec_t* rec, const char* var); 175 176 int get_path_from_string(dir_rec_t* rec, const char* path); 177 178 int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix); 179 180 int validate_apk_path(const char *path); 181 182 int append_and_increment(char** dst, const char* src, size_t* dst_size); 183 184 char *build_string2(char *s1, char *s2); 185 char *build_string3(char *s1, char *s2, char *s3); 186 187 int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid); 188 int ensure_media_user_dirs(userid_t userid); 189 190 /* commands.c */ 191 192 int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo); 193 int uninstall(const char *pkgname, userid_t userid); 194 int renamepkg(const char *oldpkgname, const char *newpkgname); 195 int fix_uid(const char *pkgname, uid_t uid, gid_t gid); 196 int delete_user_data(const char *pkgname, userid_t userid); 197 int make_user_data(const char *pkgname, uid_t uid, userid_t userid); 198 int delete_user(userid_t userid); 199 int delete_cache(const char *pkgname, userid_t userid); 200 int move_dex(const char *src, const char *dst); 201 int rm_dex(const char *path); 202 int protect(char *pkgname, gid_t gid); 203 int get_size(const char *pkgname, userid_t userid, const char *apkpath, const char *libdirpath, 204 const char *fwdlock_apkpath, const char *asecpath, int64_t *codesize, 205 int64_t *datasize, int64_t *cachesize, int64_t *asecsize); 206 int free_cache(int64_t free_size); 207 int dexopt(const char *apk_path, uid_t uid, int is_public); 208 int movefiles(); 209 int linklib(const char* target, const char* source, int userId); 210