init.c revision 469b7b2da84a62652342fd2f6006ca452cb1447d
1/* 2 * Copyright (C) 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 <stdio.h> 18#include <stdlib.h> 19#include <string.h> 20#include <unistd.h> 21#include <fcntl.h> 22#include <ctype.h> 23#include <signal.h> 24#include <sys/wait.h> 25#include <sys/mount.h> 26#include <sys/stat.h> 27#include <sys/poll.h> 28#include <errno.h> 29#include <stdarg.h> 30#include <mtd/mtd-user.h> 31#include <sys/types.h> 32#include <sys/socket.h> 33#include <sys/un.h> 34 35#ifdef HAVE_SELINUX 36#include <sys/mman.h> 37#include <selinux/selinux.h> 38#include <selinux/label.h> 39#endif 40 41#include <libgen.h> 42 43#include <cutils/list.h> 44#include <cutils/sockets.h> 45#include <cutils/iosched_policy.h> 46#include <private/android_filesystem_config.h> 47#include <termios.h> 48 49#include <sys/system_properties.h> 50 51#include "devices.h" 52#include "init.h" 53#include "log.h" 54#include "property_service.h" 55#include "bootchart.h" 56#include "signal_handler.h" 57#include "keychords.h" 58#include "init_parser.h" 59#include "util.h" 60#include "ueventd.h" 61 62#ifdef HAVE_SELINUX 63struct selabel_handle *sehandle; 64#endif 65 66static int property_triggers_enabled = 0; 67 68#if BOOTCHART 69static int bootchart_count; 70#endif 71 72static char console[32]; 73static char bootmode[32]; 74static char hardware[32]; 75static unsigned revision = 0; 76static char qemu[32]; 77 78#ifdef HAVE_SELINUX 79static int selinux_enabled = 1; 80static int selinux_enforcing = 0; 81#endif 82 83static struct action *cur_action = NULL; 84static struct command *cur_command = NULL; 85static struct listnode *command_queue = NULL; 86 87void notify_service_state(const char *name, const char *state) 88{ 89 char pname[PROP_NAME_MAX]; 90 int len = strlen(name); 91 if ((len + 10) > PROP_NAME_MAX) 92 return; 93 snprintf(pname, sizeof(pname), "init.svc.%s", name); 94 property_set(pname, state); 95} 96 97static int have_console; 98static char *console_name = "/dev/console"; 99static time_t process_needs_restart; 100 101static const char *ENV[32]; 102 103/* add_environment - add "key=value" to the current environment */ 104int add_environment(const char *key, const char *val) 105{ 106 int n; 107 108 for (n = 0; n < 31; n++) { 109 if (!ENV[n]) { 110 size_t len = strlen(key) + strlen(val) + 2; 111 char *entry = malloc(len); 112 snprintf(entry, len, "%s=%s", key, val); 113 ENV[n] = entry; 114 return 0; 115 } 116 } 117 118 return 1; 119} 120 121static void zap_stdio(void) 122{ 123 int fd; 124 fd = open("/dev/null", O_RDWR); 125 dup2(fd, 0); 126 dup2(fd, 1); 127 dup2(fd, 2); 128 close(fd); 129} 130 131static void open_console() 132{ 133 int fd; 134 if ((fd = open(console_name, O_RDWR)) < 0) { 135 fd = open("/dev/null", O_RDWR); 136 } 137 dup2(fd, 0); 138 dup2(fd, 1); 139 dup2(fd, 2); 140 close(fd); 141} 142 143static void publish_socket(const char *name, int fd) 144{ 145 char key[64] = ANDROID_SOCKET_ENV_PREFIX; 146 char val[64]; 147 148 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, 149 name, 150 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); 151 snprintf(val, sizeof(val), "%d", fd); 152 add_environment(key, val); 153 154 /* make sure we don't close-on-exec */ 155 fcntl(fd, F_SETFD, 0); 156} 157 158void service_start(struct service *svc, const char *dynamic_args) 159{ 160 struct stat s; 161 pid_t pid; 162 int needs_console; 163 int n; 164#ifdef HAVE_SELINUX 165 char *scon = NULL; 166 int rc; 167#endif 168 /* starting a service removes it from the disabled or reset 169 * state and immediately takes it out of the restarting 170 * state if it was in there 171 */ 172 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET)); 173 svc->time_started = 0; 174 175 /* running processes require no additional work -- if 176 * they're in the process of exiting, we've ensured 177 * that they will immediately restart on exit, unless 178 * they are ONESHOT 179 */ 180 if (svc->flags & SVC_RUNNING) { 181 return; 182 } 183 184 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; 185 if (needs_console && (!have_console)) { 186 ERROR("service '%s' requires console\n", svc->name); 187 svc->flags |= SVC_DISABLED; 188 return; 189 } 190 191 if (stat(svc->args[0], &s) != 0) { 192 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); 193 svc->flags |= SVC_DISABLED; 194 return; 195 } 196 197 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { 198 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", 199 svc->args[0]); 200 svc->flags |= SVC_DISABLED; 201 return; 202 } 203 204#ifdef HAVE_SELINUX 205 if (is_selinux_enabled() > 0) { 206 char *mycon = NULL, *fcon = NULL; 207 208 INFO("computing context for service '%s'\n", svc->args[0]); 209 rc = getcon(&mycon); 210 if (rc < 0) { 211 ERROR("could not get context while starting '%s'\n", svc->name); 212 return; 213 } 214 215 rc = getfilecon(svc->args[0], &fcon); 216 if (rc < 0) { 217 ERROR("could not get context while starting '%s'\n", svc->name); 218 freecon(mycon); 219 return; 220 } 221 222 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon); 223 freecon(mycon); 224 freecon(fcon); 225 if (rc < 0) { 226 ERROR("could not get context while starting '%s'\n", svc->name); 227 return; 228 } 229 } 230#endif 231 232 NOTICE("starting '%s'\n", svc->name); 233 234 pid = fork(); 235 236 if (pid == 0) { 237 struct socketinfo *si; 238 struct svcenvinfo *ei; 239 char tmp[32]; 240 int fd, sz; 241 242 if (properties_inited()) { 243 get_property_workspace(&fd, &sz); 244 sprintf(tmp, "%d,%d", dup(fd), sz); 245 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); 246 } 247 248 for (ei = svc->envvars; ei; ei = ei->next) 249 add_environment(ei->name, ei->value); 250 251#ifdef HAVE_SELINUX 252 setsockcreatecon(scon); 253#endif 254 255 for (si = svc->sockets; si; si = si->next) { 256 int socket_type = ( 257 !strcmp(si->type, "stream") ? SOCK_STREAM : 258 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); 259 int s = create_socket(si->name, socket_type, 260 si->perm, si->uid, si->gid); 261 if (s >= 0) { 262 publish_socket(si->name, s); 263 } 264 } 265 266#ifdef HAVE_SELINUX 267 freecon(scon); 268 scon = NULL; 269 setsockcreatecon(NULL); 270#endif 271 272 if (svc->ioprio_class != IoSchedClass_NONE) { 273 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { 274 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", 275 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); 276 } 277 } 278 279 if (needs_console) { 280 setsid(); 281 open_console(); 282 } else { 283 zap_stdio(); 284 } 285 286#if 0 287 for (n = 0; svc->args[n]; n++) { 288 INFO("args[%d] = '%s'\n", n, svc->args[n]); 289 } 290 for (n = 0; ENV[n]; n++) { 291 INFO("env[%d] = '%s'\n", n, ENV[n]); 292 } 293#endif 294 295 setpgid(0, getpid()); 296 297 /* as requested, set our gid, supplemental gids, and uid */ 298 if (svc->gid) { 299 if (setgid(svc->gid) != 0) { 300 ERROR("setgid failed: %s\n", strerror(errno)); 301 _exit(127); 302 } 303 } 304 if (svc->nr_supp_gids) { 305 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { 306 ERROR("setgroups failed: %s\n", strerror(errno)); 307 _exit(127); 308 } 309 } 310 if (svc->uid) { 311 if (setuid(svc->uid) != 0) { 312 ERROR("setuid failed: %s\n", strerror(errno)); 313 _exit(127); 314 } 315 } 316 317#ifdef HAVE_SELINUX 318 if (svc->seclabel) { 319 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) { 320 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno)); 321 _exit(127); 322 } 323 } 324#endif 325 326 if (!dynamic_args) { 327 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { 328 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); 329 } 330 } else { 331 char *arg_ptrs[INIT_PARSER_MAXARGS+1]; 332 int arg_idx = svc->nargs; 333 char *tmp = strdup(dynamic_args); 334 char *next = tmp; 335 char *bword; 336 337 /* Copy the static arguments */ 338 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); 339 340 while((bword = strsep(&next, " "))) { 341 arg_ptrs[arg_idx++] = bword; 342 if (arg_idx == INIT_PARSER_MAXARGS) 343 break; 344 } 345 arg_ptrs[arg_idx] = '\0'; 346 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); 347 } 348 _exit(127); 349 } 350 351#ifdef HAVE_SELINUX 352 freecon(scon); 353#endif 354 355 if (pid < 0) { 356 ERROR("failed to start '%s'\n", svc->name); 357 svc->pid = 0; 358 return; 359 } 360 361 svc->time_started = gettime(); 362 svc->pid = pid; 363 svc->flags |= SVC_RUNNING; 364 365 if (properties_inited()) 366 notify_service_state(svc->name, "running"); 367} 368 369/* The how field should be either SVC_DISABLED or SVC_RESET */ 370static void service_stop_or_reset(struct service *svc, int how) 371{ 372 /* we are no longer running, nor should we 373 * attempt to restart 374 */ 375 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING)); 376 377 if ((how != SVC_DISABLED) && (how != SVC_RESET)) { 378 /* Hrm, an illegal flag. Default to SVC_DISABLED */ 379 how = SVC_DISABLED; 380 } 381 /* if the service has not yet started, prevent 382 * it from auto-starting with its class 383 */ 384 if (how == SVC_RESET) { 385 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; 386 } else { 387 svc->flags |= how; 388 } 389 390 if (svc->pid) { 391 NOTICE("service '%s' is being killed\n", svc->name); 392 kill(-svc->pid, SIGKILL); 393 notify_service_state(svc->name, "stopping"); 394 } else { 395 notify_service_state(svc->name, "stopped"); 396 } 397} 398 399void service_reset(struct service *svc) 400{ 401 service_stop_or_reset(svc, SVC_RESET); 402} 403 404void service_stop(struct service *svc) 405{ 406 service_stop_or_reset(svc, SVC_DISABLED); 407} 408 409void property_changed(const char *name, const char *value) 410{ 411 if (property_triggers_enabled) 412 queue_property_triggers(name, value); 413} 414 415static void restart_service_if_needed(struct service *svc) 416{ 417 time_t next_start_time = svc->time_started + 5; 418 419 if (next_start_time <= gettime()) { 420 svc->flags &= (~SVC_RESTARTING); 421 service_start(svc, NULL); 422 return; 423 } 424 425 if ((next_start_time < process_needs_restart) || 426 (process_needs_restart == 0)) { 427 process_needs_restart = next_start_time; 428 } 429} 430 431static void restart_processes() 432{ 433 process_needs_restart = 0; 434 service_for_each_flags(SVC_RESTARTING, 435 restart_service_if_needed); 436} 437 438static void msg_start(const char *name) 439{ 440 struct service *svc; 441 char *tmp = NULL; 442 char *args = NULL; 443 444 if (!strchr(name, ':')) 445 svc = service_find_by_name(name); 446 else { 447 tmp = strdup(name); 448 args = strchr(tmp, ':'); 449 *args = '\0'; 450 args++; 451 452 svc = service_find_by_name(tmp); 453 } 454 455 if (svc) { 456 service_start(svc, args); 457 } else { 458 ERROR("no such service '%s'\n", name); 459 } 460 if (tmp) 461 free(tmp); 462} 463 464static void msg_stop(const char *name) 465{ 466 struct service *svc = service_find_by_name(name); 467 468 if (svc) { 469 service_stop(svc); 470 } else { 471 ERROR("no such service '%s'\n", name); 472 } 473} 474 475void handle_control_message(const char *msg, const char *arg) 476{ 477 if (!strcmp(msg,"start")) { 478 msg_start(arg); 479 } else if (!strcmp(msg,"stop")) { 480 msg_stop(arg); 481 } else if (!strcmp(msg,"restart")) { 482 msg_stop(arg); 483 msg_start(arg); 484 } else { 485 ERROR("unknown control msg '%s'\n", msg); 486 } 487} 488 489static struct command *get_first_command(struct action *act) 490{ 491 struct listnode *node; 492 node = list_head(&act->commands); 493 if (!node || list_empty(&act->commands)) 494 return NULL; 495 496 return node_to_item(node, struct command, clist); 497} 498 499static struct command *get_next_command(struct action *act, struct command *cmd) 500{ 501 struct listnode *node; 502 node = cmd->clist.next; 503 if (!node) 504 return NULL; 505 if (node == &act->commands) 506 return NULL; 507 508 return node_to_item(node, struct command, clist); 509} 510 511static int is_last_command(struct action *act, struct command *cmd) 512{ 513 return (list_tail(&act->commands) == &cmd->clist); 514} 515 516void execute_one_command(void) 517{ 518 int ret; 519 520 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) { 521 cur_action = action_remove_queue_head(); 522 cur_command = NULL; 523 if (!cur_action) 524 return; 525 INFO("processing action %p (%s)\n", cur_action, cur_action->name); 526 cur_command = get_first_command(cur_action); 527 } else { 528 cur_command = get_next_command(cur_action, cur_command); 529 } 530 531 if (!cur_command) 532 return; 533 534 ret = cur_command->func(cur_command->nargs, cur_command->args); 535 INFO("command '%s' r=%d\n", cur_command->args[0], ret); 536} 537 538static int wait_for_coldboot_done_action(int nargs, char **args) 539{ 540 int ret; 541 INFO("wait for %s\n", coldboot_done); 542 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT); 543 if (ret) 544 ERROR("Timed out waiting for %s\n", coldboot_done); 545 return ret; 546} 547 548static int keychord_init_action(int nargs, char **args) 549{ 550 keychord_init(); 551 return 0; 552} 553 554static int console_init_action(int nargs, char **args) 555{ 556 int fd; 557 char tmp[PROP_VALUE_MAX]; 558 559 if (console[0]) { 560 snprintf(tmp, sizeof(tmp), "/dev/%s", console); 561 console_name = strdup(tmp); 562 } 563 564 fd = open(console_name, O_RDWR); 565 if (fd >= 0) 566 have_console = 1; 567 close(fd); 568 569 if( load_565rle_image(INIT_IMAGE_FILE) ) { 570 fd = open("/dev/tty0", O_WRONLY); 571 if (fd >= 0) { 572 const char *msg; 573 msg = "\n" 574 "\n" 575 "\n" 576 "\n" 577 "\n" 578 "\n" 579 "\n" // console is 40 cols x 30 lines 580 "\n" 581 "\n" 582 "\n" 583 "\n" 584 "\n" 585 "\n" 586 "\n" 587 " A N D R O I D "; 588 write(fd, msg, strlen(msg)); 589 close(fd); 590 } 591 } 592 return 0; 593} 594 595static void import_kernel_nv(char *name, int for_emulator) 596{ 597 char *value = strchr(name, '='); 598 int name_len = strlen(name); 599 600 if (value == 0) return; 601 *value++ = 0; 602 if (name_len == 0) return; 603 604#ifdef HAVE_SELINUX 605 if (!strcmp(name,"enforcing")) { 606 selinux_enforcing = atoi(value); 607 } else if (!strcmp(name,"selinux")) { 608 selinux_enabled = atoi(value); 609 } 610#endif 611 612 if (for_emulator) { 613 /* in the emulator, export any kernel option with the 614 * ro.kernel. prefix */ 615 char buff[PROP_NAME_MAX]; 616 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); 617 618 if (len < (int)sizeof(buff)) 619 property_set( buff, value ); 620 return; 621 } 622 623 if (!strcmp(name,"qemu")) { 624 strlcpy(qemu, value, sizeof(qemu)); 625 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) { 626 const char *boot_prop_name = name + 12; 627 char prop[PROP_NAME_MAX]; 628 int cnt; 629 630 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name); 631 if (cnt < PROP_NAME_MAX) 632 property_set(prop, value); 633 } 634} 635 636static void export_kernel_boot_props(void) 637{ 638 char tmp[PROP_VALUE_MAX]; 639 const char *pval; 640 unsigned i; 641 struct { 642 const char *src_prop; 643 const char *dest_prop; 644 const char *def_val; 645 } prop_map[] = { 646 { "ro.boot.serialno", "ro.serialno", "", }, 647 { "ro.boot.mode", "ro.bootmode", "unknown", }, 648 { "ro.boot.baseband", "ro.baseband", "unknown", }, 649 { "ro.boot.carrier", "ro.carrier", "unknown", }, 650 { "ro.boot.bootloader", "ro.bootloader", "unknown", }, 651 }; 652 653 for (i = 0; i < ARRAY_SIZE(prop_map); i++) { 654 pval = property_get(prop_map[i].src_prop); 655 property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val); 656 } 657 658 pval = property_get("ro.boot.console"); 659 if (pval) 660 strlcpy(console, pval, sizeof(console)); 661 662 /* save a copy for init's usage during boot */ 663 strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode)); 664 665 /* if this was given on kernel command line, override what we read 666 * before (e.g. from /proc/cpuinfo), if anything */ 667 pval = property_get("ro.boot.hardware"); 668 if (pval) 669 strlcpy(hardware, pval, sizeof(hardware)); 670 property_set("ro.hardware", hardware); 671 672 snprintf(tmp, PROP_VALUE_MAX, "%d", revision); 673 property_set("ro.revision", tmp); 674 675 /* TODO: these are obsolete. We should delete them */ 676 if (!strcmp(bootmode,"factory")) 677 property_set("ro.factorytest", "1"); 678 else if (!strcmp(bootmode,"factory2")) 679 property_set("ro.factorytest", "2"); 680 else 681 property_set("ro.factorytest", "0"); 682} 683 684static void process_kernel_cmdline(void) 685{ 686 /* don't expose the raw commandline to nonpriv processes */ 687 chmod("/proc/cmdline", 0440); 688 689 /* first pass does the common stuff, and finds if we are in qemu. 690 * second pass is only necessary for qemu to export all kernel params 691 * as props. 692 */ 693 import_kernel_cmdline(0, import_kernel_nv); 694 if (qemu[0]) 695 import_kernel_cmdline(1, import_kernel_nv); 696 697 /* now propogate the info given on command line to internal variables 698 * used by init as well as the current required properties 699 */ 700 export_kernel_boot_props(); 701} 702 703static int property_service_init_action(int nargs, char **args) 704{ 705 /* read any property files on system or data and 706 * fire up the property service. This must happen 707 * after the ro.foo properties are set above so 708 * that /data/local.prop cannot interfere with them. 709 */ 710 start_property_service(); 711 return 0; 712} 713 714static int signal_init_action(int nargs, char **args) 715{ 716 signal_init(); 717 return 0; 718} 719 720static int check_startup_action(int nargs, char **args) 721{ 722 /* make sure we actually have all the pieces we need */ 723 if ((get_property_set_fd() < 0) || 724 (get_signal_fd() < 0)) { 725 ERROR("init startup failure\n"); 726 exit(1); 727 } 728 729 /* signal that we hit this point */ 730 unlink("/dev/.booting"); 731 732 return 0; 733} 734 735static int queue_property_triggers_action(int nargs, char **args) 736{ 737 queue_all_property_triggers(); 738 /* enable property triggers */ 739 property_triggers_enabled = 1; 740 return 0; 741} 742 743#if BOOTCHART 744static int bootchart_init_action(int nargs, char **args) 745{ 746 bootchart_count = bootchart_init(); 747 if (bootchart_count < 0) { 748 ERROR("bootcharting init failure\n"); 749 } else if (bootchart_count > 0) { 750 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); 751 } else { 752 NOTICE("bootcharting ignored\n"); 753 } 754 755 return 0; 756} 757#endif 758 759#ifdef HAVE_SELINUX 760void selinux_load_policy(void) 761{ 762 const char path_prefix[] = "/sepolicy"; 763 struct selinux_opt seopts[] = { 764 { SELABEL_OPT_PATH, "/file_contexts" } 765 }; 766 char path[PATH_MAX]; 767 int fd, rc, vers; 768 struct stat sb; 769 void *map; 770 771 sehandle = NULL; 772 if (!selinux_enabled) { 773 INFO("SELinux: Disabled by command line option\n"); 774 return; 775 } 776 777 mkdir(SELINUXMNT, 0755); 778 if (mount("selinuxfs", SELINUXMNT, "selinuxfs", 0, NULL)) { 779 if (errno == ENODEV) { 780 /* SELinux not enabled in kernel */ 781 return; 782 } 783 ERROR("SELinux: Could not mount selinuxfs: %s\n", 784 strerror(errno)); 785 return; 786 } 787 set_selinuxmnt(SELINUXMNT); 788 789 vers = security_policyvers(); 790 if (vers <= 0) { 791 ERROR("SELinux: Unable to read policy version\n"); 792 return; 793 } 794 INFO("SELinux: Maximum supported policy version: %d\n", vers); 795 796 snprintf(path, sizeof(path), "%s.%d", 797 path_prefix, vers); 798 fd = open(path, O_RDONLY); 799 while (fd < 0 && errno == ENOENT && --vers) { 800 snprintf(path, sizeof(path), "%s.%d", 801 path_prefix, vers); 802 fd = open(path, O_RDONLY); 803 } 804 if (fd < 0) { 805 ERROR("SELinux: Could not open %s: %s\n", 806 path, strerror(errno)); 807 return; 808 } 809 if (fstat(fd, &sb) < 0) { 810 ERROR("SELinux: Could not stat %s: %s\n", 811 path, strerror(errno)); 812 return; 813 } 814 map = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 815 if (map == MAP_FAILED) { 816 ERROR("SELinux: Could not map %s: %s\n", 817 path, strerror(errno)); 818 return; 819 } 820 821 rc = security_load_policy(map, sb.st_size); 822 if (rc < 0) { 823 ERROR("SELinux: Could not load policy: %s\n", 824 strerror(errno)); 825 return; 826 } 827 828 rc = security_setenforce(selinux_enforcing); 829 if (rc < 0) { 830 ERROR("SELinux: Could not set enforcing mode to %s: %s\n", 831 selinux_enforcing ? "enforcing" : "permissive", strerror(errno)); 832 return; 833 } 834 835 munmap(map, sb.st_size); 836 close(fd); 837 INFO("SELinux: Loaded policy from %s\n", path); 838 839 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); 840 if (!sehandle) { 841 ERROR("SELinux: Could not load file_contexts: %s\n", 842 strerror(errno)); 843 return; 844 } 845 INFO("SELinux: Loaded file contexts from %s\n", seopts[0].value); 846 return; 847} 848#endif 849 850int main(int argc, char **argv) 851{ 852 int fd_count = 0; 853 struct pollfd ufds[4]; 854 char *tmpdev; 855 char* debuggable; 856 char tmp[32]; 857 int property_set_fd_init = 0; 858 int signal_fd_init = 0; 859 int keychord_fd_init = 0; 860 bool is_charger = false; 861 862 if (!strcmp(basename(argv[0]), "ueventd")) 863 return ueventd_main(argc, argv); 864 865 /* clear the umask */ 866 umask(0); 867 868 /* Get the basic filesystem setup we need put 869 * together in the initramdisk on / and then we'll 870 * let the rc file figure out the rest. 871 */ 872 mkdir("/dev", 0755); 873 mkdir("/proc", 0755); 874 mkdir("/sys", 0755); 875 876 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); 877 mkdir("/dev/pts", 0755); 878 mkdir("/dev/socket", 0755); 879 mount("devpts", "/dev/pts", "devpts", 0, NULL); 880 mount("proc", "/proc", "proc", 0, NULL); 881 mount("sysfs", "/sys", "sysfs", 0, NULL); 882 883 /* indicate that booting is in progress to background fw loaders, etc */ 884 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000)); 885 886 /* We must have some place other than / to create the 887 * device nodes for kmsg and null, otherwise we won't 888 * be able to remount / read-only later on. 889 * Now that tmpfs is mounted on /dev, we can actually 890 * talk to the outside world. 891 */ 892 open_devnull_stdio(); 893 klog_init(); 894 property_init(); 895 896 get_hardware_name(hardware, &revision); 897 898 process_kernel_cmdline(); 899 900#ifdef HAVE_SELINUX 901 INFO("loading selinux policy\n"); 902 selinux_load_policy(); 903#endif 904 905 is_charger = !strcmp(bootmode, "charger"); 906 907 INFO("property init\n"); 908 if (!is_charger) 909 property_load_boot_defaults(); 910 911 INFO("reading config file\n"); 912 init_parse_config_file("/init.rc"); 913 914 action_for_each_trigger("early-init", action_add_queue_tail); 915 916 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done"); 917 queue_builtin_action(keychord_init_action, "keychord_init"); 918 queue_builtin_action(console_init_action, "console_init"); 919 920 /* execute all the boot actions to get us started */ 921 action_for_each_trigger("init", action_add_queue_tail); 922 923 /* skip mounting filesystems in charger mode */ 924 if (!is_charger) { 925 action_for_each_trigger("early-fs", action_add_queue_tail); 926 action_for_each_trigger("fs", action_add_queue_tail); 927 action_for_each_trigger("post-fs", action_add_queue_tail); 928 action_for_each_trigger("post-fs-data", action_add_queue_tail); 929 } 930 931 queue_builtin_action(property_service_init_action, "property_service_init"); 932 queue_builtin_action(signal_init_action, "signal_init"); 933 queue_builtin_action(check_startup_action, "check_startup"); 934 935 if (is_charger) { 936 action_for_each_trigger("charger", action_add_queue_tail); 937 } else { 938 action_for_each_trigger("early-boot", action_add_queue_tail); 939 action_for_each_trigger("boot", action_add_queue_tail); 940 } 941 942 /* run all property triggers based on current state of the properties */ 943 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers"); 944 945 946#if BOOTCHART 947 queue_builtin_action(bootchart_init_action, "bootchart_init"); 948#endif 949 950 for(;;) { 951 int nr, i, timeout = -1; 952 953 execute_one_command(); 954 restart_processes(); 955 956 if (!property_set_fd_init && get_property_set_fd() > 0) { 957 ufds[fd_count].fd = get_property_set_fd(); 958 ufds[fd_count].events = POLLIN; 959 ufds[fd_count].revents = 0; 960 fd_count++; 961 property_set_fd_init = 1; 962 } 963 if (!signal_fd_init && get_signal_fd() > 0) { 964 ufds[fd_count].fd = get_signal_fd(); 965 ufds[fd_count].events = POLLIN; 966 ufds[fd_count].revents = 0; 967 fd_count++; 968 signal_fd_init = 1; 969 } 970 if (!keychord_fd_init && get_keychord_fd() > 0) { 971 ufds[fd_count].fd = get_keychord_fd(); 972 ufds[fd_count].events = POLLIN; 973 ufds[fd_count].revents = 0; 974 fd_count++; 975 keychord_fd_init = 1; 976 } 977 978 if (process_needs_restart) { 979 timeout = (process_needs_restart - gettime()) * 1000; 980 if (timeout < 0) 981 timeout = 0; 982 } 983 984 if (!action_queue_empty() || cur_action) 985 timeout = 0; 986 987#if BOOTCHART 988 if (bootchart_count > 0) { 989 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) 990 timeout = BOOTCHART_POLLING_MS; 991 if (bootchart_step() < 0 || --bootchart_count == 0) { 992 bootchart_finish(); 993 bootchart_count = 0; 994 } 995 } 996#endif 997 998 nr = poll(ufds, fd_count, timeout); 999 if (nr <= 0) 1000 continue; 1001 1002 for (i = 0; i < fd_count; i++) { 1003 if (ufds[i].revents == POLLIN) { 1004 if (ufds[i].fd == get_property_set_fd()) 1005 handle_property_set_fd(); 1006 else if (ufds[i].fd == get_keychord_fd()) 1007 handle_keychord(); 1008 else if (ufds[i].fd == get_signal_fd()) 1009 handle_signal(); 1010 } 1011 } 1012 } 1013 1014 return 0; 1015} 1016