init.c revision c21dcef9fecaa224f153af0c5643fca4933bbc7f
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/sockets.h>
37#include <cutils/iosched_policy.h>
38#include <private/android_filesystem_config.h>
39#include <termios.h>
40
41#include <sys/system_properties.h>
42
43#include "devices.h"
44#include "init.h"
45#include "list.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 serialno[32];
63static char bootmode[32];
64static char baseband[32];
65static char carrier[32];
66static char bootloader[32];
67static char hardware[32];
68static unsigned revision = 0;
69static char qemu[32];
70
71static struct action *cur_action = NULL;
72static struct command *cur_command = NULL;
73static struct listnode *command_queue = NULL;
74
75void 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
131static void publish_socket(const char *name, int fd)
132{
133    char key[64] = ANDROID_SOCKET_ENV_PREFIX;
134    char val[64];
135
136    strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
137            name,
138            sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
139    snprintf(val, sizeof(val), "%d", fd);
140    add_environment(key, val);
141
142    /* make sure we don't close-on-exec */
143    fcntl(fd, F_SETFD, 0);
144}
145
146void service_start(struct service *svc, const char *dynamic_args)
147{
148    struct stat s;
149    pid_t pid;
150    int needs_console;
151    int n;
152
153        /* starting a service removes it from the disabled or reset
154         * state and immediately takes it out of the restarting
155         * state if it was in there
156         */
157    svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET));
158    svc->time_started = 0;
159
160        /* running processes require no additional work -- if
161         * they're in the process of exiting, we've ensured
162         * that they will immediately restart on exit, unless
163         * they are ONESHOT
164         */
165    if (svc->flags & SVC_RUNNING) {
166        return;
167    }
168
169    needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
170    if (needs_console && (!have_console)) {
171        ERROR("service '%s' requires console\n", svc->name);
172        svc->flags |= SVC_DISABLED;
173        return;
174    }
175
176    if (stat(svc->args[0], &s) != 0) {
177        ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
178        svc->flags |= SVC_DISABLED;
179        return;
180    }
181
182    if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
183        ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
184               svc->args[0]);
185        svc->flags |= SVC_DISABLED;
186        return;
187    }
188
189    NOTICE("starting '%s'\n", svc->name);
190
191    pid = fork();
192
193    if (pid == 0) {
194        struct socketinfo *si;
195        struct svcenvinfo *ei;
196        char tmp[32];
197        int fd, sz;
198
199        if (properties_inited()) {
200            get_property_workspace(&fd, &sz);
201            sprintf(tmp, "%d,%d", dup(fd), sz);
202            add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
203        }
204
205        for (ei = svc->envvars; ei; ei = ei->next)
206            add_environment(ei->name, ei->value);
207
208        for (si = svc->sockets; si; si = si->next) {
209            int socket_type = (
210                    !strcmp(si->type, "stream") ? SOCK_STREAM :
211                        (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
212            int s = create_socket(si->name, socket_type,
213                                  si->perm, si->uid, si->gid);
214            if (s >= 0) {
215                publish_socket(si->name, s);
216            }
217        }
218
219        if (svc->ioprio_class != IoSchedClass_NONE) {
220            if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
221                ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
222                      getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
223            }
224        }
225
226        if (needs_console) {
227            setsid();
228            open_console();
229        } else {
230            zap_stdio();
231        }
232
233#if 0
234        for (n = 0; svc->args[n]; n++) {
235            INFO("args[%d] = '%s'\n", n, svc->args[n]);
236        }
237        for (n = 0; ENV[n]; n++) {
238            INFO("env[%d] = '%s'\n", n, ENV[n]);
239        }
240#endif
241
242        setpgid(0, getpid());
243
244    /* as requested, set our gid, supplemental gids, and uid */
245        if (svc->gid) {
246            if (setgid(svc->gid) != 0) {
247                ERROR("setgid failed: %s\n", strerror(errno));
248                _exit(127);
249            }
250        }
251        if (svc->nr_supp_gids) {
252            if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
253                ERROR("setgroups failed: %s\n", strerror(errno));
254                _exit(127);
255            }
256        }
257        if (svc->uid) {
258            if (setuid(svc->uid) != 0) {
259                ERROR("setuid failed: %s\n", strerror(errno));
260                _exit(127);
261            }
262        }
263
264        if (!dynamic_args) {
265            if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
266                ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
267            }
268        } else {
269            char *arg_ptrs[INIT_PARSER_MAXARGS+1];
270            int arg_idx = svc->nargs;
271            char *tmp = strdup(dynamic_args);
272            char *next = tmp;
273            char *bword;
274
275            /* Copy the static arguments */
276            memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
277
278            while((bword = strsep(&next, " "))) {
279                arg_ptrs[arg_idx++] = bword;
280                if (arg_idx == INIT_PARSER_MAXARGS)
281                    break;
282            }
283            arg_ptrs[arg_idx] = '\0';
284            execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
285        }
286        _exit(127);
287    }
288
289    if (pid < 0) {
290        ERROR("failed to start '%s'\n", svc->name);
291        svc->pid = 0;
292        return;
293    }
294
295    svc->time_started = gettime();
296    svc->pid = pid;
297    svc->flags |= SVC_RUNNING;
298
299    if (properties_inited())
300        notify_service_state(svc->name, "running");
301}
302
303/* The how field should be either SVC_DISABLED or SVC_RESET */
304static void service_stop_or_reset(struct service *svc, int how)
305{
306        /* we are no longer running, nor should we
307         * attempt to restart
308         */
309    svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING));
310
311    if ((how != SVC_DISABLED) && (how != SVC_RESET)) {
312        /* Hrm, an illegal flag.  Default to SVC_DISABLED */
313        how = SVC_DISABLED;
314    }
315        /* if the service has not yet started, prevent
316         * it from auto-starting with its class
317         */
318    svc->flags |= how;
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 void import_kernel_nv(char *name, int in_qemu)
420{
421    char *value = strchr(name, '=');
422
423    if (value == 0) return;
424    *value++ = 0;
425    if (*name == 0) return;
426
427    if (!in_qemu)
428    {
429        /* on a real device, white-list the kernel options */
430        if (!strcmp(name,"qemu")) {
431            strlcpy(qemu, value, sizeof(qemu));
432        } else if (!strcmp(name,"androidboot.console")) {
433            strlcpy(console, value, sizeof(console));
434        } else if (!strcmp(name,"androidboot.mode")) {
435            strlcpy(bootmode, value, sizeof(bootmode));
436        } else if (!strcmp(name,"androidboot.serialno")) {
437            strlcpy(serialno, value, sizeof(serialno));
438        } else if (!strcmp(name,"androidboot.baseband")) {
439            strlcpy(baseband, value, sizeof(baseband));
440        } else if (!strcmp(name,"androidboot.carrier")) {
441            strlcpy(carrier, value, sizeof(carrier));
442        } else if (!strcmp(name,"androidboot.bootloader")) {
443            strlcpy(bootloader, value, sizeof(bootloader));
444        } else if (!strcmp(name,"androidboot.hardware")) {
445            strlcpy(hardware, value, sizeof(hardware));
446        }
447    } else {
448        /* in the emulator, export any kernel option with the
449         * ro.kernel. prefix */
450        char  buff[32];
451        int   len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
452        if (len < (int)sizeof(buff)) {
453            property_set( buff, value );
454        }
455    }
456}
457
458static void import_kernel_cmdline(int in_qemu)
459{
460    char cmdline[1024];
461    char *ptr;
462    int fd;
463
464    fd = open("/proc/cmdline", O_RDONLY);
465    if (fd >= 0) {
466        int n = read(fd, cmdline, 1023);
467        if (n < 0) n = 0;
468
469        /* get rid of trailing newline, it happens */
470        if (n > 0 && cmdline[n-1] == '\n') n--;
471
472        cmdline[n] = 0;
473        close(fd);
474    } else {
475        cmdline[0] = 0;
476    }
477
478    ptr = cmdline;
479    while (ptr && *ptr) {
480        char *x = strchr(ptr, ' ');
481        if (x != 0) *x++ = 0;
482        import_kernel_nv(ptr, in_qemu);
483        ptr = x;
484    }
485
486        /* don't expose the raw commandline to nonpriv processes */
487    chmod("/proc/cmdline", 0440);
488}
489
490static struct command *get_first_command(struct action *act)
491{
492    struct listnode *node;
493    node = list_head(&act->commands);
494    if (!node)
495        return NULL;
496
497    return node_to_item(node, struct command, clist);
498}
499
500static struct command *get_next_command(struct action *act, struct command *cmd)
501{
502    struct listnode *node;
503    node = cmd->clist.next;
504    if (!node)
505        return NULL;
506    if (node == &act->commands)
507        return NULL;
508
509    return node_to_item(node, struct command, clist);
510}
511
512static int is_last_command(struct action *act, struct command *cmd)
513{
514    return (list_tail(&act->commands) == &cmd->clist);
515}
516
517void execute_one_command(void)
518{
519    int ret;
520
521    if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
522        cur_action = action_remove_queue_head();
523        cur_command = NULL;
524        if (!cur_action)
525            return;
526        INFO("processing action %p (%s)\n", cur_action, cur_action->name);
527        cur_command = get_first_command(cur_action);
528    } else {
529        cur_command = get_next_command(cur_action, cur_command);
530    }
531
532    if (!cur_command)
533        return;
534
535    ret = cur_command->func(cur_command->nargs, cur_command->args);
536    INFO("command '%s' r=%d\n", cur_command->args[0], ret);
537}
538
539static int wait_for_coldboot_done_action(int nargs, char **args)
540{
541    int ret;
542    INFO("wait for %s\n", coldboot_done);
543    ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
544    if (ret)
545        ERROR("Timed out waiting for %s\n", coldboot_done);
546    return ret;
547}
548
549static int property_init_action(int nargs, char **args)
550{
551    INFO("property init\n");
552    property_init();
553    return 0;
554}
555
556static int keychord_init_action(int nargs, char **args)
557{
558    keychord_init();
559    return 0;
560}
561
562static int console_init_action(int nargs, char **args)
563{
564    int fd;
565    char tmp[PROP_VALUE_MAX];
566
567    if (console[0]) {
568        snprintf(tmp, sizeof(tmp), "/dev/%s", console);
569        console_name = strdup(tmp);
570    }
571
572    fd = open(console_name, O_RDWR);
573    if (fd >= 0)
574        have_console = 1;
575    close(fd);
576
577    if( load_565rle_image(INIT_IMAGE_FILE) ) {
578        fd = open("/dev/tty0", O_WRONLY);
579        if (fd >= 0) {
580            const char *msg;
581                msg = "\n"
582            "\n"
583            "\n"
584            "\n"
585            "\n"
586            "\n"
587            "\n"  // console is 40 cols x 30 lines
588            "\n"
589            "\n"
590            "\n"
591            "\n"
592            "\n"
593            "\n"
594            "\n"
595            "             A N D R O I D ";
596            write(fd, msg, strlen(msg));
597            close(fd);
598        }
599    }
600    return 0;
601}
602
603static int set_init_properties_action(int nargs, char **args)
604{
605    char tmp[PROP_VALUE_MAX];
606
607    if (qemu[0])
608        import_kernel_cmdline(1);
609
610    if (!strcmp(bootmode,"factory"))
611        property_set("ro.factorytest", "1");
612    else if (!strcmp(bootmode,"factory2"))
613        property_set("ro.factorytest", "2");
614    else
615        property_set("ro.factorytest", "0");
616
617    property_set("ro.serialno", serialno[0] ? serialno : "");
618    property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown");
619    property_set("ro.baseband", baseband[0] ? baseband : "unknown");
620    property_set("ro.carrier", carrier[0] ? carrier : "unknown");
621    property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown");
622
623    property_set("ro.hardware", hardware);
624    snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
625    property_set("ro.revision", tmp);
626    return 0;
627}
628
629static int property_service_init_action(int nargs, char **args)
630{
631    /* read any property files on system or data and
632     * fire up the property service.  This must happen
633     * after the ro.foo properties are set above so
634     * that /data/local.prop cannot interfere with them.
635     */
636    start_property_service();
637    return 0;
638}
639
640static int signal_init_action(int nargs, char **args)
641{
642    signal_init();
643    return 0;
644}
645
646static int check_startup_action(int nargs, char **args)
647{
648    /* make sure we actually have all the pieces we need */
649    if ((get_property_set_fd() < 0) ||
650        (get_signal_fd() < 0)) {
651        ERROR("init startup failure\n");
652        exit(1);
653    }
654
655        /* signal that we hit this point */
656    unlink("/dev/.booting");
657
658    return 0;
659}
660
661static int queue_property_triggers_action(int nargs, char **args)
662{
663    queue_all_property_triggers();
664    /* enable property triggers */
665    property_triggers_enabled = 1;
666    return 0;
667}
668
669#if BOOTCHART
670static int bootchart_init_action(int nargs, char **args)
671{
672    bootchart_count = bootchart_init();
673    if (bootchart_count < 0) {
674        ERROR("bootcharting init failure\n");
675    } else if (bootchart_count > 0) {
676        NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
677    } else {
678        NOTICE("bootcharting ignored\n");
679    }
680
681    return 0;
682}
683#endif
684
685int main(int argc, char **argv)
686{
687    int fd_count = 0;
688    struct pollfd ufds[4];
689    char *tmpdev;
690    char* debuggable;
691    char tmp[32];
692    int property_set_fd_init = 0;
693    int signal_fd_init = 0;
694    int keychord_fd_init = 0;
695
696    if (!strcmp(basename(argv[0]), "ueventd"))
697        return ueventd_main(argc, argv);
698
699    /* clear the umask */
700    umask(0);
701
702        /* Get the basic filesystem setup we need put
703         * together in the initramdisk on / and then we'll
704         * let the rc file figure out the rest.
705         */
706    mkdir("/dev", 0755);
707    mkdir("/proc", 0755);
708    mkdir("/sys", 0755);
709
710    mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
711    mkdir("/dev/pts", 0755);
712    mkdir("/dev/socket", 0755);
713    mount("devpts", "/dev/pts", "devpts", 0, NULL);
714    mount("proc", "/proc", "proc", 0, NULL);
715    mount("sysfs", "/sys", "sysfs", 0, NULL);
716
717        /* indicate that booting is in progress to background fw loaders, etc */
718    close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
719
720        /* We must have some place other than / to create the
721         * device nodes for kmsg and null, otherwise we won't
722         * be able to remount / read-only later on.
723         * Now that tmpfs is mounted on /dev, we can actually
724         * talk to the outside world.
725         */
726    open_devnull_stdio();
727    log_init();
728
729    INFO("reading config file\n");
730    init_parse_config_file("/init.rc");
731
732    /* pull the kernel commandline and ramdisk properties file in */
733    import_kernel_cmdline(0);
734
735    get_hardware_name(hardware, &revision);
736    snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
737    init_parse_config_file(tmp);
738
739    action_for_each_trigger("early-init", action_add_queue_tail);
740
741    queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
742    queue_builtin_action(property_init_action, "property_init");
743    queue_builtin_action(keychord_init_action, "keychord_init");
744    queue_builtin_action(console_init_action, "console_init");
745    queue_builtin_action(set_init_properties_action, "set_init_properties");
746
747        /* execute all the boot actions to get us started */
748    action_for_each_trigger("init", action_add_queue_tail);
749    action_for_each_trigger("early-fs", action_add_queue_tail);
750    action_for_each_trigger("fs", action_add_queue_tail);
751    action_for_each_trigger("post-fs", action_add_queue_tail);
752    action_for_each_trigger("post-fs-data", action_add_queue_tail);
753
754    queue_builtin_action(property_service_init_action, "property_service_init");
755    queue_builtin_action(signal_init_action, "signal_init");
756    queue_builtin_action(check_startup_action, "check_startup");
757
758    /* execute all the boot actions to get us started */
759    action_for_each_trigger("early-boot", action_add_queue_tail);
760    action_for_each_trigger("boot", action_add_queue_tail);
761
762        /* run all property triggers based on current state of the properties */
763    queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers");
764
765
766#if BOOTCHART
767    queue_builtin_action(bootchart_init_action, "bootchart_init");
768#endif
769
770    for(;;) {
771        int nr, i, timeout = -1;
772
773        execute_one_command();
774        restart_processes();
775
776        if (!property_set_fd_init && get_property_set_fd() > 0) {
777            ufds[fd_count].fd = get_property_set_fd();
778            ufds[fd_count].events = POLLIN;
779            ufds[fd_count].revents = 0;
780            fd_count++;
781            property_set_fd_init = 1;
782        }
783        if (!signal_fd_init && get_signal_fd() > 0) {
784            ufds[fd_count].fd = get_signal_fd();
785            ufds[fd_count].events = POLLIN;
786            ufds[fd_count].revents = 0;
787            fd_count++;
788            signal_fd_init = 1;
789        }
790        if (!keychord_fd_init && get_keychord_fd() > 0) {
791            ufds[fd_count].fd = get_keychord_fd();
792            ufds[fd_count].events = POLLIN;
793            ufds[fd_count].revents = 0;
794            fd_count++;
795            keychord_fd_init = 1;
796        }
797
798        if (process_needs_restart) {
799            timeout = (process_needs_restart - gettime()) * 1000;
800            if (timeout < 0)
801                timeout = 0;
802        }
803
804        if (!action_queue_empty() || cur_action)
805            timeout = 0;
806
807#if BOOTCHART
808        if (bootchart_count > 0) {
809            if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
810                timeout = BOOTCHART_POLLING_MS;
811            if (bootchart_step() < 0 || --bootchart_count == 0) {
812                bootchart_finish();
813                bootchart_count = 0;
814            }
815        }
816#endif
817
818        nr = poll(ufds, fd_count, timeout);
819        if (nr <= 0)
820            continue;
821
822        for (i = 0; i < fd_count; i++) {
823            if (ufds[i].revents == POLLIN) {
824                if (ufds[i].fd == get_property_set_fd())
825                    handle_property_set_fd();
826                else if (ufds[i].fd == get_keychord_fd())
827                    handle_keychord();
828                else if (ufds[i].fd == get_signal_fd())
829                    handle_signal();
830            }
831        }
832    }
833
834    return 0;
835}
836