blob: 7a5da9845c4ed3997b769e699417e4280cdcc3eb [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
43#if INCLUDE_SYS_MOUNT_FOR_STATFS
44#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
65
66#define APP_LIB_SUBDIR "app-lib/" // sub-directory under ANDROID_DATA
67
68#define MEDIA_SUBDIR "media/" // sub-directory under ANDROID_DATA
69
70/* other handy constants */
71
72#define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA
73
74#define DALVIK_CACHE_PREFIX "/data/dalvik-cache/"
75#define DALVIK_CACHE_POSTFIX "/classes.dex"
76
77#define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/"
78
Mårten Kongstad63568b12014-01-31 14:42:59 +010079#define IDMAP_PREFIX "/data/resource-cache/"
80#define IDMAP_SUFFIX "@idmap"
81
Mike Lockwood94afecf2012-10-24 10:45:23 -070082#define PKG_NAME_MAX 128 /* largest allowed package name */
83#define PKG_PATH_MAX 256 /* max size of any path we use */
84
Mike Lockwood94afecf2012-10-24 10:45:23 -070085/* data structures */
86
87typedef struct {
88 char* path;
89 size_t len;
90} dir_rec_t;
91
92typedef struct {
93 size_t count;
94 dir_rec_t* dirs;
95} dir_rec_array_t;
96
97extern dir_rec_t android_app_dir;
98extern dir_rec_t android_app_private_dir;
99extern dir_rec_t android_app_lib_dir;
100extern dir_rec_t android_data_dir;
101extern dir_rec_t android_asec_dir;
102extern dir_rec_t android_media_dir;
103extern dir_rec_array_t android_system_dirs;
104
105typedef struct cache_dir_struct {
106 struct cache_dir_struct* parent;
107 int32_t childCount;
108 int32_t hiddenCount;
109 int32_t deleted;
110 char name[];
111} cache_dir_t;
112
113typedef struct {
114 cache_dir_t* dir;
115 time_t modTime;
116 char name[];
117} cache_file_t;
118
119typedef struct {
120 size_t numDirs;
121 size_t availDirs;
122 cache_dir_t** dirs;
123 size_t numFiles;
124 size_t availFiles;
125 cache_file_t** files;
126 size_t numCollected;
127 void* memBlocks;
128 int8_t* curMemBlockAvail;
129 int8_t* curMemBlockEnd;
130} cache_t;
131
132/* util.c */
133
134int create_pkg_path_in_dir(char path[PKG_PATH_MAX],
135 const dir_rec_t* dir,
136 const char* pkgname,
137 const char* postfix);
138
139int create_pkg_path(char path[PKG_PATH_MAX],
140 const char *pkgname,
141 const char *postfix,
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700142 userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700143
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700144int create_user_path(char path[PKG_PATH_MAX],
145 userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700147int create_user_media_path(char path[PKG_PATH_MAX], userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148
Robin Lee095c7632014-04-25 15:05:19 +0100149int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
150
Mike Lockwood94afecf2012-10-24 10:45:23 -0700151int create_move_path(char path[PKG_PATH_MAX],
152 const char* pkgname,
153 const char* leaf,
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700154 userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700155
156int is_valid_package_name(const char* pkgname);
157
Narayan Kamath1b400322014-04-11 13:17:00 +0100158int create_cache_path(char path[PKG_PATH_MAX], const char *src,
159 const char *instruction_set);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700160
161int delete_dir_contents(const char *pathname,
162 int also_delete_dir,
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100163 int (*exclusion_predicate)(const char *name, const int is_dir));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700164
165int delete_dir_contents_fd(int dfd, const char *name);
166
167int lookup_media_dir(char basepath[PATH_MAX], const char *dir);
168
169int64_t data_disk_free();
170
171cache_t* start_cache_collection();
172
173void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir);
174
175void clear_cache_files(cache_t* cache, int64_t free_size);
176
177void finish_cache_collection(cache_t* cache);
178
179int validate_system_app_path(const char* path);
180
181int get_path_from_env(dir_rec_t* rec, const char* var);
182
183int get_path_from_string(dir_rec_t* rec, const char* path);
184
185int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
186
187int validate_apk_path(const char *path);
188
189int append_and_increment(char** dst, const char* src, size_t* dst_size);
190
191char *build_string2(char *s1, char *s2);
192char *build_string3(char *s1, char *s2, char *s3);
193
194int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
195int ensure_media_user_dirs(userid_t userid);
Robin Lee095c7632014-04-25 15:05:19 +0100196int ensure_config_user_dirs(userid_t userid);
Dave Allisond9370732014-01-30 14:19:23 -0800197int create_profile_file(const char *pkgname, gid_t gid);
198void remove_profile_file(const char *pkgname);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700199
200/* commands.c */
201
Robert Craig4d3fd4e2013-03-25 06:33:03 -0400202int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo);
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700203int uninstall(const char *pkgname, userid_t userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700204int renamepkg(const char *oldpkgname, const char *newpkgname);
205int fix_uid(const char *pkgname, uid_t uid, gid_t gid);
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700206int delete_user_data(const char *pkgname, userid_t userid);
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700207int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo);
Robin Lee7c8bec02014-06-10 18:46:26 +0100208int make_user_config(userid_t userid);
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700209int delete_user(userid_t userid);
210int delete_cache(const char *pkgname, userid_t userid);
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700211int delete_code_cache(const char *pkgname, userid_t userid);
Narayan Kamath1b400322014-04-11 13:17:00 +0100212int move_dex(const char *src, const char *dst, const char *instruction_set);
213int rm_dex(const char *path, const char *instruction_set);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700214int protect(char *pkgname, gid_t gid);
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700215int get_size(const char *pkgname, userid_t userid, const char *apkpath, const char *libdirpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100216 const char *fwdlock_apkpath, const char *asecpath, const char *instruction_set,
217 int64_t *codesize, int64_t *datasize, int64_t *cachesize, int64_t *asecsize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700218int free_cache(int64_t free_size);
Narayan Kamath1b400322014-04-11 13:17:00 +0100219int dexopt(const char *apk_path, uid_t uid, int is_public, const char *pkgName,
220 const char *instruction_set);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700221int movefiles();
222int linklib(const char* target, const char* source, int userId);
Mårten Kongstad63568b12014-01-31 14:42:59 +0100223int idmap(const char *target_path, const char *overlay_path, uid_t uid);
Robert Craige9887e42014-02-20 10:25:56 -0500224int restorecon_data();
Narayan Kamath1e57e4a2014-06-17 12:54:16 +0100225int prune_dex_cache(const char* subdir);