blob: f5485d24f939c298ed495a7f68ce9fed3eaa4129 [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
35#include <cutils/sockets.h>
36#include <cutils/log.h>
37#include <cutils/properties.h>
Jeff Sharkey5b1ada22012-08-14 18:47:09 -070038#include <cutils/multiuser.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40#include <private/android_filesystem_config.h>
41
42#if INCLUDE_SYS_MOUNT_FOR_STATFS
43#include <sys/mount.h>
44#else
45#include <sys/statfs.h>
46#endif
47
48#define SOCKET_PATH "installd"
49
50
51/* elements combined with a valid package name to form paths */
52
Kenny Root86c95842011-03-31 13:16:12 -070053#define PRIMARY_USER_PREFIX "data/"
54#define SECONDARY_USER_PREFIX "user/"
55
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056#define PKG_DIR_POSTFIX ""
57
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058#define PKG_LIB_POSTFIX "/lib"
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060#define CACHE_DIR_POSTFIX "/cache"
61
Kenny Root86c95842011-03-31 13:16:12 -070062#define APP_SUBDIR "app/" // sub-directory under ANDROID_DATA
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Dianne Hackborn197a0c82012-07-12 14:46:04 -070064#define MEDIA_SUBDIR "media/" // sub-directory under ANDROID_DATA
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066/* other handy constants */
67
Kenny Root86c95842011-03-31 13:16:12 -070068#define PRIVATE_APP_SUBDIR "app-private/" // sub-directory under ANDROID_DATA
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Kenny Root86c95842011-03-31 13:16:12 -070070#define DALVIK_CACHE_PREFIX "/data/dalvik-cache/"
71#define DALVIK_CACHE_POSTFIX "/classes.dex"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080073#define UPDATE_COMMANDS_DIR_PREFIX "/system/etc/updatecmds/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
75#define PKG_NAME_MAX 128 /* largest allowed package name */
76#define PKG_PATH_MAX 256 /* max size of any path we use */
77
Amith Yamasani742a6712011-05-04 14:49:28 -070078#define PER_USER_RANGE ((uid_t)100000) /* range of uids per user
79 uid = persona * PER_USER_RANGE + appid */
80
Kenny Root86c95842011-03-31 13:16:12 -070081/* data structures */
82
83typedef struct {
84 char* path;
85 size_t len;
86} dir_rec_t;
87
88typedef struct {
89 size_t count;
90 dir_rec_t* dirs;
91} dir_rec_array_t;
92
93extern dir_rec_t android_app_dir;
94extern dir_rec_t android_app_private_dir;
95extern dir_rec_t android_data_dir;
96extern dir_rec_t android_asec_dir;
Dianne Hackborn197a0c82012-07-12 14:46:04 -070097extern dir_rec_t android_media_dir;
Kenny Root86c95842011-03-31 13:16:12 -070098extern dir_rec_array_t android_system_dirs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700100typedef struct cache_dir_struct {
101 struct cache_dir_struct* parent;
102 int32_t childCount;
103 int32_t hiddenCount;
104 int32_t deleted;
105 char name[];
106} cache_dir_t;
107
108typedef struct {
109 cache_dir_t* dir;
110 time_t modTime;
111 char name[];
112} cache_file_t;
113
114typedef struct {
115 size_t numDirs;
116 size_t availDirs;
117 cache_dir_t** dirs;
118 size_t numFiles;
119 size_t availFiles;
120 cache_file_t** files;
121 size_t numCollected;
122 void* memBlocks;
123 int8_t* curMemBlockAvail;
124 int8_t* curMemBlockEnd;
125} cache_t;
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127/* util.c */
128
Kenny Root86c95842011-03-31 13:16:12 -0700129int create_pkg_path_in_dir(char path[PKG_PATH_MAX],
130 const dir_rec_t* dir,
131 const char* pkgname,
132 const char* postfix);
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134int create_pkg_path(char path[PKG_PATH_MAX],
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 const char *pkgname,
Kenny Root86c95842011-03-31 13:16:12 -0700136 const char *postfix,
137 uid_t persona);
138
Amith Yamasani0b285492011-04-14 17:35:23 -0700139int create_persona_path(char path[PKG_PATH_MAX],
140 uid_t persona);
141
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700142int create_persona_media_path(char path[PKG_PATH_MAX], userid_t userid);
143
Kenny Rootad757e92011-11-29 15:54:55 -0800144int create_move_path(char path[PKG_PATH_MAX],
145 const char* pkgname,
146 const char* leaf,
147 uid_t persona);
148
Kenny Root86c95842011-03-31 13:16:12 -0700149int is_valid_package_name(const char* pkgname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
151int create_cache_path(char path[PKG_PATH_MAX], const char *src);
152
153int delete_dir_contents(const char *pathname,
154 int also_delete_dir,
155 const char *ignore);
156
157int delete_dir_contents_fd(int dfd, const char *name);
158
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700159int lookup_media_dir(char basepath[PATH_MAX], const char *dir);
160
161int64_t data_disk_free();
162
163cache_t* start_cache_collection();
164
165void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir);
166
167void clear_cache_files(cache_t* cache, int64_t free_size);
168
169void finish_cache_collection(cache_t* cache);
170
Kenny Root86c95842011-03-31 13:16:12 -0700171int validate_system_app_path(const char* path);
172
173int get_path_from_env(dir_rec_t* rec, const char* var);
174
175int get_path_from_string(dir_rec_t* rec, const char* path);
176
177int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
178
179int validate_apk_path(const char *path);
180
181int append_and_increment(char** dst, const char* src, size_t* dst_size);
182
Amith Yamasani0b285492011-04-14 17:35:23 -0700183char *build_string2(char *s1, char *s2);
184char *build_string3(char *s1, char *s2, char *s3);
185
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700186int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188/* commands.c */
189
Kenny Root35ab3ad2011-02-02 16:42:14 -0800190int install(const char *pkgname, uid_t uid, gid_t gid);
Amith Yamasani0b285492011-04-14 17:35:23 -0700191int uninstall(const char *pkgname, uid_t persona);
Kenny Root35ab3ad2011-02-02 16:42:14 -0800192int renamepkg(const char *oldpkgname, const char *newpkgname);
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700193int fix_uid(const char *pkgname, uid_t uid, gid_t gid);
Amith Yamasani0b285492011-04-14 17:35:23 -0700194int delete_user_data(const char *pkgname, uid_t persona);
195int make_user_data(const char *pkgname, uid_t uid, uid_t persona);
196int delete_persona(uid_t persona);
Amith Yamasani742a6712011-05-04 14:49:28 -0700197int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy);
Kenny Root35ab3ad2011-02-02 16:42:14 -0800198int delete_cache(const char *pkgname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199int move_dex(const char *src, const char *dst);
200int rm_dex(const char *path);
201int protect(char *pkgname, gid_t gid);
202int get_size(const char *pkgname, const char *apkpath, const char *fwdlock_apkpath,
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700203 const char *asecpath, int64_t *codesize, int64_t *datasize, int64_t *cachesize,
204 int64_t *asecsize);
Kenny Root3e319a92010-09-07 13:58:28 -0700205int free_cache(int64_t free_size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206int dexopt(const char *apk_path, uid_t uid, int is_public);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800207int movefiles();
Kenny Root6a6b0072010-10-07 16:46:10 -0700208int linklib(const char* target, const char* source);
209int unlinklib(const char* libPath);