blob: d51004a1c62064f22b704b92d50db7716e372021 [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
Dianne Hackbornd0c5f512012-06-07 16:53:59 -070060static int do_fixuid(char **arg, char reply[REPLY_MAX])
61{
62 return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
63}
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
66{
Kenny Root3e319a92010-09-07 13:58:28 -070067 return free_cache((int64_t)atoll(arg[0])); /* free_size */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068}
69
70static int do_rm_cache(char **arg, char reply[REPLY_MAX])
71{
Kenny Root35ab3ad2011-02-02 16:42:14 -080072 return delete_cache(arg[0]); /* pkgname */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073}
74
75static int do_protect(char **arg, char reply[REPLY_MAX])
76{
77 return protect(arg[0], atoi(arg[1])); /* pkgname, gid */
78}
79
80static int do_get_size(char **arg, char reply[REPLY_MAX])
81{
Kenny Root3e319a92010-09-07 13:58:28 -070082 int64_t codesize = 0;
83 int64_t datasize = 0;
84 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070085 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 int res = 0;
87
Dianne Hackborn0c380492012-08-20 17:23:30 -070088 /* pkgdir, persona, apkpath */
89 res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4],
90 &codesize, &datasize, &cachesize, &asecsize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Kenny Root3e319a92010-09-07 13:58:28 -070092 /*
93 * Each int64_t can take up 22 characters printed out. Make sure it
94 * doesn't go over REPLY_MAX in the future.
95 */
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070096 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
97 codesize, datasize, cachesize, asecsize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 return res;
99}
100
101static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
102{
Amith Yamasani0b285492011-04-14 17:35:23 -0700103 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
104}
105
106static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
107{
108 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */
109}
110
111static int do_rm_user(char **arg, char reply[REPLY_MAX])
112{
113 return delete_persona(atoi(arg[0])); /* userid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114}
115
Amith Yamasani742a6712011-05-04 14:49:28 -0700116static int do_clone_user_data(char **arg, char reply[REPLY_MAX])
117{
118 return clone_persona_data(atoi(arg[0]), atoi(arg[1]), atoi(arg[2]));
119}
120
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800121static int do_movefiles(char **arg, char reply[REPLY_MAX])
122{
123 return movefiles();
124}
125
Kenny Root6a6b0072010-10-07 16:46:10 -0700126static int do_linklib(char **arg, char reply[REPLY_MAX])
127{
128 return linklib(arg[0], arg[1]);
129}
130
131static int do_unlinklib(char **arg, char reply[REPLY_MAX])
132{
133 return unlinklib(arg[0]);
134}
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136struct cmdinfo {
137 const char *name;
138 unsigned numargs;
139 int (*func)(char **arg, char reply[REPLY_MAX]);
140};
141
142struct cmdinfo cmds[] = {
143 { "ping", 0, do_ping },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800144 { "install", 3, do_install },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 { "dexopt", 3, do_dexopt },
146 { "movedex", 2, do_move_dex },
147 { "rmdex", 1, do_rm_dex },
Amith Yamasani0b285492011-04-14 17:35:23 -0700148 { "remove", 2, do_remove },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800149 { "rename", 2, do_rename },
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700150 { "fixuid", 3, do_fixuid },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 { "freecache", 1, do_free_cache },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800152 { "rmcache", 1, do_rm_cache },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 { "protect", 2, do_protect },
Dianne Hackborn0c380492012-08-20 17:23:30 -0700154 { "getsize", 5, do_get_size },
Amith Yamasani0b285492011-04-14 17:35:23 -0700155 { "rmuserdata", 2, do_rm_user_data },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800156 { "movefiles", 0, do_movefiles },
Kenny Root6a6b0072010-10-07 16:46:10 -0700157 { "linklib", 2, do_linklib },
158 { "unlinklib", 1, do_unlinklib },
Amith Yamasani0b285492011-04-14 17:35:23 -0700159 { "mkuserdata", 3, do_mk_user_data },
160 { "rmuser", 1, do_rm_user },
Amith Yamasani742a6712011-05-04 14:49:28 -0700161 { "cloneuserdata", 3, do_clone_user_data },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162};
163
164static int readx(int s, void *_buf, int count)
165{
166 char *buf = _buf;
167 int n = 0, r;
168 if (count < 0) return -1;
169 while (n < count) {
170 r = read(s, buf + n, count - n);
171 if (r < 0) {
172 if (errno == EINTR) continue;
Steve Block3762c312012-01-06 19:20:56 +0000173 ALOGE("read error: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 return -1;
175 }
176 if (r == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000177 ALOGE("eof\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 return -1; /* EOF */
179 }
180 n += r;
181 }
182 return 0;
183}
184
185static int writex(int s, const void *_buf, int count)
186{
187 const char *buf = _buf;
188 int n = 0, r;
189 if (count < 0) return -1;
190 while (n < count) {
191 r = write(s, buf + n, count - n);
192 if (r < 0) {
193 if (errno == EINTR) continue;
Steve Block3762c312012-01-06 19:20:56 +0000194 ALOGE("write error: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 return -1;
196 }
197 n += r;
198 }
199 return 0;
200}
201
202
203/* Tokenize the command buffer, locate a matching command,
204 * ensure that the required number of arguments are provided,
205 * call the function(), return the result.
206 */
207static int execute(int s, char cmd[BUFFER_MAX])
208{
209 char reply[REPLY_MAX];
210 char *arg[TOKEN_MAX+1];
211 unsigned i;
212 unsigned n = 0;
213 unsigned short count;
214 int ret = -1;
215
Steve Block6215d3f2012-01-04 20:05:49 +0000216// ALOGI("execute('%s')\n", cmd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
218 /* default reply is "" */
219 reply[0] = 0;
220
221 /* n is number of args (not counting arg[0]) */
222 arg[0] = cmd;
223 while (*cmd) {
224 if (isspace(*cmd)) {
225 *cmd++ = 0;
226 n++;
227 arg[n] = cmd;
228 if (n == TOKEN_MAX) {
Steve Block3762c312012-01-06 19:20:56 +0000229 ALOGE("too many arguments\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 goto done;
231 }
232 }
233 cmd++;
234 }
235
236 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
237 if (!strcmp(cmds[i].name,arg[0])) {
238 if (n != cmds[i].numargs) {
Steve Block3762c312012-01-06 19:20:56 +0000239 ALOGE("%s requires %d arguments (%d given)\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 cmds[i].name, cmds[i].numargs, n);
241 } else {
242 ret = cmds[i].func(arg + 1, reply);
243 }
244 goto done;
245 }
246 }
Steve Block3762c312012-01-06 19:20:56 +0000247 ALOGE("unsupported command '%s'\n", arg[0]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
249done:
250 if (reply[0]) {
251 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
252 } else {
253 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
254 }
255 if (n > BUFFER_MAX) n = BUFFER_MAX;
256 count = n;
257
Steve Block6215d3f2012-01-04 20:05:49 +0000258// ALOGI("reply: '%s'\n", cmd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 if (writex(s, &count, sizeof(count))) return -1;
260 if (writex(s, cmd, count)) return -1;
261 return 0;
262}
263
Kenny Root86c95842011-03-31 13:16:12 -0700264/**
265 * Initialize all the global variables that are used elsewhere. Returns 0 upon
266 * success and -1 on error.
267 */
268void free_globals() {
269 size_t i;
270
271 for (i = 0; i < android_system_dirs.count; i++) {
272 if (android_system_dirs.dirs[i].path != NULL) {
273 free(android_system_dirs.dirs[i].path);
274 }
275 }
276
277 free(android_system_dirs.dirs);
278}
279
280int initialize_globals() {
281 // Get the android data directory.
282 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
283 return -1;
284 }
285
286 // Get the android app directory.
287 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
288 return -1;
289 }
290
291 // Get the android protected app directory.
292 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
293 return -1;
294 }
295
296 // Get the sd-card ASEC mount point.
297 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
298 return -1;
299 }
300
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700301 // Get the android media directory.
302 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
303 return -1;
304 }
305
Kenny Root86c95842011-03-31 13:16:12 -0700306 // Take note of the system and vendor directories.
307 android_system_dirs.count = 2;
308
309 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
310 if (android_system_dirs.dirs == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000311 ALOGE("Couldn't allocate array for dirs; aborting\n");
Kenny Root86c95842011-03-31 13:16:12 -0700312 return -1;
313 }
314
315 // system
316 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
317 free_globals();
318 return -1;
319 }
320
Amith Yamasani0b285492011-04-14 17:35:23 -0700321 // append "app/" to dirs[0]
322 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
323 android_system_dirs.dirs[0].path = system_app_path;
324 android_system_dirs.dirs[0].len = strlen(system_app_path);
325
Kenny Root86c95842011-03-31 13:16:12 -0700326 // vendor
327 // TODO replace this with an environment variable (doesn't exist yet)
Amith Yamasani0b285492011-04-14 17:35:23 -0700328 android_system_dirs.dirs[1].path = "/vendor/app/";
Kenny Root86c95842011-03-31 13:16:12 -0700329 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
330
331 return 0;
332}
333
Amith Yamasani0b285492011-04-14 17:35:23 -0700334int initialize_directories() {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700335 int res = -1;
336 int version = 0;
337 FILE* file;
338
339 // Read current filesystem layout version to handle upgrade paths
340 char version_path[PATH_MAX];
341 if (snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path) > PATH_MAX) {
342 return -1;
343 }
344 file = fopen(version_path, "r");
345 if (file != NULL) {
346 fscanf(file, "%d", &version);
347 fclose(file);
348 }
349
Amith Yamasani0b285492011-04-14 17:35:23 -0700350 // /data/user
351 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
352 // /data/data
353 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
354 // /data/user/0
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700355 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
356 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
357 goto fail;
Amith Yamasani0b285492011-04-14 17:35:23 -0700358 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700359
360 // Make the /data/user directory if necessary
361 if (access(user_data_dir, R_OK) < 0) {
362 if (mkdir(user_data_dir, 0711) < 0) {
363 goto fail;
364 }
365 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
366 goto fail;
367 }
368 if (chmod(user_data_dir, 0711) < 0) {
369 goto fail;
370 }
371 }
372 // Make the /data/user/0 symlink to /data/data if necessary
373 if (access(primary_data_dir, R_OK) < 0) {
374 if (symlink(legacy_data_dir, primary_data_dir)) {
375 goto fail;
376 }
377 }
378
379 // /data/media/0
380 char owner_media_dir[PATH_MAX];
381 create_persona_media_path(owner_media_dir, 0);
382
383 if (version == 0) {
384 // Introducing multi-user, so migrate /data/media contents into /data/media/0
385 ALOGD("Migrating /data/media for multi-user");
386
Jeff Sharkeydc9b0122012-08-27 11:41:55 -0700387 // Ensure /data/media
388 if (ensure_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
389 goto fail;
390 }
391
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700392 // /data/media.tmp
393 char media_tmp_dir[PATH_MAX];
394 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
395
396 // Only copy when upgrade not already in progress
397 if (access(media_tmp_dir, F_OK) == -1) {
398 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
399 ALOGE("Failed to move legacy media path: %s", strerror(errno));
400 goto fail;
401 }
402 }
403
404 // Create /data/media again
405 if (ensure_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
406 goto fail;
407 }
408
409 // Move any owner data into place
410 if (access(media_tmp_dir, F_OK) == 0) {
411 if (rename(media_tmp_dir, owner_media_dir) == -1) {
412 ALOGE("Failed to move owner media path: %s", strerror(errno));
413 goto fail;
414 }
415 }
Jeff Sharkey91bbb8a2012-08-16 23:28:50 -0700416
417 // Ensure media directories for any existing users
418 DIR *dir;
419 struct dirent *dirent;
420 char user_media_dir[PATH_MAX];
421
422 dir = opendir(user_data_dir);
423 if (dir != NULL) {
424 while ((dirent = readdir(dir))) {
425 if (dirent->d_type == DT_DIR) {
426 const char *name = dirent->d_name;
427
428 // skip "." and ".."
429 if (name[0] == '.') {
430 if (name[1] == 0) continue;
431 if ((name[1] == '.') && (name[2] == 0)) continue;
432 }
433
434 // /data/media/<user_id>
435 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
436 if (ensure_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
437 ALOGE("Failed to ensure %s: %s", user_media_dir, strerror(errno));
438 goto fail;
439 }
440 }
441 }
442 closedir(dir);
443 }
444
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700445 version = 1;
446 }
447
448 // Ensure /data/media/0 is always ready
449 if (ensure_dir(owner_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
450 goto fail;
451 }
452
453 // Persist our current version
454 file = fopen(version_path, "w");
455 if (file != NULL) {
456 fprintf(file, "%d", version);
457 fsync(fileno(file));
458 fclose(file);
459 } else {
460 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
461 goto fail;
462 }
463
464 // Success!
465 res = 0;
466
467fail:
468 free(user_data_dir);
469 free(legacy_data_dir);
470 free(primary_data_dir);
471 return res;
Amith Yamasani0b285492011-04-14 17:35:23 -0700472}
473
Kenny Root86c95842011-03-31 13:16:12 -0700474int main(const int argc, const char *argv[]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 char buf[BUFFER_MAX];
476 struct sockaddr addr;
477 socklen_t alen;
478 int lsocket, s, count;
479
Kenny Root86c95842011-03-31 13:16:12 -0700480 if (initialize_globals() < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000481 ALOGE("Could not initialize globals; exiting.\n");
Kenny Root86c95842011-03-31 13:16:12 -0700482 exit(1);
483 }
484
Amith Yamasani0b285492011-04-14 17:35:23 -0700485 if (initialize_directories() < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000486 ALOGE("Could not create directories; exiting.\n");
Amith Yamasani0b285492011-04-14 17:35:23 -0700487 exit(1);
488 }
489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 lsocket = android_get_control_socket(SOCKET_PATH);
491 if (lsocket < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000492 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 exit(1);
494 }
495 if (listen(lsocket, 5)) {
Steve Block3762c312012-01-06 19:20:56 +0000496 ALOGE("Listen on socket failed: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 exit(1);
498 }
499 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
500
501 for (;;) {
502 alen = sizeof(addr);
503 s = accept(lsocket, &addr, &alen);
504 if (s < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000505 ALOGE("Accept failed: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 continue;
507 }
508 fcntl(s, F_SETFD, FD_CLOEXEC);
509
Steve Block6215d3f2012-01-04 20:05:49 +0000510 ALOGI("new connection\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 for (;;) {
512 unsigned short count;
513 if (readx(s, &count, sizeof(count))) {
Steve Block3762c312012-01-06 19:20:56 +0000514 ALOGE("failed to read size\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 break;
516 }
517 if ((count < 1) || (count >= BUFFER_MAX)) {
Steve Block3762c312012-01-06 19:20:56 +0000518 ALOGE("invalid size %d\n", count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 break;
520 }
521 if (readx(s, buf, count)) {
Steve Block3762c312012-01-06 19:20:56 +0000522 ALOGE("failed to read command\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 break;
524 }
525 buf[count] = 0;
526 if (execute(s, buf)) break;
527 }
Steve Block6215d3f2012-01-04 20:05:49 +0000528 ALOGI("closing connection\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 close(s);
530 }
531
532 return 0;
533}