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