init.c revision ed8a7d84428ec945c48b6b53dc5a3a18fabaf683
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 get_property_workspace(&fd, &sz); 197 sprintf(tmp, "%d,%d", dup(fd), sz); 198 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp); 199 200 for (ei = svc->envvars; ei; ei = ei->next) 201 add_environment(ei->name, ei->value); 202 203 for (si = svc->sockets; si; si = si->next) { 204 int s = create_socket(si->name, 205 !strcmp(si->type, "dgram") ? 206 SOCK_DGRAM : SOCK_STREAM, 207 si->perm, si->uid, si->gid); 208 if (s >= 0) { 209 publish_socket(si->name, s); 210 } 211 } 212 213 if (svc->ioprio_class != IoSchedClass_NONE) { 214 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) { 215 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n", 216 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno)); 217 } 218 } 219 220 if (needs_console) { 221 setsid(); 222 open_console(); 223 } else { 224 zap_stdio(); 225 } 226 227#if 0 228 for (n = 0; svc->args[n]; n++) { 229 INFO("args[%d] = '%s'\n", n, svc->args[n]); 230 } 231 for (n = 0; ENV[n]; n++) { 232 INFO("env[%d] = '%s'\n", n, ENV[n]); 233 } 234#endif 235 236 setpgid(0, getpid()); 237 238 /* as requested, set our gid, supplemental gids, and uid */ 239 if (svc->gid) { 240 setgid(svc->gid); 241 } 242 if (svc->nr_supp_gids) { 243 setgroups(svc->nr_supp_gids, svc->supp_gids); 244 } 245 if (svc->uid) { 246 setuid(svc->uid); 247 } 248 249 if (!dynamic_args) { 250 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) { 251 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno)); 252 } 253 } else { 254 char *arg_ptrs[SVC_MAXARGS+1]; 255 int arg_idx = svc->nargs; 256 char *tmp = strdup(dynamic_args); 257 char *next = tmp; 258 char *bword; 259 260 /* Copy the static arguments */ 261 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *))); 262 263 while((bword = strsep(&next, " "))) { 264 arg_ptrs[arg_idx++] = bword; 265 if (arg_idx == SVC_MAXARGS) 266 break; 267 } 268 arg_ptrs[arg_idx] = '\0'; 269 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV); 270 } 271 _exit(127); 272 } 273 274 if (pid < 0) { 275 ERROR("failed to start '%s'\n", svc->name); 276 svc->pid = 0; 277 return; 278 } 279 280 svc->time_started = gettime(); 281 svc->pid = pid; 282 svc->flags |= SVC_RUNNING; 283 284 notify_service_state(svc->name, "running"); 285} 286 287void service_stop(struct service *svc) 288{ 289 /* we are no longer running, nor should we 290 * attempt to restart 291 */ 292 svc->flags &= (~(SVC_RUNNING|SVC_RESTARTING)); 293 294 /* if the service has not yet started, prevent 295 * it from auto-starting with its class 296 */ 297 svc->flags |= SVC_DISABLED; 298 299 if (svc->pid) { 300 NOTICE("service '%s' is being killed\n", svc->name); 301 kill(-svc->pid, SIGTERM); 302 notify_service_state(svc->name, "stopping"); 303 } else { 304 notify_service_state(svc->name, "stopped"); 305 } 306} 307 308void property_changed(const char *name, const char *value) 309{ 310 if (property_triggers_enabled) 311 queue_property_triggers(name, value); 312} 313 314static void restart_service_if_needed(struct service *svc) 315{ 316 time_t next_start_time = svc->time_started + 5; 317 318 if (next_start_time <= gettime()) { 319 svc->flags &= (~SVC_RESTARTING); 320 service_start(svc, NULL); 321 return; 322 } 323 324 if ((next_start_time < process_needs_restart) || 325 (process_needs_restart == 0)) { 326 process_needs_restart = next_start_time; 327 } 328} 329 330static void restart_processes() 331{ 332 process_needs_restart = 0; 333 service_for_each_flags(SVC_RESTARTING, 334 restart_service_if_needed); 335} 336 337static void msg_start(const char *name) 338{ 339 struct service *svc; 340 char *tmp = NULL; 341 char *args = NULL; 342 343 if (!strchr(name, ':')) 344 svc = service_find_by_name(name); 345 else { 346 tmp = strdup(name); 347 args = strchr(tmp, ':'); 348 *args = '\0'; 349 args++; 350 351 svc = service_find_by_name(tmp); 352 } 353 354 if (svc) { 355 service_start(svc, args); 356 } else { 357 ERROR("no such service '%s'\n", name); 358 } 359 if (tmp) 360 free(tmp); 361} 362 363static void msg_stop(const char *name) 364{ 365 struct service *svc = service_find_by_name(name); 366 367 if (svc) { 368 service_stop(svc); 369 } else { 370 ERROR("no such service '%s'\n", name); 371 } 372} 373 374void handle_control_message(const char *msg, const char *arg) 375{ 376 if (!strcmp(msg,"start")) { 377 msg_start(arg); 378 } else if (!strcmp(msg,"stop")) { 379 msg_stop(arg); 380 } else { 381 ERROR("unknown control msg '%s'\n", msg); 382 } 383} 384 385static void import_kernel_nv(char *name, int in_qemu) 386{ 387 char *value = strchr(name, '='); 388 389 if (value == 0) return; 390 *value++ = 0; 391 if (*name == 0) return; 392 393 if (!in_qemu) 394 { 395 /* on a real device, white-list the kernel options */ 396 if (!strcmp(name,"qemu")) { 397 strlcpy(qemu, value, sizeof(qemu)); 398 } else if (!strcmp(name,"androidboot.console")) { 399 strlcpy(console, value, sizeof(console)); 400 } else if (!strcmp(name,"androidboot.mode")) { 401 strlcpy(bootmode, value, sizeof(bootmode)); 402 } else if (!strcmp(name,"androidboot.serialno")) { 403 strlcpy(serialno, value, sizeof(serialno)); 404 } else if (!strcmp(name,"androidboot.baseband")) { 405 strlcpy(baseband, value, sizeof(baseband)); 406 } else if (!strcmp(name,"androidboot.carrier")) { 407 strlcpy(carrier, value, sizeof(carrier)); 408 } else if (!strcmp(name,"androidboot.bootloader")) { 409 strlcpy(bootloader, value, sizeof(bootloader)); 410 } else if (!strcmp(name,"androidboot.hardware")) { 411 strlcpy(hardware, value, sizeof(hardware)); 412 } else { 413 qemu_cmdline(name, value); 414 } 415 } else { 416 /* in the emulator, export any kernel option with the 417 * ro.kernel. prefix */ 418 char buff[32]; 419 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name ); 420 if (len < (int)sizeof(buff)) { 421 property_set( buff, value ); 422 } 423 } 424} 425 426static void import_kernel_cmdline(int in_qemu) 427{ 428 char cmdline[1024]; 429 char *ptr; 430 int fd; 431 432 fd = open("/proc/cmdline", O_RDONLY); 433 if (fd >= 0) { 434 int n = read(fd, cmdline, 1023); 435 if (n < 0) n = 0; 436 437 /* get rid of trailing newline, it happens */ 438 if (n > 0 && cmdline[n-1] == '\n') n--; 439 440 cmdline[n] = 0; 441 close(fd); 442 } else { 443 cmdline[0] = 0; 444 } 445 446 ptr = cmdline; 447 while (ptr && *ptr) { 448 char *x = strchr(ptr, ' '); 449 if (x != 0) *x++ = 0; 450 import_kernel_nv(ptr, in_qemu); 451 ptr = x; 452 } 453 454 /* don't expose the raw commandline to nonpriv processes */ 455 chmod("/proc/cmdline", 0440); 456} 457 458static void get_hardware_name(void) 459{ 460 char data[1024]; 461 int fd, n; 462 char *x, *hw, *rev; 463 464 /* Hardware string was provided on kernel command line */ 465 if (hardware[0]) 466 return; 467 468 fd = open("/proc/cpuinfo", O_RDONLY); 469 if (fd < 0) return; 470 471 n = read(fd, data, 1023); 472 close(fd); 473 if (n < 0) return; 474 475 data[n] = 0; 476 hw = strstr(data, "\nHardware"); 477 rev = strstr(data, "\nRevision"); 478 479 if (hw) { 480 x = strstr(hw, ": "); 481 if (x) { 482 x += 2; 483 n = 0; 484 while (*x && !isspace(*x)) { 485 hardware[n++] = tolower(*x); 486 x++; 487 if (n == 31) break; 488 } 489 hardware[n] = 0; 490 } 491 } 492 493 if (rev) { 494 x = strstr(rev, ": "); 495 if (x) { 496 revision = strtoul(x + 2, 0, 16); 497 } 498 } 499} 500 501static struct command *get_first_command(struct action *act) 502{ 503 struct listnode *node; 504 node = list_head(&act->commands); 505 if (!node) 506 return NULL; 507 508 return node_to_item(node, struct command, clist); 509} 510 511static struct command *get_next_command(struct action *act, struct command *cmd) 512{ 513 struct listnode *node; 514 node = cmd->clist.next; 515 if (!node) 516 return NULL; 517 if (node == &act->commands) 518 return NULL; 519 520 return node_to_item(node, struct command, clist); 521} 522 523static int is_last_command(struct action *act, struct command *cmd) 524{ 525 return (list_tail(&act->commands) == &cmd->clist); 526} 527 528void execute_one_command(void) 529{ 530 int ret; 531 532 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) { 533 cur_action = action_remove_queue_head(); 534 if (!cur_action) 535 return; 536 INFO("processing action %p (%s)\n", cur_action, cur_action->name); 537 cur_command = get_first_command(cur_action); 538 } else { 539 cur_command = get_next_command(cur_action, cur_command); 540 } 541 542 if (!cur_command) 543 return; 544 545 ret = cur_command->func(cur_command->nargs, cur_command->args); 546 INFO("command '%s' r=%d\n", cur_command->args[0], ret); 547} 548 549void open_devnull_stdio(void) 550{ 551 int fd; 552 static const char *name = "/dev/__null__"; 553 if (mknod(name, S_IFCHR | 0600, (1 << 8) | 3) == 0) { 554 fd = open(name, O_RDWR); 555 unlink(name); 556 if (fd >= 0) { 557 dup2(fd, 0); 558 dup2(fd, 1); 559 dup2(fd, 2); 560 if (fd > 2) { 561 close(fd); 562 } 563 return; 564 } 565 } 566 567 exit(1); 568} 569 570static int device_init_action(int nargs, char **args) 571{ 572 INFO("device init\n"); 573 device_init(); 574 return 0; 575} 576 577static int property_init_action(int nargs, char **args) 578{ 579 INFO("property init\n"); 580 property_init(); 581 return 0; 582} 583 584static int keychord_init_action(int nargs, char **args) 585{ 586 keychord_init(); 587 return 0; 588} 589 590static int console_init_action(int nargs, char **args) 591{ 592 int fd; 593 char tmp[PROP_VALUE_MAX]; 594 595 if (console[0]) { 596 snprintf(tmp, sizeof(tmp), "/dev/%s", console); 597 console_name = strdup(tmp); 598 } 599 600 fd = open(console_name, O_RDWR); 601 if (fd >= 0) 602 have_console = 1; 603 close(fd); 604 605 if( load_565rle_image(INIT_IMAGE_FILE) ) { 606 fd = open("/dev/tty0", O_WRONLY); 607 if (fd >= 0) { 608 const char *msg; 609 msg = "\n" 610 "\n" 611 "\n" 612 "\n" 613 "\n" 614 "\n" 615 "\n" // console is 40 cols x 30 lines 616 "\n" 617 "\n" 618 "\n" 619 "\n" 620 "\n" 621 "\n" 622 "\n" 623 " A N D R O I D "; 624 write(fd, msg, strlen(msg)); 625 close(fd); 626 } 627 } 628 return 0; 629} 630 631static int set_init_properties_action(int nargs, char **args) 632{ 633 char tmp[PROP_VALUE_MAX]; 634 635 if (qemu[0]) 636 import_kernel_cmdline(1); 637 638 if (!strcmp(bootmode,"factory")) 639 property_set("ro.factorytest", "1"); 640 else if (!strcmp(bootmode,"factory2")) 641 property_set("ro.factorytest", "2"); 642 else 643 property_set("ro.factorytest", "0"); 644 645 property_set("ro.serialno", serialno[0] ? serialno : ""); 646 property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown"); 647 property_set("ro.baseband", baseband[0] ? baseband : "unknown"); 648 property_set("ro.carrier", carrier[0] ? carrier : "unknown"); 649 property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown"); 650 651 property_set("ro.hardware", hardware); 652 snprintf(tmp, PROP_VALUE_MAX, "%d", revision); 653 property_set("ro.revision", tmp); 654 return 0; 655} 656 657static int property_service_init_action(int nargs, char **args) 658{ 659 /* read any property files on system or data and 660 * fire up the property service. This must happen 661 * after the ro.foo properties are set above so 662 * that /data/local.prop cannot interfere with them. 663 */ 664 start_property_service(); 665 return 0; 666} 667 668static int signal_init_action(int nargs, char **args) 669{ 670 signal_init(); 671 return 0; 672} 673 674static int check_startup_action(int nargs, char **args) 675{ 676 /* make sure we actually have all the pieces we need */ 677 if ((get_device_fd() < 0) || 678 (get_property_set_fd() < 0) || 679 (get_signal_fd() < 0)) { 680 ERROR("init startup failure\n"); 681 exit(1); 682 } 683 return 0; 684} 685 686static int queue_property_triggers_action(int nargs, char **args) 687{ 688 queue_all_property_triggers(); 689 /* enable property triggers */ 690 property_triggers_enabled = 1; 691 return 0; 692} 693 694#if BOOTCHART 695static int bootchart_init_action(int nargs, char **args) 696{ 697 bootchart_count = bootchart_init(); 698 if (bootchart_count < 0) { 699 ERROR("bootcharting init failure\n"); 700 } else if (bootchart_count > 0) { 701 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS); 702 } else { 703 NOTICE("bootcharting ignored\n"); 704 } 705} 706#endif 707 708int main(int argc, char **argv) 709{ 710 int fd_count = 0; 711 struct pollfd ufds[4]; 712 char *tmpdev; 713 char* debuggable; 714 char tmp[32]; 715 int device_fd_init = 0; 716 int property_set_fd_init = 0; 717 int signal_fd_init = 0; 718 int keychord_fd_init = 0; 719 720 /* clear the umask */ 721 umask(0); 722 723 /* Get the basic filesystem setup we need put 724 * together in the initramdisk on / and then we'll 725 * let the rc file figure out the rest. 726 */ 727 mkdir("/dev", 0755); 728 mkdir("/proc", 0755); 729 mkdir("/sys", 0755); 730 731 mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755"); 732 mkdir("/dev/pts", 0755); 733 mkdir("/dev/socket", 0755); 734 mount("devpts", "/dev/pts", "devpts", 0, NULL); 735 mount("proc", "/proc", "proc", 0, NULL); 736 mount("sysfs", "/sys", "sysfs", 0, NULL); 737 738 /* We must have some place other than / to create the 739 * device nodes for kmsg and null, otherwise we won't 740 * be able to remount / read-only later on. 741 * Now that tmpfs is mounted on /dev, we can actually 742 * talk to the outside world. 743 */ 744 open_devnull_stdio(); 745 log_init(); 746 747 INFO("reading config file\n"); 748 parse_config_file("/init.rc"); 749 750 /* pull the kernel commandline and ramdisk properties file in */ 751 qemu_init(); 752 import_kernel_cmdline(0); 753 754 get_hardware_name(); 755 snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware); 756 parse_config_file(tmp); 757 758 action_for_each_trigger("early-init", action_add_queue_tail); 759 760 queue_builtin_action(device_init_action, "device_init"); 761 queue_builtin_action(property_init_action, "property_init"); 762 queue_builtin_action(keychord_init_action, "keychord_init"); 763 queue_builtin_action(console_init_action, "console_init"); 764 queue_builtin_action(set_init_properties_action, "set_init_properties"); 765 766 /* execute all the boot actions to get us started */ 767 action_for_each_trigger("init", action_add_queue_tail); 768 action_for_each_trigger("early-fs", action_add_queue_tail); 769 action_for_each_trigger("fs", action_add_queue_tail); 770 action_for_each_trigger("post-fs", action_add_queue_tail); 771 772 queue_builtin_action(property_service_init_action, "property_service_init"); 773 queue_builtin_action(signal_init_action, "signal_init"); 774 queue_builtin_action(check_startup_action, "check_startup"); 775 776 /* execute all the boot actions to get us started */ 777 action_for_each_trigger("early-boot", action_add_queue_tail); 778 action_for_each_trigger("boot", action_add_queue_tail); 779 780 /* run all property triggers based on current state of the properties */ 781 queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers"); 782 783 784#if BOOTCHART 785 queue_builtin_action(bootchart_init_action, "bootchart_init"); 786#endif 787 788 for(;;) { 789 int nr, i, timeout = -1; 790 791 execute_one_command(); 792 restart_processes(); 793 794 if (!device_fd_init && get_device_fd() > 0) { 795 ufds[fd_count].fd = get_device_fd(); 796 ufds[fd_count].events = POLLIN; 797 ufds[fd_count].revents = 0; 798 fd_count++; 799 device_fd_init = 1; 800 } 801 if (!property_set_fd_init && get_property_set_fd() > 0) { 802 ufds[fd_count].fd = get_property_set_fd(); 803 ufds[fd_count].events = POLLIN; 804 ufds[fd_count].revents = 0; 805 fd_count++; 806 property_set_fd_init = 1; 807 } 808 if (!signal_fd_init && get_signal_fd() > 0) { 809 ufds[fd_count].fd = get_signal_fd(); 810 ufds[fd_count].events = POLLIN; 811 ufds[fd_count].revents = 0; 812 fd_count++; 813 signal_fd_init = 1; 814 } 815 if (!keychord_fd_init && get_keychord_fd() > 0) { 816 ufds[fd_count].fd = get_keychord_fd(); 817 ufds[fd_count].events = POLLIN; 818 ufds[fd_count].revents = 0; 819 fd_count++; 820 keychord_fd_init = 1; 821 } 822 823 if (process_needs_restart) { 824 timeout = (process_needs_restart - gettime()) * 1000; 825 if (timeout < 0) 826 timeout = 0; 827 } 828 829 if (!action_queue_empty() || cur_command) 830 timeout = 0; 831 832#if BOOTCHART 833 if (bootchart_count > 0) { 834 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) 835 timeout = BOOTCHART_POLLING_MS; 836 if (bootchart_step() < 0 || --bootchart_count == 0) { 837 bootchart_finish(); 838 bootchart_count = 0; 839 } 840 } 841#endif 842 843 nr = poll(ufds, fd_count, timeout); 844 if (nr <= 0) 845 continue; 846 847 for (i = 0; i < fd_count; i++) { 848 if (ufds[i].revents == POLLIN) { 849 if (ufds[i].fd == get_device_fd()) 850 handle_device_fd(); 851 else if (ufds[i].fd == get_property_set_fd()) 852 handle_property_set_fd(); 853 else if (ufds[i].fd == get_keychord_fd()) 854 handle_keychord(); 855 else if (ufds[i].fd == get_signal_fd()) 856 handle_signal(); 857 } 858 } 859 } 860 861 return 0; 862} 863