blob: 395d0ea7270dcb1b4890247e1c65551ec413f51d [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "installd"
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <stdint.h>
23#include <inttypes.h>
24#include <sys/stat.h>
25#include <dirent.h>
26#include <unistd.h>
27#include <ctype.h>
28#include <fcntl.h>
29#include <errno.h>
30#include <utime.h>
31#include <sys/socket.h>
32#include <sys/types.h>
33#include <sys/wait.h>
Jeff Sharkeyc03de092015-04-07 18:14:05 -070034#include <string>
Mike Lockwood94afecf2012-10-24 10:45:23 -070035
36#include <cutils/fs.h>
37#include <cutils/sockets.h>
38#include <cutils/log.h>
39#include <cutils/properties.h>
40#include <cutils/multiuser.h>
41
42#include <private/android_filesystem_config.h>
43
Elliott Hughes9a4e7f42014-11-20 12:54:21 -080044#if defined(__APPLE__)
Mike Lockwood94afecf2012-10-24 10:45:23 -070045#include <sys/mount.h>
46#else
47#include <sys/statfs.h>
48#endif
49
50#define SOCKET_PATH "installd"
51
52
53/* elements combined with a valid package name to form paths */
54
55#define PRIMARY_USER_PREFIX "data/"
56#define SECONDARY_USER_PREFIX "user/"
57
58#define PKG_DIR_POSTFIX ""
59
60#define PKG_LIB_POSTFIX "/lib"
61
62#define CACHE_DIR_POSTFIX "/cache"
Jeff Sharkeyc796b682014-07-15 21:49:51 -070063#define CODE_CACHE_DIR_POSTFIX "/code_cache"
Mike Lockwood94afecf2012-10-24 10:45:23 -070064
65#define APP_SUBDIR "app/" // sub-directory under ANDROID_DATA
Jeff Sharkey770180a2014-09-08 17:14:26 -070066#define PRIV_APP_SUBDIR "priv-app/" // sub-directory under ANDROID_DATA
Mike Lockwood94afecf2012-10-24 10:45:23 -070067
68#define APP_LIB_SUBDIR "app-lib/" // sub-directory under ANDROID_DATA
69
70#define MEDIA_SUBDIR "media/" // sub-directory under ANDROID_DATA
71
72/* other handy constants */
73
74#define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA
75
76#define DALVIK_CACHE_PREFIX "/data/dalvik-cache/"
77#define DALVIK_CACHE_POSTFIX "/classes.dex"
78
79#define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/"
80
Mårten Kongstad63568b12014-01-31 14:42:59 +010081#define IDMAP_PREFIX "/data/resource-cache/"
82#define IDMAP_SUFFIX "@idmap"
83
Mike Lockwood94afecf2012-10-24 10:45:23 -070084#define PKG_NAME_MAX 128 /* largest allowed package name */
85#define PKG_PATH_MAX 256 /* max size of any path we use */
86
Richard Uhlerc92fb622015-03-26 15:47:38 -070087/* dexopt needed flags matching those in dalvik.system.DexFile */
88#define DEXOPT_DEX2OAT_NEEDED 1
89#define DEXOPT_PATCHOAT_NEEDED 2
90#define DEXOPT_SELF_PATCHOAT_NEEDED 3
91
Mike Lockwood94afecf2012-10-24 10:45:23 -070092/* data structures */
93
94typedef struct {
95 char* path;
96 size_t len;
97} dir_rec_t;
98
99typedef struct {
100 size_t count;
101 dir_rec_t* dirs;
102} dir_rec_array_t;
103
104extern dir_rec_t android_app_dir;
105extern dir_rec_t android_app_private_dir;
106extern dir_rec_t android_app_lib_dir;
107extern dir_rec_t android_data_dir;
108extern dir_rec_t android_asec_dir;
109extern dir_rec_t android_media_dir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700110extern dir_rec_t android_mnt_expand_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700111extern dir_rec_array_t android_system_dirs;
112
113typedef struct cache_dir_struct {
114 struct cache_dir_struct* parent;
115 int32_t childCount;
116 int32_t hiddenCount;
117 int32_t deleted;
118 char name[];
119} cache_dir_t;
120
121typedef struct {
122 cache_dir_t* dir;
123 time_t modTime;
124 char name[];
125} cache_file_t;
126
127typedef struct {
128 size_t numDirs;
129 size_t availDirs;
130 cache_dir_t** dirs;
131 size_t numFiles;
132 size_t availFiles;
133 cache_file_t** files;
134 size_t numCollected;
135 void* memBlocks;
136 int8_t* curMemBlockAvail;
137 int8_t* curMemBlockEnd;
138} cache_t;
139
140/* util.c */
141
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700142// TODO: rename to create_data_user_package_path
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700143std::string create_package_data_path(const char* volume_uuid,
144 const char* package_name, userid_t user);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700145
146int create_pkg_path(char path[PKG_PATH_MAX],
147 const char *pkgname,
148 const char *postfix,
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700149 userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700151std::string create_data_path(const char* volume_uuid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700152
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700153std::string create_data_user_path(const char* volume_uuid, userid_t userid);
154
155std::string create_data_media_path(const char* volume_uuid, userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700156
Robin Lee095c7632014-04-25 15:05:19 +0100157int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
158
Mike Lockwood94afecf2012-10-24 10:45:23 -0700159int create_move_path(char path[PKG_PATH_MAX],
160 const char* pkgname,
161 const char* leaf,
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700162 userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700163
164int is_valid_package_name(const char* pkgname);
165
Narayan Kamath1b400322014-04-11 13:17:00 +0100166int create_cache_path(char path[PKG_PATH_MAX], const char *src,
167 const char *instruction_set);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700168
169int delete_dir_contents(const char *pathname,
170 int also_delete_dir,
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100171 int (*exclusion_predicate)(const char *name, const int is_dir));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700172
173int delete_dir_contents_fd(int dfd, const char *name);
174
Robin Lee60fd3fe2014-10-07 16:55:02 +0100175int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
176
Mike Lockwood94afecf2012-10-24 10:45:23 -0700177int lookup_media_dir(char basepath[PATH_MAX], const char *dir);
178
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700179int64_t data_disk_free(const std::string& data_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700180
181cache_t* start_cache_collection();
182
183void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir);
184
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700185void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700186
187void finish_cache_collection(cache_t* cache);
188
189int validate_system_app_path(const char* path);
190
191int get_path_from_env(dir_rec_t* rec, const char* var);
192
193int get_path_from_string(dir_rec_t* rec, const char* path);
194
195int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
196
197int validate_apk_path(const char *path);
198
199int append_and_increment(char** dst, const char* src, size_t* dst_size);
200
Jeff Sharkey19803802015-04-07 12:44:51 -0700201char *build_string2(const char *s1, const char *s2);
202char *build_string3(const char *s1, const char *s2, const char *s3);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203
204int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700205int ensure_media_user_dirs(const char* uuid, userid_t userid);
Robin Lee095c7632014-04-25 15:05:19 +0100206int ensure_config_user_dirs(userid_t userid);
Dave Allisond9370732014-01-30 14:19:23 -0800207int create_profile_file(const char *pkgname, gid_t gid);
208void remove_profile_file(const char *pkgname);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700209
210/* commands.c */
211
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700212int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const char *seinfo);
213int uninstall(const char *uuid, const char *pkgname, userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700214int renamepkg(const char *oldpkgname, const char *newpkgname);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700215int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid);
216int delete_user_data(const char *uuid, const char *pkgname, userid_t userid);
217int make_user_data(const char *uuid, const char *pkgname, uid_t uid, userid_t userid, const char* seinfo);
Robin Lee7c8bec02014-06-10 18:46:26 +0100218int make_user_config(userid_t userid);
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700219int delete_user(const char *uuid, userid_t userid);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700220int delete_cache(const char *uuid, const char *pkgname, userid_t userid);
221int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid);
Narayan Kamath1b400322014-04-11 13:17:00 +0100222int move_dex(const char *src, const char *dst, const char *instruction_set);
223int rm_dex(const char *path, const char *instruction_set);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700224int protect(char *pkgname, gid_t gid);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700225int get_size(const char *uuid, const char *pkgname, userid_t userid, const char *apkpath, const char *libdirpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100226 const char *fwdlock_apkpath, const char *asecpath, const char *instruction_set,
227 int64_t *codesize, int64_t *datasize, int64_t *cachesize, int64_t *asecsize);
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700228int free_cache(const char *uuid, int64_t free_size);
Calin Juravleb1efac12014-08-21 19:05:20 +0100229int dexopt(const char *apk_path, uid_t uid, bool is_public, const char *pkgName,
Richard Uhlerc92fb622015-03-26 15:47:38 -0700230 const char *instruction_set, int dexopt_needed, bool vm_safe_mode,
231 bool debuggable, const char* oat_dir);
Narayan Kamath091ea772014-11-10 15:03:46 +0000232int mark_boot_complete(const char *instruction_set);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233int movefiles();
Jeff Sharkey6fe28a02015-04-09 13:10:03 -0700234int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId);
Mårten Kongstad63568b12014-01-31 14:42:59 +0100235int idmap(const char *target_path, const char *overlay_path, uid_t uid);
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700236int restorecon_data(const char *uuid, const char* pkgName, const char* seinfo, uid_t uid);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800237int create_oat_dir(const char* oat_dir, const char *instruction_set);
238int rm_package_dir(const char* apk_path);
239int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700240 const char *instruction_set);