2012-10-24 17:45:23 +00:00
|
|
|
/*
|
|
|
|
**
|
|
|
|
** Copyright 2008, The Android Open Source Project
|
|
|
|
**
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
**
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
**
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "installd"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <utime.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2015-04-08 01:14:05 +00:00
|
|
|
#include <string>
|
2015-04-09 03:56:42 +00:00
|
|
|
#include <vector>
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
#include <cutils/fs.h>
|
|
|
|
#include <cutils/sockets.h>
|
|
|
|
#include <cutils/log.h>
|
|
|
|
#include <cutils/properties.h>
|
|
|
|
#include <cutils/multiuser.h>
|
|
|
|
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2014-11-20 20:54:21 +00:00
|
|
|
#if defined(__APPLE__)
|
2012-10-24 17:45:23 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#else
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SOCKET_PATH "installd"
|
|
|
|
|
|
|
|
|
|
|
|
/* elements combined with a valid package name to form paths */
|
|
|
|
|
|
|
|
#define PRIMARY_USER_PREFIX "data/"
|
|
|
|
#define SECONDARY_USER_PREFIX "user/"
|
|
|
|
|
|
|
|
#define PKG_DIR_POSTFIX ""
|
|
|
|
|
|
|
|
#define PKG_LIB_POSTFIX "/lib"
|
|
|
|
|
|
|
|
#define CACHE_DIR_POSTFIX "/cache"
|
2014-07-16 04:49:51 +00:00
|
|
|
#define CODE_CACHE_DIR_POSTFIX "/code_cache"
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
#define APP_SUBDIR "app/" // sub-directory under ANDROID_DATA
|
2014-09-09 00:14:26 +00:00
|
|
|
#define PRIV_APP_SUBDIR "priv-app/" // sub-directory under ANDROID_DATA
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
#define APP_LIB_SUBDIR "app-lib/" // sub-directory under ANDROID_DATA
|
|
|
|
|
|
|
|
#define MEDIA_SUBDIR "media/" // sub-directory under ANDROID_DATA
|
|
|
|
|
|
|
|
/* other handy constants */
|
|
|
|
|
|
|
|
#define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA
|
|
|
|
|
|
|
|
#define DALVIK_CACHE_PREFIX "/data/dalvik-cache/"
|
|
|
|
#define DALVIK_CACHE_POSTFIX "/classes.dex"
|
|
|
|
|
|
|
|
#define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/"
|
|
|
|
|
Runtime resource overlay, iteration 2
Support any number of overlay packages. Support any target package.
UPDATED PACKAGE MATCHING
------------------------
In Runtime resource overlay, iteration 1, only a single overlay package
was considered. Package matching was based on file paths:
/vendor/overlay/system/framework-res.apk corresponded to
/system/framework-res.apk. Introduce a more flexible matching scheme
where any package is an overlay package if its manifest includes
<overlay targetPackage="com.target.package"/>
For security reasons, an overlay package must fulfill certain criteria
to take effect: see below.
THE IDMAP TOOL AND IDMAP FILES
------------------------------
Idmap files are created by the 'idmap' binary; idmap files must be
present when loading packages. For the Android system, Zygote calls
'idmap' as part of the resource pre-loading. For application packages,
'idmap' is invoked via 'installd' during package installation (similar
to 'dexopt').
UPDATED FLOW
------------
The following is an outline of the start-up sequences for the Android
system and Android apps. Steps marked with '+' are introduced by this
commit.
Zygote initialization
Initial AssetManager object created
+ idmap --scan creates idmaps for overlays targeting 'android', \
stores list of overlays in /data/resource-cache/overlays.list
AssetManager caches framework-res.apk
+ AssetManager caches overlay packages listed in overlays.list
Android boot
New AssetManager's ResTable acquired
AssetManager re-uses cached framework-res.apk
+ AssetManager re-uses cached 'android' overlays (if any)
App boot
ActivityThread prepares AssetManager to load app.apk
+ ActivityThread prepares AssetManager to load app overlays (if any)
New AssetManager's ResTable acquired as per Android boot
SECURITY
--------
Overlay packages are required to be pre-loaded (in /vendor/overlay).
These packages are trusted by definition. A future iteration of runtime
resource overlay may add support for downloaded overlays, which would
likely require target and overlay signatures match for the overlay to
be trusted.
LOOKUP PRIORITY
---------------
During resource lookup, packages are sequentially queried to provide a
best match, given the constraints of the current configuration. If any
package provide a better match than what has been found so far, it
replaces the previous match. The target package is always queried last.
When loading a package with more than one overlay, the order in which
the overlays are added become significant if several packages overlay
the same resource.
Had downloaded overlays been supported, the install time could have been
used to determine the load order. Regardless, for pre-installed
overlays, the install time is randomly determined by the order in which
the Package Manager locates the packages during initial boot. To support
a well-defined order, pre-installed overlay packages are expected to
define an additional 'priority' attribute in their <overlay> tags:
<overlay targetPackage="com.target.package" priority="1234"/>
Pre-installed overlays are loaded in order of their priority attributes,
sorted in ascending order.
Assigning the same priority to several overlays targeting the same base
package leads to undefined behaviour. It is the responsibility of the
vendor to avoid this.
The following example shows the ResTable and PackageGroups after loading
an application and two overlays. The resource lookup framework will
query the packages in the order C, B, A.
+------+------+- -+------+------+
| 0x01 | | ... | | 0x7f |
+------+------+- -+------+------+
| |
"android" Target package A
|
Pre-installed overlay B (priority 1)
|
Pre-installed overlay C (priority 2)
Change-Id: If49c963149369b1957f7d2303b3dd27f669ed24e
2014-01-31 13:42:59 +00:00
|
|
|
#define IDMAP_PREFIX "/data/resource-cache/"
|
|
|
|
#define IDMAP_SUFFIX "@idmap"
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
#define PKG_NAME_MAX 128 /* largest allowed package name */
|
|
|
|
#define PKG_PATH_MAX 256 /* max size of any path we use */
|
|
|
|
|
2015-03-26 22:47:38 +00:00
|
|
|
/* dexopt needed flags matching those in dalvik.system.DexFile */
|
|
|
|
#define DEXOPT_DEX2OAT_NEEDED 1
|
|
|
|
#define DEXOPT_PATCHOAT_NEEDED 2
|
|
|
|
#define DEXOPT_SELF_PATCHOAT_NEEDED 3
|
|
|
|
|
2015-04-09 03:56:42 +00:00
|
|
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
/* data structures */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char* path;
|
|
|
|
size_t len;
|
|
|
|
} dir_rec_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
size_t count;
|
|
|
|
dir_rec_t* dirs;
|
|
|
|
} dir_rec_array_t;
|
|
|
|
|
|
|
|
extern dir_rec_t android_app_dir;
|
|
|
|
extern dir_rec_t android_app_private_dir;
|
|
|
|
extern dir_rec_t android_app_lib_dir;
|
|
|
|
extern dir_rec_t android_data_dir;
|
|
|
|
extern dir_rec_t android_asec_dir;
|
|
|
|
extern dir_rec_t android_media_dir;
|
2015-04-06 23:19:39 +00:00
|
|
|
extern dir_rec_t android_mnt_expand_dir;
|
2012-10-24 17:45:23 +00:00
|
|
|
extern dir_rec_array_t android_system_dirs;
|
|
|
|
|
|
|
|
typedef struct cache_dir_struct {
|
|
|
|
struct cache_dir_struct* parent;
|
|
|
|
int32_t childCount;
|
|
|
|
int32_t hiddenCount;
|
|
|
|
int32_t deleted;
|
|
|
|
char name[];
|
|
|
|
} cache_dir_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
cache_dir_t* dir;
|
|
|
|
time_t modTime;
|
|
|
|
char name[];
|
|
|
|
} cache_file_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
size_t numDirs;
|
|
|
|
size_t availDirs;
|
|
|
|
cache_dir_t** dirs;
|
|
|
|
size_t numFiles;
|
|
|
|
size_t availFiles;
|
|
|
|
cache_file_t** files;
|
|
|
|
size_t numCollected;
|
|
|
|
void* memBlocks;
|
|
|
|
int8_t* curMemBlockAvail;
|
|
|
|
int8_t* curMemBlockEnd;
|
|
|
|
} cache_t;
|
|
|
|
|
|
|
|
/* util.c */
|
|
|
|
|
2015-04-09 18:34:03 +00:00
|
|
|
// TODO: rename to create_data_user_package_path
|
2015-04-08 01:14:05 +00:00
|
|
|
std::string create_package_data_path(const char* volume_uuid,
|
|
|
|
const char* package_name, userid_t user);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
int create_pkg_path(char path[PKG_PATH_MAX],
|
|
|
|
const char *pkgname,
|
|
|
|
const char *postfix,
|
2013-07-10 23:55:46 +00:00
|
|
|
userid_t userid);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
2015-04-09 18:34:03 +00:00
|
|
|
std::string create_data_path(const char* volume_uuid);
|
|
|
|
|
|
|
|
std::string create_data_user_path(const char* volume_uuid, userid_t userid);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
2015-04-09 18:34:03 +00:00
|
|
|
std::string create_data_media_path(const char* volume_uuid, userid_t userid);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
2015-04-09 03:56:42 +00:00
|
|
|
std::vector<userid_t> get_known_users(const char* volume_uuid);
|
|
|
|
|
2014-04-25 14:05:19 +00:00
|
|
|
int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
int create_move_path(char path[PKG_PATH_MAX],
|
|
|
|
const char* pkgname,
|
|
|
|
const char* leaf,
|
2013-07-10 23:55:46 +00:00
|
|
|
userid_t userid);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
int is_valid_package_name(const char* pkgname);
|
|
|
|
|
2014-04-11 12:17:00 +00:00
|
|
|
int create_cache_path(char path[PKG_PATH_MAX], const char *src,
|
|
|
|
const char *instruction_set);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
int delete_dir_contents(const char *pathname,
|
|
|
|
int also_delete_dir,
|
2014-06-10 12:16:47 +00:00
|
|
|
int (*exclusion_predicate)(const char *name, const int is_dir));
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
int delete_dir_contents_fd(int dfd, const char *name);
|
|
|
|
|
2014-10-07 15:55:02 +00:00
|
|
|
int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
|
|
|
|
|
2012-10-24 17:45:23 +00:00
|
|
|
int lookup_media_dir(char basepath[PATH_MAX], const char *dir);
|
|
|
|
|
2015-04-09 18:34:03 +00:00
|
|
|
int64_t data_disk_free(const std::string& data_path);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
cache_t* start_cache_collection();
|
|
|
|
|
|
|
|
void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir);
|
|
|
|
|
2015-04-09 18:34:03 +00:00
|
|
|
void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
void finish_cache_collection(cache_t* cache);
|
|
|
|
|
|
|
|
int validate_system_app_path(const char* path);
|
|
|
|
|
|
|
|
int get_path_from_env(dir_rec_t* rec, const char* var);
|
|
|
|
|
|
|
|
int get_path_from_string(dir_rec_t* rec, const char* path);
|
|
|
|
|
|
|
|
int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
|
|
|
|
|
|
|
|
int validate_apk_path(const char *path);
|
|
|
|
|
|
|
|
int append_and_increment(char** dst, const char* src, size_t* dst_size);
|
|
|
|
|
2015-04-07 19:44:51 +00:00
|
|
|
char *build_string2(const char *s1, const char *s2);
|
|
|
|
char *build_string3(const char *s1, const char *s2, const char *s3);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
|
2015-04-09 18:34:03 +00:00
|
|
|
int ensure_media_user_dirs(const char* uuid, userid_t userid);
|
2014-04-25 14:05:19 +00:00
|
|
|
int ensure_config_user_dirs(userid_t userid);
|
2014-01-30 22:19:23 +00:00
|
|
|
int create_profile_file(const char *pkgname, gid_t gid);
|
|
|
|
void remove_profile_file(const char *pkgname);
|
2012-10-24 17:45:23 +00:00
|
|
|
|
|
|
|
/* commands.c */
|
|
|
|
|
2015-04-08 01:14:05 +00:00
|
|
|
int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const char *seinfo);
|
|
|
|
int uninstall(const char *uuid, const char *pkgname, userid_t userid);
|
2012-10-24 17:45:23 +00:00
|
|
|
int renamepkg(const char *oldpkgname, const char *newpkgname);
|
2015-04-08 01:14:05 +00:00
|
|
|
int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid);
|
|
|
|
int delete_user_data(const char *uuid, const char *pkgname, userid_t userid);
|
2015-04-09 03:56:42 +00:00
|
|
|
int make_user_data(const char *uuid, const char *pkgname, uid_t uid,
|
|
|
|
userid_t userid, const char* seinfo);
|
|
|
|
int move_user_data(const char* from_uuid, const char *to_uuid,
|
|
|
|
const char *package_name, appid_t appid, const char* seinfo);
|
2014-06-10 17:46:26 +00:00
|
|
|
int make_user_config(userid_t userid);
|
2015-04-09 18:34:03 +00:00
|
|
|
int delete_user(const char *uuid, userid_t userid);
|
2015-04-08 01:14:05 +00:00
|
|
|
int delete_cache(const char *uuid, const char *pkgname, userid_t userid);
|
|
|
|
int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid);
|
2014-04-11 12:17:00 +00:00
|
|
|
int move_dex(const char *src, const char *dst, const char *instruction_set);
|
|
|
|
int rm_dex(const char *path, const char *instruction_set);
|
2012-10-24 17:45:23 +00:00
|
|
|
int protect(char *pkgname, gid_t gid);
|
2015-04-08 01:14:05 +00:00
|
|
|
int get_size(const char *uuid, const char *pkgname, userid_t userid, const char *apkpath, const char *libdirpath,
|
2014-04-11 12:17:00 +00:00
|
|
|
const char *fwdlock_apkpath, const char *asecpath, const char *instruction_set,
|
|
|
|
int64_t *codesize, int64_t *datasize, int64_t *cachesize, int64_t *asecsize);
|
2015-04-09 18:34:03 +00:00
|
|
|
int free_cache(const char *uuid, int64_t free_size);
|
2014-08-21 18:05:20 +00:00
|
|
|
int dexopt(const char *apk_path, uid_t uid, bool is_public, const char *pkgName,
|
2015-03-26 22:47:38 +00:00
|
|
|
const char *instruction_set, int dexopt_needed, bool vm_safe_mode,
|
|
|
|
bool debuggable, const char* oat_dir);
|
2014-11-10 15:03:46 +00:00
|
|
|
int mark_boot_complete(const char *instruction_set);
|
2012-10-24 17:45:23 +00:00
|
|
|
int movefiles();
|
2015-04-09 20:10:03 +00:00
|
|
|
int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId);
|
Runtime resource overlay, iteration 2
Support any number of overlay packages. Support any target package.
UPDATED PACKAGE MATCHING
------------------------
In Runtime resource overlay, iteration 1, only a single overlay package
was considered. Package matching was based on file paths:
/vendor/overlay/system/framework-res.apk corresponded to
/system/framework-res.apk. Introduce a more flexible matching scheme
where any package is an overlay package if its manifest includes
<overlay targetPackage="com.target.package"/>
For security reasons, an overlay package must fulfill certain criteria
to take effect: see below.
THE IDMAP TOOL AND IDMAP FILES
------------------------------
Idmap files are created by the 'idmap' binary; idmap files must be
present when loading packages. For the Android system, Zygote calls
'idmap' as part of the resource pre-loading. For application packages,
'idmap' is invoked via 'installd' during package installation (similar
to 'dexopt').
UPDATED FLOW
------------
The following is an outline of the start-up sequences for the Android
system and Android apps. Steps marked with '+' are introduced by this
commit.
Zygote initialization
Initial AssetManager object created
+ idmap --scan creates idmaps for overlays targeting 'android', \
stores list of overlays in /data/resource-cache/overlays.list
AssetManager caches framework-res.apk
+ AssetManager caches overlay packages listed in overlays.list
Android boot
New AssetManager's ResTable acquired
AssetManager re-uses cached framework-res.apk
+ AssetManager re-uses cached 'android' overlays (if any)
App boot
ActivityThread prepares AssetManager to load app.apk
+ ActivityThread prepares AssetManager to load app overlays (if any)
New AssetManager's ResTable acquired as per Android boot
SECURITY
--------
Overlay packages are required to be pre-loaded (in /vendor/overlay).
These packages are trusted by definition. A future iteration of runtime
resource overlay may add support for downloaded overlays, which would
likely require target and overlay signatures match for the overlay to
be trusted.
LOOKUP PRIORITY
---------------
During resource lookup, packages are sequentially queried to provide a
best match, given the constraints of the current configuration. If any
package provide a better match than what has been found so far, it
replaces the previous match. The target package is always queried last.
When loading a package with more than one overlay, the order in which
the overlays are added become significant if several packages overlay
the same resource.
Had downloaded overlays been supported, the install time could have been
used to determine the load order. Regardless, for pre-installed
overlays, the install time is randomly determined by the order in which
the Package Manager locates the packages during initial boot. To support
a well-defined order, pre-installed overlay packages are expected to
define an additional 'priority' attribute in their <overlay> tags:
<overlay targetPackage="com.target.package" priority="1234"/>
Pre-installed overlays are loaded in order of their priority attributes,
sorted in ascending order.
Assigning the same priority to several overlays targeting the same base
package leads to undefined behaviour. It is the responsibility of the
vendor to avoid this.
The following example shows the ResTable and PackageGroups after loading
an application and two overlays. The resource lookup framework will
query the packages in the order C, B, A.
+------+------+- -+------+------+
| 0x01 | | ... | | 0x7f |
+------+------+- -+------+------+
| |
"android" Target package A
|
Pre-installed overlay B (priority 1)
|
Pre-installed overlay C (priority 2)
Change-Id: If49c963149369b1957f7d2303b3dd27f669ed24e
2014-01-31 13:42:59 +00:00
|
|
|
int idmap(const char *target_path, const char *overlay_path, uid_t uid);
|
2015-04-08 01:14:05 +00:00
|
|
|
int restorecon_data(const char *uuid, const char* pkgName, const char* seinfo, uid_t uid);
|
2015-03-03 20:25:29 +00:00
|
|
|
int create_oat_dir(const char* oat_dir, const char *instruction_set);
|
|
|
|
int rm_package_dir(const char* apk_path);
|
|
|
|
int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
|
2015-04-06 23:19:39 +00:00
|
|
|
const char *instruction_set);
|
2015-04-09 03:56:42 +00:00
|
|
|
int move_package_dir(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
|
|
|
|
const char *instruction_set);
|