blob: 97c33e481bc6bb05701cb633937faacec9ebccf4 [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 Kralevich56fa0ac2013-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>
Alex Klyubin0d872d82013-08-16 13:19:24 -070045#include <cutils/fs.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070046#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include <termios.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049#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
Colin Crossebc6ff12010-04-13 19:52:01 -070076static struct action *cur_action = NULL;
77static struct command *cur_command = NULL;
78static struct listnode *command_queue = NULL;
79
Colin Cross9c5366b2010-04-13 19:48:59 -070080void notify_service_state(const char *name, const char *state)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081{
82 char pname[PROP_NAME_MAX];
83 int len = strlen(name);
84 if ((len + 10) > PROP_NAME_MAX)
85 return;
86 snprintf(pname, sizeof(pname), "init.svc.%s", name);
87 property_set(pname, state);
88}
89
90static int have_console;
Hong-Mei Li11467182013-04-01 11:17:51 +080091static char console_name[PROP_VALUE_MAX] = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092static time_t process_needs_restart;
93
94static const char *ENV[32];
95
96/* add_environment - add "key=value" to the current environment */
97int add_environment(const char *key, const char *val)
98{
99 int n;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700100
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101 for (n = 0; n < 31; n++) {
102 if (!ENV[n]) {
103 size_t len = strlen(key) + strlen(val) + 2;
104 char *entry = malloc(len);
105 snprintf(entry, len, "%s=%s", key, val);
106 ENV[n] = entry;
107 return 0;
108 }
109 }
110
111 return 1;
112}
113
114static void zap_stdio(void)
115{
116 int fd;
117 fd = open("/dev/null", O_RDWR);
118 dup2(fd, 0);
119 dup2(fd, 1);
120 dup2(fd, 2);
121 close(fd);
122}
123
124static void open_console()
125{
126 int fd;
127 if ((fd = open(console_name, O_RDWR)) < 0) {
128 fd = open("/dev/null", O_RDWR);
129 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700130 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131 dup2(fd, 0);
132 dup2(fd, 1);
133 dup2(fd, 2);
134 close(fd);
135}
136
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137static void publish_socket(const char *name, int fd)
138{
139 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
140 char val[64];
141
142 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
143 name,
144 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
145 snprintf(val, sizeof(val), "%d", fd);
146 add_environment(key, val);
147
148 /* make sure we don't close-on-exec */
149 fcntl(fd, F_SETFD, 0);
150}
151
San Mehatf24e2522009-05-19 13:30:46 -0700152void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700153{
154 struct stat s;
155 pid_t pid;
156 int needs_console;
157 int n;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500158 char *scon = NULL;
159 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700160
Ken Sumrall752923c2010-12-03 16:33:31 -0800161 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700162 * state and immediately takes it out of the restarting
163 * state if it was in there
164 */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700165 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700166 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700167
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700168 /* running processes require no additional work -- if
169 * they're in the process of exiting, we've ensured
170 * that they will immediately restart on exit, unless
171 * they are ONESHOT
172 */
173 if (svc->flags & SVC_RUNNING) {
174 return;
175 }
176
177 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
178 if (needs_console && (!have_console)) {
179 ERROR("service '%s' requires console\n", svc->name);
180 svc->flags |= SVC_DISABLED;
181 return;
182 }
183
184 if (stat(svc->args[0], &s) != 0) {
185 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
186 svc->flags |= SVC_DISABLED;
187 return;
188 }
189
San Mehatf24e2522009-05-19 13:30:46 -0700190 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700191 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
192 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700193 svc->flags |= SVC_DISABLED;
194 return;
195 }
196
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500197 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500198 if (svc->seclabel) {
199 scon = strdup(svc->seclabel);
200 if (!scon) {
201 ERROR("Out of memory while starting '%s'\n", svc->name);
202 return;
203 }
204 } else {
205 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500206
Stephen Smalley30f30332012-11-16 14:34:27 -0500207 INFO("computing context for service '%s'\n", svc->args[0]);
208 rc = getcon(&mycon);
209 if (rc < 0) {
210 ERROR("could not get context while starting '%s'\n", svc->name);
211 return;
212 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500213
Stephen Smalley30f30332012-11-16 14:34:27 -0500214 rc = getfilecon(svc->args[0], &fcon);
215 if (rc < 0) {
216 ERROR("could not get context while starting '%s'\n", svc->name);
217 freecon(mycon);
218 return;
219 }
220
221 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500222 if (rc == 0 && !strcmp(scon, mycon)) {
223 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
224 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500225 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500226 freecon(fcon);
227 if (rc < 0) {
228 ERROR("could not get context while starting '%s'\n", svc->name);
229 return;
230 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500231 }
232 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500233
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234 NOTICE("starting '%s'\n", svc->name);
235
236 pid = fork();
237
238 if (pid == 0) {
239 struct socketinfo *si;
240 struct svcenvinfo *ei;
241 char tmp[32];
242 int fd, sz;
243
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700244 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700245 if (properties_inited()) {
246 get_property_workspace(&fd, &sz);
247 sprintf(tmp, "%d,%d", dup(fd), sz);
248 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
249 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250
251 for (ei = svc->envvars; ei; ei = ei->next)
252 add_environment(ei->name, ei->value);
253
254 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,
Stephen Smalley8348d272013-05-13 12:37:04 -0400259 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700260 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;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500267
San Mehat4e221f02010-02-25 14:19:50 -0800268 if (svc->ioprio_class != IoSchedClass_NONE) {
269 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
270 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
271 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
272 }
273 }
274
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275 if (needs_console) {
276 setsid();
277 open_console();
278 } else {
279 zap_stdio();
280 }
281
282#if 0
283 for (n = 0; svc->args[n]; n++) {
284 INFO("args[%d] = '%s'\n", n, svc->args[n]);
285 }
286 for (n = 0; ENV[n]; n++) {
287 INFO("env[%d] = '%s'\n", n, ENV[n]);
288 }
289#endif
290
291 setpgid(0, getpid());
292
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800293 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700294 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800295 if (setgid(svc->gid) != 0) {
296 ERROR("setgid failed: %s\n", strerror(errno));
297 _exit(127);
298 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299 }
300 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800301 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
302 ERROR("setgroups failed: %s\n", strerror(errno));
303 _exit(127);
304 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305 }
306 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800307 if (setuid(svc->uid) != 0) {
308 ERROR("setuid failed: %s\n", strerror(errno));
309 _exit(127);
310 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500312 if (svc->seclabel) {
313 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
314 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
315 _exit(127);
316 }
317 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500318
San Mehat8ad15682009-05-20 08:50:40 -0700319 if (!dynamic_args) {
320 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
321 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
322 }
323 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700324 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700325 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700326 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700327 char *next = tmp;
328 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700329
330 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700331 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700332
San Mehatd4cdd132009-05-20 09:52:16 -0700333 while((bword = strsep(&next, " "))) {
334 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700335 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700336 break;
San Mehatf24e2522009-05-19 13:30:46 -0700337 }
338 arg_ptrs[arg_idx] = '\0';
339 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100340 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700341 _exit(127);
342 }
343
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500344 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500345
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346 if (pid < 0) {
347 ERROR("failed to start '%s'\n", svc->name);
348 svc->pid = 0;
349 return;
350 }
351
352 svc->time_started = gettime();
353 svc->pid = pid;
354 svc->flags |= SVC_RUNNING;
355
Colin Cross3294bbb2010-04-19 17:11:33 -0700356 if (properties_inited())
357 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358}
359
Mike Kasickb54f39f2012-01-25 23:48:46 -0500360/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800361static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500363 /* The service is still SVC_RUNNING until its process exits, but if it has
364 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700365 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700366
Mike Kasickb54f39f2012-01-25 23:48:46 -0500367 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800368 /* Hrm, an illegal flag. Default to SVC_DISABLED */
369 how = SVC_DISABLED;
370 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 /* if the service has not yet started, prevent
372 * it from auto-starting with its class
373 */
Ken Sumralla2864802011-10-26 16:56:00 -0700374 if (how == SVC_RESET) {
375 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
376 } else {
377 svc->flags |= how;
378 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379
380 if (svc->pid) {
381 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800382 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383 notify_service_state(svc->name, "stopping");
384 } else {
385 notify_service_state(svc->name, "stopped");
386 }
387}
388
Ken Sumrall752923c2010-12-03 16:33:31 -0800389void service_reset(struct service *svc)
390{
391 service_stop_or_reset(svc, SVC_RESET);
392}
393
394void service_stop(struct service *svc)
395{
396 service_stop_or_reset(svc, SVC_DISABLED);
397}
398
Mike Kasickb54f39f2012-01-25 23:48:46 -0500399void service_restart(struct service *svc)
400{
401 if (svc->flags & SVC_RUNNING) {
402 /* Stop, wait, then start the service. */
403 service_stop_or_reset(svc, SVC_RESTART);
404 } else if (!(svc->flags & SVC_RESTARTING)) {
405 /* Just start the service since it's not running. */
406 service_start(svc, NULL);
407 } /* else: Service is restarting anyways. */
408}
409
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700410void property_changed(const char *name, const char *value)
411{
Colin Crossebc6ff12010-04-13 19:52:01 -0700412 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700413 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700414}
415
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416static void restart_service_if_needed(struct service *svc)
417{
418 time_t next_start_time = svc->time_started + 5;
419
420 if (next_start_time <= gettime()) {
421 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700422 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700423 return;
424 }
425
426 if ((next_start_time < process_needs_restart) ||
427 (process_needs_restart == 0)) {
428 process_needs_restart = next_start_time;
429 }
430}
431
432static void restart_processes()
433{
434 process_needs_restart = 0;
435 service_for_each_flags(SVC_RESTARTING,
436 restart_service_if_needed);
437}
438
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700439static void msg_start(const char *name)
440{
Hong-Mei Li11467182013-04-01 11:17:51 +0800441 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700442 char *tmp = NULL;
443 char *args = NULL;
444
445 if (!strchr(name, ':'))
446 svc = service_find_by_name(name);
447 else {
448 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800449 if (tmp) {
450 args = strchr(tmp, ':');
451 *args = '\0';
452 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700453
Hong-Mei Li11467182013-04-01 11:17:51 +0800454 svc = service_find_by_name(tmp);
455 }
San Mehatf24e2522009-05-19 13:30:46 -0700456 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700457
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700458 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700459 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700460 } else {
461 ERROR("no such service '%s'\n", name);
462 }
San Mehatf24e2522009-05-19 13:30:46 -0700463 if (tmp)
464 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700465}
466
467static void msg_stop(const char *name)
468{
469 struct service *svc = service_find_by_name(name);
470
471 if (svc) {
472 service_stop(svc);
473 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700474 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475 }
476}
477
Mike Kasickb54f39f2012-01-25 23:48:46 -0500478static void msg_restart(const char *name)
479{
480 struct service *svc = service_find_by_name(name);
481
482 if (svc) {
483 service_restart(svc);
484 } else {
485 ERROR("no such service '%s'\n", name);
486 }
487}
488
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489void handle_control_message(const char *msg, const char *arg)
490{
491 if (!strcmp(msg,"start")) {
492 msg_start(arg);
493 } else if (!strcmp(msg,"stop")) {
494 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700495 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500496 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700497 } else {
498 ERROR("unknown control msg '%s'\n", msg);
499 }
500}
501
Colin Crossebc6ff12010-04-13 19:52:01 -0700502static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700503{
504 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700505 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700506 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700507 return NULL;
508
509 return node_to_item(node, struct command, clist);
510}
511
512static struct command *get_next_command(struct action *act, struct command *cmd)
513{
514 struct listnode *node;
515 node = cmd->clist.next;
516 if (!node)
517 return NULL;
518 if (node == &act->commands)
519 return NULL;
520
521 return node_to_item(node, struct command, clist);
522}
523
524static int is_last_command(struct action *act, struct command *cmd)
525{
526 return (list_tail(&act->commands) == &cmd->clist);
527}
528
529void execute_one_command(void)
530{
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700531 int ret;
532
Colin Crossebc6ff12010-04-13 19:52:01 -0700533 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
534 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700535 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700536 if (!cur_action)
537 return;
538 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
539 cur_command = get_first_command(cur_action);
540 } else {
541 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700542 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700543
544 if (!cur_command)
545 return;
546
547 ret = cur_command->func(cur_command->nargs, cur_command->args);
548 INFO("command '%s' r=%d\n", cur_command->args[0], ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700549}
550
Colin Crossf83d0b92010-04-21 12:04:20 -0700551static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700552{
Colin Crossf83d0b92010-04-21 12:04:20 -0700553 int ret;
554 INFO("wait for %s\n", coldboot_done);
555 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
556 if (ret)
557 ERROR("Timed out waiting for %s\n", coldboot_done);
558 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700559}
560
Alex Klyubin0d872d82013-08-16 13:19:24 -0700561/*
562 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
563 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
564 * Does nothing if Hardware RNG is not present.
565 *
566 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
567 * mixed into the primary pool of Linux RNG and the entropy estimate is left
568 * unmodified.
569 *
570 * If the HW RNG device /dev/hw_random is present, we require that at least
571 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
572 * devices/configurations where these I/O operations are blocking for a long
573 * time. We do not reboot or halt on failures, as this is a best-effort
574 * attempt.
575 */
576static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
577{
578 int result = -1;
579 int hwrandom_fd = -1;
580 int urandom_fd = -1;
581 char buf[512];
582 ssize_t chunk_size;
583 size_t total_bytes_written = 0;
584
585 hwrandom_fd = TEMP_FAILURE_RETRY(
586 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW));
587 if (hwrandom_fd == -1) {
588 if (errno == ENOENT) {
589 ERROR("/dev/hw_random not found\n");
590 /* It's not an error to not have a Hardware RNG. */
591 result = 0;
592 } else {
593 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
594 }
595 goto ret;
596 }
597
598 urandom_fd = TEMP_FAILURE_RETRY(
599 open("/dev/urandom", O_WRONLY | O_NOFOLLOW));
600 if (urandom_fd == -1) {
601 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
602 goto ret;
603 }
604
605 while (total_bytes_written < sizeof(buf)) {
606 chunk_size = TEMP_FAILURE_RETRY(
607 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
608 if (chunk_size == -1) {
609 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
610 goto ret;
611 } else if (chunk_size == 0) {
612 ERROR("Failed to read from /dev/hw_random: EOF\n");
613 goto ret;
614 }
615
616 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
617 if (chunk_size == -1) {
618 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
619 goto ret;
620 }
621 total_bytes_written += chunk_size;
622 }
623
Elliott Hughesccecf142014-01-16 10:53:11 -0800624 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700625 total_bytes_written);
626 result = 0;
627
628ret:
629 if (hwrandom_fd != -1) {
630 close(hwrandom_fd);
631 }
632 if (urandom_fd != -1) {
633 close(urandom_fd);
634 }
635 memset(buf, 0, sizeof(buf));
636 return result;
637}
638
Colin Crossebc6ff12010-04-13 19:52:01 -0700639static int keychord_init_action(int nargs, char **args)
640{
641 keychord_init();
642 return 0;
643}
644
645static int console_init_action(int nargs, char **args)
646{
647 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700648
649 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800650 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700651 }
652
653 fd = open(console_name, O_RDWR);
654 if (fd >= 0)
655 have_console = 1;
656 close(fd);
657
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200658 fd = open("/dev/tty0", O_WRONLY);
659 if (fd >= 0) {
660 const char *msg;
661 msg = "\n"
662 "\n"
663 "\n"
664 "\n"
665 "\n"
666 "\n"
667 "\n" // console is 40 cols x 30 lines
668 "\n"
669 "\n"
670 "\n"
671 "\n"
672 "\n"
673 "\n"
674 "\n"
675 " A N D R O I D ";
676 write(fd, msg, strlen(msg));
677 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700678 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200679
Colin Crossebc6ff12010-04-13 19:52:01 -0700680 return 0;
681}
682
Dima Zavin5511c842011-12-19 11:21:32 -0800683static void import_kernel_nv(char *name, int for_emulator)
684{
685 char *value = strchr(name, '=');
686 int name_len = strlen(name);
687
688 if (value == 0) return;
689 *value++ = 0;
690 if (name_len == 0) return;
691
692 if (for_emulator) {
693 /* in the emulator, export any kernel option with the
694 * ro.kernel. prefix */
695 char buff[PROP_NAME_MAX];
696 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
697
698 if (len < (int)sizeof(buff))
699 property_set( buff, value );
700 return;
701 }
702
703 if (!strcmp(name,"qemu")) {
704 strlcpy(qemu, value, sizeof(qemu));
705 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
706 const char *boot_prop_name = name + 12;
707 char prop[PROP_NAME_MAX];
708 int cnt;
709
710 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
711 if (cnt < PROP_NAME_MAX)
712 property_set(prop, value);
713 }
714}
715
716static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700717{
718 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800719 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800720 unsigned i;
721 struct {
722 const char *src_prop;
723 const char *dest_prop;
724 const char *def_val;
725 } prop_map[] = {
726 { "ro.boot.serialno", "ro.serialno", "", },
727 { "ro.boot.mode", "ro.bootmode", "unknown", },
728 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800729 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
730 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700731
Dima Zavin5511c842011-12-19 11:21:32 -0800732 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800733 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700734 if (ret > 0)
735 property_set(prop_map[i].dest_prop, tmp);
736 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800737 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800738 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700739
Colin Cross1a6f4c32013-01-28 17:13:35 -0800740 ret = property_get("ro.boot.console", tmp);
741 if (ret)
742 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800743
744 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800745 property_get("ro.bootmode", tmp);
746 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800747
748 /* if this was given on kernel command line, override what we read
749 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800750 ret = property_get("ro.boot.hardware", tmp);
751 if (ret)
752 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800753 property_set("ro.hardware", hardware);
754
755 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
756 property_set("ro.revision", tmp);
757
758 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700759 if (!strcmp(bootmode,"factory"))
760 property_set("ro.factorytest", "1");
761 else if (!strcmp(bootmode,"factory2"))
762 property_set("ro.factorytest", "2");
763 else
764 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800765}
Colin Crossebc6ff12010-04-13 19:52:01 -0700766
Dima Zavin5511c842011-12-19 11:21:32 -0800767static void process_kernel_cmdline(void)
768{
769 /* don't expose the raw commandline to nonpriv processes */
770 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700771
Dima Zavin5511c842011-12-19 11:21:32 -0800772 /* first pass does the common stuff, and finds if we are in qemu.
773 * second pass is only necessary for qemu to export all kernel params
774 * as props.
775 */
776 import_kernel_cmdline(0, import_kernel_nv);
777 if (qemu[0])
778 import_kernel_cmdline(1, import_kernel_nv);
779
780 /* now propogate the info given on command line to internal variables
781 * used by init as well as the current required properties
782 */
783 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700784}
785
786static int property_service_init_action(int nargs, char **args)
787{
788 /* read any property files on system or data and
789 * fire up the property service. This must happen
790 * after the ro.foo properties are set above so
791 * that /data/local.prop cannot interfere with them.
792 */
793 start_property_service();
794 return 0;
795}
796
797static int signal_init_action(int nargs, char **args)
798{
799 signal_init();
800 return 0;
801}
802
803static int check_startup_action(int nargs, char **args)
804{
805 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700806 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700807 (get_signal_fd() < 0)) {
808 ERROR("init startup failure\n");
809 exit(1);
810 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700811
812 /* signal that we hit this point */
813 unlink("/dev/.booting");
814
Colin Crossebc6ff12010-04-13 19:52:01 -0700815 return 0;
816}
817
818static int queue_property_triggers_action(int nargs, char **args)
819{
820 queue_all_property_triggers();
821 /* enable property triggers */
822 property_triggers_enabled = 1;
823 return 0;
824}
825
826#if BOOTCHART
827static int bootchart_init_action(int nargs, char **args)
828{
829 bootchart_count = bootchart_init();
830 if (bootchart_count < 0) {
831 ERROR("bootcharting init failure\n");
832 } else if (bootchart_count > 0) {
833 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
834 } else {
835 NOTICE("bootcharting ignored\n");
836 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100837
838 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700839}
840#endif
841
rpcraig63207cd2012-08-09 10:05:49 -0400842static const struct selinux_opt seopts_prop[] = {
rpcraig63207cd2012-08-09 10:05:49 -0400843 { SELABEL_OPT_PATH, "/property_contexts" },
Robert Craig03daf052014-03-17 21:16:53 -0400844 { SELABEL_OPT_PATH, "/data/security/current/property_contexts" },
rpcraig63207cd2012-08-09 10:05:49 -0400845 { 0, NULL }
846};
847
848struct selabel_handle* selinux_android_prop_context_handle(void)
849{
Robert Craig03daf052014-03-17 21:16:53 -0400850 int policy_index = selinux_android_use_data_policy() ? 1 : 0;
851 struct selabel_handle* sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP,
852 &seopts_prop[policy_index], 1);
rpcraig63207cd2012-08-09 10:05:49 -0400853 if (!sehandle) {
854 ERROR("SELinux: Could not load property_contexts: %s\n",
855 strerror(errno));
856 return NULL;
857 }
Robert Craig03daf052014-03-17 21:16:53 -0400858 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[policy_index].value);
rpcraig63207cd2012-08-09 10:05:49 -0400859 return sehandle;
860}
861
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400862void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500863{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400864 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500865 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400866 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400867}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500868
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700869static bool selinux_is_disabled(void)
870{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700871#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700872 char tmp[PROP_VALUE_MAX];
873
874 if (access("/sys/fs/selinux", F_OK) != 0) {
875 /* SELinux is not compiled into the kernel, or has been disabled
876 * via the kernel command line "selinux=0".
877 */
878 return true;
879 }
880
881 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
882 /* SELinux is compiled into the kernel, but we've been told to disable it. */
883 return true;
884 }
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700885#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700886
887 return false;
888}
889
890static bool selinux_is_enforcing(void)
891{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700892#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700893 char tmp[PROP_VALUE_MAX];
894
895 if (property_get("ro.boot.selinux", tmp) == 0) {
896 /* Property is not set. Assume enforcing */
897 return true;
898 }
899
900 if (strcmp(tmp, "permissive") == 0) {
901 /* SELinux is in the kernel, but we've been told to go into permissive mode */
902 return false;
903 }
904
905 if (strcmp(tmp, "enforcing") != 0) {
906 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
907 }
908
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700909#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700910 return true;
911}
912
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400913int selinux_reload_policy(void)
914{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700915 if (selinux_is_disabled()) {
916 return -1;
917 }
918
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400919 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500920
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400921 if (selinux_android_reload_policy() == -1) {
922 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500923 }
924
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400925 if (sehandle)
926 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500927
rpcraig63207cd2012-08-09 10:05:49 -0400928 if (sehandle_prop)
929 selabel_close(sehandle_prop);
930
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400931 selinux_init_all_handles();
932 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500933}
rpcraig63207cd2012-08-09 10:05:49 -0400934
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500935static int audit_callback(void *data, security_class_t cls __attribute__((unused)), char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400936{
937 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
938 return 0;
939}
940
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500941static int log_callback(int type, const char *fmt, ...)
942{
943 int level;
944 va_list ap;
945 switch (type) {
946 case SELINUX_WARNING:
947 level = KLOG_WARNING_LEVEL;
948 break;
949 case SELINUX_INFO:
950 level = KLOG_INFO_LEVEL;
951 break;
952 default:
953 level = KLOG_ERROR_LEVEL;
954 break;
955 }
956 va_start(ap, fmt);
957 klog_vwrite(level, fmt, ap);
958 va_end(ap);
959 return 0;
960}
961
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700962static void selinux_initialize(void)
963{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700964 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700965 return;
966 }
967
968 INFO("loading selinux policy\n");
969 if (selinux_android_load_policy() < 0) {
970 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
971 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
972 while (1) { pause(); } // never reached
973 }
974
975 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700976 bool is_enforcing = selinux_is_enforcing();
977 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
978 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700979}
980
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700981int main(int argc, char **argv)
982{
Colin Crossebc6ff12010-04-13 19:52:01 -0700983 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700984 struct pollfd ufds[4];
985 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800986 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700987 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700988 int property_set_fd_init = 0;
989 int signal_fd_init = 0;
990 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800991 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700992
Colin Crossf83d0b92010-04-21 12:04:20 -0700993 if (!strcmp(basename(argv[0]), "ueventd"))
994 return ueventd_main(argc, argv);
995
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700996 if (!strcmp(basename(argv[0]), "watchdogd"))
997 return watchdogd_main(argc, argv);
998
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700999 /* clear the umask */
1000 umask(0);
1001
1002 /* Get the basic filesystem setup we need put
1003 * together in the initramdisk on / and then we'll
1004 * let the rc file figure out the rest.
1005 */
1006 mkdir("/dev", 0755);
1007 mkdir("/proc", 0755);
1008 mkdir("/sys", 0755);
1009
Nick Kralevich150f19e2010-06-22 16:35:43 -07001010 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001011 mkdir("/dev/pts", 0755);
1012 mkdir("/dev/socket", 0755);
1013 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1014 mount("proc", "/proc", "proc", 0, NULL);
1015 mount("sysfs", "/sys", "sysfs", 0, NULL);
1016
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001017 /* indicate that booting is in progress to background fw loaders, etc */
1018 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
1019
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001020 /* We must have some place other than / to create the
1021 * device nodes for kmsg and null, otherwise we won't
1022 * be able to remount / read-only later on.
1023 * Now that tmpfs is mounted on /dev, we can actually
1024 * talk to the outside world.
1025 */
1026 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001027 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001028 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001029
Colin Crossf83d0b92010-04-21 12:04:20 -07001030 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001031
Dima Zavin5511c842011-12-19 11:21:32 -08001032 process_kernel_cmdline();
1033
rpcraig63207cd2012-08-09 10:05:49 -04001034 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001035 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001036 selinux_set_callback(SELINUX_CB_LOG, cb);
1037
1038 cb.func_audit = audit_callback;
1039 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1040
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001041 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001042 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001043 * and therefore need their security context restored to the proper value.
1044 * This must happen before /dev is populated by ueventd.
1045 */
1046 restorecon("/dev");
1047 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001048 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001049 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001050
Dima Zavind7634c92011-12-16 14:18:06 -08001051 is_charger = !strcmp(bootmode, "charger");
1052
1053 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001054 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001055
1056 INFO("reading config file\n");
1057 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001058
1059 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001060
Colin Crossf83d0b92010-04-21 12:04:20 -07001061 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001062 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001063 queue_builtin_action(keychord_init_action, "keychord_init");
1064 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001065
Dima Zavinca47cef2011-08-24 15:28:23 -07001066 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001067 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001068
Alex Klyubin0d872d82013-08-16 13:19:24 -07001069 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1070 * wasn't ready immediately after wait_for_coldboot_done
1071 */
1072 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001073 queue_builtin_action(property_service_init_action, "property_service_init");
1074 queue_builtin_action(signal_init_action, "signal_init");
1075 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001076
Riley Andrewse4b7b292014-06-16 15:06:21 -07001077 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001078 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001079 action_for_each_trigger("charger", action_add_queue_tail);
1080 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001081 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001082 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001083
1084 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001085 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001086
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001087
1088#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -07001089 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001090#endif
1091
1092 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001093 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001094
Colin Crossebc6ff12010-04-13 19:52:01 -07001095 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001096 restart_processes();
1097
Colin Crossebc6ff12010-04-13 19:52:01 -07001098 if (!property_set_fd_init && get_property_set_fd() > 0) {
1099 ufds[fd_count].fd = get_property_set_fd();
1100 ufds[fd_count].events = POLLIN;
1101 ufds[fd_count].revents = 0;
1102 fd_count++;
1103 property_set_fd_init = 1;
1104 }
1105 if (!signal_fd_init && get_signal_fd() > 0) {
1106 ufds[fd_count].fd = get_signal_fd();
1107 ufds[fd_count].events = POLLIN;
1108 ufds[fd_count].revents = 0;
1109 fd_count++;
1110 signal_fd_init = 1;
1111 }
1112 if (!keychord_fd_init && get_keychord_fd() > 0) {
1113 ufds[fd_count].fd = get_keychord_fd();
1114 ufds[fd_count].events = POLLIN;
1115 ufds[fd_count].revents = 0;
1116 fd_count++;
1117 keychord_fd_init = 1;
1118 }
1119
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001120 if (process_needs_restart) {
1121 timeout = (process_needs_restart - gettime()) * 1000;
1122 if (timeout < 0)
1123 timeout = 0;
1124 }
1125
Colin Crossebd46132010-04-22 11:52:23 -07001126 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -07001127 timeout = 0;
1128
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001129#if BOOTCHART
1130 if (bootchart_count > 0) {
1131 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
1132 timeout = BOOTCHART_POLLING_MS;
1133 if (bootchart_step() < 0 || --bootchart_count == 0) {
1134 bootchart_finish();
1135 bootchart_count = 0;
1136 }
1137 }
1138#endif
Colin Crossebc6ff12010-04-13 19:52:01 -07001139
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001140 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001141 if (nr <= 0)
1142 continue;
1143
Colin Crossebc6ff12010-04-13 19:52:01 -07001144 for (i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001145 if (ufds[i].revents & POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001146 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001147 handle_property_set_fd();
1148 else if (ufds[i].fd == get_keychord_fd())
1149 handle_keychord();
1150 else if (ufds[i].fd == get_signal_fd())
1151 handle_signal();
1152 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001153 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001154 }
1155
1156 return 0;
1157}