blob: c062d36ec93bfa80bc31e9a48484f6abd59cc78b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2** Copyright 2008, The Android Open Source Project
3**
4** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
7**
8** http://www.apache.org/licenses/LICENSE-2.0
9**
10** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
14** limitations under the License.
15*/
16
17#include "installd.h"
18
19
20#define BUFFER_MAX 1024 /* input buffer for commands */
21#define TOKEN_MAX 8 /* max number of arguments in buffer */
22#define REPLY_MAX 256 /* largest reply allowed */
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024static int do_ping(char **arg, char reply[REPLY_MAX])
25{
26 return 0;
27}
28
29static int do_install(char **arg, char reply[REPLY_MAX])
30{
Kenny Root35ab3ad2011-02-02 16:42:14 -080031 return install(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032}
33
34static int do_dexopt(char **arg, char reply[REPLY_MAX])
35{
36 /* apk_path, uid, is_public */
37 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]));
38}
39
40static int do_move_dex(char **arg, char reply[REPLY_MAX])
41{
42 return move_dex(arg[0], arg[1]); /* src, dst */
43}
44
45static int do_rm_dex(char **arg, char reply[REPLY_MAX])
46{
47 return rm_dex(arg[0]); /* pkgname */
48}
49
50static int do_remove(char **arg, char reply[REPLY_MAX])
51{
Amith Yamasani0b285492011-04-14 17:35:23 -070052 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053}
54
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080055static int do_rename(char **arg, char reply[REPLY_MAX])
56{
Kenny Root35ab3ad2011-02-02 16:42:14 -080057 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080058}
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
61{
Kenny Root3e319a92010-09-07 13:58:28 -070062 return free_cache((int64_t)atoll(arg[0])); /* free_size */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063}
64
65static int do_rm_cache(char **arg, char reply[REPLY_MAX])
66{
Kenny Root35ab3ad2011-02-02 16:42:14 -080067 return delete_cache(arg[0]); /* pkgname */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068}
69
70static int do_protect(char **arg, char reply[REPLY_MAX])
71{
72 return protect(arg[0], atoi(arg[1])); /* pkgname, gid */
73}
74
75static int do_get_size(char **arg, char reply[REPLY_MAX])
76{
Kenny Root3e319a92010-09-07 13:58:28 -070077 int64_t codesize = 0;
78 int64_t datasize = 0;
79 int64_t cachesize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 int res = 0;
81
82 /* pkgdir, apkpath */
Kenny Root35ab3ad2011-02-02 16:42:14 -080083 res = get_size(arg[0], arg[1], arg[2], &codesize, &datasize, &cachesize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Kenny Root3e319a92010-09-07 13:58:28 -070085 /*
86 * Each int64_t can take up 22 characters printed out. Make sure it
87 * doesn't go over REPLY_MAX in the future.
88 */
89 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64, codesize, datasize, cachesize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 return res;
91}
92
93static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
94{
Amith Yamasani0b285492011-04-14 17:35:23 -070095 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
96}
97
98static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
99{
100 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */
101}
102
103static int do_rm_user(char **arg, char reply[REPLY_MAX])
104{
105 return delete_persona(atoi(arg[0])); /* userid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106}
107
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800108static int do_movefiles(char **arg, char reply[REPLY_MAX])
109{
110 return movefiles();
111}
112
Kenny Root6a6b0072010-10-07 16:46:10 -0700113static int do_linklib(char **arg, char reply[REPLY_MAX])
114{
115 return linklib(arg[0], arg[1]);
116}
117
118static int do_unlinklib(char **arg, char reply[REPLY_MAX])
119{
120 return unlinklib(arg[0]);
121}
122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123struct cmdinfo {
124 const char *name;
125 unsigned numargs;
126 int (*func)(char **arg, char reply[REPLY_MAX]);
127};
128
129struct cmdinfo cmds[] = {
130 { "ping", 0, do_ping },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800131 { "install", 3, do_install },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 { "dexopt", 3, do_dexopt },
133 { "movedex", 2, do_move_dex },
134 { "rmdex", 1, do_rm_dex },
Amith Yamasani0b285492011-04-14 17:35:23 -0700135 { "remove", 2, do_remove },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800136 { "rename", 2, do_rename },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 { "freecache", 1, do_free_cache },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800138 { "rmcache", 1, do_rm_cache },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 { "protect", 2, do_protect },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800140 { "getsize", 3, do_get_size },
Amith Yamasani0b285492011-04-14 17:35:23 -0700141 { "rmuserdata", 2, do_rm_user_data },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800142 { "movefiles", 0, do_movefiles },
Kenny Root6a6b0072010-10-07 16:46:10 -0700143 { "linklib", 2, do_linklib },
144 { "unlinklib", 1, do_unlinklib },
Amith Yamasani0b285492011-04-14 17:35:23 -0700145 { "mkuserdata", 3, do_mk_user_data },
146 { "rmuser", 1, do_rm_user },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147};
148
149static int readx(int s, void *_buf, int count)
150{
151 char *buf = _buf;
152 int n = 0, r;
153 if (count < 0) return -1;
154 while (n < count) {
155 r = read(s, buf + n, count - n);
156 if (r < 0) {
157 if (errno == EINTR) continue;
158 LOGE("read error: %s\n", strerror(errno));
159 return -1;
160 }
161 if (r == 0) {
162 LOGE("eof\n");
163 return -1; /* EOF */
164 }
165 n += r;
166 }
167 return 0;
168}
169
170static int writex(int s, const void *_buf, int count)
171{
172 const char *buf = _buf;
173 int n = 0, r;
174 if (count < 0) return -1;
175 while (n < count) {
176 r = write(s, buf + n, count - n);
177 if (r < 0) {
178 if (errno == EINTR) continue;
179 LOGE("write error: %s\n", strerror(errno));
180 return -1;
181 }
182 n += r;
183 }
184 return 0;
185}
186
187
188/* Tokenize the command buffer, locate a matching command,
189 * ensure that the required number of arguments are provided,
190 * call the function(), return the result.
191 */
192static int execute(int s, char cmd[BUFFER_MAX])
193{
194 char reply[REPLY_MAX];
195 char *arg[TOKEN_MAX+1];
196 unsigned i;
197 unsigned n = 0;
198 unsigned short count;
199 int ret = -1;
200
201// LOGI("execute('%s')\n", cmd);
202
203 /* default reply is "" */
204 reply[0] = 0;
205
206 /* n is number of args (not counting arg[0]) */
207 arg[0] = cmd;
208 while (*cmd) {
209 if (isspace(*cmd)) {
210 *cmd++ = 0;
211 n++;
212 arg[n] = cmd;
213 if (n == TOKEN_MAX) {
214 LOGE("too many arguments\n");
215 goto done;
216 }
217 }
218 cmd++;
219 }
220
221 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
222 if (!strcmp(cmds[i].name,arg[0])) {
223 if (n != cmds[i].numargs) {
224 LOGE("%s requires %d arguments (%d given)\n",
225 cmds[i].name, cmds[i].numargs, n);
226 } else {
227 ret = cmds[i].func(arg + 1, reply);
228 }
229 goto done;
230 }
231 }
232 LOGE("unsupported command '%s'\n", arg[0]);
233
234done:
235 if (reply[0]) {
236 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
237 } else {
238 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
239 }
240 if (n > BUFFER_MAX) n = BUFFER_MAX;
241 count = n;
242
243// LOGI("reply: '%s'\n", cmd);
244 if (writex(s, &count, sizeof(count))) return -1;
245 if (writex(s, cmd, count)) return -1;
246 return 0;
247}
248
Kenny Root86c95842011-03-31 13:16:12 -0700249/**
250 * Initialize all the global variables that are used elsewhere. Returns 0 upon
251 * success and -1 on error.
252 */
253void free_globals() {
254 size_t i;
255
256 for (i = 0; i < android_system_dirs.count; i++) {
257 if (android_system_dirs.dirs[i].path != NULL) {
258 free(android_system_dirs.dirs[i].path);
259 }
260 }
261
262 free(android_system_dirs.dirs);
263}
264
265int initialize_globals() {
266 // Get the android data directory.
267 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
268 return -1;
269 }
270
271 // Get the android app directory.
272 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
273 return -1;
274 }
275
276 // Get the android protected app directory.
277 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
278 return -1;
279 }
280
281 // Get the sd-card ASEC mount point.
282 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
283 return -1;
284 }
285
286 // Take note of the system and vendor directories.
287 android_system_dirs.count = 2;
288
289 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
290 if (android_system_dirs.dirs == NULL) {
291 LOGE("Couldn't allocate array for dirs; aborting\n");
292 return -1;
293 }
294
295 // system
296 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
297 free_globals();
298 return -1;
299 }
300
Amith Yamasani0b285492011-04-14 17:35:23 -0700301 // append "app/" to dirs[0]
302 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
303 android_system_dirs.dirs[0].path = system_app_path;
304 android_system_dirs.dirs[0].len = strlen(system_app_path);
305
Kenny Root86c95842011-03-31 13:16:12 -0700306 // vendor
307 // TODO replace this with an environment variable (doesn't exist yet)
Amith Yamasani0b285492011-04-14 17:35:23 -0700308 android_system_dirs.dirs[1].path = "/vendor/app/";
Kenny Root86c95842011-03-31 13:16:12 -0700309 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
310
311 return 0;
312}
313
Amith Yamasani0b285492011-04-14 17:35:23 -0700314int initialize_directories() {
315 // /data/user
316 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
317 // /data/data
318 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
319 // /data/user/0
320 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX,
321 "0");
322 int ret = -1;
323 if (user_data_dir != NULL && primary_data_dir != NULL && legacy_data_dir != NULL) {
324 ret = 0;
325 // Make the /data/user directory if necessary
326 if (access(user_data_dir, R_OK) < 0) {
327 if (mkdir(user_data_dir, 0755) < 0) {
328 return -1;
329 }
330 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
331 return -1;
332 }
333 }
334 // Make the /data/user/0 symlink to /data/data if necessary
335 if (access(primary_data_dir, R_OK) < 0) {
336 ret = symlink(legacy_data_dir, primary_data_dir);
337 }
338 free(user_data_dir);
339 free(legacy_data_dir);
340 free(primary_data_dir);
341 }
342 return ret;
343}
344
Kenny Root86c95842011-03-31 13:16:12 -0700345int main(const int argc, const char *argv[]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 char buf[BUFFER_MAX];
347 struct sockaddr addr;
348 socklen_t alen;
349 int lsocket, s, count;
350
Kenny Root86c95842011-03-31 13:16:12 -0700351 if (initialize_globals() < 0) {
352 LOGE("Could not initialize globals; exiting.\n");
353 exit(1);
354 }
355
Amith Yamasani0b285492011-04-14 17:35:23 -0700356 if (initialize_directories() < 0) {
357 LOGE("Could not create directories; exiting.\n");
358 exit(1);
359 }
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 lsocket = android_get_control_socket(SOCKET_PATH);
362 if (lsocket < 0) {
363 LOGE("Failed to get socket from environment: %s\n", strerror(errno));
364 exit(1);
365 }
366 if (listen(lsocket, 5)) {
367 LOGE("Listen on socket failed: %s\n", strerror(errno));
368 exit(1);
369 }
370 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
371
372 for (;;) {
373 alen = sizeof(addr);
374 s = accept(lsocket, &addr, &alen);
375 if (s < 0) {
376 LOGE("Accept failed: %s\n", strerror(errno));
377 continue;
378 }
379 fcntl(s, F_SETFD, FD_CLOEXEC);
380
381 LOGI("new connection\n");
382 for (;;) {
383 unsigned short count;
384 if (readx(s, &count, sizeof(count))) {
385 LOGE("failed to read size\n");
386 break;
387 }
388 if ((count < 1) || (count >= BUFFER_MAX)) {
389 LOGE("invalid size %d\n", count);
390 break;
391 }
392 if (readx(s, buf, count)) {
393 LOGE("failed to read command\n");
394 break;
395 }
396 buf[count] = 0;
397 if (execute(s, buf)) break;
398 }
399 LOGI("closing connection\n");
400 close(s);
401 }
402
403 return 0;
404}