| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [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> |
| Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <inttypes.h> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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/sockets.h> |
| 36 | #include <cutils/log.h> |
| 37 | #include <cutils/properties.h> |
| 38 | |
| 39 | #include <private/android_filesystem_config.h> |
| 40 | |
| 41 | #if INCLUDE_SYS_MOUNT_FOR_STATFS |
| 42 | #include <sys/mount.h> |
| 43 | #else |
| 44 | #include <sys/statfs.h> |
| 45 | #endif |
| 46 | |
| 47 | #define SOCKET_PATH "installd" |
| 48 | |
| 49 | |
| 50 | /* elements combined with a valid package name to form paths */ |
| 51 | |
| Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 52 | #define PRIMARY_USER_PREFIX "data/" |
| 53 | #define SECONDARY_USER_PREFIX "user/" |
| 54 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | #define PKG_DIR_POSTFIX "" |
| 56 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | #define PKG_LIB_POSTFIX "/lib" |
| 58 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | #define CACHE_DIR_POSTFIX "/cache" |
| 60 | |
| Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 61 | #define APP_SUBDIR "app/" // sub-directory under ANDROID_DATA |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
| 63 | /* other handy constants */ |
| 64 | |
| Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 65 | #define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | |
| Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 67 | #define DALVIK_CACHE_PREFIX "/data/dalvik-cache/" |
| 68 | #define DALVIK_CACHE_POSTFIX "/classes.dex" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 70 | #define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | |
| 72 | #define PKG_NAME_MAX 128 /* largest allowed package name */ |
| 73 | #define PKG_PATH_MAX 256 /* max size of any path we use */ |
| 74 | |
| Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 75 | #define PER_USER_RANGE ((uid_t)100000) /* range of uids per user |
| 76 | uid = persona * PER_USER_RANGE + appid */ |
| 77 | |
| Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 78 | /* data structures */ |
| 79 | |
| 80 | typedef struct { |
| 81 | char* path; |
| 82 | size_t len; |
| 83 | } dir_rec_t; |
| 84 | |
| 85 | typedef struct { |
| 86 | size_t count; |
| 87 | dir_rec_t* dirs; |
| 88 | } dir_rec_array_t; |
| 89 | |
| 90 | extern dir_rec_t android_app_dir; |
| 91 | extern dir_rec_t android_app_private_dir; |
| 92 | extern dir_rec_t android_data_dir; |
| 93 | extern dir_rec_t android_asec_dir; |
| 94 | extern dir_rec_array_t android_system_dirs; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
| 96 | /* util.c */ |
| 97 | |
| Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 98 | int create_pkg_path_in_dir(char path[PKG_PATH_MAX], |
| 99 | const dir_rec_t* dir, |
| 100 | const char* pkgname, |
| 101 | const char* postfix); |
| 102 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | int create_pkg_path(char path[PKG_PATH_MAX], |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | const char *pkgname, |
| Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 105 | const char *postfix, |
| 106 | uid_t persona); |
| 107 | |
| Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 108 | int create_persona_path(char path[PKG_PATH_MAX], |
| 109 | uid_t persona); |
| 110 | |
| Kenny Root | ad757e9 | 2011-11-29 15:54:55 -0800 | [diff] [blame] | 111 | int create_move_path(char path[PKG_PATH_MAX], |
| 112 | const char* pkgname, |
| 113 | const char* leaf, |
| 114 | uid_t persona); |
| 115 | |
| Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 116 | int is_valid_package_name(const char* pkgname); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | |
| 118 | int create_cache_path(char path[PKG_PATH_MAX], const char *src); |
| 119 | |
| 120 | int delete_dir_contents(const char *pathname, |
| 121 | int also_delete_dir, |
| 122 | const char *ignore); |
| 123 | |
| 124 | int delete_dir_contents_fd(int dfd, const char *name); |
| 125 | |
| Kenny Root | 86c9584 | 2011-03-31 13:16:12 -0700 | [diff] [blame] | 126 | int validate_system_app_path(const char* path); |
| 127 | |
| 128 | int get_path_from_env(dir_rec_t* rec, const char* var); |
| 129 | |
| 130 | int get_path_from_string(dir_rec_t* rec, const char* path); |
| 131 | |
| 132 | int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix); |
| 133 | |
| 134 | int validate_apk_path(const char *path); |
| 135 | |
| 136 | int append_and_increment(char** dst, const char* src, size_t* dst_size); |
| 137 | |
| Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 138 | char *build_string2(char *s1, char *s2); |
| 139 | char *build_string3(char *s1, char *s2, char *s3); |
| 140 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | /* commands.c */ |
| 142 | |
| Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 143 | int install(const char *pkgname, uid_t uid, gid_t gid); |
| Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 144 | int uninstall(const char *pkgname, uid_t persona); |
| Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 145 | int renamepkg(const char *oldpkgname, const char *newpkgname); |
| Dianne Hackborn | d0c5f51 | 2012-06-07 16:53:59 -0700 | [diff] [blame^] | 146 | int fix_uid(const char *pkgname, uid_t uid, gid_t gid); |
| Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 147 | int delete_user_data(const char *pkgname, uid_t persona); |
| 148 | int make_user_data(const char *pkgname, uid_t uid, uid_t persona); |
| 149 | int delete_persona(uid_t persona); |
| Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 150 | int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy); |
| Kenny Root | 35ab3ad | 2011-02-02 16:42:14 -0800 | [diff] [blame] | 151 | int delete_cache(const char *pkgname); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | int move_dex(const char *src, const char *dst); |
| 153 | int rm_dex(const char *path); |
| 154 | int protect(char *pkgname, gid_t gid); |
| 155 | int get_size(const char *pkgname, const char *apkpath, const char *fwdlock_apkpath, |
| Dianne Hackborn | 292f8bc | 2011-06-27 16:27:41 -0700 | [diff] [blame] | 156 | const char *asecpath, int64_t *codesize, int64_t *datasize, int64_t *cachesize, |
| 157 | int64_t *asecsize); |
| Kenny Root | 3e319a9 | 2010-09-07 13:58:28 -0700 | [diff] [blame] | 158 | int free_cache(int64_t free_size); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | int dexopt(const char *apk_path, uid_t uid, int is_public); |
| Dianne Hackborn | b858dfd | 2010-02-02 10:49:14 -0800 | [diff] [blame] | 160 | int movefiles(); |
| Kenny Root | 6a6b007 | 2010-10-07 16:46:10 -0700 | [diff] [blame] | 161 | int linklib(const char* target, const char* source); |
| 162 | int unlinklib(const char* libPath); |