blob: 3d619b904deb6db32e406ab43b0194c5a0bd5dac [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"
Colin Cross3899e9f2010-04-13 20:35:46 -070048#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049
50static int property_triggers_enabled = 0;
51
52#if BOOTCHART
53static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#endif
55
56static char console[32];
57static char serialno[32];
58static char bootmode[32];
59static char baseband[32];
60static char carrier[32];
61static char bootloader[32];
62static char hardware[32];
63static unsigned revision = 0;
64static char qemu[32];
65
Colin Cross9c5366b2010-04-13 19:48:59 -070066void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067{
68 char pname[PROP_NAME_MAX];
69 int len = strlen(name);
70 if ((len + 10) > PROP_NAME_MAX)
71 return;
72 snprintf(pname, sizeof(pname), "init.svc.%s", name);
73 property_set(pname, state);
74}
75
76static int have_console;
77static char *console_name = "/dev/console";
78static time_t process_needs_restart;
79
80static const char *ENV[32];
81
82/* add_environment - add "key=value" to the current environment */
83int add_environment(const char *key, const char *val)
84{
85 int n;
86
87 for (n = 0; n < 31; n++) {
88 if (!ENV[n]) {
89 size_t len = strlen(key) + strlen(val) + 2;
90 char *entry = malloc(len);
91 snprintf(entry, len, "%s=%s", key, val);
92 ENV[n] = entry;
93 return 0;
94 }
95 }
96
97 return 1;
98}
99
100static void zap_stdio(void)
101{
102 int fd;
103 fd = open("/dev/null", O_RDWR);
104 dup2(fd, 0);
105 dup2(fd, 1);
106 dup2(fd, 2);
107 close(fd);
108}
109
110static void open_console()
111{
112 int fd;
113 if ((fd = open(console_name, O_RDWR)) < 0) {
114 fd = open("/dev/null", O_RDWR);
115 }
116 dup2(fd, 0);
117 dup2(fd, 1);
118 dup2(fd, 2);
119 close(fd);
120}
121
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700122static void publish_socket(const char *name, int fd)
123{
124 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
125 char val[64];
126
127 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
128 name,
129 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
130 snprintf(val, sizeof(val), "%d", fd);
131 add_environment(key, val);
132
133 /* make sure we don't close-on-exec */
134 fcntl(fd, F_SETFD, 0);
135}
136
San Mehatf24e2522009-05-19 13:30:46 -0700137void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700138{
139 struct stat s;
140 pid_t pid;
141 int needs_console;
142 int n;
143
144 /* starting a service removes it from the disabled
145 * state and immediately takes it out of the restarting
146 * state if it was in there
147 */
148 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING));
149 svc->time_started = 0;
150
151 /* running processes require no additional work -- if
152 * they're in the process of exiting, we've ensured
153 * that they will immediately restart on exit, unless
154 * they are ONESHOT
155 */
156 if (svc->flags & SVC_RUNNING) {
157 return;
158 }
159
160 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
161 if (needs_console && (!have_console)) {
162 ERROR("service '%s' requires console\n", svc->name);
163 svc->flags |= SVC_DISABLED;
164 return;
165 }
166
167 if (stat(svc->args[0], &s) != 0) {
168 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
169 svc->flags |= SVC_DISABLED;
170 return;
171 }
172
San Mehatf24e2522009-05-19 13:30:46 -0700173 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700174 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
175 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700176 svc->flags |= SVC_DISABLED;
177 return;
178 }
179
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700180 NOTICE("starting '%s'\n", svc->name);
181
182 pid = fork();
183
184 if (pid == 0) {
185 struct socketinfo *si;
186 struct svcenvinfo *ei;
187 char tmp[32];
188 int fd, sz;
189
190 get_property_workspace(&fd, &sz);
191 sprintf(tmp, "%d,%d", dup(fd), sz);
192 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
193
194 for (ei = svc->envvars; ei; ei = ei->next)
195 add_environment(ei->name, ei->value);
196
197 for (si = svc->sockets; si; si = si->next) {
198 int s = create_socket(si->name,
199 !strcmp(si->type, "dgram") ?
200 SOCK_DGRAM : SOCK_STREAM,
201 si->perm, si->uid, si->gid);
202 if (s >= 0) {
203 publish_socket(si->name, s);
204 }
205 }
206
San Mehat4e221f02010-02-25 14:19:50 -0800207 if (svc->ioprio_class != IoSchedClass_NONE) {
208 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
209 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
210 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
211 }
212 }
213
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214 if (needs_console) {
215 setsid();
216 open_console();
217 } else {
218 zap_stdio();
219 }
220
221#if 0
222 for (n = 0; svc->args[n]; n++) {
223 INFO("args[%d] = '%s'\n", n, svc->args[n]);
224 }
225 for (n = 0; ENV[n]; n++) {
226 INFO("env[%d] = '%s'\n", n, ENV[n]);
227 }
228#endif
229
230 setpgid(0, getpid());
231
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800232 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700233 if (svc->gid) {
234 setgid(svc->gid);
235 }
236 if (svc->nr_supp_gids) {
237 setgroups(svc->nr_supp_gids, svc->supp_gids);
238 }
239 if (svc->uid) {
240 setuid(svc->uid);
241 }
242
San Mehat8ad15682009-05-20 08:50:40 -0700243 if (!dynamic_args) {
244 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
245 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
246 }
247 } else {
San Mehatf24e2522009-05-19 13:30:46 -0700248 char *arg_ptrs[SVC_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700249 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700250 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700251 char *next = tmp;
252 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700253
254 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700255 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700256
San Mehatd4cdd132009-05-20 09:52:16 -0700257 while((bword = strsep(&next, " "))) {
258 arg_ptrs[arg_idx++] = bword;
259 if (arg_idx == SVC_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700260 break;
San Mehatf24e2522009-05-19 13:30:46 -0700261 }
262 arg_ptrs[arg_idx] = '\0';
263 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100264 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265 _exit(127);
266 }
267
268 if (pid < 0) {
269 ERROR("failed to start '%s'\n", svc->name);
270 svc->pid = 0;
271 return;
272 }
273
274 svc->time_started = gettime();
275 svc->pid = pid;
276 svc->flags |= SVC_RUNNING;
277
278 notify_service_state(svc->name, "running");
279}
280
281void service_stop(struct service *svc)
282{
283 /* we are no longer running, nor should we
284 * attempt to restart
285 */
286 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
287
288 /* if the service has not yet started, prevent
289 * it from auto-starting with its class
290 */
291 svc->flags |= SVC_DISABLED;
292
293 if (svc->pid) {
294 NOTICE("service '%s' is being killed\n", svc->name);
295 kill(-svc->pid, SIGTERM);
296 notify_service_state(svc->name, "stopping");
297 } else {
298 notify_service_state(svc->name, "stopped");
299 }
300}
301
302void property_changed(const char *name, const char *value)
303{
304 if (property_triggers_enabled) {
305 queue_property_triggers(name, value);
306 drain_action_queue();
307 }
308}
309
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310static void restart_service_if_needed(struct service *svc)
311{
312 time_t next_start_time = svc->time_started + 5;
313
314 if (next_start_time <= gettime()) {
315 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700316 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317 return;
318 }
319
320 if ((next_start_time < process_needs_restart) ||
321 (process_needs_restart == 0)) {
322 process_needs_restart = next_start_time;
323 }
324}
325
326static void restart_processes()
327{
328 process_needs_restart = 0;
329 service_for_each_flags(SVC_RESTARTING,
330 restart_service_if_needed);
331}
332
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700333static void msg_start(const char *name)
334{
San Mehatf24e2522009-05-19 13:30:46 -0700335 struct service *svc;
336 char *tmp = NULL;
337 char *args = NULL;
338
339 if (!strchr(name, ':'))
340 svc = service_find_by_name(name);
341 else {
342 tmp = strdup(name);
San Mehatf24e2522009-05-19 13:30:46 -0700343 args = strchr(tmp, ':');
344 *args = '\0';
345 args++;
346
347 svc = service_find_by_name(tmp);
348 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700349
350 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700351 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352 } else {
353 ERROR("no such service '%s'\n", name);
354 }
San Mehatf24e2522009-05-19 13:30:46 -0700355 if (tmp)
356 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357}
358
359static void msg_stop(const char *name)
360{
361 struct service *svc = service_find_by_name(name);
362
363 if (svc) {
364 service_stop(svc);
365 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700366 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700367 }
368}
369
370void handle_control_message(const char *msg, const char *arg)
371{
372 if (!strcmp(msg,"start")) {
373 msg_start(arg);
374 } else if (!strcmp(msg,"stop")) {
375 msg_stop(arg);
376 } else {
377 ERROR("unknown control msg '%s'\n", msg);
378 }
379}
380
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381static void import_kernel_nv(char *name, int in_qemu)
382{
383 char *value = strchr(name, '=');
384
385 if (value == 0) return;
386 *value++ = 0;
387 if (*name == 0) return;
388
389 if (!in_qemu)
390 {
391 /* on a real device, white-list the kernel options */
392 if (!strcmp(name,"qemu")) {
393 strlcpy(qemu, value, sizeof(qemu));
394 } else if (!strcmp(name,"androidboot.console")) {
395 strlcpy(console, value, sizeof(console));
396 } else if (!strcmp(name,"androidboot.mode")) {
397 strlcpy(bootmode, value, sizeof(bootmode));
398 } else if (!strcmp(name,"androidboot.serialno")) {
399 strlcpy(serialno, value, sizeof(serialno));
400 } else if (!strcmp(name,"androidboot.baseband")) {
401 strlcpy(baseband, value, sizeof(baseband));
402 } else if (!strcmp(name,"androidboot.carrier")) {
403 strlcpy(carrier, value, sizeof(carrier));
404 } else if (!strcmp(name,"androidboot.bootloader")) {
405 strlcpy(bootloader, value, sizeof(bootloader));
406 } else if (!strcmp(name,"androidboot.hardware")) {
407 strlcpy(hardware, value, sizeof(hardware));
408 } else {
409 qemu_cmdline(name, value);
410 }
411 } else {
412 /* in the emulator, export any kernel option with the
413 * ro.kernel. prefix */
414 char buff[32];
415 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
416 if (len < (int)sizeof(buff)) {
417 property_set( buff, value );
418 }
419 }
420}
421
422static void import_kernel_cmdline(int in_qemu)
423{
424 char cmdline[1024];
425 char *ptr;
426 int fd;
427
428 fd = open("/proc/cmdline", O_RDONLY);
429 if (fd >= 0) {
430 int n = read(fd, cmdline, 1023);
431 if (n < 0) n = 0;
432
433 /* get rid of trailing newline, it happens */
434 if (n > 0 && cmdline[n-1] == '\n') n--;
435
436 cmdline[n] = 0;
437 close(fd);
438 } else {
439 cmdline[0] = 0;
440 }
441
442 ptr = cmdline;
443 while (ptr && *ptr) {
444 char *x = strchr(ptr, ' ');
445 if (x != 0) *x++ = 0;
446 import_kernel_nv(ptr, in_qemu);
447 ptr = x;
448 }
449
450 /* don't expose the raw commandline to nonpriv processes */
451 chmod("/proc/cmdline", 0440);
452}
453
454static void get_hardware_name(void)
455{
456 char data[1024];
457 int fd, n;
458 char *x, *hw, *rev;
459
460 /* Hardware string was provided on kernel command line */
461 if (hardware[0])
462 return;
463
464 fd = open("/proc/cpuinfo", O_RDONLY);
465 if (fd < 0) return;
466
467 n = read(fd, data, 1023);
468 close(fd);
469 if (n < 0) return;
470
471 data[n] = 0;
472 hw = strstr(data, "\nHardware");
473 rev = strstr(data, "\nRevision");
474
475 if (hw) {
476 x = strstr(hw, ": ");
477 if (x) {
478 x += 2;
479 n = 0;
480 while (*x && !isspace(*x)) {
481 hardware[n++] = tolower(*x);
482 x++;
483 if (n == 31) break;
484 }
485 hardware[n] = 0;
486 }
487 }
488
489 if (rev) {
490 x = strstr(rev, ": ");
491 if (x) {
492 revision = strtoul(x + 2, 0, 16);
493 }
494 }
495}
496
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000497void drain_action_queue(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700498{
499 struct listnode *node;
500 struct command *cmd;
501 struct action *act;
502 int ret;
503
504 while ((act = action_remove_queue_head())) {
505 INFO("processing action %p (%s)\n", act, act->name);
506 list_for_each(node, &act->commands) {
507 cmd = node_to_item(node, struct command, clist);
508 ret = cmd->func(cmd->nargs, cmd->args);
509 INFO("command '%s' r=%d\n", cmd->args[0], ret);
510 }
511 }
512}
513
514void open_devnull_stdio(void)
515{
516 int fd;
517 static const char *name = "/dev/__null__";
518 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) {
519 fd = open(name, O_RDWR);
520 unlink(name);
521 if (fd >= 0) {
522 dup2(fd, 0);
523 dup2(fd, 1);
524 dup2(fd, 2);
525 if (fd > 2) {
526 close(fd);
527 }
528 return;
529 }
530 }
531
532 exit(1);
533}
534
535int main(int argc, char **argv)
536{
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800537 int fd_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700538 int s[2];
539 int fd;
540 struct sigaction act;
541 char tmp[PROP_VALUE_MAX];
542 struct pollfd ufds[4];
543 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800544 char* debuggable;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700545
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700546 /* clear the umask */
547 umask(0);
548
549 /* Get the basic filesystem setup we need put
550 * together in the initramdisk on / and then we'll
551 * let the rc file figure out the rest.
552 */
553 mkdir("/dev", 0755);
554 mkdir("/proc", 0755);
555 mkdir("/sys", 0755);
556
557 mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755");
558 mkdir("/dev/pts", 0755);
559 mkdir("/dev/socket", 0755);
560 mount("devpts", "/dev/pts", "devpts", 0, NULL);
561 mount("proc", "/proc", "proc", 0, NULL);
562 mount("sysfs", "/sys", "sysfs", 0, NULL);
563
564 /* We must have some place other than / to create the
565 * device nodes for kmsg and null, otherwise we won't
566 * be able to remount / read-only later on.
567 * Now that tmpfs is mounted on /dev, we can actually
568 * talk to the outside world.
569 */
570 open_devnull_stdio();
571 log_init();
572
573 INFO("reading config file\n");
574 parse_config_file("/init.rc");
575
576 /* pull the kernel commandline and ramdisk properties file in */
577 qemu_init();
578 import_kernel_cmdline(0);
579
580 get_hardware_name();
581 snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
582 parse_config_file(tmp);
583
584 action_for_each_trigger("early-init", action_add_queue_tail);
585 drain_action_queue();
586
587 INFO("device init\n");
Colin Cross0dd7ca62010-04-13 19:25:51 -0700588 device_init();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700589
590 property_init();
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800591
592 // only listen for keychords if ro.debuggable is true
Colin Crossa8666952010-04-13 19:20:44 -0700593 keychord_init();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700594
595 if (console[0]) {
596 snprintf(tmp, sizeof(tmp), "/dev/%s", console);
597 console_name = strdup(tmp);
598 }
599
600 fd = open(console_name, O_RDWR);
601 if (fd >= 0)
602 have_console = 1;
603 close(fd);
604
605 if( load_565rle_image(INIT_IMAGE_FILE) ) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800606 fd = open("/dev/tty0", O_WRONLY);
607 if (fd >= 0) {
608 const char *msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700609 msg = "\n"
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800610 "\n"
611 "\n"
612 "\n"
613 "\n"
614 "\n"
615 "\n" // console is 40 cols x 30 lines
616 "\n"
617 "\n"
618 "\n"
619 "\n"
620 "\n"
621 "\n"
622 "\n"
623 " A N D R O I D ";
624 write(fd, msg, strlen(msg));
625 close(fd);
626 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627 }
628
629 if (qemu[0])
630 import_kernel_cmdline(1);
631
632 if (!strcmp(bootmode,"factory"))
633 property_set("ro.factorytest", "1");
634 else if (!strcmp(bootmode,"factory2"))
635 property_set("ro.factorytest", "2");
636 else
637 property_set("ro.factorytest", "0");
638
639 property_set("ro.serialno", serialno[0] ? serialno : "");
640 property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown");
641 property_set("ro.baseband", baseband[0] ? baseband : "unknown");
642 property_set("ro.carrier", carrier[0] ? carrier : "unknown");
643 property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown");
644
645 property_set("ro.hardware", hardware);
646 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
647 property_set("ro.revision", tmp);
648
649 /* execute all the boot actions to get us started */
650 action_for_each_trigger("init", action_add_queue_tail);
Colin Cross31712be2010-04-09 12:26:06 -0700651 action_for_each_trigger("early-fs", action_add_queue_tail);
652 action_for_each_trigger("fs", action_add_queue_tail);
653 action_for_each_trigger("post-fs", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700654 drain_action_queue();
655
656 /* read any property files on system or data and
657 * fire up the property service. This must happen
658 * after the ro.foo properties are set above so
659 * that /data/local.prop cannot interfere with them.
660 */
Colin Crossd11beb22010-04-13 19:33:37 -0700661 start_property_service();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700662
Colin Cross9c5366b2010-04-13 19:48:59 -0700663 signal_init();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700664
665 /* make sure we actually have all the pieces we need */
Colin Cross0dd7ca62010-04-13 19:25:51 -0700666 if ((get_device_fd() < 0) ||
Colin Crossd11beb22010-04-13 19:33:37 -0700667 (get_property_set_fd() < 0) ||
Colin Cross9c5366b2010-04-13 19:48:59 -0700668 (get_signal_fd() < 0)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700669 ERROR("init startup failure\n");
670 return 1;
671 }
672
673 /* execute all the boot actions to get us started */
674 action_for_each_trigger("early-boot", action_add_queue_tail);
675 action_for_each_trigger("boot", action_add_queue_tail);
676 drain_action_queue();
677
678 /* run all property triggers based on current state of the properties */
679 queue_all_property_triggers();
680 drain_action_queue();
681
682 /* enable property triggers */
683 property_triggers_enabled = 1;
684
Colin Cross0dd7ca62010-04-13 19:25:51 -0700685 ufds[0].fd = get_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700686 ufds[0].events = POLLIN;
Colin Crossd11beb22010-04-13 19:33:37 -0700687 ufds[1].fd = get_property_set_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700688 ufds[1].events = POLLIN;
Colin Cross9c5366b2010-04-13 19:48:59 -0700689 ufds[2].fd = get_signal_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700690 ufds[2].events = POLLIN;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800691 fd_count = 3;
692
Colin Crossa8666952010-04-13 19:20:44 -0700693 if (get_keychord_fd() > 0) {
694 ufds[3].fd = get_keychord_fd();
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800695 ufds[3].events = POLLIN;
696 fd_count++;
697 } else {
698 ufds[3].events = 0;
699 ufds[3].revents = 0;
700 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700701
702#if BOOTCHART
The Android Open Source Project35237d12008-12-17 18:08:08 -0800703 bootchart_count = bootchart_init();
704 if (bootchart_count < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700705 ERROR("bootcharting init failure\n");
The Android Open Source Project35237d12008-12-17 18:08:08 -0800706 } else if (bootchart_count > 0) {
707 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
708 } else {
709 NOTICE("bootcharting ignored\n");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700710 }
711#endif
712
713 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800714 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700715
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800716 for (i = 0; i < fd_count; i++)
717 ufds[i].revents = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700718
719 drain_action_queue();
720 restart_processes();
721
722 if (process_needs_restart) {
723 timeout = (process_needs_restart - gettime()) * 1000;
724 if (timeout < 0)
725 timeout = 0;
726 }
727
728#if BOOTCHART
729 if (bootchart_count > 0) {
730 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
731 timeout = BOOTCHART_POLLING_MS;
732 if (bootchart_step() < 0 || --bootchart_count == 0) {
733 bootchart_finish();
734 bootchart_count = 0;
735 }
736 }
737#endif
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800738 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700739 if (nr <= 0)
740 continue;
741
742 if (ufds[2].revents == POLLIN) {
Colin Cross9c5366b2010-04-13 19:48:59 -0700743 handle_signal();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700744 continue;
745 }
746
747 if (ufds[0].revents == POLLIN)
Colin Cross0dd7ca62010-04-13 19:25:51 -0700748 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700749
750 if (ufds[1].revents == POLLIN)
Colin Crossd11beb22010-04-13 19:33:37 -0700751 handle_property_set_fd();
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800752 if (ufds[3].revents == POLLIN)
Colin Crossa8666952010-04-13 19:20:44 -0700753 handle_keychord();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700754 }
755
756 return 0;
757}