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