installd.h revision 197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0ac
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 <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/sockets.h>
36#include <cutils/log.h>
37#include <cutils/properties.h>
38
39#include <private/android_filesystem_config.h>
40
41#if INCLUDE_SYS_MOUNT_FOR_STATFS
42#include <sys/mount.h>
43#else
44#include <sys/statfs.h>
45#endif
46
47#define SOCKET_PATH "installd"
48
49
50/* elements combined with a valid package name to form paths */
51
52#define PRIMARY_USER_PREFIX    "data/"
53#define SECONDARY_USER_PREFIX  "user/"
54
55#define PKG_DIR_POSTFIX        ""
56
57#define PKG_LIB_POSTFIX        "/lib"
58
59#define CACHE_DIR_POSTFIX      "/cache"
60
61#define APP_SUBDIR             "app/" // sub-directory under ANDROID_DATA
62
63#define MEDIA_SUBDIR           "media/" // sub-directory under ANDROID_DATA
64
65/* other handy constants */
66
67#define PRIVATE_APP_SUBDIR     "app-private/" // sub-directory under ANDROID_DATA
68
69#define DALVIK_CACHE_PREFIX    "/data/dalvik-cache/"
70#define DALVIK_CACHE_POSTFIX   "/classes.dex"
71
72#define UPDATE_COMMANDS_DIR_PREFIX  "/system/etc/updatecmds/"
73
74#define PKG_NAME_MAX  128   /* largest allowed package name */
75#define PKG_PATH_MAX  256   /* max size of any path we use */
76
77#define PER_USER_RANGE ((uid_t)100000)   /* range of uids per user
78                                            uid = persona * PER_USER_RANGE + appid */
79
80/* data structures */
81
82typedef struct {
83    char* path;
84    size_t len;
85} dir_rec_t;
86
87typedef struct {
88    size_t count;
89    dir_rec_t* dirs;
90} dir_rec_array_t;
91
92extern dir_rec_t android_app_dir;
93extern dir_rec_t android_app_private_dir;
94extern dir_rec_t android_data_dir;
95extern dir_rec_t android_asec_dir;
96extern dir_rec_t android_media_dir;
97extern dir_rec_array_t android_system_dirs;
98
99typedef struct cache_dir_struct {
100    struct cache_dir_struct* parent;
101    int32_t childCount;
102    int32_t hiddenCount;
103    int32_t deleted;
104    char name[];
105} cache_dir_t;
106
107typedef struct {
108    cache_dir_t* dir;
109    time_t modTime;
110    char name[];
111} cache_file_t;
112
113typedef struct {
114    size_t numDirs;
115    size_t availDirs;
116    cache_dir_t** dirs;
117    size_t numFiles;
118    size_t availFiles;
119    cache_file_t** files;
120    size_t numCollected;
121    void* memBlocks;
122    int8_t* curMemBlockAvail;
123    int8_t* curMemBlockEnd;
124} cache_t;
125
126/* util.c */
127
128int create_pkg_path_in_dir(char path[PKG_PATH_MAX],
129                                const dir_rec_t* dir,
130                                const char* pkgname,
131                                const char* postfix);
132
133int create_pkg_path(char path[PKG_PATH_MAX],
134                    const char *pkgname,
135                    const char *postfix,
136                    uid_t persona);
137
138int create_persona_path(char path[PKG_PATH_MAX],
139                    uid_t persona);
140
141int create_move_path(char path[PKG_PATH_MAX],
142                     const char* pkgname,
143                     const char* leaf,
144                     uid_t persona);
145
146int is_valid_package_name(const char* pkgname);
147
148int create_cache_path(char path[PKG_PATH_MAX], const char *src);
149
150int delete_dir_contents(const char *pathname,
151                        int also_delete_dir,
152                        const char *ignore);
153
154int delete_dir_contents_fd(int dfd, const char *name);
155
156int lookup_media_dir(char basepath[PATH_MAX], const char *dir);
157
158int64_t data_disk_free();
159
160cache_t* start_cache_collection();
161
162void add_cache_files(cache_t* cache, const char *basepath, const char *cachedir);
163
164void clear_cache_files(cache_t* cache, int64_t free_size);
165
166void finish_cache_collection(cache_t* cache);
167
168int validate_system_app_path(const char* path);
169
170int get_path_from_env(dir_rec_t* rec, const char* var);
171
172int get_path_from_string(dir_rec_t* rec, const char* path);
173
174int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
175
176int validate_apk_path(const char *path);
177
178int append_and_increment(char** dst, const char* src, size_t* dst_size);
179
180char *build_string2(char *s1, char *s2);
181char *build_string3(char *s1, char *s2, char *s3);
182
183/* commands.c */
184
185int install(const char *pkgname, uid_t uid, gid_t gid);
186int uninstall(const char *pkgname, uid_t persona);
187int renamepkg(const char *oldpkgname, const char *newpkgname);
188int fix_uid(const char *pkgname, uid_t uid, gid_t gid);
189int delete_user_data(const char *pkgname, uid_t persona);
190int make_user_data(const char *pkgname, uid_t uid, uid_t persona);
191int delete_persona(uid_t persona);
192int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy);
193int delete_cache(const char *pkgname);
194int move_dex(const char *src, const char *dst);
195int rm_dex(const char *path);
196int protect(char *pkgname, gid_t gid);
197int get_size(const char *pkgname, const char *apkpath, const char *fwdlock_apkpath,
198             const char *asecpath, int64_t *codesize, int64_t *datasize, int64_t *cachesize,
199             int64_t *asecsize);
200int free_cache(int64_t free_size);
201int dexopt(const char *apk_path, uid_t uid, int is_public);
202int movefiles();
203int linklib(const char* target, const char* source);
204int unlinklib(const char* libPath);
205