blob: b7e34d04c5e6d6518f6a559a25b1540317c2bc4e [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>
Nick Kralevichb710ed22013-06-24 17:41:40 -070042#include <cutils/android_reboot.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043#include <cutils/sockets.h>
San Mehat4e221f02010-02-25 14:19:50 -080044#include <cutils/iosched_policy.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070045#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047
48#include <sys/system_properties.h>
49
50#include "devices.h"
51#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070052#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053#include "property_service.h"
The Android Open Source Project35237d12008-12-17 18:08:08 -080054#include "bootchart.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070055#include "signal_handler.h"
Colin Crossa8666952010-04-13 19:20:44 -070056#include "keychords.h"
Colin Cross6310a822010-04-20 14:29:05 -070057#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070058#include "util.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070059#include "ueventd.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070060#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061
Stephen Smalleye46f9d52012-01-13 08:48:47 -050062struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040063struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050064
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065static int property_triggers_enabled = 0;
66
67#if BOOTCHART
68static int bootchart_count;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069#endif
70
71static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073static char hardware[32];
74static unsigned revision = 0;
75static char qemu[32];
76
Colin Crossebc6ff12010-04-13 19:52:01 -070077static struct action *cur_action = NULL;
78static struct command *cur_command = NULL;
79static struct listnode *command_queue = NULL;
80
Colin Cross9c5366b2010-04-13 19:48:59 -070081void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082{
83 char pname[PROP_NAME_MAX];
84 int len = strlen(name);
85 if ((len + 10) > PROP_NAME_MAX)
86 return;
87 snprintf(pname, sizeof(pname), "init.svc.%s", name);
88 property_set(pname, state);
89}
90
91static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080092static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070093static time_t process_needs_restart;
94
95static const char *ENV[32];
96
97/* add_environment - add "key=value" to the current environment */
98int add_environment(const char *key, const char *val)
99{
100 int n;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700101
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102 for (n = 0; n < 31; n++) {
103 if (!ENV[n]) {
104 size_t len = strlen(key) + strlen(val) + 2;
105 char *entry = malloc(len);
106 snprintf(entry, len, "%s=%s", key, val);
107 ENV[n] = entry;
108 return 0;
109 }
110 }
111
112 return 1;
113}
114
115static void zap_stdio(void)
116{
117 int fd;
118 fd = open("/dev/null", O_RDWR);
119 dup2(fd, 0);
120 dup2(fd, 1);
121 dup2(fd, 2);
122 close(fd);
123}
124
125static void open_console()
126{
127 int fd;
128 if ((fd = open(console_name, O_RDWR)) < 0) {
129 fd = open("/dev/null", O_RDWR);
130 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700131 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700132 dup2(fd, 0);
133 dup2(fd, 1);
134 dup2(fd, 2);
135 close(fd);
136}
137
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700138static void publish_socket(const char *name, int fd)
139{
140 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
141 char val[64];
142
143 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
144 name,
145 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
146 snprintf(val, sizeof(val), "%d", fd);
147 add_environment(key, val);
148
149 /* make sure we don't close-on-exec */
150 fcntl(fd, F_SETFD, 0);
151}
152
San Mehatf24e2522009-05-19 13:30:46 -0700153void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700154{
155 struct stat s;
156 pid_t pid;
157 int needs_console;
158 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500159 char *scon = NULL;
160 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700161
Ken Sumrall752923c2010-12-03 16:33:31 -0800162 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700163 * state and immediately takes it out of the restarting
164 * state if it was in there
165 */
Mike Kasickb54f39f2012-01-25 23:48:46 -0500166 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700167 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700168
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169 /* running processes require no additional work -- if
170 * they're in the process of exiting, we've ensured
171 * that they will immediately restart on exit, unless
172 * they are ONESHOT
173 */
174 if (svc->flags & SVC_RUNNING) {
175 return;
176 }
177
178 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
179 if (needs_console && (!have_console)) {
180 ERROR("service '%s' requires console\n", svc->name);
181 svc->flags |= SVC_DISABLED;
182 return;
183 }
184
185 if (stat(svc->args[0], &s) != 0) {
186 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
187 svc->flags |= SVC_DISABLED;
188 return;
189 }
190
San Mehatf24e2522009-05-19 13:30:46 -0700191 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700192 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
193 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700194 svc->flags |= SVC_DISABLED;
195 return;
196 }
197
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500198 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500199 if (svc->seclabel) {
200 scon = strdup(svc->seclabel);
201 if (!scon) {
202 ERROR("Out of memory while starting '%s'\n", svc->name);
203 return;
204 }
205 } else {
206 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500207
Stephen Smalley30f30332012-11-16 14:34:27 -0500208 INFO("computing context for service '%s'\n", svc->args[0]);
209 rc = getcon(&mycon);
210 if (rc < 0) {
211 ERROR("could not get context while starting '%s'\n", svc->name);
212 return;
213 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500214
Stephen Smalley30f30332012-11-16 14:34:27 -0500215 rc = getfilecon(svc->args[0], &fcon);
216 if (rc < 0) {
217 ERROR("could not get context while starting '%s'\n", svc->name);
218 freecon(mycon);
219 return;
220 }
221
222 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500223 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500224 freecon(fcon);
225 if (rc < 0) {
226 ERROR("could not get context while starting '%s'\n", svc->name);
227 return;
228 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500229 }
230 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500231
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232 NOTICE("starting '%s'\n", svc->name);
233
234 pid = fork();
235
236 if (pid == 0) {
237 struct socketinfo *si;
238 struct svcenvinfo *ei;
239 char tmp[32];
240 int fd, sz;
241
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700242 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700243 if (properties_inited()) {
244 get_property_workspace(&fd, &sz);
245 sprintf(tmp, "%d,%d", dup(fd), sz);
246 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
247 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700248
249 for (ei = svc->envvars; ei; ei = ei->next)
250 add_environment(ei->name, ei->value);
251
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500252 setsockcreatecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500253
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700254 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400255 int socket_type = (
256 !strcmp(si->type, "stream") ? SOCK_STREAM :
257 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
258 int s = create_socket(si->name, socket_type,
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259 si->perm, si->uid, si->gid);
260 if (s >= 0) {
261 publish_socket(si->name, s);
262 }
263 }
264
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500265 freecon(scon);
266 scon = NULL;
267 setsockcreatecon(NULL);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500268
San Mehat4e221f02010-02-25 14:19:50 -0800269 if (svc->ioprio_class != IoSchedClass_NONE) {
270 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
271 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
272 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
273 }
274 }
275
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276 if (needs_console) {
277 setsid();
278 open_console();
279 } else {
280 zap_stdio();
281 }
282
283#if 0
284 for (n = 0; svc->args[n]; n++) {
285 INFO("args[%d] = '%s'\n", n, svc->args[n]);
286 }
287 for (n = 0; ENV[n]; n++) {
288 INFO("env[%d] = '%s'\n", n, ENV[n]);
289 }
290#endif
291
292 setpgid(0, getpid());
293
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800294 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800296 if (setgid(svc->gid) != 0) {
297 ERROR("setgid failed: %s\n", strerror(errno));
298 _exit(127);
299 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 }
301 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800302 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
303 ERROR("setgroups failed: %s\n", strerror(errno));
304 _exit(127);
305 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 }
307 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800308 if (setuid(svc->uid) != 0) {
309 ERROR("setuid failed: %s\n", strerror(errno));
310 _exit(127);
311 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500313 if (svc->seclabel) {
314 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
315 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
316 _exit(127);
317 }
318 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500319
San Mehat8ad15682009-05-20 08:50:40 -0700320 if (!dynamic_args) {
321 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
322 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
323 }
324 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700325 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700326 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700327 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700328 char *next = tmp;
329 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700330
331 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700332 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700333
San Mehatd4cdd132009-05-20 09:52:16 -0700334 while((bword = strsep(&next, " "))) {
335 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700336 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700337 break;
San Mehatf24e2522009-05-19 13:30:46 -0700338 }
339 arg_ptrs[arg_idx] = '\0';
340 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100341 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700342 _exit(127);
343 }
344
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500345 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500346
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700347 if (pid < 0) {
348 ERROR("failed to start '%s'\n", svc->name);
349 svc->pid = 0;
350 return;
351 }
352
353 svc->time_started = gettime();
354 svc->pid = pid;
355 svc->flags |= SVC_RUNNING;
356
Colin Cross3294bbb2010-04-19 17:11:33 -0700357 if (properties_inited())
358 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359}
360
Mike Kasickb54f39f2012-01-25 23:48:46 -0500361/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800362static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500364 /* The service is still SVC_RUNNING until its process exits, but if it has
365 * already exited it shoudn't attempt a restart yet. */
366 svc->flags &= (~SVC_RESTARTING);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700367
Mike Kasickb54f39f2012-01-25 23:48:46 -0500368 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800369 /* Hrm, an illegal flag. Default to SVC_DISABLED */
370 how = SVC_DISABLED;
371 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372 /* if the service has not yet started, prevent
373 * it from auto-starting with its class
374 */
Ken Sumralla2864802011-10-26 16:56:00 -0700375 if (how == SVC_RESET) {
376 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
377 } else {
378 svc->flags |= how;
379 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380
381 if (svc->pid) {
382 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800383 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384 notify_service_state(svc->name, "stopping");
385 } else {
386 notify_service_state(svc->name, "stopped");
387 }
388}
389
Ken Sumrall752923c2010-12-03 16:33:31 -0800390void service_reset(struct service *svc)
391{
392 service_stop_or_reset(svc, SVC_RESET);
393}
394
395void service_stop(struct service *svc)
396{
397 service_stop_or_reset(svc, SVC_DISABLED);
398}
399
Mike Kasickb54f39f2012-01-25 23:48:46 -0500400void service_restart(struct service *svc)
401{
402 if (svc->flags & SVC_RUNNING) {
403 /* Stop, wait, then start the service. */
404 service_stop_or_reset(svc, SVC_RESTART);
405 } else if (!(svc->flags & SVC_RESTARTING)) {
406 /* Just start the service since it's not running. */
407 service_start(svc, NULL);
408 } /* else: Service is restarting anyways. */
409}
410
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700411void property_changed(const char *name, const char *value)
412{
Colin Crossebc6ff12010-04-13 19:52:01 -0700413 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700414 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415}
416
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700417static void restart_service_if_needed(struct service *svc)
418{
419 time_t next_start_time = svc->time_started + 5;
420
421 if (next_start_time <= gettime()) {
422 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700423 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700424 return;
425 }
426
427 if ((next_start_time < process_needs_restart) ||
428 (process_needs_restart == 0)) {
429 process_needs_restart = next_start_time;
430 }
431}
432
433static void restart_processes()
434{
435 process_needs_restart = 0;
436 service_for_each_flags(SVC_RESTARTING,
437 restart_service_if_needed);
438}
439
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700440static void msg_start(const char *name)
441{
Hong-Mei Li11467182013-04-01 11:17:51 +0800442 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700443 char *tmp = NULL;
444 char *args = NULL;
445
446 if (!strchr(name, ':'))
447 svc = service_find_by_name(name);
448 else {
449 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800450 if (tmp) {
451 args = strchr(tmp, ':');
452 *args = '\0';
453 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700454
Hong-Mei Li11467182013-04-01 11:17:51 +0800455 svc = service_find_by_name(tmp);
456 }
San Mehatf24e2522009-05-19 13:30:46 -0700457 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700458
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700459 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700460 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700461 } else {
462 ERROR("no such service '%s'\n", name);
463 }
San Mehatf24e2522009-05-19 13:30:46 -0700464 if (tmp)
465 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466}
467
468static void msg_stop(const char *name)
469{
470 struct service *svc = service_find_by_name(name);
471
472 if (svc) {
473 service_stop(svc);
474 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700475 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700476 }
477}
478
Mike Kasickb54f39f2012-01-25 23:48:46 -0500479static void msg_restart(const char *name)
480{
481 struct service *svc = service_find_by_name(name);
482
483 if (svc) {
484 service_restart(svc);
485 } else {
486 ERROR("no such service '%s'\n", name);
487 }
488}
489
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700490void handle_control_message(const char *msg, const char *arg)
491{
492 if (!strcmp(msg,"start")) {
493 msg_start(arg);
494 } else if (!strcmp(msg,"stop")) {
495 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700496 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500497 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700498 } else {
499 ERROR("unknown control msg '%s'\n", msg);
500 }
501}
502
Colin Crossebc6ff12010-04-13 19:52:01 -0700503static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700504{
505 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700506 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700507 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700508 return NULL;
509
510 return node_to_item(node, struct command, clist);
511}
512
513static struct command *get_next_command(struct action *act, struct command *cmd)
514{
515 struct listnode *node;
516 node = cmd->clist.next;
517 if (!node)
518 return NULL;
519 if (node == &act->commands)
520 return NULL;
521
522 return node_to_item(node, struct command, clist);
523}
524
525static int is_last_command(struct action *act, struct command *cmd)
526{
527 return (list_tail(&act->commands) == &cmd->clist);
528}
529
530void execute_one_command(void)
531{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700532 int ret;
533
Colin Crossebc6ff12010-04-13 19:52:01 -0700534 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
535 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700536 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700537 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);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700543 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700544
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);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700550}
551
Colin Crossf83d0b92010-04-21 12:04:20 -0700552static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700553{
Colin Crossf83d0b92010-04-21 12:04:20 -0700554 int ret;
555 INFO("wait for %s\n", coldboot_done);
556 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
557 if (ret)
558 ERROR("Timed out waiting for %s\n", coldboot_done);
559 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700560}
561
Colin Crossebc6ff12010-04-13 19:52:01 -0700562static int keychord_init_action(int nargs, char **args)
563{
564 keychord_init();
565 return 0;
566}
567
568static int console_init_action(int nargs, char **args)
569{
570 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700571
572 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800573 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700574 }
575
576 fd = open(console_name, O_RDWR);
577 if (fd >= 0)
578 have_console = 1;
579 close(fd);
580
581 if( load_565rle_image(INIT_IMAGE_FILE) ) {
582 fd = open("/dev/tty0", O_WRONLY);
583 if (fd >= 0) {
584 const char *msg;
585 msg = "\n"
586 "\n"
587 "\n"
588 "\n"
589 "\n"
590 "\n"
591 "\n" // console is 40 cols x 30 lines
592 "\n"
593 "\n"
594 "\n"
595 "\n"
596 "\n"
597 "\n"
598 "\n"
599 " A N D R O I D ";
600 write(fd, msg, strlen(msg));
601 close(fd);
602 }
603 }
604 return 0;
605}
606
Dima Zavin5511c842011-12-19 11:21:32 -0800607static void import_kernel_nv(char *name, int for_emulator)
608{
609 char *value = strchr(name, '=');
610 int name_len = strlen(name);
611
612 if (value == 0) return;
613 *value++ = 0;
614 if (name_len == 0) return;
615
616 if (for_emulator) {
617 /* in the emulator, export any kernel option with the
618 * ro.kernel. prefix */
619 char buff[PROP_NAME_MAX];
620 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
621
622 if (len < (int)sizeof(buff))
623 property_set( buff, value );
624 return;
625 }
626
627 if (!strcmp(name,"qemu")) {
628 strlcpy(qemu, value, sizeof(qemu));
629 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
630 const char *boot_prop_name = name + 12;
631 char prop[PROP_NAME_MAX];
632 int cnt;
633
634 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
635 if (cnt < PROP_NAME_MAX)
636 property_set(prop, value);
637 }
638}
639
640static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700641{
642 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800643 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800644 unsigned i;
645 struct {
646 const char *src_prop;
647 const char *dest_prop;
648 const char *def_val;
649 } prop_map[] = {
650 { "ro.boot.serialno", "ro.serialno", "", },
651 { "ro.boot.mode", "ro.bootmode", "unknown", },
652 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800653 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
654 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700655
Dima Zavin5511c842011-12-19 11:21:32 -0800656 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800657 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700658 if (ret > 0)
659 property_set(prop_map[i].dest_prop, tmp);
660 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800661 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800662 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700663
Colin Cross1a6f4c32013-01-28 17:13:35 -0800664 ret = property_get("ro.boot.console", tmp);
665 if (ret)
666 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800667
668 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800669 property_get("ro.bootmode", tmp);
670 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800671
672 /* if this was given on kernel command line, override what we read
673 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800674 ret = property_get("ro.boot.hardware", tmp);
675 if (ret)
676 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800677 property_set("ro.hardware", hardware);
678
679 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
680 property_set("ro.revision", tmp);
681
682 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700683 if (!strcmp(bootmode,"factory"))
684 property_set("ro.factorytest", "1");
685 else if (!strcmp(bootmode,"factory2"))
686 property_set("ro.factorytest", "2");
687 else
688 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800689}
Colin Crossebc6ff12010-04-13 19:52:01 -0700690
Dima Zavin5511c842011-12-19 11:21:32 -0800691static void process_kernel_cmdline(void)
692{
693 /* don't expose the raw commandline to nonpriv processes */
694 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700695
Dima Zavin5511c842011-12-19 11:21:32 -0800696 /* first pass does the common stuff, and finds if we are in qemu.
697 * second pass is only necessary for qemu to export all kernel params
698 * as props.
699 */
700 import_kernel_cmdline(0, import_kernel_nv);
701 if (qemu[0])
702 import_kernel_cmdline(1, import_kernel_nv);
703
704 /* now propogate the info given on command line to internal variables
705 * used by init as well as the current required properties
706 */
707 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700708}
709
710static int property_service_init_action(int nargs, char **args)
711{
712 /* read any property files on system or data and
713 * fire up the property service. This must happen
714 * after the ro.foo properties are set above so
715 * that /data/local.prop cannot interfere with them.
716 */
717 start_property_service();
718 return 0;
719}
720
721static int signal_init_action(int nargs, char **args)
722{
723 signal_init();
724 return 0;
725}
726
727static int check_startup_action(int nargs, char **args)
728{
729 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700730 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700731 (get_signal_fd() < 0)) {
732 ERROR("init startup failure\n");
733 exit(1);
734 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700735
736 /* signal that we hit this point */
737 unlink("/dev/.booting");
738
Colin Crossebc6ff12010-04-13 19:52:01 -0700739 return 0;
740}
741
742static int queue_property_triggers_action(int nargs, char **args)
743{
744 queue_all_property_triggers();
745 /* enable property triggers */
746 property_triggers_enabled = 1;
747 return 0;
748}
749
750#if BOOTCHART
751static int bootchart_init_action(int nargs, char **args)
752{
753 bootchart_count = bootchart_init();
754 if (bootchart_count < 0) {
755 ERROR("bootcharting init failure\n");
756 } else if (bootchart_count > 0) {
757 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
758 } else {
759 NOTICE("bootcharting ignored\n");
760 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100761
762 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700763}
764#endif
765
rpcraig63207cd2012-08-09 10:05:49 -0400766static const struct selinux_opt seopts_prop[] = {
repo sync49be2402013-05-17 12:48:34 -0700767 { SELABEL_OPT_PATH, "/data/security/property_contexts" },
rpcraig63207cd2012-08-09 10:05:49 -0400768 { SELABEL_OPT_PATH, "/property_contexts" },
769 { 0, NULL }
770};
771
772struct selabel_handle* selinux_android_prop_context_handle(void)
773{
774 int i = 0;
775 struct selabel_handle* sehandle = NULL;
776 while ((sehandle == NULL) && seopts_prop[i].value) {
777 sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP, &seopts_prop[i], 1);
778 i++;
779 }
780
781 if (!sehandle) {
782 ERROR("SELinux: Could not load property_contexts: %s\n",
783 strerror(errno));
784 return NULL;
785 }
786 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[i - 1].value);
787 return sehandle;
788}
789
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400790void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500791{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400792 sehandle = selinux_android_file_context_handle();
rpcraig63207cd2012-08-09 10:05:49 -0400793 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400794}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500795
Nick Kralevich4838aa12013-06-26 15:37:26 -0700796static bool selinux_is_disabled(void)
797{
798 char tmp[PROP_VALUE_MAX];
799
800 if (access("/sys/fs/selinux", F_OK) != 0) {
801 /* SELinux is not compiled into the kernel, or has been disabled
802 * via the kernel command line "selinux=0".
803 */
804 return true;
805 }
806
807 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
808 /* SELinux is compiled into the kernel, but we've been told to disable it. */
809 return true;
810 }
811
812 return false;
813}
814
815static bool selinux_is_enforcing(void)
816{
817 char tmp[PROP_VALUE_MAX];
818
819 if (property_get("ro.boot.selinux", tmp) == 0) {
820 /* Property is not set. Assume enforcing */
821 return true;
822 }
823
824 if (strcmp(tmp, "permissive") == 0) {
825 /* SELinux is in the kernel, but we've been told to go into permissive mode */
826 return false;
827 }
828
829 if (strcmp(tmp, "enforcing") != 0) {
830 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
831 }
832
833 return true;
834}
835
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400836int selinux_reload_policy(void)
837{
Nick Kralevich4838aa12013-06-26 15:37:26 -0700838 if (selinux_is_disabled()) {
839 return -1;
840 }
841
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400842 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500843
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400844 if (selinux_android_reload_policy() == -1) {
845 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500846 }
847
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400848 if (sehandle)
849 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500850
rpcraig63207cd2012-08-09 10:05:49 -0400851 if (sehandle_prop)
852 selabel_close(sehandle_prop);
853
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400854 selinux_init_all_handles();
855 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500856}
rpcraig63207cd2012-08-09 10:05:49 -0400857
858int audit_callback(void *data, security_class_t cls, char *buf, size_t len)
859{
860 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
861 return 0;
862}
863
Nick Kralevichb710ed22013-06-24 17:41:40 -0700864static void selinux_initialize(void)
865{
Nick Kralevich4838aa12013-06-26 15:37:26 -0700866 if (selinux_is_disabled()) {
Nick Kralevichb710ed22013-06-24 17:41:40 -0700867 return;
868 }
869
870 INFO("loading selinux policy\n");
871 if (selinux_android_load_policy() < 0) {
872 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
873 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
874 while (1) { pause(); } // never reached
875 }
876
877 selinux_init_all_handles();
Nick Kralevich4838aa12013-06-26 15:37:26 -0700878 bool is_enforcing = selinux_is_enforcing();
879 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
880 security_setenforce(is_enforcing);
Nick Kralevichb710ed22013-06-24 17:41:40 -0700881}
882
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700883int main(int argc, char **argv)
884{
Colin Crossebc6ff12010-04-13 19:52:01 -0700885 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886 struct pollfd ufds[4];
887 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800888 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700889 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700890 int property_set_fd_init = 0;
891 int signal_fd_init = 0;
892 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800893 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700894
Colin Crossf83d0b92010-04-21 12:04:20 -0700895 if (!strcmp(basename(argv[0]), "ueventd"))
896 return ueventd_main(argc, argv);
897
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700898 if (!strcmp(basename(argv[0]), "watchdogd"))
899 return watchdogd_main(argc, argv);
900
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700901 /* clear the umask */
902 umask(0);
903
904 /* Get the basic filesystem setup we need put
905 * together in the initramdisk on / and then we'll
906 * let the rc file figure out the rest.
907 */
908 mkdir("/dev", 0755);
909 mkdir("/proc", 0755);
910 mkdir("/sys", 0755);
911
Nick Kralevich150f19e2010-06-22 16:35:43 -0700912 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700913 mkdir("/dev/pts", 0755);
914 mkdir("/dev/socket", 0755);
915 mount("devpts", "/dev/pts", "devpts", 0, NULL);
916 mount("proc", "/proc", "proc", 0, NULL);
917 mount("sysfs", "/sys", "sysfs", 0, NULL);
918
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700919 /* indicate that booting is in progress to background fw loaders, etc */
920 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
921
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700922 /* We must have some place other than / to create the
923 * device nodes for kmsg and null, otherwise we won't
924 * be able to remount / read-only later on.
925 * Now that tmpfs is mounted on /dev, we can actually
926 * talk to the outside world.
927 */
928 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -0700929 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800930 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700931
Colin Crossf83d0b92010-04-21 12:04:20 -0700932 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -0800933
Dima Zavin5511c842011-12-19 11:21:32 -0800934 process_kernel_cmdline();
935
rpcraig63207cd2012-08-09 10:05:49 -0400936 union selinux_callback cb;
937 cb.func_log = klog_write;
938 selinux_set_callback(SELINUX_CB_LOG, cb);
939
940 cb.func_audit = audit_callback;
941 selinux_set_callback(SELINUX_CB_AUDIT, cb);
942
Nick Kralevichb710ed22013-06-24 17:41:40 -0700943 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400944 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -0400945 * and therefore need their security context restored to the proper value.
946 * This must happen before /dev is populated by ueventd.
947 */
948 restorecon("/dev");
949 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -0800950 restorecon("/dev/__properties__");
Nick Kralevichf29c5332013-07-11 15:38:26 -0700951 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500952
Dima Zavind7634c92011-12-16 14:18:06 -0800953 is_charger = !strcmp(bootmode, "charger");
954
955 INFO("property init\n");
Dima Zavin5511c842011-12-19 11:21:32 -0800956 if (!is_charger)
957 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -0800958
959 INFO("reading config file\n");
960 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700961
962 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700963
Colin Crossf83d0b92010-04-21 12:04:20 -0700964 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Colin Crossebc6ff12010-04-13 19:52:01 -0700965 queue_builtin_action(keychord_init_action, "keychord_init");
966 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700967
Dima Zavinca47cef2011-08-24 15:28:23 -0700968 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700969 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -0700970
971 /* skip mounting filesystems in charger mode */
Dima Zavind7634c92011-12-16 14:18:06 -0800972 if (!is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700973 action_for_each_trigger("early-fs", action_add_queue_tail);
974 action_for_each_trigger("fs", action_add_queue_tail);
975 action_for_each_trigger("post-fs", action_add_queue_tail);
976 action_for_each_trigger("post-fs-data", action_add_queue_tail);
977 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700978
Colin Crossebc6ff12010-04-13 19:52:01 -0700979 queue_builtin_action(property_service_init_action, "property_service_init");
980 queue_builtin_action(signal_init_action, "signal_init");
981 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700982
Dima Zavind7634c92011-12-16 14:18:06 -0800983 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -0700984 action_for_each_trigger("charger", action_add_queue_tail);
985 } else {
986 action_for_each_trigger("early-boot", action_add_queue_tail);
987 action_for_each_trigger("boot", action_add_queue_tail);
988 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700989
990 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -0800991 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700992
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700993
994#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -0700995 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700996#endif
997
998 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800999 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001000
Colin Crossebc6ff12010-04-13 19:52:01 -07001001 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001002 restart_processes();
1003
Colin Crossebc6ff12010-04-13 19:52:01 -07001004 if (!property_set_fd_init && get_property_set_fd() > 0) {
1005 ufds[fd_count].fd = get_property_set_fd();
1006 ufds[fd_count].events = POLLIN;
1007 ufds[fd_count].revents = 0;
1008 fd_count++;
1009 property_set_fd_init = 1;
1010 }
1011 if (!signal_fd_init && get_signal_fd() > 0) {
1012 ufds[fd_count].fd = get_signal_fd();
1013 ufds[fd_count].events = POLLIN;
1014 ufds[fd_count].revents = 0;
1015 fd_count++;
1016 signal_fd_init = 1;
1017 }
1018 if (!keychord_fd_init && get_keychord_fd() > 0) {
1019 ufds[fd_count].fd = get_keychord_fd();
1020 ufds[fd_count].events = POLLIN;
1021 ufds[fd_count].revents = 0;
1022 fd_count++;
1023 keychord_fd_init = 1;
1024 }
1025
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001026 if (process_needs_restart) {
1027 timeout = (process_needs_restart - gettime()) * 1000;
1028 if (timeout < 0)
1029 timeout = 0;
1030 }
1031
Colin Crossebd46132010-04-22 11:52:23 -07001032 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -07001033 timeout = 0;
1034
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001035#if BOOTCHART
1036 if (bootchart_count > 0) {
1037 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
1038 timeout = BOOTCHART_POLLING_MS;
1039 if (bootchart_step() < 0 || --bootchart_count == 0) {
1040 bootchart_finish();
1041 bootchart_count = 0;
1042 }
1043 }
1044#endif
Colin Crossebc6ff12010-04-13 19:52:01 -07001045
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001046 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001047 if (nr <= 0)
1048 continue;
1049
Colin Crossebc6ff12010-04-13 19:52:01 -07001050 for (i = 0; i < fd_count; i++) {
1051 if (ufds[i].revents == POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001052 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001053 handle_property_set_fd();
1054 else if (ufds[i].fd == get_keychord_fd())
1055 handle_keychord();
1056 else if (ufds[i].fd == get_signal_fd())
1057 handle_signal();
1058 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001059 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001060 }
1061
1062 return 0;
1063}