installd.h revision 7f5c84a0b25706746a92ad1233cbbb8923eda54d
1/*
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 <stdbool.h>
24#include <inttypes.h>
25#include <sys/stat.h>
26#include <dirent.h>
27#include <unistd.h>
28#include <ctype.h>
29#include <fcntl.h>
30#include <errno.h>
31#include <utime.h>
32#include <sys/socket.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35
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
44#if INCLUDE_SYS_MOUNT_FOR_STATFS
45#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"
63
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
79#define PKG_NAME_MAX  128   /* largest allowed package name */
80#define PKG_PATH_MAX  256   /* max size of any path we use */
81
82#define PER_USER_RANGE ((uid_t)100000)   /* range of uids per user
83                                            uid = persona * PER_USER_RANGE + appid */
84
85/* 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,
142                    uid_t persona);
143
144int create_persona_path(char path[PKG_PATH_MAX],
145                    uid_t persona);
146
147int create_persona_media_path(char path[PKG_PATH_MAX], userid_t userid);
148
149int create_move_path(char path[PKG_PATH_MAX],
150                     const char* pkgname,
151                     const char* leaf,
152                     uid_t persona);
153
154int is_valid_package_name(const char* pkgname);
155
156int create_cache_path(char path[PKG_PATH_MAX], const char *src);
157
158int delete_dir_contents(const char *pathname,
159                        int also_delete_dir,
160                        const char *ignore);
161
162int delete_dir_contents_fd(int dfd, const char *name);
163
164int lookup_media_dir(char basepath[PATH_MAX], const char *dir);
165
166int64_t data_disk_free();
167
168cache_t* start_cache_collection();
169
170void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir);
171
172void clear_cache_files(cache_t* cache, int64_t free_size);
173
174void finish_cache_collection(cache_t* cache);
175
176int validate_system_app_path(const char* path);
177
178int get_path_from_env(dir_rec_t* rec, const char* var);
179
180int get_path_from_string(dir_rec_t* rec, const char* path);
181
182int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
183
184int validate_apk_path(const char *path);
185
186int append_and_increment(char** dst, const char* src, size_t* dst_size);
187
188char *build_string2(char *s1, char *s2);
189char *build_string3(char *s1, char *s2, char *s3);
190
191int ensure_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
192int ensure_media_user_dirs(userid_t userid);
193
194/* commands.c */
195
196int install(const char *pkgname, uid_t uid, gid_t gid,
197            bool restrictHomeDirectory);
198int uninstall(const char *pkgname, uid_t persona);
199int renamepkg(const char *oldpkgname, const char *newpkgname);
200int fix_uid(const char *pkgname, uid_t uid, gid_t gid);
201int delete_user_data(const char *pkgname, uid_t persona);
202int make_user_data(const char *pkgname, uid_t uid, uid_t persona,
203                   bool restrictHomeDirectory);
204int delete_persona(uid_t persona);
205int delete_cache(const char *pkgname, uid_t persona);
206int move_dex(const char *src, const char *dst);
207int rm_dex(const char *path);
208int protect(char *pkgname, gid_t gid);
209int get_size(const char *pkgname, int persona, const char *apkpath, const char *fwdlock_apkpath,
210             const char *asecpath, int64_t *codesize, int64_t *datasize, int64_t *cachesize,
211             int64_t *asecsize);
212int free_cache(int64_t free_size);
213int dexopt(const char *apk_path, uid_t uid, int is_public);
214int movefiles();
215int linklib(const char* target, const char* source, int userId);
216