init.c revision 5511c84a50617d1b3c280beb49de38ed1aae21c6
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 <libgen.h>
35
36#include <cutils/list.h>
37#include <cutils/sockets.h>
38#include <cutils/iosched_policy.h>
39#include <private/android_filesystem_config.h>
40#include <termios.h>
41
42#include <sys/system_properties.h>
43
44#include "devices.h"
45#include "init.h"
46#include "log.h"
47#include "property_service.h"
48#include "bootchart.h"
49#include "signal_handler.h"
50#include "keychords.h"
51#include "init_parser.h"
52#include "util.h"
53#include "ueventd.h"
54
55static int property_triggers_enabled = 0;
56
57#if BOOTCHART
58static int   bootchart_count;
59#endif
60
61static char console[32];
62static char bootmode[32];
63static char hardware[32];
64static unsigned revision = 0;
65static char qemu[32];
66
67static struct action *cur_action = NULL;
68static struct command *cur_command = NULL;
69static struct listnode *command_queue = NULL;
70
71void notify_service_state(const char *name, const char *state)
72{
73    char pname[PROP_NAME_MAX];
74    int len = strlen(name);
75    if ((len + 10) > PROP_NAME_MAX)
76        return;
77    snprintf(pname, sizeof(pname), "init.svc.%s", name);
78    property_set(pname, state);
79}
80
81static int have_console;
82static char *console_name = "/dev/console";
83static time_t process_needs_restart;
84
85static const char *ENV[32];
86
87/* add_environment - add "key=value" to the current environment */
88int add_environment(const char *key, const char *val)
89{
90    int n;
91
92    for (n = 0; n < 31; n++) {
93        if (!ENV[n]) {
94            size_t len = strlen(key) + strlen(val) + 2;
95            char *entry = malloc(len);
96            snprintf(entry, len, "%s=%s", key, val);
97            ENV[n] = entry;
98            return 0;
99        }
100    }
101
102    return 1;
103}
104
105static void zap_stdio(void)
106{
107    int fd;
108    fd = open("/dev/null", O_RDWR);
109    dup2(fd, 0);
110    dup2(fd, 1);
111    dup2(fd, 2);
112    close(fd);
113}
114
115static void open_console()
116{
117    int fd;
118    if ((fd = open(console_name, O_RDWR)) < 0) {
119        fd = open("/dev/null", O_RDWR);
120    }
121    dup2(fd, 0);
122    dup2(fd, 1);
123    dup2(fd, 2);
124    close(fd);
125}
126
127static void publish_socket(const char *name, int fd)
128{
129    char key[64] = ANDROID_SOCKET_ENV_PREFIX;
130    char val[64];
131
132    strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
133            name,
134            sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
135    snprintf(val, sizeof(val), "%d", fd);
136    add_environment(key, val);
137
138    /* make sure we don't close-on-exec */
139    fcntl(fd, F_SETFD, 0);
140}
141
142void service_start(struct service *svc, const char *dynamic_args)
143{
144    struct stat s;
145    pid_t pid;
146    int needs_console;
147    int n;
148
149        /* starting a service removes it from the disabled or reset
150         * state and immediately takes it out of the restarting
151         * state if it was in there
152         */
153    svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));
154    svc->time_started = 0;
155
156        /* running processes require no additional work -- if
157         * they're in the process of exiting, we've ensured
158         * that they will immediately restart on exit, unless
159         * they are ONESHOT
160         */
161    if (svc->flags & SVC_RUNNING) {
162        return;
163    }
164
165    needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
166    if (needs_console && (!have_console)) {
167        ERROR("service '%s' requires console\n", svc->name);
168        svc->flags |= SVC_DISABLED;
169        return;
170    }
171
172    if (stat(svc->args[0], &s) != 0) {
173        ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
174        svc->flags |= SVC_DISABLED;
175        return;
176    }
177
178    if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
179        ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
180               svc->args[0]);
181        svc->flags |= SVC_DISABLED;
182        return;
183    }
184
185    NOTICE("starting '%s'\n", svc->name);
186
187    pid = fork();
188
189    if (pid == 0) {
190        struct socketinfo *si;
191        struct svcenvinfo *ei;
192        char tmp[32];
193        int fd, sz;
194
195        if (properties_inited()) {
196            get_property_workspace(&fd, &sz);
197            sprintf(tmp, "%d,%d", dup(fd), sz);
198            add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
199        }
200
201        for (ei = svc->envvars; ei; ei = ei->next)
202            add_environment(ei->name, ei->value);
203
204        for (si = svc->sockets; si; si = si->next) {
205            int socket_type = (
206                    !strcmp(si->type, "stream") ? SOCK_STREAM :
207                        (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
208            int s = create_socket(si->name, socket_type,
209                                  si->perm, si->uid, si->gid);
210            if (s >= 0) {
211                publish_socket(si->name, s);
212            }
213        }
214
215        if (svc->ioprio_class != IoSchedClass_NONE) {
216            if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
217                ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
218                      getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
219            }
220        }
221
222        if (needs_console) {
223            setsid();
224            open_console();
225        } else {
226            zap_stdio();
227        }
228
229#if 0
230        for (n = 0; svc->args[n]; n++) {
231            INFO("args[%d] = '%s'\n", n, svc->args[n]);
232        }
233        for (n = 0; ENV[n]; n++) {
234            INFO("env[%d] = '%s'\n", n, ENV[n]);
235        }
236#endif
237
238        setpgid(0, getpid());
239
240    /* as requested, set our gid, supplemental gids, and uid */
241        if (svc->gid) {
242            if (setgid(svc->gid) != 0) {
243                ERROR("setgid failed: %s\n", strerror(errno));
244                _exit(127);
245            }
246        }
247        if (svc->nr_supp_gids) {
248            if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
249                ERROR("setgroups failed: %s\n", strerror(errno));
250                _exit(127);
251            }
252        }
253        if (svc->uid) {
254            if (setuid(svc->uid) != 0) {
255                ERROR("setuid failed: %s\n", strerror(errno));
256                _exit(127);
257            }
258        }
259
260        if (!dynamic_args) {
261            if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
262                ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
263            }
264        } else {
265            char *arg_ptrs[INIT_PARSER_MAXARGS+1];
266            int arg_idx = svc->nargs;
267            char *tmp = strdup(dynamic_args);
268            char *next = tmp;
269            char *bword;
270
271            /* Copy the static arguments */
272            memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
273
274            while((bword = strsep(&next, " "))) {
275                arg_ptrs[arg_idx++] = bword;
276                if (arg_idx == INIT_PARSER_MAXARGS)
277                    break;
278            }
279            arg_ptrs[arg_idx] = '\0';
280            execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
281        }
282        _exit(127);
283    }
284
285    if (pid < 0) {
286        ERROR("failed to start '%s'\n", svc->name);
287        svc->pid = 0;
288        return;
289    }
290
291    svc->time_started = gettime();
292    svc->pid = pid;
293    svc->flags |= SVC_RUNNING;
294
295    if (properties_inited())
296        notify_service_state(svc->name, "running");
297}
298
299/* The how field should be either SVC_DISABLED or SVC_RESET */
300static void service_stop_or_reset(struct service *svc, int how)
301{
302        /* we are no longer running, nor should we
303         * attempt to restart
304         */
305    svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
306
307    if ((how != SVC_DISABLED) && (how != SVC_RESET)) {
308        /* Hrm, an illegal flag.  Default to SVC_DISABLED */
309        how = SVC_DISABLED;
310    }
311        /* if the service has not yet started, prevent
312         * it from auto-starting with its class
313         */
314    if (how == SVC_RESET) {
315        svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
316    } else {
317        svc->flags |= how;
318    }
319
320    if (svc->pid) {
321        NOTICE("service '%s' is being killed\n", svc->name);
322        kill(-svc->pid, SIGKILL);
323        notify_service_state(svc->name, "stopping");
324    } else {
325        notify_service_state(svc->name, "stopped");
326    }
327}
328
329void service_reset(struct service *svc)
330{
331    service_stop_or_reset(svc, SVC_RESET);
332}
333
334void service_stop(struct service *svc)
335{
336    service_stop_or_reset(svc, SVC_DISABLED);
337}
338
339void property_changed(const char *name, const char *value)
340{
341    if (property_triggers_enabled)
342        queue_property_triggers(name, value);
343}
344
345static void restart_service_if_needed(struct service *svc)
346{
347    time_t next_start_time = svc->time_started + 5;
348
349    if (next_start_time <= gettime()) {
350        svc->flags &= (~SVC_RESTARTING);
351        service_start(svc, NULL);
352        return;
353    }
354
355    if ((next_start_time < process_needs_restart) ||
356        (process_needs_restart == 0)) {
357        process_needs_restart = next_start_time;
358    }
359}
360
361static void restart_processes()
362{
363    process_needs_restart = 0;
364    service_for_each_flags(SVC_RESTARTING,
365                           restart_service_if_needed);
366}
367
368static void msg_start(const char *name)
369{
370    struct service *svc;
371    char *tmp = NULL;
372    char *args = NULL;
373
374    if (!strchr(name, ':'))
375        svc = service_find_by_name(name);
376    else {
377        tmp = strdup(name);
378        args = strchr(tmp, ':');
379        *args = '\0';
380        args++;
381
382        svc = service_find_by_name(tmp);
383    }
384
385    if (svc) {
386        service_start(svc, args);
387    } else {
388        ERROR("no such service '%s'\n", name);
389    }
390    if (tmp)
391        free(tmp);
392}
393
394static void msg_stop(const char *name)
395{
396    struct service *svc = service_find_by_name(name);
397
398    if (svc) {
399        service_stop(svc);
400    } else {
401        ERROR("no such service '%s'\n", name);
402    }
403}
404
405void handle_control_message(const char *msg, const char *arg)
406{
407    if (!strcmp(msg,"start")) {
408        msg_start(arg);
409    } else if (!strcmp(msg,"stop")) {
410        msg_stop(arg);
411    } else if (!strcmp(msg,"restart")) {
412        msg_stop(arg);
413        msg_start(arg);
414    } else {
415        ERROR("unknown control msg '%s'\n", msg);
416    }
417}
418
419static struct command *get_first_command(struct action *act)
420{
421    struct listnode *node;
422    node = list_head(&act->commands);
423    if (!node || list_empty(&act->commands))
424        return NULL;
425
426    return node_to_item(node, struct command, clist);
427}
428
429static struct command *get_next_command(struct action *act, struct command *cmd)
430{
431    struct listnode *node;
432    node = cmd->clist.next;
433    if (!node)
434        return NULL;
435    if (node == &act->commands)
436        return NULL;
437
438    return node_to_item(node, struct command, clist);
439}
440
441static int is_last_command(struct action *act, struct command *cmd)
442{
443    return (list_tail(&act->commands) == &cmd->clist);
444}
445
446void execute_one_command(void)
447{
448    int ret;
449
450    if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
451        cur_action = action_remove_queue_head();
452        cur_command = NULL;
453        if (!cur_action)
454            return;
455        INFO("processing action %p (%s)\n", cur_action, cur_action->name);
456        cur_command = get_first_command(cur_action);
457    } else {
458        cur_command = get_next_command(cur_action, cur_command);
459    }
460
461    if (!cur_command)
462        return;
463
464    ret = cur_command->func(cur_command->nargs, cur_command->args);
465    INFO("command '%s' r=%d\n", cur_command->args[0], ret);
466}
467
468static int wait_for_coldboot_done_action(int nargs, char **args)
469{
470    int ret;
471    INFO("wait for %s\n", coldboot_done);
472    ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
473    if (ret)
474        ERROR("Timed out waiting for %s\n", coldboot_done);
475    return ret;
476}
477
478static int keychord_init_action(int nargs, char **args)
479{
480    keychord_init();
481    return 0;
482}
483
484static int console_init_action(int nargs, char **args)
485{
486    int fd;
487    char tmp[PROP_VALUE_MAX];
488
489    if (console[0]) {
490        snprintf(tmp, sizeof(tmp), "/dev/%s", console);
491        console_name = strdup(tmp);
492    }
493
494    fd = open(console_name, O_RDWR);
495    if (fd >= 0)
496        have_console = 1;
497    close(fd);
498
499    if( load_565rle_image(INIT_IMAGE_FILE) ) {
500        fd = open("/dev/tty0", O_WRONLY);
501        if (fd >= 0) {
502            const char *msg;
503                msg = "\n"
504            "\n"
505            "\n"
506            "\n"
507            "\n"
508            "\n"
509            "\n"  // console is 40 cols x 30 lines
510            "\n"
511            "\n"
512            "\n"
513            "\n"
514            "\n"
515            "\n"
516            "\n"
517            "             A N D R O I D ";
518            write(fd, msg, strlen(msg));
519            close(fd);
520        }
521    }
522    return 0;
523}
524
525static void import_kernel_nv(char *name, int for_emulator)
526{
527    char *value = strchr(name, '=');
528    int name_len = strlen(name);
529
530    if (value == 0) return;
531    *value++ = 0;
532    if (name_len == 0) return;
533
534    if (for_emulator) {
535        /* in the emulator, export any kernel option with the
536         * ro.kernel. prefix */
537        char buff[PROP_NAME_MAX];
538        int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
539
540        if (len < (int)sizeof(buff))
541            property_set( buff, value );
542        return;
543    }
544
545    if (!strcmp(name,"qemu")) {
546        strlcpy(qemu, value, sizeof(qemu));
547    } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
548        const char *boot_prop_name = name + 12;
549        char prop[PROP_NAME_MAX];
550        int cnt;
551
552        cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
553        if (cnt < PROP_NAME_MAX)
554            property_set(prop, value);
555    }
556}
557
558static void export_kernel_boot_props(void)
559{
560    char tmp[PROP_VALUE_MAX];
561    const char *pval;
562    unsigned i;
563    struct {
564        const char *src_prop;
565        const char *dest_prop;
566        const char *def_val;
567    } prop_map[] = {
568        { "ro.boot.serialno", "ro.serialno", "", },
569        { "ro.boot.mode", "ro.bootmode", "unknown", },
570        { "ro.boot.baseband", "ro.baseband", "unknown", },
571        { "ro.boot.carrier", "ro.carrier", "unknown", },
572        { "ro.boot.bootloader", "ro.bootloader", "unknown", },
573    };
574
575    for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
576        pval = property_get(prop_map[i].src_prop);
577        property_set(prop_map[i].dest_prop, pval ?: prop_map[i].def_val);
578    }
579
580    pval = property_get("ro.boot.console");
581    if (pval)
582        strlcpy(console, pval, sizeof(console));
583
584    /* save a copy for init's usage during boot */
585    strlcpy(bootmode, property_get("ro.bootmode"), sizeof(bootmode));
586
587    /* if this was given on kernel command line, override what we read
588     * before (e.g. from /proc/cpuinfo), if anything */
589    pval = property_get("ro.boot.hardware");
590    if (pval)
591        strlcpy(hardware, pval, sizeof(hardware));
592    property_set("ro.hardware", hardware);
593
594    snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
595    property_set("ro.revision", tmp);
596
597    /* TODO: these are obsolete. We should delete them */
598    if (!strcmp(bootmode,"factory"))
599        property_set("ro.factorytest", "1");
600    else if (!strcmp(bootmode,"factory2"))
601        property_set("ro.factorytest", "2");
602    else
603        property_set("ro.factorytest", "0");
604}
605
606static void process_kernel_cmdline(void)
607{
608    /* don't expose the raw commandline to nonpriv processes */
609    chmod("/proc/cmdline", 0440);
610
611    /* first pass does the common stuff, and finds if we are in qemu.
612     * second pass is only necessary for qemu to export all kernel params
613     * as props.
614     */
615    import_kernel_cmdline(0, import_kernel_nv);
616    if (qemu[0])
617        import_kernel_cmdline(1, import_kernel_nv);
618
619    /* now propogate the info given on command line to internal variables
620     * used by init as well as the current required properties
621     */
622    export_kernel_boot_props();
623}
624
625static int property_service_init_action(int nargs, char **args)
626{
627    /* read any property files on system or data and
628     * fire up the property service.  This must happen
629     * after the ro.foo properties are set above so
630     * that /data/local.prop cannot interfere with them.
631     */
632    start_property_service();
633    return 0;
634}
635
636static int signal_init_action(int nargs, char **args)
637{
638    signal_init();
639    return 0;
640}
641
642static int check_startup_action(int nargs, char **args)
643{
644    /* make sure we actually have all the pieces we need */
645    if ((get_property_set_fd() < 0) ||
646        (get_signal_fd() < 0)) {
647        ERROR("init startup failure\n");
648        exit(1);
649    }
650
651        /* signal that we hit this point */
652    unlink("/dev/.booting");
653
654    return 0;
655}
656
657static int queue_property_triggers_action(int nargs, char **args)
658{
659    queue_all_property_triggers();
660    /* enable property triggers */
661    property_triggers_enabled = 1;
662    return 0;
663}
664
665#if BOOTCHART
666static int bootchart_init_action(int nargs, char **args)
667{
668    bootchart_count = bootchart_init();
669    if (bootchart_count < 0) {
670        ERROR("bootcharting init failure\n");
671    } else if (bootchart_count > 0) {
672        NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
673    } else {
674        NOTICE("bootcharting ignored\n");
675    }
676
677    return 0;
678}
679#endif
680
681int main(int argc, char **argv)
682{
683    int fd_count = 0;
684    struct pollfd ufds[4];
685    char *tmpdev;
686    char* debuggable;
687    char tmp[32];
688    int property_set_fd_init = 0;
689    int signal_fd_init = 0;
690    int keychord_fd_init = 0;
691    bool is_charger = false;
692
693    if (!strcmp(basename(argv[0]), "ueventd"))
694        return ueventd_main(argc, argv);
695
696    /* clear the umask */
697    umask(0);
698
699        /* Get the basic filesystem setup we need put
700         * together in the initramdisk on / and then we'll
701         * let the rc file figure out the rest.
702         */
703    mkdir("/dev", 0755);
704    mkdir("/proc", 0755);
705    mkdir("/sys", 0755);
706
707    mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
708    mkdir("/dev/pts", 0755);
709    mkdir("/dev/socket", 0755);
710    mount("devpts", "/dev/pts", "devpts", 0, NULL);
711    mount("proc", "/proc", "proc", 0, NULL);
712    mount("sysfs", "/sys", "sysfs", 0, NULL);
713
714        /* indicate that booting is in progress to background fw loaders, etc */
715    close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
716
717        /* We must have some place other than / to create the
718         * device nodes for kmsg and null, otherwise we won't
719         * be able to remount / read-only later on.
720         * Now that tmpfs is mounted on /dev, we can actually
721         * talk to the outside world.
722         */
723    open_devnull_stdio();
724    klog_init();
725    property_init();
726
727    get_hardware_name(hardware, &revision);
728
729    process_kernel_cmdline();
730
731    is_charger = !strcmp(bootmode, "charger");
732
733    INFO("property init\n");
734    if (!is_charger)
735        property_load_boot_defaults();
736
737    INFO("reading config file\n");
738    init_parse_config_file("/init.rc");
739
740    action_for_each_trigger("early-init", action_add_queue_tail);
741
742    queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
743    queue_builtin_action(keychord_init_action, "keychord_init");
744    queue_builtin_action(console_init_action, "console_init");
745
746    /* execute all the boot actions to get us started */
747    action_for_each_trigger("init", action_add_queue_tail);
748
749    /* skip mounting filesystems in charger mode */
750    if (!is_charger) {
751        action_for_each_trigger("early-fs", action_add_queue_tail);
752        action_for_each_trigger("fs", action_add_queue_tail);
753        action_for_each_trigger("post-fs", action_add_queue_tail);
754        action_for_each_trigger("post-fs-data", action_add_queue_tail);
755    }
756
757    queue_builtin_action(property_service_init_action, "property_service_init");
758    queue_builtin_action(signal_init_action, "signal_init");
759    queue_builtin_action(check_startup_action, "check_startup");
760
761    if (is_charger) {
762        action_for_each_trigger("charger", action_add_queue_tail);
763    } else {
764        action_for_each_trigger("early-boot", action_add_queue_tail);
765        action_for_each_trigger("boot", action_add_queue_tail);
766    }
767
768        /* run all property triggers based on current state of the properties */
769    queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers");
770
771
772#if BOOTCHART
773    queue_builtin_action(bootchart_init_action, "bootchart_init");
774#endif
775
776    for(;;) {
777        int nr, i, timeout = -1;
778
779        execute_one_command();
780        restart_processes();
781
782        if (!property_set_fd_init && get_property_set_fd() > 0) {
783            ufds[fd_count].fd = get_property_set_fd();
784            ufds[fd_count].events = POLLIN;
785            ufds[fd_count].revents = 0;
786            fd_count++;
787            property_set_fd_init = 1;
788        }
789        if (!signal_fd_init && get_signal_fd() > 0) {
790            ufds[fd_count].fd = get_signal_fd();
791            ufds[fd_count].events = POLLIN;
792            ufds[fd_count].revents = 0;
793            fd_count++;
794            signal_fd_init = 1;
795        }
796        if (!keychord_fd_init && get_keychord_fd() > 0) {
797            ufds[fd_count].fd = get_keychord_fd();
798            ufds[fd_count].events = POLLIN;
799            ufds[fd_count].revents = 0;
800            fd_count++;
801            keychord_fd_init = 1;
802        }
803
804        if (process_needs_restart) {
805            timeout = (process_needs_restart - gettime()) * 1000;
806            if (timeout < 0)
807                timeout = 0;
808        }
809
810        if (!action_queue_empty() || cur_action)
811            timeout = 0;
812
813#if BOOTCHART
814        if (bootchart_count > 0) {
815            if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
816                timeout = BOOTCHART_POLLING_MS;
817            if (bootchart_step() < 0 || --bootchart_count == 0) {
818                bootchart_finish();
819                bootchart_count = 0;
820            }
821        }
822#endif
823
824        nr = poll(ufds, fd_count, timeout);
825        if (nr <= 0)
826            continue;
827
828        for (i = 0; i < fd_count; i++) {
829            if (ufds[i].revents == POLLIN) {
830                if (ufds[i].fd == get_property_set_fd())
831                    handle_property_set_fd();
832                else if (ufds[i].fd == get_keychord_fd())
833                    handle_keychord();
834                else if (ufds[i].fd == get_signal_fd())
835                    handle_signal();
836            }
837        }
838    }
839
840    return 0;
841}
842