installd.cpp revision 32717f851ec39e44b43dcd2053b091daff5e1753
1/* 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#include <android-base/logging.h> 20 21#include <sys/capability.h> 22#include <sys/prctl.h> 23#include <selinux/android.h> 24#include <selinux/avc.h> 25 26#define BUFFER_MAX 1024 /* input buffer for commands */ 27#define TOKEN_MAX 16 /* max number of arguments in buffer */ 28#define REPLY_MAX 256 /* largest reply allowed */ 29 30static char* parse_null(char* arg) { 31 if (strcmp(arg, "!") == 0) { 32 return nullptr; 33 } else { 34 return arg; 35 } 36} 37 38static int do_ping(char **arg __unused, char reply[REPLY_MAX] __unused) 39{ 40 return 0; 41} 42 43static int do_install(char **arg, char reply[REPLY_MAX] __unused) 44{ 45 return install(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]); /* uuid, pkgname, uid, gid, seinfo */ 46} 47 48static int do_dexopt(char **arg, char reply[REPLY_MAX] __unused) 49{ 50 /* apk_path, uid, pkgname, instruction_set, dexopt_needed, oat_dir, dexopt_flags */ 51 return dexopt(arg[0], atoi(arg[1]), arg[2], arg[3], atoi(arg[4]), 52 arg[5], atoi(arg[6])); 53} 54 55static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused) 56{ 57 return mark_boot_complete(arg[0] /* instruction set */); 58} 59 60static int do_move_dex(char **arg, char reply[REPLY_MAX] __unused) 61{ 62 return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */ 63} 64 65static int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused) 66{ 67 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */ 68} 69 70static int do_remove(char **arg, char reply[REPLY_MAX] __unused) 71{ 72 return uninstall(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 73} 74 75static int do_fixuid(char **arg, char reply[REPLY_MAX] __unused) 76{ 77 return fix_uid(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3])); /* uuid, pkgname, uid, gid */ 78} 79 80static int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */ 81{ 82 return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */ 83} 84 85static int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused) 86{ 87 return delete_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 88} 89 90static int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused) 91{ 92 return delete_code_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 93} 94 95static int do_get_size(char **arg, char reply[REPLY_MAX]) 96{ 97 int64_t codesize = 0; 98 int64_t datasize = 0; 99 int64_t cachesize = 0; 100 int64_t asecsize = 0; 101 int res = 0; 102 103 /* uuid, pkgdir, userid, apkpath */ 104 res = get_size(parse_null(arg[0]), arg[1], atoi(arg[2]), arg[3], arg[4], arg[5], arg[6], 105 arg[7], &codesize, &datasize, &cachesize, &asecsize); 106 107 /* 108 * Each int64_t can take up 22 characters printed out. Make sure it 109 * doesn't go over REPLY_MAX in the future. 110 */ 111 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, 112 codesize, datasize, cachesize, asecsize); 113 return res; 114} 115 116static int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused) 117{ 118 return delete_user_data(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 119} 120 121static int do_cp_complete_app(char **arg, char reply[REPLY_MAX] __unused) 122{ 123 // from_uuid, to_uuid, package_name, data_app_name, appid, seinfo 124 return copy_complete_app(parse_null(arg[0]), parse_null(arg[1]), arg[2], arg[3], atoi(arg[4]), arg[5]); 125} 126 127static int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused) 128{ 129 return make_user_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]); 130 /* uuid, pkgname, uid, userid, seinfo */ 131} 132 133static int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused) 134{ 135 return make_user_config(atoi(arg[0])); /* userid */ 136} 137 138static int do_rm_user(char **arg, char reply[REPLY_MAX] __unused) 139{ 140 return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */ 141} 142 143static int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused) 144{ 145 return movefiles(); 146} 147 148static int do_linklib(char **arg, char reply[REPLY_MAX] __unused) 149{ 150 return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3])); 151} 152 153static int do_idmap(char **arg, char reply[REPLY_MAX] __unused) 154{ 155 return idmap(arg[0], arg[1], atoi(arg[2])); 156} 157 158static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused))) 159{ 160 return restorecon_data(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3])); 161 /* uuid, pkgName, seinfo, uid*/ 162} 163 164static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused) 165{ 166 /* oat_dir, instruction_set */ 167 return create_oat_dir(arg[0], arg[1]); 168} 169 170static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused) 171{ 172 /* oat_dir */ 173 return rm_package_dir(arg[0]); 174} 175 176static int do_link_file(char **arg, char reply[REPLY_MAX] __unused) 177{ 178 /* relative_path, from_base, to_base */ 179 return link_file(arg[0], arg[1], arg[2]); 180} 181 182struct cmdinfo { 183 const char *name; 184 unsigned numargs; 185 int (*func)(char **arg, char reply[REPLY_MAX]); 186}; 187 188struct cmdinfo cmds[] = { 189 { "ping", 0, do_ping }, 190 { "install", 5, do_install }, 191 { "dexopt", 7, do_dexopt }, 192 { "markbootcomplete", 1, do_mark_boot_complete }, 193 { "movedex", 3, do_move_dex }, 194 { "rmdex", 2, do_rm_dex }, 195 { "remove", 3, do_remove }, 196 { "fixuid", 4, do_fixuid }, 197 { "freecache", 2, do_free_cache }, 198 { "rmcache", 3, do_rm_cache }, 199 { "rmcodecache", 3, do_rm_code_cache }, 200 { "getsize", 8, do_get_size }, 201 { "rmuserdata", 3, do_rm_user_data }, 202 { "cpcompleteapp", 6, do_cp_complete_app }, 203 { "movefiles", 0, do_movefiles }, 204 { "linklib", 4, do_linklib }, 205 { "mkuserdata", 5, do_mk_user_data }, 206 { "mkuserconfig", 1, do_mk_user_config }, 207 { "rmuser", 2, do_rm_user }, 208 { "idmap", 3, do_idmap }, 209 { "restorecondata", 4, do_restorecon_data }, 210 { "createoatdir", 2, do_create_oat_dir }, 211 { "rmpackagedir", 1, do_rm_package_dir }, 212 { "linkfile", 3, do_link_file } 213}; 214 215static int readx(int s, void *_buf, int count) 216{ 217 char *buf = (char *) _buf; 218 int n = 0, r; 219 if (count < 0) return -1; 220 while (n < count) { 221 r = read(s, buf + n, count - n); 222 if (r < 0) { 223 if (errno == EINTR) continue; 224 ALOGE("read error: %s\n", strerror(errno)); 225 return -1; 226 } 227 if (r == 0) { 228 ALOGE("eof\n"); 229 return -1; /* EOF */ 230 } 231 n += r; 232 } 233 return 0; 234} 235 236static int writex(int s, const void *_buf, int count) 237{ 238 const char *buf = (const char *) _buf; 239 int n = 0, r; 240 if (count < 0) return -1; 241 while (n < count) { 242 r = write(s, buf + n, count - n); 243 if (r < 0) { 244 if (errno == EINTR) continue; 245 ALOGE("write error: %s\n", strerror(errno)); 246 return -1; 247 } 248 n += r; 249 } 250 return 0; 251} 252 253 254/* Tokenize the command buffer, locate a matching command, 255 * ensure that the required number of arguments are provided, 256 * call the function(), return the result. 257 */ 258static int execute(int s, char cmd[BUFFER_MAX]) 259{ 260 char reply[REPLY_MAX]; 261 char *arg[TOKEN_MAX+1]; 262 unsigned i; 263 unsigned n = 0; 264 unsigned short count; 265 int ret = -1; 266 267 // ALOGI("execute('%s')\n", cmd); 268 269 /* default reply is "" */ 270 reply[0] = 0; 271 272 /* n is number of args (not counting arg[0]) */ 273 arg[0] = cmd; 274 while (*cmd) { 275 if (isspace(*cmd)) { 276 *cmd++ = 0; 277 n++; 278 arg[n] = cmd; 279 if (n == TOKEN_MAX) { 280 ALOGE("too many arguments\n"); 281 goto done; 282 } 283 } 284 if (*cmd) { 285 cmd++; 286 } 287 } 288 289 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { 290 if (!strcmp(cmds[i].name,arg[0])) { 291 if (n != cmds[i].numargs) { 292 ALOGE("%s requires %d arguments (%d given)\n", 293 cmds[i].name, cmds[i].numargs, n); 294 } else { 295 ret = cmds[i].func(arg + 1, reply); 296 } 297 goto done; 298 } 299 } 300 ALOGE("unsupported command '%s'\n", arg[0]); 301 302done: 303 if (reply[0]) { 304 n = snprintf(cmd, BUFFER_MAX, "%d %s", ret, reply); 305 } else { 306 n = snprintf(cmd, BUFFER_MAX, "%d", ret); 307 } 308 if (n > BUFFER_MAX) n = BUFFER_MAX; 309 count = n; 310 311 // ALOGI("reply: '%s'\n", cmd); 312 if (writex(s, &count, sizeof(count))) return -1; 313 if (writex(s, cmd, count)) return -1; 314 return 0; 315} 316 317/** 318 * Initialize all the global variables that are used elsewhere. Returns 0 upon 319 * success and -1 on error. 320 */ 321void free_globals() { 322 size_t i; 323 324 for (i = 0; i < android_system_dirs.count; i++) { 325 if (android_system_dirs.dirs[i].path != NULL) { 326 free(android_system_dirs.dirs[i].path); 327 } 328 } 329 330 free(android_system_dirs.dirs); 331} 332 333int initialize_globals() { 334 // Get the android data directory. 335 if (get_path_from_env(&android_data_dir, "ANDROID_DATA") < 0) { 336 return -1; 337 } 338 339 // Get the android app directory. 340 if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) { 341 return -1; 342 } 343 344 // Get the android protected app directory. 345 if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) { 346 return -1; 347 } 348 349 // Get the android ephemeral app directory. 350 if (copy_and_append(&android_app_ephemeral_dir, &android_data_dir, EPHEMERAL_APP_SUBDIR) < 0) { 351 return -1; 352 } 353 354 // Get the android app native library directory. 355 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) { 356 return -1; 357 } 358 359 // Get the sd-card ASEC mount point. 360 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) { 361 return -1; 362 } 363 364 // Get the android media directory. 365 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) { 366 return -1; 367 } 368 369 // Get the android external app directory. 370 if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) { 371 return -1; 372 } 373 374 // Take note of the system and vendor directories. 375 android_system_dirs.count = 5; 376 377 android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t)); 378 if (android_system_dirs.dirs == NULL) { 379 ALOGE("Couldn't allocate array for dirs; aborting\n"); 380 return -1; 381 } 382 383 dir_rec_t android_root_dir; 384 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) { 385 ALOGE("Missing ANDROID_ROOT; aborting\n"); 386 return -1; 387 } 388 389 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR); 390 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path); 391 392 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR); 393 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); 394 395 android_system_dirs.dirs[2].path = strdup("/vendor/app/"); 396 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path); 397 398 android_system_dirs.dirs[3].path = strdup("/oem/app/"); 399 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path); 400 401 android_system_dirs.dirs[4].path = build_string2(android_root_dir.path, EPHEMERAL_APP_SUBDIR); 402 android_system_dirs.dirs[4].len = strlen(android_system_dirs.dirs[4].path); 403 404 return 0; 405} 406 407int initialize_directories() { 408 int res = -1; 409 410 // Read current filesystem layout version to handle upgrade paths 411 char version_path[PATH_MAX]; 412 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path); 413 414 int oldVersion; 415 if (fs_read_atomic_int(version_path, &oldVersion) == -1) { 416 oldVersion = 0; 417 } 418 int version = oldVersion; 419 420 // /data/user 421 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); 422 // /data/data 423 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); 424 // /data/user/0 425 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0"); 426 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) { 427 goto fail; 428 } 429 430 // Make the /data/user directory if necessary 431 if (access(user_data_dir, R_OK) < 0) { 432 if (mkdir(user_data_dir, 0711) < 0) { 433 goto fail; 434 } 435 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { 436 goto fail; 437 } 438 if (chmod(user_data_dir, 0711) < 0) { 439 goto fail; 440 } 441 } 442 // Make the /data/user/0 symlink to /data/data if necessary 443 if (access(primary_data_dir, R_OK) < 0) { 444 if (symlink(legacy_data_dir, primary_data_dir)) { 445 goto fail; 446 } 447 } 448 449 if (version == 0) { 450 // Introducing multi-user, so migrate /data/media contents into /data/media/0 451 ALOGD("Upgrading /data/media for multi-user"); 452 453 // Ensure /data/media 454 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 455 goto fail; 456 } 457 458 // /data/media.tmp 459 char media_tmp_dir[PATH_MAX]; 460 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path); 461 462 // Only copy when upgrade not already in progress 463 if (access(media_tmp_dir, F_OK) == -1) { 464 if (rename(android_media_dir.path, media_tmp_dir) == -1) { 465 ALOGE("Failed to move legacy media path: %s", strerror(errno)); 466 goto fail; 467 } 468 } 469 470 // Create /data/media again 471 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 472 goto fail; 473 } 474 475 if (selinux_android_restorecon(android_media_dir.path, 0)) { 476 goto fail; 477 } 478 479 // /data/media/0 480 char owner_media_dir[PATH_MAX]; 481 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path); 482 483 // Move any owner data into place 484 if (access(media_tmp_dir, F_OK) == 0) { 485 if (rename(media_tmp_dir, owner_media_dir) == -1) { 486 ALOGE("Failed to move owner media path: %s", strerror(errno)); 487 goto fail; 488 } 489 } 490 491 // Ensure media directories for any existing users 492 DIR *dir; 493 struct dirent *dirent; 494 char user_media_dir[PATH_MAX]; 495 496 dir = opendir(user_data_dir); 497 if (dir != NULL) { 498 while ((dirent = readdir(dir))) { 499 if (dirent->d_type == DT_DIR) { 500 const char *name = dirent->d_name; 501 502 // skip "." and ".." 503 if (name[0] == '.') { 504 if (name[1] == 0) continue; 505 if ((name[1] == '.') && (name[2] == 0)) continue; 506 } 507 508 // /data/media/<user_id> 509 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name); 510 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 511 goto fail; 512 } 513 } 514 } 515 closedir(dir); 516 } 517 518 version = 1; 519 } 520 521 // /data/media/obb 522 char media_obb_dir[PATH_MAX]; 523 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path); 524 525 if (version == 1) { 526 // Introducing /data/media/obb for sharing OBB across users; migrate 527 // any existing OBB files from owner. 528 ALOGD("Upgrading to shared /data/media/obb"); 529 530 // /data/media/0/Android/obb 531 char owner_obb_path[PATH_MAX]; 532 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path); 533 534 // Only move if target doesn't already exist 535 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) { 536 if (rename(owner_obb_path, media_obb_dir) == -1) { 537 ALOGE("Failed to move OBB from owner: %s", strerror(errno)); 538 goto fail; 539 } 540 } 541 542 version = 2; 543 } 544 545 if (ensure_media_user_dirs(nullptr, 0) == -1) { 546 ALOGE("Failed to setup media for user 0"); 547 goto fail; 548 } 549 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 550 goto fail; 551 } 552 553 if (ensure_config_user_dirs(0) == -1) { 554 ALOGE("Failed to setup misc for user 0"); 555 goto fail; 556 } 557 558 if (version == 2) { 559 ALOGD("Upgrading to /data/misc/user directories"); 560 561 char misc_dir[PATH_MAX]; 562 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path); 563 564 char keychain_added_dir[PATH_MAX]; 565 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir); 566 567 char keychain_removed_dir[PATH_MAX]; 568 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir); 569 570 DIR *dir; 571 struct dirent *dirent; 572 dir = opendir(user_data_dir); 573 if (dir != NULL) { 574 while ((dirent = readdir(dir))) { 575 const char *name = dirent->d_name; 576 577 // skip "." and ".." 578 if (name[0] == '.') { 579 if (name[1] == 0) continue; 580 if ((name[1] == '.') && (name[2] == 0)) continue; 581 } 582 583 uint32_t user_id = atoi(name); 584 585 // /data/misc/user/<user_id> 586 if (ensure_config_user_dirs(user_id) == -1) { 587 goto fail; 588 } 589 590 char misc_added_dir[PATH_MAX]; 591 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name); 592 593 char misc_removed_dir[PATH_MAX]; 594 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name); 595 596 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM); 597 gid_t gid = uid; 598 if (access(keychain_added_dir, F_OK) == 0) { 599 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) { 600 ALOGE("Some files failed to copy"); 601 } 602 } 603 if (access(keychain_removed_dir, F_OK) == 0) { 604 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) { 605 ALOGE("Some files failed to copy"); 606 } 607 } 608 } 609 closedir(dir); 610 611 if (access(keychain_added_dir, F_OK) == 0) { 612 delete_dir_contents(keychain_added_dir, 1, 0); 613 } 614 if (access(keychain_removed_dir, F_OK) == 0) { 615 delete_dir_contents(keychain_removed_dir, 1, 0); 616 } 617 } 618 619 version = 3; 620 } 621 622 // Persist layout version if changed 623 if (version != oldVersion) { 624 if (fs_write_atomic_int(version_path, version) == -1) { 625 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno)); 626 goto fail; 627 } 628 } 629 630 // Success! 631 res = 0; 632 633fail: 634 free(user_data_dir); 635 free(legacy_data_dir); 636 free(primary_data_dir); 637 return res; 638} 639 640static int log_callback(int type, const char *fmt, ...) { 641 va_list ap; 642 int priority; 643 644 switch (type) { 645 case SELINUX_WARNING: 646 priority = ANDROID_LOG_WARN; 647 break; 648 case SELINUX_INFO: 649 priority = ANDROID_LOG_INFO; 650 break; 651 default: 652 priority = ANDROID_LOG_ERROR; 653 break; 654 } 655 va_start(ap, fmt); 656 LOG_PRI_VA(priority, "SELinux", fmt, ap); 657 va_end(ap); 658 return 0; 659} 660 661int main(const int argc __unused, char *argv[]) { 662 char buf[BUFFER_MAX]; 663 struct sockaddr addr; 664 socklen_t alen; 665 int lsocket, s; 666 int selinux_enabled = (is_selinux_enabled() > 0); 667 668 setenv("ANDROID_LOG_TAGS", "*:v", 1); 669 android::base::InitLogging(argv); 670 671 ALOGI("installd firing up\n"); 672 673 union selinux_callback cb; 674 cb.func_log = log_callback; 675 selinux_set_callback(SELINUX_CB_LOG, cb); 676 677 if (initialize_globals() < 0) { 678 ALOGE("Could not initialize globals; exiting.\n"); 679 exit(1); 680 } 681 682 if (initialize_directories() < 0) { 683 ALOGE("Could not create directories; exiting.\n"); 684 exit(1); 685 } 686 687 if (selinux_enabled && selinux_status_open(true) < 0) { 688 ALOGE("Could not open selinux status; exiting.\n"); 689 exit(1); 690 } 691 692 lsocket = android_get_control_socket(SOCKET_PATH); 693 if (lsocket < 0) { 694 ALOGE("Failed to get socket from environment: %s\n", strerror(errno)); 695 exit(1); 696 } 697 if (listen(lsocket, 5)) { 698 ALOGE("Listen on socket failed: %s\n", strerror(errno)); 699 exit(1); 700 } 701 fcntl(lsocket, F_SETFD, FD_CLOEXEC); 702 703 for (;;) { 704 alen = sizeof(addr); 705 s = accept(lsocket, &addr, &alen); 706 if (s < 0) { 707 ALOGE("Accept failed: %s\n", strerror(errno)); 708 continue; 709 } 710 fcntl(s, F_SETFD, FD_CLOEXEC); 711 712 ALOGI("new connection\n"); 713 for (;;) { 714 unsigned short count; 715 if (readx(s, &count, sizeof(count))) { 716 ALOGE("failed to read size\n"); 717 break; 718 } 719 if ((count < 1) || (count >= BUFFER_MAX)) { 720 ALOGE("invalid size %d\n", count); 721 break; 722 } 723 if (readx(s, buf, count)) { 724 ALOGE("failed to read command\n"); 725 break; 726 } 727 buf[count] = 0; 728 if (selinux_enabled && selinux_status_updated() > 0) { 729 selinux_android_seapp_context_reload(); 730 } 731 if (execute(s, buf)) break; 732 } 733 ALOGI("closing connection\n"); 734 close(s); 735 } 736 737 return 0; 738} 739