2008-10-21 22:00:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-02-07 12:15:18 +08:00
|
|
|
#include <ctype.h>
|
2015-02-28 22:39:11 +08:00
|
|
|
#include <dirent.h>
|
2015-02-07 12:15:18 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2016-03-26 07:49:05 +08:00
|
|
|
#include <fstream>
|
2016-11-11 09:43:47 +08:00
|
|
|
#include <inttypes.h>
|
2015-02-07 12:15:18 +08:00
|
|
|
#include <libgen.h>
|
2015-03-18 11:01:13 +08:00
|
|
|
#include <paths.h>
|
2015-02-07 12:15:18 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
2008-10-21 22:00:00 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-04-25 12:13:44 +08:00
|
|
|
#include <sys/epoll.h>
|
2008-10-21 22:00:00 +08:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/socket.h>
|
2015-02-07 12:15:18 +08:00
|
|
|
#include <sys/stat.h>
|
2016-07-27 00:32:33 +08:00
|
|
|
#include <sys/sysmacros.h>
|
2015-02-07 12:15:18 +08:00
|
|
|
#include <sys/types.h>
|
2008-10-21 22:00:00 +08:00
|
|
|
#include <sys/un.h>
|
2015-02-07 12:15:18 +08:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-01-13 21:48:47 +08:00
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/label.h>
|
2012-05-02 03:02:53 +08:00
|
|
|
#include <selinux/android.h>
|
2012-01-13 21:48:47 +08:00
|
|
|
|
2015-12-05 14:00:26 +08:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
2013-08-17 04:19:24 +08:00
|
|
|
#include <cutils/fs.h>
|
2015-02-07 12:15:18 +08:00
|
|
|
#include <cutils/iosched_policy.h>
|
|
|
|
#include <cutils/list.h>
|
|
|
|
#include <cutils/sockets.h>
|
2010-04-22 03:04:20 +08:00
|
|
|
#include <private/android_filesystem_config.h>
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-02-28 22:39:11 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2015-07-24 08:53:11 +08:00
|
|
|
#include "action.h"
|
2015-08-01 03:45:25 +08:00
|
|
|
#include "bootchart.h"
|
2008-10-21 22:00:00 +08:00
|
|
|
#include "devices.h"
|
2016-02-01 15:07:40 +08:00
|
|
|
#include "fs_mgr.h"
|
2015-08-27 02:43:36 +08:00
|
|
|
#include "import_parser.h"
|
2008-10-21 22:00:00 +08:00
|
|
|
#include "init.h"
|
2015-08-01 03:45:25 +08:00
|
|
|
#include "init_parser.h"
|
|
|
|
#include "keychords.h"
|
2010-04-20 08:05:34 +08:00
|
|
|
#include "log.h"
|
2008-10-21 22:00:00 +08:00
|
|
|
#include "property_service.h"
|
2015-08-01 03:45:25 +08:00
|
|
|
#include "service.h"
|
2010-04-14 10:48:59 +08:00
|
|
|
#include "signal_handler.h"
|
2010-04-22 03:04:20 +08:00
|
|
|
#include "ueventd.h"
|
2015-08-01 03:45:25 +08:00
|
|
|
#include "util.h"
|
2012-06-14 12:51:56 +08:00
|
|
|
#include "watchdogd.h"
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
2012-01-13 21:48:47 +08:00
|
|
|
struct selabel_handle *sehandle;
|
2012-08-09 22:05:49 +08:00
|
|
|
struct selabel_handle *sehandle_prop;
|
2012-01-13 21:48:47 +08:00
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
static int property_triggers_enabled = 0;
|
|
|
|
|
|
|
|
static char qemu[32];
|
|
|
|
|
2016-03-21 16:08:07 +08:00
|
|
|
std::string default_console = "/dev/console";
|
2016-11-11 09:43:47 +08:00
|
|
|
static time_t process_needs_restart_at;
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-08-01 03:45:25 +08:00
|
|
|
const char *ENV[32];
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-02-07 12:15:18 +08:00
|
|
|
bool waiting_for_exec = false;
|
|
|
|
|
2015-04-25 12:13:44 +08:00
|
|
|
static int epoll_fd = -1;
|
|
|
|
|
|
|
|
void register_epoll_handler(int fd, void (*fn)()) {
|
|
|
|
epoll_event ev;
|
|
|
|
ev.events = EPOLLIN;
|
|
|
|
ev.data.ptr = reinterpret_cast<void*>(fn);
|
|
|
|
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
|
2016-06-25 06:12:21 +08:00
|
|
|
PLOG(ERROR) << "epoll_ctl failed";
|
2015-04-25 12:13:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
/* add_environment - add "key=value" to the current environment */
|
|
|
|
int add_environment(const char *key, const char *val)
|
|
|
|
{
|
2014-05-16 18:36:36 +08:00
|
|
|
size_t n;
|
|
|
|
size_t key_len = strlen(key);
|
|
|
|
|
|
|
|
/* The last environment entry is reserved to terminate the list */
|
2016-06-25 06:12:21 +08:00
|
|
|
for (n = 0; n < (arraysize(ENV) - 1); n++) {
|
2014-05-16 18:36:36 +08:00
|
|
|
|
|
|
|
/* Delete any existing entry for this key */
|
|
|
|
if (ENV[n] != NULL) {
|
|
|
|
size_t entry_key_len = strcspn(ENV[n], "=");
|
|
|
|
if ((entry_key_len == key_len) && (strncmp(ENV[n], key, entry_key_len) == 0)) {
|
|
|
|
free((char*)ENV[n]);
|
|
|
|
ENV[n] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-09-29 00:55:31 +08:00
|
|
|
|
2014-05-16 18:36:36 +08:00
|
|
|
/* Add entry if a free slot is available */
|
|
|
|
if (ENV[n] == NULL) {
|
2015-02-04 09:12:07 +08:00
|
|
|
char* entry;
|
|
|
|
asprintf(&entry, "%s=%s", key, val);
|
2008-10-21 22:00:00 +08:00
|
|
|
ENV[n] = entry;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(ERROR) << "No env. room to store: '" << key << "':'" << val << "'";
|
2014-05-16 18:36:36 +08:00
|
|
|
|
|
|
|
return -1;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void property_changed(const char *name, const char *value)
|
|
|
|
{
|
2010-04-14 10:52:01 +08:00
|
|
|
if (property_triggers_enabled)
|
2015-07-24 08:53:11 +08:00
|
|
|
ActionManager::GetInstance().QueuePropertyTrigger(name, value);
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void restart_processes()
|
|
|
|
{
|
2016-11-11 09:43:47 +08:00
|
|
|
process_needs_restart_at = 0;
|
|
|
|
ServiceManager::GetInstance().ForEachServiceWithFlags(SVC_RESTARTING, [](Service* s) {
|
|
|
|
s->RestartIfNeeded(&process_needs_restart_at);
|
|
|
|
});
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2016-04-13 06:38:27 +08:00
|
|
|
void handle_control_message(const std::string& msg, const std::string& name) {
|
2015-08-01 03:45:25 +08:00
|
|
|
Service* svc = ServiceManager::GetInstance().FindServiceByName(name);
|
2016-04-13 06:38:27 +08:00
|
|
|
if (svc == nullptr) {
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(ERROR) << "no such service '" << name << "'";
|
2016-04-13 06:38:27 +08:00
|
|
|
return;
|
2012-01-26 12:48:46 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 03:45:25 +08:00
|
|
|
if (msg == "start") {
|
2016-04-13 06:38:27 +08:00
|
|
|
svc->Start();
|
2015-08-01 03:45:25 +08:00
|
|
|
} else if (msg == "stop") {
|
2016-04-13 06:38:27 +08:00
|
|
|
svc->Stop();
|
2015-08-01 03:45:25 +08:00
|
|
|
} else if (msg == "restart") {
|
2016-04-13 06:38:27 +08:00
|
|
|
svc->Restart();
|
2008-10-21 22:00:00 +08:00
|
|
|
} else {
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(ERROR) << "unknown control msg '" << msg << "'";
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
static int wait_for_coldboot_done_action(const std::vector<std::string>& args) {
|
2015-04-25 09:50:30 +08:00
|
|
|
Timer t;
|
|
|
|
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(VERBOSE) << "Waiting for " COLDBOOT_DONE "...";
|
2016-11-30 03:20:58 +08:00
|
|
|
|
|
|
|
// Historically we had a 1s timeout here because we weren't otherwise
|
|
|
|
// tracking boot time, and many OEMs made their sepolicy regular
|
|
|
|
// expressions too expensive (http://b/19899875).
|
|
|
|
|
|
|
|
// Now we're tracking boot time, just log the time taken to a system
|
|
|
|
// property. We still panic if it takes more than a minute though,
|
|
|
|
// because any build that slow isn't likely to boot at all, and we'd
|
|
|
|
// rather any test lab devices fail back to the bootloader.
|
|
|
|
if (wait_for_file(COLDBOOT_DONE, 60s) < 0) {
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(ERROR) << "Timed out waiting for " COLDBOOT_DONE;
|
2016-11-30 03:20:58 +08:00
|
|
|
panic();
|
2015-04-25 09:50:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-30 03:20:58 +08:00
|
|
|
property_set("ro.boottime.init.cold_boot_wait", std::to_string(t.duration_ns()).c_str());
|
2015-04-25 09:50:30 +08:00
|
|
|
return 0;
|
2010-04-14 10:52:01 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 04:19:24 +08:00
|
|
|
/*
|
|
|
|
* Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
|
|
|
|
* by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
|
|
|
|
* Does nothing if Hardware RNG is not present.
|
|
|
|
*
|
|
|
|
* Since we don't yet trust the quality of Hardware RNG, these bytes are not
|
|
|
|
* mixed into the primary pool of Linux RNG and the entropy estimate is left
|
|
|
|
* unmodified.
|
|
|
|
*
|
|
|
|
* If the HW RNG device /dev/hw_random is present, we require that at least
|
|
|
|
* 512 bytes read from it are written into Linux RNG. QA is expected to catch
|
|
|
|
* devices/configurations where these I/O operations are blocking for a long
|
|
|
|
* time. We do not reboot or halt on failures, as this is a best-effort
|
|
|
|
* attempt.
|
|
|
|
*/
|
2015-07-31 04:52:55 +08:00
|
|
|
static int mix_hwrng_into_linux_rng_action(const std::vector<std::string>& args)
|
2013-08-17 04:19:24 +08:00
|
|
|
{
|
|
|
|
int result = -1;
|
|
|
|
int hwrandom_fd = -1;
|
|
|
|
int urandom_fd = -1;
|
|
|
|
char buf[512];
|
|
|
|
ssize_t chunk_size;
|
|
|
|
size_t total_bytes_written = 0;
|
|
|
|
|
|
|
|
hwrandom_fd = TEMP_FAILURE_RETRY(
|
2015-02-03 06:37:22 +08:00
|
|
|
open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC));
|
2013-08-17 04:19:24 +08:00
|
|
|
if (hwrandom_fd == -1) {
|
|
|
|
if (errno == ENOENT) {
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(ERROR) << "/dev/hw_random not found";
|
|
|
|
// It's not an error to not have a Hardware RNG.
|
|
|
|
result = 0;
|
2013-08-17 04:19:24 +08:00
|
|
|
} else {
|
2016-06-25 06:12:21 +08:00
|
|
|
PLOG(ERROR) << "Failed to open /dev/hw_random";
|
2013-08-17 04:19:24 +08:00
|
|
|
}
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
urandom_fd = TEMP_FAILURE_RETRY(
|
2015-02-03 06:37:22 +08:00
|
|
|
open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
|
2013-08-17 04:19:24 +08:00
|
|
|
if (urandom_fd == -1) {
|
2016-06-25 06:12:21 +08:00
|
|
|
PLOG(ERROR) << "Failed to open /dev/urandom";
|
2013-08-17 04:19:24 +08:00
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (total_bytes_written < sizeof(buf)) {
|
|
|
|
chunk_size = TEMP_FAILURE_RETRY(
|
|
|
|
read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
|
|
|
|
if (chunk_size == -1) {
|
2016-06-25 06:12:21 +08:00
|
|
|
PLOG(ERROR) << "Failed to read from /dev/hw_random";
|
2013-08-17 04:19:24 +08:00
|
|
|
goto ret;
|
|
|
|
} else if (chunk_size == 0) {
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(ERROR) << "Failed to read from /dev/hw_random: EOF";
|
2013-08-17 04:19:24 +08:00
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
|
|
|
|
if (chunk_size == -1) {
|
2016-06-25 06:12:21 +08:00
|
|
|
PLOG(ERROR) << "Failed to write to /dev/urandom";
|
2013-08-17 04:19:24 +08:00
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
total_bytes_written += chunk_size;
|
|
|
|
}
|
|
|
|
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(INFO) << "Mixed " << total_bytes_written << " bytes from /dev/hw_random into /dev/urandom";
|
2013-08-17 04:19:24 +08:00
|
|
|
result = 0;
|
|
|
|
|
|
|
|
ret:
|
|
|
|
if (hwrandom_fd != -1) {
|
|
|
|
close(hwrandom_fd);
|
|
|
|
}
|
|
|
|
if (urandom_fd != -1) {
|
|
|
|
close(urandom_fd);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-26 07:49:05 +08:00
|
|
|
static void security_failure() {
|
|
|
|
LOG(ERROR) << "Security failure...";
|
|
|
|
panic();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MMAP_RND_PATH "/proc/sys/vm/mmap_rnd_bits"
|
|
|
|
#define MMAP_RND_COMPAT_PATH "/proc/sys/vm/mmap_rnd_compat_bits"
|
|
|
|
|
|
|
|
/* __attribute__((unused)) due to lack of mips support: see mips block
|
|
|
|
* in set_mmap_rnd_bits_action */
|
|
|
|
static bool __attribute__((unused)) set_mmap_rnd_bits_min(int start, int min, bool compat) {
|
|
|
|
std::string path;
|
|
|
|
if (compat) {
|
|
|
|
path = MMAP_RND_COMPAT_PATH;
|
|
|
|
} else {
|
|
|
|
path = MMAP_RND_PATH;
|
|
|
|
}
|
|
|
|
std::ifstream inf(path, std::fstream::in);
|
|
|
|
if (!inf) {
|
|
|
|
LOG(ERROR) << "Cannot open for reading: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while (start >= min) {
|
|
|
|
// try to write out new value
|
|
|
|
std::string str_val = std::to_string(start);
|
|
|
|
std::ofstream of(path, std::fstream::out);
|
|
|
|
if (!of) {
|
|
|
|
LOG(ERROR) << "Cannot open for writing: " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
of << str_val << std::endl;
|
|
|
|
of.close();
|
|
|
|
|
|
|
|
// check to make sure it was recorded
|
|
|
|
inf.seekg(0);
|
|
|
|
std::string str_rec;
|
|
|
|
inf >> str_rec;
|
|
|
|
if (str_val.compare(str_rec) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
start--;
|
|
|
|
}
|
|
|
|
inf.close();
|
|
|
|
if (start < min) {
|
|
|
|
LOG(ERROR) << "Unable to set minimum required entropy " << min << " in " << path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set /proc/sys/vm/mmap_rnd_bits and potentially
|
|
|
|
* /proc/sys/vm/mmap_rnd_compat_bits to the maximum supported values.
|
|
|
|
* Returns -1 if unable to set these to an acceptable value.
|
|
|
|
*
|
|
|
|
* To support this sysctl, the following upstream commits are needed:
|
|
|
|
*
|
|
|
|
* d07e22597d1d mm: mmap: add new /proc tunable for mmap_base ASLR
|
|
|
|
* e0c25d958f78 arm: mm: support ARCH_MMAP_RND_BITS
|
|
|
|
* 8f0d3aa9de57 arm64: mm: support ARCH_MMAP_RND_BITS
|
|
|
|
* 9e08f57d684a x86: mm: support ARCH_MMAP_RND_BITS
|
|
|
|
* ec9ee4acd97c drivers: char: random: add get_random_long()
|
|
|
|
* 5ef11c35ce86 mm: ASLR: use get_random_long()
|
|
|
|
*/
|
|
|
|
static int set_mmap_rnd_bits_action(const std::vector<std::string>& args)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
/* values are arch-dependent */
|
|
|
|
#if defined(__aarch64__)
|
|
|
|
/* arm64 supports 18 - 33 bits depending on pagesize and VA_SIZE */
|
|
|
|
if (set_mmap_rnd_bits_min(33, 24, false)
|
|
|
|
&& set_mmap_rnd_bits_min(16, 16, true)) {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
/* x86_64 supports 28 - 32 bits */
|
|
|
|
if (set_mmap_rnd_bits_min(32, 32, false)
|
|
|
|
&& set_mmap_rnd_bits_min(16, 16, true)) {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
#elif defined(__arm__) || defined(__i386__)
|
|
|
|
/* check to see if we're running on 64-bit kernel */
|
|
|
|
bool h64 = !access(MMAP_RND_COMPAT_PATH, F_OK);
|
|
|
|
/* supported 32-bit architecture must have 16 bits set */
|
|
|
|
if (set_mmap_rnd_bits_min(16, 16, h64)) {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
#elif defined(__mips__) || defined(__mips64__)
|
|
|
|
// TODO: add mips support b/27788820
|
|
|
|
ret = 0;
|
|
|
|
#else
|
|
|
|
ERROR("Unknown architecture\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __BRILLO__
|
|
|
|
// TODO: b/27794137
|
|
|
|
ret = 0;
|
|
|
|
#endif
|
|
|
|
if (ret == -1) {
|
|
|
|
LOG(ERROR) << "Unable to set adequate mmap entropy value!";
|
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
static int keychord_init_action(const std::vector<std::string>& args)
|
2010-04-14 10:52:01 +08:00
|
|
|
{
|
|
|
|
keychord_init();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
static int console_init_action(const std::vector<std::string>& args)
|
2008-10-21 22:00:00 +08:00
|
|
|
{
|
2016-03-21 16:23:43 +08:00
|
|
|
std::string console = property_get("ro.boot.console");
|
|
|
|
if (!console.empty()) {
|
2016-03-21 16:08:07 +08:00
|
|
|
default_console = "/dev/" + console;
|
2010-04-14 10:52:01 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-07 10:19:24 +08:00
|
|
|
static void import_kernel_nv(const std::string& key, const std::string& value, bool for_emulator) {
|
|
|
|
if (key.empty()) return;
|
2011-12-20 03:21:32 +08:00
|
|
|
|
|
|
|
if (for_emulator) {
|
2015-05-07 10:19:24 +08:00
|
|
|
// In the emulator, export any kernel option with the "ro.kernel." prefix.
|
2016-11-11 09:43:47 +08:00
|
|
|
property_set(StringPrintf("ro.kernel.%s", key.c_str()).c_str(), value.c_str());
|
2011-12-20 03:21:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-07 10:19:24 +08:00
|
|
|
if (key == "qemu") {
|
|
|
|
strlcpy(qemu, value.c_str(), sizeof(qemu));
|
|
|
|
} else if (android::base::StartsWith(key, "androidboot.")) {
|
2016-11-11 09:43:47 +08:00
|
|
|
property_set(StringPrintf("ro.boot.%s", key.c_str() + 12).c_str(), value.c_str());
|
2011-12-20 03:21:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 20:33:58 +08:00
|
|
|
static void export_oem_lock_status() {
|
|
|
|
if (property_get("ro.oem_unlock_supported") != "1") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string value = property_get("ro.boot.verifiedbootstate");
|
|
|
|
|
|
|
|
if (!value.empty()) {
|
|
|
|
property_set("ro.boot.flash.locked", value == "orange" ? "0" : "1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 23:50:46 +08:00
|
|
|
static void export_kernel_boot_props() {
|
2011-12-20 03:21:32 +08:00
|
|
|
struct {
|
|
|
|
const char *src_prop;
|
2015-03-20 23:50:46 +08:00
|
|
|
const char *dst_prop;
|
|
|
|
const char *default_value;
|
2011-12-20 03:21:32 +08:00
|
|
|
} prop_map[] = {
|
2015-03-20 23:50:46 +08:00
|
|
|
{ "ro.boot.serialno", "ro.serialno", "", },
|
|
|
|
{ "ro.boot.mode", "ro.bootmode", "unknown", },
|
|
|
|
{ "ro.boot.baseband", "ro.baseband", "unknown", },
|
2011-12-20 03:21:32 +08:00
|
|
|
{ "ro.boot.bootloader", "ro.bootloader", "unknown", },
|
2015-03-20 23:50:46 +08:00
|
|
|
{ "ro.boot.hardware", "ro.hardware", "unknown", },
|
|
|
|
{ "ro.boot.revision", "ro.revision", "0", },
|
2011-12-20 03:21:32 +08:00
|
|
|
};
|
2016-06-25 06:12:21 +08:00
|
|
|
for (size_t i = 0; i < arraysize(prop_map); i++) {
|
2015-07-25 01:11:05 +08:00
|
|
|
std::string value = property_get(prop_map[i].src_prop);
|
|
|
|
property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value);
|
2011-12-20 03:21:32 +08:00
|
|
|
}
|
|
|
|
}
|
2010-04-14 10:52:01 +08:00
|
|
|
|
2015-05-07 10:19:24 +08:00
|
|
|
static void process_kernel_dt() {
|
2015-02-28 22:39:11 +08:00
|
|
|
static const char android_dir[] = "/proc/device-tree/firmware/android";
|
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
std::string file_name = StringPrintf("%s/compatible", android_dir);
|
2015-02-28 22:39:11 +08:00
|
|
|
|
|
|
|
std::string dt_file;
|
|
|
|
android::base::ReadFileToString(file_name, &dt_file);
|
|
|
|
if (!dt_file.compare("android,firmware")) {
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(ERROR) << "firmware/android is not compatible with 'android,firmware'";
|
2015-02-28 22:39:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(android_dir), closedir);
|
2015-05-07 10:19:24 +08:00
|
|
|
if (!dir) return;
|
2015-02-28 22:39:11 +08:00
|
|
|
|
|
|
|
struct dirent *dp;
|
|
|
|
while ((dp = readdir(dir.get())) != NULL) {
|
2015-11-09 09:51:50 +08:00
|
|
|
if (dp->d_type != DT_REG || !strcmp(dp->d_name, "compatible") || !strcmp(dp->d_name, "name")) {
|
2015-02-28 22:39:11 +08:00
|
|
|
continue;
|
2015-05-07 10:19:24 +08:00
|
|
|
}
|
2015-02-28 22:39:11 +08:00
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
file_name = StringPrintf("%s/%s", android_dir, dp->d_name);
|
2015-02-28 22:39:11 +08:00
|
|
|
|
|
|
|
android::base::ReadFileToString(file_name, &dt_file);
|
|
|
|
std::replace(dt_file.begin(), dt_file.end(), ',', '.');
|
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
std::string property_name = StringPrintf("ro.boot.%s", dp->d_name);
|
2015-03-21 00:45:18 +08:00
|
|
|
property_set(property_name.c_str(), dt_file.c_str());
|
2015-02-28 22:39:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 10:19:24 +08:00
|
|
|
static void process_kernel_cmdline() {
|
|
|
|
// The first pass does the common stuff, and finds if we are in qemu.
|
|
|
|
// The second pass is only necessary for qemu to export all kernel params
|
|
|
|
// as properties.
|
2015-04-26 08:42:52 +08:00
|
|
|
import_kernel_cmdline(false, import_kernel_nv);
|
2015-05-07 10:19:24 +08:00
|
|
|
if (qemu[0]) import_kernel_cmdline(true, import_kernel_nv);
|
2010-04-14 10:52:01 +08:00
|
|
|
}
|
|
|
|
|
2016-11-26 21:14:07 +08:00
|
|
|
static int property_enable_triggers_action(const std::vector<std::string>& args)
|
|
|
|
{
|
|
|
|
/* Enable property triggers. */
|
|
|
|
property_triggers_enabled = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-31 04:52:55 +08:00
|
|
|
static int queue_property_triggers_action(const std::vector<std::string>& args)
|
2010-04-14 10:52:01 +08:00
|
|
|
{
|
2016-11-26 21:14:07 +08:00
|
|
|
ActionManager::GetInstance().QueueBuiltinAction(property_enable_triggers_action, "enable_property_trigger");
|
2015-07-24 08:53:11 +08:00
|
|
|
ActionManager::GetInstance().QueueAllPropertyTriggers();
|
2010-04-14 10:52:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-25 09:50:30 +08:00
|
|
|
static void selinux_init_all_handles(void)
|
2012-01-13 21:48:47 +08:00
|
|
|
{
|
2012-05-02 03:02:53 +08:00
|
|
|
sehandle = selinux_android_file_context_handle();
|
2014-01-28 23:34:09 +08:00
|
|
|
selinux_android_set_sehandle(sehandle);
|
2012-08-09 22:05:49 +08:00
|
|
|
sehandle_prop = selinux_android_prop_context_handle();
|
2012-05-02 03:02:53 +08:00
|
|
|
}
|
2012-01-13 21:48:47 +08:00
|
|
|
|
2015-04-29 03:39:41 +08:00
|
|
|
enum selinux_enforcing_status { SELINUX_PERMISSIVE, SELINUX_ENFORCING };
|
2015-04-26 08:42:52 +08:00
|
|
|
|
|
|
|
static selinux_enforcing_status selinux_status_from_cmdline() {
|
|
|
|
selinux_enforcing_status status = SELINUX_ENFORCING;
|
|
|
|
|
2015-05-07 10:19:24 +08:00
|
|
|
import_kernel_cmdline(false, [&](const std::string& key, const std::string& value, bool in_qemu) {
|
|
|
|
if (key == "androidboot.selinux" && value == "permissive") {
|
|
|
|
status = SELINUX_PERMISSIVE;
|
2015-04-26 08:42:52 +08:00
|
|
|
}
|
2015-05-07 10:19:24 +08:00
|
|
|
});
|
2015-04-26 08:42:52 +08:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-06-27 06:37:26 +08:00
|
|
|
static bool selinux_is_enforcing(void)
|
|
|
|
{
|
2015-04-29 03:39:41 +08:00
|
|
|
if (ALLOW_PERMISSIVE_SELINUX) {
|
2015-04-26 08:42:52 +08:00
|
|
|
return selinux_status_from_cmdline() == SELINUX_ENFORCING;
|
2013-06-27 06:37:26 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-28 14:20:44 +08:00
|
|
|
static int audit_callback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
|
2015-10-02 07:03:47 +08:00
|
|
|
|
|
|
|
property_audit_data *d = reinterpret_cast<property_audit_data*>(data);
|
|
|
|
|
|
|
|
if (!d || !d->name || !d->cr) {
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(ERROR) << "audit_callback invoked with null data arguments!";
|
2015-10-02 07:03:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buf, len, "property=%s pid=%d uid=%d gid=%d", d->name,
|
|
|
|
d->cr->pid, d->cr->uid, d->cr->gid);
|
2012-08-09 22:05:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-25 02:38:17 +08:00
|
|
|
static void selinux_initialize(bool in_kernel_domain) {
|
2015-03-28 14:20:44 +08:00
|
|
|
Timer t;
|
|
|
|
|
|
|
|
selinux_callback cb;
|
|
|
|
cb.func_log = selinux_klog_callback;
|
|
|
|
selinux_set_callback(SELINUX_CB_LOG, cb);
|
|
|
|
cb.func_audit = audit_callback;
|
|
|
|
selinux_set_callback(SELINUX_CB_AUDIT, cb);
|
2014-02-13 05:17:00 +08:00
|
|
|
|
2015-04-25 02:38:17 +08:00
|
|
|
if (in_kernel_domain) {
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(INFO) << "Loading SELinux policy...";
|
2015-04-25 02:38:17 +08:00
|
|
|
if (selinux_android_load_policy() < 0) {
|
2016-06-25 06:12:21 +08:00
|
|
|
PLOG(ERROR) << "failed to load policy";
|
2015-04-25 02:38:17 +08:00
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
|
2015-04-29 03:39:41 +08:00
|
|
|
bool kernel_enforcing = (security_getenforce() == 1);
|
2015-04-25 02:38:17 +08:00
|
|
|
bool is_enforcing = selinux_is_enforcing();
|
2015-04-29 03:39:41 +08:00
|
|
|
if (kernel_enforcing != is_enforcing) {
|
|
|
|
if (security_setenforce(is_enforcing)) {
|
2016-06-25 06:12:21 +08:00
|
|
|
PLOG(ERROR) << "security_setenforce(%s) failed" << (is_enforcing ? "true" : "false");
|
2015-04-29 03:39:41 +08:00
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
}
|
2015-03-28 14:20:44 +08:00
|
|
|
|
2015-04-26 05:10:03 +08:00
|
|
|
if (write_file("/sys/fs/selinux/checkreqprot", "0") == -1) {
|
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
|
2016-11-30 03:20:58 +08:00
|
|
|
// init's first stage can't set properties, so pass the time to the second stage.
|
|
|
|
setenv("INIT_SELINUX_TOOK", std::to_string(t.duration_ns()).c_str(), 1);
|
2015-04-25 02:38:17 +08:00
|
|
|
} else {
|
|
|
|
selinux_init_all_handles();
|
|
|
|
}
|
2013-06-25 08:41:40 +08:00
|
|
|
}
|
|
|
|
|
2016-06-29 21:30:00 +08:00
|
|
|
// Set the UDC controller for the ConfigFS USB Gadgets.
|
|
|
|
// Read the UDC controller in use from "/sys/class/udc".
|
|
|
|
// In case of multiple UDC controllers select the first one.
|
|
|
|
static void set_usb_controller() {
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)>dir(opendir("/sys/class/udc"), closedir);
|
|
|
|
if (!dir) return;
|
|
|
|
|
|
|
|
dirent* dp;
|
|
|
|
while ((dp = readdir(dir.get())) != nullptr) {
|
|
|
|
if (dp->d_name[0] == '.') continue;
|
|
|
|
|
|
|
|
property_set("sys.usb.controller", dp->d_name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 15:07:40 +08:00
|
|
|
/* Returns a new path consisting of base_path and the file name in reference_path. */
|
|
|
|
static std::string get_path(const std::string& base_path, const std::string& reference_path) {
|
|
|
|
std::string::size_type pos = reference_path.rfind('/');
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
return base_path + '/' + reference_path;
|
|
|
|
} else {
|
|
|
|
return base_path + reference_path.substr(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Imports the fstab info from cmdline. */
|
|
|
|
static std::string import_cmdline_fstab() {
|
|
|
|
std::string prefix, fstab, fstab_full;
|
|
|
|
|
|
|
|
import_kernel_cmdline(false,
|
|
|
|
[&](const std::string& key, const std::string& value, bool in_qemu __attribute__((__unused__))) {
|
|
|
|
if (key == "android.early.prefix") {
|
|
|
|
prefix = value;
|
|
|
|
} else if (key == "android.early.fstab") {
|
|
|
|
fstab = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!fstab.empty()) {
|
|
|
|
// Convert "mmcblk0p09+/odm+ext4+ro+verify" to "mmcblk0p09 /odm ext4 ro verify"
|
|
|
|
std::replace(fstab.begin(), fstab.end(), '+', ' ');
|
|
|
|
for (const auto& entry : android::base::Split(fstab, "\n")) {
|
|
|
|
fstab_full += prefix + entry + '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fstab_full;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Early mount vendor and ODM partitions. The fstab info is read from kernel cmdline. */
|
|
|
|
static void early_mount() {
|
|
|
|
std::string fstab_string = import_cmdline_fstab();
|
|
|
|
if (fstab_string.empty()) {
|
|
|
|
LOG(INFO) << "Failed to load vendor fstab from kernel cmdline";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FILE *fstab_file = fmemopen((void *)fstab_string.c_str(), fstab_string.length(), "r");
|
|
|
|
if (!fstab_file) {
|
|
|
|
PLOG(ERROR) << "Failed to open fstab string as FILE";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_file(fstab_file), fs_mgr_free_fstab);
|
|
|
|
fclose(fstab_file);
|
|
|
|
if (!fstab) {
|
|
|
|
LOG(ERROR) << "Failed to parse fstab string: " << fstab_string;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Loaded vendor fstab from cmdline";
|
|
|
|
|
|
|
|
if (early_device_socket_open()) {
|
|
|
|
LOG(ERROR) << "Failed to open device uevent socket";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create /dev/device-mapper for dm-verity */
|
|
|
|
early_create_dev("/sys/devices/virtual/misc/device-mapper", EARLY_CHAR_DEV);
|
|
|
|
|
|
|
|
for (int i = 0; i < fstab->num_entries; ++i) {
|
|
|
|
struct fstab_rec *rec = &fstab->recs[i];
|
|
|
|
std::string mount_point = rec->mount_point;
|
|
|
|
std::string syspath = rec->blk_device;
|
|
|
|
|
|
|
|
if (mount_point != "/vendor" && mount_point != "/odm")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Create mount target under /dev/block/ from sysfs via uevent */
|
|
|
|
LOG(INFO) << "Mounting " << mount_point << " from " << syspath << "...";
|
|
|
|
char *devpath = strdup(get_path("/dev/block", syspath).c_str());
|
|
|
|
if (!devpath) {
|
|
|
|
PLOG(ERROR) << "Failed to strdup dev path in early mount " << syspath;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rec->blk_device = devpath;
|
|
|
|
early_create_dev(syspath, EARLY_BLOCK_DEV);
|
|
|
|
|
|
|
|
int rc = fs_mgr_early_setup_verity(rec);
|
|
|
|
if (rc == FS_MGR_EARLY_SETUP_VERITY_SUCCESS) {
|
|
|
|
/* Mount target is changed to /dev/block/dm-<n>; initiate its creation from sysfs counterpart */
|
|
|
|
early_create_dev(get_path("/sys/devices/virtual/block", rec->blk_device), EARLY_BLOCK_DEV);
|
|
|
|
} else if (rc == FS_MGR_EARLY_SETUP_VERITY_FAIL) {
|
|
|
|
LOG(ERROR) << "Failed to set up dm-verity on " << rec->blk_device;
|
|
|
|
continue;
|
|
|
|
} else { /* FS_MGR_EARLY_SETUP_VERITY_NO_VERITY */
|
|
|
|
LOG(INFO) << "dm-verity disabled on debuggable device; mount directly on " << rec->blk_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir(mount_point.c_str(), 0755);
|
|
|
|
rc = mount(rec->blk_device, mount_point.c_str(), rec->fs_type, rec->flags, rec->fs_options);
|
|
|
|
if (rc) {
|
|
|
|
PLOG(ERROR) << "Failed to mount on " << rec->blk_device;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
early_device_socket_close();
|
|
|
|
}
|
|
|
|
|
2015-02-07 12:15:18 +08:00
|
|
|
int main(int argc, char** argv) {
|
2015-03-28 14:20:44 +08:00
|
|
|
if (!strcmp(basename(argv[0]), "ueventd")) {
|
2010-04-22 03:04:20 +08:00
|
|
|
return ueventd_main(argc, argv);
|
2015-03-28 14:20:44 +08:00
|
|
|
}
|
2010-04-22 03:04:20 +08:00
|
|
|
|
2015-03-28 14:20:44 +08:00
|
|
|
if (!strcmp(basename(argv[0]), "watchdogd")) {
|
2012-06-14 12:51:56 +08:00
|
|
|
return watchdogd_main(argc, argv);
|
2015-03-28 14:20:44 +08:00
|
|
|
}
|
2012-06-14 12:51:56 +08:00
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
boot_clock::time_point start_time = boot_clock::now();
|
|
|
|
|
2015-02-07 12:15:18 +08:00
|
|
|
// Clear the umask.
|
2008-10-21 22:00:00 +08:00
|
|
|
umask(0);
|
|
|
|
|
2015-03-18 11:01:13 +08:00
|
|
|
add_environment("PATH", _PATH_DEFPATH);
|
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
bool is_first_stage = (getenv("INIT_SECOND_STAGE") == nullptr);
|
2015-04-26 06:50:03 +08:00
|
|
|
|
2016-02-01 15:07:40 +08:00
|
|
|
// Don't expose the raw commandline to unprivileged processes.
|
|
|
|
chmod("/proc/cmdline", 0440);
|
|
|
|
|
2015-02-07 12:15:18 +08:00
|
|
|
// Get the basic filesystem setup we need put together in the initramdisk
|
|
|
|
// on / and then we'll let the rc file figure out the rest.
|
2015-04-26 06:50:03 +08:00
|
|
|
if (is_first_stage) {
|
2015-04-26 08:42:52 +08:00
|
|
|
mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
|
|
|
|
mkdir("/dev/pts", 0755);
|
|
|
|
mkdir("/dev/socket", 0755);
|
|
|
|
mount("devpts", "/dev/pts", "devpts", 0, NULL);
|
2015-11-08 08:52:17 +08:00
|
|
|
#define MAKE_STR(x) __STRING(x)
|
|
|
|
mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC));
|
2016-10-30 03:20:00 +08:00
|
|
|
gid_t groups[] = { AID_READPROC };
|
|
|
|
setgroups(arraysize(groups), groups);
|
2015-04-26 06:50:03 +08:00
|
|
|
mount("sysfs", "/sys", "sysfs", 0, NULL);
|
2016-03-04 02:40:12 +08:00
|
|
|
mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL);
|
2016-07-27 00:32:33 +08:00
|
|
|
mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11));
|
2016-11-19 06:58:40 +08:00
|
|
|
mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8));
|
|
|
|
mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9));
|
2015-04-26 06:50:03 +08:00
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2016-07-27 00:32:33 +08:00
|
|
|
// Now that tmpfs is mounted on /dev and we have /dev/kmsg, we can actually
|
|
|
|
// talk to the outside world...
|
2016-06-25 06:12:21 +08:00
|
|
|
InitKernelLogging(argv);
|
2015-03-28 14:20:44 +08:00
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
LOG(INFO) << "init " << (is_first_stage ? "first" : "second") << " stage started!";
|
2016-10-29 03:22:32 +08:00
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
if (is_first_stage) {
|
2016-10-29 03:22:32 +08:00
|
|
|
// Mount devices defined in android.early.* kernel commandline
|
|
|
|
early_mount();
|
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
// Set up SELinux, loading the SELinux policy.
|
2016-10-29 03:22:32 +08:00
|
|
|
selinux_initialize(true);
|
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
// We're in the kernel domain, so re-exec init to transition to the init domain now
|
2016-10-29 03:22:32 +08:00
|
|
|
// that the SELinux policy has been loaded.
|
|
|
|
if (restorecon("/init") == -1) {
|
|
|
|
PLOG(ERROR) << "restorecon failed";
|
|
|
|
security_failure();
|
|
|
|
}
|
2016-11-11 09:43:47 +08:00
|
|
|
|
|
|
|
setenv("INIT_SECOND_STAGE", "true", 1);
|
|
|
|
|
|
|
|
uint64_t start_ns = start_time.time_since_epoch().count();
|
|
|
|
setenv("INIT_STARTED_AT", StringPrintf("%" PRIu64, start_ns).c_str(), 1);
|
|
|
|
|
2016-10-29 03:22:32 +08:00
|
|
|
char* path = argv[0];
|
2016-11-11 09:43:47 +08:00
|
|
|
char* args[] = { path, nullptr };
|
2016-10-29 03:22:32 +08:00
|
|
|
if (execv(path, args) == -1) {
|
|
|
|
PLOG(ERROR) << "execv(\"" << path << "\") failed";
|
|
|
|
security_failure();
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-26 08:42:52 +08:00
|
|
|
// Indicate that booting is in progress to background fw loaders, etc.
|
|
|
|
close(open("/dev/.booting", O_WRONLY | O_CREAT | O_CLOEXEC, 0000));
|
2011-09-29 00:55:31 +08:00
|
|
|
|
2015-04-26 08:42:52 +08:00
|
|
|
property_init();
|
2011-12-20 03:21:32 +08:00
|
|
|
|
2015-04-26 08:42:52 +08:00
|
|
|
// If arguments are passed both on the command line and in DT,
|
|
|
|
// properties set in DT always have priority over the command-line ones.
|
|
|
|
process_kernel_dt();
|
|
|
|
process_kernel_cmdline();
|
|
|
|
|
2015-05-07 10:19:24 +08:00
|
|
|
// Propagate the kernel variables to internal variables
|
2015-04-26 08:42:52 +08:00
|
|
|
// used by init as well as the current required properties.
|
|
|
|
export_kernel_boot_props();
|
2015-02-28 22:39:11 +08:00
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
// Make the time that init started available for bootstat to log.
|
2016-11-30 03:20:58 +08:00
|
|
|
property_set("ro.boottime.init", getenv("INIT_STARTED_AT"));
|
|
|
|
property_set("ro.boottime.init.selinux", getenv("INIT_SELINUX_TOOK"));
|
|
|
|
|
|
|
|
// Clean up our environment.
|
|
|
|
unsetenv("INIT_SECOND_STAGE");
|
|
|
|
unsetenv("INIT_STARTED_AT");
|
|
|
|
unsetenv("INIT_SELINUX_TOOK");
|
2016-11-11 09:43:47 +08:00
|
|
|
|
|
|
|
// Now set up SELinux for second stage.
|
2016-10-29 03:22:32 +08:00
|
|
|
selinux_initialize(false);
|
2015-04-25 02:38:17 +08:00
|
|
|
}
|
2015-02-07 12:15:18 +08:00
|
|
|
|
|
|
|
// These directories were necessarily created before initial policy load
|
|
|
|
// and therefore need their security context restored to the proper value.
|
|
|
|
// This must happen before /dev is populated by ueventd.
|
2016-06-25 06:12:21 +08:00
|
|
|
LOG(INFO) << "Running restorecon...";
|
2012-06-12 01:37:39 +08:00
|
|
|
restorecon("/dev");
|
2016-07-27 00:32:33 +08:00
|
|
|
restorecon("/dev/kmsg");
|
2012-06-12 01:37:39 +08:00
|
|
|
restorecon("/dev/socket");
|
2016-11-19 06:58:40 +08:00
|
|
|
restorecon("/dev/random");
|
|
|
|
restorecon("/dev/urandom");
|
2013-03-01 09:29:58 +08:00
|
|
|
restorecon("/dev/__properties__");
|
2015-12-01 08:40:25 +08:00
|
|
|
restorecon("/property_contexts");
|
2016-11-15 07:40:18 +08:00
|
|
|
restorecon("/sys", SELINUX_ANDROID_RESTORECON_RECURSE);
|
|
|
|
restorecon("/dev/block", SELINUX_ANDROID_RESTORECON_RECURSE);
|
2016-02-01 15:07:40 +08:00
|
|
|
restorecon("/dev/device-mapper");
|
2012-01-13 21:48:47 +08:00
|
|
|
|
2015-04-25 12:13:44 +08:00
|
|
|
epoll_fd = epoll_create1(EPOLL_CLOEXEC);
|
|
|
|
if (epoll_fd == -1) {
|
2016-06-25 06:12:21 +08:00
|
|
|
PLOG(ERROR) << "epoll_create1 failed";
|
2015-04-25 12:13:44 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
signal_handler_init();
|
2015-04-25 08:43:21 +08:00
|
|
|
|
2014-06-17 06:06:21 +08:00
|
|
|
property_load_boot_defaults();
|
2015-12-07 20:33:58 +08:00
|
|
|
export_oem_lock_status();
|
2015-04-25 09:50:30 +08:00
|
|
|
start_property_service();
|
2016-06-29 21:30:00 +08:00
|
|
|
set_usb_controller();
|
2011-12-17 06:18:06 +08:00
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
const BuiltinFunctionMap function_map;
|
|
|
|
Action::set_function_map(&function_map);
|
|
|
|
|
|
|
|
Parser& parser = Parser::GetInstance();
|
|
|
|
parser.AddSectionParser("service",std::make_unique<ServiceParser>());
|
|
|
|
parser.AddSectionParser("on", std::make_unique<ActionParser>());
|
|
|
|
parser.AddSectionParser("import", std::make_unique<ImportParser>());
|
|
|
|
parser.ParseConfig("/init.rc");
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-07-24 08:53:11 +08:00
|
|
|
ActionManager& am = ActionManager::GetInstance();
|
|
|
|
|
|
|
|
am.QueueEventTrigger("early-init");
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-04-25 09:50:30 +08:00
|
|
|
// Queue an action that waits for coldboot done so we know ueventd has set up all of /dev...
|
2015-07-24 08:53:11 +08:00
|
|
|
am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done");
|
2015-04-25 09:50:30 +08:00
|
|
|
// ... so that we can start queuing up actions that require stuff from /dev.
|
2015-07-24 08:53:11 +08:00
|
|
|
am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
|
2016-03-26 07:49:05 +08:00
|
|
|
am.QueueBuiltinAction(set_mmap_rnd_bits_action, "set_mmap_rnd_bits");
|
2015-07-24 08:53:11 +08:00
|
|
|
am.QueueBuiltinAction(keychord_init_action, "keychord_init");
|
|
|
|
am.QueueBuiltinAction(console_init_action, "console_init");
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-03-28 14:20:44 +08:00
|
|
|
// Trigger all the boot actions to get us started.
|
2015-07-24 08:53:11 +08:00
|
|
|
am.QueueEventTrigger("init");
|
2011-08-25 06:28:23 +08:00
|
|
|
|
2015-02-07 12:15:18 +08:00
|
|
|
// Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
|
|
|
|
// wasn't ready immediately after wait_for_coldboot_done
|
2015-07-24 08:53:11 +08:00
|
|
|
am.QueueBuiltinAction(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-02-07 12:15:18 +08:00
|
|
|
// Don't mount filesystems or start core system services in charger mode.
|
2015-07-25 01:11:05 +08:00
|
|
|
std::string bootmode = property_get("ro.bootmode");
|
|
|
|
if (bootmode == "charger") {
|
2015-07-24 08:53:11 +08:00
|
|
|
am.QueueEventTrigger("charger");
|
2011-08-25 06:28:23 +08:00
|
|
|
} else {
|
2015-07-24 08:53:11 +08:00
|
|
|
am.QueueEventTrigger("late-init");
|
2011-08-25 06:28:23 +08:00
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2015-02-07 12:15:18 +08:00
|
|
|
// Run all property triggers based on current state of the properties.
|
2015-07-24 08:53:11 +08:00
|
|
|
am.QueueBuiltinAction(queue_property_triggers_action, "queue_property_triggers");
|
2010-04-14 10:52:01 +08:00
|
|
|
|
2015-04-25 09:50:30 +08:00
|
|
|
while (true) {
|
2015-02-07 12:15:18 +08:00
|
|
|
if (!waiting_for_exec) {
|
2015-07-24 08:53:11 +08:00
|
|
|
am.ExecuteOneCommand();
|
2015-02-07 12:15:18 +08:00
|
|
|
restart_processes();
|
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
// By default, sleep until something happens.
|
|
|
|
int epoll_timeout_ms = -1;
|
|
|
|
|
|
|
|
// If there's a process that needs restarting, wake up in time for that.
|
|
|
|
if (process_needs_restart_at != 0) {
|
|
|
|
epoll_timeout_ms = (process_needs_restart_at - time(nullptr)) * 1000;
|
|
|
|
if (epoll_timeout_ms < 0) epoll_timeout_ms = 0;
|
2015-02-05 06:46:36 +08:00
|
|
|
}
|
2010-04-14 10:52:01 +08:00
|
|
|
|
2016-12-01 07:43:42 +08:00
|
|
|
// If there's more work to do, wake up again immediately.
|
|
|
|
if (am.HasMoreCommands()) epoll_timeout_ms = 0;
|
|
|
|
|
2016-11-11 09:43:47 +08:00
|
|
|
bootchart_sample(&epoll_timeout_ms);
|
2010-04-14 10:52:01 +08:00
|
|
|
|
2015-04-25 12:13:44 +08:00
|
|
|
epoll_event ev;
|
2016-11-11 09:43:47 +08:00
|
|
|
int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, epoll_timeout_ms));
|
2015-04-25 12:13:44 +08:00
|
|
|
if (nr == -1) {
|
2016-06-25 06:12:21 +08:00
|
|
|
PLOG(ERROR) << "epoll_wait failed";
|
2015-04-25 12:13:44 +08:00
|
|
|
} else if (nr == 1) {
|
|
|
|
((void (*)()) ev.data.ptr)();
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|