2018-02-14 00:50:08 +00:00
|
|
|
//
|
|
|
|
// Copyright (C) 2018 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.
|
|
|
|
//
|
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <errno.h>
|
2018-02-14 00:50:08 +00:00
|
|
|
#include <pwd.h>
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <stdio.h>
|
2018-02-14 00:50:08 +00:00
|
|
|
|
2018-05-10 00:38:30 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <vector>
|
2018-05-10 00:38:30 +00:00
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <android-base/file.h>
|
2018-02-14 00:50:08 +00:00
|
|
|
#include <android-base/logging.h>
|
2018-05-10 01:33:31 +00:00
|
|
|
#include <android-base/parseint.h>
|
2018-05-10 00:38:30 +00:00
|
|
|
#include <android-base/strings.h>
|
2018-02-14 00:50:08 +00:00
|
|
|
|
|
|
|
#include "action.h"
|
|
|
|
#include "action_manager.h"
|
|
|
|
#include "action_parser.h"
|
2018-05-10 00:38:30 +00:00
|
|
|
#include "host_import_parser.h"
|
|
|
|
#include "host_init_stubs.h"
|
2018-02-14 00:50:08 +00:00
|
|
|
#include "parser.h"
|
|
|
|
#include "result.h"
|
|
|
|
#include "service.h"
|
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
#define EXCLUDE_FS_CONFIG_STRUCTURES
|
|
|
|
#include "generated_android_ids.h"
|
|
|
|
|
2018-05-10 00:38:30 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
using android::base::ParseInt;
|
|
|
|
using android::base::ReadFileToString;
|
2018-05-10 00:38:30 +00:00
|
|
|
using android::base::Split;
|
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
static std::string out_dir;
|
|
|
|
|
|
|
|
static std::vector<std::pair<std::string, int>> GetVendorPasswd() {
|
|
|
|
std::string passwd;
|
|
|
|
if (!ReadFileToString(out_dir + "/vendor/etc/passwd", &passwd)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, int>> result;
|
|
|
|
auto passwd_lines = Split(passwd, "\n");
|
|
|
|
for (const auto& line : passwd_lines) {
|
|
|
|
auto split_line = Split(line, ":");
|
|
|
|
if (split_line.size() < 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int uid = 0;
|
|
|
|
if (!ParseInt(split_line[2], &uid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result.emplace_back(split_line[0], uid);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-14 00:50:08 +00:00
|
|
|
passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
|
2018-05-10 01:33:31 +00:00
|
|
|
// This isn't thread safe, but that's okay for our purposes.
|
|
|
|
static char static_name[32] = "";
|
|
|
|
static char static_dir[32] = "/";
|
|
|
|
static char static_shell[32] = "/system/bin/sh";
|
|
|
|
static passwd static_passwd = {
|
|
|
|
.pw_name = static_name,
|
|
|
|
.pw_dir = static_dir,
|
|
|
|
.pw_shell = static_shell,
|
|
|
|
.pw_uid = 0,
|
|
|
|
.pw_gid = 0,
|
2018-02-14 00:50:08 +00:00
|
|
|
};
|
2018-05-10 01:33:31 +00:00
|
|
|
|
|
|
|
for (size_t n = 0; n < android_id_count; ++n) {
|
|
|
|
if (!strcmp(android_ids[n].name, login)) {
|
|
|
|
snprintf(static_name, sizeof(static_name), "%s", android_ids[n].name);
|
|
|
|
static_passwd.pw_uid = android_ids[n].aid;
|
|
|
|
static_passwd.pw_gid = android_ids[n].aid;
|
|
|
|
return &static_passwd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const auto vendor_passwd = GetVendorPasswd();
|
|
|
|
|
|
|
|
for (const auto& [name, uid] : vendor_passwd) {
|
|
|
|
if (name == login) {
|
|
|
|
snprintf(static_name, sizeof(static_name), "%s", name.c_str());
|
|
|
|
static_passwd.pw_uid = uid;
|
|
|
|
static_passwd.pw_gid = uid;
|
|
|
|
return &static_passwd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = ENOENT;
|
|
|
|
return nullptr;
|
2018-02-14 00:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
|
|
|
static Result<Success> do_stub(const BuiltinArguments& args) {
|
|
|
|
return Success();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "generated_stub_builtin_function_map.h"
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2018-05-23 16:16:46 +00:00
|
|
|
android::base::InitLogging(argv, &android::base::StdioLogger);
|
2018-05-10 00:38:30 +00:00
|
|
|
android::base::SetMinimumLogSeverity(android::base::ERROR);
|
|
|
|
if (argc != 3) {
|
|
|
|
LOG(ERROR) << "Usage: " << argv[0] << " <out directory> <properties>";
|
2018-02-14 00:50:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-05-10 00:38:30 +00:00
|
|
|
|
2018-05-10 01:33:31 +00:00
|
|
|
out_dir = argv[1];
|
|
|
|
|
2018-05-10 00:38:30 +00:00
|
|
|
auto properties = Split(argv[2], ",");
|
|
|
|
for (const auto& property : properties) {
|
|
|
|
auto split_property = Split(property, "=");
|
|
|
|
if (split_property.size() != 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
property_set(split_property[0], split_property[1]);
|
|
|
|
}
|
|
|
|
|
2018-02-14 00:50:08 +00:00
|
|
|
const BuiltinFunctionMap function_map;
|
|
|
|
Action::set_function_map(&function_map);
|
|
|
|
ActionManager& am = ActionManager::GetInstance();
|
|
|
|
ServiceList& sl = ServiceList::GetInstance();
|
|
|
|
Parser parser;
|
|
|
|
parser.AddSectionParser("service", std::make_unique<ServiceParser>(&sl, nullptr));
|
|
|
|
parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
|
2018-05-10 01:33:31 +00:00
|
|
|
parser.AddSectionParser("import", std::make_unique<HostImportParser>(out_dir, &parser));
|
2018-02-14 00:50:08 +00:00
|
|
|
|
2018-05-10 00:38:30 +00:00
|
|
|
if (!parser.ParseConfig(argv[1] + "/root/init.rc"s)) {
|
|
|
|
LOG(ERROR) << "Failed to find root init.rc script";
|
2018-02-14 00:50:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-05-10 00:38:30 +00:00
|
|
|
if (parser.parse_error_count() > 0) {
|
|
|
|
LOG(ERROR) << "Init script parsing failed with " << parser.parse_error_count() << " errors";
|
2018-02-14 00:50:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
android::init::main(argc, argv);
|
|
|
|
}
|