blob: 85065b39b606143ce9c56acd5640c1a35037f254 [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>
Elliott Hughes9605a942016-11-10 17:43:47 -080021#include <inttypes.h>
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
Elliott Hughes79f2b332016-11-10 19:22:18 -080053#include <fstream>
Rom Lemarchand6a524432015-02-28 06:39:11 -080054#include <memory>
55
Tom Cherryfa0c21c2015-07-23 17:53:11 -070056#include "action.h"
Tom Cherrybac32992015-07-31 12:45:25 -070057#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058#include "devices.h"
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080059#include "fs_mgr.h"
Tom Cherryb7349902015-08-26 11:43:36 -070060#include "import_parser.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061#include "init.h"
Tom Cherrybac32992015-07-31 12:45:25 -070062#include "init_parser.h"
63#include "keychords.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070064#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065#include "property_service.h"
Tom Cherrybac32992015-07-31 12:45:25 -070066#include "service.h"
Colin Cross9c5366b2010-04-13 19:48:59 -070067#include "signal_handler.h"
Colin Crossf83d0b92010-04-21 12:04:20 -070068#include "ueventd.h"
Tom Cherrybac32992015-07-31 12:45:25 -070069#include "util.h"
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070070#include "watchdogd.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071
Elliott Hughes9605a942016-11-10 17:43:47 -080072using android::base::StringPrintf;
73
Stephen Smalleye46f9d52012-01-13 08:48:47 -050074struct selabel_handle *sehandle;
rpcraig63207cd2012-08-09 10:05:49 -040075struct selabel_handle *sehandle_prop;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050076
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077static int property_triggers_enabled = 0;
78
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079static char qemu[32];
80
Viorel Suman70daa672016-03-21 10:08:07 +020081std::string default_console = "/dev/console";
Elliott Hughes9605a942016-11-10 17:43:47 -080082static time_t process_needs_restart_at;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083
Tom Cherrybac32992015-07-31 12:45:25 -070084const char *ENV[32];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085
Elliott Hughes8d82ea02015-02-06 20:15:18 -080086bool waiting_for_exec = false;
87
Elliott Hughes929f4072015-04-24 21:13:44 -070088static int epoll_fd = -1;
89
90void register_epoll_handler(int fd, void (*fn)()) {
91 epoll_event ev;
92 ev.events = EPOLLIN;
93 ev.data.ptr = reinterpret_cast<void*>(fn);
94 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070095 PLOG(ERROR) << "epoll_ctl failed";
Elliott Hughes929f4072015-04-24 21:13:44 -070096 }
97}
98
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070099/* add_environment - add "key=value" to the current environment */
100int add_environment(const char *key, const char *val)
101{
James Morrissey381341f2014-05-16 11:36:36 +0100102 size_t n;
103 size_t key_len = strlen(key);
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700104
James Morrissey381341f2014-05-16 11:36:36 +0100105 /* The last environment entry is reserved to terminate the list */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700106 for (n = 0; n < (arraysize(ENV) - 1); n++) {
James Morrissey381341f2014-05-16 11:36:36 +0100107
108 /* Delete any existing entry for this key */
109 if (ENV[n] != NULL) {
110 size_t entry_key_len = strcspn(ENV[n], "=");
111 if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
112 free((char*)ENV[n]);
113 ENV[n] = NULL;
114 }
115 }
116
117 /* Add entry if a free slot is available */
118 if (ENV[n] == NULL) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800119 char* entry;
120 asprintf(&entry, "%s=%s", key, val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700121 ENV[n] = entry;
122 return 0;
123 }
124 }
125
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700126 LOG(ERROR) << "No env. room to store: '" << key << "':'" << val << "'";
James Morrissey381341f2014-05-16 11:36:36 +0100127
128 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129}
130
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131void property_changed(const char *name, const char *value)
132{
Colin Crossebc6ff12010-04-13 19:52:01 -0700133 if (property_triggers_enabled)
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700134 ActionManager::GetInstance().QueuePropertyTrigger(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700135}
136
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137static void restart_processes()
138{
Elliott Hughes9605a942016-11-10 17:43:47 -0800139 process_needs_restart_at = 0;
140 ServiceManager::GetInstance().ForEachServiceWithFlags(SVC_RESTARTING, [](Service* s) {
141 s->RestartIfNeeded(&process_needs_restart_at);
142 });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143}
144
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700145void handle_control_message(const std::string& msg, const std::string& name) {
Tom Cherrybac32992015-07-31 12:45:25 -0700146 Service* svc = ServiceManager::GetInstance().FindServiceByName(name);
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700147 if (svc == nullptr) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700148 LOG(ERROR) << "no such service '" << name << "'";
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700149 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700151
Tom Cherrybac32992015-07-31 12:45:25 -0700152 if (msg == "start") {
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700153 svc->Start();
Tom Cherrybac32992015-07-31 12:45:25 -0700154 } else if (msg == "stop") {
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700155 svc->Stop();
Tom Cherrybac32992015-07-31 12:45:25 -0700156 } else if (msg == "restart") {
Elliott Hughesa3cc6022016-04-12 15:38:27 -0700157 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700158 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700159 LOG(ERROR) << "unknown control msg '" << msg << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700160 }
161}
162
Tom Cherry96f67312015-07-30 13:52:55 -0700163static int wait_for_coldboot_done_action(const std::vector<std::string>& args) {
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700164 Timer t;
165
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700166 LOG(VERBOSE) << "Waiting for " COLDBOOT_DONE "...";
Elliott Hughesc7331d02015-05-14 12:03:14 -0700167 // Any longer than 1s is an unreasonable length of time to delay booting.
168 // If you're hitting this timeout, check that you didn't make your
169 // sepolicy regular expressions too expensive (http://b/19899875).
Elliott Hughesa4526602016-11-29 16:34:56 +0000170 if (wait_for_file(COLDBOOT_DONE, 1s)) {
171 LOG(ERROR) << "Timed out waiting for " COLDBOOT_DONE;
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700172 }
173
Elliott Hughesa4526602016-11-29 16:34:56 +0000174 LOG(VERBOSE) << "Waiting for " COLDBOOT_DONE " took " << t.duration() << "s.";
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700175 return 0;
Colin Crossebc6ff12010-04-13 19:52:01 -0700176}
177
Alex Klyubin0d872d82013-08-16 13:19:24 -0700178/*
179 * Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
180 * by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
181 * Does nothing if Hardware RNG is not present.
182 *
183 * Since we don't yet trust the quality of Hardware RNG, these bytes are not
184 * mixed into the primary pool of Linux RNG and the entropy estimate is left
185 * unmodified.
186 *
187 * If the HW RNG device /dev/hw_random is present, we require that at least
188 * 512 bytes read from it are written into Linux RNG. QA is expected to catch
189 * devices/configurations where these I/O operations are blocking for a long
190 * time. We do not reboot or halt on failures, as this is a best-effort
191 * attempt.
192 */
Tom Cherry96f67312015-07-30 13:52:55 -0700193static int mix_hwrng_into_linux_rng_action(const std::vector<std::string>& args)
Alex Klyubin0d872d82013-08-16 13:19:24 -0700194{
195 int result = -1;
196 int hwrandom_fd = -1;
197 int urandom_fd = -1;
198 char buf[512];
199 ssize_t chunk_size;
200 size_t total_bytes_written = 0;
201
202 hwrandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800203 open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700204 if (hwrandom_fd == -1) {
205 if (errno == ENOENT) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700206 LOG(ERROR) << "/dev/hw_random not found";
207 // It's not an error to not have a Hardware RNG.
208 result = 0;
Alex Klyubin0d872d82013-08-16 13:19:24 -0700209 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700210 PLOG(ERROR) << "Failed to open /dev/hw_random";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700211 }
212 goto ret;
213 }
214
215 urandom_fd = TEMP_FAILURE_RETRY(
Nick Kralevich45a884f2015-02-02 14:37:22 -0800216 open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
Alex Klyubin0d872d82013-08-16 13:19:24 -0700217 if (urandom_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700218 PLOG(ERROR) << "Failed to open /dev/urandom";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700219 goto ret;
220 }
221
222 while (total_bytes_written < sizeof(buf)) {
223 chunk_size = TEMP_FAILURE_RETRY(
224 read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
225 if (chunk_size == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700226 PLOG(ERROR) << "Failed to read from /dev/hw_random";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700227 goto ret;
228 } else if (chunk_size == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700229 LOG(ERROR) << "Failed to read from /dev/hw_random: EOF";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700230 goto ret;
231 }
232
233 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
234 if (chunk_size == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700235 PLOG(ERROR) << "Failed to write to /dev/urandom";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700236 goto ret;
237 }
238 total_bytes_written += chunk_size;
239 }
240
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700241 LOG(INFO) << "Mixed " << total_bytes_written << " bytes from /dev/hw_random into /dev/urandom";
Alex Klyubin0d872d82013-08-16 13:19:24 -0700242 result = 0;
243
244ret:
245 if (hwrandom_fd != -1) {
246 close(hwrandom_fd);
247 }
248 if (urandom_fd != -1) {
249 close(urandom_fd);
250 }
Alex Klyubin0d872d82013-08-16 13:19:24 -0700251 return result;
252}
253
Daniel Cashmanf6112912016-03-25 23:49:05 +0000254static void security_failure() {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700255 LOG(ERROR) << "Security failure; rebooting into recovery mode...";
Daniel Cashmanf6112912016-03-25 23:49:05 +0000256 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
257 while (true) { pause(); } // never reached
258}
259
260#define MMAP_RND_PATH "/proc/sys/vm/mmap_rnd_bits"
261#define MMAP_RND_COMPAT_PATH "/proc/sys/vm/mmap_rnd_compat_bits"
262
263/* __attribute__((unused)) due to lack of mips support: see mips block
264 * in set_mmap_rnd_bits_action */
265static bool __attribute__((unused)) set_mmap_rnd_bits_min(int start, int min, bool compat) {
266 std::string path;
267 if (compat) {
268 path = MMAP_RND_COMPAT_PATH;
269 } else {
270 path = MMAP_RND_PATH;
271 }
272 std::ifstream inf(path, std::fstream::in);
273 if (!inf) {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700274 LOG(ERROR) << "Cannot open for reading: " << path;
Daniel Cashmanf6112912016-03-25 23:49:05 +0000275 return false;
276 }
277 while (start >= min) {
278 // try to write out new value
279 std::string str_val = std::to_string(start);
280 std::ofstream of(path, std::fstream::out);
281 if (!of) {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700282 LOG(ERROR) << "Cannot open for writing: " << path;
Daniel Cashmanf6112912016-03-25 23:49:05 +0000283 return false;
284 }
285 of << str_val << std::endl;
286 of.close();
287
288 // check to make sure it was recorded
289 inf.seekg(0);
290 std::string str_rec;
291 inf >> str_rec;
292 if (str_val.compare(str_rec) == 0) {
293 break;
294 }
295 start--;
296 }
297 inf.close();
298 if (start < min) {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700299 LOG(ERROR) << "Unable to set minimum required entropy " << min << " in " << path;
Daniel Cashmanf6112912016-03-25 23:49:05 +0000300 return false;
301 }
302 return true;
303}
304
305/*
306 * Set /proc/sys/vm/mmap_rnd_bits and potentially
307 * /proc/sys/vm/mmap_rnd_compat_bits to the maximum supported values.
308 * Returns -1 if unable to set these to an acceptable value.
309 *
310 * To support this sysctl, the following upstream commits are needed:
311 *
312 * d07e22597d1d mm: mmap: add new /proc tunable for mmap_base ASLR
313 * e0c25d958f78 arm: mm: support ARCH_MMAP_RND_BITS
314 * 8f0d3aa9de57 arm64: mm: support ARCH_MMAP_RND_BITS
315 * 9e08f57d684a x86: mm: support ARCH_MMAP_RND_BITS
316 * ec9ee4acd97c drivers: char: random: add get_random_long()
317 * 5ef11c35ce86 mm: ASLR: use get_random_long()
318 */
319static int set_mmap_rnd_bits_action(const std::vector<std::string>& args)
320{
321 int ret = -1;
322
323 /* values are arch-dependent */
324#if defined(__aarch64__)
325 /* arm64 supports 18 - 33 bits depending on pagesize and VA_SIZE */
326 if (set_mmap_rnd_bits_min(33, 24, false)
327 && set_mmap_rnd_bits_min(16, 16, true)) {
328 ret = 0;
329 }
330#elif defined(__x86_64__)
331 /* x86_64 supports 28 - 32 bits */
332 if (set_mmap_rnd_bits_min(32, 32, false)
333 && set_mmap_rnd_bits_min(16, 16, true)) {
334 ret = 0;
335 }
336#elif defined(__arm__) || defined(__i386__)
337 /* check to see if we're running on 64-bit kernel */
338 bool h64 = !access(MMAP_RND_COMPAT_PATH, F_OK);
339 /* supported 32-bit architecture must have 16 bits set */
340 if (set_mmap_rnd_bits_min(16, 16, h64)) {
341 ret = 0;
342 }
343#elif defined(__mips__) || defined(__mips64__)
344 // TODO: add mips support b/27788820
345 ret = 0;
346#else
347 ERROR("Unknown architecture\n");
348#endif
349
350#ifdef __BRILLO__
351 // TODO: b/27794137
352 ret = 0;
353#endif
354 if (ret == -1) {
Elliott Hughes4dc40842016-06-27 10:49:57 -0700355 LOG(ERROR) << "Unable to set adequate mmap entropy value!";
Daniel Cashmanf6112912016-03-25 23:49:05 +0000356 security_failure();
357 }
358 return ret;
359}
360
Tom Cherry96f67312015-07-30 13:52:55 -0700361static int keychord_init_action(const std::vector<std::string>& args)
Colin Crossebc6ff12010-04-13 19:52:01 -0700362{
363 keychord_init();
364 return 0;
365}
366
Tom Cherry96f67312015-07-30 13:52:55 -0700367static int console_init_action(const std::vector<std::string>& args)
Colin Crossebc6ff12010-04-13 19:52:01 -0700368{
Viorel Sumanefc72752016-03-21 10:23:43 +0200369 std::string console = property_get("ro.boot.console");
370 if (!console.empty()) {
Viorel Suman70daa672016-03-21 10:08:07 +0200371 default_console = "/dev/" + console;
Colin Crossebc6ff12010-04-13 19:52:01 -0700372 }
Colin Crossebc6ff12010-04-13 19:52:01 -0700373 return 0;
374}
375
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700376static void import_kernel_nv(const std::string& key, const std::string& value, bool for_emulator) {
377 if (key.empty()) return;
Dima Zavin5511c842011-12-19 11:21:32 -0800378
379 if (for_emulator) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700380 // In the emulator, export any kernel option with the "ro.kernel." prefix.
Elliott Hughes9605a942016-11-10 17:43:47 -0800381 property_set(StringPrintf("ro.kernel.%s", key.c_str()).c_str(), value.c_str());
Dima Zavin5511c842011-12-19 11:21:32 -0800382 return;
383 }
384
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700385 if (key == "qemu") {
386 strlcpy(qemu, value.c_str(), sizeof(qemu));
387 } else if (android::base::StartsWith(key, "androidboot.")) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800388 property_set(StringPrintf("ro.boot.%s", key.c_str() + 12).c_str(), value.c_str());
Dima Zavin5511c842011-12-19 11:21:32 -0800389 }
390}
391
Sami Tolvanen9e9efca2015-12-07 12:33:58 +0000392static void export_oem_lock_status() {
393 if (property_get("ro.oem_unlock_supported") != "1") {
394 return;
395 }
396
397 std::string value = property_get("ro.boot.verifiedbootstate");
398
399 if (!value.empty()) {
400 property_set("ro.boot.flash.locked", value == "orange" ? "0" : "1");
401 }
402}
403
Elliott Hughesd679bc92015-03-20 08:50:46 -0700404static void export_kernel_boot_props() {
Dima Zavin5511c842011-12-19 11:21:32 -0800405 struct {
406 const char *src_prop;
Elliott Hughesd679bc92015-03-20 08:50:46 -0700407 const char *dst_prop;
408 const char *default_value;
Dima Zavin5511c842011-12-19 11:21:32 -0800409 } prop_map[] = {
Elliott Hughesd679bc92015-03-20 08:50:46 -0700410 { "ro.boot.serialno", "ro.serialno", "", },
411 { "ro.boot.mode", "ro.bootmode", "unknown", },
412 { "ro.boot.baseband", "ro.baseband", "unknown", },
Dima Zavin5511c842011-12-19 11:21:32 -0800413 { "ro.boot.bootloader", "ro.bootloader", "unknown", },
Elliott Hughesd679bc92015-03-20 08:50:46 -0700414 { "ro.boot.hardware", "ro.hardware", "unknown", },
415 { "ro.boot.revision", "ro.revision", "0", },
Dima Zavin5511c842011-12-19 11:21:32 -0800416 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700417 for (size_t i = 0; i < arraysize(prop_map); i++) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700418 std::string value = property_get(prop_map[i].src_prop);
419 property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value);
Dima Zavin5511c842011-12-19 11:21:32 -0800420 }
Dima Zavin5511c842011-12-19 11:21:32 -0800421}
Colin Crossebc6ff12010-04-13 19:52:01 -0700422
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700423static void process_kernel_dt() {
Rom Lemarchand6a524432015-02-28 06:39:11 -0800424 static const char android_dir[] = "/proc/device-tree/firmware/android";
425
Elliott Hughes9605a942016-11-10 17:43:47 -0800426 std::string file_name = StringPrintf("%s/compatible", android_dir);
Rom Lemarchand6a524432015-02-28 06:39:11 -0800427
428 std::string dt_file;
429 android::base::ReadFileToString(file_name, &dt_file);
430 if (!dt_file.compare("android,firmware")) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700431 LOG(ERROR) << "firmware/android is not compatible with 'android,firmware'";
Rom Lemarchand6a524432015-02-28 06:39:11 -0800432 return;
433 }
434
435 std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir);
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700436 if (!dir) return;
Rom Lemarchand6a524432015-02-28 06:39:11 -0800437
438 struct dirent *dp;
439 while ((dp = readdir(dir.get())) != NULL) {
Rom Lemarchand1dcf3252015-11-08 17:51:50 -0800440 if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible") || !strcmp(dp->d_name, "name")) {
Rom Lemarchand6a524432015-02-28 06:39:11 -0800441 continue;
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700442 }
Rom Lemarchand6a524432015-02-28 06:39:11 -0800443
Elliott Hughes9605a942016-11-10 17:43:47 -0800444 file_name = StringPrintf("%s/%s", android_dir, dp->d_name);
Rom Lemarchand6a524432015-02-28 06:39:11 -0800445
446 android::base::ReadFileToString(file_name, &dt_file);
447 std::replace(dt_file.begin(), dt_file.end(), ',', '.');
448
Elliott Hughes9605a942016-11-10 17:43:47 -0800449 std::string property_name = StringPrintf("ro.boot.%s", dp->d_name);
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700450 property_set(property_name.c_str(), dt_file.c_str());
Rom Lemarchand6a524432015-02-28 06:39:11 -0800451 }
452}
453
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700454static void process_kernel_cmdline() {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700455 // The first pass does the common stuff, and finds if we are in qemu.
456 // The second pass is only necessary for qemu to export all kernel params
457 // as properties.
Nick Kralevichf667a322015-04-25 17:42:52 -0700458 import_kernel_cmdline(false, import_kernel_nv);
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700459 if (qemu[0]) import_kernel_cmdline(true, import_kernel_nv);
Colin Crossebc6ff12010-04-13 19:52:01 -0700460}
461
Tom Cherry96f67312015-07-30 13:52:55 -0700462static int queue_property_triggers_action(const std::vector<std::string>& args)
Colin Crossebc6ff12010-04-13 19:52:01 -0700463{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700464 ActionManager::GetInstance().QueueAllPropertyTriggers();
Colin Crossebc6ff12010-04-13 19:52:01 -0700465 /* enable property triggers */
466 property_triggers_enabled = 1;
467 return 0;
468}
469
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700470static void selinux_init_all_handles(void)
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500471{
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400472 sehandle = selinux_android_file_context_handle();
Stephen Smalleydbd37f22014-01-28 10:34:09 -0500473 selinux_android_set_sehandle(sehandle);
rpcraig63207cd2012-08-09 10:05:49 -0400474 sehandle_prop = selinux_android_prop_context_handle();
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400475}
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500476
Nick Kralevichd34e4072015-04-28 12:39:41 -0700477enum selinux_enforcing_status { SELINUX_PERMISSIVE, SELINUX_ENFORCING };
Nick Kralevichf667a322015-04-25 17:42:52 -0700478
479static selinux_enforcing_status selinux_status_from_cmdline() {
480 selinux_enforcing_status status = SELINUX_ENFORCING;
481
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700482 import_kernel_cmdline(false, [&](const std::string& key, const std::string& value, bool in_qemu) {
483 if (key == "androidboot.selinux" && value == "permissive") {
484 status = SELINUX_PERMISSIVE;
Nick Kralevichf667a322015-04-25 17:42:52 -0700485 }
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700486 });
Nick Kralevichf667a322015-04-25 17:42:52 -0700487
488 return status;
489}
490
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700491static bool selinux_is_enforcing(void)
492{
Nick Kralevichd34e4072015-04-28 12:39:41 -0700493 if (ALLOW_PERMISSIVE_SELINUX) {
Nick Kralevichf667a322015-04-25 17:42:52 -0700494 return selinux_status_from_cmdline() == SELINUX_ENFORCING;
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700495 }
Nick Kralevich935bd3e2013-06-26 15:37:26 -0700496 return true;
497}
498
Elliott Hughesda40c002015-03-27 23:20:44 -0700499static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
William Robertsd7aea442015-10-01 16:03:47 -0700500
501 property_audit_data *d = reinterpret_cast<property_audit_data*>(data);
502
503 if (!d || !d->name || !d->cr) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700504 LOG(ERROR) << "audit_callback invoked with null data arguments!";
William Robertsd7aea442015-10-01 16:03:47 -0700505 return 0;
506 }
507
508 snprintf(buf, len, "property=%s pid=%d uid=%d gid=%d", d->name,
509 d->cr->pid, d->cr->uid, d->cr->gid);
rpcraig63207cd2012-08-09 10:05:49 -0400510 return 0;
511}
512
Elliott Hughesf65730e2015-04-24 18:38:17 +0000513static void selinux_initialize(bool in_kernel_domain) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700514 Timer t;
Stephen Smalleyeb3f4212014-02-12 16:17:00 -0500515
Elliott Hughesda40c002015-03-27 23:20:44 -0700516 selinux_callback cb;
517 cb.func_log = selinux_klog_callback;
518 selinux_set_callback(SELINUX_CB_LOG, cb);
519 cb.func_audit = audit_callback;
520 selinux_set_callback(SELINUX_CB_AUDIT, cb);
521
Elliott Hughesf65730e2015-04-24 18:38:17 +0000522 if (in_kernel_domain) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700523 LOG(INFO) << "Loading SELinux policy...";
Elliott Hughesf65730e2015-04-24 18:38:17 +0000524 if (selinux_android_load_policy() < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700525 PLOG(ERROR) << "failed to load policy";
Elliott Hughesf65730e2015-04-24 18:38:17 +0000526 security_failure();
527 }
528
Nick Kralevichd34e4072015-04-28 12:39:41 -0700529 bool kernel_enforcing = (security_getenforce() == 1);
Elliott Hughesf65730e2015-04-24 18:38:17 +0000530 bool is_enforcing = selinux_is_enforcing();
Nick Kralevichd34e4072015-04-28 12:39:41 -0700531 if (kernel_enforcing != is_enforcing) {
532 if (security_setenforce(is_enforcing)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700533 PLOG(ERROR) << "security_setenforce(%s) failed" << (is_enforcing ? "true" : "false");
Nick Kralevichd34e4072015-04-28 12:39:41 -0700534 security_failure();
535 }
536 }
Elliott Hughesf65730e2015-04-24 18:38:17 +0000537
Nick Kralevicheedbe812015-04-25 14:10:03 -0700538 if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) {
539 security_failure();
540 }
541
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700542 LOG(INFO) << "(Initializing SELinux " << (is_enforcing ? "enforcing" : "non-enforcing")
543 << " took " << t.duration() << "s.)";
Elliott Hughesf65730e2015-04-24 18:38:17 +0000544 } else {
545 selinux_init_all_handles();
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700546 }
Nick Kralevich56fa0ac2013-06-24 17:41:40 -0700547}
548
Amit Pundird2e74db2016-06-29 19:00:00 +0530549// Set the UDC controller for the ConfigFS USB Gadgets.
550// Read the UDC controller in use from "/sys/class/udc".
551// In case of multiple UDC controllers select the first one.
552static void set_usb_controller() {
553 std::unique_ptr<DIR, decltype(&closedir)>dir(opendir("/sys/class/udc"), closedir);
554 if (!dir) return;
555
556 dirent* dp;
557 while ((dp = readdir(dir.get())) != nullptr) {
558 if (dp->d_name[0] == '.') continue;
559
560 property_set("sys.usb.controller", dp->d_name);
561 break;
562 }
563}
564
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800565/* Returns a new path consisting of base_path and the file name in reference_path. */
566static std::string get_path(const std::string& base_path, const std::string& reference_path) {
567 std::string::size_type pos = reference_path.rfind('/');
568 if (pos == std::string::npos) {
569 return base_path + '/' + reference_path;
570 } else {
571 return base_path + reference_path.substr(pos);
572 }
573}
574
575/* Imports the fstab info from cmdline. */
576static std::string import_cmdline_fstab() {
577 std::string prefix, fstab, fstab_full;
578
579 import_kernel_cmdline(false,
580 [&](const std::string& key, const std::string& value, bool in_qemu __attribute__((__unused__))) {
581 if (key == "android.early.prefix") {
582 prefix = value;
583 } else if (key == "android.early.fstab") {
584 fstab = value;
585 }
586 });
587 if (!fstab.empty()) {
588 // Convert "mmcblk0p09+/odm+ext4+ro+verify" to "mmcblk0p09 /odm ext4 ro verify"
589 std::replace(fstab.begin(), fstab.end(), '+', ' ');
590 for (const auto& entry : android::base::Split(fstab, "\n")) {
591 fstab_full += prefix + entry + '\n';
592 }
593 }
594 return fstab_full;
595}
596
597/* Early mount vendor and ODM partitions. The fstab info is read from kernel cmdline. */
598static void early_mount() {
599 std::string fstab_string = import_cmdline_fstab();
600 if (fstab_string.empty()) {
601 LOG(INFO) << "Failed to load vendor fstab from kernel cmdline";
602 return;
603 }
604 FILE *fstab_file = fmemopen((void *)fstab_string.c_str(), fstab_string.length(), "r");
605 if (!fstab_file) {
606 PLOG(ERROR) << "Failed to open fstab string as FILE";
607 return;
608 }
609 std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_file(fstab_file), fs_mgr_free_fstab);
610 fclose(fstab_file);
611 if (!fstab) {
612 LOG(ERROR) << "Failed to parse fstab string: " << fstab_string;
613 return;
614 }
615 LOG(INFO) << "Loaded vendor fstab from cmdline";
616
617 if (early_device_socket_open()) {
618 LOG(ERROR) << "Failed to open device uevent socket";
619 return;
620 }
621
622 /* Create /dev/device-mapper for dm-verity */
623 early_create_dev("/sys/devices/virtual/misc/device-mapper", EARLY_CHAR_DEV);
624
625 for (int i = 0; i < fstab->num_entries; ++i) {
626 struct fstab_rec *rec = &fstab->recs[i];
627 std::string mount_point = rec->mount_point;
628 std::string syspath = rec->blk_device;
629
630 if (mount_point != "/vendor" && mount_point != "/odm")
631 continue;
632
633 /* Create mount target under /dev/block/ from sysfs via uevent */
634 LOG(INFO) << "Mounting " << mount_point << " from " << syspath << "...";
635 char *devpath = strdup(get_path("/dev/block", syspath).c_str());
636 if (!devpath) {
637 PLOG(ERROR) << "Failed to strdup dev path in early mount " << syspath;
638 continue;
639 }
640 rec->blk_device = devpath;
641 early_create_dev(syspath, EARLY_BLOCK_DEV);
642
643 int rc = fs_mgr_early_setup_verity(rec);
644 if (rc == FS_MGR_EARLY_SETUP_VERITY_SUCCESS) {
645 /* Mount target is changed to /dev/block/dm-<n>; initiate its creation from sysfs counterpart */
646 early_create_dev(get_path("/sys/devices/virtual/block", rec->blk_device), EARLY_BLOCK_DEV);
647 } else if (rc == FS_MGR_EARLY_SETUP_VERITY_FAIL) {
648 LOG(ERROR) << "Failed to set up dm-verity on " << rec->blk_device;
649 continue;
650 } else { /* FS_MGR_EARLY_SETUP_VERITY_NO_VERITY */
651 LOG(INFO) << "dm-verity disabled on debuggable device; mount directly on " << rec->blk_device;
652 }
653
654 mkdir(mount_point.c_str(), 0755);
655 rc = mount(rec->blk_device, mount_point.c_str(), rec->fs_type, rec->flags, rec->fs_options);
656 if (rc) {
657 PLOG(ERROR) << "Failed to mount on " << rec->blk_device;
658 }
659 }
660 early_device_socket_close();
661}
662
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800663int main(int argc, char** argv) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700664 if (!strcmp(basename(argv[0]), "ueventd")) {
Colin Crossf83d0b92010-04-21 12:04:20 -0700665 return ueventd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700666 }
Colin Crossf83d0b92010-04-21 12:04:20 -0700667
Elliott Hughesda40c002015-03-27 23:20:44 -0700668 if (!strcmp(basename(argv[0]), "watchdogd")) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700669 return watchdogd_main(argc, argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700670 }
Arve Hjønnevågd97d9072012-06-13 21:51:56 -0700671
Elliott Hughes9605a942016-11-10 17:43:47 -0800672 boot_clock::time_point start_time = boot_clock::now();
673
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800674 // Clear the umask.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700675 umask(0);
676
Elliott Hughescc86fb22015-03-17 20:01:13 -0700677 add_environment("PATH", _PATH_DEFPATH);
678
Elliott Hughes9605a942016-11-10 17:43:47 -0800679 bool is_first_stage = (getenv("INIT_SECOND_STAGE") == nullptr);
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700680
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800681 // Don't expose the raw commandline to unprivileged processes.
682 chmod("/proc/cmdline", 0440);
683
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800684 // Get the basic filesystem setup we need put together in the initramdisk
685 // on / and then we'll let the rc file figure out the rest.
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700686 if (is_first_stage) {
Nick Kralevichf667a322015-04-25 17:42:52 -0700687 mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
688 mkdir("/dev/pts", 0755);
689 mkdir("/dev/socket", 0755);
690 mount("devpts", "/dev/pts", "devpts", 0, NULL);
Nick Kralevichc39ba5a2015-11-07 16:52:17 -0800691 #define MAKE_STR(x) __STRING(x)
692 mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC));
Nick Kralevich80960d22016-10-29 12:20:00 -0700693 gid_t groups[] = { AID_READPROC };
694 setgroups(arraysize(groups), groups);
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700695 mount("sysfs", "/sys", "sysfs", 0, NULL);
Nick Kralevich3d9e2732016-03-03 10:40:12 -0800696 mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL);
Elliott Hughes35f5d042016-07-26 09:32:33 -0700697 mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11));
Evgenii Stepanov102daa32016-11-18 14:58:40 -0800698 mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8));
699 mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9));
Nick Kralevich9dec93b2015-04-25 15:50:03 -0700700 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700701
Elliott Hughes35f5d042016-07-26 09:32:33 -0700702 // Now that tmpfs is mounted on /dev and we have /dev/kmsg, we can actually
703 // talk to the outside world...
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700704 InitKernelLogging(argv);
Elliott Hughesda40c002015-03-27 23:20:44 -0700705
Elliott Hughes9605a942016-11-10 17:43:47 -0800706 LOG(INFO) << "init " << (is_first_stage ? "first" : "second") << " stage started!";
Elliott Hughesda40c002015-03-27 23:20:44 -0700707
Elliott Hughes9605a942016-11-10 17:43:47 -0800708 if (is_first_stage) {
Wei Wang313b3522016-10-28 12:22:32 -0700709 // Mount devices defined in android.early.* kernel commandline
710 early_mount();
711
Elliott Hughes9605a942016-11-10 17:43:47 -0800712 // Set up SELinux, loading the SELinux policy.
Wei Wang313b3522016-10-28 12:22:32 -0700713 selinux_initialize(true);
714
Elliott Hughes9605a942016-11-10 17:43:47 -0800715 // We're in the kernel domain, so re-exec init to transition to the init domain now
Wei Wang313b3522016-10-28 12:22:32 -0700716 // that the SELinux policy has been loaded.
Wei Wang313b3522016-10-28 12:22:32 -0700717 if (restorecon("/init") == -1) {
718 PLOG(ERROR) << "restorecon failed";
719 security_failure();
720 }
Elliott Hughes9605a942016-11-10 17:43:47 -0800721
722 setenv("INIT_SECOND_STAGE", "true", 1);
723
724 uint64_t start_ns = start_time.time_since_epoch().count();
725 setenv("INIT_STARTED_AT", StringPrintf("%" PRIu64, start_ns).c_str(), 1);
726
Wei Wang313b3522016-10-28 12:22:32 -0700727 char* path = argv[0];
Elliott Hughes9605a942016-11-10 17:43:47 -0800728 char* args[] = { path, nullptr };
Wei Wang313b3522016-10-28 12:22:32 -0700729 if (execv(path, args) == -1) {
730 PLOG(ERROR) << "execv(\"" << path << "\") failed";
731 security_failure();
732 }
Wei Wang313b3522016-10-28 12:22:32 -0700733 } else {
Nick Kralevichf667a322015-04-25 17:42:52 -0700734 // Indicate that booting is in progress to background fw loaders, etc.
735 close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
Vladimir Chtchetkine2b995432011-09-28 09:55:31 -0700736
Nick Kralevichf667a322015-04-25 17:42:52 -0700737 property_init();
Dima Zavin5511c842011-12-19 11:21:32 -0800738
Nick Kralevichf667a322015-04-25 17:42:52 -0700739 // If arguments are passed both on the command line and in DT,
740 // properties set in DT always have priority over the command-line ones.
741 process_kernel_dt();
742 process_kernel_cmdline();
743
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700744 // Propagate the kernel variables to internal variables
Nick Kralevichf667a322015-04-25 17:42:52 -0700745 // used by init as well as the current required properties.
746 export_kernel_boot_props();
Rom Lemarchand6a524432015-02-28 06:39:11 -0800747
Elliott Hughes9605a942016-11-10 17:43:47 -0800748 // Make the time that init started available for bootstat to log.
749 property_set("init.start", getenv("INIT_STARTED_AT"));
750
751 // Now set up SELinux for second stage.
Wei Wang313b3522016-10-28 12:22:32 -0700752 selinux_initialize(false);
Elliott Hughesf65730e2015-04-24 18:38:17 +0000753 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800754
755 // These directories were necessarily created before initial policy load
756 // and therefore need their security context restored to the proper value.
757 // This must happen before /dev is populated by ueventd.
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700758 LOG(INFO) << "Running restorecon...";
Stephen Smalleye096e362012-06-11 13:37:39 -0400759 restorecon("/dev");
Elliott Hughes35f5d042016-07-26 09:32:33 -0700760 restorecon("/dev/kmsg");
Stephen Smalleye096e362012-06-11 13:37:39 -0400761 restorecon("/dev/socket");
Evgenii Stepanov102daa32016-11-18 14:58:40 -0800762 restorecon("/dev/random");
763 restorecon("/dev/urandom");
Geremy Condra8e15eab2013-02-28 17:29:58 -0800764 restorecon("/dev/__properties__");
Tom Cherrye36a85c2015-11-30 16:40:25 -0800765 restorecon("/property_contexts");
Paul Lawrencea8d84342016-11-14 15:40:18 -0800766 restorecon("/sys", SELINUX_ANDROID_RESTORECON_RECURSE);
767 restorecon("/dev/block", SELINUX_ANDROID_RESTORECON_RECURSE);
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800768 restorecon("/dev/device-mapper");
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500769
Elliott Hughes929f4072015-04-24 21:13:44 -0700770 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
771 if (epoll_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700772 PLOG(ERROR) << "epoll_create1 failed";
Elliott Hughes929f4072015-04-24 21:13:44 -0700773 exit(1);
774 }
775
776 signal_handler_init();
Elliott Hughes9042cae2015-04-24 17:43:21 -0700777
Riley Andrewse4b7b292014-06-16 15:06:21 -0700778 property_load_boot_defaults();
Sami Tolvanen9e9efca2015-12-07 12:33:58 +0000779 export_oem_lock_status();
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700780 start_property_service();
Amit Pundird2e74db2016-06-29 19:00:00 +0530781 set_usb_controller();
Dima Zavind7634c92011-12-16 14:18:06 -0800782
Tom Cherryb7349902015-08-26 11:43:36 -0700783 const BuiltinFunctionMap function_map;
784 Action::set_function_map(&function_map);
785
786 Parser& parser = Parser::GetInstance();
787 parser.AddSectionParser("service",std::make_unique<ServiceParser>());
788 parser.AddSectionParser("on", std::make_unique<ActionParser>());
789 parser.AddSectionParser("import", std::make_unique<ImportParser>());
790 parser.ParseConfig("/init.rc");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700792 ActionManager& am = ActionManager::GetInstance();
793
794 am.QueueEventTrigger("early-init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700796 // 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 -0700797 am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done");
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700798 // ... so that we can start queuing up actions that require stuff from /dev.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700799 am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
Daniel Cashmanf6112912016-03-25 23:49:05 +0000800 am.QueueBuiltinAction(set_mmap_rnd_bits_action, "set_mmap_rnd_bits");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700801 am.QueueBuiltinAction(keychord_init_action, "keychord_init");
802 am.QueueBuiltinAction(console_init_action, "console_init");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803
Elliott Hughesda40c002015-03-27 23:20:44 -0700804 // Trigger all the boot actions to get us started.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700805 am.QueueEventTrigger("init");
Dima Zavinca47cef2011-08-24 15:28:23 -0700806
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800807 // Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
808 // wasn't ready immediately after wait_for_coldboot_done
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700809 am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700810
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800811 // Don't mount filesystems or start core system services in charger mode.
Yabin Cui74edcea2015-07-24 10:11:05 -0700812 std::string bootmode = property_get("ro.bootmode");
813 if (bootmode == "charger") {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700814 am.QueueEventTrigger("charger");
Dima Zavinca47cef2011-08-24 15:28:23 -0700815 } else {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700816 am.QueueEventTrigger("late-init");
Dima Zavinca47cef2011-08-24 15:28:23 -0700817 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700818
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800819 // Run all property triggers based on current state of the properties.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700820 am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821
Elliott Hughesc6c26ed2015-04-24 18:50:30 -0700822 while (true) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800823 if (!waiting_for_exec) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700824 am.ExecuteOneCommand();
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800825 restart_processes();
826 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827
Elliott Hughes9605a942016-11-10 17:43:47 -0800828 // By default, sleep until something happens.
829 int epoll_timeout_ms = -1;
830
831 // If there's more work to do, wake up again immediately.
832 if (am.HasMoreCommands()) epoll_timeout_ms = 0;
833
834 // If there's a process that needs restarting, wake up in time for that.
835 if (process_needs_restart_at != 0) {
836 epoll_timeout_ms = (process_needs_restart_at - time(nullptr)) * 1000;
837 if (epoll_timeout_ms < 0) epoll_timeout_ms = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838 }
839
Elliott Hughes9605a942016-11-10 17:43:47 -0800840 bootchart_sample(&epoll_timeout_ms);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800841
Elliott Hughes929f4072015-04-24 21:13:44 -0700842 epoll_event ev;
Elliott Hughes9605a942016-11-10 17:43:47 -0800843 int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, epoll_timeout_ms));
Elliott Hughes929f4072015-04-24 21:13:44 -0700844 if (nr == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700845 PLOG(ERROR) << "epoll_wait failed";
Elliott Hughes929f4072015-04-24 21:13:44 -0700846 } else if (nr == 1) {
847 ((void (*)()) ev.data.ptr)();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700848 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700849 }
850
851 return 0;
852}