installd.cpp revision d792118c493806eeb24a8203f508e6e18fe93bd7
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 <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, is_public, pkgname, instruction_set, 51 * dexopt_needed, vm_safe_mode, debuggable, oat_dir */ 52 return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]), 53 atoi(arg[6]), atoi(arg[7]), arg[8]); 54} 55 56static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused) 57{ 58 return mark_boot_complete(arg[0] /* instruction set */); 59} 60 61static int do_move_dex(char **arg, char reply[REPLY_MAX] __unused) 62{ 63 return move_dex(arg[0], arg[1], arg[2]); /* src, dst, instruction_set */ 64} 65 66static int do_rm_dex(char **arg, char reply[REPLY_MAX] __unused) 67{ 68 return rm_dex(arg[0], arg[1]); /* pkgname, instruction_set */ 69} 70 71static int do_remove(char **arg, char reply[REPLY_MAX] __unused) 72{ 73 return uninstall(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 74} 75 76static int do_rename(char **arg, char reply[REPLY_MAX] __unused) 77{ 78 return renamepkg(arg[0], arg[1]); /* oldpkgname, newpkgname */ 79} 80 81static int do_fixuid(char **arg, char reply[REPLY_MAX] __unused) 82{ 83 return fix_uid(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3])); /* uuid, pkgname, uid, gid */ 84} 85 86static int do_free_cache(char **arg, char reply[REPLY_MAX] __unused) /* TODO int:free_size */ 87{ 88 return free_cache(parse_null(arg[0]), (int64_t)atoll(arg[1])); /* uuid, free_size */ 89} 90 91static int do_rm_cache(char **arg, char reply[REPLY_MAX] __unused) 92{ 93 return delete_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 94} 95 96static int do_rm_code_cache(char **arg, char reply[REPLY_MAX] __unused) 97{ 98 return delete_code_cache(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 99} 100 101static int do_get_size(char **arg, char reply[REPLY_MAX]) 102{ 103 int64_t codesize = 0; 104 int64_t datasize = 0; 105 int64_t cachesize = 0; 106 int64_t asecsize = 0; 107 int res = 0; 108 109 /* uuid, pkgdir, userid, apkpath */ 110 res = get_size(parse_null(arg[0]), arg[1], atoi(arg[2]), arg[3], arg[4], arg[5], arg[6], 111 arg[7], &codesize, &datasize, &cachesize, &asecsize); 112 113 /* 114 * Each int64_t can take up 22 characters printed out. Make sure it 115 * doesn't go over REPLY_MAX in the future. 116 */ 117 snprintf(reply, REPLY_MAX, "%" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, 118 codesize, datasize, cachesize, asecsize); 119 return res; 120} 121 122static int do_rm_user_data(char **arg, char reply[REPLY_MAX] __unused) 123{ 124 return delete_user_data(parse_null(arg[0]), arg[1], atoi(arg[2])); /* uuid, pkgname, userid */ 125} 126 127static int do_mv_complete_app(char **arg, char reply[REPLY_MAX] __unused) 128{ 129 // from_uuid, to_uuid, package_name, data_app_name, appid, seinfo 130 return move_complete_app(parse_null(arg[0]), parse_null(arg[1]), arg[2], arg[3], atoi(arg[4]), arg[5]); 131} 132 133static int do_mk_user_data(char **arg, char reply[REPLY_MAX] __unused) 134{ 135 return make_user_data(parse_null(arg[0]), arg[1], atoi(arg[2]), atoi(arg[3]), arg[4]); 136 /* uuid, pkgname, uid, userid, seinfo */ 137} 138 139static int do_mk_user_config(char **arg, char reply[REPLY_MAX] __unused) 140{ 141 return make_user_config(atoi(arg[0])); /* userid */ 142} 143 144static int do_rm_user(char **arg, char reply[REPLY_MAX] __unused) 145{ 146 return delete_user(parse_null(arg[0]), atoi(arg[1])); /* uuid, userid */ 147} 148 149static int do_movefiles(char **arg __unused, char reply[REPLY_MAX] __unused) 150{ 151 return movefiles(); 152} 153 154static int do_linklib(char **arg, char reply[REPLY_MAX] __unused) 155{ 156 return linklib(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3])); 157} 158 159static int do_idmap(char **arg, char reply[REPLY_MAX] __unused) 160{ 161 return idmap(arg[0], arg[1], atoi(arg[2])); 162} 163 164static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((unused))) 165{ 166 return restorecon_data(parse_null(arg[0]), arg[1], arg[2], atoi(arg[3])); 167 /* uuid, pkgName, seinfo, uid*/ 168} 169 170static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused) 171{ 172 /* oat_dir, instruction_set */ 173 return create_oat_dir(arg[0], arg[1]); 174} 175 176static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused) 177{ 178 /* oat_dir */ 179 return rm_package_dir(arg[0]); 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", 9, 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 { "rename", 2, do_rename }, 197 { "fixuid", 4, do_fixuid }, 198 { "freecache", 2, do_free_cache }, 199 { "rmcache", 3, do_rm_cache }, 200 { "rmcodecache", 3, do_rm_code_cache }, 201 { "getsize", 8, do_get_size }, 202 { "rmuserdata", 3, do_rm_user_data }, 203 { "mvcompleteapp", 6, do_mv_complete_app }, 204 { "movefiles", 0, do_movefiles }, 205 { "linklib", 4, do_linklib }, 206 { "mkuserdata", 5, do_mk_user_data }, 207 { "mkuserconfig", 1, do_mk_user_config }, 208 { "rmuser", 2, do_rm_user }, 209 { "idmap", 3, do_idmap }, 210 { "restorecondata", 4, do_restorecon_data }, 211 { "createoatdir", 2, do_create_oat_dir }, 212 { "rmpackagedir", 1, do_rm_package_dir}, 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 app native library directory. 350 if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) { 351 return -1; 352 } 353 354 // Get the sd-card ASEC mount point. 355 if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) { 356 return -1; 357 } 358 359 // Get the android media directory. 360 if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) { 361 return -1; 362 } 363 364 // Get the android external app directory. 365 if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) { 366 return -1; 367 } 368 369 // Take note of the system and vendor directories. 370 android_system_dirs.count = 4; 371 372 android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t)); 373 if (android_system_dirs.dirs == NULL) { 374 ALOGE("Couldn't allocate array for dirs; aborting\n"); 375 return -1; 376 } 377 378 dir_rec_t android_root_dir; 379 if (get_path_from_env(&android_root_dir, "ANDROID_ROOT") < 0) { 380 ALOGE("Missing ANDROID_ROOT; aborting\n"); 381 return -1; 382 } 383 384 android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR); 385 android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path); 386 387 android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR); 388 android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); 389 390 android_system_dirs.dirs[2].path = strdup("/vendor/app/"); 391 android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path); 392 393 android_system_dirs.dirs[3].path = strdup("/oem/app/"); 394 android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path); 395 396 return 0; 397} 398 399int initialize_directories() { 400 int res = -1; 401 402 // Read current filesystem layout version to handle upgrade paths 403 char version_path[PATH_MAX]; 404 snprintf(version_path, PATH_MAX, "%s.layout_version", android_data_dir.path); 405 406 int oldVersion; 407 if (fs_read_atomic_int(version_path, &oldVersion) == -1) { 408 oldVersion = 0; 409 } 410 int version = oldVersion; 411 412 // /data/user 413 char *user_data_dir = build_string2(android_data_dir.path, SECONDARY_USER_PREFIX); 414 // /data/data 415 char *legacy_data_dir = build_string2(android_data_dir.path, PRIMARY_USER_PREFIX); 416 // /data/user/0 417 char *primary_data_dir = build_string3(android_data_dir.path, SECONDARY_USER_PREFIX, "0"); 418 if (!user_data_dir || !legacy_data_dir || !primary_data_dir) { 419 goto fail; 420 } 421 422 // Make the /data/user directory if necessary 423 if (access(user_data_dir, R_OK) < 0) { 424 if (mkdir(user_data_dir, 0711) < 0) { 425 goto fail; 426 } 427 if (chown(user_data_dir, AID_SYSTEM, AID_SYSTEM) < 0) { 428 goto fail; 429 } 430 if (chmod(user_data_dir, 0711) < 0) { 431 goto fail; 432 } 433 } 434 // Make the /data/user/0 symlink to /data/data if necessary 435 if (access(primary_data_dir, R_OK) < 0) { 436 if (symlink(legacy_data_dir, primary_data_dir)) { 437 goto fail; 438 } 439 } 440 441 if (version == 0) { 442 // Introducing multi-user, so migrate /data/media contents into /data/media/0 443 ALOGD("Upgrading /data/media for multi-user"); 444 445 // Ensure /data/media 446 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 447 goto fail; 448 } 449 450 // /data/media.tmp 451 char media_tmp_dir[PATH_MAX]; 452 snprintf(media_tmp_dir, PATH_MAX, "%smedia.tmp", android_data_dir.path); 453 454 // Only copy when upgrade not already in progress 455 if (access(media_tmp_dir, F_OK) == -1) { 456 if (rename(android_media_dir.path, media_tmp_dir) == -1) { 457 ALOGE("Failed to move legacy media path: %s", strerror(errno)); 458 goto fail; 459 } 460 } 461 462 // Create /data/media again 463 if (fs_prepare_dir(android_media_dir.path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 464 goto fail; 465 } 466 467 if (selinux_android_restorecon(android_media_dir.path, 0)) { 468 goto fail; 469 } 470 471 // /data/media/0 472 char owner_media_dir[PATH_MAX]; 473 snprintf(owner_media_dir, PATH_MAX, "%s0", android_media_dir.path); 474 475 // Move any owner data into place 476 if (access(media_tmp_dir, F_OK) == 0) { 477 if (rename(media_tmp_dir, owner_media_dir) == -1) { 478 ALOGE("Failed to move owner media path: %s", strerror(errno)); 479 goto fail; 480 } 481 } 482 483 // Ensure media directories for any existing users 484 DIR *dir; 485 struct dirent *dirent; 486 char user_media_dir[PATH_MAX]; 487 488 dir = opendir(user_data_dir); 489 if (dir != NULL) { 490 while ((dirent = readdir(dir))) { 491 if (dirent->d_type == DT_DIR) { 492 const char *name = dirent->d_name; 493 494 // skip "." and ".." 495 if (name[0] == '.') { 496 if (name[1] == 0) continue; 497 if ((name[1] == '.') && (name[2] == 0)) continue; 498 } 499 500 // /data/media/<user_id> 501 snprintf(user_media_dir, PATH_MAX, "%s%s", android_media_dir.path, name); 502 if (fs_prepare_dir(user_media_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 503 goto fail; 504 } 505 } 506 } 507 closedir(dir); 508 } 509 510 version = 1; 511 } 512 513 // /data/media/obb 514 char media_obb_dir[PATH_MAX]; 515 snprintf(media_obb_dir, PATH_MAX, "%sobb", android_media_dir.path); 516 517 if (version == 1) { 518 // Introducing /data/media/obb for sharing OBB across users; migrate 519 // any existing OBB files from owner. 520 ALOGD("Upgrading to shared /data/media/obb"); 521 522 // /data/media/0/Android/obb 523 char owner_obb_path[PATH_MAX]; 524 snprintf(owner_obb_path, PATH_MAX, "%s0/Android/obb", android_media_dir.path); 525 526 // Only move if target doesn't already exist 527 if (access(media_obb_dir, F_OK) != 0 && access(owner_obb_path, F_OK) == 0) { 528 if (rename(owner_obb_path, media_obb_dir) == -1) { 529 ALOGE("Failed to move OBB from owner: %s", strerror(errno)); 530 goto fail; 531 } 532 } 533 534 version = 2; 535 } 536 537 if (ensure_media_user_dirs(nullptr, 0) == -1) { 538 ALOGE("Failed to setup media for user 0"); 539 goto fail; 540 } 541 if (fs_prepare_dir(media_obb_dir, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) { 542 goto fail; 543 } 544 545 if (ensure_config_user_dirs(0) == -1) { 546 ALOGE("Failed to setup misc for user 0"); 547 goto fail; 548 } 549 550 if (version == 2) { 551 ALOGD("Upgrading to /data/misc/user directories"); 552 553 char misc_dir[PATH_MAX]; 554 snprintf(misc_dir, PATH_MAX, "%smisc", android_data_dir.path); 555 556 char keychain_added_dir[PATH_MAX]; 557 snprintf(keychain_added_dir, PATH_MAX, "%s/keychain/cacerts-added", misc_dir); 558 559 char keychain_removed_dir[PATH_MAX]; 560 snprintf(keychain_removed_dir, PATH_MAX, "%s/keychain/cacerts-removed", misc_dir); 561 562 DIR *dir; 563 struct dirent *dirent; 564 dir = opendir(user_data_dir); 565 if (dir != NULL) { 566 while ((dirent = readdir(dir))) { 567 const char *name = dirent->d_name; 568 569 // skip "." and ".." 570 if (name[0] == '.') { 571 if (name[1] == 0) continue; 572 if ((name[1] == '.') && (name[2] == 0)) continue; 573 } 574 575 uint32_t user_id = atoi(name); 576 577 // /data/misc/user/<user_id> 578 if (ensure_config_user_dirs(user_id) == -1) { 579 goto fail; 580 } 581 582 char misc_added_dir[PATH_MAX]; 583 snprintf(misc_added_dir, PATH_MAX, "%s/user/%s/cacerts-added", misc_dir, name); 584 585 char misc_removed_dir[PATH_MAX]; 586 snprintf(misc_removed_dir, PATH_MAX, "%s/user/%s/cacerts-removed", misc_dir, name); 587 588 uid_t uid = multiuser_get_uid(user_id, AID_SYSTEM); 589 gid_t gid = uid; 590 if (access(keychain_added_dir, F_OK) == 0) { 591 if (copy_dir_files(keychain_added_dir, misc_added_dir, uid, gid) != 0) { 592 ALOGE("Some files failed to copy"); 593 } 594 } 595 if (access(keychain_removed_dir, F_OK) == 0) { 596 if (copy_dir_files(keychain_removed_dir, misc_removed_dir, uid, gid) != 0) { 597 ALOGE("Some files failed to copy"); 598 } 599 } 600 } 601 closedir(dir); 602 603 if (access(keychain_added_dir, F_OK) == 0) { 604 delete_dir_contents(keychain_added_dir, 1, 0); 605 } 606 if (access(keychain_removed_dir, F_OK) == 0) { 607 delete_dir_contents(keychain_removed_dir, 1, 0); 608 } 609 } 610 611 version = 3; 612 } 613 614 // Persist layout version if changed 615 if (version != oldVersion) { 616 if (fs_write_atomic_int(version_path, version) == -1) { 617 ALOGE("Failed to save version to %s: %s", version_path, strerror(errno)); 618 goto fail; 619 } 620 } 621 622 // Success! 623 res = 0; 624 625fail: 626 free(user_data_dir); 627 free(legacy_data_dir); 628 free(primary_data_dir); 629 return res; 630} 631 632static int log_callback(int type, const char *fmt, ...) { 633 va_list ap; 634 int priority; 635 636 switch (type) { 637 case SELINUX_WARNING: 638 priority = ANDROID_LOG_WARN; 639 break; 640 case SELINUX_INFO: 641 priority = ANDROID_LOG_INFO; 642 break; 643 default: 644 priority = ANDROID_LOG_ERROR; 645 break; 646 } 647 va_start(ap, fmt); 648 LOG_PRI_VA(priority, "SELinux", fmt, ap); 649 va_end(ap); 650 return 0; 651} 652 653int main(const int argc __unused, char *argv[]) { 654 char buf[BUFFER_MAX]; 655 struct sockaddr addr; 656 socklen_t alen; 657 int lsocket, s; 658 int selinux_enabled = (is_selinux_enabled() > 0); 659 660 setenv("ANDROID_LOG_TAGS", "*:v", 1); 661 android::base::InitLogging(argv); 662 663 ALOGI("installd firing up\n"); 664 665 union selinux_callback cb; 666 cb.func_log = log_callback; 667 selinux_set_callback(SELINUX_CB_LOG, cb); 668 669 if (initialize_globals() < 0) { 670 ALOGE("Could not initialize globals; exiting.\n"); 671 exit(1); 672 } 673 674 if (initialize_directories() < 0) { 675 ALOGE("Could not create directories; exiting.\n"); 676 exit(1); 677 } 678 679 if (selinux_enabled && selinux_status_open(true) < 0) { 680 ALOGE("Could not open selinux status; exiting.\n"); 681 exit(1); 682 } 683 684 lsocket = android_get_control_socket(SOCKET_PATH); 685 if (lsocket < 0) { 686 ALOGE("Failed to get socket from environment: %s\n", strerror(errno)); 687 exit(1); 688 } 689 if (listen(lsocket, 5)) { 690 ALOGE("Listen on socket failed: %s\n", strerror(errno)); 691 exit(1); 692 } 693 fcntl(lsocket, F_SETFD, FD_CLOEXEC); 694 695 for (;;) { 696 alen = sizeof(addr); 697 s = accept(lsocket, &addr, &alen); 698 if (s < 0) { 699 ALOGE("Accept failed: %s\n", strerror(errno)); 700 continue; 701 } 702 fcntl(s, F_SETFD, FD_CLOEXEC); 703 704 ALOGI("new connection\n"); 705 for (;;) { 706 unsigned short count; 707 if (readx(s, &count, sizeof(count))) { 708 ALOGE("failed to read size\n"); 709 break; 710 } 711 if ((count < 1) || (count >= BUFFER_MAX)) { 712 ALOGE("invalid size %d\n", count); 713 break; 714 } 715 if (readx(s, buf, count)) { 716 ALOGE("failed to read command\n"); 717 break; 718 } 719 buf[count] = 0; 720 if (selinux_enabled && selinux_status_updated() > 0) { 721 selinux_android_seapp_context_reload(); 722 } 723 if (execute(s, buf)) break; 724 } 725 ALOGI("closing connection\n"); 726 close(s); 727 } 728 729 return 0; 730} 731