blob: fef8a2e46643458514454ab67198f90a9fd2bba8 [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;
Bo (Andover) Zhang37003732014-07-24 13:11:35 -040068static long long bootchart_time = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069#endif
70
71static char console[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072static char bootmode[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073static char hardware[32];
74static unsigned revision = 0;
75static char qemu[32];
76
Colin Crossebc6ff12010-04-13 19:52:01 -070077static struct action *cur_action = NULL;
78static struct command *cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -070079
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{
James Morrissey381341f2014-05-16 11:36:36 +010099 size_t n;
100 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700101
James Morrissey381341f2014-05-16 11:36:36 +0100102 /* The last environment entry is reserved to terminate the list */
103 for (n = 0; n < (ARRAY_SIZE(ENV) - 1); n++) {
104
105 /* Delete any existing entry for this key */
106 if (ENV[n] != NULL) {
107 size_t entry_key_len = strcspn(ENV[n], "=");
108 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
109 free((char*)ENV[n]);
110 ENV[n] = NULL;
111 }
112 }
113
114 /* Add entry if a free slot is available */
115 if (ENV[n] == NULL) {
116 size_t len = key_len + strlen(val) + 2;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700117 char *entry = malloc(len);
118 snprintf(entry, len, "%s=%s", key, val);
119 ENV[n] = entry;
120 return 0;
121 }
122 }
123
James Morrissey381341f2014-05-16 11:36:36 +0100124 ERROR("No env. room to store: '%s':'%s'\n", key, val);
125
126 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127}
128
129static void zap_stdio(void)
130{
131 int fd;
132 fd = open("/dev/null", O_RDWR);
133 dup2(fd, 0);
134 dup2(fd, 1);
135 dup2(fd, 2);
136 close(fd);
137}
138
139static void open_console()
140{
141 int fd;
142 if ((fd = open(console_name, O_RDWR)) < 0) {
143 fd = open("/dev/null", O_RDWR);
144 }
Colin Cross50fb5a62012-03-18 15:38:19 -0700145 ioctl(fd, TIOCSCTTY, 0);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700146 dup2(fd, 0);
147 dup2(fd, 1);
148 dup2(fd, 2);
149 close(fd);
150}
151
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152static void publish_socket(const char *name, int fd)
153{
154 char key[64] = ANDROID_SOCKET_ENV_PREFIX;
155 char val[64];
156
157 strlcpy(key + sizeof(ANDROID_SOCKET_ENV_PREFIX) - 1,
158 name,
159 sizeof(key) - sizeof(ANDROID_SOCKET_ENV_PREFIX));
160 snprintf(val, sizeof(val), "%d", fd);
161 add_environment(key, val);
162
163 /* make sure we don't close-on-exec */
164 fcntl(fd, F_SETFD, 0);
165}
166
San Mehatf24e2522009-05-19 13:30:46 -0700167void service_start(struct service *svc, const char *dynamic_args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700168{
169 struct stat s;
170 pid_t pid;
171 int needs_console;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500172 char *scon = NULL;
173 int rc;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700174
Ken Sumrall752923c2010-12-03 16:33:31 -0800175 /* starting a service removes it from the disabled or reset
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700176 * state and immediately takes it out of the restarting
177 * state if it was in there
178 */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700179 svc->flags &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700180 svc->time_started = 0;
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700181
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182 /* running processes require no additional work -- if
183 * they're in the process of exiting, we've ensured
184 * that they will immediately restart on exit, unless
185 * they are ONESHOT
186 */
187 if (svc->flags & SVC_RUNNING) {
188 return;
189 }
190
191 needs_console = (svc->flags & SVC_CONSOLE) ? 1 : 0;
192 if (needs_console && (!have_console)) {
193 ERROR("service '%s' requires console\n", svc->name);
194 svc->flags |= SVC_DISABLED;
195 return;
196 }
197
198 if (stat(svc->args[0], &s) != 0) {
199 ERROR("cannot find '%s', disabling '%s'\n", svc->args[0], svc->name);
200 svc->flags |= SVC_DISABLED;
201 return;
202 }
203
San Mehatf24e2522009-05-19 13:30:46 -0700204 if ((!(svc->flags & SVC_ONESHOT)) && dynamic_args) {
San Mehatd4cdd132009-05-20 09:52:16 -0700205 ERROR("service '%s' must be one-shot to use dynamic args, disabling\n",
206 svc->args[0]);
San Mehatf24e2522009-05-19 13:30:46 -0700207 svc->flags |= SVC_DISABLED;
208 return;
209 }
210
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500211 if (is_selinux_enabled() > 0) {
Stephen Smalley30f30332012-11-16 14:34:27 -0500212 if (svc->seclabel) {
213 scon = strdup(svc->seclabel);
214 if (!scon) {
215 ERROR("Out of memory while starting '%s'\n", svc->name);
216 return;
217 }
218 } else {
219 char *mycon = NULL, *fcon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500220
Stephen Smalley30f30332012-11-16 14:34:27 -0500221 INFO("computing context for service '%s'\n", svc->args[0]);
222 rc = getcon(&mycon);
223 if (rc < 0) {
224 ERROR("could not get context while starting '%s'\n", svc->name);
225 return;
226 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500227
Stephen Smalley30f30332012-11-16 14:34:27 -0500228 rc = getfilecon(svc->args[0], &fcon);
229 if (rc < 0) {
230 ERROR("could not get context while starting '%s'\n", svc->name);
231 freecon(mycon);
232 return;
233 }
234
235 rc = security_compute_create(mycon, fcon, string_to_security_class("process"), &scon);
Stephen Smalleyaf06c672013-12-09 15:40:24 -0500236 if (rc == 0 && !strcmp(scon, mycon)) {
237 ERROR("Warning! Service %s needs a SELinux domain defined; please fix!\n", svc->name);
238 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500239 freecon(mycon);
Stephen Smalley30f30332012-11-16 14:34:27 -0500240 freecon(fcon);
241 if (rc < 0) {
242 ERROR("could not get context while starting '%s'\n", svc->name);
243 return;
244 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500245 }
246 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500247
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700248 NOTICE("starting '%s'\n", svc->name);
249
250 pid = fork();
251
252 if (pid == 0) {
253 struct socketinfo *si;
254 struct svcenvinfo *ei;
255 char tmp[32];
256 int fd, sz;
257
Nick Kralevich6ebf12f2012-03-26 09:09:11 -0700258 umask(077);
Colin Cross3294bbb2010-04-19 17:11:33 -0700259 if (properties_inited()) {
260 get_property_workspace(&fd, &sz);
261 sprintf(tmp, "%d,%d", dup(fd), sz);
262 add_environment("ANDROID_PROPERTY_WORKSPACE", tmp);
263 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700264
265 for (ei = svc->envvars; ei; ei = ei->next)
266 add_environment(ei->name, ei->value);
267
268 for (si = svc->sockets; si; si = si->next) {
Mike Lockwood912ff852010-10-01 08:20:36 -0400269 int socket_type = (
270 !strcmp(si->type, "stream") ? SOCK_STREAM :
271 (!strcmp(si->type, "dgram") ? SOCK_DGRAM : SOCK_SEQPACKET));
272 int s = create_socket(si->name, socket_type,
Stephen Smalley8348d272013-05-13 12:37:04 -0400273 si->perm, si->uid, si->gid, si->socketcon ?: scon);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274 if (s >= 0) {
275 publish_socket(si->name, s);
276 }
277 }
278
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500279 freecon(scon);
280 scon = NULL;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500281
San Mehat4e221f02010-02-25 14:19:50 -0800282 if (svc->ioprio_class != IoSchedClass_NONE) {
283 if (android_set_ioprio(getpid(), svc->ioprio_class, svc->ioprio_pri)) {
284 ERROR("Failed to set pid %d ioprio = %d,%d: %s\n",
285 getpid(), svc->ioprio_class, svc->ioprio_pri, strerror(errno));
286 }
287 }
288
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289 if (needs_console) {
290 setsid();
291 open_console();
292 } else {
293 zap_stdio();
294 }
295
296#if 0
297 for (n = 0; svc->args[n]; n++) {
298 INFO("args[%d] = '%s'\n", n, svc->args[n]);
299 }
300 for (n = 0; ENV[n]; n++) {
301 INFO("env[%d] = '%s'\n", n, ENV[n]);
302 }
303#endif
304
305 setpgid(0, getpid());
306
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800307 /* as requested, set our gid, supplemental gids, and uid */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 if (svc->gid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800309 if (setgid(svc->gid) != 0) {
310 ERROR("setgid failed: %s\n", strerror(errno));
311 _exit(127);
312 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700313 }
314 if (svc->nr_supp_gids) {
Nick Kralevich22687182010-11-17 16:55:42 -0800315 if (setgroups(svc->nr_supp_gids, svc->supp_gids) != 0) {
316 ERROR("setgroups failed: %s\n", strerror(errno));
317 _exit(127);
318 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700319 }
320 if (svc->uid) {
Nick Kralevich22687182010-11-17 16:55:42 -0800321 if (setuid(svc->uid) != 0) {
322 ERROR("setuid failed: %s\n", strerror(errno));
323 _exit(127);
324 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500326 if (svc->seclabel) {
327 if (is_selinux_enabled() > 0 && setexeccon(svc->seclabel) < 0) {
328 ERROR("cannot setexeccon('%s'): %s\n", svc->seclabel, strerror(errno));
329 _exit(127);
330 }
331 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500332
San Mehat8ad15682009-05-20 08:50:40 -0700333 if (!dynamic_args) {
334 if (execve(svc->args[0], (char**) svc->args, (char**) ENV) < 0) {
335 ERROR("cannot execve('%s'): %s\n", svc->args[0], strerror(errno));
336 }
337 } else {
Colin Cross6310a822010-04-20 14:29:05 -0700338 char *arg_ptrs[INIT_PARSER_MAXARGS+1];
San Mehatd4cdd132009-05-20 09:52:16 -0700339 int arg_idx = svc->nargs;
San Mehatf24e2522009-05-19 13:30:46 -0700340 char *tmp = strdup(dynamic_args);
San Mehatd4cdd132009-05-20 09:52:16 -0700341 char *next = tmp;
342 char *bword;
San Mehatf24e2522009-05-19 13:30:46 -0700343
344 /* Copy the static arguments */
San Mehatd4cdd132009-05-20 09:52:16 -0700345 memcpy(arg_ptrs, svc->args, (svc->nargs * sizeof(char *)));
San Mehatf24e2522009-05-19 13:30:46 -0700346
San Mehatd4cdd132009-05-20 09:52:16 -0700347 while((bword = strsep(&next, " "))) {
348 arg_ptrs[arg_idx++] = bword;
Colin Cross6310a822010-04-20 14:29:05 -0700349 if (arg_idx == INIT_PARSER_MAXARGS)
San Mehatf24e2522009-05-19 13:30:46 -0700350 break;
San Mehatf24e2522009-05-19 13:30:46 -0700351 }
352 arg_ptrs[arg_idx] = '\0';
353 execve(svc->args[0], (char**) arg_ptrs, (char**) ENV);
Ivan Djelic165de922008-11-23 22:26:39 +0100354 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700355 _exit(127);
356 }
357
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500358 freecon(scon);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500359
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360 if (pid < 0) {
361 ERROR("failed to start '%s'\n", svc->name);
362 svc->pid = 0;
363 return;
364 }
365
366 svc->time_started = gettime();
367 svc->pid = pid;
368 svc->flags |= SVC_RUNNING;
369
Colin Cross3294bbb2010-04-19 17:11:33 -0700370 if (properties_inited())
371 notify_service_state(svc->name, "running");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372}
373
Mike Kasickb54f39f2012-01-25 23:48:46 -0500374/* The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART */
Ken Sumrall752923c2010-12-03 16:33:31 -0800375static void service_stop_or_reset(struct service *svc, int how)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376{
Mike Kasick7e36edd2012-02-06 10:32:13 -0500377 /* The service is still SVC_RUNNING until its process exits, but if it has
378 * already exited it shoudn't attempt a restart yet. */
JP Abgrall3beec7e2014-05-02 21:14:29 -0700379 svc->flags &= ~(SVC_RESTARTING | SVC_DISABLED_START);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380
Mike Kasickb54f39f2012-01-25 23:48:46 -0500381 if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800382 /* Hrm, an illegal flag. Default to SVC_DISABLED */
383 how = SVC_DISABLED;
384 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 /* if the service has not yet started, prevent
386 * it from auto-starting with its class
387 */
Ken Sumralla2864802011-10-26 16:56:00 -0700388 if (how == SVC_RESET) {
389 svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
390 } else {
391 svc->flags |= how;
392 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700393
394 if (svc->pid) {
395 NOTICE("service '%s' is being killed\n", svc->name);
Ken Sumrall752923c2010-12-03 16:33:31 -0800396 kill(-svc->pid, SIGKILL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397 notify_service_state(svc->name, "stopping");
398 } else {
399 notify_service_state(svc->name, "stopped");
400 }
401}
402
Ken Sumrall752923c2010-12-03 16:33:31 -0800403void service_reset(struct service *svc)
404{
405 service_stop_or_reset(svc, SVC_RESET);
406}
407
408void service_stop(struct service *svc)
409{
410 service_stop_or_reset(svc, SVC_DISABLED);
411}
412
Mike Kasickb54f39f2012-01-25 23:48:46 -0500413void service_restart(struct service *svc)
414{
415 if (svc->flags & SVC_RUNNING) {
416 /* Stop, wait, then start the service. */
417 service_stop_or_reset(svc, SVC_RESTART);
418 } else if (!(svc->flags & SVC_RESTARTING)) {
419 /* Just start the service since it's not running. */
420 service_start(svc, NULL);
421 } /* else: Service is restarting anyways. */
422}
423
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700424void property_changed(const char *name, const char *value)
425{
Colin Crossebc6ff12010-04-13 19:52:01 -0700426 if (property_triggers_enabled)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427 queue_property_triggers(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428}
429
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430static void restart_service_if_needed(struct service *svc)
431{
432 time_t next_start_time = svc->time_started + 5;
433
434 if (next_start_time <= gettime()) {
435 svc->flags &= (~SVC_RESTARTING);
San Mehatf24e2522009-05-19 13:30:46 -0700436 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700437 return;
438 }
439
440 if ((next_start_time < process_needs_restart) ||
441 (process_needs_restart == 0)) {
442 process_needs_restart = next_start_time;
443 }
444}
445
446static void restart_processes()
447{
448 process_needs_restart = 0;
449 service_for_each_flags(SVC_RESTARTING,
450 restart_service_if_needed);
451}
452
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700453static void msg_start(const char *name)
454{
Hong-Mei Li11467182013-04-01 11:17:51 +0800455 struct service *svc = NULL;
San Mehatf24e2522009-05-19 13:30:46 -0700456 char *tmp = NULL;
457 char *args = NULL;
458
459 if (!strchr(name, ':'))
460 svc = service_find_by_name(name);
461 else {
462 tmp = strdup(name);
Hong-Mei Li11467182013-04-01 11:17:51 +0800463 if (tmp) {
464 args = strchr(tmp, ':');
465 *args = '\0';
466 args++;
San Mehatf24e2522009-05-19 13:30:46 -0700467
Hong-Mei Li11467182013-04-01 11:17:51 +0800468 svc = service_find_by_name(tmp);
469 }
San Mehatf24e2522009-05-19 13:30:46 -0700470 }
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700471
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700472 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700473 service_start(svc, args);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700474 } else {
475 ERROR("no such service '%s'\n", name);
476 }
San Mehatf24e2522009-05-19 13:30:46 -0700477 if (tmp)
478 free(tmp);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700479}
480
481static void msg_stop(const char *name)
482{
483 struct service *svc = service_find_by_name(name);
484
485 if (svc) {
486 service_stop(svc);
487 } else {
Dima Zavin770354d2009-05-05 18:33:07 -0700488 ERROR("no such service '%s'\n", name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489 }
490}
491
Mike Kasickb54f39f2012-01-25 23:48:46 -0500492static void msg_restart(const char *name)
493{
494 struct service *svc = service_find_by_name(name);
495
496 if (svc) {
497 service_restart(svc);
498 } else {
499 ERROR("no such service '%s'\n", name);
500 }
501}
502
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700503void handle_control_message(const char *msg, const char *arg)
504{
505 if (!strcmp(msg,"start")) {
506 msg_start(arg);
507 } else if (!strcmp(msg,"stop")) {
508 msg_stop(arg);
Wink Savillecfa0d842010-10-03 13:30:11 -0700509 } else if (!strcmp(msg,"restart")) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500510 msg_restart(arg);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700511 } else {
512 ERROR("unknown control msg '%s'\n", msg);
513 }
514}
515
Colin Crossebc6ff12010-04-13 19:52:01 -0700516static struct command *get_first_command(struct action *act)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700517{
518 struct listnode *node;
Colin Crossebc6ff12010-04-13 19:52:01 -0700519 node = list_head(&act->commands);
Dima Zavin3bea0792011-08-26 13:59:18 -0700520 if (!node || list_empty(&act->commands))
Colin Crossebc6ff12010-04-13 19:52:01 -0700521 return NULL;
522
523 return node_to_item(node, struct command, clist);
524}
525
526static struct command *get_next_command(struct action *act, struct command *cmd)
527{
528 struct listnode *node;
529 node = cmd->clist.next;
530 if (!node)
531 return NULL;
532 if (node == &act->commands)
533 return NULL;
534
535 return node_to_item(node, struct command, clist);
536}
537
538static int is_last_command(struct action *act, struct command *cmd)
539{
540 return (list_tail(&act->commands) == &cmd->clist);
541}
542
543void execute_one_command(void)
544{
Riley Andrews24a3b782014-06-26 13:56:01 -0700545 int ret, i;
546 char cmd_str[256] = "";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700547
Colin Crossebc6ff12010-04-13 19:52:01 -0700548 if (!cur_action || !cur_command || is_last_command(cur_action, cur_command)) {
549 cur_action = action_remove_queue_head();
Colin Crossebd46132010-04-22 11:52:23 -0700550 cur_command = NULL;
Colin Crossebc6ff12010-04-13 19:52:01 -0700551 if (!cur_action)
552 return;
553 INFO("processing action %p (%s)\n", cur_action, cur_action->name);
554 cur_command = get_first_command(cur_action);
555 } else {
556 cur_command = get_next_command(cur_action, cur_command);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700557 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700558
559 if (!cur_command)
560 return;
561
562 ret = cur_command->func(cur_command->nargs, cur_command->args);
Riley Andrews24a3b782014-06-26 13:56:01 -0700563 if (klog_get_level() >= KLOG_INFO_LEVEL) {
564 for (i = 0; i < cur_command->nargs; i++) {
565 strlcat(cmd_str, cur_command->args[i], sizeof(cmd_str));
566 if (i < cur_command->nargs - 1) {
567 strlcat(cmd_str, " ", sizeof(cmd_str));
568 }
569 }
570 INFO("command '%s' action=%s status=%d (%s:%d)\n",
571 cmd_str, cur_action ? cur_action->name : "", ret, cur_command->filename,
572 cur_command->line);
573 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700574}
575
Colin Crossf83d0b92010-04-21 12:04:20 -0700576static int wait_for_coldboot_done_action(int nargs, char **args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700577{
Colin Crossf83d0b92010-04-21 12:04:20 -0700578 int ret;
Andreas Gampea016c422014-11-24 19:52:41 -0800579 INFO("wait for %s\n", COLDBOOT_DONE);
580 ret = wait_for_file(COLDBOOT_DONE, COMMAND_RETRY_TIMEOUT);
Colin Crossf83d0b92010-04-21 12:04:20 -0700581 if (ret)
Andreas Gampea016c422014-11-24 19:52:41 -0800582 ERROR("Timed out waiting for %s\n", COLDBOOT_DONE);
Colin Crossf83d0b92010-04-21 12:04:20 -0700583 return ret;
Colin Crossebc6ff12010-04-13 19:52:01 -0700584}
585
Alex Klyubin0d872d82013-08-16 13:19:24 -0700586/*
587 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
588 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
589 * Does nothing if Hardware RNG is not present.
590 *
591 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
592 * mixed into the primary pool of Linux RNG and the entropy estimate is left
593 * unmodified.
594 *
595 * If the HW RNG device /dev/hw_random is present, we require that at least
596 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
597 * devices/configurations where these I/O operations are blocking for a long
598 * time. We do not reboot or halt on failures, as this is a best-effort
599 * attempt.
600 */
601static int mix_hwrng_into_linux_rng_action(int nargs, char **args)
602{
603 int result = -1;
604 int hwrandom_fd = -1;
605 int urandom_fd = -1;
606 char buf[512];
607 ssize_t chunk_size;
608 size_t total_bytes_written = 0;
609
610 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800611 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700612 if (hwrandom_fd == -1) {
613 if (errno == ENOENT) {
614 ERROR("/dev/hw_random not found\n");
615 /* It's not an error to not have a Hardware RNG. */
616 result = 0;
617 } else {
618 ERROR("Failed to open /dev/hw_random: %s\n", strerror(errno));
619 }
620 goto ret;
621 }
622
623 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800624 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700625 if (urandom_fd == -1) {
626 ERROR("Failed to open /dev/urandom: %s\n", strerror(errno));
627 goto ret;
628 }
629
630 while (total_bytes_written < sizeof(buf)) {
631 chunk_size = TEMP_FAILURE_RETRY(
632 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
633 if (chunk_size == -1) {
634 ERROR("Failed to read from /dev/hw_random: %s\n", strerror(errno));
635 goto ret;
636 } else if (chunk_size == 0) {
637 ERROR("Failed to read from /dev/hw_random: EOF\n");
638 goto ret;
639 }
640
641 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
642 if (chunk_size == -1) {
643 ERROR("Failed to write to /dev/urandom: %s\n", strerror(errno));
644 goto ret;
645 }
646 total_bytes_written += chunk_size;
647 }
648
Elliott Hughesccecf142014-01-16 10:53:11 -0800649 INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
Alex Klyubin0d872d82013-08-16 13:19:24 -0700650 total_bytes_written);
651 result = 0;
652
653ret:
654 if (hwrandom_fd != -1) {
655 close(hwrandom_fd);
656 }
657 if (urandom_fd != -1) {
658 close(urandom_fd);
659 }
660 memset(buf, 0, sizeof(buf));
661 return result;
662}
663
Colin Crossebc6ff12010-04-13 19:52:01 -0700664static int keychord_init_action(int nargs, char **args)
665{
666 keychord_init();
667 return 0;
668}
669
670static int console_init_action(int nargs, char **args)
671{
672 int fd;
Colin Crossebc6ff12010-04-13 19:52:01 -0700673
674 if (console[0]) {
Hong-Mei Li11467182013-04-01 11:17:51 +0800675 snprintf(console_name, sizeof(console_name), "/dev/%s", console);
Colin Crossebc6ff12010-04-13 19:52:01 -0700676 }
677
Nick Kralevich45a884f2015-02-02 14:37:22 -0800678 fd = open(console_name, O_RDWR | O_CLOEXEC);
Colin Crossebc6ff12010-04-13 19:52:01 -0700679 if (fd >= 0)
680 have_console = 1;
681 close(fd);
682
Nick Kralevich45a884f2015-02-02 14:37:22 -0800683 fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200684 if (fd >= 0) {
685 const char *msg;
686 msg = "\n"
687 "\n"
688 "\n"
689 "\n"
690 "\n"
691 "\n"
692 "\n" // console is 40 cols x 30 lines
693 "\n"
694 "\n"
695 "\n"
696 "\n"
697 "\n"
698 "\n"
699 "\n"
700 " A N D R O I D ";
701 write(fd, msg, strlen(msg));
702 close(fd);
Colin Crossebc6ff12010-04-13 19:52:01 -0700703 }
Marcin Chojnacki50dc9362013-10-16 17:39:16 +0200704
Colin Crossebc6ff12010-04-13 19:52:01 -0700705 return 0;
706}
707
Dima Zavin5511c842011-12-19 11:21:32 -0800708static void import_kernel_nv(char *name, int for_emulator)
709{
710 char *value = strchr(name, '=');
711 int name_len = strlen(name);
712
713 if (value == 0) return;
714 *value++ = 0;
715 if (name_len == 0) return;
716
717 if (for_emulator) {
718 /* in the emulator, export any kernel option with the
719 * ro.kernel. prefix */
720 char buff[PROP_NAME_MAX];
721 int len = snprintf( buff, sizeof(buff), "ro.kernel.%s", name );
722
723 if (len < (int)sizeof(buff))
724 property_set( buff, value );
725 return;
726 }
727
728 if (!strcmp(name,"qemu")) {
729 strlcpy(qemu, value, sizeof(qemu));
730 } else if (!strncmp(name, "androidboot.", 12) && name_len > 12) {
731 const char *boot_prop_name = name + 12;
732 char prop[PROP_NAME_MAX];
733 int cnt;
734
735 cnt = snprintf(prop, sizeof(prop), "ro.boot.%s", boot_prop_name);
736 if (cnt < PROP_NAME_MAX)
737 property_set(prop, value);
738 }
739}
740
741static void export_kernel_boot_props(void)
Colin Crossebc6ff12010-04-13 19:52:01 -0700742{
743 char tmp[PROP_VALUE_MAX];
Colin Cross1a6f4c32013-01-28 17:13:35 -0800744 int ret;
Dima Zavin5511c842011-12-19 11:21:32 -0800745 unsigned i;
746 struct {
747 const char *src_prop;
748 const char *dest_prop;
749 const char *def_val;
750 } prop_map[] = {
751 { "ro.boot.serialno", "ro.serialno", "", },
752 { "ro.boot.mode", "ro.bootmode", "unknown", },
753 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800754 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
755 };
Colin Crossebc6ff12010-04-13 19:52:01 -0700756
Dima Zavin5511c842011-12-19 11:21:32 -0800757 for (i = 0; i < ARRAY_SIZE(prop_map); i++) {
Colin Cross1a6f4c32013-01-28 17:13:35 -0800758 ret = property_get(prop_map[i].src_prop, tmp);
Colin Cross5e484e92013-06-17 16:20:08 -0700759 if (ret > 0)
760 property_set(prop_map[i].dest_prop, tmp);
761 else
Colin Cross1a6f4c32013-01-28 17:13:35 -0800762 property_set(prop_map[i].dest_prop, prop_map[i].def_val);
Dima Zavin5511c842011-12-19 11:21:32 -0800763 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700764
Colin Cross1a6f4c32013-01-28 17:13:35 -0800765 ret = property_get("ro.boot.console", tmp);
766 if (ret)
767 strlcpy(console, tmp, sizeof(console));
Dima Zavin5511c842011-12-19 11:21:32 -0800768
769 /* save a copy for init's usage during boot */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800770 property_get("ro.bootmode", tmp);
771 strlcpy(bootmode, tmp, sizeof(bootmode));
Dima Zavin5511c842011-12-19 11:21:32 -0800772
773 /* if this was given on kernel command line, override what we read
774 * before (e.g. from /proc/cpuinfo), if anything */
Colin Cross1a6f4c32013-01-28 17:13:35 -0800775 ret = property_get("ro.boot.hardware", tmp);
776 if (ret)
777 strlcpy(hardware, tmp, sizeof(hardware));
Dima Zavin5511c842011-12-19 11:21:32 -0800778 property_set("ro.hardware", hardware);
779
780 snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
781 property_set("ro.revision", tmp);
782
783 /* TODO: these are obsolete. We should delete them */
Colin Crossebc6ff12010-04-13 19:52:01 -0700784 if (!strcmp(bootmode,"factory"))
785 property_set("ro.factorytest", "1");
786 else if (!strcmp(bootmode,"factory2"))
787 property_set("ro.factorytest", "2");
788 else
789 property_set("ro.factorytest", "0");
Dima Zavin5511c842011-12-19 11:21:32 -0800790}
Colin Crossebc6ff12010-04-13 19:52:01 -0700791
Dima Zavin5511c842011-12-19 11:21:32 -0800792static void process_kernel_cmdline(void)
793{
794 /* don't expose the raw commandline to nonpriv processes */
795 chmod("/proc/cmdline", 0440);
Colin Crossebc6ff12010-04-13 19:52:01 -0700796
Dima Zavin5511c842011-12-19 11:21:32 -0800797 /* first pass does the common stuff, and finds if we are in qemu.
798 * second pass is only necessary for qemu to export all kernel params
799 * as props.
800 */
801 import_kernel_cmdline(0, import_kernel_nv);
802 if (qemu[0])
803 import_kernel_cmdline(1, import_kernel_nv);
804
805 /* now propogate the info given on command line to internal variables
806 * used by init as well as the current required properties
807 */
808 export_kernel_boot_props();
Colin Crossebc6ff12010-04-13 19:52:01 -0700809}
810
811static int property_service_init_action(int nargs, char **args)
812{
813 /* read any property files on system or data and
814 * fire up the property service. This must happen
815 * after the ro.foo properties are set above so
816 * that /data/local.prop cannot interfere with them.
817 */
818 start_property_service();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700819 if (get_property_set_fd() < 0) {
820 ERROR("start_property_service() failed\n");
821 exit(1);
822 }
823
Colin Crossebc6ff12010-04-13 19:52:01 -0700824 return 0;
825}
826
827static int signal_init_action(int nargs, char **args)
828{
829 signal_init();
Riley Andrews9464e5a2014-07-11 15:05:23 -0700830 if (get_signal_fd() < 0) {
831 ERROR("signal_init() failed\n");
Colin Crossebc6ff12010-04-13 19:52:01 -0700832 exit(1);
833 }
834 return 0;
835}
836
837static int queue_property_triggers_action(int nargs, char **args)
838{
839 queue_all_property_triggers();
840 /* enable property triggers */
841 property_triggers_enabled = 1;
842 return 0;
843}
844
845#if BOOTCHART
846static int bootchart_init_action(int nargs, char **args)
847{
848 bootchart_count = bootchart_init();
849 if (bootchart_count < 0) {
850 ERROR("bootcharting init failure\n");
851 } else if (bootchart_count > 0) {
852 NOTICE("bootcharting started (period=%d ms)\n", bootchart_count*BOOTCHART_POLLING_MS);
853 } else {
854 NOTICE("bootcharting ignored\n");
855 }
Carl-Emil Lagerstedt9ab81902011-01-14 09:35:30 +0100856
857 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700858}
859#endif
860
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400861void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500862{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400863 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500864 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400865 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400866}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500867
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700868static bool selinux_is_disabled(void)
869{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700870#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700871 char tmp[PROP_VALUE_MAX];
872
873 if (access("/sys/fs/selinux", F_OK) != 0) {
874 /* SELinux is not compiled into the kernel, or has been disabled
875 * via the kernel command line "selinux=0".
876 */
877 return true;
878 }
879
880 if ((property_get("ro.boot.selinux", tmp) != 0) && (strcmp(tmp, "disabled") == 0)) {
881 /* SELinux is compiled into the kernel, but we've been told to disable it. */
882 return true;
883 }
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700884#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700885
886 return false;
887}
888
889static bool selinux_is_enforcing(void)
890{
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700891#ifdef ALLOW_DISABLE_SELINUX
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700892 char tmp[PROP_VALUE_MAX];
893
894 if (property_get("ro.boot.selinux", tmp) == 0) {
895 /* Property is not set. Assume enforcing */
896 return true;
897 }
898
899 if (strcmp(tmp, "permissive") == 0) {
900 /* SELinux is in the kernel, but we've been told to go into permissive mode */
901 return false;
902 }
903
904 if (strcmp(tmp, "enforcing") != 0) {
905 ERROR("SELinux: Unknown value of ro.boot.selinux. Got: \"%s\". Assuming enforcing.\n", tmp);
906 }
907
Nick Kralevichf3c85b22014-06-02 20:56:04 -0700908#endif
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700909 return true;
910}
911
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400912int selinux_reload_policy(void)
913{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700914 if (selinux_is_disabled()) {
915 return -1;
916 }
917
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400918 INFO("SELinux: Attempting to reload policy files\n");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500919
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400920 if (selinux_android_reload_policy() == -1) {
921 return -1;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500922 }
923
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400924 if (sehandle)
925 selabel_close(sehandle);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500926
rpcraig63207cd2012-08-09 10:05:49 -0400927 if (sehandle_prop)
928 selabel_close(sehandle_prop);
929
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400930 selinux_init_all_handles();
931 return 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500932}
rpcraig63207cd2012-08-09 10:05:49 -0400933
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500934static int audit_callback(void *data, security_class_t cls __attribute__((unused)), char *buf, size_t len)
rpcraig63207cd2012-08-09 10:05:49 -0400935{
936 snprintf(buf, len, "property=%s", !data ? "NULL" : (char *)data);
937 return 0;
938}
939
Stephen Smalley439224e2014-06-24 13:45:43 -0400940int log_callback(int type, const char *fmt, ...)
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500941{
942 int level;
943 va_list ap;
944 switch (type) {
945 case SELINUX_WARNING:
946 level = KLOG_WARNING_LEVEL;
947 break;
948 case SELINUX_INFO:
949 level = KLOG_INFO_LEVEL;
950 break;
951 default:
952 level = KLOG_ERROR_LEVEL;
953 break;
954 }
955 va_start(ap, fmt);
956 klog_vwrite(level, fmt, ap);
957 va_end(ap);
958 return 0;
959}
960
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700961static void selinux_initialize(void)
962{
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700963 if (selinux_is_disabled()) {
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700964 return;
965 }
966
967 INFO("loading selinux policy\n");
968 if (selinux_android_load_policy() < 0) {
969 ERROR("SELinux: Failed to load policy; rebooting into recovery mode\n");
970 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
971 while (1) { pause(); } // never reached
972 }
973
974 selinux_init_all_handles();
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700975 bool is_enforcing = selinux_is_enforcing();
976 INFO("SELinux: security_setenforce(%d)\n", is_enforcing);
977 security_setenforce(is_enforcing);
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700978}
979
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700980int main(int argc, char **argv)
981{
Colin Crossebc6ff12010-04-13 19:52:01 -0700982 int fd_count = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700983 struct pollfd ufds[4];
Colin Crossebc6ff12010-04-13 19:52:01 -0700984 int property_set_fd_init = 0;
985 int signal_fd_init = 0;
986 int keychord_fd_init = 0;
Dima Zavind7634c92011-12-16 14:18:06 -0800987 bool is_charger = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700988
Colin Crossf83d0b92010-04-21 12:04:20 -0700989 if (!strcmp(basename(argv[0]), "ueventd"))
990 return ueventd_main(argc, argv);
991
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700992 if (!strcmp(basename(argv[0]), "watchdogd"))
993 return watchdogd_main(argc, argv);
994
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700995 /* clear the umask */
996 umask(0);
997
998 /* Get the basic filesystem setup we need put
999 * together in the initramdisk on / and then we'll
1000 * let the rc file figure out the rest.
1001 */
1002 mkdir("/dev", 0755);
1003 mkdir("/proc", 0755);
1004 mkdir("/sys", 0755);
1005
Nick Kralevich150f19e2010-06-22 16:35:43 -07001006 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001007 mkdir("/dev/pts", 0755);
1008 mkdir("/dev/socket", 0755);
1009 mount("devpts", "/dev/pts", "devpts", 0, NULL);
1010 mount("proc", "/proc", "proc", 0, NULL);
1011 mount("sysfs", "/sys", "sysfs", 0, NULL);
1012
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001013 /* indicate that booting is in progress to background fw loaders, etc */
Nick Kralevich45a884f2015-02-02 14:37:22 -08001014 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Brian Swetland8d48c8e2011-03-24 15:45:30 -07001015
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001016 /* We must have some place other than / to create the
1017 * device nodes for kmsg and null, otherwise we won't
1018 * be able to remount / read-only later on.
1019 * Now that tmpfs is mounted on /dev, we can actually
1020 * talk to the outside world.
1021 */
1022 open_devnull_stdio();
Dima Zavin8f912822011-08-31 18:26:17 -07001023 klog_init();
Dima Zavin5511c842011-12-19 11:21:32 -08001024 property_init();
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -07001025
Colin Crossf83d0b92010-04-21 12:04:20 -07001026 get_hardware_name(hardware, &revision);
Dima Zavind7634c92011-12-16 14:18:06 -08001027
Dima Zavin5511c842011-12-19 11:21:32 -08001028 process_kernel_cmdline();
1029
rpcraig63207cd2012-08-09 10:05:49 -04001030 union selinux_callback cb;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -05001031 cb.func_log = log_callback;
rpcraig63207cd2012-08-09 10:05:49 -04001032 selinux_set_callback(SELINUX_CB_LOG, cb);
1033
1034 cb.func_audit = audit_callback;
1035 selinux_set_callback(SELINUX_CB_AUDIT, cb);
1036
Nick Kralevich56fa0ac2013-06-24 17:41:40 -07001037 selinux_initialize();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -04001038 /* These directories were necessarily created before initial policy load
Stephen Smalleye096e362012-06-11 13:37:39 -04001039 * and therefore need their security context restored to the proper value.
1040 * This must happen before /dev is populated by ueventd.
1041 */
1042 restorecon("/dev");
1043 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -08001044 restorecon("/dev/__properties__");
Nick Kralevichae76f6d2013-07-11 15:38:26 -07001045 restorecon_recursive("/sys");
Stephen Smalleye46f9d52012-01-13 08:48:47 -05001046
Dima Zavind7634c92011-12-16 14:18:06 -08001047 is_charger = !strcmp(bootmode, "charger");
1048
1049 INFO("property init\n");
Riley Andrewse4b7b292014-06-16 15:06:21 -07001050 property_load_boot_defaults();
Dima Zavind7634c92011-12-16 14:18:06 -08001051
1052 INFO("reading config file\n");
1053 init_parse_config_file("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001054
1055 action_for_each_trigger("early-init", action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001056
Colin Crossf83d0b92010-04-21 12:04:20 -07001057 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Alex Klyubin0d872d82013-08-16 13:19:24 -07001058 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001059 queue_builtin_action(keychord_init_action, "keychord_init");
1060 queue_builtin_action(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001061
Dima Zavinca47cef2011-08-24 15:28:23 -07001062 /* execute all the boot actions to get us started */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001063 action_for_each_trigger("init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001064
Alex Klyubin0d872d82013-08-16 13:19:24 -07001065 /* Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
1066 * wasn't ready immediately after wait_for_coldboot_done
1067 */
1068 queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Colin Crossebc6ff12010-04-13 19:52:01 -07001069 queue_builtin_action(property_service_init_action, "property_service_init");
1070 queue_builtin_action(signal_init_action, "signal_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001071
Riley Andrewse4b7b292014-06-16 15:06:21 -07001072 /* Don't mount filesystems or start core system services if in charger mode. */
Dima Zavind7634c92011-12-16 14:18:06 -08001073 if (is_charger) {
Dima Zavinca47cef2011-08-24 15:28:23 -07001074 action_for_each_trigger("charger", action_add_queue_tail);
1075 } else {
Riley Andrewse4b7b292014-06-16 15:06:21 -07001076 action_for_each_trigger("late-init", action_add_queue_tail);
Dima Zavinca47cef2011-08-24 15:28:23 -07001077 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001078
Riley Andrews9464e5a2014-07-11 15:05:23 -07001079 /* run all property triggers based on current state of the properties */
Chris Dearman469b7b22012-03-01 15:29:20 -08001080 queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001081
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001082
1083#if BOOTCHART
Colin Crossebc6ff12010-04-13 19:52:01 -07001084 queue_builtin_action(bootchart_init_action, "bootchart_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001085#endif
1086
1087 for(;;) {
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001088 int nr, i, timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001089
Colin Crossebc6ff12010-04-13 19:52:01 -07001090 execute_one_command();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001091 restart_processes();
1092
Colin Crossebc6ff12010-04-13 19:52:01 -07001093 if (!property_set_fd_init && get_property_set_fd() > 0) {
1094 ufds[fd_count].fd = get_property_set_fd();
1095 ufds[fd_count].events = POLLIN;
1096 ufds[fd_count].revents = 0;
1097 fd_count++;
1098 property_set_fd_init = 1;
1099 }
1100 if (!signal_fd_init && get_signal_fd() > 0) {
1101 ufds[fd_count].fd = get_signal_fd();
1102 ufds[fd_count].events = POLLIN;
1103 ufds[fd_count].revents = 0;
1104 fd_count++;
1105 signal_fd_init = 1;
1106 }
1107 if (!keychord_fd_init && get_keychord_fd() > 0) {
1108 ufds[fd_count].fd = get_keychord_fd();
1109 ufds[fd_count].events = POLLIN;
1110 ufds[fd_count].revents = 0;
1111 fd_count++;
1112 keychord_fd_init = 1;
1113 }
1114
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001115 if (process_needs_restart) {
1116 timeout = (process_needs_restart - gettime()) * 1000;
1117 if (timeout < 0)
1118 timeout = 0;
1119 }
1120
Colin Crossebd46132010-04-22 11:52:23 -07001121 if (!action_queue_empty() || cur_action)
Colin Crossebc6ff12010-04-13 19:52:01 -07001122 timeout = 0;
1123
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001124#if BOOTCHART
1125 if (bootchart_count > 0) {
Bo (Andover) Zhang37003732014-07-24 13:11:35 -04001126 long long current_time;
1127 int elapsed_time, remaining_time;
1128
1129 current_time = bootchart_gettime();
1130 elapsed_time = current_time - bootchart_time;
1131
1132 if (elapsed_time >= BOOTCHART_POLLING_MS) {
1133 /* count missed samples */
1134 while (elapsed_time >= BOOTCHART_POLLING_MS) {
1135 elapsed_time -= BOOTCHART_POLLING_MS;
1136 bootchart_count--;
1137 }
1138 /* count may be negative, take a sample anyway */
1139 bootchart_time = current_time;
1140 if (bootchart_step() < 0 || bootchart_count <= 0) {
1141 bootchart_finish();
1142 bootchart_count = 0;
1143 }
1144 }
1145 if (bootchart_count > 0) {
1146 remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
1147 if (timeout < 0 || timeout > remaining_time)
1148 timeout = remaining_time;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001149 }
1150 }
1151#endif
Colin Crossebc6ff12010-04-13 19:52:01 -07001152
The Android Open Source Project5ae090e2009-01-09 17:51:25 -08001153 nr = poll(ufds, fd_count, timeout);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001154 if (nr <= 0)
1155 continue;
1156
Colin Crossebc6ff12010-04-13 19:52:01 -07001157 for (i = 0; i < fd_count; i++) {
Amir Goldstein1d4e86c2013-11-10 15:36:58 +02001158 if (ufds[i].revents & POLLIN) {
Colin Crossf83d0b92010-04-21 12:04:20 -07001159 if (ufds[i].fd == get_property_set_fd())
Colin Crossebc6ff12010-04-13 19:52:01 -07001160 handle_property_set_fd();
1161 else if (ufds[i].fd == get_keychord_fd())
1162 handle_keychord();
1163 else if (ufds[i].fd == get_signal_fd())
1164 handle_signal();
1165 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001166 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001167 }
1168
1169 return 0;
1170}