blob: 19298a34d408d87bfc8612ca66bc4c5133cee82d [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
Nick Kralevich812b19a2012-08-31 16:08:06 -070017#include <linux/capability.h>
18#include <linux/prctl.h>
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include "installd.h"
21
22
23#define BUFFER_MAX 1024 /* input buffer for commands */
24#define TOKEN_MAX 8 /* max number of arguments in buffer */
25#define REPLY_MAX 256 /* largest reply allowed */
26
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027static int do_ping(char **arg, char reply[REPLY_MAX])
28{
29 return 0;
30}
31
32static int do_install(char **arg, char reply[REPLY_MAX])
33{
Kenny Root35ab3ad2011-02-02 16:42:14 -080034 return install(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035}
36
37static int do_dexopt(char **arg, char reply[REPLY_MAX])
38{
39 /* apk_path, uid, is_public */
40 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]));
41}
42
43static int do_move_dex(char **arg, char reply[REPLY_MAX])
44{
45 return move_dex(arg[0], arg[1]); /* src, dst */
46}
47
48static int do_rm_dex(char **arg, char reply[REPLY_MAX])
49{
50 return rm_dex(arg[0]); /* pkgname */
51}
52
53static int do_remove(char **arg, char reply[REPLY_MAX])
54{
Amith Yamasani0b285492011-04-14 17:35:23 -070055 return uninstall(arg[0], atoi(arg[1])); /* pkgname, userid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056}
57
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080058static int do_rename(char **arg, char reply[REPLY_MAX])
59{
Kenny Root35ab3ad2011-02-02 16:42:14 -080060 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */
Dianne Hackbornb858dfd2010-02-02 10:49:14 -080061}
62
Dianne Hackbornd0c5f512012-06-07 16:53:59 -070063static int do_fixuid(char **arg, char reply[REPLY_MAX])
64{
65 return fix_uid(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, gid */
66}
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068static int do_free_cache(char **arg, char reply[REPLY_MAX]) /* TODO int:free_size */
69{
Kenny Root3e319a92010-09-07 13:58:28 -070070 return free_cache((int64_t)atoll(arg[0])); /* free_size */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071}
72
73static int do_rm_cache(char **arg, char reply[REPLY_MAX])
74{
Amith Yamasani54289b82012-10-01 10:39:14 -070075 return delete_cache(arg[0], atoi(arg[1])); /* pkgname, userid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076}
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078static int do_get_size(char **arg, char reply[REPLY_MAX])
79{
Kenny Root3e319a92010-09-07 13:58:28 -070080 int64_t codesize = 0;
81 int64_t datasize = 0;
82 int64_t cachesize = 0;
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070083 int64_t asecsize = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 int res = 0;
85
Dianne Hackborn0c380492012-08-20 17:23:30 -070086 /* pkgdir, persona, apkpath */
87 res = get_size(arg[0], atoi(arg[1]), arg[2], arg[3], arg[4],
88 &codesize, &datasize, &cachesize, &asecsize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
Kenny Root3e319a92010-09-07 13:58:28 -070090 /*
91 * Each int64_t can take up 22 characters printed out. Make sure it
92 * doesn't go over REPLY_MAX in the future.
93 */
Dianne Hackborn292f8bc2011-06-27 16:27:41 -070094 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64,
95 codesize, datasize, cachesize, asecsize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 return res;
97}
98
99static int do_rm_user_data(char **arg, char reply[REPLY_MAX])
100{
Amith Yamasani0b285492011-04-14 17:35:23 -0700101 return delete_user_data(arg[0], atoi(arg[1])); /* pkgname, userid */
102}
103
104static int do_mk_user_data(char **arg, char reply[REPLY_MAX])
105{
106 return make_user_data(arg[0], atoi(arg[1]), atoi(arg[2])); /* pkgname, uid, userid */
107}
108
109static int do_rm_user(char **arg, char reply[REPLY_MAX])
110{
111 return delete_persona(atoi(arg[0])); /* userid */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112}
113
Amith Yamasani742a6712011-05-04 14:49:28 -0700114static int do_clone_user_data(char **arg, char reply[REPLY_MAX])
115{
116 return clone_persona_data(atoi(arg[0]), atoi(arg[1]), atoi(arg[2]));
117}
118
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800119static int do_movefiles(char **arg, char reply[REPLY_MAX])
120{
121 return movefiles();
122}
123
Kenny Root6a6b0072010-10-07 16:46:10 -0700124static int do_linklib(char **arg, char reply[REPLY_MAX])
125{
126 return linklib(arg[0], arg[1]);
127}
128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129struct cmdinfo {
130 const char *name;
131 unsigned numargs;
132 int (*func)(char **arg, char reply[REPLY_MAX]);
133};
134
135struct cmdinfo cmds[] = {
136 { "ping", 0, do_ping },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800137 { "install", 3, do_install },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 { "dexopt", 3, do_dexopt },
139 { "movedex", 2, do_move_dex },
140 { "rmdex", 1, do_rm_dex },
Amith Yamasani0b285492011-04-14 17:35:23 -0700141 { "remove", 2, do_remove },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800142 { "rename", 2, do_rename },
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700143 { "fixuid", 3, do_fixuid },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 { "freecache", 1, do_free_cache },
Amith Yamasani54289b82012-10-01 10:39:14 -0700145 { "rmcache", 2, do_rm_cache },
Dianne Hackborn0c380492012-08-20 17:23:30 -0700146 { "getsize", 5, do_get_size },
Amith Yamasani0b285492011-04-14 17:35:23 -0700147 { "rmuserdata", 2, do_rm_user_data },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800148 { "movefiles", 0, do_movefiles },
Kenny Root6a6b0072010-10-07 16:46:10 -0700149 { "linklib", 2, do_linklib },
Amith Yamasani0b285492011-04-14 17:35:23 -0700150 { "mkuserdata", 3, do_mk_user_data },
151 { "rmuser", 1, do_rm_user },
Amith Yamasani742a6712011-05-04 14:49:28 -0700152 { "cloneuserdata", 3, do_clone_user_data },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153};
154
155static int readx(int s, void *_buf, int count)
156{
157 char *buf = _buf;
158 int n = 0, r;
159 if (count < 0) return -1;
160 while (n < count) {
161 r = read(s, buf + n, count - n);
162 if (r < 0) {
163 if (errno == EINTR) continue;
Steve Block3762c312012-01-06 19:20:56 +0000164 ALOGE("read error: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 return -1;
166 }
167 if (r == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000168 ALOGE("eof\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 return -1; /* EOF */
170 }
171 n += r;
172 }
173 return 0;
174}
175
176static int writex(int s, const void *_buf, int count)
177{
178 const char *buf = _buf;
179 int n = 0, r;
180 if (count < 0) return -1;
181 while (n < count) {
182 r = write(s, buf + n, count - n);
183 if (r < 0) {
184 if (errno == EINTR) continue;
Steve Block3762c312012-01-06 19:20:56 +0000185 ALOGE("write error: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 return -1;
187 }
188 n += r;
189 }
190 return 0;
191}
192
193
194/* Tokenize the command buffer, locate a matching command,
195 * ensure that the required number of arguments are provided,
196 * call the function(), return the result.
197 */
198static int execute(int s, char cmd[BUFFER_MAX])
199{
200 char reply[REPLY_MAX];
201 char *arg[TOKEN_MAX+1];
202 unsigned i;
203 unsigned n = 0;
204 unsigned short count;
205 int ret = -1;
206
Steve Block6215d3f2012-01-04 20:05:49 +0000207// ALOGI("execute('%s')\n", cmd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
209 /* default reply is "" */
210 reply[0] = 0;
211
212 /* n is number of args (not counting arg[0]) */
213 arg[0] = cmd;
214 while (*cmd) {
215 if (isspace(*cmd)) {
216 *cmd++ = 0;
217 n++;
218 arg[n] = cmd;
219 if (n == TOKEN_MAX) {
Steve Block3762c312012-01-06 19:20:56 +0000220 ALOGE("too many arguments\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 goto done;
222 }
223 }
224 cmd++;
225 }
226
227 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
228 if (!strcmp(cmds[i].name,arg[0])) {
229 if (n != cmds[i].numargs) {
Steve Block3762c312012-01-06 19:20:56 +0000230 ALOGE("%s requires %d arguments (%d given)\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 cmds[i].name, cmds[i].numargs, n);
232 } else {
233 ret = cmds[i].func(arg + 1, reply);
234 }
235 goto done;
236 }
237 }
Steve Block3762c312012-01-06 19:20:56 +0000238 ALOGE("unsupported command '%s'\n", arg[0]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239
240done:
241 if (reply[0]) {
242 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
243 } else {
244 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
245 }
246 if (n > BUFFER_MAX) n = BUFFER_MAX;
247 count = n;
248
Steve Block6215d3f2012-01-04 20:05:49 +0000249// ALOGI("reply: '%s'\n", cmd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 if (writex(s, &count, sizeof(count))) return -1;
251 if (writex(s, cmd, count)) return -1;
252 return 0;
253}
254
Kenny Root86c95842011-03-31 13:16:12 -0700255/**
256 * Initialize all the global variables that are used elsewhere. Returns 0 upon
257 * success and -1 on error.
258 */
259void free_globals() {
260 size_t i;
261
262 for (i = 0; i < android_system_dirs.count; i++) {
263 if (android_system_dirs.dirs[i].path != NULL) {
264 free(android_system_dirs.dirs[i].path);
265 }
266 }
267
268 free(android_system_dirs.dirs);
269}
270
271int initialize_globals() {
272 // Get the android data directory.
273 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
274 return -1;
275 }
276
277 // Get the android app directory.
278 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
279 return -1;
280 }
281
282 // Get the android protected app directory.
283 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
284 return -1;
285 }
286
Kenny Root9bbd70a2012-09-10 11:13:36 -0700287 // Get the android app native library directory.
288 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
289 return -1;
290 }
291
Kenny Root86c95842011-03-31 13:16:12 -0700292 // Get the sd-card ASEC mount point.
293 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
294 return -1;
295 }
296
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700297 // Get the android media directory.
298 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
299 return -1;
300 }
301
Kenny Root86c95842011-03-31 13:16:12 -0700302 // Take note of the system and vendor directories.
303 android_system_dirs.count = 2;
304
305 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
306 if (android_system_dirs.dirs == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000307 ALOGE("Couldn't allocate array for dirs; aborting\n");
Kenny Root86c95842011-03-31 13:16:12 -0700308 return -1;
309 }
310
311 // system
312 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
313 free_globals();
314 return -1;
315 }
316
Amith Yamasani0b285492011-04-14 17:35:23 -0700317 // append "app/" to dirs[0]
318 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
319 android_system_dirs.dirs[0].path = system_app_path;
320 android_system_dirs.dirs[0].len = strlen(system_app_path);
321
Kenny Root86c95842011-03-31 13:16:12 -0700322 // vendor
323 // TODO replace this with an environment variable (doesn't exist yet)
Amith Yamasani0b285492011-04-14 17:35:23 -0700324 android_system_dirs.dirs[1].path = "/vendor/app/";
Kenny Root86c95842011-03-31 13:16:12 -0700325 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
326
327 return 0;
328}
329
Amith Yamasani0b285492011-04-14 17:35:23 -0700330int initialize_directories() {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700331 int res = -1;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700332
333 // Read current filesystem layout version to handle upgrade paths
334 char version_path[PATH_MAX];
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700335 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
336
337 int oldVersion;
338 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
339 oldVersion = 0;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700340 }
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700341 int version = oldVersion;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700342
Amith Yamasani0b285492011-04-14 17:35:23 -0700343 // /data/user
344 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
345 // /data/data
346 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
347 // /data/user/0
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700348 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
349 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
350 goto fail;
Amith Yamasani0b285492011-04-14 17:35:23 -0700351 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700352
353 // Make the /data/user directory if necessary
354 if (access(user_data_dir, R_OK) < 0) {
355 if (mkdir(user_data_dir, 0711) < 0) {
356 goto fail;
357 }
358 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
359 goto fail;
360 }
361 if (chmod(user_data_dir, 0711) < 0) {
362 goto fail;
363 }
364 }
365 // Make the /data/user/0 symlink to /data/data if necessary
366 if (access(primary_data_dir, R_OK) < 0) {
367 if (symlink(legacy_data_dir, primary_data_dir)) {
368 goto fail;
369 }
370 }
371
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700372 if (version == 0) {
373 // Introducing multi-user, so migrate /data/media contents into /data/media/0
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700374 ALOGD("Upgrading /data/media for multi-user");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700375
Jeff Sharkeydc9b0122012-08-27 11:41:55 -0700376 // Ensure /data/media
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700377 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
Jeff Sharkeydc9b0122012-08-27 11:41:55 -0700378 goto fail;
379 }
380
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700381 // /data/media.tmp
382 char media_tmp_dir[PATH_MAX];
383 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
384
385 // Only copy when upgrade not already in progress
386 if (access(media_tmp_dir, F_OK) == -1) {
387 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
388 ALOGE("Failed to move legacy media path: %s", strerror(errno));
389 goto fail;
390 }
391 }
392
393 // Create /data/media again
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700394 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700395 goto fail;
396 }
397
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700398 // /data/media/0
399 char owner_media_dir[PATH_MAX];
400 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
401
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700402 // Move any owner data into place
403 if (access(media_tmp_dir, F_OK) == 0) {
404 if (rename(media_tmp_dir, owner_media_dir) == -1) {
405 ALOGE("Failed to move owner media path: %s", strerror(errno));
406 goto fail;
407 }
408 }
Jeff Sharkey91bbb8a2012-08-16 23:28:50 -0700409
410 // Ensure media directories for any existing users
411 DIR *dir;
412 struct dirent *dirent;
413 char user_media_dir[PATH_MAX];
414
415 dir = opendir(user_data_dir);
416 if (dir != NULL) {
417 while ((dirent = readdir(dir))) {
418 if (dirent->d_type == DT_DIR) {
419 const char *name = dirent->d_name;
420
421 // skip "." and ".."
422 if (name[0] == '.') {
423 if (name[1] == 0) continue;
424 if ((name[1] == '.') && (name[2] == 0)) continue;
425 }
426
427 // /data/media/<user_id>
428 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700429 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
Jeff Sharkey91bbb8a2012-08-16 23:28:50 -0700430 goto fail;
431 }
432 }
433 }
434 closedir(dir);
435 }
436
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700437 version = 1;
438 }
439
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700440 // /data/media/obb
441 char media_obb_dir[PATH_MAX];
442 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
443
444 if (version == 1) {
445 // Introducing /data/media/obb for sharing OBB across users; migrate
446 // any existing OBB files from owner.
447 ALOGD("Upgrading to shared /data/media/obb");
448
449 // /data/media/0/Android/obb
450 char owner_obb_path[PATH_MAX];
451 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
452
453 // Only move if target doesn't already exist
454 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
455 if (rename(owner_obb_path, media_obb_dir) == -1) {
456 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
457 goto fail;
458 }
459 }
460
461 version = 2;
462 }
463
464 if (ensure_media_user_dirs(0) == -1) {
465 ALOGE("Failed to setup media for user 0");
466 goto fail;
467 }
468 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700469 goto fail;
470 }
471
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700472 // Persist layout version if changed
473 if (version != oldVersion) {
474 if (fs_write_atomic_int(version_path, version) == -1) {
475 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
476 goto fail;
477 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700478 }
479
480 // Success!
481 res = 0;
482
483fail:
484 free(user_data_dir);
485 free(legacy_data_dir);
486 free(primary_data_dir);
487 return res;
Amith Yamasani0b285492011-04-14 17:35:23 -0700488}
489
Nick Kralevich812b19a2012-08-31 16:08:06 -0700490static void drop_privileges() {
491 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
492 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
493 exit(1);
494 }
495
496 if (setgid(AID_INSTALL) < 0) {
497 ALOGE("setgid() can't drop privileges; exiting.\n");
498 exit(1);
499 }
500
501 if (setuid(AID_INSTALL) < 0) {
502 ALOGE("setuid() can't drop privileges; exiting.\n");
503 exit(1);
504 }
505
506 struct __user_cap_header_struct capheader;
507 struct __user_cap_data_struct capdata[2];
508 memset(&capheader, 0, sizeof(capheader));
509 memset(&capdata, 0, sizeof(capdata));
510 capheader.version = _LINUX_CAPABILITY_VERSION_3;
511 capheader.pid = 0;
512
513 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
514 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
515 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
516 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
517
518 capdata[0].effective = capdata[0].permitted;
519 capdata[1].effective = capdata[1].permitted;
520 capdata[0].inheritable = 0;
521 capdata[1].inheritable = 0;
522
523 if (capset(&capheader, &capdata[0]) < 0) {
524 ALOGE("capset failed: %s\n", strerror(errno));
525 exit(1);
526 }
527}
528
Kenny Root86c95842011-03-31 13:16:12 -0700529int main(const int argc, const char *argv[]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 char buf[BUFFER_MAX];
531 struct sockaddr addr;
532 socklen_t alen;
533 int lsocket, s, count;
534
Nick Kralevich812b19a2012-08-31 16:08:06 -0700535 ALOGI("installd firing up\n");
536
Kenny Root86c95842011-03-31 13:16:12 -0700537 if (initialize_globals() < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000538 ALOGE("Could not initialize globals; exiting.\n");
Kenny Root86c95842011-03-31 13:16:12 -0700539 exit(1);
540 }
541
Amith Yamasani0b285492011-04-14 17:35:23 -0700542 if (initialize_directories() < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000543 ALOGE("Could not create directories; exiting.\n");
Amith Yamasani0b285492011-04-14 17:35:23 -0700544 exit(1);
545 }
546
Nick Kralevich812b19a2012-08-31 16:08:06 -0700547 drop_privileges();
548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 lsocket = android_get_control_socket(SOCKET_PATH);
550 if (lsocket < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000551 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 exit(1);
553 }
554 if (listen(lsocket, 5)) {
Steve Block3762c312012-01-06 19:20:56 +0000555 ALOGE("Listen on socket failed: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 exit(1);
557 }
558 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
559
560 for (;;) {
561 alen = sizeof(addr);
562 s = accept(lsocket, &addr, &alen);
563 if (s < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000564 ALOGE("Accept failed: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 continue;
566 }
567 fcntl(s, F_SETFD, FD_CLOEXEC);
568
Steve Block6215d3f2012-01-04 20:05:49 +0000569 ALOGI("new connection\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 for (;;) {
571 unsigned short count;
572 if (readx(s, &count, sizeof(count))) {
Steve Block3762c312012-01-06 19:20:56 +0000573 ALOGE("failed to read size\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 break;
575 }
576 if ((count < 1) || (count >= BUFFER_MAX)) {
Steve Block3762c312012-01-06 19:20:56 +0000577 ALOGE("invalid size %d\n", count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 break;
579 }
580 if (readx(s, buf, count)) {
Steve Block3762c312012-01-06 19:20:56 +0000581 ALOGE("failed to read command\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 break;
583 }
584 buf[count] = 0;
585 if (execute(s, buf)) break;
586 }
Steve Block6215d3f2012-01-04 20:05:49 +0000587 ALOGI("closing connection\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 close(s);
589 }
590
591 return 0;
592}