blob: c21a49510dd19c714cba942aa8dd61b19a98072a [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
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>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#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>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050034
Stephen Smalleye46f9d52012-01-13 08:48:47 -050035#include <selinux/selinux.h>
36#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040037#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038
Colin Crossf83d0b92010-04-21 12:04:20 -070039#include <libgen.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Dima Zavinda04c522011-09-01 17:09:44 -070041#include <cutils/list.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080043#include <cutils/iosched_policy.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070044#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046
47#include <sys/system_properties.h>
48
49#include "devices.h"
50#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070051#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080053#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070054#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070055#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070056#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070057#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070058#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070059#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Stephen Smalleye46f9d52012-01-13 08:48:47 -050061struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040062struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050063
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064static int property_triggers_enabled = 0;
65
66#if BOOTCHART
67static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068#endif
69
70static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072static char hardware[32];
73static unsigned revision = 0;
74static char qemu[32];
75
Stephen Smalleye46f9d52012-01-13 08:48:47 -050076static int selinux_enabled = 1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050077
Colin Crossebc6ff12010-04-13 19:52:01 -070078static struct action *cur_action = NULL;
79static struct command *cur_command = NULL;
80static struct listnode *command_queue = NULL;
81
Colin Cross9c5366b2010-04-13 19:48:59 -070082void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083{
84 char pname[PROP_NAME_MAX];
85 int len = strlen(name);
86 if ((len + 10) > PROP_NAME_MAX)
87 return;
88 snprintf(pname, sizeof(pname), "init.svc.%s", name);
89 property_set(pname, state);
90}
91
92static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080093static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070094static time_t process_needs_restart;
95
96static const char *ENV[32];
97
98/* add_environment - add "key=value" to the current environment */
99int add_environment(const char *key, const char *val)
100{
101 int n;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700102
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103 for (n = 0; n < 31; n++) {
104 if (!ENV[n]) {
105 size_t len = strlen(key) + strlen(val) + 2;
106 char *entry = malloc(len);
107 snprintf(entry, len, "%s=%s", key, val);
108 ENV[n] = entry;
109 return 0;
110 }
111 }
112
113 return 1;
114}
115
116static void zap_stdio(void)
117{
118 int fd;
119 fd = open("/dev/null", O_RDWR);
120 dup2(fd, 0);
121 dup2(fd, 1);
122 dup2(fd, 2);
123 close(fd);
124}
125
126static void open_console()
127{
128 int fd;
129 if ((fd = open(console_name, O_RDWR)) < 0) {
130 fd = open("/dev/null", O_RDWR);
131 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700132 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133 dup2(fd, 0);
134 dup2(fd, 1);
135 dup2(fd, 2);
136 close(fd);
137}
138
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139static void publish_socket(const char *name, int fd)
140{
141 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
142 char val[64];
143
144 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
145 name,
146 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
147 snprintf(val, sizeof(val), "%d", fd);
148 add_environment(key, val);
149
150 /* make sure we don't close-on-exec */
151 fcntl(fd, F_SETFD, 0);
152}
153
San Mehatf24e2522009-05-19 13:30:46 -0700154void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700155{
156 struct stat s;
157 pid_t pid;
158 int needs_console;
159 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500160 char *scon = NULL;
161 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700162
Ken Sumrall752923c2010-12-03 16:33:31 -0800163 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164 * state and immediately takes it out of the restarting
165 * state if it was in there
166 */
Mike Kasickb54f39f2012-01-25 23:48:46 -0500167 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700168 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700169
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700170 /* running processes require no additional work -- if
171 * they're in the process of exiting, we've ensured
172 * that they will immediately restart on exit, unless
173 * they are ONESHOT
174 */
175 if (svc->flags & SVC_RUNNING) {
176 return;
177 }
178
179 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
180 if (needs_console && (!have_console)) {
181 ERROR("service '%s' requires console\n", svc->name);
182 svc->flags |= SVC_DISABLED;
183 return;
184 }
185
186 if (stat(svc->args[0], &s) != 0) {
187 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
188 svc->flags |= SVC_DISABLED;
189 return;
190 }
191
San Mehatf24e2522009-05-19 13:30:46 -0700192 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700193 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
194 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700195 svc->flags |= SVC_DISABLED;
196 return;
197 }
198
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500199 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500200 if (svc->seclabel) {
201 scon = strdup(svc->seclabel);
202 if (!scon) {
203 ERROR("Out of memory while starting '%s'\n", svc->name);
204 return;
205 }
206 } else {
207 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500208
Stephen Smalley30f30332012-11-16 14:34:27 -0500209 INFO("computing context for service '%s'\n", svc->args[0]);
210 rc = getcon(&mycon);
211 if (rc < 0) {
212 ERROR("could not get context while starting '%s'\n", svc->name);
213 return;
214 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500215
Stephen Smalley30f30332012-11-16 14:34:27 -0500216 rc = getfilecon(svc->args[0], &fcon);
217 if (rc < 0) {
218 ERROR("could not get context while starting '%s'\n", svc->name);
219 freecon(mycon);
220 return;
221 }
222
223 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500224 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500225 freecon(fcon);
226 if (rc < 0) {
227 ERROR("could not get context while starting '%s'\n", svc->name);
228 return;
229 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500230 }
231 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500232
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700233 NOTICE("starting '%s'\n", svc->name);
234
235 pid = fork();
236
237 if (pid == 0) {
238 struct socketinfo *si;
239 struct svcenvinfo *ei;
240 char tmp[32];
241 int fd, sz;
242
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700243 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700244 if (properties_inited()) {
245 get_property_workspace(&fd, &sz);
246 sprintf(tmp, "%d,%d", dup(fd), sz);
247 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
248 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700249
250 for (ei = svc->envvars; ei; ei = ei->next)
251 add_environment(ei->name, ei->value);
252
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500253 setsockcreatecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500254
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700255 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400256 int socket_type = (
257 !strcmp(si->type, "stream") ? SOCK_STREAM :
258 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
259 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700260 si->perm, si->uid, si->gid);
261 if (s >= 0) {
262 publish_socket(si->name, s);
263 }
264 }
265
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500266 freecon(scon);
267 scon = NULL;
268 setsockcreatecon(NULL);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500269
San Mehat4e221f02010-02-25 14:19:50 -0800270 if (svc->ioprio_class != IoSchedClass_NONE) {
271 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
272 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
273 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
274 }
275 }
276
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700277 if (needs_console) {
278 setsid();
279 open_console();
280 } else {
281 zap_stdio();
282 }
283
284#if 0
285 for (n = 0; svc->args[n]; n++) {
286 INFO("args[%d] = '%s'\n", n, svc->args[n]);
287 }
288 for (n = 0; ENV[n]; n++) {
289 INFO("env[%d] = '%s'\n", n, ENV[n]);
290 }
291#endif
292
293 setpgid(0, getpid());
294
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800295 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800297 if (setgid(svc->gid) != 0) {
298 ERROR("setgid failed: %s\n", strerror(errno));
299 _exit(127);
300 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 }
302 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800303 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
304 ERROR("setgroups failed: %s\n", strerror(errno));
305 _exit(127);
306 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 }
308 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800309 if (setuid(svc->uid) != 0) {
310 ERROR("setuid failed: %s\n", strerror(errno));
311 _exit(127);
312 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700313 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500314 if (svc->seclabel) {
315 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
316 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
317 _exit(127);
318 }
319 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500320
San Mehat8ad15682009-05-20 08:50:40 -0700321 if (!dynamic_args) {
322 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
323 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
324 }
325 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700326 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700327 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700328 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700329 char *next = tmp;
330 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700331
332 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700333 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700334
San Mehatd4cdd132009-05-20 09:52:16 -0700335 while((bword = strsep(&next, " "))) {
336 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700337 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700338 break;
San Mehatf24e2522009-05-19 13:30:46 -0700339 }
340 arg_ptrs[arg_idx] = '\0';
341 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100342 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700343 _exit(127);
344 }
345
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500346 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500347
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 if (pid < 0) {
349 ERROR("failed to start '%s'\n", svc->name);
350 svc->pid = 0;
351 return;
352 }
353
354 svc->time_started = gettime();
355 svc->pid = pid;
356 svc->flags |= SVC_RUNNING;
357
Colin Cross3294bbb2010-04-19 17:11:33 -0700358 if (properties_inited())
359 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360}
361
Mike Kasickb54f39f2012-01-25 23:48:46 -0500362/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800363static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700364{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500365 /* The service is still SVC_RUNNING until its process exits, but if it has
366 * already exited it shoudn't attempt a restart yet. */
367 svc->flags &= (~SVC_RESTARTING);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700368
Mike Kasickb54f39f2012-01-25 23:48:46 -0500369 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800370 /* Hrm, an illegal flag. Default to SVC_DISABLED */
371 how = SVC_DISABLED;
372 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373 /* if the service has not yet started, prevent
374 * it from auto-starting with its class
375 */
Ken Sumralla2864802011-10-26 16:56:00 -0700376 if (how == SVC_RESET) {
377 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
378 } else {
379 svc->flags |= how;
380 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381
382 if (svc->pid) {
383 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800384 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 notify_service_state(svc->name, "stopping");
386 } else {
387 notify_service_state(svc->name, "stopped");
388 }
389}
390
Ken Sumrall752923c2010-12-03 16:33:31 -0800391void service_reset(struct service *svc)
392{
393 service_stop_or_reset(svc, SVC_RESET);
394}
395
396void service_stop(struct service *svc)
397{
398 service_stop_or_reset(svc, SVC_DISABLED);
399}
400
Mike Kasickb54f39f2012-01-25 23:48:46 -0500401void service_restart(struct service *svc)
402{
403 if (svc->flags & SVC_RUNNING) {
404 /* Stop, wait, then start the service. */
405 service_stop_or_reset(svc, SVC_RESTART);
406 } else if (!(svc->flags & SVC_RESTARTING)) {
407 /* Just start the service since it's not running. */
408 service_start(svc, NULL);
409 } /* else: Service is restarting anyways. */
410}
411
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412void property_changed(const char *name, const char *value)
413{
Colin Crossebc6ff12010-04-13 19:52:01 -0700414 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416}
417
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418static void restart_service_if_needed(struct service *svc)
419{
420 time_t next_start_time = svc->time_started + 5;
421
422 if (next_start_time <= gettime()) {
423 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700424 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700425 return;
426 }
427
428 if ((next_start_time < process_needs_restart) ||
429 (process_needs_restart == 0)) {
430 process_needs_restart = next_start_time;
431 }
432}
433
434static void restart_processes()
435{
436 process_needs_restart = 0;
437 service_for_each_flags(SVC_RESTARTING,
438 restart_service_if_needed);
439}
440
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700441static void msg_start(const char *name)
442{
Hong-Mei Li11467182013-04-01 11:17:51 +0800443 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700444 char *tmp = NULL;
445 char *args = NULL;
446
447 if (!strchr(name, ':'))
448 svc = service_find_by_name(name);
449 else {
450 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800451 if (tmp) {
452 args = strchr(tmp, ':');
453 *args = '\0';
454 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700455
Hong-Mei Li11467182013-04-01 11:17:51 +0800456 svc = service_find_by_name(tmp);
457 }
San Mehatf24e2522009-05-19 13:30:46 -0700458 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700459
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700460 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700461 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700462 } else {
463 ERROR("no such service '%s'\n", name);
464 }
San Mehatf24e2522009-05-19 13:30:46 -0700465 if (tmp)
466 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700467}
468
469static void msg_stop(const char *name)
470{
471 struct service *svc = service_find_by_name(name);
472
473 if (svc) {
474 service_stop(svc);
475 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700476 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700477 }
478}
479
Mike Kasickb54f39f2012-01-25 23:48:46 -0500480static void msg_restart(const char *name)
481{
482 struct service *svc = service_find_by_name(name);
483
484 if (svc) {
485 service_restart(svc);
486 } else {
487 ERROR("no such service '%s'\n", name);
488 }
489}
490
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491void handle_control_message(const char *msg, const char *arg)
492{
493 if (!strcmp(msg,"start")) {
494 msg_start(arg);
495 } else if (!strcmp(msg,"stop")) {
496 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700497 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500498 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700499 } else {
500 ERROR("unknown control msg '%s'\n", msg);
501 }
502}
503
Colin Crossebc6ff12010-04-13 19:52:01 -0700504static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700505{
506 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700507 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700508 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700509 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{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700533 int ret;
534
Colin Crossebc6ff12010-04-13 19:52:01 -0700535 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
536 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700537 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700538 if (!cur_action)
539 return;
540 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
541 cur_command = get_first_command(cur_action);
542 } else {
543 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700544 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700545
546 if (!cur_command)
547 return;
548
549 ret = cur_command->func(cur_command->nargs, cur_command->args);
550 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700551}
552
Colin Crossf83d0b92010-04-21 12:04:20 -0700553static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554{
Colin Crossf83d0b92010-04-21 12:04:20 -0700555 int ret;
556 INFO("wait for %s\n", coldboot_done);
557 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
558 if (ret)
559 ERROR("Timed out waiting for %s\n", coldboot_done);
560 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700561}
562
Colin Crossebc6ff12010-04-13 19:52:01 -0700563static int keychord_init_action(int nargs, char **args)
564{
565 keychord_init();
566 return 0;
567}
568
569static int console_init_action(int nargs, char **args)
570{
571 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700572
573 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800574 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700575 }
576
577 fd = open(console_name, O_RDWR);
578 if (fd >= 0)
579 have_console = 1;
580 close(fd);
581
582 if( load_565rle_image(INIT_IMAGE_FILE) ) {
583 fd = open("/dev/tty0", O_WRONLY);
584 if (fd >= 0) {
585 const char *msg;
586 msg = "\n"
587 "\n"
588 "\n"
589 "\n"
590 "\n"
591 "\n"
592 "\n" // console is 40 cols x 30 lines
593 "\n"
594 "\n"
595 "\n"
596 "\n"
597 "\n"
598 "\n"
599 "\n"
600 " A N D R O I D ";
601 write(fd, msg, strlen(msg));
602 close(fd);
603 }
604 }
605 return 0;
606}
607
Dima Zavin5511c842011-12-19 11:21:32 -0800608static void import_kernel_nv(char *name, int for_emulator)
609{
610 char *value = strchr(name, '=');
611 int name_len = strlen(name);
612
613 if (value == 0) return;
614 *value++ = 0;
615 if (name_len == 0) return;
616
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400617 if (!strcmp(name,"selinux")) {
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500618 selinux_enabled = atoi(value);
619 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500620
Dima Zavin5511c842011-12-19 11:21:32 -0800621 if (for_emulator) {
622 /* in the emulator, export any kernel option with the
623 * ro.kernel. prefix */
624 char buff[PROP_NAME_MAX];
625 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
626
627 if (len < (int)sizeof(buff))
628 property_set( buff, value );
629 return;
630 }
631
632 if (!strcmp(name,"qemu")) {
633 strlcpy(qemu, value, sizeof(qemu));
634 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
635 const char *boot_prop_name = name + 12;
636 char prop[PROP_NAME_MAX];
637 int cnt;
638
639 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
640 if (cnt < PROP_NAME_MAX)
641 property_set(prop, value);
642 }
643}
644
645static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700646{
647 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800648 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800649 unsigned i;
650 struct {
651 const char *src_prop;
652 const char *dest_prop;
653 const char *def_val;
654 } prop_map[] = {
655 { "ro.boot.serialno", "ro.serialno", "", },
656 { "ro.boot.mode", "ro.bootmode", "unknown", },
657 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800658 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
659 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700660
Dima Zavin5511c842011-12-19 11:21:32 -0800661 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800662 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700663 if (ret > 0)
664 property_set(prop_map[i].dest_prop, tmp);
665 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800666 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800667 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700668
Colin Cross1a6f4c32013-01-28 17:13:35 -0800669 ret = property_get("ro.boot.console", tmp);
670 if (ret)
671 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800672
673 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800674 property_get("ro.bootmode", tmp);
675 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800676
677 /* if this was given on kernel command line, override what we read
678 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800679 ret = property_get("ro.boot.hardware", tmp);
680 if (ret)
681 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800682 property_set("ro.hardware", hardware);
683
684 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
685 property_set("ro.revision", tmp);
686
687 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700688 if (!strcmp(bootmode,"factory"))
689 property_set("ro.factorytest", "1");
690 else if (!strcmp(bootmode,"factory2"))
691 property_set("ro.factorytest", "2");
692 else
693 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800694}
Colin Crossebc6ff12010-04-13 19:52:01 -0700695
Dima Zavin5511c842011-12-19 11:21:32 -0800696static void process_kernel_cmdline(void)
697{
698 /* don't expose the raw commandline to nonpriv processes */
699 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700700
Dima Zavin5511c842011-12-19 11:21:32 -0800701 /* first pass does the common stuff, and finds if we are in qemu.
702 * second pass is only necessary for qemu to export all kernel params
703 * as props.
704 */
705 import_kernel_cmdline(0, import_kernel_nv);
706 if (qemu[0])
707 import_kernel_cmdline(1, import_kernel_nv);
708
709 /* now propogate the info given on command line to internal variables
710 * used by init as well as the current required properties
711 */
712 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700713}
714
715static int property_service_init_action(int nargs, char **args)
716{
717 /* read any property files on system or data and
718 * fire up the property service. This must happen
719 * after the ro.foo properties are set above so
720 * that /data/local.prop cannot interfere with them.
721 */
722 start_property_service();
723 return 0;
724}
725
726static int signal_init_action(int nargs, char **args)
727{
728 signal_init();
729 return 0;
730}
731
732static int check_startup_action(int nargs, char **args)
733{
734 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700735 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700736 (get_signal_fd() < 0)) {
737 ERROR("init startup failure\n");
738 exit(1);
739 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700740
741 /* signal that we hit this point */
742 unlink("/dev/.booting");
743
Colin Crossebc6ff12010-04-13 19:52:01 -0700744 return 0;
745}
746
747static int queue_property_triggers_action(int nargs, char **args)
748{
749 queue_all_property_triggers();
750 /* enable property triggers */
751 property_triggers_enabled = 1;
752 return 0;
753}
754
755#if BOOTCHART
756static int bootchart_init_action(int nargs, char **args)
757{
758 bootchart_count = bootchart_init();
759 if (bootchart_count < 0) {
760 ERROR("bootcharting init failure\n");
761 } else if (bootchart_count > 0) {
762 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
763 } else {
764 NOTICE("bootcharting ignored\n");
765 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100766
767 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700768}
769#endif
770
rpcraig63207cd2012-08-09 10:05:49 -0400771static const struct selinux_opt seopts_prop[] = {
William Roberts46e1bd82013-01-23 14:05:04 -0800772 { SELABEL_OPT_PATH, "/data/security/property_contexts" },
rpcraig63207cd2012-08-09 10:05:49 -0400773 { SELABEL_OPT_PATH, "/property_contexts" },
774 { 0, NULL }
775};
776
777struct selabel_handle* selinux_android_prop_context_handle(void)
778{
779 int i = 0;
780 struct selabel_handle* sehandle = NULL;
781 while ((sehandle == NULL) && seopts_prop[i].value) {
782 sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP, &seopts_prop[i], 1);
783 i++;
784 }
785
786 if (!sehandle) {
787 ERROR("SELinux: Could not load property_contexts: %s\n",
788 strerror(errno));
789 return NULL;
790 }
791 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[i - 1].value);
792 return sehandle;
793}
794
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400795void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500796{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400797 sehandle = selinux_android_file_context_handle();
rpcraig63207cd2012-08-09 10:05:49 -0400798 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400799}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500800
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400801int selinux_reload_policy(void)
802{
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500803 if (!selinux_enabled) {
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400804 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500805 }
806
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400807 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500808
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400809 if (selinux_android_reload_policy() == -1) {
810 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500811 }
812
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400813 if (sehandle)
814 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500815
rpcraig63207cd2012-08-09 10:05:49 -0400816 if (sehandle_prop)
817 selabel_close(sehandle_prop);
818
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400819 selinux_init_all_handles();
820 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500821}
rpcraig63207cd2012-08-09 10:05:49 -0400822
823int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
824{
825 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
826 return 0;
827}
828
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829int main(int argc, char **argv)
830{
Colin Crossebc6ff12010-04-13 19:52:01 -0700831 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700832 struct pollfd ufds[4];
833 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800834 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700835 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700836 int property_set_fd_init = 0;
837 int signal_fd_init = 0;
838 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800839 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700840
Colin Crossf83d0b92010-04-21 12:04:20 -0700841 if (!strcmp(basename(argv[0]), "ueventd"))
842 return ueventd_main(argc, argv);
843
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700844 if (!strcmp(basename(argv[0]), "watchdogd"))
845 return watchdogd_main(argc, argv);
846
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700847 /* clear the umask */
848 umask(0);
849
850 /* Get the basic filesystem setup we need put
851 * together in the initramdisk on / and then we'll
852 * let the rc file figure out the rest.
853 */
854 mkdir("/dev", 0755);
855 mkdir("/proc", 0755);
856 mkdir("/sys", 0755);
857
Nick Kralevich150f19e2010-06-22 16:35:43 -0700858 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859 mkdir("/dev/pts", 0755);
860 mkdir("/dev/socket", 0755);
861 mount("devpts", "/dev/pts", "devpts", 0, NULL);
862 mount("proc", "/proc", "proc", 0, NULL);
863 mount("sysfs", "/sys", "sysfs", 0, NULL);
864
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700865 /* indicate that booting is in progress to background fw loaders, etc */
866 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
867
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700868 /* We must have some place other than / to create the
869 * device nodes for kmsg and null, otherwise we won't
870 * be able to remount / read-only later on.
871 * Now that tmpfs is mounted on /dev, we can actually
872 * talk to the outside world.
873 */
874 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700875 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800876 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700877
Colin Crossf83d0b92010-04-21 12:04:20 -0700878 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -0800879
Dima Zavin5511c842011-12-19 11:21:32 -0800880 process_kernel_cmdline();
881
rpcraig63207cd2012-08-09 10:05:49 -0400882 union selinux_callback cb;
883 cb.func_log = klog_write;
884 selinux_set_callback(SELINUX_CB_LOG, cb);
885
886 cb.func_audit = audit_callback;
887 selinux_set_callback(SELINUX_CB_AUDIT, cb);
888
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500889 INFO("loading selinux policy\n");
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400890 if (selinux_enabled) {
891 if (selinux_android_load_policy() < 0) {
892 selinux_enabled = 0;
893 INFO("SELinux: Disabled due to failed policy load\n");
894 } else {
895 selinux_init_all_handles();
896 }
897 } else {
898 INFO("SELinux: Disabled by command line option\n");
899 }
900 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -0400901 * and therefore need their security context restored to the proper value.
902 * This must happen before /dev is populated by ueventd.
903 */
904 restorecon("/dev");
905 restorecon("/dev/socket");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500906
Dima Zavind7634c92011-12-16 14:18:06 -0800907 is_charger = !strcmp(bootmode, "charger");
908
909 INFO("property init\n");
Dima Zavin5511c842011-12-19 11:21:32 -0800910 if (!is_charger)
911 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -0800912
913 INFO("reading config file\n");
914 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700915
916 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700917
Colin Crossf83d0b92010-04-21 12:04:20 -0700918 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700919 queue_builtin_action(keychord_init_action, "keychord_init");
920 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700921
Dima Zavinca47cef2011-08-24 15:28:23 -0700922 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700923 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -0700924
925 /* skip mounting filesystems in charger mode */
Dima Zavind7634c92011-12-16 14:18:06 -0800926 if (!is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700927 action_for_each_trigger("early-fs", action_add_queue_tail);
928 action_for_each_trigger("fs", action_add_queue_tail);
929 action_for_each_trigger("post-fs", action_add_queue_tail);
930 action_for_each_trigger("post-fs-data", action_add_queue_tail);
931 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700932
Colin Crossebc6ff12010-04-13 19:52:01 -0700933 queue_builtin_action(property_service_init_action, "property_service_init");
934 queue_builtin_action(signal_init_action, "signal_init");
935 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700936
Dima Zavind7634c92011-12-16 14:18:06 -0800937 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700938 action_for_each_trigger("charger", action_add_queue_tail);
939 } else {
940 action_for_each_trigger("early-boot", action_add_queue_tail);
941 action_for_each_trigger("boot", action_add_queue_tail);
942 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700943
944 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -0800945 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700946
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700947
948#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700949 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700950#endif
951
952 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800953 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700954
Colin Crossebc6ff12010-04-13 19:52:01 -0700955 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700956 restart_processes();
957
Colin Crossebc6ff12010-04-13 19:52:01 -0700958 if (!property_set_fd_init && get_property_set_fd() > 0) {
959 ufds[fd_count].fd = get_property_set_fd();
960 ufds[fd_count].events = POLLIN;
961 ufds[fd_count].revents = 0;
962 fd_count++;
963 property_set_fd_init = 1;
964 }
965 if (!signal_fd_init && get_signal_fd() > 0) {
966 ufds[fd_count].fd = get_signal_fd();
967 ufds[fd_count].events = POLLIN;
968 ufds[fd_count].revents = 0;
969 fd_count++;
970 signal_fd_init = 1;
971 }
972 if (!keychord_fd_init && get_keychord_fd() > 0) {
973 ufds[fd_count].fd = get_keychord_fd();
974 ufds[fd_count].events = POLLIN;
975 ufds[fd_count].revents = 0;
976 fd_count++;
977 keychord_fd_init = 1;
978 }
979
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700980 if (process_needs_restart) {
981 timeout = (process_needs_restart - gettime()) * 1000;
982 if (timeout < 0)
983 timeout = 0;
984 }
985
Colin Crossebd46132010-04-22 11:52:23 -0700986 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -0700987 timeout = 0;
988
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700989#if BOOTCHART
990 if (bootchart_count > 0) {
991 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
992 timeout = BOOTCHART_POLLING_MS;
993 if (bootchart_step() < 0 || --bootchart_count == 0) {
994 bootchart_finish();
995 bootchart_count = 0;
996 }
997 }
998#endif
Colin Crossebc6ff12010-04-13 19:52:01 -0700999
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001000 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001001 if (nr <= 0)
1002 continue;
1003
Colin Crossebc6ff12010-04-13 19:52:01 -07001004 for (i = 0; i < fd_count; i++) {
1005 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001006 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001007 handle_property_set_fd();
1008 else if (ufds[i].fd == get_keychord_fd())
1009 handle_keychord();
1010 else if (ufds[i].fd == get_signal_fd())
1011 handle_signal();
1012 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001013 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001014 }
1015
1016 return 0;
1017}