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