blob: 95ed2fff3545392d2840c95fb4a79df9456e9cba [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Jeff Sharkey19803802015-04-07 12:44:51 -07004** 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 Lockwood94afecf2012-10-24 10:45:23 -07007**
Jeff Sharkey19803802015-04-07 12:44:51 -07008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Jeff Sharkey19803802015-04-07 12:44:51 -070010** 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 Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
Mark Salyzyna5e161b2016-09-29 08:08:05 -070016#define LOG_TAG "installd"
Mike Lockwood94afecf2012-10-24 10:45:23 -070017
Andreas Gampe02d0de52015-11-11 20:43:16 -080018#include <fcntl.h>
Stephen Smalleybd558d62013-04-16 12:16:50 -040019#include <selinux/android.h>
20#include <selinux/avc.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080021#include <sys/capability.h>
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070022#include <sys/fsuid.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080023#include <sys/prctl.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080024#include <sys/stat.h>
25
26#include <android-base/logging.h>
27#include <cutils/fs.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080028#include <cutils/properties.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070029#include <log/log.h> // TODO: Move everything to base::logging.
Andreas Gampe02d0de52015-11-11 20:43:16 -080030#include <private/android_filesystem_config.h>
31
Jeff Sharkeyf3e30b92016-12-09 17:06:57 -070032#include "InstalldNativeService.h"
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060033#include "dexopt.h"
Jeff Sharkeyf3e30b92016-12-09 17:06:57 -070034#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 Gampe02d0de52015-11-11 20:43:16 -080038
Andreas Gampe02d0de52015-11-11 20:43:16 -080039namespace android {
40namespace installd {
41
42// Check that installd-deps sizes match cutils sizes.
43static_assert(kPropertyKeyMax == PROPERTY_KEY_MAX, "Size mismatch.");
44static_assert(kPropertyValueMax == PROPERTY_VALUE_MAX, "Size mismatch.");
45
46////////////////////////
47// Plug-in functions. //
48////////////////////////
49
50int get_property(const char *key, char *value, const char *default_value) {
51 return property_get(key, value, default_value);
52}
53
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060054bool 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 Gampe02d0de52015-11-11 20:43:16 -080057}
58
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060059bool 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 Gampe02d0de52015-11-11 20:43:16 -080062}
63
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060064bool 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 Gampe02d0de52015-11-11 20:43:16 -080066}
67
Andreas Gamped089ca12016-06-27 14:25:30 -070068static bool initialize_globals() {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060069 return init_globals_from_data_and_root();
Mike Lockwood94afecf2012-10-24 10:45:23 -070070}
71
Andreas Gampe02d0de52015-11-11 20:43:16 -080072static int initialize_directories() {
Mike Lockwood94afecf2012-10-24 10:45:23 -070073 int res = -1;
74
75 // Read current filesystem layout version to handle upgrade paths
76 char version_path[PATH_MAX];
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060077 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.c_str());
Mike Lockwood94afecf2012-10-24 10:45:23 -070078
79 int oldVersion;
80 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
81 oldVersion = 0;
82 }
83 int version = oldVersion;
84
Jeff Sharkeye02657d2016-01-13 09:37:46 -070085 if (version < 2) {
86 SLOGD("Assuming that device has multi-user storage layout; upgrade no longer supported");
Mike Lockwood94afecf2012-10-24 10:45:23 -070087 version = 2;
88 }
89
Robin Lee07053fc2014-04-29 19:42:01 +010090 if (ensure_config_user_dirs(0) == -1) {
Mark Salyzyna5e161b2016-09-29 08:08:05 -070091 SLOGE("Failed to setup misc for user 0");
Robin Lee07053fc2014-04-29 19:42:01 +010092 goto fail;
93 }
94
Robin Lee095c7632014-04-25 15:05:19 +010095 if (version == 2) {
Mark Salyzyna5e161b2016-09-29 08:08:05 -070096 SLOGD("Upgrading to /data/misc/user directories");
Robin Lee095c7632014-04-25 15:05:19 +010097
Robin Lee60fd3fe2014-10-07 16:55:02 +010098 char misc_dir[PATH_MAX];
Jeff Sharkeyc1149c92017-09-21 14:51:09 -060099 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.c_str());
Robin Lee60fd3fe2014-10-07 16:55:02 +0100100
101 char keychain_added_dir[PATH_MAX];
102 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir);
103
104 char keychain_removed_dir[PATH_MAX];
105 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir);
106
Robin Lee095c7632014-04-25 15:05:19 +0100107 DIR *dir;
108 struct dirent *dirent;
Jeff Sharkeye02657d2016-01-13 09:37:46 -0700109 dir = opendir("/data/user");
Robin Lee095c7632014-04-25 15:05:19 +0100110 if (dir != NULL) {
111 while ((dirent = readdir(dir))) {
Robin Lee60fd3fe2014-10-07 16:55:02 +0100112 const char *name = dirent->d_name;
Robin Lee095c7632014-04-25 15:05:19 +0100113
Robin Lee60fd3fe2014-10-07 16:55:02 +0100114 // skip "." and ".."
115 if (name[0] == '.') {
116 if (name[1] == 0) continue;
117 if ((name[1] == '.') && (name[2] == 0)) continue;
118 }
119
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600120 uint32_t user_id = std::stoi(name);
Robin Lee60fd3fe2014-10-07 16:55:02 +0100121
122 // /data/misc/user/<user_id>
123 if (ensure_config_user_dirs(user_id) == -1) {
124 goto fail;
125 }
126
127 char misc_added_dir[PATH_MAX];
128 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name);
129
130 char misc_removed_dir[PATH_MAX];
131 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name);
132
133 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM);
134 gid_t gid = uid;
135 if (access(keychain_added_dir, F_OK) == 0) {
136 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) {
Mark Salyzyna5e161b2016-09-29 08:08:05 -0700137 SLOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100138 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100139 }
140 if (access(keychain_removed_dir, F_OK) == 0) {
141 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) {
Mark Salyzyna5e161b2016-09-29 08:08:05 -0700142 SLOGE("Some files failed to copy");
Robin Lee095c7632014-04-25 15:05:19 +0100143 }
144 }
145 }
146 closedir(dir);
Robin Lee095c7632014-04-25 15:05:19 +0100147
Robin Lee60fd3fe2014-10-07 16:55:02 +0100148 if (access(keychain_added_dir, F_OK) == 0) {
149 delete_dir_contents(keychain_added_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100150 }
Robin Lee60fd3fe2014-10-07 16:55:02 +0100151 if (access(keychain_removed_dir, F_OK) == 0) {
152 delete_dir_contents(keychain_removed_dir, 1, 0);
Robin Lee07053fc2014-04-29 19:42:01 +0100153 }
154 }
155
156 version = 3;
Robin Lee095c7632014-04-25 15:05:19 +0100157 }
158
Mike Lockwood94afecf2012-10-24 10:45:23 -0700159 // Persist layout version if changed
160 if (version != oldVersion) {
161 if (fs_write_atomic_int(version_path, version) == -1) {
Mark Salyzyna5e161b2016-09-29 08:08:05 -0700162 SLOGE("Failed to save version to %s: %s", version_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700163 goto fail;
164 }
165 }
166
167 // Success!
168 res = 0;
169
170fail:
Mike Lockwood94afecf2012-10-24 10:45:23 -0700171 return res;
172}
173
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600174static int log_callback(int type, const char *fmt, ...) { // NOLINT
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400175 va_list ap;
176 int priority;
177
178 switch (type) {
179 case SELINUX_WARNING:
180 priority = ANDROID_LOG_WARN;
181 break;
182 case SELINUX_INFO:
183 priority = ANDROID_LOG_INFO;
184 break;
185 default:
186 priority = ANDROID_LOG_ERROR;
187 break;
188 }
189 va_start(ap, fmt);
190 LOG_PRI_VA(priority, "SELinux", fmt, ap);
191 va_end(ap);
192 return 0;
193}
194
Andreas Gampe02d0de52015-11-11 20:43:16 -0800195static int installd_main(const int argc ATTRIBUTE_UNUSED, char *argv[]) {
Jeff Sharkey6c2c0562016-12-07 12:12:00 -0700196 int ret;
Stephen Smalleybd558d62013-04-16 12:16:50 -0400197 int selinux_enabled = (is_selinux_enabled() > 0);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700198
Jeff Sharkeye3637242015-04-08 20:56:42 -0700199 setenv("ANDROID_LOG_TAGS", "*:v", 1);
200 android::base::InitLogging(argv);
201
Mark Salyzyna5e161b2016-09-29 08:08:05 -0700202 SLOGI("installd firing up");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203
Stephen Smalley7abb52b2014-03-26 09:30:37 -0400204 union selinux_callback cb;
205 cb.func_log = log_callback;
206 selinux_set_callback(SELINUX_CB_LOG, cb);
207
Andreas Gampe02d0de52015-11-11 20:43:16 -0800208 if (!initialize_globals()) {
Mark Salyzyna5e161b2016-09-29 08:08:05 -0700209 SLOGE("Could not initialize globals; exiting.\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210 exit(1);
211 }
212
213 if (initialize_directories() < 0) {
Mark Salyzyna5e161b2016-09-29 08:08:05 -0700214 SLOGE("Could not create directories; exiting.\n");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700215 exit(1);
216 }
217
Stephen Smalleybd558d62013-04-16 12:16:50 -0400218 if (selinux_enabled && selinux_status_open(true) < 0) {
Mark Salyzyna5e161b2016-09-29 08:08:05 -0700219 SLOGE("Could not open selinux status; exiting.\n");
Stephen Smalleybd558d62013-04-16 12:16:50 -0400220 exit(1);
221 }
222
Jeff Sharkey90874002016-12-05 11:18:55 -0700223 if ((ret = InstalldNativeService::start()) != android::OK) {
Mark Salyzyna5e161b2016-09-29 08:08:05 -0700224 SLOGE("Unable to start InstalldNativeService: %d", ret);
Jeff Sharkey90874002016-12-05 11:18:55 -0700225 exit(1);
226 }
227
Jeff Sharkey6c2c0562016-12-07 12:12:00 -0700228 IPCThreadState::self()->joinThreadPool();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700229
Jeff Sharkey6c2c0562016-12-07 12:12:00 -0700230 LOG(INFO) << "installd shutting down";
Mike Lockwood94afecf2012-10-24 10:45:23 -0700231
232 return 0;
233}
Andreas Gampe02d0de52015-11-11 20:43:16 -0800234
235} // namespace installd
236} // namespace android
237
238int main(const int argc, char *argv[]) {
239 return android::installd::installd_main(argc, argv);
240}