blob: 7799ab9124b5501501ab6121c0d3e64770443665 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** 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**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** 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*/
16
Andreas Gampe02d0de52015-11-11 20:43:16 -080017#include "commands.h"
18
19#include <errno.h>
20#include <inttypes.h>
21#include <stdlib.h>
22#include <sys/capability.h>
23#include <sys/file.h>
24#include <sys/resource.h>
25#include <sys/stat.h>
26#include <unistd.h>
Jeff Sharkey41ea4242015-04-09 11:34:03 -070027
Elliott Hughese4ec9eb2015-12-04 15:39:32 -080028#include <android-base/stringprintf.h>
29#include <android-base/logging.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080030#include <cutils/fs.h>
31#include <cutils/log.h> // TODO: Move everything to base/logging.
Jeff Sharkeye3637242015-04-08 20:56:42 -070032#include <cutils/sched_policy.h>
33#include <diskusage/dirsize.h>
34#include <logwrap/logwrap.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080035#include <private/android_filesystem_config.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070036#include <selinux/android.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080037#include <system/thread_defs.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070038
Andreas Gampe02d0de52015-11-11 20:43:16 -080039#include <globals.h>
40#include <installd_deps.h>
41#include <utils.h>
42
43#ifndef LOG_TAG
44#define LOG_TAG "installd"
45#endif
Jeff Sharkey41ea4242015-04-09 11:34:03 -070046
47using android::base::StringPrintf;
Mike Lockwood94afecf2012-10-24 10:45:23 -070048
Andreas Gampe02d0de52015-11-11 20:43:16 -080049namespace android {
50namespace installd {
Mike Lockwood94afecf2012-10-24 10:45:23 -070051
Jeff Sharkeye3637242015-04-08 20:56:42 -070052static const char* kCpPath = "/system/bin/cp";
53
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070054int create_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags,
55 appid_t appid, const char* seinfo) {
56 uid_t uid = multiuser_get_uid(userid, appid);
57 if (flags & FLAG_CE_STORAGE) {
58 auto path = create_data_user_package_path(uuid, userid, pkgname);
59 if (fs_prepare_dir_strict(path.c_str(), 0751, uid, uid) != 0) {
60 PLOG(ERROR) << "Failed to prepare " << path;
61 return -1;
62 }
63 if (selinux_android_setfilecon(path.c_str(), pkgname, seinfo, uid) < 0) {
64 PLOG(ERROR) << "Failed to setfilecon " << path;
65 return -1;
66 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070067 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070068 if (flags & FLAG_DE_STORAGE) {
69 auto path = create_data_user_de_package_path(uuid, userid, pkgname);
70 if (fs_prepare_dir_strict(path.c_str(), 0751, uid, uid) == -1) {
71 PLOG(ERROR) << "Failed to prepare " << path;
Jeff Sharkeya03c7472016-01-13 09:47:08 -070072 // TODO: include result once 25796509 is fixed
73 return 0;
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070074 }
75 if (selinux_android_setfilecon(path.c_str(), pkgname, seinfo, uid) < 0) {
76 PLOG(ERROR) << "Failed to setfilecon " << path;
Jeff Sharkeya03c7472016-01-13 09:47:08 -070077 // TODO: include result once 25796509 is fixed
78 return 0;
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070079 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070080 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070081 return 0;
82}
83
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070084int clear_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags) {
85 std::string suffix = "";
86 if (flags & FLAG_CLEAR_CACHE_ONLY) {
87 suffix = CACHE_DIR_POSTFIX;
88 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
89 suffix = CODE_CACHE_DIR_POSTFIX;
90 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070091
Jeff Sharkeyebf728f2015-11-18 14:15:17 -070092 int res = 0;
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -070093 if (flags & FLAG_CE_STORAGE) {
94 auto path = create_data_user_package_path(uuid, userid, pkgname) + suffix;
95 if (access(path.c_str(), F_OK) == 0) {
96 res |= delete_dir_contents(path);
97 }
98 }
99 if (flags & FLAG_DE_STORAGE) {
100 auto path = create_data_user_de_package_path(uuid, userid, pkgname) + suffix;
101 if (access(path.c_str(), F_OK) == 0) {
Jeff Sharkeya03c7472016-01-13 09:47:08 -0700102 // TODO: include result once 25796509 is fixed
103 delete_dir_contents(path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700104 }
105 }
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700106 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700107}
108
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700109int destroy_app_data(const char *uuid, const char *pkgname, userid_t userid, int flags) {
110 int res = 0;
111 if (flags & FLAG_CE_STORAGE) {
112 res |= delete_dir_contents_and_dir(
113 create_data_user_package_path(uuid, userid, pkgname));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700114 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700115 if (flags & FLAG_DE_STORAGE) {
Jeff Sharkeya03c7472016-01-13 09:47:08 -0700116 // TODO: include result once 25796509 is fixed
117 delete_dir_contents_and_dir(
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700118 create_data_user_de_package_path(uuid, userid, pkgname));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700119 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700120 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700121}
122
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700123int move_complete_app(const char *from_uuid, const char *to_uuid, const char *package_name,
124 const char *data_app_name, appid_t appid, const char* seinfo) {
Jeff Sharkeye3637242015-04-08 20:56:42 -0700125 std::vector<userid_t> users = get_known_users(from_uuid);
126
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700127 // Copy app
128 {
129 std::string from(create_data_app_package_path(from_uuid, data_app_name));
130 std::string to(create_data_app_package_path(to_uuid, data_app_name));
131 std::string to_parent(create_data_app_path(to_uuid));
132
133 char *argv[] = {
134 (char*) kCpPath,
135 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
136 (char*) "-p", /* preserve timestamps, ownership, and permissions */
137 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
138 (char*) "-P", /* Do not follow symlinks [default] */
139 (char*) "-d", /* don't dereference symlinks */
140 (char*) from.c_str(),
141 (char*) to_parent.c_str()
142 };
143
144 LOG(DEBUG) << "Copying " << from << " to " << to;
145 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
146
147 if (rc != 0) {
148 LOG(ERROR) << "Failed copying " << from << " to " << to
149 << ": status " << rc;
150 goto fail;
151 }
152
153 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
154 LOG(ERROR) << "Failed to restorecon " << to;
155 goto fail;
156 }
157 }
158
159 // Copy private data for all known users
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700160 // TODO: handle user_de paths
Jeff Sharkeye3637242015-04-08 20:56:42 -0700161 for (auto user : users) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700162 std::string from(create_data_user_package_path(from_uuid, user, package_name));
163 std::string to(create_data_user_package_path(to_uuid, user, package_name));
164 std::string to_parent(create_data_user_path(to_uuid, user));
Jeff Sharkeye3637242015-04-08 20:56:42 -0700165
166 // Data source may not exist for all users; that's okay
167 if (access(from.c_str(), F_OK) != 0) {
168 LOG(INFO) << "Missing source " << from;
169 continue;
170 }
171
172 std::string user_path(create_data_user_path(to_uuid, user));
173 if (fs_prepare_dir(user_path.c_str(), 0771, AID_SYSTEM, AID_SYSTEM) != 0) {
174 LOG(ERROR) << "Failed to prepare user target " << user_path;
175 goto fail;
176 }
177
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700178 if (create_app_data(to_uuid, package_name, user, FLAG_CE_STORAGE | FLAG_DE_STORAGE,
179 appid, seinfo) != 0) {
Jeff Sharkeye3637242015-04-08 20:56:42 -0700180 LOG(ERROR) << "Failed to create package target " << to;
181 goto fail;
182 }
183
184 char *argv[] = {
185 (char*) kCpPath,
186 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
187 (char*) "-p", /* preserve timestamps, ownership, and permissions */
188 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
189 (char*) "-P", /* Do not follow symlinks [default] */
190 (char*) "-d", /* don't dereference symlinks */
191 (char*) from.c_str(),
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700192 (char*) to_parent.c_str()
Jeff Sharkeye3637242015-04-08 20:56:42 -0700193 };
194
195 LOG(DEBUG) << "Copying " << from << " to " << to;
196 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
197
198 if (rc != 0) {
199 LOG(ERROR) << "Failed copying " << from << " to " << to
200 << ": status " << rc;
201 goto fail;
202 }
203
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700204 if (restorecon_app_data(to_uuid, package_name, user, FLAG_CE_STORAGE | FLAG_DE_STORAGE,
205 appid, seinfo) != 0) {
206 LOG(ERROR) << "Failed to restorecon";
207 goto fail;
208 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700209 }
210
Jeff Sharkey31f08982015-07-07 13:31:37 -0700211 // We let the framework scan the new location and persist that before
212 // deleting the data in the old location; this ordering ensures that
213 // we can recover from things like battery pulls.
Jeff Sharkeye3637242015-04-08 20:56:42 -0700214 return 0;
215
216fail:
217 // Nuke everything we might have already copied
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700218 {
219 std::string to(create_data_app_package_path(to_uuid, data_app_name));
220 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
221 LOG(WARNING) << "Failed to rollback " << to;
222 }
223 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700224 for (auto user : users) {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700225 std::string to(create_data_user_package_path(to_uuid, user, package_name));
Jeff Sharkeye3637242015-04-08 20:56:42 -0700226 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
227 LOG(WARNING) << "Failed to rollback " << to;
228 }
229 }
230 return -1;
231}
232
Robin Lee7c8bec02014-06-10 18:46:26 +0100233int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700234{
Robin Lee095c7632014-04-25 15:05:19 +0100235 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700236 return -1;
237 }
238
239 return 0;
240}
241
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700242int delete_user(const char *uuid, userid_t userid) {
243 int res = 0;
Robin Lee095c7632014-04-25 15:05:19 +0100244
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700245 std::string data_path(create_data_user_path(uuid, userid));
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700246 std::string data_de_path(create_data_user_de_path(uuid, userid));
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700247 std::string media_path(create_data_media_path(uuid, userid));
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700248
249 res |= delete_dir_contents_and_dir(data_path);
Jeff Sharkeya03c7472016-01-13 09:47:08 -0700250 // TODO: include result once 25796509 is fixed
251 delete_dir_contents_and_dir(data_de_path);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700252 res |= delete_dir_contents_and_dir(media_path);
Robin Lee095c7632014-04-25 15:05:19 +0100253
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700254 // Config paths only exist on internal storage
255 if (uuid == nullptr) {
256 char config_path[PATH_MAX];
257 if ((create_user_config_path(config_path, userid) != 0)
258 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700259 res = -1;
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700260 }
Robin Lee095c7632014-04-25 15:05:19 +0100261 }
262
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700263 return res;
Robin Lee095c7632014-04-25 15:05:19 +0100264}
265
Mike Lockwood94afecf2012-10-24 10:45:23 -0700266/* Try to ensure free_size bytes of storage are available.
267 * Returns 0 on success.
268 * This is rather simple-minded because doing a full LRU would
269 * be potentially memory-intensive, and without atime it would
270 * also require that apps constantly modify file metadata even
271 * when just reading from the cache, which is pretty awful.
272 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700273int free_cache(const char *uuid, int64_t free_size)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700274{
275 cache_t* cache;
276 int64_t avail;
277 DIR *d;
278 struct dirent *de;
279 char tmpdir[PATH_MAX];
280 char *dirpos;
281
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700282 std::string data_path(create_data_path(uuid));
283
284 avail = data_disk_free(data_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700285 if (avail < 0) return -1;
286
287 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
288 if (avail >= free_size) return 0;
289
290 cache = start_cache_collection();
291
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700292 // Special case for owner on internal storage
293 if (uuid == nullptr) {
294 std::string _tmpdir(create_data_user_path(nullptr, 0));
295 add_cache_files(cache, _tmpdir.c_str(), "cache");
Mike Lockwood94afecf2012-10-24 10:45:23 -0700296 }
297
298 // Search for other users and add any cache files from them.
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700299 std::string _tmpdir(create_data_path(uuid) + "/" + SECONDARY_USER_PREFIX);
300 strcpy(tmpdir, _tmpdir.c_str());
301
Mike Lockwood94afecf2012-10-24 10:45:23 -0700302 dirpos = tmpdir + strlen(tmpdir);
303 d = opendir(tmpdir);
304 if (d != NULL) {
305 while ((de = readdir(d))) {
306 if (de->d_type == DT_DIR) {
307 const char *name = de->d_name;
308 /* always skip "." and ".." */
309 if (name[0] == '.') {
310 if (name[1] == 0) continue;
311 if ((name[1] == '.') && (name[2] == 0)) continue;
312 }
313 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
314 strcpy(dirpos, name);
315 //ALOGI("adding cache files from %s\n", tmpdir);
316 add_cache_files(cache, tmpdir, "cache");
317 } else {
318 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
319 }
320 }
321 }
322 closedir(d);
323 }
324
325 // Collect cache files on external storage for all users (if it is mounted as part
326 // of the internal storage).
327 strcpy(tmpdir, android_media_dir.path);
328 dirpos = tmpdir + strlen(tmpdir);
329 d = opendir(tmpdir);
330 if (d != NULL) {
331 while ((de = readdir(d))) {
332 if (de->d_type == DT_DIR) {
333 const char *name = de->d_name;
334 /* skip any dir that doesn't start with a number, so not a user */
335 if (name[0] < '0' || name[0] > '9') {
336 continue;
337 }
338 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
339 strcpy(dirpos, name);
340 if (lookup_media_dir(tmpdir, "Android") == 0
341 && lookup_media_dir(tmpdir, "data") == 0) {
342 //ALOGI("adding cache files from %s\n", tmpdir);
343 add_cache_files(cache, tmpdir, "cache");
344 }
345 } else {
346 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
347 }
348 }
349 }
350 closedir(d);
351 }
352
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700353 clear_cache_files(data_path, cache, free_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700354 finish_cache_collection(cache);
355
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700356 return data_disk_free(data_path) >= free_size ? 0 : -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700357}
358
Narayan Kamath1b400322014-04-11 13:17:00 +0100359int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700360{
361 char dex_path[PKG_PATH_MAX];
362
Jeff Sharkey770180a2014-09-08 17:14:26 -0700363 if (validate_apk_path(path) && validate_system_app_path(path)) {
364 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
365 return -1;
366 }
367
Andreas Gampe02d0de52015-11-11 20:43:16 -0800368 if (!create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700369
370 ALOGV("unlink %s\n", dex_path);
371 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700372 if (errno != ENOENT) {
373 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
374 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700375 return -1;
376 } else {
377 return 0;
378 }
379}
380
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700381int get_app_size(const char *uuid, const char *pkgname, int userid, int flags,
382 const char *apkpath, const char *libdirpath, const char *fwdlock_apkpath,
383 const char *asecpath, const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
384 int64_t *_cachesize, int64_t* _asecsize) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700385 DIR *d;
386 int dfd;
387 struct dirent *de;
388 struct stat s;
389 char path[PKG_PATH_MAX];
390
391 int64_t codesize = 0;
392 int64_t datasize = 0;
393 int64_t cachesize = 0;
394 int64_t asecsize = 0;
395
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700396 /* count the source apk as code -- but only if it's not
397 * on the /system partition and its not on the sdcard. */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700398 if (validate_system_app_path(apkpath) &&
399 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
400 if (stat(apkpath, &s) == 0) {
401 codesize += stat_size(&s);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700402 if (S_ISDIR(s.st_mode)) {
403 d = opendir(apkpath);
404 if (d != NULL) {
405 dfd = dirfd(d);
406 codesize += calculate_dir_size(dfd);
407 closedir(d);
408 }
409 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700410 }
411 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700412
413 /* count the forward locked apk as code if it is given */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700414 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
415 if (stat(fwdlock_apkpath, &s) == 0) {
416 codesize += stat_size(&s);
417 }
418 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700419
420 /* count the cached dexfile as code */
Andreas Gampe02d0de52015-11-11 20:43:16 -0800421 if (create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700422 if (stat(path, &s) == 0) {
423 codesize += stat_size(&s);
424 }
425 }
426
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700427 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700428 if (libdirpath != NULL && libdirpath[0] != '!') {
429 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700430 if (d != NULL) {
431 dfd = dirfd(d);
432 codesize += calculate_dir_size(dfd);
433 closedir(d);
434 }
435 }
436
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700437 /* compute asec size if it is given */
Mike Lockwood94afecf2012-10-24 10:45:23 -0700438 if (asecpath != NULL && asecpath[0] != '!') {
439 if (stat(asecpath, &s) == 0) {
440 asecsize += stat_size(&s);
441 }
442 }
443
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700444 std::vector<userid_t> users;
445 if (userid == -1) {
446 users = get_known_users(uuid);
447 } else {
448 users.push_back(userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700449 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700450
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700451 for (auto user : users) {
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700452 // TODO: handle user_de directories
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700453 if (!(flags & FLAG_CE_STORAGE)) continue;
454
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700455 std::string _pkgdir(create_data_user_package_path(uuid, user, pkgname));
456 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700457
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700458 d = opendir(pkgdir);
459 if (d == NULL) {
460 PLOG(WARNING) << "Failed to open " << pkgdir;
461 continue;
462 }
463 dfd = dirfd(d);
464
465 /* most stuff in the pkgdir is data, except for the "cache"
466 * directory and below, which is cache, and the "lib" directory
467 * and below, which is code...
468 */
469 while ((de = readdir(d))) {
470 const char *name = de->d_name;
471
472 if (de->d_type == DT_DIR) {
473 int subfd;
474 int64_t statsize = 0;
475 int64_t dirsize = 0;
476 /* always skip "." and ".." */
477 if (name[0] == '.') {
478 if (name[1] == 0) continue;
479 if ((name[1] == '.') && (name[2] == 0)) continue;
480 }
481 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
482 statsize = stat_size(&s);
483 }
484 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
485 if (subfd >= 0) {
486 dirsize = calculate_dir_size(subfd);
487 }
488 if(!strcmp(name,"lib")) {
489 codesize += dirsize + statsize;
490 } else if(!strcmp(name,"cache")) {
491 cachesize += dirsize + statsize;
492 } else {
493 datasize += dirsize + statsize;
494 }
495 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
496 // This is the symbolic link to the application's library
497 // code. We'll count this as code instead of data, since
498 // it is not something that the app creates.
499 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
500 codesize += stat_size(&s);
501 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700502 } else {
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700503 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
504 datasize += stat_size(&s);
505 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700506 }
507 }
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700508 closedir(d);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700509 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700510 *_codesize = codesize;
511 *_datasize = datasize;
512 *_cachesize = cachesize;
513 *_asecsize = asecsize;
514 return 0;
515}
516
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700517static int split_count(const char *str)
518{
519 char *ctx;
520 int count = 0;
Andreas Gampe02d0de52015-11-11 20:43:16 -0800521 char buf[kPropertyValueMax];
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700522
523 strncpy(buf, str, sizeof(buf));
524 char *pBuf = buf;
525
526 while(strtok_r(pBuf, " ", &ctx) != NULL) {
527 count++;
528 pBuf = NULL;
529 }
530
531 return count;
532}
533
neo.chae14e084d2015-01-07 18:46:13 +0900534static int split(char *buf, const char **argv)
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700535{
536 char *ctx;
537 int count = 0;
538 char *tok;
539 char *pBuf = buf;
540
541 while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
542 argv[count++] = tok;
543 pBuf = NULL;
544 }
545
546 return count;
547}
548
Alex Light7365a102014-07-21 12:23:48 -0700549static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Andreas Gampe02d0de52015-11-11 20:43:16 -0800550 const char* output_file_name, const char *pkgname ATTRIBUTE_UNUSED, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700551{
552 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100553 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700554
555 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
556 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
557 ALOGE("Instruction set %s longer than max length of %d",
558 instruction_set, MAX_INSTRUCTION_SET_LEN);
559 return;
560 }
561
562 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
563 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
564 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
565 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
566 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
567 // The caller has already gotten all the locks we need.
568 const char* no_lock_arg = "--no-lock-output";
569 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
570 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
571 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700572 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700573 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
574
575 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
576 char* argv[7];
577 argv[0] = (char*) PATCHOAT_BIN;
578 argv[1] = (char*) patched_image_location_arg;
579 argv[2] = (char*) no_lock_arg;
580 argv[3] = instruction_set_arg;
581 argv[4] = output_oat_fd_arg;
582 argv[5] = input_oat_fd_arg;
583 argv[6] = NULL;
584
585 execv(PATCHOAT_BIN, (char* const *)argv);
586 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
587}
588
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700589static bool check_boolean_property(const char* property_name, bool default_value = false) {
Andreas Gampe02d0de52015-11-11 20:43:16 -0800590 char tmp_property_value[kPropertyValueMax];
591 bool have_property = get_property(property_name, tmp_property_value, nullptr) > 0;
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700592 if (!have_property) {
593 return default_value;
594 }
595 return strcmp(tmp_property_value, "true") == 0;
596}
597
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700598static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Calin Juravledf9dadd2015-11-04 14:47:37 +0000599 const char* output_file_name, int swap_fd, const char *instruction_set,
Todd Kennedy12434f82015-09-25 14:45:37 -0700600 bool vm_safe_mode, bool debuggable, bool post_bootcomplete, bool use_jit)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700601{
Calin Juravle8fc73152014-08-19 18:48:50 +0100602 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
603
604 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
605 ALOGE("Instruction set %s longer than max length of %d",
606 instruction_set, MAX_INSTRUCTION_SET_LEN);
607 return;
608 }
609
Andreas Gampe02d0de52015-11-11 20:43:16 -0800610 char dex2oat_Xms_flag[kPropertyValueMax];
611 bool have_dex2oat_Xms_flag = get_property("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700612
Andreas Gampe02d0de52015-11-11 20:43:16 -0800613 char dex2oat_Xmx_flag[kPropertyValueMax];
614 bool have_dex2oat_Xmx_flag = get_property("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700615
Andreas Gampe02d0de52015-11-11 20:43:16 -0800616 char dex2oat_compiler_filter_flag[kPropertyValueMax];
617 bool have_dex2oat_compiler_filter_flag = get_property("dalvik.vm.dex2oat-filter",
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700618 dex2oat_compiler_filter_flag, NULL) > 0;
619
Andreas Gampe02d0de52015-11-11 20:43:16 -0800620 char dex2oat_threads_buf[kPropertyValueMax];
621 bool have_dex2oat_threads_flag = get_property(post_bootcomplete
Andreas Gampe919461c2015-09-28 08:55:01 -0700622 ? "dalvik.vm.dex2oat-threads"
623 : "dalvik.vm.boot-dex2oat-threads",
624 dex2oat_threads_buf,
625 NULL) > 0;
Andreas Gampe02d0de52015-11-11 20:43:16 -0800626 char dex2oat_threads_arg[kPropertyValueMax + 2];
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700627 if (have_dex2oat_threads_flag) {
628 sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf);
629 }
630
Andreas Gampe02d0de52015-11-11 20:43:16 -0800631 char dex2oat_isa_features_key[kPropertyKeyMax];
Calin Juravle8fc73152014-08-19 18:48:50 +0100632 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800633 char dex2oat_isa_features[kPropertyValueMax];
634 bool have_dex2oat_isa_features = get_property(dex2oat_isa_features_key,
Calin Juravle8fc73152014-08-19 18:48:50 +0100635 dex2oat_isa_features, NULL) > 0;
636
Andreas Gampe02d0de52015-11-11 20:43:16 -0800637 char dex2oat_isa_variant_key[kPropertyKeyMax];
Ian Rogers16a95b22014-11-08 16:58:13 -0800638 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800639 char dex2oat_isa_variant[kPropertyValueMax];
640 bool have_dex2oat_isa_variant = get_property(dex2oat_isa_variant_key,
Ian Rogers16a95b22014-11-08 16:58:13 -0800641 dex2oat_isa_variant, NULL) > 0;
642
neo.chae14e084d2015-01-07 18:46:13 +0900643 const char *dex2oat_norelocation = "-Xnorelocate";
644 bool have_dex2oat_relocation_skip_flag = false;
645
Andreas Gampe02d0de52015-11-11 20:43:16 -0800646 char dex2oat_flags[kPropertyValueMax];
647 int dex2oat_flags_count = get_property("dalvik.vm.dex2oat-flags",
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700648 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800649 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
650
Brian Carlstrom538998f2014-07-30 14:37:11 -0700651 // If we booting without the real /data, don't spend time compiling.
Andreas Gampe02d0de52015-11-11 20:43:16 -0800652 char vold_decrypt[kPropertyValueMax];
653 bool have_vold_decrypt = get_property("vold.decrypt", vold_decrypt, "") > 0;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700654 bool skip_compilation = (have_vold_decrypt &&
655 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
656 (strcmp(vold_decrypt, "1") == 0)));
657
David Srbecky528c8dd2015-05-28 16:55:50 +0100658 bool generate_debug_info = check_boolean_property("debug.generate-debug-info");
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700659
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700660 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700661
Brian Carlstrom53e07762014-06-27 14:15:19 -0700662 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700663
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700664 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100665
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700666 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
667 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
668 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700669 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100670 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Andreas Gampe02d0de52015-11-11 20:43:16 -0800671 char instruction_set_variant_arg[strlen("--instruction-set-variant=") + kPropertyValueMax];
672 char instruction_set_features_arg[strlen("--instruction-set-features=") + kPropertyValueMax];
673 char dex2oat_Xms_arg[strlen("-Xms") + kPropertyValueMax];
674 char dex2oat_Xmx_arg[strlen("-Xmx") + kPropertyValueMax];
675 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + kPropertyValueMax];
Andreas Gampee1c01352014-12-10 16:41:11 -0800676 bool have_dex2oat_swap_fd = false;
677 char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700678
679 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
680 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
681 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
682 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100683 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Ian Rogers16a95b22014-11-08 16:58:13 -0800684 sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
Calin Juravle8fc73152014-08-19 18:48:50 +0100685 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Andreas Gampee1c01352014-12-10 16:41:11 -0800686 if (swap_fd >= 0) {
687 have_dex2oat_swap_fd = true;
688 sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
689 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100690
Todd Kennedy12434f82015-09-25 14:45:37 -0700691 // use the JIT if either it's specified as a dexopt flag or if the property is set
692 use_jit = use_jit || check_boolean_property("debug.usejit");
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700693 if (have_dex2oat_Xms_flag) {
694 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
695 }
696 if (have_dex2oat_Xmx_flag) {
697 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
698 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700699 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700700 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700701 have_dex2oat_compiler_filter_flag = true;
neo.chae14e084d2015-01-07 18:46:13 +0900702 have_dex2oat_relocation_skip_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100703 } else if (vm_safe_mode) {
704 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100705 have_dex2oat_compiler_filter_flag = true;
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700706 } else if (use_jit) {
707 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-at-runtime");
708 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700709 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700710 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
711 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700712
Andreas Gampe598c25e2015-03-03 09:15:06 -0800713 // Check whether all apps should be compiled debuggable.
714 if (!debuggable) {
Andreas Gampe02d0de52015-11-11 20:43:16 -0800715 char prop_buf[kPropertyValueMax];
Andreas Gampe598c25e2015-03-03 09:15:06 -0800716 debuggable =
Andreas Gampe02d0de52015-11-11 20:43:16 -0800717 (get_property("dalvik.vm.always_debuggable", prop_buf, "0") > 0) &&
Andreas Gampe598c25e2015-03-03 09:15:06 -0800718 (prop_buf[0] == '1');
719 }
720
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700721 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100722
neo.chae14e084d2015-01-07 18:46:13 +0900723 const char* argv[7 // program name, mandatory arguments and the final NULL
724 + (have_dex2oat_isa_variant ? 1 : 0)
725 + (have_dex2oat_isa_features ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900726 + (have_dex2oat_Xms_flag ? 2 : 0)
727 + (have_dex2oat_Xmx_flag ? 2 : 0)
728 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700729 + (have_dex2oat_threads_flag ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900730 + (have_dex2oat_swap_fd ? 1 : 0)
731 + (have_dex2oat_relocation_skip_flag ? 2 : 0)
David Srbecky528c8dd2015-05-28 16:55:50 +0100732 + (generate_debug_info ? 1 : 0)
Andreas Gampe598c25e2015-03-03 09:15:06 -0800733 + (debuggable ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900734 + dex2oat_flags_count];
Calin Juravle4fdff462014-06-06 16:58:43 +0100735 int i = 0;
neo.chae14e084d2015-01-07 18:46:13 +0900736 argv[i++] = DEX2OAT_BIN;
Calin Juravle4fdff462014-06-06 16:58:43 +0100737 argv[i++] = zip_fd_arg;
738 argv[i++] = zip_location_arg;
739 argv[i++] = oat_fd_arg;
740 argv[i++] = oat_location_arg;
741 argv[i++] = instruction_set_arg;
Ian Rogers16a95b22014-11-08 16:58:13 -0800742 if (have_dex2oat_isa_variant) {
743 argv[i++] = instruction_set_variant_arg;
744 }
Calin Juravle8fc73152014-08-19 18:48:50 +0100745 if (have_dex2oat_isa_features) {
746 argv[i++] = instruction_set_features_arg;
747 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700748 if (have_dex2oat_Xms_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900749 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700750 argv[i++] = dex2oat_Xms_arg;
751 }
752 if (have_dex2oat_Xmx_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900753 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700754 argv[i++] = dex2oat_Xmx_arg;
755 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700756 if (have_dex2oat_compiler_filter_flag) {
757 argv[i++] = dex2oat_compiler_filter_arg;
758 }
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700759 if (have_dex2oat_threads_flag) {
760 argv[i++] = dex2oat_threads_arg;
761 }
Andreas Gampee1c01352014-12-10 16:41:11 -0800762 if (have_dex2oat_swap_fd) {
763 argv[i++] = dex2oat_swap_fd;
764 }
David Srbecky528c8dd2015-05-28 16:55:50 +0100765 if (generate_debug_info) {
766 argv[i++] = "--generate-debug-info";
Andreas Gampe3822b8b2015-04-24 14:30:04 -0700767 }
Andreas Gampe598c25e2015-03-03 09:15:06 -0800768 if (debuggable) {
769 argv[i++] = "--debuggable";
770 }
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700771 if (dex2oat_flags_count) {
772 i += split(dex2oat_flags, argv + i);
Calin Juravle4fdff462014-06-06 16:58:43 +0100773 }
neo.chae14e084d2015-01-07 18:46:13 +0900774 if (have_dex2oat_relocation_skip_flag) {
775 argv[i++] = RUNTIME_ARG;
776 argv[i++] = dex2oat_norelocation;
777 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700778 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100779 argv[i] = NULL;
780
neo.chae14e084d2015-01-07 18:46:13 +0900781 execv(DEX2OAT_BIN, (char * const *)argv);
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700782 ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700783}
784
Andreas Gampee1c01352014-12-10 16:41:11 -0800785/*
Andreas Gampec968c012015-07-16 15:55:41 -0700786 * Whether dexopt should use a swap file when compiling an APK.
787 *
788 * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision
789 * itself, anyways).
790 *
791 * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true".
792 *
793 * Otherwise, return true if this is a low-mem device.
794 *
795 * Otherwise, return default value.
Andreas Gampee1c01352014-12-10 16:41:11 -0800796 */
Andreas Gampec968c012015-07-16 15:55:41 -0700797static bool kAlwaysProvideSwapFile = false;
798static bool kDefaultProvideSwapFile = true;
Andreas Gampee1c01352014-12-10 16:41:11 -0800799
800static bool ShouldUseSwapFileForDexopt() {
801 if (kAlwaysProvideSwapFile) {
802 return true;
803 }
804
Andreas Gampec968c012015-07-16 15:55:41 -0700805 // Check the "override" property. If it exists, return value == "true".
Andreas Gampe02d0de52015-11-11 20:43:16 -0800806 char dex2oat_prop_buf[kPropertyValueMax];
807 if (get_property("dalvik.vm.dex2oat-swap", dex2oat_prop_buf, "") > 0) {
Andreas Gampec968c012015-07-16 15:55:41 -0700808 if (strcmp(dex2oat_prop_buf, "true") == 0) {
809 return true;
810 } else {
811 return false;
812 }
813 }
814
815 // Shortcut for default value. This is an implementation optimization for the process sketched
816 // above. If the default value is true, we can avoid to check whether this is a low-mem device,
817 // as low-mem is never returning false. The compiler will optimize this away if it can.
818 if (kDefaultProvideSwapFile) {
819 return true;
820 }
821
822 bool is_low_mem = check_boolean_property("ro.config.low_ram");
823 if (is_low_mem) {
824 return true;
825 }
826
827 // Default value must be false here.
828 return kDefaultProvideSwapFile;
Andreas Gampee1c01352014-12-10 16:41:11 -0800829}
830
Andreas Gampe94dd3d32015-09-14 16:33:11 -0700831static void SetDex2OatAndPatchOatScheduling(bool set_to_bg) {
832 if (set_to_bg) {
833 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
834 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
835 exit(70);
836 }
837 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
838 ALOGE("setpriority failed: %s\n", strerror(errno));
839 exit(71);
840 }
841 }
842}
843
Todd Kennedy76e767c2015-09-25 07:47:47 -0700844int dexopt(const char *apk_path, uid_t uid, const char *pkgname, const char *instruction_set,
845 int dexopt_needed, const char* oat_dir, int dexopt_flags)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700846{
847 struct utimbuf ut;
Fyodor Kupolov26ff93c2015-04-02 16:59:10 -0700848 struct stat input_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700849 char out_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800850 char swap_file_name[PKG_PATH_MAX];
Alex Light7365a102014-07-21 12:23:48 -0700851 const char *input_file;
852 char in_odex_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800853 int res, input_fd=-1, out_fd=-1, swap_fd=-1;
Todd Kennedy76e767c2015-09-25 07:47:47 -0700854 bool is_public = (dexopt_flags & DEXOPT_PUBLIC) != 0;
855 bool vm_safe_mode = (dexopt_flags & DEXOPT_SAFEMODE) != 0;
856 bool debuggable = (dexopt_flags & DEXOPT_DEBUGGABLE) != 0;
857 bool boot_complete = (dexopt_flags & DEXOPT_BOOTCOMPLETE) != 0;
Todd Kennedy12434f82015-09-25 14:45:37 -0700858 bool use_jit = (dexopt_flags & DEXOPT_USEJIT) != 0;
Todd Kennedy76e767c2015-09-25 07:47:47 -0700859
Todd Kennedye296e002015-11-16 14:41:36 -0800860 if ((dexopt_flags & ~DEXOPT_MASK) != 0) {
Todd Kennedy76e767c2015-09-25 07:47:47 -0700861 LOG_FATAL("dexopt flags contains unknown fields\n");
862 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700863
Andreas Gampee1c01352014-12-10 16:41:11 -0800864 // Early best-effort check whether we can fit the the path into our buffers.
865 // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
866 // without a swap file, if necessary.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700867 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800868 ALOGE("apk_path too long '%s'\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700869 return -1;
870 }
871
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800872 if (oat_dir != NULL && oat_dir[0] != '!') {
873 if (validate_apk_path(oat_dir)) {
874 ALOGE("invalid oat_dir '%s'\n", oat_dir);
875 return -1;
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800876 }
Andreas Gampe02d0de52015-11-11 20:43:16 -0800877 if (!calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800878 return -1;
879 }
880 } else {
Andreas Gampe02d0de52015-11-11 20:43:16 -0800881 if (!create_cache_path(out_path, apk_path, instruction_set)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800882 return -1;
883 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700884 }
885
Richard Uhlerc92fb622015-03-26 15:47:38 -0700886 switch (dexopt_needed) {
887 case DEXOPT_DEX2OAT_NEEDED:
888 input_file = apk_path;
889 break;
890
891 case DEXOPT_PATCHOAT_NEEDED:
892 if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
893 return -1;
894 }
895 input_file = in_odex_path;
896 break;
897
898 case DEXOPT_SELF_PATCHOAT_NEEDED:
899 input_file = out_path;
900 break;
901
902 default:
903 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
904 exit(72);
Alex Light7365a102014-07-21 12:23:48 -0700905 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700906
Alex Light7365a102014-07-21 12:23:48 -0700907 memset(&input_stat, 0, sizeof(input_stat));
908 stat(input_file, &input_stat);
909
910 input_fd = open(input_file, O_RDONLY, 0);
911 if (input_fd < 0) {
912 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700913 return -1;
914 }
915
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700916 unlink(out_path);
917 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
918 if (out_fd < 0) {
919 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700920 goto fail;
921 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700922 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700923 S_IRUSR|S_IWUSR|S_IRGRP |
924 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700925 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700926 goto fail;
927 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700928 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
929 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700930 goto fail;
931 }
932
Andreas Gampee1c01352014-12-10 16:41:11 -0800933 // Create a swap file if necessary.
Richard Uhlerc92fb622015-03-26 15:47:38 -0700934 if (ShouldUseSwapFileForDexopt()) {
Andreas Gampee1c01352014-12-10 16:41:11 -0800935 // Make sure there really is enough space.
936 size_t out_len = strlen(out_path);
937 if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
938 strcpy(swap_file_name, out_path);
939 strcpy(swap_file_name + strlen(out_path), ".swap");
940 unlink(swap_file_name);
941 swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600);
942 if (swap_fd < 0) {
943 // Could not create swap file. Optimistically go on and hope that we can compile
944 // without it.
945 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
946 } else {
947 // Immediately unlink. We don't really want to hit flash.
948 unlink(swap_file_name);
949 }
950 } else {
951 // Swap file path is too long. Try to run without.
952 ALOGE("installd could not create swap file for path %s during dexopt\n", out_path);
953 }
954 }
Dave Allisond9370732014-01-30 14:19:23 -0800955
Alex Light7365a102014-07-21 12:23:48 -0700956 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700957
958 pid_t pid;
959 pid = fork();
960 if (pid == 0) {
961 /* child -- drop privileges before continuing */
962 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700963 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700964 exit(64);
965 }
966 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700967 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700968 exit(65);
969 }
970 // drop capabilities
971 struct __user_cap_header_struct capheader;
972 struct __user_cap_data_struct capdata[2];
973 memset(&capheader, 0, sizeof(capheader));
974 memset(&capdata, 0, sizeof(capdata));
975 capheader.version = _LINUX_CAPABILITY_VERSION_3;
976 if (capset(&capheader, &capdata[0]) < 0) {
977 ALOGE("capset failed: %s\n", strerror(errno));
978 exit(66);
979 }
Andreas Gampe13f14192015-09-21 13:21:30 -0700980 SetDex2OatAndPatchOatScheduling(boot_complete);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700981 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
982 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700983 exit(67);
984 }
985
Richard Uhlerc92fb622015-03-26 15:47:38 -0700986 if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED
987 || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) {
Andreas Gampebd872e42014-12-15 11:41:11 -0800988 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
Richard Uhlerc92fb622015-03-26 15:47:38 -0700989 } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800990 const char *input_file_name = strrchr(input_file, '/');
991 if (input_file_name == NULL) {
992 input_file_name = input_file;
993 } else {
994 input_file_name++;
995 }
Calin Juravledf9dadd2015-11-04 14:47:37 +0000996 run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd,
Todd Kennedy12434f82015-09-25 14:45:37 -0700997 instruction_set, vm_safe_mode, debuggable, boot_complete, use_jit);
Richard Uhlerc92fb622015-03-26 15:47:38 -0700998 } else {
999 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1000 exit(73);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001001 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001002 exit(68); /* only get here on exec failure */
1003 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001004 res = wait_child(pid);
1005 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001006 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001007 } else {
Alex Light7365a102014-07-21 12:23:48 -07001008 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001009 goto fail;
1010 }
1011 }
1012
Alex Light7365a102014-07-21 12:23:48 -07001013 ut.actime = input_stat.st_atime;
1014 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001015 utime(out_path, &ut);
1016
1017 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001018 close(input_fd);
Andreas Gampee1c01352014-12-10 16:41:11 -08001019 if (swap_fd != -1) {
1020 close(swap_fd);
1021 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001022 return 0;
1023
1024fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001025 if (out_fd >= 0) {
1026 close(out_fd);
1027 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001028 }
Alex Light7365a102014-07-21 12:23:48 -07001029 if (input_fd >= 0) {
1030 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001031 }
1032 return -1;
1033}
1034
Narayan Kamath091ea772014-11-10 15:03:46 +00001035int mark_boot_complete(const char* instruction_set)
1036{
1037 char boot_marker_path[PKG_PATH_MAX];
Andreas Gampe02d0de52015-11-11 20:43:16 -08001038 sprintf(boot_marker_path,
1039 "%s/%s/%s/.booting",
1040 android_data_dir.path,
1041 DALVIK_CACHE,
1042 instruction_set);
Narayan Kamath091ea772014-11-10 15:03:46 +00001043
1044 ALOGV("mark_boot_complete : %s", boot_marker_path);
1045 if (unlink(boot_marker_path) != 0) {
1046 ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1047 strerror(errno));
1048 return -1;
1049 }
1050
1051 return 0;
1052}
1053
Mike Lockwood94afecf2012-10-24 10:45:23 -07001054void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1055 struct stat* statbuf)
1056{
1057 while (path[basepos] != 0) {
1058 if (path[basepos] == '/') {
1059 path[basepos] = 0;
1060 if (lstat(path, statbuf) < 0) {
1061 ALOGV("Making directory: %s\n", path);
1062 if (mkdir(path, mode) == 0) {
1063 chown(path, uid, gid);
1064 } else {
1065 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1066 }
1067 }
1068 path[basepos] = '/';
1069 basepos++;
1070 }
1071 basepos++;
1072 }
1073}
1074
1075int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1076 int dstuid, int dstgid, struct stat* statbuf)
1077{
1078 DIR *d;
1079 struct dirent *de;
1080 int res;
1081
1082 int srcend = strlen(srcpath);
1083 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001084
Mike Lockwood94afecf2012-10-24 10:45:23 -07001085 if (lstat(srcpath, statbuf) < 0) {
1086 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1087 return 1;
1088 }
Dave Allisond9370732014-01-30 14:19:23 -08001089
Mike Lockwood94afecf2012-10-24 10:45:23 -07001090 if ((statbuf->st_mode&S_IFDIR) == 0) {
1091 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1092 dstuid, dstgid, statbuf);
1093 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1094 if (rename(srcpath, dstpath) >= 0) {
1095 if (chown(dstpath, dstuid, dstgid) < 0) {
1096 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1097 unlink(dstpath);
1098 return 1;
1099 }
1100 } else {
1101 ALOGW("Unable to rename %s to %s: %s\n",
1102 srcpath, dstpath, strerror(errno));
1103 return 1;
1104 }
1105 return 0;
1106 }
1107
1108 d = opendir(srcpath);
1109 if (d == NULL) {
1110 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1111 return 1;
1112 }
1113
1114 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001115
Mike Lockwood94afecf2012-10-24 10:45:23 -07001116 while ((de = readdir(d))) {
1117 const char *name = de->d_name;
1118 /* always skip "." and ".." */
1119 if (name[0] == '.') {
1120 if (name[1] == 0) continue;
1121 if ((name[1] == '.') && (name[2] == 0)) continue;
1122 }
Dave Allisond9370732014-01-30 14:19:23 -08001123
Mike Lockwood94afecf2012-10-24 10:45:23 -07001124 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1125 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1126 continue;
1127 }
Dave Allisond9370732014-01-30 14:19:23 -08001128
Mike Lockwood94afecf2012-10-24 10:45:23 -07001129 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1130 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1131 continue;
1132 }
Dave Allisond9370732014-01-30 14:19:23 -08001133
Mike Lockwood94afecf2012-10-24 10:45:23 -07001134 srcpath[srcend] = dstpath[dstend] = '/';
1135 strcpy(srcpath+srcend+1, name);
1136 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001137
Mike Lockwood94afecf2012-10-24 10:45:23 -07001138 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1139 res = 1;
1140 }
Dave Allisond9370732014-01-30 14:19:23 -08001141
Mike Lockwood94afecf2012-10-24 10:45:23 -07001142 // Note: we will be leaving empty directories behind in srcpath,
1143 // but that is okay, the package manager will be erasing all of the
1144 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001145
Mike Lockwood94afecf2012-10-24 10:45:23 -07001146 srcpath[srcend] = dstpath[dstend] = 0;
1147 }
Dave Allisond9370732014-01-30 14:19:23 -08001148
Mike Lockwood94afecf2012-10-24 10:45:23 -07001149 closedir(d);
1150 return res;
1151}
1152
1153int movefiles()
1154{
1155 DIR *d;
1156 int dfd, subfd;
1157 struct dirent *de;
1158 struct stat s;
1159 char buf[PKG_PATH_MAX+1];
1160 int bufp, bufe, bufi, readlen;
1161
1162 char srcpkg[PKG_NAME_MAX];
1163 char dstpkg[PKG_NAME_MAX];
1164 char srcpath[PKG_PATH_MAX];
1165 char dstpath[PKG_PATH_MAX];
1166 int dstuid=-1, dstgid=-1;
1167 int hasspace;
1168
1169 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1170 if (d == NULL) {
1171 goto done;
1172 }
1173 dfd = dirfd(d);
1174
1175 /* Iterate through all files in the directory, executing the
1176 * file movements requested there-in.
1177 */
1178 while ((de = readdir(d))) {
1179 const char *name = de->d_name;
1180
1181 if (de->d_type == DT_DIR) {
1182 continue;
1183 } else {
1184 subfd = openat(dfd, name, O_RDONLY);
1185 if (subfd < 0) {
1186 ALOGW("Unable to open update commands at %s%s\n",
1187 UPDATE_COMMANDS_DIR_PREFIX, name);
1188 continue;
1189 }
Dave Allisond9370732014-01-30 14:19:23 -08001190
Mike Lockwood94afecf2012-10-24 10:45:23 -07001191 bufp = 0;
1192 bufe = 0;
1193 buf[PKG_PATH_MAX] = 0;
1194 srcpkg[0] = dstpkg[0] = 0;
1195 while (1) {
1196 bufi = bufp;
1197 while (bufi < bufe && buf[bufi] != '\n') {
1198 bufi++;
1199 }
1200 if (bufi < bufe) {
1201 buf[bufi] = 0;
1202 ALOGV("Processing line: %s\n", buf+bufp);
1203 hasspace = 0;
1204 while (bufp < bufi && isspace(buf[bufp])) {
1205 hasspace = 1;
1206 bufp++;
1207 }
1208 if (buf[bufp] == '#' || bufp == bufi) {
1209 // skip comments and empty lines.
1210 } else if (hasspace) {
1211 if (dstpkg[0] == 0) {
1212 ALOGW("Path before package line in %s%s: %s\n",
1213 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1214 } else if (srcpkg[0] == 0) {
1215 // Skip -- source package no longer exists.
1216 } else {
1217 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1218 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1219 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1220 movefileordir(srcpath, dstpath,
1221 strlen(dstpath)-strlen(buf+bufp),
1222 dstuid, dstgid, &s);
1223 }
1224 }
1225 } else {
1226 char* div = strchr(buf+bufp, ':');
1227 if (div == NULL) {
1228 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1229 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1230 } else {
1231 *div = 0;
1232 div++;
1233 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1234 strcpy(dstpkg, buf+bufp);
1235 } else {
1236 srcpkg[0] = dstpkg[0] = 0;
1237 ALOGW("Package name too long in %s%s: %s\n",
1238 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1239 }
1240 if (strlen(div) < PKG_NAME_MAX) {
1241 strcpy(srcpkg, div);
1242 } else {
1243 srcpkg[0] = dstpkg[0] = 0;
1244 ALOGW("Package name too long in %s%s: %s\n",
1245 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1246 }
1247 if (srcpkg[0] != 0) {
1248 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1249 if (lstat(srcpath, &s) < 0) {
1250 // Package no longer exists -- skip.
1251 srcpkg[0] = 0;
1252 }
1253 } else {
1254 srcpkg[0] = 0;
1255 ALOGW("Can't create path %s in %s%s\n",
1256 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1257 }
1258 if (srcpkg[0] != 0) {
1259 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1260 if (lstat(dstpath, &s) == 0) {
1261 dstuid = s.st_uid;
1262 dstgid = s.st_gid;
1263 } else {
1264 // Destination package doesn't
1265 // exist... due to original-package,
1266 // this is normal, so don't be
1267 // noisy about it.
1268 srcpkg[0] = 0;
1269 }
1270 } else {
1271 srcpkg[0] = 0;
1272 ALOGW("Can't create path %s in %s%s\n",
1273 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1274 }
1275 }
1276 ALOGV("Transfering from %s to %s: uid=%d\n",
1277 srcpkg, dstpkg, dstuid);
1278 }
1279 }
1280 }
1281 bufp = bufi+1;
1282 } else {
1283 if (bufp == 0) {
1284 if (bufp < bufe) {
1285 ALOGW("Line too long in %s%s, skipping: %s\n",
1286 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1287 }
1288 } else if (bufp < bufe) {
1289 memcpy(buf, buf+bufp, bufe-bufp);
1290 bufe -= bufp;
1291 bufp = 0;
1292 }
1293 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1294 if (readlen < 0) {
1295 ALOGW("Failure reading update commands in %s%s: %s\n",
1296 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1297 break;
1298 } else if (readlen == 0) {
1299 break;
1300 }
1301 bufe += readlen;
1302 buf[bufe] = 0;
1303 ALOGV("Read buf: %s\n", buf);
1304 }
1305 }
1306 close(subfd);
1307 }
1308 }
1309 closedir(d);
1310done:
1311 return 0;
1312}
1313
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001314int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001315{
Mike Lockwood94afecf2012-10-24 10:45:23 -07001316 struct stat s, libStat;
1317 int rc = 0;
1318
Jeff Sharkeyd7921182015-04-30 15:58:19 -07001319 std::string _pkgdir(create_data_user_package_path(uuid, userId, pkgname));
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001320 std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
1321
1322 const char* pkgdir = _pkgdir.c_str();
1323 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001324
1325 if (stat(pkgdir, &s) < 0) return -1;
1326
1327 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1328 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1329 return -1;
1330 }
1331
1332 if (chmod(pkgdir, 0700) < 0) {
1333 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1334 rc = -1;
1335 goto out;
1336 }
1337
1338 if (lstat(libsymlink, &libStat) < 0) {
1339 if (errno != ENOENT) {
1340 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1341 rc = -1;
1342 goto out;
1343 }
1344 } else {
1345 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001346 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001347 rc = -1;
1348 goto out;
1349 }
1350 } else if (S_ISLNK(libStat.st_mode)) {
1351 if (unlink(libsymlink) < 0) {
1352 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1353 rc = -1;
1354 goto out;
1355 }
1356 }
1357 }
1358
1359 if (symlink(asecLibDir, libsymlink) < 0) {
1360 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1361 strerror(errno));
1362 rc = -errno;
1363 goto out;
1364 }
1365
1366out:
1367 if (chmod(pkgdir, s.st_mode) < 0) {
1368 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1369 rc = -errno;
1370 }
1371
1372 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1373 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1374 return -errno;
1375 }
1376
1377 return rc;
1378}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001379
1380static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1381{
1382 static const char *IDMAP_BIN = "/system/bin/idmap";
1383 static const size_t MAX_INT_LEN = 32;
1384 char idmap_str[MAX_INT_LEN];
1385
1386 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1387
1388 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1389 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1390}
1391
1392// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1393// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1394static int flatten_path(const char *prefix, const char *suffix,
1395 const char *overlay_path, char *idmap_path, size_t N)
1396{
1397 if (overlay_path == NULL || idmap_path == NULL) {
1398 return -1;
1399 }
1400 const size_t len_overlay_path = strlen(overlay_path);
1401 // will access overlay_path + 1 further below; requires absolute path
1402 if (len_overlay_path < 2 || *overlay_path != '/') {
1403 return -1;
1404 }
1405 const size_t len_idmap_root = strlen(prefix);
1406 const size_t len_suffix = strlen(suffix);
1407 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1408 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1409 // additions below would cause overflow
1410 return -1;
1411 }
1412 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1413 return -1;
1414 }
1415 memset(idmap_path, 0, N);
1416 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1417 char *ch = idmap_path + len_idmap_root;
1418 while (*ch != '\0') {
1419 if (*ch == '/') {
1420 *ch = '@';
1421 }
1422 ++ch;
1423 }
1424 return 0;
1425}
1426
1427int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1428{
1429 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1430
1431 int idmap_fd = -1;
1432 char idmap_path[PATH_MAX];
1433
1434 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1435 idmap_path, sizeof(idmap_path)) == -1) {
1436 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1437 goto fail;
1438 }
1439
1440 unlink(idmap_path);
1441 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1442 if (idmap_fd < 0) {
1443 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1444 goto fail;
1445 }
1446 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1447 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1448 goto fail;
1449 }
1450 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1451 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1452 goto fail;
1453 }
1454
1455 pid_t pid;
1456 pid = fork();
1457 if (pid == 0) {
1458 /* child -- drop privileges before continuing */
1459 if (setgid(uid) != 0) {
1460 ALOGE("setgid(%d) failed during idmap\n", uid);
1461 exit(1);
1462 }
1463 if (setuid(uid) != 0) {
1464 ALOGE("setuid(%d) failed during idmap\n", uid);
1465 exit(1);
1466 }
1467 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1468 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1469 exit(1);
1470 }
1471
1472 run_idmap(target_apk, overlay_apk, idmap_fd);
1473 exit(1); /* only if exec call to idmap failed */
1474 } else {
1475 int status = wait_child(pid);
1476 if (status != 0) {
1477 ALOGE("idmap failed, status=0x%04x\n", status);
1478 goto fail;
1479 }
1480 }
1481
1482 close(idmap_fd);
1483 return 0;
1484fail:
1485 if (idmap_fd >= 0) {
1486 close(idmap_fd);
1487 unlink(idmap_path);
1488 }
1489 return -1;
1490}
Robert Craige9887e42014-02-20 10:25:56 -05001491
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001492int restorecon_app_data(const char* uuid, const char* pkgName, userid_t userid, int flags,
1493 appid_t appid, const char* seinfo) {
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001494 int res = 0;
Robert Craige9887e42014-02-20 10:25:56 -05001495
Robert Craigda30dc72014-03-27 10:21:12 -04001496 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001497 unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE;
Robert Craigda30dc72014-03-27 10:21:12 -04001498
1499 if (!pkgName || !seinfo) {
1500 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001501 return -1;
1502 }
1503
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001504 uid_t uid = multiuser_get_uid(userid, appid);
1505 if (flags & FLAG_CE_STORAGE) {
1506 auto path = create_data_user_package_path(uuid, userid, pkgName);
1507 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
1508 PLOG(ERROR) << "restorecon failed for " << path;
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001509 res = -1;
1510 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001511 }
1512 if (flags & FLAG_DE_STORAGE) {
1513 auto path = create_data_user_de_package_path(uuid, userid, pkgName);
1514 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
1515 PLOG(ERROR) << "restorecon failed for " << path;
Jeff Sharkeyea0e4b12015-11-19 15:35:27 -07001516 // TODO: include result once 25796509 is fixed
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001517 }
Robert Craige9887e42014-02-20 10:25:56 -05001518 }
1519
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001520 return res;
Robert Craige9887e42014-02-20 10:25:56 -05001521}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001522
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001523int create_oat_dir(const char* oat_dir, const char* instruction_set)
1524{
1525 char oat_instr_dir[PKG_PATH_MAX];
1526
1527 if (validate_apk_path(oat_dir)) {
1528 ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
1529 return -1;
1530 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001531 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001532 return -1;
1533 }
1534 if (selinux_android_restorecon(oat_dir, 0)) {
1535 ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
1536 return -1;
1537 }
1538 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001539 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001540 return -1;
1541 }
1542 return 0;
1543}
1544
1545int rm_package_dir(const char* apk_path)
1546{
1547 if (validate_apk_path(apk_path)) {
1548 ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
1549 return -1;
1550 }
1551 return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */);
1552}
1553
Narayan Kamathd845c962015-06-04 13:20:27 +01001554int link_file(const char* relative_path, const char* from_base, const char* to_base) {
1555 char from_path[PKG_PATH_MAX];
1556 char to_path[PKG_PATH_MAX];
1557 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
1558 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
1559
1560 if (validate_apk_path_subdirs(from_path)) {
1561 ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path);
1562 return -1;
1563 }
1564
1565 if (validate_apk_path_subdirs(to_path)) {
1566 ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path);
1567 return -1;
1568 }
1569
1570 const int ret = link(from_path, to_path);
1571 if (ret < 0) {
1572 ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno));
1573 return -1;
1574 }
1575
1576 return 0;
1577}
1578
Andreas Gampe02d0de52015-11-11 20:43:16 -08001579} // namespace installd
1580} // namespace android