blob: 2540dbe18c2f87b0ba691f5d08911e2bd7ee6daf [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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>
Kenny Root3e319a92010-09-07 13:58:28 -070022#include <stdint.h>
23#include <inttypes.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#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
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -070035#include <cutils/fs.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <cutils/sockets.h>
37#include <cutils/log.h>
38#include <cutils/properties.h>
Jeff Sharkey5b1ada22012-08-14 18:47:09 -070039#include <cutils/multiuser.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
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
Kenny Root86c95842011-03-31 13:16:12 -070054#define PRIMARY_USER_PREFIX "data/"
55#define SECONDARY_USER_PREFIX "user/"
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057#define PKG_DIR_POSTFIX ""
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059#define PKG_LIB_POSTFIX "/lib"
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061#define CACHE_DIR_POSTFIX "/cache"
62
Kenny Root86c95842011-03-31 13:16:12 -070063#define APP_SUBDIR "app/" // sub-directory under ANDROID_DATA
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Kenny Root9bbd70a2012-09-10 11:13:36 -070065#define APP_LIB_SUBDIR "app-lib/" // sub-directory under ANDROID_DATA
66
Dianne Hackborn197a0c82012-07-12 14:46:04 -070067#define MEDIA_SUBDIR "media/" // sub-directory under ANDROID_DATA
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069/* other handy constants */
70
Kenny Root86c95842011-03-31 13:16:12 -070071#define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Kenny Root86c95842011-03-31 13:16:12 -070073#define DALVIK_CACHE_PREFIX "/data/dalvik-cache/"
74#define DALVIK_CACHE_POSTFIX "/classes.dex"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080076#define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
78#define PKG_NAME_MAX 128 /* largest allowed package name */
79#define PKG_PATH_MAX 256 /* max size of any path we use */
80
Amith Yamasani742a6712011-05-04 14:49:28 -070081#define PER_USER_RANGE ((uid_t)100000) /* range of uids per user
82 uid = persona * PER_USER_RANGE + appid */
83
Kenny Root86c95842011-03-31 13:16:12 -070084/* data structures */
85
86typedef struct {
87 char* path;
88 size_t len;
89} dir_rec_t;
90
91typedef struct {
92 size_t count;
93 dir_rec_t* dirs;
94} dir_rec_array_t;
95
96extern dir_rec_t android_app_dir;
97extern dir_rec_t android_app_private_dir;
Kenny Root9bbd70a2012-09-10 11:13:36 -070098extern dir_rec_t android_app_lib_dir;
Kenny Root86c95842011-03-31 13:16:12 -070099extern dir_rec_t android_data_dir;
100extern dir_rec_t android_asec_dir;
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700101extern dir_rec_t android_media_dir;
Kenny Root86c95842011-03-31 13:16:12 -0700102extern dir_rec_array_t android_system_dirs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700104typedef struct cache_dir_struct {
105 struct cache_dir_struct* parent;
106 int32_t childCount;
107 int32_t hiddenCount;
108 int32_t deleted;
109 char name[];
110} cache_dir_t;
111
112typedef struct {
113 cache_dir_t* dir;
114 time_t modTime;
115 char name[];
116} cache_file_t;
117
118typedef struct {
119 size_t numDirs;
120 size_t availDirs;
121 cache_dir_t** dirs;
122 size_t numFiles;
123 size_t availFiles;
124 cache_file_t** files;
125 size_t numCollected;
126 void* memBlocks;
127 int8_t* curMemBlockAvail;
128 int8_t* curMemBlockEnd;
129} cache_t;
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131/* util.c */
132
Kenny Root86c95842011-03-31 13:16:12 -0700133int create_pkg_path_in_dir(char path[PKG_PATH_MAX],
134 const dir_rec_t* dir,
135 const char* pkgname,
136 const char* postfix);
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138int create_pkg_path(char path[PKG_PATH_MAX],
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 const char *pkgname,
Kenny Root86c95842011-03-31 13:16:12 -0700140 const char *postfix,
141 uid_t persona);
142
Amith Yamasani0b285492011-04-14 17:35:23 -0700143int create_persona_path(char path[PKG_PATH_MAX],
144 uid_t persona);
145
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700146int create_persona_media_path(char path[PKG_PATH_MAX], userid_t userid);
147
Kenny Rootad757e92011-11-29 15:54:55 -0800148int create_move_path(char path[PKG_PATH_MAX],
149 const char* pkgname,
150 const char* leaf,
151 uid_t persona);
152
Kenny Root86c95842011-03-31 13:16:12 -0700153int is_valid_package_name(const char* pkgname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
155int create_cache_path(char path[PKG_PATH_MAX], const char *src);
156
157int delete_dir_contents(const char *pathname,
158 int also_delete_dir,
159 const char *ignore);
160
161int delete_dir_contents_fd(int dfd, const char *name);
162
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700163int lookup_media_dir(char basepath[PATH_MAX], const char *dir);
164
165int64_t data_disk_free();
166
167cache_t* start_cache_collection();
168
169void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir);
170
171void clear_cache_files(cache_t* cache, int64_t free_size);
172
173void finish_cache_collection(cache_t* cache);
174
Kenny Root86c95842011-03-31 13:16:12 -0700175int validate_system_app_path(const char* path);
176
177int get_path_from_env(dir_rec_t* rec, const char* var);
178
179int get_path_from_string(dir_rec_t* rec, const char* path);
180
181int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
182
183int validate_apk_path(const char *path);
184
185int append_and_increment(char** dst, const char* src, size_t* dst_size);
186
Amith Yamasani0b285492011-04-14 17:35:23 -0700187char *build_string2(char *s1, char *s2);
188char *build_string3(char *s1, char *s2, char *s3);
189
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700190int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192/* commands.c */
193
Kenny Root35ab3ad2011-02-02 16:42:14 -0800194int install(const char *pkgname, uid_t uid, gid_t gid);
Amith Yamasani0b285492011-04-14 17:35:23 -0700195int uninstall(const char *pkgname, uid_t persona);
Kenny Root35ab3ad2011-02-02 16:42:14 -0800196int renamepkg(const char *oldpkgname, const char *newpkgname);
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700197int fix_uid(const char *pkgname, uid_t uid, gid_t gid);
Amith Yamasani0b285492011-04-14 17:35:23 -0700198int delete_user_data(const char *pkgname, uid_t persona);
199int make_user_data(const char *pkgname, uid_t uid, uid_t persona);
200int delete_persona(uid_t persona);
Amith Yamasani742a6712011-05-04 14:49:28 -0700201int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy);
Amith Yamasani54289b82012-10-01 10:39:14 -0700202int delete_cache(const char *pkgname, uid_t persona);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203int move_dex(const char *src, const char *dst);
204int rm_dex(const char *path);
205int protect(char *pkgname, gid_t gid);
Dianne Hackborn0c380492012-08-20 17:23:30 -0700206int get_size(const char *pkgname, int persona, const char *apkpath, const char *fwdlock_apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700207 const char *asecpath, int64_t *codesize, int64_t *datasize, int64_t *cachesize,
208 int64_t *asecsize);
Kenny Root3e319a92010-09-07 13:58:28 -0700209int free_cache(int64_t free_size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210int dexopt(const char *apk_path, uid_t uid, int is_public);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800211int movefiles();
Kenny Root6a6b0072010-10-07 16:46:10 -0700212int linklib(const char* target, const char* source);