blob: 809a6f567bdbe9aa8ea591b13fafc9bc21aa2faf [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
Elliott Hughes8d82ea02015-02-06 20:15:18 -080017#include <ctype.h>
Rom Lemarchand6a524432015-02-28 06:39:11 -080018#include <dirent.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080019#include <errno.h>
20#include <fcntl.h>
Daniel Cashmanf6112912016-03-25 23:49:05 +000021#include <fstream>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080022#include <libgen.h>
Elliott Hughescc86fb22015-03-17 20:01:13 -070023#include <paths.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080024#include <signal.h>
25#include <stdarg.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Elliott Hughes929f4072015-04-24 21:13:44 -070029#include <sys/epoll.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070030#include <sys/mount.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070031#include <sys/socket.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080032#include <sys/stat.h>
Elliott Hughes35f5d042016-07-26 09:32:33 -070033#include <sys/sysmacros.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080034#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035#include <sys/un.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080036#include <sys/wait.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080037#include <unistd.h>
38
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039#include <selinux/selinux.h>
40#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040041#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042
Elliott Hughes4f713192015-12-04 22:00:26 -080043#include <android-base/file.h>
44#include <android-base/stringprintf.h>
45#include <android-base/strings.h>
Nick Kralevich56fa0ac2013-06-24 17:41:40 -070046#include <cutils/android_reboot.h>
Alex Klyubin0d872d82013-08-16 13:19:24 -070047#include <cutils/fs.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080048#include <cutils/iosched_policy.h>
49#include <cutils/list.h>
50#include <cutils/sockets.h>
Colin Crossf83d0b92010-04-21 12:04:20 -070051#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052
Rom Lemarchand6a524432015-02-28 06:39:11 -080053#include <memory>
54
Tom Cherryfa0c21c2015-07-23 17:53:11 -070055#include "action.h"
Tom Cherrybac32992015-07-31 12:45:25 -070056#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057#include "devices.h"
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080058#include "fs_mgr.h"
Tom Cherryb7349902015-08-26 11:43:36 -070059#include "import_parser.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060#include "init.h"
Tom Cherrybac32992015-07-31 12:45:25 -070061#include "init_parser.h"
62#include "keychords.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070063#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064#include "property_service.h"
Tom Cherrybac32992015-07-31 12:45:25 -070065#include "service.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070066#include "signal_handler.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070067#include "ueventd.h"
Tom Cherrybac32992015-07-31 12:45:25 -070068#include "util.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070069#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070
Stephen Smalleye46f9d52012-01-13 08:48:47 -050071struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040072struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050073
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074static int property_triggers_enabled = 0;
75
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076static char qemu[32];
77
Viorel Suman70daa672016-03-21 10:08:07 +020078std::string default_console = "/dev/console";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079static time_t process_needs_restart;
80
Tom Cherrybac32992015-07-31 12:45:25 -070081const char *ENV[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082
Elliott Hughes8d82ea02015-02-06 20:15:18 -080083bool waiting_for_exec = false;
84
Elliott Hughes929f4072015-04-24 21:13:44 -070085static int epoll_fd = -1;
86
87void register_epoll_handler(int fd, void (*fn)()) {
88 epoll_event ev;
89 ev.events = EPOLLIN;
90 ev.data.ptr = reinterpret_cast<void*>(fn);
91 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070092 PLOG(ERROR) << "epoll_ctl failed";
Elliott Hughes929f4072015-04-24 21:13:44 -070093 }
94}
95
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070096/* 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 */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700103 for (n = 0; n < (arraysize(ENV) - 1); n++) {
James Morrissey381341f2014-05-16 11:36:36 +0100104
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) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800116 char* entry;
117 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 ENV[n] = entry;
119 return 0;
120 }
121 }
122
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700123 LOG(ERROR) << "No env. room to store: '" << key << "':'" << val << "'";
James Morrissey381341f2014-05-16 11:36:36 +0100124
125 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700126}
127
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128void property_changed(const char *name, const char *value)
129{
Colin Crossebc6ff12010-04-13 19:52:01 -0700130 if (property_triggers_enabled)
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700131 ActionManager::GetInstance().QueuePropertyTrigger(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700132}
133
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700134static void restart_processes()
135{
136 process_needs_restart = 0;
Tom Cherrybac32992015-07-31 12:45:25 -0700137 ServiceManager::GetInstance().
138 ForEachServiceWithFlags(SVC_RESTARTING, [] (Service* s) {
139 s->RestartIfNeeded(process_needs_restart);
140 });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700141}
142
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700143void handle_control_message(const std::string& msg, const std::string& name) {
Tom Cherrybac32992015-07-31 12:45:25 -0700144 Service* svc = ServiceManager::GetInstance().FindServiceByName(name);
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700145 if (svc == nullptr) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700146 LOG(ERROR) << "no such service '" << name << "'";
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700147 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700148 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700149
Tom Cherrybac32992015-07-31 12:45:25 -0700150 if (msg == "start") {
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700151 svc->Start();
Tom Cherrybac32992015-07-31 12:45:25 -0700152 } else if (msg == "stop") {
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700153 svc->Stop();
Tom Cherrybac32992015-07-31 12:45:25 -0700154 } else if (msg == "restart") {
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700155 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700156 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700157 LOG(ERROR) << "unknown control msg '" << msg << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700158 }
159}
160
Tom Cherry96f67312015-07-30 13:52:55 -0700161static int wait_for_coldboot_done_action(const std::vector<std::string>& args) {
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700162 Timer t;
163
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700164 LOG(VERBOSE) << "Waiting for " COLDBOOT_DONE "...";
Elliott Hughesc7331d02015-05-14 12:03:14 -0700165 // Any longer than 1s is an unreasonable length of time to delay booting.
166 // If you're hitting this timeout, check that you didn't make your
167 // sepolicy regular expressions too expensive (http://b/19899875).
168 if (wait_for_file(COLDBOOT_DONE, 1)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700169 LOG(ERROR) << "Timed out waiting for " COLDBOOT_DONE;
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700170 }
171
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700172 LOG(VERBOSE) << "Waiting for " COLDBOOT_DONE " took " << t.duration() << "s.";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700173 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700174}
175
Alex Klyubin0d872d82013-08-16 13:19:24 -0700176/*
177 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
178 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
179 * Does nothing if Hardware RNG is not present.
180 *
181 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
182 * mixed into the primary pool of Linux RNG and the entropy estimate is left
183 * unmodified.
184 *
185 * If the HW RNG device /dev/hw_random is present, we require that at least
186 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
187 * devices/configurations where these I/O operations are blocking for a long
188 * time. We do not reboot or halt on failures, as this is a best-effort
189 * attempt.
190 */
Tom Cherry96f67312015-07-30 13:52:55 -0700191static int mix_hwrng_into_linux_rng_action(const std::vector<std::string>& args)
Alex Klyubin0d872d82013-08-16 13:19:24 -0700192{
193 int result = -1;
194 int hwrandom_fd = -1;
195 int urandom_fd = -1;
196 char buf[512];
197 ssize_t chunk_size;
198 size_t total_bytes_written = 0;
199
200 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800201 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700202 if (hwrandom_fd == -1) {
203 if (errno == ENOENT) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700204 LOG(ERROR) << "/dev/hw_random not found";
205 // It's not an error to not have a Hardware RNG.
206 result = 0;
Alex Klyubin0d872d82013-08-16 13:19:24 -0700207 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700208 PLOG(ERROR) << "Failed to open /dev/hw_random";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700209 }
210 goto ret;
211 }
212
213 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800214 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700215 if (urandom_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700216 PLOG(ERROR) << "Failed to open /dev/urandom";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700217 goto ret;
218 }
219
220 while (total_bytes_written < sizeof(buf)) {
221 chunk_size = TEMP_FAILURE_RETRY(
222 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
223 if (chunk_size == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700224 PLOG(ERROR) << "Failed to read from /dev/hw_random";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700225 goto ret;
226 } else if (chunk_size == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700227 LOG(ERROR) << "Failed to read from /dev/hw_random: EOF";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700228 goto ret;
229 }
230
231 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
232 if (chunk_size == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700233 PLOG(ERROR) << "Failed to write to /dev/urandom";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700234 goto ret;
235 }
236 total_bytes_written += chunk_size;
237 }
238
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700239 LOG(INFO) << "Mixed " << total_bytes_written << " bytes from /dev/hw_random into /dev/urandom";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700240 result = 0;
241
242ret:
243 if (hwrandom_fd != -1) {
244 close(hwrandom_fd);
245 }
246 if (urandom_fd != -1) {
247 close(urandom_fd);
248 }
Alex Klyubin0d872d82013-08-16 13:19:24 -0700249 return result;
250}
251
Daniel Cashmanf6112912016-03-25 23:49:05 +0000252static void security_failure() {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700253 LOG(ERROR) << "Security failure; rebooting into recovery mode...";
Daniel Cashmanf6112912016-03-25 23:49:05 +0000254 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
255 while (true) { pause(); } // never reached
256}
257
258#define MMAP_RND_PATH "/proc/sys/vm/mmap_rnd_bits"
259#define MMAP_RND_COMPAT_PATH "/proc/sys/vm/mmap_rnd_compat_bits"
260
261/* __attribute__((unused)) due to lack of mips support: see mips block
262 * in set_mmap_rnd_bits_action */
263static bool __attribute__((unused)) set_mmap_rnd_bits_min(int start, int min, bool compat) {
264 std::string path;
265 if (compat) {
266 path = MMAP_RND_COMPAT_PATH;
267 } else {
268 path = MMAP_RND_PATH;
269 }
270 std::ifstream inf(path, std::fstream::in);
271 if (!inf) {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700272 LOG(ERROR) << "Cannot open for reading: " << path;
Daniel Cashmanf6112912016-03-25 23:49:05 +0000273 return false;
274 }
275 while (start >= min) {
276 // try to write out new value
277 std::string str_val = std::to_string(start);
278 std::ofstream of(path, std::fstream::out);
279 if (!of) {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700280 LOG(ERROR) << "Cannot open for writing: " << path;
Daniel Cashmanf6112912016-03-25 23:49:05 +0000281 return false;
282 }
283 of << str_val << std::endl;
284 of.close();
285
286 // check to make sure it was recorded
287 inf.seekg(0);
288 std::string str_rec;
289 inf >> str_rec;
290 if (str_val.compare(str_rec) == 0) {
291 break;
292 }
293 start--;
294 }
295 inf.close();
296 if (start < min) {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700297 LOG(ERROR) << "Unable to set minimum required entropy " << min << " in " << path;
Daniel Cashmanf6112912016-03-25 23:49:05 +0000298 return false;
299 }
300 return true;
301}
302
303/*
304 * Set /proc/sys/vm/mmap_rnd_bits and potentially
305 * /proc/sys/vm/mmap_rnd_compat_bits to the maximum supported values.
306 * Returns -1 if unable to set these to an acceptable value.
307 *
308 * To support this sysctl, the following upstream commits are needed:
309 *
310 * d07e22597d1d mm: mmap: add new /proc tunable for mmap_base ASLR
311 * e0c25d958f78 arm: mm: support ARCH_MMAP_RND_BITS
312 * 8f0d3aa9de57 arm64: mm: support ARCH_MMAP_RND_BITS
313 * 9e08f57d684a x86: mm: support ARCH_MMAP_RND_BITS
314 * ec9ee4acd97c drivers: char: random: add get_random_long()
315 * 5ef11c35ce86 mm: ASLR: use get_random_long()
316 */
317static int set_mmap_rnd_bits_action(const std::vector<std::string>& args)
318{
319 int ret = -1;
320
321 /* values are arch-dependent */
322#if defined(__aarch64__)
323 /* arm64 supports 18 - 33 bits depending on pagesize and VA_SIZE */
324 if (set_mmap_rnd_bits_min(33, 24, false)
325 && set_mmap_rnd_bits_min(16, 16, true)) {
326 ret = 0;
327 }
328#elif defined(__x86_64__)
329 /* x86_64 supports 28 - 32 bits */
330 if (set_mmap_rnd_bits_min(32, 32, false)
331 && set_mmap_rnd_bits_min(16, 16, true)) {
332 ret = 0;
333 }
334#elif defined(__arm__) || defined(__i386__)
335 /* check to see if we're running on 64-bit kernel */
336 bool h64 = !access(MMAP_RND_COMPAT_PATH, F_OK);
337 /* supported 32-bit architecture must have 16 bits set */
338 if (set_mmap_rnd_bits_min(16, 16, h64)) {
339 ret = 0;
340 }
341#elif defined(__mips__) || defined(__mips64__)
342 // TODO: add mips support b/27788820
343 ret = 0;
344#else
345 ERROR("Unknown architecture\n");
346#endif
347
348#ifdef __BRILLO__
349 // TODO: b/27794137
350 ret = 0;
351#endif
352 if (ret == -1) {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700353 LOG(ERROR) << "Unable to set adequate mmap entropy value!";
Daniel Cashmanf6112912016-03-25 23:49:05 +0000354 security_failure();
355 }
356 return ret;
357}
358
Tom Cherry96f67312015-07-30 13:52:55 -0700359static int keychord_init_action(const std::vector<std::string>& args)
Colin Crossebc6ff12010-04-13 19:52:01 -0700360{
361 keychord_init();
362 return 0;
363}
364
Tom Cherry96f67312015-07-30 13:52:55 -0700365static int console_init_action(const std::vector<std::string>& args)
Colin Crossebc6ff12010-04-13 19:52:01 -0700366{
Viorel Sumanefc72752016-03-21 10:23:43 +0200367 std::string console = property_get("ro.boot.console");
368 if (!console.empty()) {
Viorel Suman70daa672016-03-21 10:08:07 +0200369 default_console = "/dev/" + console;
Colin Crossebc6ff12010-04-13 19:52:01 -0700370 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700371 return 0;
372}
373
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700374static void import_kernel_nv(const std::string& key, const std::string& value, bool for_emulator) {
375 if (key.empty()) return;
Dima Zavin5511c842011-12-19 11:21:32 -0800376
377 if (for_emulator) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700378 // In the emulator, export any kernel option with the "ro.kernel." prefix.
379 property_set(android::base::StringPrintf("ro.kernel.%s", key.c_str()).c_str(), value.c_str());
Dima Zavin5511c842011-12-19 11:21:32 -0800380 return;
381 }
382
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700383 if (key == "qemu") {
384 strlcpy(qemu, value.c_str(), sizeof(qemu));
385 } else if (android::base::StartsWith(key, "androidboot.")) {
386 property_set(android::base::StringPrintf("ro.boot.%s", key.c_str() + 12).c_str(),
387 value.c_str());
Dima Zavin5511c842011-12-19 11:21:32 -0800388 }
389}
390
Sami Tolvanen9e9efca2015-12-07 12:33:58 +0000391static void export_oem_lock_status() {
392 if (property_get("ro.oem_unlock_supported") != "1") {
393 return;
394 }
395
396 std::string value = property_get("ro.boot.verifiedbootstate");
397
398 if (!value.empty()) {
399 property_set("ro.boot.flash.locked", value == "orange" ? "0" : "1");
400 }
401}
402
Elliott Hughesd679bc92015-03-20 08:50:46 -0700403static void export_kernel_boot_props() {
Dima Zavin5511c842011-12-19 11:21:32 -0800404 struct {
405 const char *src_prop;
Elliott Hughesd679bc92015-03-20 08:50:46 -0700406 const char *dst_prop;
407 const char *default_value;
Dima Zavin5511c842011-12-19 11:21:32 -0800408 } prop_map[] = {
Elliott Hughesd679bc92015-03-20 08:50:46 -0700409 { "ro.boot.serialno", "ro.serialno", "", },
410 { "ro.boot.mode", "ro.bootmode", "unknown", },
411 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800412 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
Elliott Hughesd679bc92015-03-20 08:50:46 -0700413 { "ro.boot.hardware", "ro.hardware", "unknown", },
414 { "ro.boot.revision", "ro.revision", "0", },
Dima Zavin5511c842011-12-19 11:21:32 -0800415 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700416 for (size_t i = 0; i < arraysize(prop_map); i++) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700417 std::string value = property_get(prop_map[i].src_prop);
418 property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value);
Dima Zavin5511c842011-12-19 11:21:32 -0800419 }
Dima Zavin5511c842011-12-19 11:21:32 -0800420}
Colin Crossebc6ff12010-04-13 19:52:01 -0700421
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700422static void process_kernel_dt() {
Rom Lemarchand6a524432015-02-28 06:39:11 -0800423 static const char android_dir[] = "/proc/device-tree/firmware/android";
424
425 std::string file_name = android::base::StringPrintf("%s/compatible", android_dir);
426
427 std::string dt_file;
428 android::base::ReadFileToString(file_name, &dt_file);
429 if (!dt_file.compare("android,firmware")) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700430 LOG(ERROR) << "firmware/android is not compatible with 'android,firmware'";
Rom Lemarchand6a524432015-02-28 06:39:11 -0800431 return;
432 }
433
434 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir);
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700435 if (!dir) return;
Rom Lemarchand6a524432015-02-28 06:39:11 -0800436
437 struct dirent *dp;
438 while ((dp = readdir(dir.get())) != NULL) {
Rom Lemarchand1dcf3252015-11-08 17:51:50 -0800439 if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible") || !strcmp(dp->d_name, "name")) {
Rom Lemarchand6a524432015-02-28 06:39:11 -0800440 continue;
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700441 }
Rom Lemarchand6a524432015-02-28 06:39:11 -0800442
443 file_name = android::base::StringPrintf("%s/%s", android_dir, dp->d_name);
444
445 android::base::ReadFileToString(file_name, &dt_file);
446 std::replace(dt_file.begin(), dt_file.end(), ',', '.');
447
448 std::string property_name = android::base::StringPrintf("ro.boot.%s", dp->d_name);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700449 property_set(property_name.c_str(), dt_file.c_str());
Rom Lemarchand6a524432015-02-28 06:39:11 -0800450 }
451}
452
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700453static void process_kernel_cmdline() {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700454 // The first pass does the common stuff, and finds if we are in qemu.
455 // The second pass is only necessary for qemu to export all kernel params
456 // as properties.
Nick Kralevichf667a322015-04-25 17:42:52 -0700457 import_kernel_cmdline(false, import_kernel_nv);
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700458 if (qemu[0]) import_kernel_cmdline(true, import_kernel_nv);
Colin Crossebc6ff12010-04-13 19:52:01 -0700459}
460
Tom Cherry96f67312015-07-30 13:52:55 -0700461static int queue_property_triggers_action(const std::vector<std::string>& args)
Colin Crossebc6ff12010-04-13 19:52:01 -0700462{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700463 ActionManager::GetInstance().QueueAllPropertyTriggers();
Colin Crossebc6ff12010-04-13 19:52:01 -0700464 /* enable property triggers */
465 property_triggers_enabled = 1;
466 return 0;
467}
468
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700469static void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500470{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400471 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500472 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400473 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400474}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500475
Nick Kralevichd34e4072015-04-28 12:39:41 -0700476enum selinux_enforcing_status { SELINUX_PERMISSIVE, SELINUX_ENFORCING };
Nick Kralevichf667a322015-04-25 17:42:52 -0700477
478static selinux_enforcing_status selinux_status_from_cmdline() {
479 selinux_enforcing_status status = SELINUX_ENFORCING;
480
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700481 import_kernel_cmdline(false, [&](const std::string& key, const std::string& value, bool in_qemu) {
482 if (key == "androidboot.selinux" && value == "permissive") {
483 status = SELINUX_PERMISSIVE;
Nick Kralevichf667a322015-04-25 17:42:52 -0700484 }
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700485 });
Nick Kralevichf667a322015-04-25 17:42:52 -0700486
487 return status;
488}
489
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700490static bool selinux_is_enforcing(void)
491{
Nick Kralevichd34e4072015-04-28 12:39:41 -0700492 if (ALLOW_PERMISSIVE_SELINUX) {
Nick Kralevichf667a322015-04-25 17:42:52 -0700493 return selinux_status_from_cmdline() == SELINUX_ENFORCING;
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700494 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700495 return true;
496}
497
Elliott Hughesda40c002015-03-27 23:20:44 -0700498static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
William Robertsd7aea442015-10-01 16:03:47 -0700499
500 property_audit_data *d = reinterpret_cast<property_audit_data*>(data);
501
502 if (!d || !d->name || !d->cr) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700503 LOG(ERROR) << "audit_callback invoked with null data arguments!";
William Robertsd7aea442015-10-01 16:03:47 -0700504 return 0;
505 }
506
507 snprintf(buf, len, "property=%s pid=%d uid=%d gid=%d", d->name,
508 d->cr->pid, d->cr->uid, d->cr->gid);
rpcraig63207cd2012-08-09 10:05:49 -0400509 return 0;
510}
511
Elliott Hughesf65730e2015-04-24 18:38:17 +0000512static void selinux_initialize(bool in_kernel_domain) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700513 Timer t;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500514
Elliott Hughesda40c002015-03-27 23:20:44 -0700515 selinux_callback cb;
516 cb.func_log = selinux_klog_callback;
517 selinux_set_callback(SELINUX_CB_LOG, cb);
518 cb.func_audit = audit_callback;
519 selinux_set_callback(SELINUX_CB_AUDIT, cb);
520
Elliott Hughesf65730e2015-04-24 18:38:17 +0000521 if (in_kernel_domain) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700522 LOG(INFO) << "Loading SELinux policy...";
Elliott Hughesf65730e2015-04-24 18:38:17 +0000523 if (selinux_android_load_policy() < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700524 PLOG(ERROR) << "failed to load policy";
Elliott Hughesf65730e2015-04-24 18:38:17 +0000525 security_failure();
526 }
527
Nick Kralevichd34e4072015-04-28 12:39:41 -0700528 bool kernel_enforcing = (security_getenforce() == 1);
Elliott Hughesf65730e2015-04-24 18:38:17 +0000529 bool is_enforcing = selinux_is_enforcing();
Nick Kralevichd34e4072015-04-28 12:39:41 -0700530 if (kernel_enforcing != is_enforcing) {
531 if (security_setenforce(is_enforcing)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700532 PLOG(ERROR) << "security_setenforce(%s) failed" << (is_enforcing ? "true" : "false");
Nick Kralevichd34e4072015-04-28 12:39:41 -0700533 security_failure();
534 }
535 }
Elliott Hughesf65730e2015-04-24 18:38:17 +0000536
Nick Kralevicheedbe812015-04-25 14:10:03 -0700537 if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) {
538 security_failure();
539 }
540
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700541 LOG(INFO) << "(Initializing SELinux " << (is_enforcing ? "enforcing" : "non-enforcing")
542 << " took " << t.duration() << "s.)";
Elliott Hughesf65730e2015-04-24 18:38:17 +0000543 } else {
544 selinux_init_all_handles();
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700545 }
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700546}
547
Amit Pundird2e74db2016-06-29 19:00:00 +0530548// Set the UDC controller for the ConfigFS USB Gadgets.
549// Read the UDC controller in use from "/sys/class/udc".
550// In case of multiple UDC controllers select the first one.
551static void set_usb_controller() {
552 std::unique_ptr<DIR, decltype(&closedir)>dir(opendir("/sys/class/udc"), closedir);
553 if (!dir) return;
554
555 dirent* dp;
556 while ((dp = readdir(dir.get())) != nullptr) {
557 if (dp->d_name[0] == '.') continue;
558
559 property_set("sys.usb.controller", dp->d_name);
560 break;
561 }
562}
563
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800564/* Returns a new path consisting of base_path and the file name in reference_path. */
565static std::string get_path(const std::string& base_path, const std::string& reference_path) {
566 std::string::size_type pos = reference_path.rfind('/');
567 if (pos == std::string::npos) {
568 return base_path + '/' + reference_path;
569 } else {
570 return base_path + reference_path.substr(pos);
571 }
572}
573
574/* Imports the fstab info from cmdline. */
575static std::string import_cmdline_fstab() {
576 std::string prefix, fstab, fstab_full;
577
578 import_kernel_cmdline(false,
579 [&](const std::string& key, const std::string& value, bool in_qemu __attribute__((__unused__))) {
580 if (key == "android.early.prefix") {
581 prefix = value;
582 } else if (key == "android.early.fstab") {
583 fstab = value;
584 }
585 });
586 if (!fstab.empty()) {
587 // Convert "mmcblk0p09+/odm+ext4+ro+verify" to "mmcblk0p09 /odm ext4 ro verify"
588 std::replace(fstab.begin(), fstab.end(), '+', ' ');
589 for (const auto& entry : android::base::Split(fstab, "\n")) {
590 fstab_full += prefix + entry + '\n';
591 }
592 }
593 return fstab_full;
594}
595
596/* Early mount vendor and ODM partitions. The fstab info is read from kernel cmdline. */
597static void early_mount() {
598 std::string fstab_string = import_cmdline_fstab();
599 if (fstab_string.empty()) {
600 LOG(INFO) << "Failed to load vendor fstab from kernel cmdline";
601 return;
602 }
603 FILE *fstab_file = fmemopen((void *)fstab_string.c_str(), fstab_string.length(), "r");
604 if (!fstab_file) {
605 PLOG(ERROR) << "Failed to open fstab string as FILE";
606 return;
607 }
608 std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_file(fstab_file), fs_mgr_free_fstab);
609 fclose(fstab_file);
610 if (!fstab) {
611 LOG(ERROR) << "Failed to parse fstab string: " << fstab_string;
612 return;
613 }
614 LOG(INFO) << "Loaded vendor fstab from cmdline";
615
616 if (early_device_socket_open()) {
617 LOG(ERROR) << "Failed to open device uevent socket";
618 return;
619 }
620
621 /* Create /dev/device-mapper for dm-verity */
622 early_create_dev("/sys/devices/virtual/misc/device-mapper", EARLY_CHAR_DEV);
623
624 for (int i = 0; i < fstab->num_entries; ++i) {
625 struct fstab_rec *rec = &fstab->recs[i];
626 std::string mount_point = rec->mount_point;
627 std::string syspath = rec->blk_device;
628
629 if (mount_point != "/vendor" && mount_point != "/odm")
630 continue;
631
632 /* Create mount target under /dev/block/ from sysfs via uevent */
633 LOG(INFO) << "Mounting " << mount_point << " from " << syspath << "...";
634 char *devpath = strdup(get_path("/dev/block", syspath).c_str());
635 if (!devpath) {
636 PLOG(ERROR) << "Failed to strdup dev path in early mount " << syspath;
637 continue;
638 }
639 rec->blk_device = devpath;
640 early_create_dev(syspath, EARLY_BLOCK_DEV);
641
642 int rc = fs_mgr_early_setup_verity(rec);
643 if (rc == FS_MGR_EARLY_SETUP_VERITY_SUCCESS) {
644 /* Mount target is changed to /dev/block/dm-<n>; initiate its creation from sysfs counterpart */
645 early_create_dev(get_path("/sys/devices/virtual/block", rec->blk_device), EARLY_BLOCK_DEV);
646 } else if (rc == FS_MGR_EARLY_SETUP_VERITY_FAIL) {
647 LOG(ERROR) << "Failed to set up dm-verity on " << rec->blk_device;
648 continue;
649 } else { /* FS_MGR_EARLY_SETUP_VERITY_NO_VERITY */
650 LOG(INFO) << "dm-verity disabled on debuggable device; mount directly on " << rec->blk_device;
651 }
652
653 mkdir(mount_point.c_str(), 0755);
654 rc = mount(rec->blk_device, mount_point.c_str(), rec->fs_type, rec->flags, rec->fs_options);
655 if (rc) {
656 PLOG(ERROR) << "Failed to mount on " << rec->blk_device;
657 }
658 }
659 early_device_socket_close();
660}
661
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800662int main(int argc, char** argv) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700663 if (!strcmp(basename(argv[0]), "ueventd")) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700664 return ueventd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700665 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700666
Elliott Hughesda40c002015-03-27 23:20:44 -0700667 if (!strcmp(basename(argv[0]), "watchdogd")) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700668 return watchdogd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700669 }
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700670
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800671 // Clear the umask.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700672 umask(0);
673
Elliott Hughescc86fb22015-03-17 20:01:13 -0700674 add_environment("PATH", _PATH_DEFPATH);
675
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700676 bool is_first_stage = (argc == 1) || (strcmp(argv[1], "--second-stage") != 0);
677
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800678 // Don't expose the raw commandline to unprivileged processes.
679 chmod("/proc/cmdline", 0440);
680
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800681 // Get the basic filesystem setup we need put together in the initramdisk
682 // on / and then we'll let the rc file figure out the rest.
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700683 if (is_first_stage) {
Nick Kralevichf667a322015-04-25 17:42:52 -0700684 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
685 mkdir("/dev/pts", 0755);
686 mkdir("/dev/socket", 0755);
687 mount("devpts", "/dev/pts", "devpts", 0, NULL);
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800688 #define MAKE_STR(x) __STRING(x)
689 mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC));
Nick Kralevich80960d22016-10-29 12:20:00 -0700690 gid_t groups[] = { AID_READPROC };
691 setgroups(arraysize(groups), groups);
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700692 mount("sysfs", "/sys", "sysfs", 0, NULL);
Nick Kralevich3d9e2732016-03-03 10:40:12 -0800693 mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL);
Elliott Hughes35f5d042016-07-26 09:32:33 -0700694 mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11));
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700695 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700696
Elliott Hughes35f5d042016-07-26 09:32:33 -0700697 // Now that tmpfs is mounted on /dev and we have /dev/kmsg, we can actually
698 // talk to the outside world...
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700699 InitKernelLogging(argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700700
Wei Wang313b3522016-10-28 12:22:32 -0700701 if (is_first_stage) {
702 LOG(INFO) << "init first stage started!";
Elliott Hughesda40c002015-03-27 23:20:44 -0700703
Wei Wang313b3522016-10-28 12:22:32 -0700704 // Mount devices defined in android.early.* kernel commandline
705 early_mount();
706
707 // Set up SELinux, including loading the SELinux policy if we're in the kernel domain.
708 selinux_initialize(true);
709
710 // If we're in the kernel domain, re-exec init to transition to the init domain now
711 // that the SELinux policy has been loaded.
712
713 if (restorecon("/init") == -1) {
714 PLOG(ERROR) << "restorecon failed";
715 security_failure();
716 }
717 char* path = argv[0];
718 char* args[] = { path, const_cast<char*>("--second-stage"), nullptr };
719 if (execv(path, args) == -1) {
720 PLOG(ERROR) << "execv(\"" << path << "\") failed";
721 security_failure();
722 }
723
724 } else {
725 LOG(INFO) << "init second stage started!";
726
Nick Kralevichf667a322015-04-25 17:42:52 -0700727 // Indicate that booting is in progress to background fw loaders, etc.
728 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700729
Nick Kralevichf667a322015-04-25 17:42:52 -0700730 property_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800731
Nick Kralevichf667a322015-04-25 17:42:52 -0700732 // If arguments are passed both on the command line and in DT,
733 // properties set in DT always have priority over the command-line ones.
734 process_kernel_dt();
735 process_kernel_cmdline();
736
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700737 // Propagate the kernel variables to internal variables
Nick Kralevichf667a322015-04-25 17:42:52 -0700738 // used by init as well as the current required properties.
739 export_kernel_boot_props();
Rom Lemarchand6a524432015-02-28 06:39:11 -0800740
Wei Wang313b3522016-10-28 12:22:32 -0700741 // Now set up SELinux for second stage
742 selinux_initialize(false);
Elliott Hughesf65730e2015-04-24 18:38:17 +0000743 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800744
745 // These directories were necessarily created before initial policy load
746 // and therefore need their security context restored to the proper value.
747 // This must happen before /dev is populated by ueventd.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700748 LOG(INFO) << "Running restorecon...";
Stephen Smalleye096e362012-06-11 13:37:39 -0400749 restorecon("/dev");
Elliott Hughes35f5d042016-07-26 09:32:33 -0700750 restorecon("/dev/kmsg");
Stephen Smalleye096e362012-06-11 13:37:39 -0400751 restorecon("/dev/socket");
Geremy Condra8e15eab2013-02-28 17:29:58 -0800752 restorecon("/dev/__properties__");
Tom Cherrye36a85c2015-11-30 16:40:25 -0800753 restorecon("/property_contexts");
Nick Kralevichae76f6d2013-07-11 15:38:26 -0700754 restorecon_recursive("/sys");
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800755 restorecon_recursive("/dev/block");
756 restorecon("/dev/device-mapper");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500757
Elliott Hughes929f4072015-04-24 21:13:44 -0700758 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
759 if (epoll_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700760 PLOG(ERROR) << "epoll_create1 failed";
Elliott Hughes929f4072015-04-24 21:13:44 -0700761 exit(1);
762 }
763
764 signal_handler_init();
Elliott Hughes9042cae2015-04-24 17:43:21 -0700765
Riley Andrewse4b7b292014-06-16 15:06:21 -0700766 property_load_boot_defaults();
Sami Tolvanen9e9efca2015-12-07 12:33:58 +0000767 export_oem_lock_status();
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700768 start_property_service();
Amit Pundird2e74db2016-06-29 19:00:00 +0530769 set_usb_controller();
Dima Zavind7634c92011-12-16 14:18:06 -0800770
Tom Cherryb7349902015-08-26 11:43:36 -0700771 const BuiltinFunctionMap function_map;
772 Action::set_function_map(&function_map);
773
774 Parser& parser = Parser::GetInstance();
775 parser.AddSectionParser("service",std::make_unique<ServiceParser>());
776 parser.AddSectionParser("on", std::make_unique<ActionParser>());
777 parser.AddSectionParser("import", std::make_unique<ImportParser>());
778 parser.ParseConfig("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700779
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700780 ActionManager& am = ActionManager::GetInstance();
781
782 am.QueueEventTrigger("early-init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700783
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700784 // Queue an action that waits for coldboot done so we know ueventd has set up all of /dev...
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700785 am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700786 // ... so that we can start queuing up actions that require stuff from /dev.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700787 am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Daniel Cashmanf6112912016-03-25 23:49:05 +0000788 am.QueueBuiltinAction(set_mmap_rnd_bits_action, "set_mmap_rnd_bits");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700789 am.QueueBuiltinAction(keychord_init_action, "keychord_init");
790 am.QueueBuiltinAction(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791
Elliott Hughesda40c002015-03-27 23:20:44 -0700792 // Trigger all the boot actions to get us started.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700793 am.QueueEventTrigger("init");
Dima Zavinca47cef2011-08-24 15:28:23 -0700794
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800795 // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
796 // wasn't ready immediately after wait_for_coldboot_done
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700797 am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700798
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800799 // Don't mount filesystems or start core system services in charger mode.
Yabin Cui74edcea2015-07-24 10:11:05 -0700800 std::string bootmode = property_get("ro.bootmode");
801 if (bootmode == "charger") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700802 am.QueueEventTrigger("charger");
Dima Zavinca47cef2011-08-24 15:28:23 -0700803 } else {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700804 am.QueueEventTrigger("late-init");
Dima Zavinca47cef2011-08-24 15:28:23 -0700805 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800807 // Run all property triggers based on current state of the properties.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700808 am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700809
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700810 while (true) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800811 if (!waiting_for_exec) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700812 am.ExecuteOneCommand();
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800813 restart_processes();
814 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815
Yongqin Liua197ff12014-12-05 13:45:02 +0800816 int timeout = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817 if (process_needs_restart) {
818 timeout = (process_needs_restart - gettime()) * 1000;
819 if (timeout < 0)
820 timeout = 0;
821 }
822
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700823 if (am.HasMoreCommands()) {
Colin Crossebc6ff12010-04-13 19:52:01 -0700824 timeout = 0;
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800825 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700826
Yongqin Liua197ff12014-12-05 13:45:02 +0800827 bootchart_sample(&timeout);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800828
Elliott Hughes929f4072015-04-24 21:13:44 -0700829 epoll_event ev;
830 int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, timeout));
831 if (nr == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700832 PLOG(ERROR) << "epoll_wait failed";
Elliott Hughes929f4072015-04-24 21:13:44 -0700833 } else if (nr == 1) {
834 ((void (*)()) ev.data.ptr)();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700835 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700836 }
837
838 return 0;
839}