blob: e4ac1cf2da8e59726bf4624a41c3778853a8b6c1 [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{
Riley Andrews24a3b782014-06-26 13:56:01 -0700531 int ret, i;
532 char cmd_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700533
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);
Riley Andrews24a3b782014-06-26 13:56:01 -0700549 if (klog_get_level() >= KLOG_INFO_LEVEL) {
550 for (i = 0; i < cur_command->nargs; i++) {
551 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
552 if (i < cur_command->nargs - 1) {
553 strlcat(cmd_str, " ", sizeof(cmd_str));
554 }
555 }
556 INFO("command '%s' action=%s status=%d (%s:%d)\n",
557 cmd_str, cur_action ? cur_action->name : "", ret, cur_command->filename,
558 cur_command->line);
559 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700560}
561
Colin Crossf83d0b92010-04-21 12:04:20 -0700562static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700563{
Colin Crossf83d0b92010-04-21 12:04:20 -0700564 int ret;
565 INFO("wait for %s\n", coldboot_done);
566 ret = wait_for_file(coldboot_done, COMMAND_RETRY_TIMEOUT);
567 if (ret)
568 ERROR("Timed out waiting for %s\n", coldboot_done);
569 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700570}
571
Alex Klyubin0d872d82013-08-16 13:19:24 -0700572/*
573 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
574 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
575 * Does nothing if Hardware RNG is not present.
576 *
577 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
578 * mixed into the primary pool of Linux RNG and the entropy estimate is left
579 * unmodified.
580 *
581 * If the HW RNG device /dev/hw_random is present, we require that at least
582 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
583 * devices/configurations where these I/O operations are blocking for a long
584 * time. We do not reboot or halt on failures, as this is a best-effort
585 * attempt.
586 */
587static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
588{
589 int result = -1;
590 int hwrandom_fd = -1;
591 int urandom_fd = -1;
592 char buf[512];
593 ssize_t chunk_size;
594 size_t total_bytes_written = 0;
595
596 hwrandom_fd = TEMP_FAILURE_RETRY(
597 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW));
598 if (hwrandom_fd == -1) {
599 if (errno == ENOENT) {
600 ERROR("/dev/hw_random not found\n");
601 /* It's not an error to not have a Hardware RNG. */
602 result = 0;
603 } else {
604 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
605 }
606 goto ret;
607 }
608
609 urandom_fd = TEMP_FAILURE_RETRY(
610 open("/dev/urandom", O_WRONLY | O_NOFOLLOW));
611 if (urandom_fd == -1) {
612 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
613 goto ret;
614 }
615
616 while (total_bytes_written < sizeof(buf)) {
617 chunk_size = TEMP_FAILURE_RETRY(
618 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
619 if (chunk_size == -1) {
620 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
621 goto ret;
622 } else if (chunk_size == 0) {
623 ERROR("Failed to read from /dev/hw_random: EOF\n");
624 goto ret;
625 }
626
627 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
628 if (chunk_size == -1) {
629 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
630 goto ret;
631 }
632 total_bytes_written += chunk_size;
633 }
634
Elliott Hughesccecf142014-01-16 10:53:11 -0800635 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700636 total_bytes_written);
637 result = 0;
638
639ret:
640 if (hwrandom_fd != -1) {
641 close(hwrandom_fd);
642 }
643 if (urandom_fd != -1) {
644 close(urandom_fd);
645 }
646 memset(buf, 0, sizeof(buf));
647 return result;
648}
649
Colin Crossebc6ff12010-04-13 19:52:01 -0700650static int keychord_init_action(int nargs, char **args)
651{
652 keychord_init();
653 return 0;
654}
655
656static int console_init_action(int nargs, char **args)
657{
658 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700659
660 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800661 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700662 }
663
664 fd = open(console_name, O_RDWR);
665 if (fd >= 0)
666 have_console = 1;
667 close(fd);
668
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200669 fd = open("/dev/tty0", O_WRONLY);
670 if (fd >= 0) {
671 const char *msg;
672 msg = "\n"
673 "\n"
674 "\n"
675 "\n"
676 "\n"
677 "\n"
678 "\n" // console is 40 cols x 30 lines
679 "\n"
680 "\n"
681 "\n"
682 "\n"
683 "\n"
684 "\n"
685 "\n"
686 " A N D R O I D ";
687 write(fd, msg, strlen(msg));
688 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700689 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200690
Colin Crossebc6ff12010-04-13 19:52:01 -0700691 return 0;
692}
693
Dima Zavin5511c842011-12-19 11:21:32 -0800694static void import_kernel_nv(char *name, int for_emulator)
695{
696 char *value = strchr(name, '=');
697 int name_len = strlen(name);
698
699 if (value == 0) return;
700 *value++ = 0;
701 if (name_len == 0) return;
702
703 if (for_emulator) {
704 /* in the emulator, export any kernel option with the
705 * ro.kernel. prefix */
706 char buff[PROP_NAME_MAX];
707 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
708
709 if (len < (int)sizeof(buff))
710 property_set( buff, value );
711 return;
712 }
713
714 if (!strcmp(name,"qemu")) {
715 strlcpy(qemu, value, sizeof(qemu));
716 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
717 const char *boot_prop_name = name + 12;
718 char prop[PROP_NAME_MAX];
719 int cnt;
720
721 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
722 if (cnt < PROP_NAME_MAX)
723 property_set(prop, value);
724 }
725}
726
727static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700728{
729 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800730 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800731 unsigned i;
732 struct {
733 const char *src_prop;
734 const char *dest_prop;
735 const char *def_val;
736 } prop_map[] = {
737 { "ro.boot.serialno", "ro.serialno", "", },
738 { "ro.boot.mode", "ro.bootmode", "unknown", },
739 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800740 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
741 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700742
Dima Zavin5511c842011-12-19 11:21:32 -0800743 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800744 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700745 if (ret > 0)
746 property_set(prop_map[i].dest_prop, tmp);
747 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800748 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800749 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700750
Colin Cross1a6f4c32013-01-28 17:13:35 -0800751 ret = property_get("ro.boot.console", tmp);
752 if (ret)
753 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800754
755 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800756 property_get("ro.bootmode", tmp);
757 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800758
759 /* if this was given on kernel command line, override what we read
760 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800761 ret = property_get("ro.boot.hardware", tmp);
762 if (ret)
763 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800764 property_set("ro.hardware", hardware);
765
766 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
767 property_set("ro.revision", tmp);
768
769 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700770 if (!strcmp(bootmode,"factory"))
771 property_set("ro.factorytest", "1");
772 else if (!strcmp(bootmode,"factory2"))
773 property_set("ro.factorytest", "2");
774 else
775 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800776}
Colin Crossebc6ff12010-04-13 19:52:01 -0700777
Dima Zavin5511c842011-12-19 11:21:32 -0800778static void process_kernel_cmdline(void)
779{
780 /* don't expose the raw commandline to nonpriv processes */
781 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700782
Dima Zavin5511c842011-12-19 11:21:32 -0800783 /* first pass does the common stuff, and finds if we are in qemu.
784 * second pass is only necessary for qemu to export all kernel params
785 * as props.
786 */
787 import_kernel_cmdline(0, import_kernel_nv);
788 if (qemu[0])
789 import_kernel_cmdline(1, import_kernel_nv);
790
791 /* now propogate the info given on command line to internal variables
792 * used by init as well as the current required properties
793 */
794 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700795}
796
797static int property_service_init_action(int nargs, char **args)
798{
799 /* read any property files on system or data and
800 * fire up the property service. This must happen
801 * after the ro.foo properties are set above so
802 * that /data/local.prop cannot interfere with them.
803 */
804 start_property_service();
805 return 0;
806}
807
808static int signal_init_action(int nargs, char **args)
809{
810 signal_init();
811 return 0;
812}
813
814static int check_startup_action(int nargs, char **args)
815{
816 /* make sure we actually have all the pieces we need */
Colin Crossf83d0b92010-04-21 12:04:20 -0700817 if ((get_property_set_fd() < 0) ||
Colin Crossebc6ff12010-04-13 19:52:01 -0700818 (get_signal_fd() < 0)) {
819 ERROR("init startup failure\n");
820 exit(1);
821 }
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700822
823 /* signal that we hit this point */
824 unlink("/dev/.booting");
825
Colin Crossebc6ff12010-04-13 19:52:01 -0700826 return 0;
827}
828
829static int queue_property_triggers_action(int nargs, char **args)
830{
831 queue_all_property_triggers();
832 /* enable property triggers */
833 property_triggers_enabled = 1;
834 return 0;
835}
836
837#if BOOTCHART
838static int bootchart_init_action(int nargs, char **args)
839{
840 bootchart_count = bootchart_init();
841 if (bootchart_count < 0) {
842 ERROR("bootcharting init failure\n");
843 } else if (bootchart_count > 0) {
844 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
845 } else {
846 NOTICE("bootcharting ignored\n");
847 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100848
849 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700850}
851#endif
852
rpcraig63207cd2012-08-09 10:05:49 -0400853static const struct selinux_opt seopts_prop[] = {
rpcraig63207cd2012-08-09 10:05:49 -0400854 { SELABEL_OPT_PATH, "/property_contexts" },
Robert Craig03daf052014-03-17 21:16:53 -0400855 { SELABEL_OPT_PATH, "/data/security/current/property_contexts" },
rpcraig63207cd2012-08-09 10:05:49 -0400856 { 0, NULL }
857};
858
859struct selabel_handle* selinux_android_prop_context_handle(void)
860{
Robert Craig03daf052014-03-17 21:16:53 -0400861 int policy_index = selinux_android_use_data_policy() ? 1 : 0;
862 struct selabel_handle* sehandle = selabel_open(SELABEL_CTX_ANDROID_PROP,
863 &seopts_prop[policy_index], 1);
rpcraig63207cd2012-08-09 10:05:49 -0400864 if (!sehandle) {
865 ERROR("SELinux: Could not load property_contexts: %s\n",
866 strerror(errno));
867 return NULL;
868 }
Robert Craig03daf052014-03-17 21:16:53 -0400869 INFO("SELinux: Loaded property contexts from %s\n", seopts_prop[policy_index].value);
rpcraig63207cd2012-08-09 10:05:49 -0400870 return sehandle;
871}
872
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400873void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500874{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400875 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500876 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400877 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400878}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500879
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700880static bool selinux_is_disabled(void)
881{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700882#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700883 char tmp[PROP_VALUE_MAX];
884
885 if (access("/sys/fs/selinux", F_OK) != 0) {
886 /* SELinux is not compiled into the kernel, or has been disabled
887 * via the kernel command line "selinux=0".
888 */
889 return true;
890 }
891
892 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
893 /* SELinux is compiled into the kernel, but we've been told to disable it. */
894 return true;
895 }
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700896#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700897
898 return false;
899}
900
901static bool selinux_is_enforcing(void)
902{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700903#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700904 char tmp[PROP_VALUE_MAX];
905
906 if (property_get("ro.boot.selinux", tmp) == 0) {
907 /* Property is not set. Assume enforcing */
908 return true;
909 }
910
911 if (strcmp(tmp, "permissive") == 0) {
912 /* SELinux is in the kernel, but we've been told to go into permissive mode */
913 return false;
914 }
915
916 if (strcmp(tmp, "enforcing") != 0) {
917 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
918 }
919
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700920#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700921 return true;
922}
923
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400924int selinux_reload_policy(void)
925{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700926 if (selinux_is_disabled()) {
927 return -1;
928 }
929
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400930 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500931
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400932 if (selinux_android_reload_policy() == -1) {
933 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500934 }
935
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400936 if (sehandle)
937 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500938
rpcraig63207cd2012-08-09 10:05:49 -0400939 if (sehandle_prop)
940 selabel_close(sehandle_prop);
941
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400942 selinux_init_all_handles();
943 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500944}
rpcraig63207cd2012-08-09 10:05:49 -0400945
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500946static int audit_callback(void *data, security_class_t cls __attribute__((unused)), char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400947{
948 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
949 return 0;
950}
951
Stephen Smalley439224e2014-06-24 13:45:43 -0400952int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500953{
954 int level;
955 va_list ap;
956 switch (type) {
957 case SELINUX_WARNING:
958 level = KLOG_WARNING_LEVEL;
959 break;
960 case SELINUX_INFO:
961 level = KLOG_INFO_LEVEL;
962 break;
963 default:
964 level = KLOG_ERROR_LEVEL;
965 break;
966 }
967 va_start(ap, fmt);
968 klog_vwrite(level, fmt, ap);
969 va_end(ap);
970 return 0;
971}
972
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700973static void selinux_initialize(void)
974{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700975 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700976 return;
977 }
978
979 INFO("loading selinux policy\n");
980 if (selinux_android_load_policy() < 0) {
981 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
982 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
983 while (1) { pause(); } // never reached
984 }
985
986 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700987 bool is_enforcing = selinux_is_enforcing();
988 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
989 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700990}
991
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700992int main(int argc, char **argv)
993{
Colin Crossebc6ff12010-04-13 19:52:01 -0700994 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700995 struct pollfd ufds[4];
996 char *tmpdev;
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800997 char* debuggable;
Colin Crossebc6ff12010-04-13 19:52:01 -0700998 char tmp[32];
Colin Crossebc6ff12010-04-13 19:52:01 -0700999 int property_set_fd_init = 0;
1000 int signal_fd_init = 0;
1001 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -08001002 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001003
Colin Crossf83d0b92010-04-21 12:04:20 -07001004 if (!strcmp(basename(argv[0]), "ueventd"))
1005 return ueventd_main(argc, argv);
1006
Arve Hjønnevågd97d9072012-06-13 21:51:56 -07001007 if (!strcmp(basename(argv[0]), "watchdogd"))
1008 return watchdogd_main(argc, argv);
1009
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001010 /* clear the umask */
1011 umask(0);
1012
1013 /* Get the basic filesystem setup we need put
1014 * together in the initramdisk on / and then we'll
1015 * let the rc file figure out the rest.
1016 */
1017 mkdir("/dev", 0755);
1018 mkdir("/proc", 0755);
1019 mkdir("/sys", 0755);
1020
Nick Kralevich150f19e2010-06-22 16:35:43 -07001021 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001022 mkdir("/dev/pts", 0755);
1023 mkdir("/dev/socket", 0755);
1024 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1025 mount("proc", "/proc", "proc", 0, NULL);
1026 mount("sysfs", "/sys", "sysfs", 0, NULL);
1027
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001028 /* indicate that booting is in progress to background fw loaders, etc */
1029 close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000));
1030
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001031 /* We must have some place other than / to create the
1032 * device nodes for kmsg and null, otherwise we won't
1033 * be able to remount / read-only later on.
1034 * Now that tmpfs is mounted on /dev, we can actually
1035 * talk to the outside world.
1036 */
1037 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001038 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001039 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001040
Colin Crossf83d0b92010-04-21 12:04:20 -07001041 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001042
Dima Zavin5511c842011-12-19 11:21:32 -08001043 process_kernel_cmdline();
1044
rpcraig63207cd2012-08-09 10:05:49 -04001045 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001046 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001047 selinux_set_callback(SELINUX_CB_LOG, cb);
1048
1049 cb.func_audit = audit_callback;
1050 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1051
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001052 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001053 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001054 * and therefore need their security context restored to the proper value.
1055 * This must happen before /dev is populated by ueventd.
1056 */
1057 restorecon("/dev");
1058 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001059 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001060 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001061
Dima Zavind7634c92011-12-16 14:18:06 -08001062 is_charger = !strcmp(bootmode, "charger");
1063
1064 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001065 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001066
1067 INFO("reading config file\n");
1068 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001069
1070 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001071
Colin Crossf83d0b92010-04-21 12:04:20 -07001072 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001073 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001074 queue_builtin_action(keychord_init_action, "keychord_init");
1075 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001076
Dima Zavinca47cef2011-08-24 15:28:23 -07001077 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001078 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001079
Alex Klyubin0d872d82013-08-16 13:19:24 -07001080 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1081 * wasn't ready immediately after wait_for_coldboot_done
1082 */
1083 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001084 queue_builtin_action(property_service_init_action, "property_service_init");
1085 queue_builtin_action(signal_init_action, "signal_init");
1086 queue_builtin_action(check_startup_action, "check_startup");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001087
Riley Andrewse4b7b292014-06-16 15:06:21 -07001088 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001089 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001090 action_for_each_trigger("charger", action_add_queue_tail);
1091 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001092 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001093 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001094
1095 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001096 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001097
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001098
1099#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -07001100 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001101#endif
1102
1103 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001104 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001105
Colin Crossebc6ff12010-04-13 19:52:01 -07001106 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001107 restart_processes();
1108
Colin Crossebc6ff12010-04-13 19:52:01 -07001109 if (!property_set_fd_init && get_property_set_fd() > 0) {
1110 ufds[fd_count].fd = get_property_set_fd();
1111 ufds[fd_count].events = POLLIN;
1112 ufds[fd_count].revents = 0;
1113 fd_count++;
1114 property_set_fd_init = 1;
1115 }
1116 if (!signal_fd_init && get_signal_fd() > 0) {
1117 ufds[fd_count].fd = get_signal_fd();
1118 ufds[fd_count].events = POLLIN;
1119 ufds[fd_count].revents = 0;
1120 fd_count++;
1121 signal_fd_init = 1;
1122 }
1123 if (!keychord_fd_init && get_keychord_fd() > 0) {
1124 ufds[fd_count].fd = get_keychord_fd();
1125 ufds[fd_count].events = POLLIN;
1126 ufds[fd_count].revents = 0;
1127 fd_count++;
1128 keychord_fd_init = 1;
1129 }
1130
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001131 if (process_needs_restart) {
1132 timeout = (process_needs_restart - gettime()) * 1000;
1133 if (timeout < 0)
1134 timeout = 0;
1135 }
1136
Colin Crossebd46132010-04-22 11:52:23 -07001137 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -07001138 timeout = 0;
1139
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001140#if BOOTCHART
1141 if (bootchart_count > 0) {
1142 if (timeout < 0 || timeout > BOOTCHART_POLLING_MS)
1143 timeout = BOOTCHART_POLLING_MS;
1144 if (bootchart_step() < 0 || --bootchart_count == 0) {
1145 bootchart_finish();
1146 bootchart_count = 0;
1147 }
1148 }
1149#endif
Colin Crossebc6ff12010-04-13 19:52:01 -07001150
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001151 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001152 if (nr <= 0)
1153 continue;
1154
Colin Crossebc6ff12010-04-13 19:52:01 -07001155 for (i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001156 if (ufds[i].revents & POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001157 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001158 handle_property_set_fd();
1159 else if (ufds[i].fd == get_keychord_fd())
1160 handle_keychord();
1161 else if (ufds[i].fd == get_signal_fd())
1162 handle_signal();
1163 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001164 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001165 }
1166
1167 return 0;
1168}