Merge "Relax host init parser to work with the build system"

This commit is contained in:
Tom Cherry 2018-06-15 17:03:42 +00:00 committed by Gerrit Code Review
commit 4632f4e868
3 changed files with 28 additions and 42 deletions

View file

@ -23,22 +23,17 @@ using android::base::StartsWith;
namespace android {
namespace init {
Result<Success> HostImportParser::ParseSection(std::vector<std::string>&& args,
const std::string& filename, int line) {
Result<Success> HostImportParser::ParseSection(std::vector<std::string>&& args, const std::string&,
int) {
if (args.size() != 2) {
return Error() << "single argument needed for import\n";
}
auto import_path = args[1];
return Success();
}
if (StartsWith(import_path, "/system") || StartsWith(import_path, "/product") ||
StartsWith(import_path, "/odm") || StartsWith(import_path, "/vendor")) {
import_path = out_dir_ + "/" + import_path;
} else {
import_path = out_dir_ + "/root/" + import_path;
}
return ImportParser::ParseSection({"import", import_path}, filename, line);
Result<Success> HostImportParser::ParseLineSection(std::vector<std::string>&&, int) {
return Error() << "Unexpected line found after import statement";
}
} // namespace init

View file

@ -19,21 +19,16 @@
#include <string>
#include <vector>
#include "import_parser.h"
#include "parser.h"
namespace android {
namespace init {
class HostImportParser : public ImportParser {
class HostImportParser : public SectionParser {
public:
HostImportParser(const std::string& out_dir, Parser* parser)
: ImportParser(parser), out_dir_(out_dir) {}
Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
int line) override;
private:
std::string out_dir_;
HostImportParser() {}
Result<Success> ParseSection(std::vector<std::string>&& args, const std::string&, int) override;
Result<Success> ParseLineSection(std::vector<std::string>&&, int) override;
};
} // namespace init

View file

@ -17,6 +17,7 @@
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
@ -45,11 +46,11 @@ using android::base::ParseInt;
using android::base::ReadFileToString;
using android::base::Split;
static std::string out_dir;
static std::string passwd_file;
static std::vector<std::pair<std::string, int>> GetVendorPasswd() {
std::string passwd;
if (!ReadFileToString(out_dir + "/vendor/etc/passwd", &passwd)) {
if (!ReadFileToString(passwd_file, &passwd)) {
return {};
}
@ -118,20 +119,14 @@ static Result<Success> do_stub(const BuiltinArguments& args) {
int main(int argc, char** argv) {
android::base::InitLogging(argv, &android::base::StdioLogger);
android::base::SetMinimumLogSeverity(android::base::ERROR);
if (argc != 3) {
LOG(ERROR) << "Usage: " << argv[0] << " <out directory> <properties>";
return -1;
if (argc != 2 && argc != 3) {
LOG(ERROR) << "Usage: " << argv[0] << " <init rc file> [passwd file]";
return EXIT_FAILURE;
}
out_dir = argv[1];
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]);
if (argc == 3) {
passwd_file = argv[2];
}
const BuiltinFunctionMap function_map;
@ -141,22 +136,23 @@ int main(int argc, char** argv) {
Parser parser;
parser.AddSectionParser("service", std::make_unique<ServiceParser>(&sl, nullptr));
parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
parser.AddSectionParser("import", std::make_unique<HostImportParser>(out_dir, &parser));
parser.AddSectionParser("import", std::make_unique<HostImportParser>());
if (!parser.ParseConfig(argv[1] + "/root/init.rc"s)) {
LOG(ERROR) << "Failed to find root init.rc script";
return -1;
if (!parser.ParseConfig(argv[1])) {
LOG(ERROR) << "Failed to open init rc script '" << argv[1] << "'";
return EXIT_FAILURE;
}
if (parser.parse_error_count() > 0) {
LOG(ERROR) << "Init script parsing failed with " << parser.parse_error_count() << " errors";
return -1;
LOG(ERROR) << "Failed to parse init script '" << argv[1] << "' with "
<< parser.parse_error_count() << " errors";
return EXIT_FAILURE;
}
return 0;
return EXIT_SUCCESS;
}
} // namespace init
} // namespace android
int main(int argc, char** argv) {
android::init::main(argc, argv);
return android::init::main(argc, argv);
}