init.c revision ff5c802e6ce489a36ac28e3d93b7e8541b3e4d81
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <stdio.h> 18#include <stdlib.h> 19#include <string.h> 20#include <unistd.h> 21#include <fcntl.h> 22#include <ctype.h> 23#include <signal.h> 24#include <sys/wait.h> 25#include <sys/mount.h> 26#include <sys/stat.h> 27#include <sys/poll.h> 28#include <errno.h> 29#include <stdarg.h> 30#include <mtd/mtd-user.h> 31#include <sys/types.h> 32#include <sys/socket.h> 33#include <sys/un.h> 34 35#ifdef HAVE_SELINUX 36#include <sys/mman.h> 37#include <selinux/selinux.h> 38#include <selinux/label.h> 39#endif 40 41#include <libgen.h> 42 43#include <cutils/list.h> 44#include <cutils/sockets.h> 45#include <cutils/iosched_policy.h> 46#include <private/android_filesystem_config.h> 47#include <termios.h> 48 49#include <sys/system_properties.h> 50 51#include "devices.h" 52#include "init.h" 53#include "log.h" 54#include "property_service.h" 55#include "bootchart.h" 56#include "signal_handler.h" 57#include "keychords.h" 58#include "init_parser.h" 59#include "util.h" 60#include "ueventd.h" 61#include "watchdogd.h" 62 63#ifdef HAVE_SELINUX 64struct selabel_handle *sehandle; 65#endif 66 67static int property_triggers_enabled = 0; 68 69#if BOOTCHART 70static int bootchart_count; 71#endif 72 73static char console[32]; 74static char bootmode[32]; 75static char hardware[32]; 76static unsigned revision = 0; 77static char qemu[32]; 78 79#ifdef HAVE_SELINUX 80static int selinux_enabled = 1; 81static int selinux_enforcing = 0; 82#endif 83 84static struct action *cur_action = NULL; 85static struct command *cur_command = NULL; 86static struct listnode *command_queue = NULL; 87 88void notify_service_state(const char *name, const char *state) 89{ 90 char pname[PROP_NAME_MAX]; 91 int len = strlen(name); 92 if ((len + 10) > PROP_NAME_MAX) 93 return; 94 snprintf(pname, sizeof(pname), "init.svc.%s", name); 95 property_set(pname, state); 96} 97 98static int have_console; 99static char *console_name = "/dev/console"; 100static time_t process_needs_restart; 101 102static const char *ENV[32]; 103 104/* add_environment - add "key=value" to the current environment */ 105int add_environment(const char *key, const char *val) 106{ 107 int n; 108 109 for (n = 0; n < 31; n++) { 110 if (!ENV[n]) { 111 size_t len = strlen(key) + strlen(val) + 2; 112 char *entry = malloc(len); 113 snprintf(entry, len, "%s=%s", key, val); 114 ENV[n] = entry; 115 return 0; 116 } 117 } 118 119 return 1; 120} 121 122static void zap_stdio(void) 123{ 124 int fd; 125 fd = open("/dev/null", O_RDWR); 126 dup2(fd, 0); 127 dup2(fd, 1); 128 dup2(fd, 2); 129 close(fd); 130} 131 132static void open_console() 133{ 134 int fd; 135 if ((fd = open(console_name, O_RDWR)) < 0) { 136 fd = open("/dev/null", O_RDWR); 137 } 138 ioctl(fd, TIOCSCTTY, 0); 139 dup2(fd, 0); 140 dup2(fd, 1); 141 dup2(fd, 2); 142 close(fd); 143} 144 145static void publish_socket(const char *name, int fd) 146{ 147 char key[64] = ANDROID_SOCKET_ENV_PREFIX; 148 char val[64]; 149 150 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, 151 name, 152 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); 153 snprintf(val, sizeof(val), "%d", fd); 154 add_environment(key, val); 155 156 /* make sure we don't close-on-exec */ 157 fcntl(fd, F_SETFD, 0); 158} 159 160void service_start(struct service *svc, const char *dynamic_args) 161{ 162 struct stat s; 163 pid_t pid; 164 int needs_console; 165 int n; 166#ifdef HAVE_SELINUX 167 char *scon = NULL; 168 int rc; 169#endif 170 /* starting a service removes it from the disabled or reset 171 * state and immediately takes it out of the restarting 172 * state if it was in there 173 */ 174 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET)); 175 svc->time_started = 0; 176 177 /* running processes require no additional work -- if 178 * they're in the process of exiting, we've ensured 179 * that they will immediately restart on exit, unless 180 * they are ONESHOT 181 */ 182 if (svc->flags & SVC_RUNNING) { 183 return; 184 } 185 186 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; 187 if (needs_console && (!have_console)) { 188 ERROR("service '%s' requires console\n", svc->name); 189 svc->flags |= SVC_DISABLED; 190 return; 191 } 192 193 if (stat(svc->args[0], &s) != 0) { 194 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); 195 svc->flags |= SVC_DISABLED; 196 return; 197 } 198 199 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { 200 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", 201 svc->args[0]); 202 svc->flags |= SVC_DISABLED; 203 return; 204 } 205 206#ifdef HAVE_SELINUX 207 if (is_selinux_enabled() > 0) { 208 char *mycon = NULL, *fcon = NULL; 209 210 INFO("computing context for service '%s'\n", svc->args[0]); 211 rc = getcon(&mycon); 212 if (rc < 0) { 213 ERROR("could not get context while starting '%s'\n", svc->name); 214 return; 215 } 216 217 rc = getfilecon(svc->args[0], &fcon); 218 if (rc < 0) { 219 ERROR("could not get context while starting '%s'\n", svc->name); 220 freecon(mycon); 221 return; 222 } 223 224 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon); 225 freecon(mycon); 226 freecon(fcon); 227 if (rc < 0) { 228 ERROR("could not get context while starting '%s'\n", svc->name); 229 return; 230 } 231 } 232#endif 233 234 NOTICE("starting '%s'\n", svc->name); 235 236 pid = fork(); 237 238 if (pid == 0) { 239 struct socketinfo *si; 240 struct svcenvinfo *ei; 241 char tmp[32]; 242 int fd, sz; 243 244 umask(077); 245 if (properties_inited()) { 246 get_property_workspace(&fd, &sz); 247 sprintf(tmp, "%d,%d", dup(fd), sz); 248 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); 249 } 250 251 for (ei = svc->envvars; ei; ei = ei->next) 252 add_environment(ei->name, ei->value); 253 254#ifdef HAVE_SELINUX 255 setsockcreatecon(scon); 256#endif 257 258 for (si = svc->sockets; si; si = si->next) { 259 int socket_type = ( 260 !strcmp(si->type, "stream") ? SOCK_STREAM : 261 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); 262 int s = create_socket(si->name, socket_type, 263 si->perm, si->uid, si->gid); 264 if (s >= 0) { 265 publish_socket(si->name, s); 266 } 267 } 268 269#ifdef HAVE_SELINUX 270 freecon(scon); 271 scon = NULL; 272 setsockcreatecon(NULL); 273#endif 274 275 if (svc->ioprio_class != IoSchedClass_NONE) { 276 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { 277 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", 278 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); 279 } 280 } 281 282 if (needs_console) { 283 setsid(); 284 open_console(); 285 } else { 286 zap_stdio(); 287 } 288 289#if 0 290 for (n = 0; svc->args[n]; n++) { 291 INFO("args[%d] = '%s'\n", n, svc->args[n]); 292 } 293 for (n = 0; ENV[n]; n++) { 294 INFO("env[%d] = '%s'\n", n, ENV[n]); 295 } 296#endif 297 298 setpgid(0, getpid()); 299 300 /* as requested, set our gid, supplemental gids, and uid */ 301 if (svc->gid) { 302 if (setgid(svc->gid) != 0) { 303 ERROR("setgid failed: %s\n", strerror(errno)); 304 _exit(127); 305 } 306 } 307 if (svc->nr_supp_gids) { 308 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { 309 ERROR("setgroups failed: %s\n", strerror(errno)); 310 _exit(127); 311 } 312 } 313 if (svc->uid) { 314 if (setuid(svc->uid) != 0) { 315 ERROR("setuid failed: %s\n", strerror(errno)); 316 _exit(127); 317 } 318 } 319 320#ifdef HAVE_SELINUX 321 if (svc->seclabel) { 322 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) { 323 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno)); 324 _exit(127); 325 } 326 } 327#endif 328 329 if (!dynamic_args) { 330 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { 331 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); 332 } 333 } else { 334 char *arg_ptrs[INIT_PARSER_MAXARGS+1]; 335 int arg_idx = svc->nargs; 336 char *tmp = strdup(dynamic_args); 337 char *next = tmp; 338 char *bword; 339 340 /* Copy the static arguments */ 341 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); 342 343 while((bword = strsep(&next, " "))) { 344 arg_ptrs[arg_idx++] = bword; 345 if (arg_idx == INIT_PARSER_MAXARGS) 346 break; 347 } 348 arg_ptrs[arg_idx] = '\0'; 349 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); 350 } 351 _exit(127); 352 } 353 354#ifdef HAVE_SELINUX 355 freecon(scon); 356#endif 357 358 if (pid < 0) { 359 ERROR("failed to start '%s'\n", svc->name); 360 svc->pid = 0; 361 return; 362 } 363 364 svc->time_started = gettime(); 365 svc->pid = pid; 366 svc->flags |= SVC_RUNNING; 367 368 if (properties_inited()) 369 notify_service_state(svc->name, "running"); 370} 371 372/* The how field should be either SVC_DISABLED or SVC_RESET */ 373static void service_stop_or_reset(struct service *svc, int how) 374{ 375 /* we are no longer running, nor should we 376 * attempt to restart 377 */ 378 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING)); 379 380 if ((how != SVC_DISABLED) && (how != SVC_RESET)) { 381 /* Hrm, an illegal flag. Default to SVC_DISABLED */ 382 how = SVC_DISABLED; 383 } 384 /* if the service has not yet started, prevent 385 * it from auto-starting with its class 386 */ 387 if (how == SVC_RESET) { 388 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; 389 } else { 390 svc->flags |= how; 391 } 392 393 if (svc->pid) { 394 NOTICE("service '%s' is being killed\n", svc->name); 395 kill(-svc->pid, SIGKILL); 396 notify_service_state(svc->name, "stopping"); 397 } else { 398 notify_service_state(svc->name, "stopped"); 399 } 400} 401 402void service_reset(struct service *svc) 403{ 404 service_stop_or_reset(svc, SVC_RESET); 405} 406 407void service_stop(struct service *svc) 408{ 409 service_stop_or_reset(svc, SVC_DISABLED); 410} 411 412void property_changed(const char *name, const char *value) 413{ 414 if (property_triggers_enabled) 415 queue_property_triggers(name, value); 416} 417 418static void restart_service_if_needed(struct service *svc) 419{ 420 time_t next_start_time = svc->time_started + 5; 421 422 if (next_start_time <= gettime()) { 423 svc->flags &= (~SVC_RESTARTING); 424 service_start(svc, NULL); 425 return; 426 } 427 428 if ((next_start_time < process_needs_restart) || 429 (process_needs_restart == 0)) { 430 process_needs_restart = next_start_time; 431 } 432} 433 434static void restart_processes() 435{ 436 process_needs_restart = 0; 437 service_for_each_flags(SVC_RESTARTING, 438 restart_service_if_needed); 439} 440 441static void msg_start(const char *name) 442{ 443 struct service *svc; 444 char *tmp = NULL; 445 char *args = NULL; 446 447 if (!strchr(name, ':')) 448 svc = service_find_by_name(name); 449 else { 450 tmp = strdup(name); 451 args = strchr(tmp, ':'); 452 *args = '\0'; 453 args++; 454 455 svc = service_find_by_name(tmp); 456 } 457 458 if (svc) { 459 service_start(svc, args); 460 } else { 461 ERROR("no such service '%s'\n", name); 462 } 463 if (tmp) 464 free(tmp); 465} 466 467static void msg_stop(const char *name) 468{ 469 struct service *svc = service_find_by_name(name); 470 471 if (svc) { 472 service_stop(svc); 473 } else { 474 ERROR("no such service '%s'\n", name); 475 } 476} 477 478void handle_control_message(const char *msg, const char *arg) 479{ 480 if (!strcmp(msg,"start")) { 481 msg_start(arg); 482 } else if (!strcmp(msg,"stop")) { 483 msg_stop(arg); 484 } else if (!strcmp(msg,"restart")) { 485 msg_stop(arg); 486 msg_start(arg); 487 } else { 488 ERROR("unknown control msg '%s'\n", msg); 489 } 490} 491 492static struct command *get_first_command(struct action *act) 493{ 494 struct listnode *node; 495 node = list_head(&act->commands); 496 if (!node || list_empty(&act->commands)) 497 return NULL; 498 499 return node_to_item(node, struct command, clist); 500} 501 502static struct command *get_next_command(struct action *act, struct command *cmd) 503{ 504 struct listnode *node; 505 node = cmd->clist.next; 506 if (!node) 507 return NULL; 508 if (node == &act->commands) 509 return NULL; 510 511 return node_to_item(node, struct command, clist); 512} 513 514static int is_last_command(struct action *act, struct command *cmd) 515{ 516 return (list_tail(&act->commands) == &cmd->clist); 517} 518 519void execute_one_command(void) 520{ 521 int ret; 522 523 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) { 524 cur_action = action_remove_queue_head(); 525 cur_command = NULL; 526 if (!cur_action) 527 return; 528 INFO("processing action %p (%s)\n", cur_action, cur_action->name); 529 cur_command = get_first_command(cur_action); 530 } else { 531 cur_command = get_next_command(cur_action, cur_command); 532 } 533 534 if (!cur_command) 535 return; 536 537 ret = cur_command->func(cur_command->nargs, cur_command->args); 538 INFO("command '%s' r=%d\n", cur_command->args[0], ret); 539} 540 541static int wait_for_coldboot_done_action(int nargs, char **args) 542{ 543 int ret; 544 INFO("wait for %s\n", coldboot_done); 545 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT); 546 if (ret) 547 ERROR("Timed out waiting for %s\n", coldboot_done); 548 return ret; 549} 550 551static int keychord_init_action(int nargs, char **args) 552{ 553 keychord_init(); 554 return 0; 555} 556 557static int console_init_action(int nargs, char **args) 558{ 559 int fd; 560 char tmp[PROP_VALUE_MAX]; 561 562 if (console[0]) { 563 snprintf(tmp, sizeof(tmp), "/dev/%s", console); 564 console_name = strdup(tmp); 565 } 566 567 fd = open(console_name, O_RDWR); 568 if (fd >= 0) 569 have_console = 1; 570 close(fd); 571 572 if( load_565rle_image(INIT_IMAGE_FILE) ) { 573 fd = open("/dev/tty0", O_WRONLY); 574 if (fd >= 0) { 575 const char *msg; 576 msg = "\n" 577 "\n" 578 "\n" 579 "\n" 580 "\n" 581 "\n" 582 "\n" // console is 40 cols x 30 lines 583 "\n" 584 "\n" 585 "\n" 586 "\n" 587 "\n" 588 "\n" 589 "\n" 590 " A N D R O I D "; 591 write(fd, msg, strlen(msg)); 592 close(fd); 593 } 594 } 595 return 0; 596} 597 598static void import_kernel_nv(char *name, int for_emulator) 599{ 600 char *value = strchr(name, '='); 601 int name_len = strlen(name); 602 603 if (value == 0) return; 604 *value++ = 0; 605 if (name_len == 0) return; 606 607#ifdef HAVE_SELINUX 608 if (!strcmp(name,"enforcing")) { 609 selinux_enforcing = atoi(value); 610 } else if (!strcmp(name,"selinux")) { 611 selinux_enabled = atoi(value); 612 } 613#endif 614 615 if (for_emulator) { 616 /* in the emulator, export any kernel option with the 617 * ro.kernel. prefix */ 618 char buff[PROP_NAME_MAX]; 619 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); 620 621 if (len < (int)sizeof(buff)) 622 property_set( buff, value ); 623 return; 624 } 625 626 if (!strcmp(name,"qemu")) { 627 strlcpy(qemu, value, sizeof(qemu)); 628 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) { 629 const char *boot_prop_name = name + 12; 630 char prop[PROP_NAME_MAX]; 631 int cnt; 632 633 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name); 634 if (cnt < PROP_NAME_MAX) 635 property_set(prop, value); 636 } 637} 638 639static void export_kernel_boot_props(void) 640{ 641 char tmp[PROP_VALUE_MAX]; 642 const char *pval; 643 unsigned i; 644 struct { 645 const char *src_prop; 646 const char *dest_prop; 647 const char *def_val; 648 } prop_map[] = { 649 { "ro.boot.serialno", "ro.serialno", "", }, 650 { "ro.boot.mode", "ro.bootmode", "unknown", }, 651 { "ro.boot.baseband", "ro.baseband", "unknown", }, 652 { "ro.boot.bootloader", "ro.bootloader", "unknown", }, 653 }; 654 655 for (i = 0; i < ARRAY_SIZE(prop_map); i++) { 656 pval = property_get(prop_map[i].src_prop); 657 property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val); 658 } 659 660 pval = property_get("ro.boot.console"); 661 if (pval) 662 strlcpy(console, pval, sizeof(console)); 663 664 /* save a copy for init's usage during boot */ 665 strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode)); 666 667 /* if this was given on kernel command line, override what we read 668 * before (e.g. from /proc/cpuinfo), if anything */ 669 pval = property_get("ro.boot.hardware"); 670 if (pval) 671 strlcpy(hardware, pval, sizeof(hardware)); 672 property_set("ro.hardware", hardware); 673 674 snprintf(tmp, PROP_VALUE_MAX, "%d", revision); 675 property_set("ro.revision", tmp); 676 677 /* TODO: these are obsolete. We should delete them */ 678 if (!strcmp(bootmode,"factory")) 679 property_set("ro.factorytest", "1"); 680 else if (!strcmp(bootmode,"factory2")) 681 property_set("ro.factorytest", "2"); 682 else 683 property_set("ro.factorytest", "0"); 684} 685 686static void process_kernel_cmdline(void) 687{ 688 /* don't expose the raw commandline to nonpriv processes */ 689 chmod("/proc/cmdline", 0440); 690 691 /* first pass does the common stuff, and finds if we are in qemu. 692 * second pass is only necessary for qemu to export all kernel params 693 * as props. 694 */ 695 import_kernel_cmdline(0, import_kernel_nv); 696 if (qemu[0]) 697 import_kernel_cmdline(1, import_kernel_nv); 698 699 /* now propogate the info given on command line to internal variables 700 * used by init as well as the current required properties 701 */ 702 export_kernel_boot_props(); 703} 704 705static int property_service_init_action(int nargs, char **args) 706{ 707 /* read any property files on system or data and 708 * fire up the property service. This must happen 709 * after the ro.foo properties are set above so 710 * that /data/local.prop cannot interfere with them. 711 */ 712 start_property_service(); 713 return 0; 714} 715 716static int signal_init_action(int nargs, char **args) 717{ 718 signal_init(); 719 return 0; 720} 721 722static int check_startup_action(int nargs, char **args) 723{ 724 /* make sure we actually have all the pieces we need */ 725 if ((get_property_set_fd() < 0) || 726 (get_signal_fd() < 0)) { 727 ERROR("init startup failure\n"); 728 exit(1); 729 } 730 731 /* signal that we hit this point */ 732 unlink("/dev/.booting"); 733 734 return 0; 735} 736 737static int queue_property_triggers_action(int nargs, char **args) 738{ 739 queue_all_property_triggers(); 740 /* enable property triggers */ 741 property_triggers_enabled = 1; 742 return 0; 743} 744 745#if BOOTCHART 746static int bootchart_init_action(int nargs, char **args) 747{ 748 bootchart_count = bootchart_init(); 749 if (bootchart_count < 0) { 750 ERROR("bootcharting init failure\n"); 751 } else if (bootchart_count > 0) { 752 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); 753 } else { 754 NOTICE("bootcharting ignored\n"); 755 } 756 757 return 0; 758} 759#endif 760 761#ifdef HAVE_SELINUX 762void selinux_load_policy(void) 763{ 764 const char path_prefix[] = "/sepolicy"; 765 struct selinux_opt seopts[] = { 766 { SELABEL_OPT_PATH, "/file_contexts" } 767 }; 768 char path[PATH_MAX]; 769 int fd, rc, vers; 770 struct stat sb; 771 void *map; 772 773 sehandle = NULL; 774 if (!selinux_enabled) { 775 INFO("SELinux: Disabled by command line option\n"); 776 return; 777 } 778 779 mkdir(SELINUXMNT, 0755); 780 if (mount("selinuxfs", SELINUXMNT, "selinuxfs", 0, NULL)) { 781 if (errno == ENODEV) { 782 /* SELinux not enabled in kernel */ 783 return; 784 } 785 ERROR("SELinux: Could not mount selinuxfs: %s\n", 786 strerror(errno)); 787 return; 788 } 789 set_selinuxmnt(SELINUXMNT); 790 791 vers = security_policyvers(); 792 if (vers <= 0) { 793 ERROR("SELinux: Unable to read policy version\n"); 794 return; 795 } 796 INFO("SELinux: Maximum supported policy version: %d\n", vers); 797 798 snprintf(path, sizeof(path), "%s.%d", 799 path_prefix, vers); 800 fd = open(path, O_RDONLY); 801 while (fd < 0 && errno == ENOENT && --vers) { 802 snprintf(path, sizeof(path), "%s.%d", 803 path_prefix, vers); 804 fd = open(path, O_RDONLY); 805 } 806 if (fd < 0) { 807 ERROR("SELinux: Could not open %s: %s\n", 808 path, strerror(errno)); 809 return; 810 } 811 if (fstat(fd, &sb) < 0) { 812 ERROR("SELinux: Could not stat %s: %s\n", 813 path, strerror(errno)); 814 return; 815 } 816 map = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 817 if (map == MAP_FAILED) { 818 ERROR("SELinux: Could not map %s: %s\n", 819 path, strerror(errno)); 820 return; 821 } 822 823 rc = security_load_policy(map, sb.st_size); 824 if (rc < 0) { 825 ERROR("SELinux: Could not load policy: %s\n", 826 strerror(errno)); 827 return; 828 } 829 830 rc = security_setenforce(selinux_enforcing); 831 if (rc < 0) { 832 ERROR("SELinux: Could not set enforcing mode to %s: %s\n", 833 selinux_enforcing ? "enforcing" : "permissive", strerror(errno)); 834 return; 835 } 836 837 munmap(map, sb.st_size); 838 close(fd); 839 INFO("SELinux: Loaded policy from %s\n", path); 840 841 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); 842 if (!sehandle) { 843 ERROR("SELinux: Could not load file_contexts: %s\n", 844 strerror(errno)); 845 return; 846 } 847 INFO("SELinux: Loaded file contexts from %s\n", seopts[0].value); 848 return; 849} 850#endif 851 852int main(int argc, char **argv) 853{ 854 int fd_count = 0; 855 struct pollfd ufds[4]; 856 char *tmpdev; 857 char* debuggable; 858 char tmp[32]; 859 int property_set_fd_init = 0; 860 int signal_fd_init = 0; 861 int keychord_fd_init = 0; 862 bool is_charger = false; 863 864 if (!strcmp(basename(argv[0]), "ueventd")) 865 return ueventd_main(argc, argv); 866 867 if (!strcmp(basename(argv[0]), "watchdogd")) 868 return watchdogd_main(argc, argv); 869 870 /* clear the umask */ 871 umask(0); 872 873 /* Get the basic filesystem setup we need put 874 * together in the initramdisk on / and then we'll 875 * let the rc file figure out the rest. 876 */ 877 mkdir("/dev", 0755); 878 mkdir("/proc", 0755); 879 mkdir("/sys", 0755); 880 881 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); 882 mkdir("/dev/pts", 0755); 883 mkdir("/dev/socket", 0755); 884 mount("devpts", "/dev/pts", "devpts", 0, NULL); 885 mount("proc", "/proc", "proc", 0, NULL); 886 mount("sysfs", "/sys", "sysfs", 0, NULL); 887 888 /* indicate that booting is in progress to background fw loaders, etc */ 889 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000)); 890 891 /* We must have some place other than / to create the 892 * device nodes for kmsg and null, otherwise we won't 893 * be able to remount / read-only later on. 894 * Now that tmpfs is mounted on /dev, we can actually 895 * talk to the outside world. 896 */ 897 open_devnull_stdio(); 898 klog_init(); 899 property_init(); 900 901 get_hardware_name(hardware, &revision); 902 903 process_kernel_cmdline(); 904 905#ifdef HAVE_SELINUX 906 INFO("loading selinux policy\n"); 907 selinux_load_policy(); 908 /* These directories were necessarily created before policy load 909 * and therefore need their security context restored to the proper value. 910 * This must happen before /dev is populated by ueventd. 911 */ 912 restorecon("/dev"); 913 restorecon("/dev/socket"); 914#endif 915 916 is_charger = !strcmp(bootmode, "charger"); 917 918 INFO("property init\n"); 919 if (!is_charger) 920 property_load_boot_defaults(); 921 922 INFO("reading config file\n"); 923 init_parse_config_file("/init.rc"); 924 925 action_for_each_trigger("early-init", action_add_queue_tail); 926 927 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done"); 928 queue_builtin_action(keychord_init_action, "keychord_init"); 929 queue_builtin_action(console_init_action, "console_init"); 930 931 /* execute all the boot actions to get us started */ 932 action_for_each_trigger("init", action_add_queue_tail); 933 934 /* skip mounting filesystems in charger mode */ 935 if (!is_charger) { 936 action_for_each_trigger("early-fs", action_add_queue_tail); 937 action_for_each_trigger("fs", action_add_queue_tail); 938 action_for_each_trigger("post-fs", action_add_queue_tail); 939 action_for_each_trigger("post-fs-data", action_add_queue_tail); 940 } 941 942 queue_builtin_action(property_service_init_action, "property_service_init"); 943 queue_builtin_action(signal_init_action, "signal_init"); 944 queue_builtin_action(check_startup_action, "check_startup"); 945 946 if (is_charger) { 947 action_for_each_trigger("charger", action_add_queue_tail); 948 } else { 949 action_for_each_trigger("early-boot", action_add_queue_tail); 950 action_for_each_trigger("boot", action_add_queue_tail); 951 } 952 953 /* run all property triggers based on current state of the properties */ 954 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers"); 955 956 957#if BOOTCHART 958 queue_builtin_action(bootchart_init_action, "bootchart_init"); 959#endif 960 961 for(;;) { 962 int nr, i, timeout = -1; 963 964 execute_one_command(); 965 restart_processes(); 966 967 if (!property_set_fd_init && get_property_set_fd() > 0) { 968 ufds[fd_count].fd = get_property_set_fd(); 969 ufds[fd_count].events = POLLIN; 970 ufds[fd_count].revents = 0; 971 fd_count++; 972 property_set_fd_init = 1; 973 } 974 if (!signal_fd_init && get_signal_fd() > 0) { 975 ufds[fd_count].fd = get_signal_fd(); 976 ufds[fd_count].events = POLLIN; 977 ufds[fd_count].revents = 0; 978 fd_count++; 979 signal_fd_init = 1; 980 } 981 if (!keychord_fd_init && get_keychord_fd() > 0) { 982 ufds[fd_count].fd = get_keychord_fd(); 983 ufds[fd_count].events = POLLIN; 984 ufds[fd_count].revents = 0; 985 fd_count++; 986 keychord_fd_init = 1; 987 } 988 989 if (process_needs_restart) { 990 timeout = (process_needs_restart - gettime()) * 1000; 991 if (timeout < 0) 992 timeout = 0; 993 } 994 995 if (!action_queue_empty() || cur_action) 996 timeout = 0; 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 1009 nr = poll(ufds, fd_count, timeout); 1010 if (nr <= 0) 1011 continue; 1012 1013 for (i = 0; i < fd_count; i++) { 1014 if (ufds[i].revents == POLLIN) { 1015 if (ufds[i].fd == get_property_set_fd()) 1016 handle_property_set_fd(); 1017 else if (ufds[i].fd == get_keychord_fd()) 1018 handle_keychord(); 1019 else if (ufds[i].fd == get_signal_fd()) 1020 handle_signal(); 1021 } 1022 } 1023 } 1024 1025 return 0; 1026} 1027