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