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