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