init.c revision 504bc5175a8fe5a2f2552903afee761a86283cf4
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <signal.h>
24#include <sys/wait.h>
25#include <sys/mount.h>
26#include <sys/stat.h>
27#include <sys/poll.h>
28#include <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>
37#include <cutils/iosched_policy.h>
38#include <termios.h>
39
40#include <sys/system_properties.h>
41
42#include "devices.h"
43#include "init.h"
44#include "property_service.h"
45#include "bootchart.h"
46#include "keychords.h"
47#include "parser.h"
48
49static int property_triggers_enabled = 0;
50
51#if BOOTCHART
52static int   bootchart_count;
53#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
65static 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
121static 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
136void service_start(struct service *svc, const char *dynamic_args)
137{
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
172    if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
173        ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
174               svc->args[0]);
175        svc->flags |= SVC_DISABLED;
176        return;
177    }
178
179    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
206        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
213        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
231    /* as requested, set our gid, supplemental gids, and uid */
232        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
242        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 {
247            char *arg_ptrs[SVC_MAXARGS+1];
248            int arg_idx = svc->nargs;
249            char *tmp = strdup(dynamic_args);
250            char *next = tmp;
251            char *bword;
252
253            /* Copy the static arguments */
254            memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
255
256            while((bword = strsep(&next, " "))) {
257                arg_ptrs[arg_idx++] = bword;
258                if (arg_idx == SVC_MAXARGS)
259                    break;
260            }
261            arg_ptrs[arg_idx] = '\0';
262            execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
263        }
264        _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
378    svc->flags |= SVC_RESTARTING;
379
380    /* 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    }
385    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);
395        service_start(svc, NULL);
396        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{
421    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);
429        args = strchr(tmp, ':');
430        *args = '\0';
431        args++;
432
433        svc = service_find_by_name(tmp);
434    }
435
436    if (svc) {
437        service_start(svc, args);
438    } else {
439        ERROR("no such service '%s'\n", name);
440    }
441    if (tmp)
442        free(tmp);
443}
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 {
452        ERROR("no such service '%s'\n", name);
453    }
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
467static 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
583void drain_action_queue(void)
584{
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{
623    int signal_recv_fd = -1;
624    int fd_count;
625    int s[2];
626    int fd;
627    struct sigaction act;
628    char tmp[PROP_VALUE_MAX];
629    struct pollfd ufds[4];
630    char *tmpdev;
631    char* debuggable;
632
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");
681    device_init();
682
683    property_init();
684
685    // only listen for keychords if ro.debuggable is true
686    keychord_init();
687
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) ) {
699    fd = open("/dev/tty0", O_WRONLY);
700    if (fd >= 0) {
701        const char *msg;
702            msg = "\n"
703        "\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    }
720    }
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);
744    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);
747    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         */
754    start_property_service();
755
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 */
767    if ((get_device_fd() < 0) ||
768        (get_property_set_fd() < 0) ||
769        (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
786    ufds[0].fd = get_device_fd();
787    ufds[0].events = POLLIN;
788    ufds[1].fd = get_property_set_fd();
789    ufds[1].events = POLLIN;
790    ufds[2].fd = signal_recv_fd;
791    ufds[2].events = POLLIN;
792    fd_count = 3;
793
794    if (get_keychord_fd() > 0) {
795        ufds[3].fd = get_keychord_fd();
796        ufds[3].events = POLLIN;
797        fd_count++;
798    } else {
799        ufds[3].events = 0;
800        ufds[3].revents = 0;
801    }
802
803#if BOOTCHART
804    bootchart_count = bootchart_init();
805    if (bootchart_count < 0) {
806        ERROR("bootcharting init failure\n");
807    } 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");
811    }
812#endif
813
814    for(;;) {
815        int nr, i, timeout = -1;
816
817        for (i = 0; i < fd_count; i++)
818            ufds[i].revents = 0;
819
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
839        nr = poll(ufds, fd_count, timeout);
840        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)
852            handle_device_fd();
853
854        if (ufds[1].revents == POLLIN)
855            handle_property_set_fd();
856        if (ufds[3].revents == POLLIN)
857            handle_keychord();
858    }
859
860    return 0;
861}
862