Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2008, The Android Open Source Project |
| 3 | ** |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 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 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** |
Jeff Sharkey | 1980380 | 2015-04-07 12:44:51 -0700 | [diff] [blame] | 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 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 16 | #define LOG_TAG "installd" |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 17 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 18 | #include <fcntl.h> |
Stephen Smalley | bd558d6 | 2013-04-16 12:16:50 -0400 | [diff] [blame] | 19 | #include <selinux/android.h> |
| 20 | #include <selinux/avc.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 21 | #include <sys/capability.h> |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 22 | #include <sys/fsuid.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 23 | #include <sys/prctl.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | |
| 26 | #include <android-base/logging.h> |
| 27 | #include <cutils/fs.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 28 | #include <cutils/properties.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 29 | #include <log/log.h> // TODO: Move everything to base::logging. |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 30 | #include <private/android_filesystem_config.h> |
| 31 | |
Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 32 | #include "InstalldNativeService.h" |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 33 | #include "dexopt.h" |
Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 34 | #include "globals.h" |
| 35 | #include "installd_constants.h" |
| 36 | #include "installd_deps.h" // Need to fill in requirements of commands. |
| 37 | #include "utils.h" |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 38 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace installd { |
| 41 | |
| 42 | // Check that installd-deps sizes match cutils sizes. |
| 43 | static_assert(kPropertyKeyMax == PROPERTY_KEY_MAX, "Size mismatch."); |
| 44 | static_assert(kPropertyValueMax == PROPERTY_VALUE_MAX, "Size mismatch."); |
| 45 | |
| 46 | //////////////////////// |
| 47 | // Plug-in functions. // |
| 48 | //////////////////////// |
| 49 | |
| 50 | int get_property(const char *key, char *value, const char *default_value) { |
| 51 | return property_get(key, value, default_value); |
| 52 | } |
| 53 | |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 54 | bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, |
| 55 | const char *instruction_set) { |
| 56 | return calculate_oat_file_path_default(path, oat_dir, apk_path, instruction_set); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 59 | bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path, |
| 60 | const char *instruction_set) { |
| 61 | return calculate_odex_file_path_default(path, apk_path, instruction_set); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 64 | bool create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) { |
| 65 | return create_cache_path_default(path, src, instruction_set); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Jiakai Zhang | 6af6b97 | 2023-03-13 10:10:18 +0000 | [diff] [blame] | 68 | bool force_compile_without_image() { |
| 69 | return false; |
| 70 | } |
| 71 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 72 | static bool initialize_globals() { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 73 | return init_globals_from_data_and_root(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 76 | static int initialize_directories() { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 77 | int res = -1; |
| 78 | |
| 79 | // Read current filesystem layout version to handle upgrade paths |
| 80 | char version_path[PATH_MAX]; |
Paul Crowley | 3dfd55f | 2019-08-16 15:12:23 -0700 | [diff] [blame] | 81 | snprintf(version_path, PATH_MAX, "%smisc/installd/layout_version", android_data_dir.c_str()); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 82 | |
| 83 | int oldVersion; |
| 84 | if (fs_read_atomic_int(version_path, &oldVersion) == -1) { |
| 85 | oldVersion = 0; |
| 86 | } |
| 87 | int version = oldVersion; |
| 88 | |
Jeff Sharkey | e02657d | 2016-01-13 09:37:46 -0700 | [diff] [blame] | 89 | if (version < 2) { |
| 90 | SLOGD("Assuming that device has multi-user storage layout; upgrade no longer supported"); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 91 | version = 2; |
| 92 | } |
| 93 | |
Robin Lee | 07053fc | 2014-04-29 19:42:01 +0100 | [diff] [blame] | 94 | if (ensure_config_user_dirs(0) == -1) { |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 95 | SLOGE("Failed to setup misc for user 0"); |
Robin Lee | 07053fc | 2014-04-29 19:42:01 +0100 | [diff] [blame] | 96 | goto fail; |
| 97 | } |
| 98 | |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 99 | if (version == 2) { |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 100 | SLOGD("Upgrading to /data/misc/user directories"); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 101 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 102 | char misc_dir[PATH_MAX]; |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 103 | snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.c_str()); |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 104 | |
| 105 | char keychain_added_dir[PATH_MAX]; |
| 106 | snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir); |
| 107 | |
| 108 | char keychain_removed_dir[PATH_MAX]; |
| 109 | snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir); |
| 110 | |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 111 | DIR *dir; |
| 112 | struct dirent *dirent; |
Jeff Sharkey | e02657d | 2016-01-13 09:37:46 -0700 | [diff] [blame] | 113 | dir = opendir("/data/user"); |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 114 | if (dir != nullptr) { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 115 | while ((dirent = readdir(dir))) { |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 116 | const char *name = dirent->d_name; |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 117 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 118 | // skip "." and ".." |
| 119 | if (name[0] == '.') { |
| 120 | if (name[1] == 0) continue; |
| 121 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 122 | } |
| 123 | |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 124 | uint32_t user_id = std::stoi(name); |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 125 | |
| 126 | // /data/misc/user/<user_id> |
| 127 | if (ensure_config_user_dirs(user_id) == -1) { |
| 128 | goto fail; |
| 129 | } |
| 130 | |
| 131 | char misc_added_dir[PATH_MAX]; |
| 132 | snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name); |
| 133 | |
| 134 | char misc_removed_dir[PATH_MAX]; |
| 135 | snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name); |
| 136 | |
| 137 | uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM); |
| 138 | gid_t gid = uid; |
| 139 | if (access(keychain_added_dir, F_OK) == 0) { |
| 140 | if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) { |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 141 | SLOGE("Some files failed to copy"); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 142 | } |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 143 | } |
| 144 | if (access(keychain_removed_dir, F_OK) == 0) { |
| 145 | if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) { |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 146 | SLOGE("Some files failed to copy"); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
| 150 | closedir(dir); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 151 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 152 | if (access(keychain_added_dir, F_OK) == 0) { |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 153 | delete_dir_contents(keychain_added_dir, 1, nullptr); |
Robin Lee | 07053fc | 2014-04-29 19:42:01 +0100 | [diff] [blame] | 154 | } |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 155 | if (access(keychain_removed_dir, F_OK) == 0) { |
Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 156 | delete_dir_contents(keychain_removed_dir, 1, nullptr); |
Robin Lee | 07053fc | 2014-04-29 19:42:01 +0100 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | version = 3; |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 163 | // Persist layout version if changed |
| 164 | if (version != oldVersion) { |
| 165 | if (fs_write_atomic_int(version_path, version) == -1) { |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 166 | SLOGE("Failed to save version to %s: %s", version_path, strerror(errno)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 167 | goto fail; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Success! |
| 172 | res = 0; |
| 173 | |
| 174 | fail: |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 175 | return res; |
| 176 | } |
| 177 | |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 178 | static int log_callback(int type, const char *fmt, ...) { // NOLINT |
Stephen Smalley | 7abb52b | 2014-03-26 09:30:37 -0400 | [diff] [blame] | 179 | va_list ap; |
| 180 | int priority; |
| 181 | |
| 182 | switch (type) { |
| 183 | case SELINUX_WARNING: |
| 184 | priority = ANDROID_LOG_WARN; |
| 185 | break; |
| 186 | case SELINUX_INFO: |
| 187 | priority = ANDROID_LOG_INFO; |
| 188 | break; |
| 189 | default: |
| 190 | priority = ANDROID_LOG_ERROR; |
| 191 | break; |
| 192 | } |
| 193 | va_start(ap, fmt); |
| 194 | LOG_PRI_VA(priority, "SELinux", fmt, ap); |
| 195 | va_end(ap); |
| 196 | return 0; |
| 197 | } |
| 198 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 199 | static int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) { |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 200 | int ret; |
Stephen Smalley | bd558d6 | 2013-04-16 12:16:50 -0400 | [diff] [blame] | 201 | int selinux_enabled = (is_selinux_enabled() > 0); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 202 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 203 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
| 204 | android::base::InitLogging(argv); |
| 205 | |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 206 | SLOGI("installd firing up"); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 207 | |
Stephen Smalley | 7abb52b | 2014-03-26 09:30:37 -0400 | [diff] [blame] | 208 | union selinux_callback cb; |
| 209 | cb.func_log = log_callback; |
| 210 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 211 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 212 | if (!initialize_globals()) { |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 213 | SLOGE("Could not initialize globals; exiting.\n"); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 214 | exit(1); |
| 215 | } |
| 216 | |
| 217 | if (initialize_directories() < 0) { |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 218 | SLOGE("Could not create directories; exiting.\n"); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 219 | exit(1); |
| 220 | } |
| 221 | |
Stephen Smalley | bd558d6 | 2013-04-16 12:16:50 -0400 | [diff] [blame] | 222 | if (selinux_enabled && selinux_status_open(true) < 0) { |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 223 | SLOGE("Could not open selinux status; exiting.\n"); |
Stephen Smalley | bd558d6 | 2013-04-16 12:16:50 -0400 | [diff] [blame] | 224 | exit(1); |
| 225 | } |
| 226 | |
Jeff Sharkey | 9087400 | 2016-12-05 11:18:55 -0700 | [diff] [blame] | 227 | if ((ret = InstalldNativeService::start()) != android::OK) { |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 228 | SLOGE("Unable to start InstalldNativeService: %d", ret); |
Jeff Sharkey | 9087400 | 2016-12-05 11:18:55 -0700 | [diff] [blame] | 229 | exit(1); |
| 230 | } |
| 231 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 232 | IPCThreadState::self()->joinThreadPool(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 233 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 234 | LOG(INFO) << "installd shutting down"; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 235 | |
| 236 | return 0; |
| 237 | } |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 238 | |
| 239 | } // namespace installd |
| 240 | } // namespace android |
| 241 | |
| 242 | int main(const int argc, char *argv[]) { |
| 243 | return android::installd::installd_main(argc, argv); |
| 244 | } |