init.c revision e4b7b294f37d9b64d6b7c1931e2c9bfb1a500d68
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 <selinux/selinux.h> 36#include <selinux/label.h> 37#include <selinux/android.h> 38 39#include <libgen.h> 40 41#include <cutils/list.h> 42#include <cutils/android_reboot.h> 43#include <cutils/sockets.h> 44#include <cutils/iosched_policy.h> 45#include <cutils/fs.h> 46#include <private/android_filesystem_config.h> 47#include <termios.h> 48 49#include "devices.h" 50#include "init.h" 51#include "log.h" 52#include "property_service.h" 53#include "bootchart.h" 54#include "signal_handler.h" 55#include "keychords.h" 56#include "init_parser.h" 57#include "util.h" 58#include "ueventd.h" 59#include "watchdogd.h" 60 61struct selabel_handle *sehandle; 62struct selabel_handle *sehandle_prop; 63 64static int property_triggers_enabled = 0; 65 66#if BOOTCHART 67static int bootchart_count; 68#endif 69 70static char console[32]; 71static char bootmode[32]; 72static char hardware[32]; 73static unsigned revision = 0; 74static char qemu[32]; 75 76static struct action *cur_action = NULL; 77static struct command *cur_command = NULL; 78static struct listnode *command_queue = NULL; 79 80void notify_service_state(const char *name, const char *state) 81{ 82 char pname[PROP_NAME_MAX]; 83 int len = strlen(name); 84 if ((len + 10) > PROP_NAME_MAX) 85 return; 86 snprintf(pname, sizeof(pname), "init.svc.%s", name); 87 property_set(pname, state); 88} 89 90static int have_console; 91static char console_name[PROP_VALUE_MAX] = "/dev/console"; 92static time_t process_needs_restart; 93 94static const char *ENV[32]; 95 96/* add_environment - add "key=value" to the current environment */ 97int add_environment(const char *key, const char *val) 98{ 99 int n; 100 101 for (n = 0; n < 31; n++) { 102 if (!ENV[n]) { 103 size_t len = strlen(key) + strlen(val) + 2; 104 char *entry = malloc(len); 105 snprintf(entry, len, "%s=%s", key, val); 106 ENV[n] = entry; 107 return 0; 108 } 109 } 110 111 return 1; 112} 113 114static void zap_stdio(void) 115{ 116 int fd; 117 fd = open("/dev/null", O_RDWR); 118 dup2(fd, 0); 119 dup2(fd, 1); 120 dup2(fd, 2); 121 close(fd); 122} 123 124static void open_console() 125{ 126 int fd; 127 if ((fd = open(console_name, O_RDWR)) < 0) { 128 fd = open("/dev/null", O_RDWR); 129 } 130 ioctl(fd, TIOCSCTTY, 0); 131 dup2(fd, 0); 132 dup2(fd, 1); 133 dup2(fd, 2); 134 close(fd); 135} 136 137static void publish_socket(const char *name, int fd) 138{ 139 char key[64] = ANDROID_SOCKET_ENV_PREFIX; 140 char val[64]; 141 142 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1, 143 name, 144 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX)); 145 snprintf(val, sizeof(val), "%d", fd); 146 add_environment(key, val); 147 148 /* make sure we don't close-on-exec */ 149 fcntl(fd, F_SETFD, 0); 150} 151 152void service_start(struct service *svc, const char *dynamic_args) 153{ 154 struct stat s; 155 pid_t pid; 156 int needs_console; 157 int n; 158 char *scon = NULL; 159 int rc; 160 161 /* starting a service removes it from the disabled or reset 162 * state and immediately takes it out of the restarting 163 * state if it was in there 164 */ 165 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START)); 166 svc->time_started = 0; 167 168 /* running processes require no additional work -- if 169 * they're in the process of exiting, we've ensured 170 * that they will immediately restart on exit, unless 171 * they are ONESHOT 172 */ 173 if (svc->flags & SVC_RUNNING) { 174 return; 175 } 176 177 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0; 178 if (needs_console && (!have_console)) { 179 ERROR("service '%s' requires console\n", svc->name); 180 svc->flags |= SVC_DISABLED; 181 return; 182 } 183 184 if (stat(svc->args[0], &s) != 0) { 185 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name); 186 svc->flags |= SVC_DISABLED; 187 return; 188 } 189 190 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) { 191 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n", 192 svc->args[0]); 193 svc->flags |= SVC_DISABLED; 194 return; 195 } 196 197 if (is_selinux_enabled() > 0) { 198 if (svc->seclabel) { 199 scon = strdup(svc->seclabel); 200 if (!scon) { 201 ERROR("Out of memory while starting '%s'\n", svc->name); 202 return; 203 } 204 } else { 205 char *mycon = NULL, *fcon = NULL; 206 207 INFO("computing context for service '%s'\n", svc->args[0]); 208 rc = getcon(&mycon); 209 if (rc < 0) { 210 ERROR("could not get context while starting '%s'\n", svc->name); 211 return; 212 } 213 214 rc = getfilecon(svc->args[0], &fcon); 215 if (rc < 0) { 216 ERROR("could not get context while starting '%s'\n", svc->name); 217 freecon(mycon); 218 return; 219 } 220 221 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon); 222 if (rc == 0 && !strcmp(scon, mycon)) { 223 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name); 224 } 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 } 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 for (si = svc->sockets; si; si = si->next) { 255 int socket_type = ( 256 !strcmp(si->type, "stream") ? SOCK_STREAM : 257 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET)); 258 int s = create_socket(si->name, socket_type, 259 si->perm, si->uid, si->gid, si->socketcon ?: scon); 260 if (s >= 0) { 261 publish_socket(si->name, s); 262 } 263 } 264 265 freecon(scon); 266 scon = NULL; 267 268 if (svc->ioprio_class != IoSchedClass_NONE) { 269 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { 270 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", 271 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); 272 } 273 } 274 275 if (needs_console) { 276 setsid(); 277 open_console(); 278 } else { 279 zap_stdio(); 280 } 281 282#if 0 283 for (n = 0; svc->args[n]; n++) { 284 INFO("args[%d] = '%s'\n", n, svc->args[n]); 285 } 286 for (n = 0; ENV[n]; n++) { 287 INFO("env[%d] = '%s'\n", n, ENV[n]); 288 } 289#endif 290 291 setpgid(0, getpid()); 292 293 /* as requested, set our gid, supplemental gids, and uid */ 294 if (svc->gid) { 295 if (setgid(svc->gid) != 0) { 296 ERROR("setgid failed: %s\n", strerror(errno)); 297 _exit(127); 298 } 299 } 300 if (svc->nr_supp_gids) { 301 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) { 302 ERROR("setgroups failed: %s\n", strerror(errno)); 303 _exit(127); 304 } 305 } 306 if (svc->uid) { 307 if (setuid(svc->uid) != 0) { 308 ERROR("setuid failed: %s\n", strerror(errno)); 309 _exit(127); 310 } 311 } 312 if (svc->seclabel) { 313 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) { 314 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno)); 315 _exit(127); 316 } 317 } 318 319 if (!dynamic_args) { 320 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { 321 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); 322 } 323 } else { 324 char *arg_ptrs[INIT_PARSER_MAXARGS+1]; 325 int arg_idx = svc->nargs; 326 char *tmp = strdup(dynamic_args); 327 char *next = tmp; 328 char *bword; 329 330 /* Copy the static arguments */ 331 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); 332 333 while((bword = strsep(&next, " "))) { 334 arg_ptrs[arg_idx++] = bword; 335 if (arg_idx == INIT_PARSER_MAXARGS) 336 break; 337 } 338 arg_ptrs[arg_idx] = '\0'; 339 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); 340 } 341 _exit(127); 342 } 343 344 freecon(scon); 345 346 if (pid < 0) { 347 ERROR("failed to start '%s'\n", svc->name); 348 svc->pid = 0; 349 return; 350 } 351 352 svc->time_started = gettime(); 353 svc->pid = pid; 354 svc->flags |= SVC_RUNNING; 355 356 if (properties_inited()) 357 notify_service_state(svc->name, "running"); 358} 359 360/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */ 361static void service_stop_or_reset(struct service *svc, int how) 362{ 363 /* The service is still SVC_RUNNING until its process exits, but if it has 364 * already exited it shoudn't attempt a restart yet. */ 365 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START); 366 367 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) { 368 /* Hrm, an illegal flag. Default to SVC_DISABLED */ 369 how = SVC_DISABLED; 370 } 371 /* if the service has not yet started, prevent 372 * it from auto-starting with its class 373 */ 374 if (how == SVC_RESET) { 375 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; 376 } else { 377 svc->flags |= how; 378 } 379 380 if (svc->pid) { 381 NOTICE("service '%s' is being killed\n", svc->name); 382 kill(-svc->pid, SIGKILL); 383 notify_service_state(svc->name, "stopping"); 384 } else { 385 notify_service_state(svc->name, "stopped"); 386 } 387} 388 389void service_reset(struct service *svc) 390{ 391 service_stop_or_reset(svc, SVC_RESET); 392} 393 394void service_stop(struct service *svc) 395{ 396 service_stop_or_reset(svc, SVC_DISABLED); 397} 398 399void service_restart(struct service *svc) 400{ 401 if (svc->flags & SVC_RUNNING) { 402 /* Stop, wait, then start the service. */ 403 service_stop_or_reset(svc, SVC_RESTART); 404 } else if (!(svc->flags & SVC_RESTARTING)) { 405 /* Just start the service since it's not running. */ 406 service_start(svc, NULL); 407 } /* else: Service is restarting anyways. */ 408} 409 410void property_changed(const char *name, const char *value) 411{ 412 if (property_triggers_enabled) 413 queue_property_triggers(name, value); 414} 415 416static void restart_service_if_needed(struct service *svc) 417{ 418 time_t next_start_time = svc->time_started + 5; 419 420 if (next_start_time <= gettime()) { 421 svc->flags &= (~SVC_RESTARTING); 422 service_start(svc, NULL); 423 return; 424 } 425 426 if ((next_start_time < process_needs_restart) || 427 (process_needs_restart == 0)) { 428 process_needs_restart = next_start_time; 429 } 430} 431 432static void restart_processes() 433{ 434 process_needs_restart = 0; 435 service_for_each_flags(SVC_RESTARTING, 436 restart_service_if_needed); 437} 438 439static void msg_start(const char *name) 440{ 441 struct service *svc = NULL; 442 char *tmp = NULL; 443 char *args = NULL; 444 445 if (!strchr(name, ':')) 446 svc = service_find_by_name(name); 447 else { 448 tmp = strdup(name); 449 if (tmp) { 450 args = strchr(tmp, ':'); 451 *args = '\0'; 452 args++; 453 454 svc = service_find_by_name(tmp); 455 } 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 478static void msg_restart(const char *name) 479{ 480 struct service *svc = service_find_by_name(name); 481 482 if (svc) { 483 service_restart(svc); 484 } else { 485 ERROR("no such service '%s'\n", name); 486 } 487} 488 489void handle_control_message(const char *msg, const char *arg) 490{ 491 if (!strcmp(msg,"start")) { 492 msg_start(arg); 493 } else if (!strcmp(msg,"stop")) { 494 msg_stop(arg); 495 } else if (!strcmp(msg,"restart")) { 496 msg_restart(arg); 497 } else { 498 ERROR("unknown control msg '%s'\n", msg); 499 } 500} 501 502static struct command *get_first_command(struct action *act) 503{ 504 struct listnode *node; 505 node = list_head(&act->commands); 506 if (!node || list_empty(&act->commands)) 507 return NULL; 508 509 return node_to_item(node, struct command, clist); 510} 511 512static struct command *get_next_command(struct action *act, struct command *cmd) 513{ 514 struct listnode *node; 515 node = cmd->clist.next; 516 if (!node) 517 return NULL; 518 if (node == &act->commands) 519 return NULL; 520 521 return node_to_item(node, struct command, clist); 522} 523 524static int is_last_command(struct action *act, struct command *cmd) 525{ 526 return (list_tail(&act->commands) == &cmd->clist); 527} 528 529void execute_one_command(void) 530{ 531 int ret; 532 533 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) { 534 cur_action = action_remove_queue_head(); 535 cur_command = NULL; 536 if (!cur_action) 537 return; 538 INFO("processing action %p (%s)\n", cur_action, cur_action->name); 539 cur_command = get_first_command(cur_action); 540 } else { 541 cur_command = get_next_command(cur_action, cur_command); 542 } 543 544 if (!cur_command) 545 return; 546 547 ret = cur_command->func(cur_command->nargs, cur_command->args); 548 INFO("command '%s' r=%d\n", cur_command->args[0], ret); 549} 550 551static int wait_for_coldboot_done_action(int nargs, char **args) 552{ 553 int ret; 554 INFO("wait for %s\n", coldboot_done); 555 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT); 556 if (ret) 557 ERROR("Timed out waiting for %s\n", coldboot_done); 558 return ret; 559} 560 561/* 562 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed 563 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom. 564 * Does nothing if Hardware RNG is not present. 565 * 566 * Since we don't yet trust the quality of Hardware RNG, these bytes are not 567 * mixed into the primary pool of Linux RNG and the entropy estimate is left 568 * unmodified. 569 * 570 * If the HW RNG device /dev/hw_random is present, we require that at least 571 * 512 bytes read from it are written into Linux RNG. QA is expected to catch 572 * devices/configurations where these I/O operations are blocking for a long 573 * time. We do not reboot or halt on failures, as this is a best-effort 574 * attempt. 575 */ 576static int mix_hwrng_into_linux_rng_action(int nargs, char **args) 577{ 578 int result = -1; 579 int hwrandom_fd = -1; 580 int urandom_fd = -1; 581 char buf[512]; 582 ssize_t chunk_size; 583 size_t total_bytes_written = 0; 584 585 hwrandom_fd = TEMP_FAILURE_RETRY( 586 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW)); 587 if (hwrandom_fd == -1) { 588 if (errno == ENOENT) { 589 ERROR("/dev/hw_random not found\n"); 590 /* It's not an error to not have a Hardware RNG. */ 591 result = 0; 592 } else { 593 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno)); 594 } 595 goto ret; 596 } 597 598 urandom_fd = TEMP_FAILURE_RETRY( 599 open("/dev/urandom", O_WRONLY | O_NOFOLLOW)); 600 if (urandom_fd == -1) { 601 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno)); 602 goto ret; 603 } 604 605 while (total_bytes_written < sizeof(buf)) { 606 chunk_size = TEMP_FAILURE_RETRY( 607 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written)); 608 if (chunk_size == -1) { 609 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno)); 610 goto ret; 611 } else if (chunk_size == 0) { 612 ERROR("Failed to read from /dev/hw_random: EOF\n"); 613 goto ret; 614 } 615 616 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size)); 617 if (chunk_size == -1) { 618 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno)); 619 goto ret; 620 } 621 total_bytes_written += chunk_size; 622 } 623 624 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom", 625 total_bytes_written); 626 result = 0; 627 628ret: 629 if (hwrandom_fd != -1) { 630 close(hwrandom_fd); 631 } 632 if (urandom_fd != -1) { 633 close(urandom_fd); 634 } 635 memset(buf, 0, sizeof(buf)); 636 return result; 637} 638 639static int keychord_init_action(int nargs, char **args) 640{ 641 keychord_init(); 642 return 0; 643} 644 645static int console_init_action(int nargs, char **args) 646{ 647 int fd; 648 649 if (console[0]) { 650 snprintf(console_name, sizeof(console_name), "/dev/%s", console); 651 } 652 653 fd = open(console_name, O_RDWR); 654 if (fd >= 0) 655 have_console = 1; 656 close(fd); 657 658 fd = open("/dev/tty0", O_WRONLY); 659 if (fd >= 0) { 660 const char *msg; 661 msg = "\n" 662 "\n" 663 "\n" 664 "\n" 665 "\n" 666 "\n" 667 "\n" // console is 40 cols x 30 lines 668 "\n" 669 "\n" 670 "\n" 671 "\n" 672 "\n" 673 "\n" 674 "\n" 675 " A N D R O I D "; 676 write(fd, msg, strlen(msg)); 677 close(fd); 678 } 679 680 return 0; 681} 682 683static void import_kernel_nv(char *name, int for_emulator) 684{ 685 char *value = strchr(name, '='); 686 int name_len = strlen(name); 687 688 if (value == 0) return; 689 *value++ = 0; 690 if (name_len == 0) return; 691 692 if (for_emulator) { 693 /* in the emulator, export any kernel option with the 694 * ro.kernel. prefix */ 695 char buff[PROP_NAME_MAX]; 696 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); 697 698 if (len < (int)sizeof(buff)) 699 property_set( buff, value ); 700 return; 701 } 702 703 if (!strcmp(name,"qemu")) { 704 strlcpy(qemu, value, sizeof(qemu)); 705 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) { 706 const char *boot_prop_name = name + 12; 707 char prop[PROP_NAME_MAX]; 708 int cnt; 709 710 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name); 711 if (cnt < PROP_NAME_MAX) 712 property_set(prop, value); 713 } 714} 715 716static void export_kernel_boot_props(void) 717{ 718 char tmp[PROP_VALUE_MAX]; 719 int ret; 720 unsigned i; 721 struct { 722 const char *src_prop; 723 const char *dest_prop; 724 const char *def_val; 725 } prop_map[] = { 726 { "ro.boot.serialno", "ro.serialno", "", }, 727 { "ro.boot.mode", "ro.bootmode", "unknown", }, 728 { "ro.boot.baseband", "ro.baseband", "unknown", }, 729 { "ro.boot.bootloader", "ro.bootloader", "unknown", }, 730 }; 731 732 for (i = 0; i < ARRAY_SIZE(prop_map); i++) { 733 ret = property_get(prop_map[i].src_prop, tmp); 734 if (ret > 0) 735 property_set(prop_map[i].dest_prop, tmp); 736 else 737 property_set(prop_map[i].dest_prop, prop_map[i].def_val); 738 } 739 740 ret = property_get("ro.boot.console", tmp); 741 if (ret) 742 strlcpy(console, tmp, sizeof(console)); 743 744 /* save a copy for init's usage during boot */ 745 property_get("ro.bootmode", tmp); 746 strlcpy(bootmode, tmp, sizeof(bootmode)); 747 748 /* if this was given on kernel command line, override what we read 749 * before (e.g. from /proc/cpuinfo), if anything */ 750 ret = property_get("ro.boot.hardware", tmp); 751 if (ret) 752 strlcpy(hardware, tmp, sizeof(hardware)); 753 property_set("ro.hardware", hardware); 754 755 snprintf(tmp, PROP_VALUE_MAX, "%d", revision); 756 property_set("ro.revision", tmp); 757 758 /* TODO: these are obsolete. We should delete them */ 759 if (!strcmp(bootmode,"factory")) 760 property_set("ro.factorytest", "1"); 761 else if (!strcmp(bootmode,"factory2")) 762 property_set("ro.factorytest", "2"); 763 else 764 property_set("ro.factorytest", "0"); 765} 766 767static void process_kernel_cmdline(void) 768{ 769 /* don't expose the raw commandline to nonpriv processes */ 770 chmod("/proc/cmdline", 0440); 771 772 /* first pass does the common stuff, and finds if we are in qemu. 773 * second pass is only necessary for qemu to export all kernel params 774 * as props. 775 */ 776 import_kernel_cmdline(0, import_kernel_nv); 777 if (qemu[0]) 778 import_kernel_cmdline(1, import_kernel_nv); 779 780 /* now propogate the info given on command line to internal variables 781 * used by init as well as the current required properties 782 */ 783 export_kernel_boot_props(); 784} 785 786static int property_service_init_action(int nargs, char **args) 787{ 788 /* read any property files on system or data and 789 * fire up the property service. This must happen 790 * after the ro.foo properties are set above so 791 * that /data/local.prop cannot interfere with them. 792 */ 793 start_property_service(); 794 return 0; 795} 796 797static int signal_init_action(int nargs, char **args) 798{ 799 signal_init(); 800 return 0; 801} 802 803static int check_startup_action(int nargs, char **args) 804{ 805 /* make sure we actually have all the pieces we need */ 806 if ((get_property_set_fd() < 0) || 807 (get_signal_fd() < 0)) { 808 ERROR("init startup failure\n"); 809 exit(1); 810 } 811 812 /* signal that we hit this point */ 813 unlink("/dev/.booting"); 814 815 return 0; 816} 817 818static int queue_property_triggers_action(int nargs, char **args) 819{ 820 queue_all_property_triggers(); 821 /* enable property triggers */ 822 property_triggers_enabled = 1; 823 return 0; 824} 825 826#if BOOTCHART 827static int bootchart_init_action(int nargs, char **args) 828{ 829 bootchart_count = bootchart_init(); 830 if (bootchart_count < 0) { 831 ERROR("bootcharting init failure\n"); 832 } else if (bootchart_count > 0) { 833 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); 834 } else { 835 NOTICE("bootcharting ignored\n"); 836 } 837 838 return 0; 839} 840#endif 841 842static const struct selinux_opt seopts_prop[] = { 843 { SELABEL_OPT_PATH, "/property_contexts" }, 844 { SELABEL_OPT_PATH, "/data/security/current/property_contexts" }, 845 { 0, NULL } 846}; 847 848struct selabel_handle* selinux_android_prop_context_handle(void) 849{ 850 int policy_index = selinux_android_use_data_policy() ? 1 : 0; 851 struct selabel_handle* sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP, 852 &seopts_prop[policy_index], 1); 853 if (!sehandle) { 854 ERROR("SELinux: Could not load property_contexts: %s\n", 855 strerror(errno)); 856 return NULL; 857 } 858 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[policy_index].value); 859 return sehandle; 860} 861 862void selinux_init_all_handles(void) 863{ 864 sehandle = selinux_android_file_context_handle(); 865 selinux_android_set_sehandle(sehandle); 866 sehandle_prop = selinux_android_prop_context_handle(); 867} 868 869static bool selinux_is_disabled(void) 870{ 871#ifdef ALLOW_DISABLE_SELINUX 872 char tmp[PROP_VALUE_MAX]; 873 874 if (access("/sys/fs/selinux", F_OK) != 0) { 875 /* SELinux is not compiled into the kernel, or has been disabled 876 * via the kernel command line "selinux=0". 877 */ 878 return true; 879 } 880 881 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) { 882 /* SELinux is compiled into the kernel, but we've been told to disable it. */ 883 return true; 884 } 885#endif 886 887 return false; 888} 889 890static bool selinux_is_enforcing(void) 891{ 892#ifdef ALLOW_DISABLE_SELINUX 893 char tmp[PROP_VALUE_MAX]; 894 895 if (property_get("ro.boot.selinux", tmp) == 0) { 896 /* Property is not set. Assume enforcing */ 897 return true; 898 } 899 900 if (strcmp(tmp, "permissive") == 0) { 901 /* SELinux is in the kernel, but we've been told to go into permissive mode */ 902 return false; 903 } 904 905 if (strcmp(tmp, "enforcing") != 0) { 906 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp); 907 } 908 909#endif 910 return true; 911} 912 913int selinux_reload_policy(void) 914{ 915 if (selinux_is_disabled()) { 916 return -1; 917 } 918 919 INFO("SELinux: Attempting to reload policy files\n"); 920 921 if (selinux_android_reload_policy() == -1) { 922 return -1; 923 } 924 925 if (sehandle) 926 selabel_close(sehandle); 927 928 if (sehandle_prop) 929 selabel_close(sehandle_prop); 930 931 selinux_init_all_handles(); 932 return 0; 933} 934 935static int audit_callback(void *data, security_class_t cls __attribute__((unused)), char *buf, size_t len) 936{ 937 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data); 938 return 0; 939} 940 941static int log_callback(int type, const char *fmt, ...) 942{ 943 int level; 944 va_list ap; 945 switch (type) { 946 case SELINUX_WARNING: 947 level = KLOG_WARNING_LEVEL; 948 break; 949 case SELINUX_INFO: 950 level = KLOG_INFO_LEVEL; 951 break; 952 default: 953 level = KLOG_ERROR_LEVEL; 954 break; 955 } 956 va_start(ap, fmt); 957 klog_vwrite(level, fmt, ap); 958 va_end(ap); 959 return 0; 960} 961 962static void selinux_initialize(void) 963{ 964 if (selinux_is_disabled()) { 965 return; 966 } 967 968 INFO("loading selinux policy\n"); 969 if (selinux_android_load_policy() < 0) { 970 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n"); 971 android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); 972 while (1) { pause(); } // never reached 973 } 974 975 selinux_init_all_handles(); 976 bool is_enforcing = selinux_is_enforcing(); 977 INFO("SELinux: security_setenforce(%d)\n", is_enforcing); 978 security_setenforce(is_enforcing); 979} 980 981int main(int argc, char **argv) 982{ 983 int fd_count = 0; 984 struct pollfd ufds[4]; 985 char *tmpdev; 986 char* debuggable; 987 char tmp[32]; 988 int property_set_fd_init = 0; 989 int signal_fd_init = 0; 990 int keychord_fd_init = 0; 991 bool is_charger = false; 992 993 if (!strcmp(basename(argv[0]), "ueventd")) 994 return ueventd_main(argc, argv); 995 996 if (!strcmp(basename(argv[0]), "watchdogd")) 997 return watchdogd_main(argc, argv); 998 999 /* clear the umask */ 1000 umask(0); 1001 1002 /* Get the basic filesystem setup we need put 1003 * together in the initramdisk on / and then we'll 1004 * let the rc file figure out the rest. 1005 */ 1006 mkdir("/dev", 0755); 1007 mkdir("/proc", 0755); 1008 mkdir("/sys", 0755); 1009 1010 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); 1011 mkdir("/dev/pts", 0755); 1012 mkdir("/dev/socket", 0755); 1013 mount("devpts", "/dev/pts", "devpts", 0, NULL); 1014 mount("proc", "/proc", "proc", 0, NULL); 1015 mount("sysfs", "/sys", "sysfs", 0, NULL); 1016 1017 /* indicate that booting is in progress to background fw loaders, etc */ 1018 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000)); 1019 1020 /* We must have some place other than / to create the 1021 * device nodes for kmsg and null, otherwise we won't 1022 * be able to remount / read-only later on. 1023 * Now that tmpfs is mounted on /dev, we can actually 1024 * talk to the outside world. 1025 */ 1026 open_devnull_stdio(); 1027 klog_init(); 1028 property_init(); 1029 1030 get_hardware_name(hardware, &revision); 1031 1032 process_kernel_cmdline(); 1033 1034 union selinux_callback cb; 1035 cb.func_log = log_callback; 1036 selinux_set_callback(SELINUX_CB_LOG, cb); 1037 1038 cb.func_audit = audit_callback; 1039 selinux_set_callback(SELINUX_CB_AUDIT, cb); 1040 1041 selinux_initialize(); 1042 /* These directories were necessarily created before initial policy load 1043 * and therefore need their security context restored to the proper value. 1044 * This must happen before /dev is populated by ueventd. 1045 */ 1046 restorecon("/dev"); 1047 restorecon("/dev/socket"); 1048 restorecon("/dev/__properties__"); 1049 restorecon_recursive("/sys"); 1050 1051 is_charger = !strcmp(bootmode, "charger"); 1052 1053 INFO("property init\n"); 1054 property_load_boot_defaults(); 1055 1056 INFO("reading config file\n"); 1057 init_parse_config_file("/init.rc"); 1058 1059 action_for_each_trigger("early-init", action_add_queue_tail); 1060 1061 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done"); 1062 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); 1063 queue_builtin_action(keychord_init_action, "keychord_init"); 1064 queue_builtin_action(console_init_action, "console_init"); 1065 1066 /* execute all the boot actions to get us started */ 1067 action_for_each_trigger("init", action_add_queue_tail); 1068 1069 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random 1070 * wasn't ready immediately after wait_for_coldboot_done 1071 */ 1072 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng"); 1073 queue_builtin_action(property_service_init_action, "property_service_init"); 1074 queue_builtin_action(signal_init_action, "signal_init"); 1075 queue_builtin_action(check_startup_action, "check_startup"); 1076 1077 /* Don't mount filesystems or start core system services if in charger mode. */ 1078 if (is_charger) { 1079 action_for_each_trigger("charger", action_add_queue_tail); 1080 } else { 1081 action_for_each_trigger("late-init", action_add_queue_tail); 1082 } 1083 1084 /* run all property triggers based on current state of the properties */ 1085 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers"); 1086 1087 1088#if BOOTCHART 1089 queue_builtin_action(bootchart_init_action, "bootchart_init"); 1090#endif 1091 1092 for(;;) { 1093 int nr, i, timeout = -1; 1094 1095 execute_one_command(); 1096 restart_processes(); 1097 1098 if (!property_set_fd_init && get_property_set_fd() > 0) { 1099 ufds[fd_count].fd = get_property_set_fd(); 1100 ufds[fd_count].events = POLLIN; 1101 ufds[fd_count].revents = 0; 1102 fd_count++; 1103 property_set_fd_init = 1; 1104 } 1105 if (!signal_fd_init && get_signal_fd() > 0) { 1106 ufds[fd_count].fd = get_signal_fd(); 1107 ufds[fd_count].events = POLLIN; 1108 ufds[fd_count].revents = 0; 1109 fd_count++; 1110 signal_fd_init = 1; 1111 } 1112 if (!keychord_fd_init && get_keychord_fd() > 0) { 1113 ufds[fd_count].fd = get_keychord_fd(); 1114 ufds[fd_count].events = POLLIN; 1115 ufds[fd_count].revents = 0; 1116 fd_count++; 1117 keychord_fd_init = 1; 1118 } 1119 1120 if (process_needs_restart) { 1121 timeout = (process_needs_restart - gettime()) * 1000; 1122 if (timeout < 0) 1123 timeout = 0; 1124 } 1125 1126 if (!action_queue_empty() || cur_action) 1127 timeout = 0; 1128 1129#if BOOTCHART 1130 if (bootchart_count > 0) { 1131 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) 1132 timeout = BOOTCHART_POLLING_MS; 1133 if (bootchart_step() < 0 || --bootchart_count == 0) { 1134 bootchart_finish(); 1135 bootchart_count = 0; 1136 } 1137 } 1138#endif 1139 1140 nr = poll(ufds, fd_count, timeout); 1141 if (nr <= 0) 1142 continue; 1143 1144 for (i = 0; i < fd_count; i++) { 1145 if (ufds[i].revents & POLLIN) { 1146 if (ufds[i].fd == get_property_set_fd()) 1147 handle_property_set_fd(); 1148 else if (ufds[i].fd == get_keychord_fd()) 1149 handle_keychord(); 1150 else if (ufds[i].fd == get_signal_fd()) 1151 handle_signal(); 1152 } 1153 } 1154 } 1155 1156 return 0; 1157} 1158