init.c revision f24ed8ca0a13f1b97bd55d10f75a289bf9ccd98d
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 <time.h> 29#include <errno.h> 30#include <stdarg.h> 31#include <mtd/mtd-user.h> 32#include <sys/types.h> 33#include <sys/socket.h> 34#include <sys/un.h> 35#include <sys/reboot.h> 36 37#include <cutils/sockets.h> 38#include <cutils/iosched_policy.h> 39#include <termios.h> 40#include <linux/kd.h> 41#include <linux/keychord.h> 42 43#include <sys/system_properties.h> 44 45#include "devices.h" 46#include "init.h" 47#include "property_service.h" 48#include "bootchart.h" 49 50static int property_triggers_enabled = 0; 51 52#if BOOTCHART 53static int bootchart_count; 54#endif 55 56static char console[32]; 57static char serialno[32]; 58static char bootmode[32]; 59static char baseband[32]; 60static char carrier[32]; 61static char bootloader[32]; 62static char hardware[32]; 63static unsigned revision = 0; 64static char qemu[32]; 65static struct input_keychord *keychords = 0; 66static int keychords_count = 0; 67static int keychords_length = 0; 68 69static void notify_service_state(const char *name, const char *state) 70{ 71 char pname[PROP_NAME_MAX]; 72 int len = strlen(name); 73 if ((len + 10) > PROP_NAME_MAX) 74 return; 75 snprintf(pname, sizeof(pname), "init.svc.%s", name); 76 property_set(pname, state); 77} 78 79static int have_console; 80static char *console_name = "/dev/console"; 81static time_t process_needs_restart; 82 83static const char *ENV[32]; 84 85/* add_environment - add "key=value" to the current environment */ 86int add_environment(const char *key, const char *val) 87{ 88 int n; 89 90 for (n = 0; n < 31; n++) { 91 if (!ENV[n]) { 92 size_t len = strlen(key) + strlen(val) + 2; 93 char *entry = malloc(len); 94 snprintf(entry, len, "%s=%s", key, val); 95 ENV[n] = entry; 96 return 0; 97 } 98 } 99 100 return 1; 101} 102 103static void zap_stdio(void) 104{ 105 int fd; 106 fd = open("/dev/null", O_RDWR); 107 dup2(fd, 0); 108 dup2(fd, 1); 109 dup2(fd, 2); 110 close(fd); 111} 112 113static void open_console() 114{ 115 int fd; 116 if ((fd = open(console_name, O_RDWR)) < 0) { 117 fd = open("/dev/null", O_RDWR); 118 } 119 dup2(fd, 0); 120 dup2(fd, 1); 121 dup2(fd, 2); 122 close(fd); 123} 124 125/* 126 * gettime() - returns the time in seconds of the system's monotonic clock or 127 * zero on error. 128 */ 129static time_t gettime(void) 130{ 131 struct timespec ts; 132 int ret; 133 134 ret = clock_gettime(CLOCK_MONOTONIC, &ts); 135 if (ret < 0) { 136 ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno)); 137 return 0; 138 } 139 140 return ts.tv_sec; 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 165 /* starting a service removes it from the disabled 166 * state and immediately takes it out of the restarting 167 * state if it was in there 168 */ 169 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING)); 170 svc->time_started = 0; 171 172 /* running processes require no additional work -- if 173 * they're in the process of exiting, we've ensured 174 * that they will immediately restart on exit, unless 175 * they are ONESHOT 176 */ 177 if (svc->flags & SVC_RUNNING) { 178 return; 179 } 180 181 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; 182 if (needs_console && (!have_console)) { 183 ERROR("service '%s' requires console\n", svc->name); 184 svc->flags |= SVC_DISABLED; 185 return; 186 } 187 188 if (stat(svc->args[0], &s) != 0) { 189 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); 190 svc->flags |= SVC_DISABLED; 191 return; 192 } 193 194 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { 195 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", 196 svc->args[0]); 197 svc->flags |= SVC_DISABLED; 198 return; 199 } 200 201 NOTICE("starting '%s'\n", svc->name); 202 203 pid = fork(); 204 205 if (pid == 0) { 206 struct socketinfo *si; 207 struct svcenvinfo *ei; 208 char tmp[32]; 209 int fd, sz; 210 211 get_property_workspace(&fd, &sz); 212 sprintf(tmp, "%d,%d", dup(fd), sz); 213 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); 214 215 for (ei = svc->envvars; ei; ei = ei->next) 216 add_environment(ei->name, ei->value); 217 218 for (si = svc->sockets; si; si = si->next) { 219 int s = create_socket(si->name, 220 !strcmp(si->type, "dgram") ? 221 SOCK_DGRAM : SOCK_STREAM, 222 si->perm, si->uid, si->gid); 223 if (s >= 0) { 224 publish_socket(si->name, s); 225 } 226 } 227 228 if (svc->ioprio_class != IoSchedClass_NONE) { 229 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { 230 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", 231 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); 232 } 233 } 234 235 if (needs_console) { 236 setsid(); 237 open_console(); 238 } else { 239 zap_stdio(); 240 } 241 242#if 0 243 for (n = 0; svc->args[n]; n++) { 244 INFO("args[%d] = '%s'\n", n, svc->args[n]); 245 } 246 for (n = 0; ENV[n]; n++) { 247 INFO("env[%d] = '%s'\n", n, ENV[n]); 248 } 249#endif 250 251 setpgid(0, getpid()); 252 253 /* as requested, set our gid, supplemental gids, and uid */ 254 if (svc->gid) { 255 setgid(svc->gid); 256 } 257 if (svc->nr_supp_gids) { 258 setgroups(svc->nr_supp_gids, svc->supp_gids); 259 } 260 if (svc->uid) { 261 setuid(svc->uid); 262 } 263 264 if (!dynamic_args) { 265 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { 266 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); 267 } 268 } else { 269 char *arg_ptrs[SVC_MAXARGS+1]; 270 int arg_idx = svc->nargs; 271 char *tmp = strdup(dynamic_args); 272 char *next = tmp; 273 char *bword; 274 275 /* Copy the static arguments */ 276 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); 277 278 while((bword = strsep(&next, " "))) { 279 arg_ptrs[arg_idx++] = bword; 280 if (arg_idx == SVC_MAXARGS) 281 break; 282 } 283 arg_ptrs[arg_idx] = '\0'; 284 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); 285 } 286 _exit(127); 287 } 288 289 if (pid < 0) { 290 ERROR("failed to start '%s'\n", svc->name); 291 svc->pid = 0; 292 return; 293 } 294 295 svc->time_started = gettime(); 296 svc->pid = pid; 297 svc->flags |= SVC_RUNNING; 298 299 notify_service_state(svc->name, "running"); 300} 301 302void service_stop(struct service *svc) 303{ 304 /* we are no longer running, nor should we 305 * attempt to restart 306 */ 307 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING)); 308 309 /* if the service has not yet started, prevent 310 * it from auto-starting with its class 311 */ 312 svc->flags |= SVC_DISABLED; 313 314 if (svc->pid) { 315 NOTICE("service '%s' is being killed\n", svc->name); 316 kill(-svc->pid, SIGTERM); 317 notify_service_state(svc->name, "stopping"); 318 } else { 319 notify_service_state(svc->name, "stopped"); 320 } 321} 322 323void property_changed(const char *name, const char *value) 324{ 325 if (property_triggers_enabled) { 326 queue_property_triggers(name, value); 327 drain_action_queue(); 328 } 329} 330 331#define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */ 332#define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery*/ 333 334static int wait_for_one_process(int block) 335{ 336 pid_t pid; 337 int status; 338 struct service *svc; 339 struct socketinfo *si; 340 time_t now; 341 struct listnode *node; 342 struct command *cmd; 343 344 while ( (pid = waitpid(-1, &status, block ? 0 : WNOHANG)) == -1 && errno == EINTR ); 345 if (pid <= 0) return -1; 346 INFO("waitpid returned pid %d, status = %08x\n", pid, status); 347 348 svc = service_find_by_pid(pid); 349 if (!svc) { 350 ERROR("untracked pid %d exited\n", pid); 351 return 0; 352 } 353 354 NOTICE("process '%s', pid %d exited\n", svc->name, pid); 355 356 if (!(svc->flags & SVC_ONESHOT)) { 357 kill(-pid, SIGKILL); 358 NOTICE("process '%s' killing any children in process group\n", svc->name); 359 } 360 361 /* remove any sockets we may have created */ 362 for (si = svc->sockets; si; si = si->next) { 363 char tmp[128]; 364 snprintf(tmp, sizeof(tmp), ANDROID_SOCKET_DIR"/%s", si->name); 365 unlink(tmp); 366 } 367 368 svc->pid = 0; 369 svc->flags &= (~SVC_RUNNING); 370 371 /* oneshot processes go into the disabled state on exit */ 372 if (svc->flags & SVC_ONESHOT) { 373 svc->flags |= SVC_DISABLED; 374 } 375 376 /* disabled processes do not get restarted automatically */ 377 if (svc->flags & SVC_DISABLED) { 378 notify_service_state(svc->name, "stopped"); 379 return 0; 380 } 381 382 now = gettime(); 383 if (svc->flags & SVC_CRITICAL) { 384 if (svc->time_crashed + CRITICAL_CRASH_WINDOW >= now) { 385 if (++svc->nr_crashed > CRITICAL_CRASH_THRESHOLD) { 386 ERROR("critical process '%s' exited %d times in %d minutes; " 387 "rebooting into recovery mode\n", svc->name, 388 CRITICAL_CRASH_THRESHOLD, CRITICAL_CRASH_WINDOW / 60); 389 sync(); 390 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, 391 LINUX_REBOOT_CMD_RESTART2, "recovery"); 392 return 0; 393 } 394 } else { 395 svc->time_crashed = now; 396 svc->nr_crashed = 1; 397 } 398 } 399 400 svc->flags |= SVC_RESTARTING; 401 402 /* Execute all onrestart commands for this service. */ 403 list_for_each(node, &svc->onrestart.commands) { 404 cmd = node_to_item(node, struct command, clist); 405 cmd->func(cmd->nargs, cmd->args); 406 } 407 notify_service_state(svc->name, "restarting"); 408 return 0; 409} 410 411static void restart_service_if_needed(struct service *svc) 412{ 413 time_t next_start_time = svc->time_started + 5; 414 415 if (next_start_time <= gettime()) { 416 svc->flags &= (~SVC_RESTARTING); 417 service_start(svc, NULL); 418 return; 419 } 420 421 if ((next_start_time < process_needs_restart) || 422 (process_needs_restart == 0)) { 423 process_needs_restart = next_start_time; 424 } 425} 426 427static void restart_processes() 428{ 429 process_needs_restart = 0; 430 service_for_each_flags(SVC_RESTARTING, 431 restart_service_if_needed); 432} 433 434static int signal_fd = -1; 435 436static void sigchld_handler(int s) 437{ 438 write(signal_fd, &s, 1); 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 { 485 ERROR("unknown control msg '%s'\n", msg); 486 } 487} 488 489static void import_kernel_nv(char *name, int in_qemu) 490{ 491 char *value = strchr(name, '='); 492 493 if (value == 0) return; 494 *value++ = 0; 495 if (*name == 0) return; 496 497 if (!in_qemu) 498 { 499 /* on a real device, white-list the kernel options */ 500 if (!strcmp(name,"qemu")) { 501 strlcpy(qemu, value, sizeof(qemu)); 502 } else if (!strcmp(name,"androidboot.console")) { 503 strlcpy(console, value, sizeof(console)); 504 } else if (!strcmp(name,"androidboot.mode")) { 505 strlcpy(bootmode, value, sizeof(bootmode)); 506 } else if (!strcmp(name,"androidboot.serialno")) { 507 strlcpy(serialno, value, sizeof(serialno)); 508 } else if (!strcmp(name,"androidboot.baseband")) { 509 strlcpy(baseband, value, sizeof(baseband)); 510 } else if (!strcmp(name,"androidboot.carrier")) { 511 strlcpy(carrier, value, sizeof(carrier)); 512 } else if (!strcmp(name,"androidboot.bootloader")) { 513 strlcpy(bootloader, value, sizeof(bootloader)); 514 } else if (!strcmp(name,"androidboot.hardware")) { 515 strlcpy(hardware, value, sizeof(hardware)); 516 } else { 517 qemu_cmdline(name, value); 518 } 519 } else { 520 /* in the emulator, export any kernel option with the 521 * ro.kernel. prefix */ 522 char buff[32]; 523 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); 524 if (len < (int)sizeof(buff)) { 525 property_set( buff, value ); 526 } 527 } 528} 529 530static void import_kernel_cmdline(int in_qemu) 531{ 532 char cmdline[1024]; 533 char *ptr; 534 int fd; 535 536 fd = open("/proc/cmdline", O_RDONLY); 537 if (fd >= 0) { 538 int n = read(fd, cmdline, 1023); 539 if (n < 0) n = 0; 540 541 /* get rid of trailing newline, it happens */ 542 if (n > 0 && cmdline[n-1] == '\n') n--; 543 544 cmdline[n] = 0; 545 close(fd); 546 } else { 547 cmdline[0] = 0; 548 } 549 550 ptr = cmdline; 551 while (ptr && *ptr) { 552 char *x = strchr(ptr, ' '); 553 if (x != 0) *x++ = 0; 554 import_kernel_nv(ptr, in_qemu); 555 ptr = x; 556 } 557 558 /* don't expose the raw commandline to nonpriv processes */ 559 chmod("/proc/cmdline", 0440); 560} 561 562static void get_hardware_name(void) 563{ 564 char data[1024]; 565 int fd, n; 566 char *x, *hw, *rev; 567 568 /* Hardware string was provided on kernel command line */ 569 if (hardware[0]) 570 return; 571 572 fd = open("/proc/cpuinfo", O_RDONLY); 573 if (fd < 0) return; 574 575 n = read(fd, data, 1023); 576 close(fd); 577 if (n < 0) return; 578 579 data[n] = 0; 580 hw = strstr(data, "\nHardware"); 581 rev = strstr(data, "\nRevision"); 582 583 if (hw) { 584 x = strstr(hw, ": "); 585 if (x) { 586 x += 2; 587 n = 0; 588 while (*x && !isspace(*x)) { 589 hardware[n++] = tolower(*x); 590 x++; 591 if (n == 31) break; 592 } 593 hardware[n] = 0; 594 } 595 } 596 597 if (rev) { 598 x = strstr(rev, ": "); 599 if (x) { 600 revision = strtoul(x + 2, 0, 16); 601 } 602 } 603} 604 605void drain_action_queue(void) 606{ 607 struct listnode *node; 608 struct command *cmd; 609 struct action *act; 610 int ret; 611 612 while ((act = action_remove_queue_head())) { 613 INFO("processing action %p (%s)\n", act, act->name); 614 list_for_each(node, &act->commands) { 615 cmd = node_to_item(node, struct command, clist); 616 ret = cmd->func(cmd->nargs, cmd->args); 617 INFO("command '%s' r=%d\n", cmd->args[0], ret); 618 } 619 } 620} 621 622void open_devnull_stdio(void) 623{ 624 int fd; 625 static const char *name = "/dev/__null__"; 626 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) { 627 fd = open(name, O_RDWR); 628 unlink(name); 629 if (fd >= 0) { 630 dup2(fd, 0); 631 dup2(fd, 1); 632 dup2(fd, 2); 633 if (fd > 2) { 634 close(fd); 635 } 636 return; 637 } 638 } 639 640 exit(1); 641} 642 643void add_service_keycodes(struct service *svc) 644{ 645 struct input_keychord *keychord; 646 int i, size; 647 648 if (svc->keycodes) { 649 /* add a new keychord to the list */ 650 size = sizeof(*keychord) + svc->nkeycodes * sizeof(keychord->keycodes[0]); 651 keychords = realloc(keychords, keychords_length + size); 652 if (!keychords) { 653 ERROR("could not allocate keychords\n"); 654 keychords_length = 0; 655 keychords_count = 0; 656 return; 657 } 658 659 keychord = (struct input_keychord *)((char *)keychords + keychords_length); 660 keychord->version = KEYCHORD_VERSION; 661 keychord->id = keychords_count + 1; 662 keychord->count = svc->nkeycodes; 663 svc->keychord_id = keychord->id; 664 665 for (i = 0; i < svc->nkeycodes; i++) { 666 keychord->keycodes[i] = svc->keycodes[i]; 667 } 668 keychords_count++; 669 keychords_length += size; 670 } 671} 672 673int open_keychord() 674{ 675 int fd, ret; 676 677 service_for_each(add_service_keycodes); 678 679 /* nothing to do if no services require keychords */ 680 if (!keychords) 681 return -1; 682 683 fd = open("/dev/keychord", O_RDWR); 684 if (fd < 0) { 685 ERROR("could not open /dev/keychord\n"); 686 return fd; 687 } 688 fcntl(fd, F_SETFD, FD_CLOEXEC); 689 690 ret = write(fd, keychords, keychords_length); 691 if (ret != keychords_length) { 692 ERROR("could not configure /dev/keychord %d (%d)\n", ret, errno); 693 close(fd); 694 fd = -1; 695 } 696 697 free(keychords); 698 keychords = 0; 699 700 return fd; 701} 702 703void handle_keychord(int fd) 704{ 705 struct service *svc; 706 char* debuggable; 707 char* adb_enabled; 708 int ret; 709 __u16 id; 710 711 // only handle keychords if ro.debuggable is set or adb is enabled. 712 // the logic here is that bugreports should be enabled in userdebug or eng builds 713 // and on user builds for users that are developers. 714 debuggable = property_get("ro.debuggable"); 715 adb_enabled = property_get("init.svc.adbd"); 716 if ((debuggable && !strcmp(debuggable, "1")) || 717 (adb_enabled && !strcmp(adb_enabled, "running"))) { 718 ret = read(fd, &id, sizeof(id)); 719 if (ret != sizeof(id)) { 720 ERROR("could not read keychord id\n"); 721 return; 722 } 723 724 svc = service_find_by_keychord(id); 725 if (svc) { 726 INFO("starting service %s from keychord\n", svc->name); 727 service_start(svc, NULL); 728 } else { 729 ERROR("service for keychord %d not found\n", id); 730 } 731 } 732} 733 734int main(int argc, char **argv) 735{ 736 int device_fd = -1; 737 int property_set_fd = -1; 738 int signal_recv_fd = -1; 739 int keychord_fd = -1; 740 int fd_count; 741 int s[2]; 742 int fd; 743 struct sigaction act; 744 char tmp[PROP_VALUE_MAX]; 745 struct pollfd ufds[4]; 746 char *tmpdev; 747 char* debuggable; 748 749 act.sa_handler = sigchld_handler; 750 act.sa_flags = SA_NOCLDSTOP; 751 act.sa_mask = 0; 752 act.sa_restorer = NULL; 753 sigaction(SIGCHLD, &act, 0); 754 755 /* clear the umask */ 756 umask(0); 757 758 /* Get the basic filesystem setup we need put 759 * together in the initramdisk on / and then we'll 760 * let the rc file figure out the rest. 761 */ 762 mkdir("/dev", 0755); 763 mkdir("/proc", 0755); 764 mkdir("/sys", 0755); 765 766 mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755"); 767 mkdir("/dev/pts", 0755); 768 mkdir("/dev/socket", 0755); 769 mount("devpts", "/dev/pts", "devpts", 0, NULL); 770 mount("proc", "/proc", "proc", 0, NULL); 771 mount("sysfs", "/sys", "sysfs", 0, NULL); 772 773 /* We must have some place other than / to create the 774 * device nodes for kmsg and null, otherwise we won't 775 * be able to remount / read-only later on. 776 * Now that tmpfs is mounted on /dev, we can actually 777 * talk to the outside world. 778 */ 779 open_devnull_stdio(); 780 log_init(); 781 782 INFO("reading config file\n"); 783 parse_config_file("/init.rc"); 784 785 /* pull the kernel commandline and ramdisk properties file in */ 786 qemu_init(); 787 import_kernel_cmdline(0); 788 789 get_hardware_name(); 790 snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware); 791 parse_config_file(tmp); 792 793 action_for_each_trigger("early-init", action_add_queue_tail); 794 drain_action_queue(); 795 796 INFO("device init\n"); 797 device_fd = device_init(); 798 799 property_init(); 800 801 // only listen for keychords if ro.debuggable is true 802 keychord_fd = open_keychord(); 803 804 if (console[0]) { 805 snprintf(tmp, sizeof(tmp), "/dev/%s", console); 806 console_name = strdup(tmp); 807 } 808 809 fd = open(console_name, O_RDWR); 810 if (fd >= 0) 811 have_console = 1; 812 close(fd); 813 814 if( load_565rle_image(INIT_IMAGE_FILE) ) { 815 fd = open("/dev/tty0", O_WRONLY); 816 if (fd >= 0) { 817 const char *msg; 818 msg = "\n" 819 "\n" 820 "\n" 821 "\n" 822 "\n" 823 "\n" 824 "\n" // console is 40 cols x 30 lines 825 "\n" 826 "\n" 827 "\n" 828 "\n" 829 "\n" 830 "\n" 831 "\n" 832 " A N D R O I D "; 833 write(fd, msg, strlen(msg)); 834 close(fd); 835 } 836 } 837 838 if (qemu[0]) 839 import_kernel_cmdline(1); 840 841 if (!strcmp(bootmode,"factory")) 842 property_set("ro.factorytest", "1"); 843 else if (!strcmp(bootmode,"factory2")) 844 property_set("ro.factorytest", "2"); 845 else 846 property_set("ro.factorytest", "0"); 847 848 property_set("ro.serialno", serialno[0] ? serialno : ""); 849 property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown"); 850 property_set("ro.baseband", baseband[0] ? baseband : "unknown"); 851 property_set("ro.carrier", carrier[0] ? carrier : "unknown"); 852 property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown"); 853 854 property_set("ro.hardware", hardware); 855 snprintf(tmp, PROP_VALUE_MAX, "%d", revision); 856 property_set("ro.revision", tmp); 857 858 /* execute all the boot actions to get us started */ 859 action_for_each_trigger("init", action_add_queue_tail); 860 action_for_each_trigger("early-fs", action_add_queue_tail); 861 action_for_each_trigger("fs", action_add_queue_tail); 862 action_for_each_trigger("post-fs", action_add_queue_tail); 863 drain_action_queue(); 864 865 /* read any property files on system or data and 866 * fire up the property service. This must happen 867 * after the ro.foo properties are set above so 868 * that /data/local.prop cannot interfere with them. 869 */ 870 property_set_fd = start_property_service(); 871 872 /* create a signalling mechanism for the sigchld handler */ 873 if (socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0) { 874 signal_fd = s[0]; 875 signal_recv_fd = s[1]; 876 fcntl(s[0], F_SETFD, FD_CLOEXEC); 877 fcntl(s[0], F_SETFL, O_NONBLOCK); 878 fcntl(s[1], F_SETFD, FD_CLOEXEC); 879 fcntl(s[1], F_SETFL, O_NONBLOCK); 880 } 881 882 /* make sure we actually have all the pieces we need */ 883 if ((device_fd < 0) || 884 (property_set_fd < 0) || 885 (signal_recv_fd < 0)) { 886 ERROR("init startup failure\n"); 887 return 1; 888 } 889 890 /* execute all the boot actions to get us started */ 891 action_for_each_trigger("early-boot", action_add_queue_tail); 892 action_for_each_trigger("boot", action_add_queue_tail); 893 drain_action_queue(); 894 895 /* run all property triggers based on current state of the properties */ 896 queue_all_property_triggers(); 897 drain_action_queue(); 898 899 /* enable property triggers */ 900 property_triggers_enabled = 1; 901 902 ufds[0].fd = device_fd; 903 ufds[0].events = POLLIN; 904 ufds[1].fd = property_set_fd; 905 ufds[1].events = POLLIN; 906 ufds[2].fd = signal_recv_fd; 907 ufds[2].events = POLLIN; 908 fd_count = 3; 909 910 if (keychord_fd > 0) { 911 ufds[3].fd = keychord_fd; 912 ufds[3].events = POLLIN; 913 fd_count++; 914 } else { 915 ufds[3].events = 0; 916 ufds[3].revents = 0; 917 } 918 919#if BOOTCHART 920 bootchart_count = bootchart_init(); 921 if (bootchart_count < 0) { 922 ERROR("bootcharting init failure\n"); 923 } else if (bootchart_count > 0) { 924 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); 925 } else { 926 NOTICE("bootcharting ignored\n"); 927 } 928#endif 929 930 for(;;) { 931 int nr, i, timeout = -1; 932 933 for (i = 0; i < fd_count; i++) 934 ufds[i].revents = 0; 935 936 drain_action_queue(); 937 restart_processes(); 938 939 if (process_needs_restart) { 940 timeout = (process_needs_restart - gettime()) * 1000; 941 if (timeout < 0) 942 timeout = 0; 943 } 944 945#if BOOTCHART 946 if (bootchart_count > 0) { 947 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) 948 timeout = BOOTCHART_POLLING_MS; 949 if (bootchart_step() < 0 || --bootchart_count == 0) { 950 bootchart_finish(); 951 bootchart_count = 0; 952 } 953 } 954#endif 955 nr = poll(ufds, fd_count, timeout); 956 if (nr <= 0) 957 continue; 958 959 if (ufds[2].revents == POLLIN) { 960 /* we got a SIGCHLD - reap and restart as needed */ 961 read(signal_recv_fd, tmp, sizeof(tmp)); 962 while (!wait_for_one_process(0)) 963 ; 964 continue; 965 } 966 967 if (ufds[0].revents == POLLIN) 968 handle_device_fd(device_fd); 969 970 if (ufds[1].revents == POLLIN) 971 handle_property_set_fd(property_set_fd); 972 if (ufds[3].revents == POLLIN) 973 handle_keychord(keychord_fd); 974 } 975 976 return 0; 977} 978