blob: b76fa667a968a8add78410f4dfc79043f0e78ff5 [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>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070034
35#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080036#include <cutils/iosched_policy.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038
39#include <sys/system_properties.h>
40
41#include "devices.h"
42#include "init.h"
43#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080044#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070045#include "signal_handler.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
Colin Cross9c5366b2010-04-13 19:48:59 -070065void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066{
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
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309static void restart_service_if_needed(struct service *svc)
310{
311 time_t next_start_time = svc->time_started + 5;
312
313 if (next_start_time <= gettime()) {
314 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700315 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 return;
317 }
318
319 if ((next_start_time < process_needs_restart) ||
320 (process_needs_restart == 0)) {
321 process_needs_restart = next_start_time;
322 }
323}
324
325static void restart_processes()
326{
327 process_needs_restart = 0;
328 service_for_each_flags(SVC_RESTARTING,
329 restart_service_if_needed);
330}
331
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700332static void msg_start(const char *name)
333{
San Mehatf24e2522009-05-19 13:30:46 -0700334 struct service *svc;
335 char *tmp = NULL;
336 char *args = NULL;
337
338 if (!strchr(name, ':'))
339 svc = service_find_by_name(name);
340 else {
341 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700342 args = strchr(tmp, ':');
343 *args = '\0';
344 args++;
345
346 svc = service_find_by_name(tmp);
347 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348
349 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700350 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700351 } else {
352 ERROR("no such service '%s'\n", name);
353 }
San Mehatf24e2522009-05-19 13:30:46 -0700354 if (tmp)
355 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356}
357
358static void msg_stop(const char *name)
359{
360 struct service *svc = service_find_by_name(name);
361
362 if (svc) {
363 service_stop(svc);
364 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700365 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700366 }
367}
368
369void handle_control_message(const char *msg, const char *arg)
370{
371 if (!strcmp(msg,"start")) {
372 msg_start(arg);
373 } else if (!strcmp(msg,"stop")) {
374 msg_stop(arg);
375 } else {
376 ERROR("unknown control msg '%s'\n", msg);
377 }
378}
379
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380static void import_kernel_nv(char *name, int in_qemu)
381{
382 char *value = strchr(name, '=');
383
384 if (value == 0) return;
385 *value++ = 0;
386 if (*name == 0) return;
387
388 if (!in_qemu)
389 {
390 /* on a real device, white-list the kernel options */
391 if (!strcmp(name,"qemu")) {
392 strlcpy(qemu, value, sizeof(qemu));
393 } else if (!strcmp(name,"androidboot.console")) {
394 strlcpy(console, value, sizeof(console));
395 } else if (!strcmp(name,"androidboot.mode")) {
396 strlcpy(bootmode, value, sizeof(bootmode));
397 } else if (!strcmp(name,"androidboot.serialno")) {
398 strlcpy(serialno, value, sizeof(serialno));
399 } else if (!strcmp(name,"androidboot.baseband")) {
400 strlcpy(baseband, value, sizeof(baseband));
401 } else if (!strcmp(name,"androidboot.carrier")) {
402 strlcpy(carrier, value, sizeof(carrier));
403 } else if (!strcmp(name,"androidboot.bootloader")) {
404 strlcpy(bootloader, value, sizeof(bootloader));
405 } else if (!strcmp(name,"androidboot.hardware")) {
406 strlcpy(hardware, value, sizeof(hardware));
407 } else {
408 qemu_cmdline(name, value);
409 }
410 } else {
411 /* in the emulator, export any kernel option with the
412 * ro.kernel. prefix */
413 char buff[32];
414 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
415 if (len < (int)sizeof(buff)) {
416 property_set( buff, value );
417 }
418 }
419}
420
421static void import_kernel_cmdline(int in_qemu)
422{
423 char cmdline[1024];
424 char *ptr;
425 int fd;
426
427 fd = open("/proc/cmdline", O_RDONLY);
428 if (fd >= 0) {
429 int n = read(fd, cmdline, 1023);
430 if (n < 0) n = 0;
431
432 /* get rid of trailing newline, it happens */
433 if (n > 0 && cmdline[n-1] == '\n') n--;
434
435 cmdline[n] = 0;
436 close(fd);
437 } else {
438 cmdline[0] = 0;
439 }
440
441 ptr = cmdline;
442 while (ptr && *ptr) {
443 char *x = strchr(ptr, ' ');
444 if (x != 0) *x++ = 0;
445 import_kernel_nv(ptr, in_qemu);
446 ptr = x;
447 }
448
449 /* don't expose the raw commandline to nonpriv processes */
450 chmod("/proc/cmdline", 0440);
451}
452
453static void get_hardware_name(void)
454{
455 char data[1024];
456 int fd, n;
457 char *x, *hw, *rev;
458
459 /* Hardware string was provided on kernel command line */
460 if (hardware[0])
461 return;
462
463 fd = open("/proc/cpuinfo", O_RDONLY);
464 if (fd < 0) return;
465
466 n = read(fd, data, 1023);
467 close(fd);
468 if (n < 0) return;
469
470 data[n] = 0;
471 hw = strstr(data, "\nHardware");
472 rev = strstr(data, "\nRevision");
473
474 if (hw) {
475 x = strstr(hw, ": ");
476 if (x) {
477 x += 2;
478 n = 0;
479 while (*x && !isspace(*x)) {
480 hardware[n++] = tolower(*x);
481 x++;
482 if (n == 31) break;
483 }
484 hardware[n] = 0;
485 }
486 }
487
488 if (rev) {
489 x = strstr(rev, ": ");
490 if (x) {
491 revision = strtoul(x + 2, 0, 16);
492 }
493 }
494}
495
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000496void drain_action_queue(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700497{
498 struct listnode *node;
499 struct command *cmd;
500 struct action *act;
501 int ret;
502
503 while ((act = action_remove_queue_head())) {
504 INFO("processing action %p (%s)\n", act, act->name);
505 list_for_each(node, &act->commands) {
506 cmd = node_to_item(node, struct command, clist);
507 ret = cmd->func(cmd->nargs, cmd->args);
508 INFO("command '%s' r=%d\n", cmd->args[0], ret);
509 }
510 }
511}
512
513void open_devnull_stdio(void)
514{
515 int fd;
516 static const char *name = "/dev/__null__";
517 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
518 fd = open(name, O_RDWR);
519 unlink(name);
520 if (fd >= 0) {
521 dup2(fd, 0);
522 dup2(fd, 1);
523 dup2(fd, 2);
524 if (fd > 2) {
525 close(fd);
526 }
527 return;
528 }
529 }
530
531 exit(1);
532}
533
534int main(int argc, char **argv)
535{
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800536 int fd_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700537 int s[2];
538 int fd;
539 struct sigaction act;
540 char tmp[PROP_VALUE_MAX];
541 struct pollfd ufds[4];
542 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800543 char* debuggable;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700544
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700545 /* clear the umask */
546 umask(0);
547
548 /* Get the basic filesystem setup we need put
549 * together in the initramdisk on / and then we'll
550 * let the rc file figure out the rest.
551 */
552 mkdir("/dev", 0755);
553 mkdir("/proc", 0755);
554 mkdir("/sys", 0755);
555
556 mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755");
557 mkdir("/dev/pts", 0755);
558 mkdir("/dev/socket", 0755);
559 mount("devpts", "/dev/pts", "devpts", 0, NULL);
560 mount("proc", "/proc", "proc", 0, NULL);
561 mount("sysfs", "/sys", "sysfs", 0, NULL);
562
563 /* We must have some place other than / to create the
564 * device nodes for kmsg and null, otherwise we won't
565 * be able to remount / read-only later on.
566 * Now that tmpfs is mounted on /dev, we can actually
567 * talk to the outside world.
568 */
569 open_devnull_stdio();
570 log_init();
571
572 INFO("reading config file\n");
573 parse_config_file("/init.rc");
574
575 /* pull the kernel commandline and ramdisk properties file in */
576 qemu_init();
577 import_kernel_cmdline(0);
578
579 get_hardware_name();
580 snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
581 parse_config_file(tmp);
582
583 action_for_each_trigger("early-init", action_add_queue_tail);
584 drain_action_queue();
585
586 INFO("device init\n");
Colin Cross0dd7ca62010-04-13 19:25:51 -0700587 device_init();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700588
589 property_init();
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800590
591 // only listen for keychords if ro.debuggable is true
Colin Crossa8666952010-04-13 19:20:44 -0700592 keychord_init();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700593
594 if (console[0]) {
595 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
596 console_name = strdup(tmp);
597 }
598
599 fd = open(console_name, O_RDWR);
600 if (fd >= 0)
601 have_console = 1;
602 close(fd);
603
604 if( load_565rle_image(INIT_IMAGE_FILE) ) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800605 fd = open("/dev/tty0", O_WRONLY);
606 if (fd >= 0) {
607 const char *msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700608 msg = "\n"
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800609 "\n"
610 "\n"
611 "\n"
612 "\n"
613 "\n"
614 "\n" // console is 40 cols x 30 lines
615 "\n"
616 "\n"
617 "\n"
618 "\n"
619 "\n"
620 "\n"
621 "\n"
622 " A N D R O I D ";
623 write(fd, msg, strlen(msg));
624 close(fd);
625 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700626 }
627
628 if (qemu[0])
629 import_kernel_cmdline(1);
630
631 if (!strcmp(bootmode,"factory"))
632 property_set("ro.factorytest", "1");
633 else if (!strcmp(bootmode,"factory2"))
634 property_set("ro.factorytest", "2");
635 else
636 property_set("ro.factorytest", "0");
637
638 property_set("ro.serialno", serialno[0] ? serialno : "");
639 property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown");
640 property_set("ro.baseband", baseband[0] ? baseband : "unknown");
641 property_set("ro.carrier", carrier[0] ? carrier : "unknown");
642 property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown");
643
644 property_set("ro.hardware", hardware);
645 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
646 property_set("ro.revision", tmp);
647
648 /* execute all the boot actions to get us started */
649 action_for_each_trigger("init", action_add_queue_tail);
Colin Cross31712be2010-04-09 12:26:06 -0700650 action_for_each_trigger("early-fs", action_add_queue_tail);
651 action_for_each_trigger("fs", action_add_queue_tail);
652 action_for_each_trigger("post-fs", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700653 drain_action_queue();
654
655 /* read any property files on system or data and
656 * fire up the property service. This must happen
657 * after the ro.foo properties are set above so
658 * that /data/local.prop cannot interfere with them.
659 */
Colin Crossd11beb22010-04-13 19:33:37 -0700660 start_property_service();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700661
Colin Cross9c5366b2010-04-13 19:48:59 -0700662 signal_init();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700663
664 /* make sure we actually have all the pieces we need */
Colin Cross0dd7ca62010-04-13 19:25:51 -0700665 if ((get_device_fd() < 0) ||
Colin Crossd11beb22010-04-13 19:33:37 -0700666 (get_property_set_fd() < 0) ||
Colin Cross9c5366b2010-04-13 19:48:59 -0700667 (get_signal_fd() < 0)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700668 ERROR("init startup failure\n");
669 return 1;
670 }
671
672 /* execute all the boot actions to get us started */
673 action_for_each_trigger("early-boot", action_add_queue_tail);
674 action_for_each_trigger("boot", action_add_queue_tail);
675 drain_action_queue();
676
677 /* run all property triggers based on current state of the properties */
678 queue_all_property_triggers();
679 drain_action_queue();
680
681 /* enable property triggers */
682 property_triggers_enabled = 1;
683
Colin Cross0dd7ca62010-04-13 19:25:51 -0700684 ufds[0].fd = get_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700685 ufds[0].events = POLLIN;
Colin Crossd11beb22010-04-13 19:33:37 -0700686 ufds[1].fd = get_property_set_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700687 ufds[1].events = POLLIN;
Colin Cross9c5366b2010-04-13 19:48:59 -0700688 ufds[2].fd = get_signal_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700689 ufds[2].events = POLLIN;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800690 fd_count = 3;
691
Colin Crossa8666952010-04-13 19:20:44 -0700692 if (get_keychord_fd() > 0) {
693 ufds[3].fd = get_keychord_fd();
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800694 ufds[3].events = POLLIN;
695 fd_count++;
696 } else {
697 ufds[3].events = 0;
698 ufds[3].revents = 0;
699 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700700
701#if BOOTCHART
The Android Open Source Project35237d12008-12-17 18:08:08 -0800702 bootchart_count = bootchart_init();
703 if (bootchart_count < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700704 ERROR("bootcharting init failure\n");
The Android Open Source Project35237d12008-12-17 18:08:08 -0800705 } else if (bootchart_count > 0) {
706 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
707 } else {
708 NOTICE("bootcharting ignored\n");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700709 }
710#endif
711
712 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800713 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700714
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800715 for (i = 0; i < fd_count; i++)
716 ufds[i].revents = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700717
718 drain_action_queue();
719 restart_processes();
720
721 if (process_needs_restart) {
722 timeout = (process_needs_restart - gettime()) * 1000;
723 if (timeout < 0)
724 timeout = 0;
725 }
726
727#if BOOTCHART
728 if (bootchart_count > 0) {
729 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
730 timeout = BOOTCHART_POLLING_MS;
731 if (bootchart_step() < 0 || --bootchart_count == 0) {
732 bootchart_finish();
733 bootchart_count = 0;
734 }
735 }
736#endif
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800737 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700738 if (nr <= 0)
739 continue;
740
741 if (ufds[2].revents == POLLIN) {
Colin Cross9c5366b2010-04-13 19:48:59 -0700742 handle_signal();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700743 continue;
744 }
745
746 if (ufds[0].revents == POLLIN)
Colin Cross0dd7ca62010-04-13 19:25:51 -0700747 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700748
749 if (ufds[1].revents == POLLIN)
Colin Crossd11beb22010-04-13 19:33:37 -0700750 handle_property_set_fd();
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800751 if (ufds[3].revents == POLLIN)
Colin Crossa8666952010-04-13 19:20:44 -0700752 handle_keychord();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700753 }
754
755 return 0;
756}