blob: feb6b927dd9b9147f73d97299eb96f1ce72bd3b4 [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;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070080 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 int res = 0;
82
83 /* pkgdir, apkpath */
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070084 res = get_size(arg[0], arg[1], arg[2], arg[3], &codesize, &datasize, &cachesize, &asecsize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
Kenny Root3e319a92010-09-07 13:58:28 -070086 /*
87 * Each int64_t can take up 22 characters printed out. Make sure it
88 * doesn't go over REPLY_MAX in the future.
89 */
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070090 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
91 codesize, datasize, cachesize, asecsize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 return res;
93}
94
95static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
96{
Amith Yamasani0b285492011-04-14 17:35:23 -070097 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
98}
99
100static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
101{
102 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */
103}
104
105static int do_rm_user(char **arg, char reply[REPLY_MAX])
106{
107 return delete_persona(atoi(arg[0])); /* userid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108}
109
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800110static int do_movefiles(char **arg, char reply[REPLY_MAX])
111{
112 return movefiles();
113}
114
Kenny Root6a6b0072010-10-07 16:46:10 -0700115static int do_linklib(char **arg, char reply[REPLY_MAX])
116{
117 return linklib(arg[0], arg[1]);
118}
119
120static int do_unlinklib(char **arg, char reply[REPLY_MAX])
121{
122 return unlinklib(arg[0]);
123}
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125struct cmdinfo {
126 const char *name;
127 unsigned numargs;
128 int (*func)(char **arg, char reply[REPLY_MAX]);
129};
130
131struct cmdinfo cmds[] = {
132 { "ping", 0, do_ping },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800133 { "install", 3, do_install },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 { "dexopt", 3, do_dexopt },
135 { "movedex", 2, do_move_dex },
136 { "rmdex", 1, do_rm_dex },
Amith Yamasani0b285492011-04-14 17:35:23 -0700137 { "remove", 2, do_remove },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800138 { "rename", 2, do_rename },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 { "freecache", 1, do_free_cache },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800140 { "rmcache", 1, do_rm_cache },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 { "protect", 2, do_protect },
Dianne Hackborn292f8bc2011-06-27 16:27:41 -0700142 { "getsize", 4, do_get_size },
Amith Yamasani0b285492011-04-14 17:35:23 -0700143 { "rmuserdata", 2, do_rm_user_data },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800144 { "movefiles", 0, do_movefiles },
Kenny Root6a6b0072010-10-07 16:46:10 -0700145 { "linklib", 2, do_linklib },
146 { "unlinklib", 1, do_unlinklib },
Amith Yamasani0b285492011-04-14 17:35:23 -0700147 { "mkuserdata", 3, do_mk_user_data },
148 { "rmuser", 1, do_rm_user },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149};
150
151static int readx(int s, void *_buf, int count)
152{
153 char *buf = _buf;
154 int n = 0, r;
155 if (count < 0) return -1;
156 while (n < count) {
157 r = read(s, buf + n, count - n);
158 if (r < 0) {
159 if (errno == EINTR) continue;
160 LOGE("read error: %s\n", strerror(errno));
161 return -1;
162 }
163 if (r == 0) {
164 LOGE("eof\n");
165 return -1; /* EOF */
166 }
167 n += r;
168 }
169 return 0;
170}
171
172static int writex(int s, const void *_buf, int count)
173{
174 const char *buf = _buf;
175 int n = 0, r;
176 if (count < 0) return -1;
177 while (n < count) {
178 r = write(s, buf + n, count - n);
179 if (r < 0) {
180 if (errno == EINTR) continue;
181 LOGE("write error: %s\n", strerror(errno));
182 return -1;
183 }
184 n += r;
185 }
186 return 0;
187}
188
189
190/* Tokenize the command buffer, locate a matching command,
191 * ensure that the required number of arguments are provided,
192 * call the function(), return the result.
193 */
194static int execute(int s, char cmd[BUFFER_MAX])
195{
196 char reply[REPLY_MAX];
197 char *arg[TOKEN_MAX+1];
198 unsigned i;
199 unsigned n = 0;
200 unsigned short count;
201 int ret = -1;
202
203// LOGI("execute('%s')\n", cmd);
204
205 /* default reply is "" */
206 reply[0] = 0;
207
208 /* n is number of args (not counting arg[0]) */
209 arg[0] = cmd;
210 while (*cmd) {
211 if (isspace(*cmd)) {
212 *cmd++ = 0;
213 n++;
214 arg[n] = cmd;
215 if (n == TOKEN_MAX) {
216 LOGE("too many arguments\n");
217 goto done;
218 }
219 }
220 cmd++;
221 }
222
223 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
224 if (!strcmp(cmds[i].name,arg[0])) {
225 if (n != cmds[i].numargs) {
226 LOGE("%s requires %d arguments (%d given)\n",
227 cmds[i].name, cmds[i].numargs, n);
228 } else {
229 ret = cmds[i].func(arg + 1, reply);
230 }
231 goto done;
232 }
233 }
234 LOGE("unsupported command '%s'\n", arg[0]);
235
236done:
237 if (reply[0]) {
238 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
239 } else {
240 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
241 }
242 if (n > BUFFER_MAX) n = BUFFER_MAX;
243 count = n;
244
245// LOGI("reply: '%s'\n", cmd);
246 if (writex(s, &count, sizeof(count))) return -1;
247 if (writex(s, cmd, count)) return -1;
248 return 0;
249}
250
Kenny Root86c95842011-03-31 13:16:12 -0700251/**
252 * Initialize all the global variables that are used elsewhere. Returns 0 upon
253 * success and -1 on error.
254 */
255void free_globals() {
256 size_t i;
257
258 for (i = 0; i < android_system_dirs.count; i++) {
259 if (android_system_dirs.dirs[i].path != NULL) {
260 free(android_system_dirs.dirs[i].path);
261 }
262 }
263
264 free(android_system_dirs.dirs);
265}
266
267int initialize_globals() {
268 // Get the android data directory.
269 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
270 return -1;
271 }
272
273 // Get the android app directory.
274 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
275 return -1;
276 }
277
278 // Get the android protected app directory.
279 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
280 return -1;
281 }
282
283 // Get the sd-card ASEC mount point.
284 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
285 return -1;
286 }
287
288 // Take note of the system and vendor directories.
289 android_system_dirs.count = 2;
290
291 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
292 if (android_system_dirs.dirs == NULL) {
293 LOGE("Couldn't allocate array for dirs; aborting\n");
294 return -1;
295 }
296
297 // system
298 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
299 free_globals();
300 return -1;
301 }
302
Amith Yamasani0b285492011-04-14 17:35:23 -0700303 // append "app/" to dirs[0]
304 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
305 android_system_dirs.dirs[0].path = system_app_path;
306 android_system_dirs.dirs[0].len = strlen(system_app_path);
307
Kenny Root86c95842011-03-31 13:16:12 -0700308 // vendor
309 // TODO replace this with an environment variable (doesn't exist yet)
Amith Yamasani0b285492011-04-14 17:35:23 -0700310 android_system_dirs.dirs[1].path = "/vendor/app/";
Kenny Root86c95842011-03-31 13:16:12 -0700311 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
312
313 return 0;
314}
315
Amith Yamasani0b285492011-04-14 17:35:23 -0700316int initialize_directories() {
317 // /data/user
318 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
319 // /data/data
320 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
321 // /data/user/0
322 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX,
323 "0");
324 int ret = -1;
325 if (user_data_dir != NULL && primary_data_dir != NULL && legacy_data_dir != NULL) {
326 ret = 0;
327 // Make the /data/user directory if necessary
328 if (access(user_data_dir, R_OK) < 0) {
329 if (mkdir(user_data_dir, 0755) < 0) {
330 return -1;
331 }
332 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
333 return -1;
334 }
335 }
336 // Make the /data/user/0 symlink to /data/data if necessary
337 if (access(primary_data_dir, R_OK) < 0) {
338 ret = symlink(legacy_data_dir, primary_data_dir);
339 }
340 free(user_data_dir);
341 free(legacy_data_dir);
342 free(primary_data_dir);
343 }
344 return ret;
345}
346
Kenny Root86c95842011-03-31 13:16:12 -0700347int main(const int argc, const char *argv[]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 char buf[BUFFER_MAX];
349 struct sockaddr addr;
350 socklen_t alen;
351 int lsocket, s, count;
352
Kenny Root86c95842011-03-31 13:16:12 -0700353 if (initialize_globals() < 0) {
354 LOGE("Could not initialize globals; exiting.\n");
355 exit(1);
356 }
357
Amith Yamasani0b285492011-04-14 17:35:23 -0700358 if (initialize_directories() < 0) {
359 LOGE("Could not create directories; exiting.\n");
360 exit(1);
361 }
362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 lsocket = android_get_control_socket(SOCKET_PATH);
364 if (lsocket < 0) {
365 LOGE("Failed to get socket from environment: %s\n", strerror(errno));
366 exit(1);
367 }
368 if (listen(lsocket, 5)) {
369 LOGE("Listen on socket failed: %s\n", strerror(errno));
370 exit(1);
371 }
372 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
373
374 for (;;) {
375 alen = sizeof(addr);
376 s = accept(lsocket, &addr, &alen);
377 if (s < 0) {
378 LOGE("Accept failed: %s\n", strerror(errno));
379 continue;
380 }
381 fcntl(s, F_SETFD, FD_CLOEXEC);
382
383 LOGI("new connection\n");
384 for (;;) {
385 unsigned short count;
386 if (readx(s, &count, sizeof(count))) {
387 LOGE("failed to read size\n");
388 break;
389 }
390 if ((count < 1) || (count >= BUFFER_MAX)) {
391 LOGE("invalid size %d\n", count);
392 break;
393 }
394 if (readx(s, buf, count)) {
395 LOGE("failed to read command\n");
396 break;
397 }
398 buf[count] = 0;
399 if (execute(s, buf)) break;
400 }
401 LOGI("closing connection\n");
402 close(s);
403 }
404
405 return 0;
406}