init.c revision 3899e9fc01cf608f19f716749c54cc5c4d17e766
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#include <cutils/sockets.h> 36#include <cutils/iosched_policy.h> 37#include <termios.h> 38 39#include <sys/system_properties.h> 40 41#include "devices.h" 42#include "init.h" 43#include "property_service.h" 44#include "bootchart.h" 45#include "signal_handler.h" 46#include "keychords.h" 47#include "parser.h" 48#include "util.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]; 65 66void notify_service_state(const char *name, const char *state) 67{ 68 char pname[PROP_NAME_MAX]; 69 int len = strlen(name); 70 if ((len + 10) > PROP_NAME_MAX) 71 return; 72 snprintf(pname, sizeof(pname), "init.svc.%s", name); 73 property_set(pname, state); 74} 75 76static int have_console; 77static char *console_name = "/dev/console"; 78static time_t process_needs_restart; 79 80static const char *ENV[32]; 81 82/* add_environment - add "key=value" to the current environment */ 83int add_environment(const char *key, const char *val) 84{ 85 int n; 86 87 for (n = 0; n < 31; n++) { 88 if (!ENV[n]) { 89 size_t len = strlen(key) + strlen(val) + 2; 90 char *entry = malloc(len); 91 snprintf(entry, len, "%s=%s", key, val); 92 ENV[n] = entry; 93 return 0; 94 } 95 } 96 97 return 1; 98} 99 100static void zap_stdio(void) 101{ 102 int fd; 103 fd = open("/dev/null", O_RDWR); 104 dup2(fd, 0); 105 dup2(fd, 1); 106 dup2(fd, 2); 107 close(fd); 108} 109 110static void open_console() 111{ 112 int fd; 113 if ((fd = open(console_name, O_RDWR)) < 0) { 114 fd = open("/dev/null", O_RDWR); 115 } 116 dup2(fd, 0); 117 dup2(fd, 1); 118 dup2(fd, 2); 119 close(fd); 120} 121 122static void publish_socket(const char *name, int fd) 123{ 124 char key[64] = ANDROID_SOCKET_ENV_PREFIX; 125 char val[64]; 126 127 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, 128 name, 129 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); 130 snprintf(val, sizeof(val), "%d", fd); 131 add_environment(key, val); 132 133 /* make sure we don't close-on-exec */ 134 fcntl(fd, F_SETFD, 0); 135} 136 137void service_start(struct service *svc, const char *dynamic_args) 138{ 139 struct stat s; 140 pid_t pid; 141 int needs_console; 142 int n; 143 144 /* starting a service removes it from the disabled 145 * state and immediately takes it out of the restarting 146 * state if it was in there 147 */ 148 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING)); 149 svc->time_started = 0; 150 151 /* running processes require no additional work -- if 152 * they're in the process of exiting, we've ensured 153 * that they will immediately restart on exit, unless 154 * they are ONESHOT 155 */ 156 if (svc->flags & SVC_RUNNING) { 157 return; 158 } 159 160 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; 161 if (needs_console && (!have_console)) { 162 ERROR("service '%s' requires console\n", svc->name); 163 svc->flags |= SVC_DISABLED; 164 return; 165 } 166 167 if (stat(svc->args[0], &s) != 0) { 168 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); 169 svc->flags |= SVC_DISABLED; 170 return; 171 } 172 173 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { 174 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", 175 svc->args[0]); 176 svc->flags |= SVC_DISABLED; 177 return; 178 } 179 180 NOTICE("starting '%s'\n", svc->name); 181 182 pid = fork(); 183 184 if (pid == 0) { 185 struct socketinfo *si; 186 struct svcenvinfo *ei; 187 char tmp[32]; 188 int fd, sz; 189 190 get_property_workspace(&fd, &sz); 191 sprintf(tmp, "%d,%d", dup(fd), sz); 192 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); 193 194 for (ei = svc->envvars; ei; ei = ei->next) 195 add_environment(ei->name, ei->value); 196 197 for (si = svc->sockets; si; si = si->next) { 198 int s = create_socket(si->name, 199 !strcmp(si->type, "dgram") ? 200 SOCK_DGRAM : SOCK_STREAM, 201 si->perm, si->uid, si->gid); 202 if (s >= 0) { 203 publish_socket(si->name, s); 204 } 205 } 206 207 if (svc->ioprio_class != IoSchedClass_NONE) { 208 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { 209 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", 210 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); 211 } 212 } 213 214 if (needs_console) { 215 setsid(); 216 open_console(); 217 } else { 218 zap_stdio(); 219 } 220 221#if 0 222 for (n = 0; svc->args[n]; n++) { 223 INFO("args[%d] = '%s'\n", n, svc->args[n]); 224 } 225 for (n = 0; ENV[n]; n++) { 226 INFO("env[%d] = '%s'\n", n, ENV[n]); 227 } 228#endif 229 230 setpgid(0, getpid()); 231 232 /* as requested, set our gid, supplemental gids, and uid */ 233 if (svc->gid) { 234 setgid(svc->gid); 235 } 236 if (svc->nr_supp_gids) { 237 setgroups(svc->nr_supp_gids, svc->supp_gids); 238 } 239 if (svc->uid) { 240 setuid(svc->uid); 241 } 242 243 if (!dynamic_args) { 244 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { 245 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); 246 } 247 } else { 248 char *arg_ptrs[SVC_MAXARGS+1]; 249 int arg_idx = svc->nargs; 250 char *tmp = strdup(dynamic_args); 251 char *next = tmp; 252 char *bword; 253 254 /* Copy the static arguments */ 255 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); 256 257 while((bword = strsep(&next, " "))) { 258 arg_ptrs[arg_idx++] = bword; 259 if (arg_idx == SVC_MAXARGS) 260 break; 261 } 262 arg_ptrs[arg_idx] = '\0'; 263 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); 264 } 265 _exit(127); 266 } 267 268 if (pid < 0) { 269 ERROR("failed to start '%s'\n", svc->name); 270 svc->pid = 0; 271 return; 272 } 273 274 svc->time_started = gettime(); 275 svc->pid = pid; 276 svc->flags |= SVC_RUNNING; 277 278 notify_service_state(svc->name, "running"); 279} 280 281void service_stop(struct service *svc) 282{ 283 /* we are no longer running, nor should we 284 * attempt to restart 285 */ 286 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING)); 287 288 /* if the service has not yet started, prevent 289 * it from auto-starting with its class 290 */ 291 svc->flags |= SVC_DISABLED; 292 293 if (svc->pid) { 294 NOTICE("service '%s' is being killed\n", svc->name); 295 kill(-svc->pid, SIGTERM); 296 notify_service_state(svc->name, "stopping"); 297 } else { 298 notify_service_state(svc->name, "stopped"); 299 } 300} 301 302void property_changed(const char *name, const char *value) 303{ 304 if (property_triggers_enabled) { 305 queue_property_triggers(name, value); 306 drain_action_queue(); 307 } 308} 309 310static void restart_service_if_needed(struct service *svc) 311{ 312 time_t next_start_time = svc->time_started + 5; 313 314 if (next_start_time <= gettime()) { 315 svc->flags &= (~SVC_RESTARTING); 316 service_start(svc, NULL); 317 return; 318 } 319 320 if ((next_start_time < process_needs_restart) || 321 (process_needs_restart == 0)) { 322 process_needs_restart = next_start_time; 323 } 324} 325 326static void restart_processes() 327{ 328 process_needs_restart = 0; 329 service_for_each_flags(SVC_RESTARTING, 330 restart_service_if_needed); 331} 332 333static void msg_start(const char *name) 334{ 335 struct service *svc; 336 char *tmp = NULL; 337 char *args = NULL; 338 339 if (!strchr(name, ':')) 340 svc = service_find_by_name(name); 341 else { 342 tmp = strdup(name); 343 args = strchr(tmp, ':'); 344 *args = '\0'; 345 args++; 346 347 svc = service_find_by_name(tmp); 348 } 349 350 if (svc) { 351 service_start(svc, args); 352 } else { 353 ERROR("no such service '%s'\n", name); 354 } 355 if (tmp) 356 free(tmp); 357} 358 359static void msg_stop(const char *name) 360{ 361 struct service *svc = service_find_by_name(name); 362 363 if (svc) { 364 service_stop(svc); 365 } else { 366 ERROR("no such service '%s'\n", name); 367 } 368} 369 370void handle_control_message(const char *msg, const char *arg) 371{ 372 if (!strcmp(msg,"start")) { 373 msg_start(arg); 374 } else if (!strcmp(msg,"stop")) { 375 msg_stop(arg); 376 } else { 377 ERROR("unknown control msg '%s'\n", msg); 378 } 379} 380 381static void import_kernel_nv(char *name, int in_qemu) 382{ 383 char *value = strchr(name, '='); 384 385 if (value == 0) return; 386 *value++ = 0; 387 if (*name == 0) return; 388 389 if (!in_qemu) 390 { 391 /* on a real device, white-list the kernel options */ 392 if (!strcmp(name,"qemu")) { 393 strlcpy(qemu, value, sizeof(qemu)); 394 } else if (!strcmp(name,"androidboot.console")) { 395 strlcpy(console, value, sizeof(console)); 396 } else if (!strcmp(name,"androidboot.mode")) { 397 strlcpy(bootmode, value, sizeof(bootmode)); 398 } else if (!strcmp(name,"androidboot.serialno")) { 399 strlcpy(serialno, value, sizeof(serialno)); 400 } else if (!strcmp(name,"androidboot.baseband")) { 401 strlcpy(baseband, value, sizeof(baseband)); 402 } else if (!strcmp(name,"androidboot.carrier")) { 403 strlcpy(carrier, value, sizeof(carrier)); 404 } else if (!strcmp(name,"androidboot.bootloader")) { 405 strlcpy(bootloader, value, sizeof(bootloader)); 406 } else if (!strcmp(name,"androidboot.hardware")) { 407 strlcpy(hardware, value, sizeof(hardware)); 408 } else { 409 qemu_cmdline(name, value); 410 } 411 } else { 412 /* in the emulator, export any kernel option with the 413 * ro.kernel. prefix */ 414 char buff[32]; 415 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); 416 if (len < (int)sizeof(buff)) { 417 property_set( buff, value ); 418 } 419 } 420} 421 422static void import_kernel_cmdline(int in_qemu) 423{ 424 char cmdline[1024]; 425 char *ptr; 426 int fd; 427 428 fd = open("/proc/cmdline", O_RDONLY); 429 if (fd >= 0) { 430 int n = read(fd, cmdline, 1023); 431 if (n < 0) n = 0; 432 433 /* get rid of trailing newline, it happens */ 434 if (n > 0 && cmdline[n-1] == '\n') n--; 435 436 cmdline[n] = 0; 437 close(fd); 438 } else { 439 cmdline[0] = 0; 440 } 441 442 ptr = cmdline; 443 while (ptr && *ptr) { 444 char *x = strchr(ptr, ' '); 445 if (x != 0) *x++ = 0; 446 import_kernel_nv(ptr, in_qemu); 447 ptr = x; 448 } 449 450 /* don't expose the raw commandline to nonpriv processes */ 451 chmod("/proc/cmdline", 0440); 452} 453 454static void get_hardware_name(void) 455{ 456 char data[1024]; 457 int fd, n; 458 char *x, *hw, *rev; 459 460 /* Hardware string was provided on kernel command line */ 461 if (hardware[0]) 462 return; 463 464 fd = open("/proc/cpuinfo", O_RDONLY); 465 if (fd < 0) return; 466 467 n = read(fd, data, 1023); 468 close(fd); 469 if (n < 0) return; 470 471 data[n] = 0; 472 hw = strstr(data, "\nHardware"); 473 rev = strstr(data, "\nRevision"); 474 475 if (hw) { 476 x = strstr(hw, ": "); 477 if (x) { 478 x += 2; 479 n = 0; 480 while (*x && !isspace(*x)) { 481 hardware[n++] = tolower(*x); 482 x++; 483 if (n == 31) break; 484 } 485 hardware[n] = 0; 486 } 487 } 488 489 if (rev) { 490 x = strstr(rev, ": "); 491 if (x) { 492 revision = strtoul(x + 2, 0, 16); 493 } 494 } 495} 496 497void drain_action_queue(void) 498{ 499 struct listnode *node; 500 struct command *cmd; 501 struct action *act; 502 int ret; 503 504 while ((act = action_remove_queue_head())) { 505 INFO("processing action %p (%s)\n", act, act->name); 506 list_for_each(node, &act->commands) { 507 cmd = node_to_item(node, struct command, clist); 508 ret = cmd->func(cmd->nargs, cmd->args); 509 INFO("command '%s' r=%d\n", cmd->args[0], ret); 510 } 511 } 512} 513 514void open_devnull_stdio(void) 515{ 516 int fd; 517 static const char *name = "/dev/__null__"; 518 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) { 519 fd = open(name, O_RDWR); 520 unlink(name); 521 if (fd >= 0) { 522 dup2(fd, 0); 523 dup2(fd, 1); 524 dup2(fd, 2); 525 if (fd > 2) { 526 close(fd); 527 } 528 return; 529 } 530 } 531 532 exit(1); 533} 534 535int main(int argc, char **argv) 536{ 537 int fd_count; 538 int s[2]; 539 int fd; 540 struct sigaction act; 541 char tmp[PROP_VALUE_MAX]; 542 struct pollfd ufds[4]; 543 char *tmpdev; 544 char* debuggable; 545 546 /* clear the umask */ 547 umask(0); 548 549 /* Get the basic filesystem setup we need put 550 * together in the initramdisk on / and then we'll 551 * let the rc file figure out the rest. 552 */ 553 mkdir("/dev", 0755); 554 mkdir("/proc", 0755); 555 mkdir("/sys", 0755); 556 557 mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755"); 558 mkdir("/dev/pts", 0755); 559 mkdir("/dev/socket", 0755); 560 mount("devpts", "/dev/pts", "devpts", 0, NULL); 561 mount("proc", "/proc", "proc", 0, NULL); 562 mount("sysfs", "/sys", "sysfs", 0, NULL); 563 564 /* We must have some place other than / to create the 565 * device nodes for kmsg and null, otherwise we won't 566 * be able to remount / read-only later on. 567 * Now that tmpfs is mounted on /dev, we can actually 568 * talk to the outside world. 569 */ 570 open_devnull_stdio(); 571 log_init(); 572 573 INFO("reading config file\n"); 574 parse_config_file("/init.rc"); 575 576 /* pull the kernel commandline and ramdisk properties file in */ 577 qemu_init(); 578 import_kernel_cmdline(0); 579 580 get_hardware_name(); 581 snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware); 582 parse_config_file(tmp); 583 584 action_for_each_trigger("early-init", action_add_queue_tail); 585 drain_action_queue(); 586 587 INFO("device init\n"); 588 device_init(); 589 590 property_init(); 591 592 // only listen for keychords if ro.debuggable is true 593 keychord_init(); 594 595 if (console[0]) { 596 snprintf(tmp, sizeof(tmp), "/dev/%s", console); 597 console_name = strdup(tmp); 598 } 599 600 fd = open(console_name, O_RDWR); 601 if (fd >= 0) 602 have_console = 1; 603 close(fd); 604 605 if( load_565rle_image(INIT_IMAGE_FILE) ) { 606 fd = open("/dev/tty0", O_WRONLY); 607 if (fd >= 0) { 608 const char *msg; 609 msg = "\n" 610 "\n" 611 "\n" 612 "\n" 613 "\n" 614 "\n" 615 "\n" // console is 40 cols x 30 lines 616 "\n" 617 "\n" 618 "\n" 619 "\n" 620 "\n" 621 "\n" 622 "\n" 623 " A N D R O I D "; 624 write(fd, msg, strlen(msg)); 625 close(fd); 626 } 627 } 628 629 if (qemu[0]) 630 import_kernel_cmdline(1); 631 632 if (!strcmp(bootmode,"factory")) 633 property_set("ro.factorytest", "1"); 634 else if (!strcmp(bootmode,"factory2")) 635 property_set("ro.factorytest", "2"); 636 else 637 property_set("ro.factorytest", "0"); 638 639 property_set("ro.serialno", serialno[0] ? serialno : ""); 640 property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown"); 641 property_set("ro.baseband", baseband[0] ? baseband : "unknown"); 642 property_set("ro.carrier", carrier[0] ? carrier : "unknown"); 643 property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown"); 644 645 property_set("ro.hardware", hardware); 646 snprintf(tmp, PROP_VALUE_MAX, "%d", revision); 647 property_set("ro.revision", tmp); 648 649 /* execute all the boot actions to get us started */ 650 action_for_each_trigger("init", action_add_queue_tail); 651 action_for_each_trigger("early-fs", action_add_queue_tail); 652 action_for_each_trigger("fs", action_add_queue_tail); 653 action_for_each_trigger("post-fs", action_add_queue_tail); 654 drain_action_queue(); 655 656 /* read any property files on system or data and 657 * fire up the property service. This must happen 658 * after the ro.foo properties are set above so 659 * that /data/local.prop cannot interfere with them. 660 */ 661 start_property_service(); 662 663 signal_init(); 664 665 /* make sure we actually have all the pieces we need */ 666 if ((get_device_fd() < 0) || 667 (get_property_set_fd() < 0) || 668 (get_signal_fd() < 0)) { 669 ERROR("init startup failure\n"); 670 return 1; 671 } 672 673 /* execute all the boot actions to get us started */ 674 action_for_each_trigger("early-boot", action_add_queue_tail); 675 action_for_each_trigger("boot", action_add_queue_tail); 676 drain_action_queue(); 677 678 /* run all property triggers based on current state of the properties */ 679 queue_all_property_triggers(); 680 drain_action_queue(); 681 682 /* enable property triggers */ 683 property_triggers_enabled = 1; 684 685 ufds[0].fd = get_device_fd(); 686 ufds[0].events = POLLIN; 687 ufds[1].fd = get_property_set_fd(); 688 ufds[1].events = POLLIN; 689 ufds[2].fd = get_signal_fd(); 690 ufds[2].events = POLLIN; 691 fd_count = 3; 692 693 if (get_keychord_fd() > 0) { 694 ufds[3].fd = get_keychord_fd(); 695 ufds[3].events = POLLIN; 696 fd_count++; 697 } else { 698 ufds[3].events = 0; 699 ufds[3].revents = 0; 700 } 701 702#if BOOTCHART 703 bootchart_count = bootchart_init(); 704 if (bootchart_count < 0) { 705 ERROR("bootcharting init failure\n"); 706 } else if (bootchart_count > 0) { 707 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); 708 } else { 709 NOTICE("bootcharting ignored\n"); 710 } 711#endif 712 713 for(;;) { 714 int nr, i, timeout = -1; 715 716 for (i = 0; i < fd_count; i++) 717 ufds[i].revents = 0; 718 719 drain_action_queue(); 720 restart_processes(); 721 722 if (process_needs_restart) { 723 timeout = (process_needs_restart - gettime()) * 1000; 724 if (timeout < 0) 725 timeout = 0; 726 } 727 728#if BOOTCHART 729 if (bootchart_count > 0) { 730 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) 731 timeout = BOOTCHART_POLLING_MS; 732 if (bootchart_step() < 0 || --bootchart_count == 0) { 733 bootchart_finish(); 734 bootchart_count = 0; 735 } 736 } 737#endif 738 nr = poll(ufds, fd_count, timeout); 739 if (nr <= 0) 740 continue; 741 742 if (ufds[2].revents == POLLIN) { 743 handle_signal(); 744 continue; 745 } 746 747 if (ufds[0].revents == POLLIN) 748 handle_device_fd(); 749 750 if (ufds[1].revents == POLLIN) 751 handle_property_set_fd(); 752 if (ufds[3].revents == POLLIN) 753 handle_keychord(); 754 } 755 756 return 0; 757} 758