blob: cc8f014d70dea2e61d6eb2e76bdbcf59993298ee [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{
Kenny Root35ab3ad2011-02-02 16:42:14 -080075 return delete_cache(arg[0]); /* pkgname */
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
129static int do_unlinklib(char **arg, char reply[REPLY_MAX])
130{
131 return unlinklib(arg[0]);
132}
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134struct cmdinfo {
135 const char *name;
136 unsigned numargs;
137 int (*func)(char **arg, char reply[REPLY_MAX]);
138};
139
140struct cmdinfo cmds[] = {
141 { "ping", 0, do_ping },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800142 { "install", 3, do_install },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 { "dexopt", 3, do_dexopt },
144 { "movedex", 2, do_move_dex },
145 { "rmdex", 1, do_rm_dex },
Amith Yamasani0b285492011-04-14 17:35:23 -0700146 { "remove", 2, do_remove },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800147 { "rename", 2, do_rename },
Dianne Hackbornd0c5f512012-06-07 16:53:59 -0700148 { "fixuid", 3, do_fixuid },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 { "freecache", 1, do_free_cache },
Kenny Root35ab3ad2011-02-02 16:42:14 -0800150 { "rmcache", 1, do_rm_cache },
Dianne Hackborn0c380492012-08-20 17:23:30 -0700151 { "getsize", 5, do_get_size },
Amith Yamasani0b285492011-04-14 17:35:23 -0700152 { "rmuserdata", 2, do_rm_user_data },
Dianne Hackbornb858dfd2010-02-02 10:49:14 -0800153 { "movefiles", 0, do_movefiles },
Kenny Root6a6b0072010-10-07 16:46:10 -0700154 { "linklib", 2, do_linklib },
155 { "unlinklib", 1, do_unlinklib },
Amith Yamasani0b285492011-04-14 17:35:23 -0700156 { "mkuserdata", 3, do_mk_user_data },
157 { "rmuser", 1, do_rm_user },
Amith Yamasani742a6712011-05-04 14:49:28 -0700158 { "cloneuserdata", 3, do_clone_user_data },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159};
160
161static int readx(int s, void *_buf, int count)
162{
163 char *buf = _buf;
164 int n = 0, r;
165 if (count < 0) return -1;
166 while (n < count) {
167 r = read(s, buf + n, count - n);
168 if (r < 0) {
169 if (errno == EINTR) continue;
Steve Block3762c312012-01-06 19:20:56 +0000170 ALOGE("read error: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 return -1;
172 }
173 if (r == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000174 ALOGE("eof\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 return -1; /* EOF */
176 }
177 n += r;
178 }
179 return 0;
180}
181
182static int writex(int s, const void *_buf, int count)
183{
184 const char *buf = _buf;
185 int n = 0, r;
186 if (count < 0) return -1;
187 while (n < count) {
188 r = write(s, buf + n, count - n);
189 if (r < 0) {
190 if (errno == EINTR) continue;
Steve Block3762c312012-01-06 19:20:56 +0000191 ALOGE("write error: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 return -1;
193 }
194 n += r;
195 }
196 return 0;
197}
198
199
200/* Tokenize the command buffer, locate a matching command,
201 * ensure that the required number of arguments are provided,
202 * call the function(), return the result.
203 */
204static int execute(int s, char cmd[BUFFER_MAX])
205{
206 char reply[REPLY_MAX];
207 char *arg[TOKEN_MAX+1];
208 unsigned i;
209 unsigned n = 0;
210 unsigned short count;
211 int ret = -1;
212
Steve Block6215d3f2012-01-04 20:05:49 +0000213// ALOGI("execute('%s')\n", cmd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214
215 /* default reply is "" */
216 reply[0] = 0;
217
218 /* n is number of args (not counting arg[0]) */
219 arg[0] = cmd;
220 while (*cmd) {
221 if (isspace(*cmd)) {
222 *cmd++ = 0;
223 n++;
224 arg[n] = cmd;
225 if (n == TOKEN_MAX) {
Steve Block3762c312012-01-06 19:20:56 +0000226 ALOGE("too many arguments\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 goto done;
228 }
229 }
230 cmd++;
231 }
232
233 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
234 if (!strcmp(cmds[i].name,arg[0])) {
235 if (n != cmds[i].numargs) {
Steve Block3762c312012-01-06 19:20:56 +0000236 ALOGE("%s requires %d arguments (%d given)\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 cmds[i].name, cmds[i].numargs, n);
238 } else {
239 ret = cmds[i].func(arg + 1, reply);
240 }
241 goto done;
242 }
243 }
Steve Block3762c312012-01-06 19:20:56 +0000244 ALOGE("unsupported command '%s'\n", arg[0]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
246done:
247 if (reply[0]) {
248 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply);
249 } else {
250 n = snprintf(cmd, BUFFER_MAX, "%d", ret);
251 }
252 if (n > BUFFER_MAX) n = BUFFER_MAX;
253 count = n;
254
Steve Block6215d3f2012-01-04 20:05:49 +0000255// ALOGI("reply: '%s'\n", cmd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 if (writex(s, &count, sizeof(count))) return -1;
257 if (writex(s, cmd, count)) return -1;
258 return 0;
259}
260
Kenny Root86c95842011-03-31 13:16:12 -0700261/**
262 * Initialize all the global variables that are used elsewhere. Returns 0 upon
263 * success and -1 on error.
264 */
265void free_globals() {
266 size_t i;
267
268 for (i = 0; i < android_system_dirs.count; i++) {
269 if (android_system_dirs.dirs[i].path != NULL) {
270 free(android_system_dirs.dirs[i].path);
271 }
272 }
273
274 free(android_system_dirs.dirs);
275}
276
277int initialize_globals() {
278 // Get the android data directory.
279 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) {
280 return -1;
281 }
282
283 // Get the android app directory.
284 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
285 return -1;
286 }
287
288 // Get the android protected app directory.
289 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
290 return -1;
291 }
292
293 // Get the sd-card ASEC mount point.
294 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
295 return -1;
296 }
297
Dianne Hackborn197a0c82012-07-12 14:46:04 -0700298 // Get the android media directory.
299 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
300 return -1;
301 }
302
Kenny Root86c95842011-03-31 13:16:12 -0700303 // Take note of the system and vendor directories.
304 android_system_dirs.count = 2;
305
306 android_system_dirs.dirs = calloc(android_system_dirs.count, sizeof(dir_rec_t));
307 if (android_system_dirs.dirs == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000308 ALOGE("Couldn't allocate array for dirs; aborting\n");
Kenny Root86c95842011-03-31 13:16:12 -0700309 return -1;
310 }
311
312 // system
313 if (get_path_from_env(&android_system_dirs.dirs[0], "ANDROID_ROOT") < 0) {
314 free_globals();
315 return -1;
316 }
317
Amith Yamasani0b285492011-04-14 17:35:23 -0700318 // append "app/" to dirs[0]
319 char *system_app_path = build_string2(android_system_dirs.dirs[0].path, APP_SUBDIR);
320 android_system_dirs.dirs[0].path = system_app_path;
321 android_system_dirs.dirs[0].len = strlen(system_app_path);
322
Kenny Root86c95842011-03-31 13:16:12 -0700323 // vendor
324 // TODO replace this with an environment variable (doesn't exist yet)
Amith Yamasani0b285492011-04-14 17:35:23 -0700325 android_system_dirs.dirs[1].path = "/vendor/app/";
Kenny Root86c95842011-03-31 13:16:12 -0700326 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
327
328 return 0;
329}
330
Amith Yamasani0b285492011-04-14 17:35:23 -0700331int initialize_directories() {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700332 int res = -1;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700333
334 // Read current filesystem layout version to handle upgrade paths
335 char version_path[PATH_MAX];
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700336 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path);
337
338 int oldVersion;
339 if (fs_read_atomic_int(version_path, &oldVersion) == -1) {
340 oldVersion = 0;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700341 }
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700342 int version = oldVersion;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700343
Amith Yamasani0b285492011-04-14 17:35:23 -0700344 // /data/user
345 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX);
346 // /data/data
347 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX);
348 // /data/user/0
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700349 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0");
350 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) {
351 goto fail;
Amith Yamasani0b285492011-04-14 17:35:23 -0700352 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700353
354 // Make the /data/user directory if necessary
355 if (access(user_data_dir, R_OK) < 0) {
356 if (mkdir(user_data_dir, 0711) < 0) {
357 goto fail;
358 }
359 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) {
360 goto fail;
361 }
362 if (chmod(user_data_dir, 0711) < 0) {
363 goto fail;
364 }
365 }
366 // Make the /data/user/0 symlink to /data/data if necessary
367 if (access(primary_data_dir, R_OK) < 0) {
368 if (symlink(legacy_data_dir, primary_data_dir)) {
369 goto fail;
370 }
371 }
372
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700373 if (version == 0) {
374 // Introducing multi-user, so migrate /data/media contents into /data/media/0
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700375 ALOGD("Upgrading /data/media for multi-user");
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700376
Jeff Sharkeydc9b0122012-08-27 11:41:55 -0700377 // Ensure /data/media
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700378 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
Jeff Sharkeydc9b0122012-08-27 11:41:55 -0700379 goto fail;
380 }
381
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700382 // /data/media.tmp
383 char media_tmp_dir[PATH_MAX];
384 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path);
385
386 // Only copy when upgrade not already in progress
387 if (access(media_tmp_dir, F_OK) == -1) {
388 if (rename(android_media_dir.path, media_tmp_dir) == -1) {
389 ALOGE("Failed to move legacy media path: %s", strerror(errno));
390 goto fail;
391 }
392 }
393
394 // Create /data/media again
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700395 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700396 goto fail;
397 }
398
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700399 // /data/media/0
400 char owner_media_dir[PATH_MAX];
401 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path);
402
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700403 // Move any owner data into place
404 if (access(media_tmp_dir, F_OK) == 0) {
405 if (rename(media_tmp_dir, owner_media_dir) == -1) {
406 ALOGE("Failed to move owner media path: %s", strerror(errno));
407 goto fail;
408 }
409 }
Jeff Sharkey91bbb8a2012-08-16 23:28:50 -0700410
411 // Ensure media directories for any existing users
412 DIR *dir;
413 struct dirent *dirent;
414 char user_media_dir[PATH_MAX];
415
416 dir = opendir(user_data_dir);
417 if (dir != NULL) {
418 while ((dirent = readdir(dir))) {
419 if (dirent->d_type == DT_DIR) {
420 const char *name = dirent->d_name;
421
422 // skip "." and ".."
423 if (name[0] == '.') {
424 if (name[1] == 0) continue;
425 if ((name[1] == '.') && (name[2] == 0)) continue;
426 }
427
428 // /data/media/<user_id>
429 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name);
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700430 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
Jeff Sharkey91bbb8a2012-08-16 23:28:50 -0700431 goto fail;
432 }
433 }
434 }
435 closedir(dir);
436 }
437
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700438 version = 1;
439 }
440
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700441 // /data/media/obb
442 char media_obb_dir[PATH_MAX];
443 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path);
444
445 if (version == 1) {
446 // Introducing /data/media/obb for sharing OBB across users; migrate
447 // any existing OBB files from owner.
448 ALOGD("Upgrading to shared /data/media/obb");
449
450 // /data/media/0/Android/obb
451 char owner_obb_path[PATH_MAX];
452 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path);
453
454 // Only move if target doesn't already exist
455 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) {
456 if (rename(owner_obb_path, media_obb_dir) == -1) {
457 ALOGE("Failed to move OBB from owner: %s", strerror(errno));
458 goto fail;
459 }
460 }
461
462 version = 2;
463 }
464
465 if (ensure_media_user_dirs(0) == -1) {
466 ALOGE("Failed to setup media for user 0");
467 goto fail;
468 }
469 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700470 goto fail;
471 }
472
Jeff Sharkey8ea0dc62012-08-27 15:46:54 -0700473 // Persist layout version if changed
474 if (version != oldVersion) {
475 if (fs_write_atomic_int(version_path, version) == -1) {
476 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno));
477 goto fail;
478 }
Jeff Sharkey5b1ada22012-08-14 18:47:09 -0700479 }
480
481 // Success!
482 res = 0;
483
484fail:
485 free(user_data_dir);
486 free(legacy_data_dir);
487 free(primary_data_dir);
488 return res;
Amith Yamasani0b285492011-04-14 17:35:23 -0700489}
490
Nick Kralevich812b19a2012-08-31 16:08:06 -0700491static void drop_privileges() {
492 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
493 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
494 exit(1);
495 }
496
497 if (setgid(AID_INSTALL) < 0) {
498 ALOGE("setgid() can't drop privileges; exiting.\n");
499 exit(1);
500 }
501
502 if (setuid(AID_INSTALL) < 0) {
503 ALOGE("setuid() can't drop privileges; exiting.\n");
504 exit(1);
505 }
506
507 struct __user_cap_header_struct capheader;
508 struct __user_cap_data_struct capdata[2];
509 memset(&capheader, 0, sizeof(capheader));
510 memset(&capdata, 0, sizeof(capdata));
511 capheader.version = _LINUX_CAPABILITY_VERSION_3;
512 capheader.pid = 0;
513
514 capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted |= CAP_TO_MASK(CAP_DAC_OVERRIDE);
515 capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted |= CAP_TO_MASK(CAP_CHOWN);
516 capdata[CAP_TO_INDEX(CAP_SETUID)].permitted |= CAP_TO_MASK(CAP_SETUID);
517 capdata[CAP_TO_INDEX(CAP_SETGID)].permitted |= CAP_TO_MASK(CAP_SETGID);
518
519 capdata[0].effective = capdata[0].permitted;
520 capdata[1].effective = capdata[1].permitted;
521 capdata[0].inheritable = 0;
522 capdata[1].inheritable = 0;
523
524 if (capset(&capheader, &capdata[0]) < 0) {
525 ALOGE("capset failed: %s\n", strerror(errno));
526 exit(1);
527 }
528}
529
Kenny Root86c95842011-03-31 13:16:12 -0700530int main(const int argc, const char *argv[]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 char buf[BUFFER_MAX];
532 struct sockaddr addr;
533 socklen_t alen;
534 int lsocket, s, count;
535
Nick Kralevich812b19a2012-08-31 16:08:06 -0700536 ALOGI("installd firing up\n");
537
Kenny Root86c95842011-03-31 13:16:12 -0700538 if (initialize_globals() < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000539 ALOGE("Could not initialize globals; exiting.\n");
Kenny Root86c95842011-03-31 13:16:12 -0700540 exit(1);
541 }
542
Amith Yamasani0b285492011-04-14 17:35:23 -0700543 if (initialize_directories() < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000544 ALOGE("Could not create directories; exiting.\n");
Amith Yamasani0b285492011-04-14 17:35:23 -0700545 exit(1);
546 }
547
Nick Kralevich812b19a2012-08-31 16:08:06 -0700548 drop_privileges();
549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 lsocket = android_get_control_socket(SOCKET_PATH);
551 if (lsocket < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000552 ALOGE("Failed to get socket from environment: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 exit(1);
554 }
555 if (listen(lsocket, 5)) {
Steve Block3762c312012-01-06 19:20:56 +0000556 ALOGE("Listen on socket failed: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 exit(1);
558 }
559 fcntl(lsocket, F_SETFD, FD_CLOEXEC);
560
561 for (;;) {
562 alen = sizeof(addr);
563 s = accept(lsocket, &addr, &alen);
564 if (s < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000565 ALOGE("Accept failed: %s\n", strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 continue;
567 }
568 fcntl(s, F_SETFD, FD_CLOEXEC);
569
Steve Block6215d3f2012-01-04 20:05:49 +0000570 ALOGI("new connection\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 for (;;) {
572 unsigned short count;
573 if (readx(s, &count, sizeof(count))) {
Steve Block3762c312012-01-06 19:20:56 +0000574 ALOGE("failed to read size\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 break;
576 }
577 if ((count < 1) || (count >= BUFFER_MAX)) {
Steve Block3762c312012-01-06 19:20:56 +0000578 ALOGE("invalid size %d\n", count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 break;
580 }
581 if (readx(s, buf, count)) {
Steve Block3762c312012-01-06 19:20:56 +0000582 ALOGE("failed to read command\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 break;
584 }
585 buf[count] = 0;
586 if (execute(s, buf)) break;
587 }
Steve Block6215d3f2012-01-04 20:05:49 +0000588 ALOGI("closing connection\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 close(s);
590 }
591
592 return 0;
593}