blob: cf5e21f503c3803465a9ee65cb6128ea6d3f69a7 [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>
34
35#include <cutils/fs.h>
36#include <cutils/sockets.h>
37#include <cutils/log.h>
38#include <cutils/properties.h>
39#include <cutils/multiuser.h>
40
41#include <private/android_filesystem_config.h>
42
Elliott Hughes9a4e7f42014-11-20 12:54:21 -080043#if defined(__APPLE__)
Mike Lockwood94afecf2012-10-24 10:45:23 -070044#include <sys/mount.h>
45#else
46#include <sys/statfs.h>
47#endif
48
49#define SOCKET_PATH "installd"
50
51
52/* elements combined with a valid package name to form paths */
53
54#define PRIMARY_USER_PREFIX "data/"
55#define SECONDARY_USER_PREFIX "user/"
56
57#define PKG_DIR_POSTFIX ""
58
59#define PKG_LIB_POSTFIX "/lib"
60
61#define CACHE_DIR_POSTFIX "/cache"
Jeff Sharkeyc796b682014-07-15 21:49:51 -070062#define CODE_CACHE_DIR_POSTFIX "/code_cache"
Mike Lockwood94afecf2012-10-24 10:45:23 -070063
64#define APP_SUBDIR "app/" // sub-directory under ANDROID_DATA
Jeff Sharkey770180a2014-09-08 17:14:26 -070065#define PRIV_APP_SUBDIR "priv-app/" // sub-directory under ANDROID_DATA
Mike Lockwood94afecf2012-10-24 10:45:23 -070066
67#define APP_LIB_SUBDIR "app-lib/" // sub-directory under ANDROID_DATA
68
69#define MEDIA_SUBDIR "media/" // sub-directory under ANDROID_DATA
70
71/* other handy constants */
72
73#define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA
74
75#define DALVIK_CACHE_PREFIX "/data/dalvik-cache/"
76#define DALVIK_CACHE_POSTFIX "/classes.dex"
77
78#define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/"
79
Mårten Kongstad63568b12014-01-31 14:42:59 +010080#define IDMAP_PREFIX "/data/resource-cache/"
81#define IDMAP_SUFFIX "@idmap"
82
Mike Lockwood94afecf2012-10-24 10:45:23 -070083#define PKG_NAME_MAX 128 /* largest allowed package name */
84#define PKG_PATH_MAX 256 /* max size of any path we use */
85
Richard Uhlerc92fb622015-03-26 15:47:38 -070086/* dexopt needed flags matching those in dalvik.system.DexFile */
87#define DEXOPT_DEX2OAT_NEEDED 1
88#define DEXOPT_PATCHOAT_NEEDED 2
89#define DEXOPT_SELF_PATCHOAT_NEEDED 3
90
Mike Lockwood94afecf2012-10-24 10:45:23 -070091/* data structures */
92
93typedef struct {
94 char* path;
95 size_t len;
96} dir_rec_t;
97
98typedef struct {
99 size_t count;
100 dir_rec_t* dirs;
101} dir_rec_array_t;
102
103extern dir_rec_t android_app_dir;
104extern dir_rec_t android_app_private_dir;
105extern dir_rec_t android_app_lib_dir;
106extern dir_rec_t android_data_dir;
107extern dir_rec_t android_asec_dir;
108extern dir_rec_t android_media_dir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700109extern dir_rec_t android_mnt_expand_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700110extern dir_rec_array_t android_system_dirs;
111
112typedef struct cache_dir_struct {
113 struct cache_dir_struct* parent;
114 int32_t childCount;
115 int32_t hiddenCount;
116 int32_t deleted;
117 char name[];
118} cache_dir_t;
119
120typedef struct {
121 cache_dir_t* dir;
122 time_t modTime;
123 char name[];
124} cache_file_t;
125
126typedef struct {
127 size_t numDirs;
128 size_t availDirs;
129 cache_dir_t** dirs;
130 size_t numFiles;
131 size_t availFiles;
132 cache_file_t** files;
133 size_t numCollected;
134 void* memBlocks;
135 int8_t* curMemBlockAvail;
136 int8_t* curMemBlockEnd;
137} cache_t;
138
139/* util.c */
140
141int create_pkg_path_in_dir(char path[PKG_PATH_MAX],
142 const dir_rec_t* dir,
143 const char* pkgname,
144 const char* postfix);
145
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 Sharkeyabe4fe52013-07-10 16:55:46 -0700151int create_user_path(char path[PKG_PATH_MAX],
152 userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700153
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700154int create_user_media_path(char path[PKG_PATH_MAX], userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700155
Robin Lee095c7632014-04-25 15:05:19 +0100156int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
157
Mike Lockwood94afecf2012-10-24 10:45:23 -0700158int create_move_path(char path[PKG_PATH_MAX],
159 const char* pkgname,
160 const char* leaf,
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700161 userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700162
163int is_valid_package_name(const char* pkgname);
164
Narayan Kamath1b400322014-04-11 13:17:00 +0100165int create_cache_path(char path[PKG_PATH_MAX], const char *src,
166 const char *instruction_set);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700167
168int delete_dir_contents(const char *pathname,
169 int also_delete_dir,
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100170 int (*exclusion_predicate)(const char *name, const int is_dir));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700171
172int delete_dir_contents_fd(int dfd, const char *name);
173
Robin Lee60fd3fe2014-10-07 16:55:02 +0100174int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
175
Mike Lockwood94afecf2012-10-24 10:45:23 -0700176int lookup_media_dir(char basepath[PATH_MAX], const char *dir);
177
178int64_t data_disk_free();
179
180cache_t* start_cache_collection();
181
182void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir);
183
184void clear_cache_files(cache_t* cache, int64_t free_size);
185
186void finish_cache_collection(cache_t* cache);
187
188int validate_system_app_path(const char* path);
189
190int get_path_from_env(dir_rec_t* rec, const char* var);
191
192int get_path_from_string(dir_rec_t* rec, const char* path);
193
194int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
195
196int validate_apk_path(const char *path);
197
198int append_and_increment(char** dst, const char* src, size_t* dst_size);
199
Jeff Sharkey19803802015-04-07 12:44:51 -0700200char *build_string2(const char *s1, const char *s2);
201char *build_string3(const char *s1, const char *s2, const char *s3);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700202
203int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
204int ensure_media_user_dirs(userid_t userid);
Robin Lee095c7632014-04-25 15:05:19 +0100205int ensure_config_user_dirs(userid_t userid);
Dave Allisond9370732014-01-30 14:19:23 -0800206int create_profile_file(const char *pkgname, gid_t gid);
207void remove_profile_file(const char *pkgname);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700208
209/* commands.c */
210
Robert Craig4d3fd4e2013-03-25 06:33:03 -0400211int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo);
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700212int uninstall(const char *pkgname, userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700213int renamepkg(const char *oldpkgname, const char *newpkgname);
214int fix_uid(const char *pkgname, uid_t uid, gid_t gid);
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700215int delete_user_data(const char *pkgname, userid_t userid);
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700216int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo);
Robin Lee7c8bec02014-06-10 18:46:26 +0100217int make_user_config(userid_t userid);
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700218int delete_user(userid_t userid);
219int delete_cache(const char *pkgname, userid_t userid);
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700220int delete_code_cache(const char *pkgname, userid_t userid);
Narayan Kamath1b400322014-04-11 13:17:00 +0100221int move_dex(const char *src, const char *dst, const char *instruction_set);
222int rm_dex(const char *path, const char *instruction_set);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700223int protect(char *pkgname, gid_t gid);
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700224int get_size(const char *pkgname, userid_t userid, const char *apkpath, const char *libdirpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100225 const char *fwdlock_apkpath, const char *asecpath, const char *instruction_set,
226 int64_t *codesize, int64_t *datasize, int64_t *cachesize, int64_t *asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700227int free_cache(int64_t free_size);
Calin Juravleb1efac12014-08-21 19:05:20 +0100228int dexopt(const char *apk_path, uid_t uid, bool is_public, const char *pkgName,
Richard Uhlerc92fb622015-03-26 15:47:38 -0700229 const char *instruction_set, int dexopt_needed, bool vm_safe_mode,
230 bool debuggable, const char* oat_dir);
Narayan Kamath091ea772014-11-10 15:03:46 +0000231int mark_boot_complete(const char *instruction_set);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700232int movefiles();
233int linklib(const char* target, const char* source, int userId);
Mårten Kongstad63568b12014-01-31 14:42:59 +0100234int idmap(const char *target_path, const char *overlay_path, uid_t uid);
Jeff Sharkey19803802015-04-07 12:44:51 -0700235int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800236int create_oat_dir(const char* oat_dir, const char *instruction_set);
237int rm_package_dir(const char* apk_path);
238int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
Jeff Sharkeye23a1322015-04-06 16:19:39 -0700239 const char *instruction_set);