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