blob: 008c5134e6a150191a657721ddae28ec78942e53 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
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>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#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#include <sys/reboot.h>
35
36#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080037#include <cutils/iosched_policy.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070039
40#include <sys/system_properties.h>
41
42#include "devices.h"
43#include "init.h"
44#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080045#include "bootchart.h"
Colin Crossa8666952010-04-13 19:20:44 -070046#include "keychords.h"
Colin Crossca7648d2010-04-13 19:29:51 -070047#include "parser.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048
49static int property_triggers_enabled = 0;
50
51#if BOOTCHART
52static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053#endif
54
55static char console[32];
56static char serialno[32];
57static char bootmode[32];
58static char baseband[32];
59static char carrier[32];
60static char bootloader[32];
61static char hardware[32];
62static unsigned revision = 0;
63static char qemu[32];
64
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065static void notify_service_state(const char *name, const char *state)
66{
67 char pname[PROP_NAME_MAX];
68 int len = strlen(name);
69 if ((len + 10) > PROP_NAME_MAX)
70 return;
71 snprintf(pname, sizeof(pname), "init.svc.%s", name);
72 property_set(pname, state);
73}
74
75static int have_console;
76static char *console_name = "/dev/console";
77static time_t process_needs_restart;
78
79static const char *ENV[32];
80
81/* add_environment - add "key=value" to the current environment */
82int add_environment(const char *key, const char *val)
83{
84 int n;
85
86 for (n = 0; n < 31; n++) {
87 if (!ENV[n]) {
88 size_t len = strlen(key) + strlen(val) + 2;
89 char *entry = malloc(len);
90 snprintf(entry, len, "%s=%s", key, val);
91 ENV[n] = entry;
92 return 0;
93 }
94 }
95
96 return 1;
97}
98
99static void zap_stdio(void)
100{
101 int fd;
102 fd = open("/dev/null", O_RDWR);
103 dup2(fd, 0);
104 dup2(fd, 1);
105 dup2(fd, 2);
106 close(fd);
107}
108
109static void open_console()
110{
111 int fd;
112 if ((fd = open(console_name, O_RDWR)) < 0) {
113 fd = open("/dev/null", O_RDWR);
114 }
115 dup2(fd, 0);
116 dup2(fd, 1);
117 dup2(fd, 2);
118 close(fd);
119}
120
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700121static void publish_socket(const char *name, int fd)
122{
123 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
124 char val[64];
125
126 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
127 name,
128 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
129 snprintf(val, sizeof(val), "%d", fd);
130 add_environment(key, val);
131
132 /* make sure we don't close-on-exec */
133 fcntl(fd, F_SETFD, 0);
134}
135
San Mehatf24e2522009-05-19 13:30:46 -0700136void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137{
138 struct stat s;
139 pid_t pid;
140 int needs_console;
141 int n;
142
143 /* starting a service removes it from the disabled
144 * state and immediately takes it out of the restarting
145 * state if it was in there
146 */
147 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING));
148 svc->time_started = 0;
149
150 /* running processes require no additional work -- if
151 * they're in the process of exiting, we've ensured
152 * that they will immediately restart on exit, unless
153 * they are ONESHOT
154 */
155 if (svc->flags & SVC_RUNNING) {
156 return;
157 }
158
159 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
160 if (needs_console && (!have_console)) {
161 ERROR("service '%s' requires console\n", svc->name);
162 svc->flags |= SVC_DISABLED;
163 return;
164 }
165
166 if (stat(svc->args[0], &s) != 0) {
167 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
168 svc->flags |= SVC_DISABLED;
169 return;
170 }
171
San Mehatf24e2522009-05-19 13:30:46 -0700172 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700173 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
174 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700175 svc->flags |= SVC_DISABLED;
176 return;
177 }
178
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179 NOTICE("starting '%s'\n", svc->name);
180
181 pid = fork();
182
183 if (pid == 0) {
184 struct socketinfo *si;
185 struct svcenvinfo *ei;
186 char tmp[32];
187 int fd, sz;
188
189 get_property_workspace(&fd, &sz);
190 sprintf(tmp, "%d,%d", dup(fd), sz);
191 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
192
193 for (ei = svc->envvars; ei; ei = ei->next)
194 add_environment(ei->name, ei->value);
195
196 for (si = svc->sockets; si; si = si->next) {
197 int s = create_socket(si->name,
198 !strcmp(si->type, "dgram") ?
199 SOCK_DGRAM : SOCK_STREAM,
200 si->perm, si->uid, si->gid);
201 if (s >= 0) {
202 publish_socket(si->name, s);
203 }
204 }
205
San Mehat4e221f02010-02-25 14:19:50 -0800206 if (svc->ioprio_class != IoSchedClass_NONE) {
207 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
208 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
209 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
210 }
211 }
212
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213 if (needs_console) {
214 setsid();
215 open_console();
216 } else {
217 zap_stdio();
218 }
219
220#if 0
221 for (n = 0; svc->args[n]; n++) {
222 INFO("args[%d] = '%s'\n", n, svc->args[n]);
223 }
224 for (n = 0; ENV[n]; n++) {
225 INFO("env[%d] = '%s'\n", n, ENV[n]);
226 }
227#endif
228
229 setpgid(0, getpid());
230
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800231 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232 if (svc->gid) {
233 setgid(svc->gid);
234 }
235 if (svc->nr_supp_gids) {
236 setgroups(svc->nr_supp_gids, svc->supp_gids);
237 }
238 if (svc->uid) {
239 setuid(svc->uid);
240 }
241
San Mehat8ad15682009-05-20 08:50:40 -0700242 if (!dynamic_args) {
243 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
244 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
245 }
246 } else {
San Mehatf24e2522009-05-19 13:30:46 -0700247 char *arg_ptrs[SVC_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700248 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700249 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700250 char *next = tmp;
251 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700252
253 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700254 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700255
San Mehatd4cdd132009-05-20 09:52:16 -0700256 while((bword = strsep(&next, " "))) {
257 arg_ptrs[arg_idx++] = bword;
258 if (arg_idx == SVC_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700259 break;
San Mehatf24e2522009-05-19 13:30:46 -0700260 }
261 arg_ptrs[arg_idx] = '\0';
262 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100263 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700264 _exit(127);
265 }
266
267 if (pid < 0) {
268 ERROR("failed to start '%s'\n", svc->name);
269 svc->pid = 0;
270 return;
271 }
272
273 svc->time_started = gettime();
274 svc->pid = pid;
275 svc->flags |= SVC_RUNNING;
276
277 notify_service_state(svc->name, "running");
278}
279
280void service_stop(struct service *svc)
281{
282 /* we are no longer running, nor should we
283 * attempt to restart
284 */
285 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
286
287 /* if the service has not yet started, prevent
288 * it from auto-starting with its class
289 */
290 svc->flags |= SVC_DISABLED;
291
292 if (svc->pid) {
293 NOTICE("service '%s' is being killed\n", svc->name);
294 kill(-svc->pid, SIGTERM);
295 notify_service_state(svc->name, "stopping");
296 } else {
297 notify_service_state(svc->name, "stopped");
298 }
299}
300
301void property_changed(const char *name, const char *value)
302{
303 if (property_triggers_enabled) {
304 queue_property_triggers(name, value);
305 drain_action_queue();
306 }
307}
308
309#define CRITICAL_CRASH_THRESHOLD 4 /* if we crash >4 times ... */
310#define CRITICAL_CRASH_WINDOW (4*60) /* ... in 4 minutes, goto recovery*/
311
312static int wait_for_one_process(int block)
313{
314 pid_t pid;
315 int status;
316 struct service *svc;
317 struct socketinfo *si;
318 time_t now;
319 struct listnode *node;
320 struct command *cmd;
321
322 while ( (pid = waitpid(-1, &status, block ? 0 : WNOHANG)) == -1 && errno == EINTR );
323 if (pid <= 0) return -1;
324 INFO("waitpid returned pid %d, status = %08x\n", pid, status);
325
326 svc = service_find_by_pid(pid);
327 if (!svc) {
328 ERROR("untracked pid %d exited\n", pid);
329 return 0;
330 }
331
332 NOTICE("process '%s', pid %d exited\n", svc->name, pid);
333
334 if (!(svc->flags & SVC_ONESHOT)) {
335 kill(-pid, SIGKILL);
336 NOTICE("process '%s' killing any children in process group\n", svc->name);
337 }
338
339 /* remove any sockets we may have created */
340 for (si = svc->sockets; si; si = si->next) {
341 char tmp[128];
342 snprintf(tmp, sizeof(tmp), ANDROID_SOCKET_DIR"/%s", si->name);
343 unlink(tmp);
344 }
345
346 svc->pid = 0;
347 svc->flags &= (~SVC_RUNNING);
348
349 /* oneshot processes go into the disabled state on exit */
350 if (svc->flags & SVC_ONESHOT) {
351 svc->flags |= SVC_DISABLED;
352 }
353
354 /* disabled processes do not get restarted automatically */
355 if (svc->flags & SVC_DISABLED) {
356 notify_service_state(svc->name, "stopped");
357 return 0;
358 }
359
360 now = gettime();
361 if (svc->flags & SVC_CRITICAL) {
362 if (svc->time_crashed + CRITICAL_CRASH_WINDOW >= now) {
363 if (++svc->nr_crashed > CRITICAL_CRASH_THRESHOLD) {
364 ERROR("critical process '%s' exited %d times in %d minutes; "
365 "rebooting into recovery mode\n", svc->name,
366 CRITICAL_CRASH_THRESHOLD, CRITICAL_CRASH_WINDOW / 60);
367 sync();
368 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
369 LINUX_REBOOT_CMD_RESTART2, "recovery");
370 return 0;
371 }
372 } else {
373 svc->time_crashed = now;
374 svc->nr_crashed = 1;
375 }
376 }
377
Ben Gruverdc816d52009-01-13 20:43:30 -0600378 svc->flags |= SVC_RESTARTING;
379
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380 /* Execute all onrestart commands for this service. */
381 list_for_each(node, &svc->onrestart.commands) {
382 cmd = node_to_item(node, struct command, clist);
383 cmd->func(cmd->nargs, cmd->args);
384 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 notify_service_state(svc->name, "restarting");
386 return 0;
387}
388
389static void restart_service_if_needed(struct service *svc)
390{
391 time_t next_start_time = svc->time_started + 5;
392
393 if (next_start_time <= gettime()) {
394 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700395 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700396 return;
397 }
398
399 if ((next_start_time < process_needs_restart) ||
400 (process_needs_restart == 0)) {
401 process_needs_restart = next_start_time;
402 }
403}
404
405static void restart_processes()
406{
407 process_needs_restart = 0;
408 service_for_each_flags(SVC_RESTARTING,
409 restart_service_if_needed);
410}
411
412static int signal_fd = -1;
413
414static void sigchld_handler(int s)
415{
416 write(signal_fd, &s, 1);
417}
418
419static void msg_start(const char *name)
420{
San Mehatf24e2522009-05-19 13:30:46 -0700421 struct service *svc;
422 char *tmp = NULL;
423 char *args = NULL;
424
425 if (!strchr(name, ':'))
426 svc = service_find_by_name(name);
427 else {
428 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700429 args = strchr(tmp, ':');
430 *args = '\0';
431 args++;
432
433 svc = service_find_by_name(tmp);
434 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700435
436 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700437 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700438 } else {
439 ERROR("no such service '%s'\n", name);
440 }
San Mehatf24e2522009-05-19 13:30:46 -0700441 if (tmp)
442 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700443}
444
445static void msg_stop(const char *name)
446{
447 struct service *svc = service_find_by_name(name);
448
449 if (svc) {
450 service_stop(svc);
451 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700452 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700453 }
454}
455
456void handle_control_message(const char *msg, const char *arg)
457{
458 if (!strcmp(msg,"start")) {
459 msg_start(arg);
460 } else if (!strcmp(msg,"stop")) {
461 msg_stop(arg);
462 } else {
463 ERROR("unknown control msg '%s'\n", msg);
464 }
465}
466
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700467static void import_kernel_nv(char *name, int in_qemu)
468{
469 char *value = strchr(name, '=');
470
471 if (value == 0) return;
472 *value++ = 0;
473 if (*name == 0) return;
474
475 if (!in_qemu)
476 {
477 /* on a real device, white-list the kernel options */
478 if (!strcmp(name,"qemu")) {
479 strlcpy(qemu, value, sizeof(qemu));
480 } else if (!strcmp(name,"androidboot.console")) {
481 strlcpy(console, value, sizeof(console));
482 } else if (!strcmp(name,"androidboot.mode")) {
483 strlcpy(bootmode, value, sizeof(bootmode));
484 } else if (!strcmp(name,"androidboot.serialno")) {
485 strlcpy(serialno, value, sizeof(serialno));
486 } else if (!strcmp(name,"androidboot.baseband")) {
487 strlcpy(baseband, value, sizeof(baseband));
488 } else if (!strcmp(name,"androidboot.carrier")) {
489 strlcpy(carrier, value, sizeof(carrier));
490 } else if (!strcmp(name,"androidboot.bootloader")) {
491 strlcpy(bootloader, value, sizeof(bootloader));
492 } else if (!strcmp(name,"androidboot.hardware")) {
493 strlcpy(hardware, value, sizeof(hardware));
494 } else {
495 qemu_cmdline(name, value);
496 }
497 } else {
498 /* in the emulator, export any kernel option with the
499 * ro.kernel. prefix */
500 char buff[32];
501 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
502 if (len < (int)sizeof(buff)) {
503 property_set( buff, value );
504 }
505 }
506}
507
508static void import_kernel_cmdline(int in_qemu)
509{
510 char cmdline[1024];
511 char *ptr;
512 int fd;
513
514 fd = open("/proc/cmdline", O_RDONLY);
515 if (fd >= 0) {
516 int n = read(fd, cmdline, 1023);
517 if (n < 0) n = 0;
518
519 /* get rid of trailing newline, it happens */
520 if (n > 0 && cmdline[n-1] == '\n') n--;
521
522 cmdline[n] = 0;
523 close(fd);
524 } else {
525 cmdline[0] = 0;
526 }
527
528 ptr = cmdline;
529 while (ptr && *ptr) {
530 char *x = strchr(ptr, ' ');
531 if (x != 0) *x++ = 0;
532 import_kernel_nv(ptr, in_qemu);
533 ptr = x;
534 }
535
536 /* don't expose the raw commandline to nonpriv processes */
537 chmod("/proc/cmdline", 0440);
538}
539
540static void get_hardware_name(void)
541{
542 char data[1024];
543 int fd, n;
544 char *x, *hw, *rev;
545
546 /* Hardware string was provided on kernel command line */
547 if (hardware[0])
548 return;
549
550 fd = open("/proc/cpuinfo", O_RDONLY);
551 if (fd < 0) return;
552
553 n = read(fd, data, 1023);
554 close(fd);
555 if (n < 0) return;
556
557 data[n] = 0;
558 hw = strstr(data, "\nHardware");
559 rev = strstr(data, "\nRevision");
560
561 if (hw) {
562 x = strstr(hw, ": ");
563 if (x) {
564 x += 2;
565 n = 0;
566 while (*x && !isspace(*x)) {
567 hardware[n++] = tolower(*x);
568 x++;
569 if (n == 31) break;
570 }
571 hardware[n] = 0;
572 }
573 }
574
575 if (rev) {
576 x = strstr(rev, ": ");
577 if (x) {
578 revision = strtoul(x + 2, 0, 16);
579 }
580 }
581}
582
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000583void drain_action_queue(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700584{
585 struct listnode *node;
586 struct command *cmd;
587 struct action *act;
588 int ret;
589
590 while ((act = action_remove_queue_head())) {
591 INFO("processing action %p (%s)\n", act, act->name);
592 list_for_each(node, &act->commands) {
593 cmd = node_to_item(node, struct command, clist);
594 ret = cmd->func(cmd->nargs, cmd->args);
595 INFO("command '%s' r=%d\n", cmd->args[0], ret);
596 }
597 }
598}
599
600void open_devnull_stdio(void)
601{
602 int fd;
603 static const char *name = "/dev/__null__";
604 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
605 fd = open(name, O_RDWR);
606 unlink(name);
607 if (fd >= 0) {
608 dup2(fd, 0);
609 dup2(fd, 1);
610 dup2(fd, 2);
611 if (fd > 2) {
612 close(fd);
613 }
614 return;
615 }
616 }
617
618 exit(1);
619}
620
621int main(int argc, char **argv)
622{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700623 int signal_recv_fd = -1;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800624 int fd_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700625 int s[2];
626 int fd;
627 struct sigaction act;
628 char tmp[PROP_VALUE_MAX];
629 struct pollfd ufds[4];
630 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800631 char* debuggable;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700632
633 act.sa_handler = sigchld_handler;
634 act.sa_flags = SA_NOCLDSTOP;
635 act.sa_mask = 0;
636 act.sa_restorer = NULL;
637 sigaction(SIGCHLD, &act, 0);
638
639 /* clear the umask */
640 umask(0);
641
642 /* Get the basic filesystem setup we need put
643 * together in the initramdisk on / and then we'll
644 * let the rc file figure out the rest.
645 */
646 mkdir("/dev", 0755);
647 mkdir("/proc", 0755);
648 mkdir("/sys", 0755);
649
650 mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755");
651 mkdir("/dev/pts", 0755);
652 mkdir("/dev/socket", 0755);
653 mount("devpts", "/dev/pts", "devpts", 0, NULL);
654 mount("proc", "/proc", "proc", 0, NULL);
655 mount("sysfs", "/sys", "sysfs", 0, NULL);
656
657 /* We must have some place other than / to create the
658 * device nodes for kmsg and null, otherwise we won't
659 * be able to remount / read-only later on.
660 * Now that tmpfs is mounted on /dev, we can actually
661 * talk to the outside world.
662 */
663 open_devnull_stdio();
664 log_init();
665
666 INFO("reading config file\n");
667 parse_config_file("/init.rc");
668
669 /* pull the kernel commandline and ramdisk properties file in */
670 qemu_init();
671 import_kernel_cmdline(0);
672
673 get_hardware_name();
674 snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
675 parse_config_file(tmp);
676
677 action_for_each_trigger("early-init", action_add_queue_tail);
678 drain_action_queue();
679
680 INFO("device init\n");
Colin Cross0dd7ca62010-04-13 19:25:51 -0700681 device_init();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700682
683 property_init();
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800684
685 // only listen for keychords if ro.debuggable is true
Colin Crossa8666952010-04-13 19:20:44 -0700686 keychord_init();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700687
688 if (console[0]) {
689 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
690 console_name = strdup(tmp);
691 }
692
693 fd = open(console_name, O_RDWR);
694 if (fd >= 0)
695 have_console = 1;
696 close(fd);
697
698 if( load_565rle_image(INIT_IMAGE_FILE) ) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800699 fd = open("/dev/tty0", O_WRONLY);
700 if (fd >= 0) {
701 const char *msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700702 msg = "\n"
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800703 "\n"
704 "\n"
705 "\n"
706 "\n"
707 "\n"
708 "\n" // console is 40 cols x 30 lines
709 "\n"
710 "\n"
711 "\n"
712 "\n"
713 "\n"
714 "\n"
715 "\n"
716 " A N D R O I D ";
717 write(fd, msg, strlen(msg));
718 close(fd);
719 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720 }
721
722 if (qemu[0])
723 import_kernel_cmdline(1);
724
725 if (!strcmp(bootmode,"factory"))
726 property_set("ro.factorytest", "1");
727 else if (!strcmp(bootmode,"factory2"))
728 property_set("ro.factorytest", "2");
729 else
730 property_set("ro.factorytest", "0");
731
732 property_set("ro.serialno", serialno[0] ? serialno : "");
733 property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown");
734 property_set("ro.baseband", baseband[0] ? baseband : "unknown");
735 property_set("ro.carrier", carrier[0] ? carrier : "unknown");
736 property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown");
737
738 property_set("ro.hardware", hardware);
739 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
740 property_set("ro.revision", tmp);
741
742 /* execute all the boot actions to get us started */
743 action_for_each_trigger("init", action_add_queue_tail);
Colin Cross31712be2010-04-09 12:26:06 -0700744 action_for_each_trigger("early-fs", action_add_queue_tail);
745 action_for_each_trigger("fs", action_add_queue_tail);
746 action_for_each_trigger("post-fs", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700747 drain_action_queue();
748
749 /* read any property files on system or data and
750 * fire up the property service. This must happen
751 * after the ro.foo properties are set above so
752 * that /data/local.prop cannot interfere with them.
753 */
Colin Crossd11beb22010-04-13 19:33:37 -0700754 start_property_service();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700755
756 /* create a signalling mechanism for the sigchld handler */
757 if (socketpair(AF_UNIX, SOCK_STREAM, 0, s) == 0) {
758 signal_fd = s[0];
759 signal_recv_fd = s[1];
760 fcntl(s[0], F_SETFD, FD_CLOEXEC);
761 fcntl(s[0], F_SETFL, O_NONBLOCK);
762 fcntl(s[1], F_SETFD, FD_CLOEXEC);
763 fcntl(s[1], F_SETFL, O_NONBLOCK);
764 }
765
766 /* make sure we actually have all the pieces we need */
Colin Cross0dd7ca62010-04-13 19:25:51 -0700767 if ((get_device_fd() < 0) ||
Colin Crossd11beb22010-04-13 19:33:37 -0700768 (get_property_set_fd() < 0) ||
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700769 (signal_recv_fd < 0)) {
770 ERROR("init startup failure\n");
771 return 1;
772 }
773
774 /* execute all the boot actions to get us started */
775 action_for_each_trigger("early-boot", action_add_queue_tail);
776 action_for_each_trigger("boot", action_add_queue_tail);
777 drain_action_queue();
778
779 /* run all property triggers based on current state of the properties */
780 queue_all_property_triggers();
781 drain_action_queue();
782
783 /* enable property triggers */
784 property_triggers_enabled = 1;
785
Colin Cross0dd7ca62010-04-13 19:25:51 -0700786 ufds[0].fd = get_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700787 ufds[0].events = POLLIN;
Colin Crossd11beb22010-04-13 19:33:37 -0700788 ufds[1].fd = get_property_set_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789 ufds[1].events = POLLIN;
790 ufds[2].fd = signal_recv_fd;
791 ufds[2].events = POLLIN;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800792 fd_count = 3;
793
Colin Crossa8666952010-04-13 19:20:44 -0700794 if (get_keychord_fd() > 0) {
795 ufds[3].fd = get_keychord_fd();
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800796 ufds[3].events = POLLIN;
797 fd_count++;
798 } else {
799 ufds[3].events = 0;
800 ufds[3].revents = 0;
801 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700802
803#if BOOTCHART
The Android Open Source Project35237d12008-12-17 18:08:08 -0800804 bootchart_count = bootchart_init();
805 if (bootchart_count < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806 ERROR("bootcharting init failure\n");
The Android Open Source Project35237d12008-12-17 18:08:08 -0800807 } else if (bootchart_count > 0) {
808 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
809 } else {
810 NOTICE("bootcharting ignored\n");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811 }
812#endif
813
814 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800815 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700816
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800817 for (i = 0; i < fd_count; i++)
818 ufds[i].revents = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700819
820 drain_action_queue();
821 restart_processes();
822
823 if (process_needs_restart) {
824 timeout = (process_needs_restart - gettime()) * 1000;
825 if (timeout < 0)
826 timeout = 0;
827 }
828
829#if BOOTCHART
830 if (bootchart_count > 0) {
831 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
832 timeout = BOOTCHART_POLLING_MS;
833 if (bootchart_step() < 0 || --bootchart_count == 0) {
834 bootchart_finish();
835 bootchart_count = 0;
836 }
837 }
838#endif
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800839 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700840 if (nr <= 0)
841 continue;
842
843 if (ufds[2].revents == POLLIN) {
844 /* we got a SIGCHLD - reap and restart as needed */
845 read(signal_recv_fd, tmp, sizeof(tmp));
846 while (!wait_for_one_process(0))
847 ;
848 continue;
849 }
850
851 if (ufds[0].revents == POLLIN)
Colin Cross0dd7ca62010-04-13 19:25:51 -0700852 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700853
854 if (ufds[1].revents == POLLIN)
Colin Crossd11beb22010-04-13 19:33:37 -0700855 handle_property_set_fd();
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800856 if (ufds[3].revents == POLLIN)
Colin Crossa8666952010-04-13 19:20:44 -0700857 handle_keychord();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858 }
859
860 return 0;
861}