From bbcbc2ffb339b2388e0cc282bb698fe436100b42 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Mon, 10 Jun 2019 11:08:01 -0700 Subject: [PATCH] init: replace Result with Result Now that Result is actually expected, and the expected proposal states expected as the way to indicate an expected object that returns either successfully with no object or an error, let's move init's Result to the preferred Result. Bug: 132145659 Test: boot, init unit tests Change-Id: Ib2f98396d8e6e274f95a496fcdfd8341f77585ee --- init/action.cpp | 11 +- init/action.h | 8 +- init/action_parser.cpp | 28 ++-- init/action_parser.h | 8 +- init/bootchart.cpp | 14 +- init/bootchart.h | 2 +- init/boringssl_self_test.cpp | 4 +- init/boringssl_self_test.h | 2 +- init/builtins.cpp | 242 ++++++++++++++++----------------- init/builtins.h | 2 +- init/epoll.cpp | 20 +-- init/epoll.h | 9 +- init/host_import_parser.cpp | 8 +- init/host_import_parser.h | 4 +- init/host_init_verifier.cpp | 4 +- init/import_parser.cpp | 8 +- init/import_parser.h | 6 +- init/init.cpp | 56 ++++---- init/init_test.cpp | 2 +- init/modalias_handler.cpp | 18 +-- init/modalias_handler.h | 8 +- init/parser.h | 12 +- init/persistent_properties.cpp | 4 +- init/persistent_properties.h | 2 +- init/reboot.cpp | 2 +- init/result.h | 58 +------- init/security.cpp | 22 +-- init/security.h | 6 +- init/service.cpp | 172 +++++++++++------------ init/service.h | 90 ++++++------ init/service_utils.cpp | 25 ++-- init/service_utils.h | 6 +- init/subcontext.cpp | 10 +- init/subcontext.h | 2 +- init/subcontext_benchmark.cpp | 2 +- init/subcontext_test.cpp | 18 +-- init/test_function_map.h | 8 +- init/ueventd_parser.cpp | 62 ++++----- init/util.cpp | 4 +- init/util.h | 2 +- 40 files changed, 457 insertions(+), 514 deletions(-) diff --git a/init/action.cpp b/init/action.cpp index a40172e68..a1695912e 100644 --- a/init/action.cpp +++ b/init/action.cpp @@ -28,9 +28,8 @@ using android::base::Join; namespace android { namespace init { -Result RunBuiltinFunction(const BuiltinFunction& function, - const std::vector& args, - const std::string& context) { +Result RunBuiltinFunction(const BuiltinFunction& function, + const std::vector& args, const std::string& context) { auto builtin_arguments = BuiltinArguments(context); builtin_arguments.args.resize(args.size()); @@ -51,7 +50,7 @@ Command::Command(BuiltinFunction f, bool execute_in_subcontext, std::vector Command::InvokeFunc(Subcontext* subcontext) const { +Result Command::InvokeFunc(Subcontext* subcontext) const { if (subcontext) { if (execute_in_subcontext_) { return subcontext->Execute(args_); @@ -83,7 +82,7 @@ Action::Action(bool oneshot, Subcontext* subcontext, const std::string& filename const KeywordFunctionMap* Action::function_map_ = nullptr; -Result Action::AddCommand(std::vector&& args, int line) { +Result Action::AddCommand(std::vector&& args, int line) { if (!function_map_) { return Error() << "no function map available"; } @@ -92,7 +91,7 @@ Result Action::AddCommand(std::vector&& args, int line) { if (!function) return Error() << function.error(); commands_.emplace_back(function->second, function->first, std::move(args), line); - return Success(); + return {}; } void Action::AddCommand(BuiltinFunction f, std::vector&& args, int line) { diff --git a/init/action.h b/init/action.h index 967c68230..13b250a71 100644 --- a/init/action.h +++ b/init/action.h @@ -31,15 +31,15 @@ namespace android { namespace init { -Result RunBuiltinFunction(const BuiltinFunction& function, - const std::vector& args, const std::string& context); +Result RunBuiltinFunction(const BuiltinFunction& function, + const std::vector& args, const std::string& context); class Command { public: Command(BuiltinFunction f, bool execute_in_subcontext, std::vector&& args, int line); - Result InvokeFunc(Subcontext* subcontext) const; + Result InvokeFunc(Subcontext* subcontext) const; std::string BuildCommandString() const; int line() const { return line_; } @@ -61,7 +61,7 @@ class Action { const std::string& event_trigger, const std::map& property_triggers); - Result AddCommand(std::vector&& args, int line); + Result AddCommand(std::vector&& args, int line); void AddCommand(BuiltinFunction f, std::vector&& args, int line); std::size_t NumCommands() const; void ExecuteOneCommand(std::size_t command) const; diff --git a/init/action_parser.cpp b/init/action_parser.cpp index 4f8bd166a..ff20e43a3 100644 --- a/init/action_parser.cpp +++ b/init/action_parser.cpp @@ -55,8 +55,8 @@ bool IsActionableProperty(Subcontext* subcontext, const std::string& prop_name) return CanReadProperty(subcontext->context(), prop_name); } -Result ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext, - std::map* property_triggers) { +Result ParsePropertyTrigger(const std::string& trigger, Subcontext* subcontext, + std::map* property_triggers) { const static std::string prop_str("property:"); std::string prop_name(trigger.substr(prop_str.length())); size_t equal_pos = prop_name.find('='); @@ -74,12 +74,12 @@ Result ParsePropertyTrigger(const std::string& trigger, Subcontext* sub if (auto [it, inserted] = property_triggers->emplace(prop_name, prop_value); !inserted) { return Error() << "multiple property triggers found for same property"; } - return Success(); + return {}; } -Result ParseTriggers(const std::vector& args, Subcontext* subcontext, - std::string* event_trigger, - std::map* property_triggers) { +Result ParseTriggers(const std::vector& args, Subcontext* subcontext, + std::string* event_trigger, + std::map* property_triggers) { const static std::string prop_str("property:"); for (std::size_t i = 0; i < args.size(); ++i) { if (args[i].empty()) { @@ -108,13 +108,13 @@ Result ParseTriggers(const std::vector& args, Subcontext* } } - return Success(); + return {}; } } // namespace -Result ActionParser::ParseSection(std::vector&& args, - const std::string& filename, int line) { +Result ActionParser::ParseSection(std::vector&& args, + const std::string& filename, int line) { std::vector triggers(args.begin() + 1, args.end()); if (triggers.size() < 1) { return Error() << "Actions must have a trigger"; @@ -142,19 +142,19 @@ Result ActionParser::ParseSection(std::vector&& args, property_triggers); action_ = std::move(action); - return Success(); + return {}; } -Result ActionParser::ParseLineSection(std::vector&& args, int line) { - return action_ ? action_->AddCommand(std::move(args), line) : Success(); +Result ActionParser::ParseLineSection(std::vector&& args, int line) { + return action_ ? action_->AddCommand(std::move(args), line) : Result{}; } -Result ActionParser::EndSection() { +Result ActionParser::EndSection() { if (action_ && action_->NumCommands() > 0) { action_manager_->AddAction(std::move(action_)); } - return Success(); + return {}; } } // namespace init diff --git a/init/action_parser.h b/init/action_parser.h index b7f70743a..2fe9983a3 100644 --- a/init/action_parser.h +++ b/init/action_parser.h @@ -32,10 +32,10 @@ class ActionParser : public SectionParser { public: ActionParser(ActionManager* action_manager, std::vector* subcontexts) : action_manager_(action_manager), subcontexts_(subcontexts), action_(nullptr) {} - Result ParseSection(std::vector&& args, const std::string& filename, - int line) override; - Result ParseLineSection(std::vector&& args, int line) override; - Result EndSection() override; + Result ParseSection(std::vector&& args, const std::string& filename, + int line) override; + Result ParseLineSection(std::vector&& args, int line) override; + Result EndSection() override; private: ActionManager* action_manager_; diff --git a/init/bootchart.cpp b/init/bootchart.cpp index c2cf57399..b7db9b6d6 100644 --- a/init/bootchart.cpp +++ b/init/bootchart.cpp @@ -165,20 +165,20 @@ static void bootchart_thread_main() { LOG(INFO) << "Bootcharting finished"; } -static Result do_bootchart_start() { +static Result do_bootchart_start() { // We don't care about the content, but we do care that /data/bootchart/enabled actually exists. std::string start; if (!android::base::ReadFileToString("/data/bootchart/enabled", &start)) { LOG(VERBOSE) << "Not bootcharting"; - return Success(); + return {}; } g_bootcharting_thread = new std::thread(bootchart_thread_main); - return Success(); + return {}; } -static Result do_bootchart_stop() { - if (!g_bootcharting_thread) return Success(); +static Result do_bootchart_stop() { + if (!g_bootcharting_thread) return {}; // Tell the worker thread it's time to quit. { @@ -190,10 +190,10 @@ static Result do_bootchart_stop() { g_bootcharting_thread->join(); delete g_bootcharting_thread; g_bootcharting_thread = nullptr; - return Success(); + return {}; } -Result do_bootchart(const BuiltinArguments& args) { +Result do_bootchart(const BuiltinArguments& args) { if (args[1] == "start") return do_bootchart_start(); return do_bootchart_stop(); } diff --git a/init/bootchart.h b/init/bootchart.h index 05474ca09..6f19aadb0 100644 --- a/init/bootchart.h +++ b/init/bootchart.h @@ -26,7 +26,7 @@ namespace android { namespace init { -Result do_bootchart(const BuiltinArguments& args); +Result do_bootchart(const BuiltinArguments& args); } // namespace init } // namespace android diff --git a/init/boringssl_self_test.cpp b/init/boringssl_self_test.cpp index 0408d30eb..759eb43cf 100644 --- a/init/boringssl_self_test.cpp +++ b/init/boringssl_self_test.cpp @@ -25,7 +25,7 @@ namespace android { namespace init { -Result StartBoringSslSelfTest(const BuiltinArguments&) { +Result StartBoringSslSelfTest(const BuiltinArguments&) { pid_t id = fork(); if (id == 0) { @@ -49,7 +49,7 @@ Result StartBoringSslSelfTest(const BuiltinArguments&) { PLOG(FATAL) << "Failed to fork for BoringSSL self test"; } - return Success(); + return {}; } } // namespace init diff --git a/init/boringssl_self_test.h b/init/boringssl_self_test.h index b21fc7883..9e717d0bd 100644 --- a/init/boringssl_self_test.h +++ b/init/boringssl_self_test.h @@ -22,7 +22,7 @@ namespace android { namespace init { -Result StartBoringSslSelfTest(const BuiltinArguments&); +Result StartBoringSslSelfTest(const BuiltinArguments&); } // namespace init } // namespace android diff --git a/init/builtins.cpp b/init/builtins.cpp index e9d58c605..2a583e896 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -90,14 +90,14 @@ namespace init { static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s; -static Result reboot_into_recovery(const std::vector& options) { +static Result reboot_into_recovery(const std::vector& options) { LOG(ERROR) << "Rebooting into recovery"; std::string err; if (!write_bootloader_message(options, &err)) { return Error() << "Failed to set bootloader message: " << err; } property_set("sys.powerctl", "reboot,recovery"); - return Success(); + return {}; } template @@ -107,10 +107,10 @@ static void ForEachServiceInClass(const std::string& classname, F function) { } } -static Result do_class_start(const BuiltinArguments& args) { +static Result do_class_start(const BuiltinArguments& args) { // Do not start a class if it has a property persist.dont_start_class.CLASS set to 1. if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false)) - return Success(); + return {}; // Starting a class does not start services which are explicitly disabled. // They must be started individually. for (const auto& service : ServiceList::GetInstance()) { @@ -121,10 +121,10 @@ static Result do_class_start(const BuiltinArguments& args) { } } } - return Success(); + return {}; } -static Result do_class_start_post_data(const BuiltinArguments& args) { +static Result do_class_start_post_data(const BuiltinArguments& args) { if (args.context != kInitContext) { return Error() << "command 'class_start_post_data' only available in init context"; } @@ -136,43 +136,43 @@ static Result do_class_start_post_data(const BuiltinArguments& args) { } } } - return Success(); + return {}; } -static Result do_class_stop(const BuiltinArguments& args) { +static Result do_class_stop(const BuiltinArguments& args) { ForEachServiceInClass(args[1], &Service::Stop); - return Success(); + return {}; } -static Result do_class_reset(const BuiltinArguments& args) { +static Result do_class_reset(const BuiltinArguments& args) { ForEachServiceInClass(args[1], &Service::Reset); - return Success(); + return {}; } -static Result do_class_reset_post_data(const BuiltinArguments& args) { +static Result do_class_reset_post_data(const BuiltinArguments& args) { if (args.context != kInitContext) { return Error() << "command 'class_reset_post_data' only available in init context"; } ForEachServiceInClass(args[1], &Service::ResetIfPostData); - return Success(); + return {}; } -static Result do_class_restart(const BuiltinArguments& args) { +static Result do_class_restart(const BuiltinArguments& args) { // Do not restart a class if it has a property persist.dont_start_class.CLASS set to 1. if (android::base::GetBoolProperty("persist.init.dont_start_class." + args[1], false)) - return Success(); + return {}; ForEachServiceInClass(args[1], &Service::Restart); - return Success(); + return {}; } -static Result do_domainname(const BuiltinArguments& args) { +static Result do_domainname(const BuiltinArguments& args) { if (auto result = WriteFile("/proc/sys/kernel/domainname", args[1]); !result) { return Error() << "Unable to write to /proc/sys/kernel/domainname: " << result.error(); } - return Success(); + return {}; } -static Result do_enable(const BuiltinArguments& args) { +static Result do_enable(const BuiltinArguments& args) { Service* svc = ServiceList::GetInstance().FindService(args[1]); if (!svc) return Error() << "Could not find service"; @@ -180,10 +180,10 @@ static Result do_enable(const BuiltinArguments& args) { return Error() << "Could not enable service: " << result.error(); } - return Success(); + return {}; } -static Result do_exec(const BuiltinArguments& args) { +static Result do_exec(const BuiltinArguments& args) { auto service = Service::MakeTemporaryOneshotService(args.args); if (!service) { return Error() << "Could not create exec service"; @@ -193,10 +193,10 @@ static Result do_exec(const BuiltinArguments& args) { } ServiceList::GetInstance().AddService(std::move(service)); - return Success(); + return {}; } -static Result do_exec_background(const BuiltinArguments& args) { +static Result do_exec_background(const BuiltinArguments& args) { auto service = Service::MakeTemporaryOneshotService(args.args); if (!service) { return Error() << "Could not create exec background service"; @@ -206,10 +206,10 @@ static Result do_exec_background(const BuiltinArguments& args) { } ServiceList::GetInstance().AddService(std::move(service)); - return Success(); + return {}; } -static Result do_exec_start(const BuiltinArguments& args) { +static Result do_exec_start(const BuiltinArguments& args) { Service* service = ServiceList::GetInstance().FindService(args[1]); if (!service) { return Error() << "Service not found"; @@ -219,24 +219,24 @@ static Result do_exec_start(const BuiltinArguments& args) { return Error() << "Could not start exec service: " << result.error(); } - return Success(); + return {}; } -static Result do_export(const BuiltinArguments& args) { +static Result do_export(const BuiltinArguments& args) { if (setenv(args[1].c_str(), args[2].c_str(), 1) == -1) { return ErrnoError() << "setenv() failed"; } - return Success(); + return {}; } -static Result do_hostname(const BuiltinArguments& args) { +static Result do_hostname(const BuiltinArguments& args) { if (auto result = WriteFile("/proc/sys/kernel/hostname", args[1]); !result) { return Error() << "Unable to write to /proc/sys/kernel/hostname: " << result.error(); } - return Success(); + return {}; } -static Result do_ifup(const BuiltinArguments& args) { +static Result do_ifup(const BuiltinArguments& args) { struct ifreq ifr; strlcpy(ifr.ifr_name, args[1].c_str(), IFNAMSIZ); @@ -254,10 +254,10 @@ static Result do_ifup(const BuiltinArguments& args) { return ErrnoError() << "ioctl(..., SIOCSIFFLAGS, ...) failed"; } - return Success(); + return {}; } -static Result do_insmod(const BuiltinArguments& args) { +static Result do_insmod(const BuiltinArguments& args) { int flags = 0; auto it = args.begin() + 1; @@ -275,34 +275,34 @@ static Result do_insmod(const BuiltinArguments& args) { int rc = syscall(__NR_finit_module, fd.get(), options.c_str(), flags); if (rc == -1) return ErrnoError() << "finit_module for \"" << filename << "\" failed"; - return Success(); + return {}; } -static Result do_interface_restart(const BuiltinArguments& args) { +static Result do_interface_restart(const BuiltinArguments& args) { Service* svc = ServiceList::GetInstance().FindInterface(args[1]); if (!svc) return Error() << "interface " << args[1] << " not found"; svc->Restart(); - return Success(); + return {}; } -static Result do_interface_start(const BuiltinArguments& args) { +static Result do_interface_start(const BuiltinArguments& args) { Service* svc = ServiceList::GetInstance().FindInterface(args[1]); if (!svc) return Error() << "interface " << args[1] << " not found"; if (auto result = svc->Start(); !result) { return Error() << "Could not start interface: " << result.error(); } - return Success(); + return {}; } -static Result do_interface_stop(const BuiltinArguments& args) { +static Result do_interface_stop(const BuiltinArguments& args) { Service* svc = ServiceList::GetInstance().FindInterface(args[1]); if (!svc) return Error() << "interface " << args[1] << " not found"; svc->Stop(); - return Success(); + return {}; } // mkdir [mode] [owner] [group] -static Result do_mkdir(const BuiltinArguments& args) { +static Result do_mkdir(const BuiltinArguments& args) { mode_t mode = 0755; if (args.size() >= 3) { mode = std::strtoul(args[2].c_str(), 0, 8); @@ -351,15 +351,15 @@ static Result do_mkdir(const BuiltinArguments& args) { {"--prompt_and_wipe_data", "--reason=set_policy_failed:"s + args[1]}); } } - return Success(); + return {}; } /* umount */ -static Result do_umount(const BuiltinArguments& args) { +static Result do_umount(const BuiltinArguments& args) { if (umount(args[1].c_str()) < 0) { return ErrnoError() << "umount() failed"; } - return Success(); + return {}; } static struct { @@ -387,7 +387,7 @@ static struct { #define DATA_MNT_POINT "/data" /* mount */ -static Result do_mount(const BuiltinArguments& args) { +static Result do_mount(const BuiltinArguments& args) { const char* options = nullptr; unsigned flags = 0; bool wait = false; @@ -434,7 +434,7 @@ static Result do_mount(const BuiltinArguments& args) { ioctl(loop, LOOP_CLR_FD, 0); return ErrnoError() << "mount() failed"; } - return Success(); + return {}; } } } @@ -449,7 +449,7 @@ static Result do_mount(const BuiltinArguments& args) { } - return Success(); + return {}; } /* Imports .rc files from the specified paths. Default ones are applied if none is given. @@ -486,23 +486,23 @@ static void import_late(const std::vector& args, size_t start_index * * return code is processed based on input code */ -static Result queue_fs_event(int code) { +static Result queue_fs_event(int code) { if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) { ActionManager::GetInstance().QueueEventTrigger("encrypt"); - return Success(); + return {}; } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) { property_set("ro.crypto.state", "encrypted"); property_set("ro.crypto.type", "block"); ActionManager::GetInstance().QueueEventTrigger("defaultcrypto"); - return Success(); + return {}; } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { property_set("ro.crypto.state", "unencrypted"); ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); - return Success(); + return {}; } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) { property_set("ro.crypto.state", "unsupported"); ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); - return Success(); + return {}; } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) { /* Setup a wipe via recovery, and reboot into recovery */ if (android::gsi::IsGsiRunning()) { @@ -522,7 +522,7 @@ static Result queue_fs_event(int code) { // Although encrypted, we have device key, so we do not need to // do anything different from the nonencrypted case. ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); - return Success(); + return {}; } else if (code == FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED) { if (fscrypt_install_keyring()) { return Error() << "fscrypt_install_keyring() failed"; @@ -533,7 +533,7 @@ static Result queue_fs_event(int code) { // Although encrypted, vold has already set the device up, so we do not need to // do anything different from the nonencrypted case. ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); - return Success(); + return {}; } else if (code == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) { if (fscrypt_install_keyring()) { return Error() << "fscrypt_install_keyring() failed"; @@ -544,7 +544,7 @@ static Result queue_fs_event(int code) { // Although encrypted, vold has already set the device up, so we do not need to // do anything different from the nonencrypted case. ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); - return Success(); + return {}; } else if (code > 0) { Error() << "fs_mgr_mount_all() returned unexpected error " << code; } @@ -558,7 +558,7 @@ static Result queue_fs_event(int code) { * This function might request a reboot, in which case it will * not return. */ -static Result do_mount_all(const BuiltinArguments& args) { +static Result do_mount_all(const BuiltinArguments& args) { std::size_t na = 0; bool import_rc = true; bool queue_event = true; @@ -605,11 +605,11 @@ static Result do_mount_all(const BuiltinArguments& args) { } } - return Success(); + return {}; } /* umount_all */ -static Result do_umount_all(const BuiltinArguments& args) { +static Result do_umount_all(const BuiltinArguments& args) { Fstab fstab; if (!ReadFstabFromFile(args[1], &fstab)) { return Error() << "Could not read fstab"; @@ -618,10 +618,10 @@ static Result do_umount_all(const BuiltinArguments& args) { if (auto result = fs_mgr_umount_all(&fstab); result != 0) { return Error() << "umount_fstab() failed " << result; } - return Success(); + return {}; } -static Result do_swapon_all(const BuiltinArguments& args) { +static Result do_swapon_all(const BuiltinArguments& args) { Fstab fstab; if (!ReadFstabFromFile(args[1], &fstab)) { return Error() << "Could not read fstab '" << args[1] << "'"; @@ -631,50 +631,50 @@ static Result do_swapon_all(const BuiltinArguments& args) { return Error() << "fs_mgr_swapon_all() failed"; } - return Success(); + return {}; } -static Result do_setprop(const BuiltinArguments& args) { +static Result do_setprop(const BuiltinArguments& args) { property_set(args[1], args[2]); - return Success(); + return {}; } -static Result do_setrlimit(const BuiltinArguments& args) { +static Result do_setrlimit(const BuiltinArguments& args) { auto rlimit = ParseRlimit(args.args); if (!rlimit) return rlimit.error(); if (setrlimit(rlimit->first, &rlimit->second) == -1) { return ErrnoError() << "setrlimit failed"; } - return Success(); + return {}; } -static Result do_start(const BuiltinArguments& args) { +static Result do_start(const BuiltinArguments& args) { Service* svc = ServiceList::GetInstance().FindService(args[1]); if (!svc) return Error() << "service " << args[1] << " not found"; if (auto result = svc->Start(); !result) { return Error() << "Could not start service: " << result.error(); } - return Success(); + return {}; } -static Result do_stop(const BuiltinArguments& args) { +static Result do_stop(const BuiltinArguments& args) { Service* svc = ServiceList::GetInstance().FindService(args[1]); if (!svc) return Error() << "service " << args[1] << " not found"; svc->Stop(); - return Success(); + return {}; } -static Result do_restart(const BuiltinArguments& args) { +static Result do_restart(const BuiltinArguments& args) { Service* svc = ServiceList::GetInstance().FindService(args[1]); if (!svc) return Error() << "service " << args[1] << " not found"; svc->Restart(); - return Success(); + return {}; } -static Result do_trigger(const BuiltinArguments& args) { +static Result do_trigger(const BuiltinArguments& args) { ActionManager::GetInstance().QueueEventTrigger(args[1]); - return Success(); + return {}; } static int MakeSymlink(const std::string& target, const std::string& linkpath) { @@ -695,33 +695,33 @@ static int MakeSymlink(const std::string& target, const std::string& linkpath) { return rc; } -static Result do_symlink(const BuiltinArguments& args) { +static Result do_symlink(const BuiltinArguments& args) { if (MakeSymlink(args[1], args[2]) < 0) { // The symlink builtin is often used to create symlinks for older devices to be backwards // compatible with new paths, therefore we skip reporting this error. if (errno == EEXIST && android::base::GetMinimumLogSeverity() > android::base::DEBUG) { - return Success(); + return {}; } return ErrnoError() << "symlink() failed"; } - return Success(); + return {}; } -static Result do_rm(const BuiltinArguments& args) { +static Result do_rm(const BuiltinArguments& args) { if (unlink(args[1].c_str()) < 0) { return ErrnoError() << "unlink() failed"; } - return Success(); + return {}; } -static Result do_rmdir(const BuiltinArguments& args) { +static Result do_rmdir(const BuiltinArguments& args) { if (rmdir(args[1].c_str()) < 0) { return ErrnoError() << "rmdir() failed"; } - return Success(); + return {}; } -static Result do_sysclktz(const BuiltinArguments& args) { +static Result do_sysclktz(const BuiltinArguments& args) { struct timezone tz = {}; if (!android::base::ParseInt(args[1], &tz.tz_minuteswest)) { return Error() << "Unable to parse mins_west_of_gmt"; @@ -730,10 +730,10 @@ static Result do_sysclktz(const BuiltinArguments& args) { if (settimeofday(nullptr, &tz) == -1) { return ErrnoError() << "settimeofday() failed"; } - return Success(); + return {}; } -static Result do_verity_update_state(const BuiltinArguments& args) { +static Result do_verity_update_state(const BuiltinArguments& args) { int mode; if (!fs_mgr_load_verity_state(&mode)) { return Error() << "fs_mgr_load_verity_state() failed"; @@ -755,18 +755,18 @@ static Result do_verity_update_state(const BuiltinArguments& args) { property_set("partition." + partition + ".verified", std::to_string(mode)); } - return Success(); + return {}; } -static Result do_write(const BuiltinArguments& args) { +static Result do_write(const BuiltinArguments& args) { if (auto result = WriteFile(args[1], args[2]); !result) { return Error() << "Unable to write to file '" << args[1] << "': " << result.error(); } - return Success(); + return {}; } -static Result readahead_file(const std::string& filename, bool fully) { +static Result readahead_file(const std::string& filename, bool fully) { android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY))); if (fd == -1) { return ErrnoError() << "Error opening file"; @@ -786,10 +786,10 @@ static Result readahead_file(const std::string& filename, bool fully) { return ErrnoError() << "Error reading file"; } } - return Success(); + return {}; } -static Result do_readahead(const BuiltinArguments& args) { +static Result do_readahead(const BuiltinArguments& args) { struct stat sb; if (stat(args[1].c_str(), &sb)) { @@ -845,10 +845,10 @@ static Result do_readahead(const BuiltinArguments& args) { } else if (pid < 0) { return ErrnoError() << "Fork failed"; } - return Success(); + return {}; } -static Result do_copy(const BuiltinArguments& args) { +static Result do_copy(const BuiltinArguments& args) { auto file_contents = ReadFile(args[1]); if (!file_contents) { return Error() << "Could not read input file '" << args[1] << "': " << file_contents.error(); @@ -857,10 +857,10 @@ static Result do_copy(const BuiltinArguments& args) { return Error() << "Could not write to output file '" << args[2] << "': " << result.error(); } - return Success(); + return {}; } -static Result do_chown(const BuiltinArguments& args) { +static Result do_chown(const BuiltinArguments& args) { auto uid = DecodeUid(args[1]); if (!uid) { return Error() << "Unable to decode UID for '" << args[1] << "': " << uid.error(); @@ -881,7 +881,7 @@ static Result do_chown(const BuiltinArguments& args) { return ErrnoError() << "lchown() failed"; } - return Success(); + return {}; } static mode_t get_mode(const char *s) { @@ -897,15 +897,15 @@ static mode_t get_mode(const char *s) { return mode; } -static Result do_chmod(const BuiltinArguments& args) { +static Result do_chmod(const BuiltinArguments& args) { mode_t mode = get_mode(args[1].c_str()); if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) { return ErrnoError() << "fchmodat() failed"; } - return Success(); + return {}; } -static Result do_restorecon(const BuiltinArguments& args) { +static Result do_restorecon(const BuiltinArguments& args) { int ret = 0; struct flag_type {const char* name; int value;}; @@ -944,16 +944,16 @@ static Result do_restorecon(const BuiltinArguments& args) { } if (ret) return ErrnoError() << "selinux_android_restorecon() failed"; - return Success(); + return {}; } -static Result do_restorecon_recursive(const BuiltinArguments& args) { +static Result do_restorecon_recursive(const BuiltinArguments& args) { std::vector non_const_args(args.args); non_const_args.insert(std::next(non_const_args.begin()), "--recursive"); return do_restorecon({std::move(non_const_args), args.context}); } -static Result do_loglevel(const BuiltinArguments& args) { +static Result do_loglevel(const BuiltinArguments& args) { // TODO: support names instead/as well? int log_level = -1; android::base::ParseInt(args[1], &log_level); @@ -971,20 +971,20 @@ static Result do_loglevel(const BuiltinArguments& args) { return Error() << "invalid log level " << log_level; } android::base::SetMinimumLogSeverity(severity); - return Success(); + return {}; } -static Result do_load_persist_props(const BuiltinArguments& args) { +static Result do_load_persist_props(const BuiltinArguments& args) { load_persist_props(); - return Success(); + return {}; } -static Result do_load_system_props(const BuiltinArguments& args) { +static Result do_load_system_props(const BuiltinArguments& args) { LOG(INFO) << "deprecated action `load_system_props` called."; - return Success(); + return {}; } -static Result do_wait(const BuiltinArguments& args) { +static Result do_wait(const BuiltinArguments& args) { auto timeout = kCommandRetryTimeout; if (args.size() == 3) { int timeout_int; @@ -998,10 +998,10 @@ static Result do_wait(const BuiltinArguments& args) { return Error() << "wait_for_file() failed"; } - return Success(); + return {}; } -static Result do_wait_for_prop(const BuiltinArguments& args) { +static Result do_wait_for_prop(const BuiltinArguments& args) { const char* name = args[1].c_str(); const char* value = args[2].c_str(); size_t value_len = strlen(value); @@ -1015,15 +1015,15 @@ static Result do_wait_for_prop(const BuiltinArguments& args) { if (!start_waiting_for_property(name, value)) { return Error() << "already waiting for a property"; } - return Success(); + return {}; } static bool is_file_crypto() { return android::base::GetProperty("ro.crypto.type", "") == "file"; } -static Result ExecWithRebootOnFailure(const std::string& reboot_reason, - const BuiltinArguments& args) { +static Result ExecWithRebootOnFailure(const std::string& reboot_reason, + const BuiltinArguments& args) { auto service = Service::MakeTemporaryOneshotService(args.args); if (!service) { return Error() << "Could not create exec service"; @@ -1047,11 +1047,11 @@ static Result ExecWithRebootOnFailure(const std::string& reboot_reason, return Error() << "Could not start exec service: " << result.error(); } ServiceList::GetInstance().AddService(std::move(service)); - return Success(); + return {}; } -static Result do_installkey(const BuiltinArguments& args) { - if (!is_file_crypto()) return Success(); +static Result do_installkey(const BuiltinArguments& args) { + if (!is_file_crypto()) return {}; auto unencrypted_dir = args[1] + fscrypt_unencrypted_folder; if (!make_dir(unencrypted_dir, 0700) && errno != EEXIST) { @@ -1062,19 +1062,19 @@ static Result do_installkey(const BuiltinArguments& args) { {{"exec", "/system/bin/vdc", "--wait", "cryptfs", "enablefilecrypto"}, args.context}); } -static Result do_init_user0(const BuiltinArguments& args) { +static Result do_init_user0(const BuiltinArguments& args) { return ExecWithRebootOnFailure( "init_user0_failed", {{"exec", "/system/bin/vdc", "--wait", "cryptfs", "init_user0"}, args.context}); } -static Result do_mark_post_data(const BuiltinArguments& args) { +static Result do_mark_post_data(const BuiltinArguments& args) { ServiceList::GetInstance().MarkPostData(); - return Success(); + return {}; } -static Result do_parse_apex_configs(const BuiltinArguments& args) { +static Result do_parse_apex_configs(const BuiltinArguments& args) { glob_t glob_result; // @ is added to filter out the later paths, which are bind mounts of the places // where the APEXes are really mounted at. Otherwise, we will parse the @@ -1102,15 +1102,15 @@ static Result do_parse_apex_configs(const BuiltinArguments& args) { } ServiceList::GetInstance().MarkServicesUpdate(); if (success) { - return Success(); + return {}; } else { return Error() << "Could not parse apex configs"; } } -static Result do_enter_default_mount_ns(const BuiltinArguments& args) { +static Result do_enter_default_mount_ns(const BuiltinArguments& args) { if (SwitchToDefaultMountNamespace()) { - return Success(); + return {}; } else { return Error() << "Failed to enter into default mount namespace"; } diff --git a/init/builtins.h b/init/builtins.h index 814b2d558..5db0d1cdd 100644 --- a/init/builtins.h +++ b/init/builtins.h @@ -29,7 +29,7 @@ namespace android { namespace init { -using BuiltinFunction = std::function(const BuiltinArguments&)>; +using BuiltinFunction = std::function(const BuiltinArguments&)>; using KeywordFunctionMap = KeywordMap>; class BuiltinFunctionMap : public KeywordFunctionMap { diff --git a/init/epoll.cpp b/init/epoll.cpp index 94dd55350..01d886717 100644 --- a/init/epoll.cpp +++ b/init/epoll.cpp @@ -28,17 +28,17 @@ namespace init { Epoll::Epoll() {} -Result Epoll::Open() { - if (epoll_fd_ >= 0) return Success(); +Result Epoll::Open() { + if (epoll_fd_ >= 0) return {}; epoll_fd_.reset(epoll_create1(EPOLL_CLOEXEC)); if (epoll_fd_ == -1) { return ErrnoError() << "epoll_create1 failed"; } - return Success(); + return {}; } -Result Epoll::RegisterHandler(int fd, std::function handler, uint32_t events) { +Result Epoll::RegisterHandler(int fd, std::function handler, uint32_t events) { if (!events) { return Error() << "Must specify events"; } @@ -52,24 +52,24 @@ Result Epoll::RegisterHandler(int fd, std::function handler, ui // pointer to the std::function in the map directly for epoll_ctl. ev.data.ptr = reinterpret_cast(&it->second); if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &ev) == -1) { - Result result = ErrnoError() << "epoll_ctl failed to add fd"; + Result result = ErrnoError() << "epoll_ctl failed to add fd"; epoll_handlers_.erase(fd); return result; } - return Success(); + return {}; } -Result Epoll::UnregisterHandler(int fd) { +Result Epoll::UnregisterHandler(int fd) { if (epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, nullptr) == -1) { return ErrnoError() << "epoll_ctl failed to remove fd"; } if (epoll_handlers_.erase(fd) != 1) { return Error() << "Attempting to remove epoll handler for FD without an existing handler"; } - return Success(); + return {}; } -Result Epoll::Wait(std::optional timeout) { +Result Epoll::Wait(std::optional timeout) { int timeout_ms = -1; if (timeout && timeout->count() < INT_MAX) { timeout_ms = timeout->count(); @@ -81,7 +81,7 @@ Result Epoll::Wait(std::optional timeout) { } else if (nr == 1) { std::invoke(*reinterpret_cast*>(ev.data.ptr)); } - return Success(); + return {}; } } // namespace init diff --git a/init/epoll.h b/init/epoll.h index 9789bef58..ca8426634 100644 --- a/init/epoll.h +++ b/init/epoll.h @@ -36,11 +36,10 @@ class Epoll { public: Epoll(); - Result Open(); - Result RegisterHandler(int fd, std::function handler, - uint32_t events = EPOLLIN); - Result UnregisterHandler(int fd); - Result Wait(std::optional timeout); + Result Open(); + Result RegisterHandler(int fd, std::function handler, uint32_t events = EPOLLIN); + Result UnregisterHandler(int fd); + Result Wait(std::optional timeout); private: android::base::unique_fd epoll_fd_; diff --git a/init/host_import_parser.cpp b/init/host_import_parser.cpp index 93e363ff3..aa80199fc 100644 --- a/init/host_import_parser.cpp +++ b/init/host_import_parser.cpp @@ -23,16 +23,16 @@ using android::base::StartsWith; namespace android { namespace init { -Result HostImportParser::ParseSection(std::vector&& args, const std::string&, - int) { +Result HostImportParser::ParseSection(std::vector&& args, const std::string&, + int) { if (args.size() != 2) { return Error() << "single argument needed for import\n"; } - return Success(); + return {}; } -Result HostImportParser::ParseLineSection(std::vector&&, int) { +Result HostImportParser::ParseLineSection(std::vector&&, int) { return Error() << "Unexpected line found after import statement"; } diff --git a/init/host_import_parser.h b/init/host_import_parser.h index 52b889196..d6f7286b0 100644 --- a/init/host_import_parser.h +++ b/init/host_import_parser.h @@ -27,8 +27,8 @@ namespace init { class HostImportParser : public SectionParser { public: HostImportParser() {} - Result ParseSection(std::vector&& args, const std::string&, int) override; - Result ParseLineSection(std::vector&&, int) override; + Result ParseSection(std::vector&& args, const std::string&, int) override; + Result ParseLineSection(std::vector&&, int) override; }; } // namespace init diff --git a/init/host_init_verifier.cpp b/init/host_init_verifier.cpp index 84077291c..cb861f391 100644 --- a/init/host_init_verifier.cpp +++ b/init/host_init_verifier.cpp @@ -118,8 +118,8 @@ passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. namespace android { namespace init { -static Result do_stub(const BuiltinArguments& args) { - return Success(); +static Result do_stub(const BuiltinArguments& args) { + return {}; } #include "generated_stub_builtin_function_map.h" diff --git a/init/import_parser.cpp b/init/import_parser.cpp index fb3185e83..c72b7d65e 100644 --- a/init/import_parser.cpp +++ b/init/import_parser.cpp @@ -23,8 +23,8 @@ namespace android { namespace init { -Result ImportParser::ParseSection(std::vector&& args, - const std::string& filename, int line) { +Result ImportParser::ParseSection(std::vector&& args, + const std::string& filename, int line) { if (args.size() != 2) { return Error() << "single argument needed for import\n"; } @@ -38,10 +38,10 @@ Result ImportParser::ParseSection(std::vector&& args, LOG(INFO) << "Added '" << conf_file << "' to import list"; if (filename_.empty()) filename_ = filename; imports_.emplace_back(std::move(conf_file), line); - return Success(); + return {}; } -Result ImportParser::ParseLineSection(std::vector&&, int) { +Result ImportParser::ParseLineSection(std::vector&&, int) { return Error() << "Unexpected line found after import statement"; } diff --git a/init/import_parser.h b/init/import_parser.h index 7bc72e6e8..5bf9c6c66 100644 --- a/init/import_parser.h +++ b/init/import_parser.h @@ -28,9 +28,9 @@ namespace init { class ImportParser : public SectionParser { public: ImportParser(Parser* parser) : parser_(parser) {} - Result ParseSection(std::vector&& args, const std::string& filename, - int line) override; - Result ParseLineSection(std::vector&&, int) override; + Result ParseSection(std::vector&& args, const std::string& filename, + int line) override; + Result ParseLineSection(std::vector&&, int) override; void EndFile() override; private: diff --git a/init/init.cpp b/init/init.cpp index 8ce96f6e4..1412e4a5e 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -237,18 +237,18 @@ static std::optional HandleProcessActions() { return next_process_action_time; } -static Result DoControlStart(Service* service) { +static Result DoControlStart(Service* service) { return service->Start(); } -static Result DoControlStop(Service* service) { +static Result DoControlStop(Service* service) { service->Stop(); - return Success(); + return {}; } -static Result DoControlRestart(Service* service) { +static Result DoControlRestart(Service* service) { service->Restart(); - return Success(); + return {}; } enum class ControlTarget { @@ -258,16 +258,16 @@ enum class ControlTarget { struct ControlMessageFunction { ControlTarget target; - std::function(Service*)> action; + std::function(Service*)> action; }; static const std::map& get_control_message_map() { // clang-format off static const std::map control_message_functions = { {"sigstop_on", {ControlTarget::SERVICE, - [](auto* service) { service->set_sigstop(true); return Success(); }}}, + [](auto* service) { service->set_sigstop(true); return Result{}; }}}, {"sigstop_off", {ControlTarget::SERVICE, - [](auto* service) { service->set_sigstop(false); return Success(); }}}, + [](auto* service) { service->set_sigstop(false); return Result{}; }}}, {"start", {ControlTarget::SERVICE, DoControlStart}}, {"stop", {ControlTarget::SERVICE, DoControlStop}}, {"restart", {ControlTarget::SERVICE, DoControlRestart}}, @@ -330,7 +330,7 @@ bool HandleControlMessage(const std::string& msg, const std::string& name, pid_t return true; } -static Result wait_for_coldboot_done_action(const BuiltinArguments& args) { +static Result wait_for_coldboot_done_action(const BuiltinArguments& args) { Timer t; LOG(VERBOSE) << "Waiting for " COLDBOOT_DONE "..."; @@ -348,18 +348,18 @@ static Result wait_for_coldboot_done_action(const BuiltinArguments& arg } property_set("ro.boottime.init.cold_boot_wait", std::to_string(t.duration().count())); - return Success(); + return {}; } -static Result console_init_action(const BuiltinArguments& args) { +static Result console_init_action(const BuiltinArguments& args) { std::string console = GetProperty("ro.boot.console", ""); if (!console.empty()) { default_console = "/dev/" + console; } - return Success(); + return {}; } -static Result SetupCgroupsAction(const BuiltinArguments&) { +static Result SetupCgroupsAction(const BuiltinArguments&) { // Have to create using make_dir function // for appropriate sepolicy to be set for it make_dir(android::base::Dirname(CGROUPS_RC_PATH), 0711); @@ -367,7 +367,7 @@ static Result SetupCgroupsAction(const BuiltinArguments&) { return ErrnoError() << "Failed to setup cgroups"; } - return Success(); + return {}; } static void import_kernel_nv(const std::string& key, const std::string& value, bool for_emulator) { @@ -451,19 +451,19 @@ static void process_kernel_cmdline() { if (qemu[0]) import_kernel_cmdline(true, import_kernel_nv); } -static Result property_enable_triggers_action(const BuiltinArguments& args) { +static Result property_enable_triggers_action(const BuiltinArguments& args) { /* Enable property triggers. */ property_triggers_enabled = 1; - return Success(); + return {}; } -static Result queue_property_triggers_action(const BuiltinArguments& args) { +static Result queue_property_triggers_action(const BuiltinArguments& args) { ActionManager::GetInstance().QueueBuiltinAction(property_enable_triggers_action, "enable_property_trigger"); ActionManager::GetInstance().QueueAllPropertyActions(); - return Success(); + return {}; } -static Result InitBinder(const BuiltinArguments& args) { +static Result InitBinder(const BuiltinArguments& args) { // init's use of binder is very limited. init cannot: // - have any binder threads // - receive incoming binder calls @@ -478,7 +478,7 @@ static Result InitBinder(const BuiltinArguments& args) { android::ProcessState::self()->setCallRestriction( ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY); #endif - return Success(); + return {}; } // Set the UDC controller for the ConfigFS USB Gadgets. @@ -757,14 +757,14 @@ int SecondStageMain(int argc, char** argv) { am.QueueBuiltinAction(SetKptrRestrictAction, "SetKptrRestrict"); Keychords keychords; am.QueueBuiltinAction( - [&epoll, &keychords](const BuiltinArguments& args) -> Result { - for (const auto& svc : ServiceList::GetInstance()) { - keychords.Register(svc->keycodes()); - } - keychords.Start(&epoll, HandleKeychord); - return Success(); - }, - "KeychordInit"); + [&epoll, &keychords](const BuiltinArguments& args) -> Result { + for (const auto& svc : ServiceList::GetInstance()) { + keychords.Register(svc->keycodes()); + } + keychords.Start(&epoll, HandleKeychord); + return {}; + }, + "KeychordInit"); am.QueueBuiltinAction(console_init_action, "console_init"); // Trigger all the boot actions to get us started. diff --git a/init/init_test.cpp b/init/init_test.cpp index c2f0c41e3..18c2b38eb 100644 --- a/init/init_test.cpp +++ b/init/init_test.cpp @@ -180,7 +180,7 @@ TEST(init, EventTriggerOrderMultipleFiles) { auto execute_command = [&num_executed](const BuiltinArguments& args) { EXPECT_EQ(2U, args.size()); EXPECT_EQ(++num_executed, std::stoi(args[1])); - return Success(); + return Result{}; }; TestFunctionMap test_function_map; diff --git a/init/modalias_handler.cpp b/init/modalias_handler.cpp index c61c21039..a51115628 100644 --- a/init/modalias_handler.cpp +++ b/init/modalias_handler.cpp @@ -33,7 +33,7 @@ namespace android { namespace init { -Result ModaliasHandler::ParseDepCallback(std::vector&& args) { +Result ModaliasHandler::ParseDepCallback(std::vector&& args) { std::vector deps; // Set first item as our modules path @@ -58,10 +58,10 @@ Result ModaliasHandler::ParseDepCallback(std::vector&& arg std::replace(mod_name.begin(), mod_name.end(), '-', '_'); this->module_deps_[mod_name] = deps; - return Success(); + return {}; } -Result ModaliasHandler::ParseAliasCallback(std::vector&& args) { +Result ModaliasHandler::ParseAliasCallback(std::vector&& args) { auto it = args.begin(); const std::string& type = *it++; @@ -77,7 +77,7 @@ Result ModaliasHandler::ParseAliasCallback(std::vector&& a std::string& module_name = *it++; this->module_aliases_.emplace_back(alias, module_name); - return Success(); + return {}; } ModaliasHandler::ModaliasHandler() { @@ -100,7 +100,7 @@ ModaliasHandler::ModaliasHandler() { for (const auto& base_path : base_paths) dep_parser.ParseConfig(base_path + "modules.dep"); } -Result ModaliasHandler::Insmod(const std::string& path_name, const std::string& args) { +Result ModaliasHandler::Insmod(const std::string& path_name, const std::string& args) { base::unique_fd fd( TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC))); if (fd == -1) return ErrnoError() << "Could not open module '" << path_name << "'"; @@ -109,17 +109,17 @@ Result ModaliasHandler::Insmod(const std::string& path_name, const std: if (ret != 0) { if (errno == EEXIST) { // Module already loaded - return Success(); + return {}; } return ErrnoError() << "Failed to insmod '" << path_name << "' with args '" << args << "'"; } LOG(INFO) << "Loaded kernel module " << path_name; - return Success(); + return {}; } -Result ModaliasHandler::InsmodWithDeps(const std::string& module_name, - const std::string& args) { +Result ModaliasHandler::InsmodWithDeps(const std::string& module_name, + const std::string& args) { if (module_name.empty()) { return Error() << "Need valid module name"; } diff --git a/init/modalias_handler.h b/init/modalias_handler.h index 3247c86d4..7d0afded5 100644 --- a/init/modalias_handler.h +++ b/init/modalias_handler.h @@ -35,11 +35,11 @@ class ModaliasHandler : public UeventHandler { void HandleUevent(const Uevent& uevent) override; private: - Result InsmodWithDeps(const std::string& module_name, const std::string& args); - Result Insmod(const std::string& path_name, const std::string& args); + Result InsmodWithDeps(const std::string& module_name, const std::string& args); + Result Insmod(const std::string& path_name, const std::string& args); - Result ParseDepCallback(std::vector&& args); - Result ParseAliasCallback(std::vector&& args); + Result ParseDepCallback(std::vector&& args); + Result ParseAliasCallback(std::vector&& args); std::vector> module_aliases_; std::unordered_map> module_deps_; diff --git a/init/parser.h b/init/parser.h index f30bda707..95b0cd76e 100644 --- a/init/parser.h +++ b/init/parser.h @@ -27,7 +27,7 @@ // SectionParser is an interface that can parse a given 'section' in init. // // You can implement up to 4 functions below, with ParseSection being mandatory. The first two -// functions return Result indicating if they have an error. It will be reported along +// functions return Result indicating if they have an error. It will be reported along // with the filename and line number of where the error occurred. // // 1) ParseSection @@ -51,10 +51,10 @@ namespace init { class SectionParser { public: virtual ~SectionParser() {} - virtual Result ParseSection(std::vector&& args, - const std::string& filename, int line) = 0; - virtual Result ParseLineSection(std::vector&&, int) { return Success(); }; - virtual Result EndSection() { return Success(); }; + virtual Result ParseSection(std::vector&& args, const std::string& filename, + int line) = 0; + virtual Result ParseLineSection(std::vector&&, int) { return {}; }; + virtual Result EndSection() { return {}; }; virtual void EndFile(){}; }; @@ -67,7 +67,7 @@ class Parser { // Similar to ParseSection() and ParseLineSection(), this function returns bool with false // indicating a failure and has an std::string* err parameter into which an error string can // be written. - using LineCallback = std::function(std::vector&&)>; + using LineCallback = std::function(std::vector&&)>; Parser(); diff --git a/init/persistent_properties.cpp b/init/persistent_properties.cpp index 21adce914..73787b913 100644 --- a/init/persistent_properties.cpp +++ b/init/persistent_properties.cpp @@ -169,7 +169,7 @@ Result LoadPersistentPropertyFile() { return Error() << "Unable to parse persistent property file: Could not parse protobuf"; } -Result WritePersistentPropertyFile(const PersistentProperties& persistent_properties) { +Result WritePersistentPropertyFile(const PersistentProperties& persistent_properties) { const std::string temp_filename = persistent_property_filename + ".tmp"; unique_fd fd(TEMP_FAILURE_RETRY( open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600))); @@ -191,7 +191,7 @@ Result WritePersistentPropertyFile(const PersistentProperties& persiste unlink(temp_filename.c_str()); return Error(saved_errno) << "Unable to rename persistent property file"; } - return Success(); + return {}; } // Persistent properties are not written often, so we rather not keep any data in memory and read diff --git a/init/persistent_properties.h b/init/persistent_properties.h index 5f4df85bd..3845a0d86 100644 --- a/init/persistent_properties.h +++ b/init/persistent_properties.h @@ -30,7 +30,7 @@ void WritePersistentProperty(const std::string& name, const std::string& value); // Exposed only for testing Result LoadPersistentPropertyFile(); -Result WritePersistentPropertyFile(const PersistentProperties& persistent_properties); +Result WritePersistentPropertyFile(const PersistentProperties& persistent_properties); extern std::string persistent_property_filename; } // namespace init diff --git a/init/reboot.cpp b/init/reboot.cpp index 54f68bb8f..fbc03c276 100644 --- a/init/reboot.cpp +++ b/init/reboot.cpp @@ -707,7 +707,7 @@ bool HandlePowerctlMessage(const std::string& command) { // Queue built-in shutdown_done auto shutdown_handler = [cmd, command, reboot_target, run_fsck](const BuiltinArguments&) { DoReboot(cmd, command, reboot_target, run_fsck); - return Success(); + return Result{}; }; ActionManager::GetInstance().QueueBuiltinAction(shutdown_handler, "shutdown_done"); diff --git a/init/result.h b/init/result.h index 984b25792..8c1f91e03 100644 --- a/init/result.h +++ b/init/result.h @@ -14,66 +14,14 @@ * limitations under the License. */ -// This file contains classes for returning a successful result along with an optional -// arbitrarily typed return value or for returning a failure result along with an optional string -// indicating why the function failed. - -// There are 3 classes that implement this functionality and one additional helper type. -// -// Result either contains a member of type T that can be accessed using similar semantics as -// std::optional or it contains a ResultError describing an error, which can be accessed via -// Result::error(). -// -// ResultError is a type that contains both a std::string describing the error and a copy of errno -// from when the error occurred. ResultError can be used in an ostream directly to print its -// string value. -// -// Success is a typedef that aids in creating Result that do not contain a return value. -// Result is the correct return type for a function that either returns successfully or -// returns an error value. Returning Nothing() from a function that returns Result is the -// correct way to indicate that a function without a return type has completed successfully. -// -// A successful Result is constructed implicitly from any type that can be implicitly converted -// to T or from the constructor arguments for T. This allows you to return a type T directly from -// a function that returns Result. -// -// Error and ErrnoError are used to construct a Result that has failed. The Error class takes -// an ostream as an input and are implicitly cast to a Result containing that failure. -// ErrnoError() is a helper function to create an Error class that appends ": " + strerror(errno) -// to the end of the failure string to aid in interacting with C APIs. Alternatively, an errno -// value can be directly specified via the Error() constructor. -// -// ResultError can be used in the ostream when using Error to construct a Result. In this case, -// the string that the ResultError takes is passed through the stream normally, but the errno is -// passed to the Result. This can be used to pass errno from a failing C function up multiple -// callers. -// -// ResultError can also directly construct a Result. This is particularly useful if you have a -// function that return Result but you have a Result and want to return its error. In this -// case, you can return the .error() from the Result to construct the Result. - -// An example of how to use these is below: -// Result CalculateResult(const T& input) { -// U output; -// if (!SomeOtherCppFunction(input, &output)) { -// return Error() << "SomeOtherCppFunction(" << input << ") failed"; -// } -// if (!c_api_function(output)) { -// return ErrnoError() << "c_api_function(" << output << ") failed"; -// } -// return output; -// } -// -// auto output = CalculateResult(input); -// if (!output) return Error() << "CalculateResult failed: " << output.error(); -// UseOutput(*output); - #pragma once +// The implementation of this file has moved to android-base. This file remains since historically, +// these classes were a part of init. + #include using android::base::ErrnoError; using android::base::Error; using android::base::Result; using android::base::ResultError; -using android::base::Success; diff --git a/init/security.cpp b/init/security.cpp index a3494a280..5d87f3c18 100644 --- a/init/security.cpp +++ b/init/security.cpp @@ -43,14 +43,14 @@ namespace init { // 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. -Result MixHwrngIntoLinuxRngAction(const BuiltinArguments&) { +Result MixHwrngIntoLinuxRngAction(const BuiltinArguments&) { unique_fd hwrandom_fd( TEMP_FAILURE_RETRY(open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC))); if (hwrandom_fd == -1) { if (errno == ENOENT) { LOG(INFO) << "/dev/hw_random not found"; // It's not an error to not have a Hardware RNG. - return Success(); + return {}; } return ErrnoError() << "Failed to open /dev/hw_random"; } @@ -80,7 +80,7 @@ Result MixHwrngIntoLinuxRngAction(const BuiltinArguments&) { } LOG(INFO) << "Mixed " << total_bytes_written << " bytes from /dev/hw_random into /dev/urandom"; - return Success(); + return {}; } static bool SetHighestAvailableOptionValue(std::string path, int min, int max) { @@ -147,31 +147,31 @@ static bool __attribute__((unused)) SetMmapRndBitsMin(int start, int min, bool c // 9e08f57d684a x86: mm: support ARCH_MMAP_RND_BITS // ec9ee4acd97c drivers: char: random: add get_random_long() // 5ef11c35ce86 mm: ASLR: use get_random_long() -Result SetMmapRndBitsAction(const BuiltinArguments&) { +Result SetMmapRndBitsAction(const BuiltinArguments&) { // values are arch-dependent #if defined(USER_MODE_LINUX) // uml does not support mmap_rnd_bits - return Success(); + return {}; #elif defined(__aarch64__) // arm64 supports 18 - 33 bits depending on pagesize and VA_SIZE if (SetMmapRndBitsMin(33, 24, false) && SetMmapRndBitsMin(16, 16, true)) { - return Success(); + return {}; } #elif defined(__x86_64__) // x86_64 supports 28 - 32 bits if (SetMmapRndBitsMin(32, 32, false) && SetMmapRndBitsMin(16, 16, true)) { - return Success(); + return {}; } #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 (SetMmapRndBitsMin(16, 16, h64)) { - return Success(); + return {}; } #elif defined(__mips__) || defined(__mips64__) // TODO: add mips support b/27788820 - return Success(); + return {}; #else LOG(ERROR) << "Unknown architecture"; #endif @@ -187,14 +187,14 @@ Result SetMmapRndBitsAction(const BuiltinArguments&) { // Set kptr_restrict to the highest available level. // // Aborts if unable to set this to an acceptable value. -Result SetKptrRestrictAction(const BuiltinArguments&) { +Result SetKptrRestrictAction(const BuiltinArguments&) { std::string path = KPTR_RESTRICT_PATH; if (!SetHighestAvailableOptionValue(path, KPTR_RESTRICT_MINVALUE, KPTR_RESTRICT_MAXVALUE)) { LOG(FATAL) << "Unable to set adequate kptr_restrict value!"; return Error(); } - return Success(); + return {}; } } // namespace init diff --git a/init/security.h b/init/security.h index 6f6b94400..b081a0549 100644 --- a/init/security.h +++ b/init/security.h @@ -26,9 +26,9 @@ namespace android { namespace init { -Result MixHwrngIntoLinuxRngAction(const BuiltinArguments&); -Result SetMmapRndBitsAction(const BuiltinArguments&); -Result SetKptrRestrictAction(const BuiltinArguments&); +Result MixHwrngIntoLinuxRngAction(const BuiltinArguments&); +Result SetMmapRndBitsAction(const BuiltinArguments&); +Result SetKptrRestrictAction(const BuiltinArguments&); } // namespace init } // namespace android diff --git a/init/service.cpp b/init/service.cpp index a54cb6bdf..b6a7c33f7 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -315,7 +315,7 @@ void Service::DumpState() const { [] (const auto& info) { LOG(INFO) << *info; }); } -Result Service::ParseCapabilities(std::vector&& args) { +Result Service::ParseCapabilities(std::vector&& args) { capabilities_ = 0; if (!CapAmbientSupported()) { @@ -341,32 +341,32 @@ Result Service::ParseCapabilities(std::vector&& args) { } (*capabilities_)[cap] = true; } - return Success(); + return {}; } -Result Service::ParseClass(std::vector&& args) { +Result Service::ParseClass(std::vector&& args) { classnames_ = std::set(args.begin() + 1, args.end()); - return Success(); + return {}; } -Result Service::ParseConsole(std::vector&& args) { +Result Service::ParseConsole(std::vector&& args) { flags_ |= SVC_CONSOLE; proc_attr_.console = args.size() > 1 ? "/dev/" + args[1] : ""; - return Success(); + return {}; } -Result Service::ParseCritical(std::vector&& args) { +Result Service::ParseCritical(std::vector&& args) { flags_ |= SVC_CRITICAL; - return Success(); + return {}; } -Result Service::ParseDisabled(std::vector&& args) { +Result Service::ParseDisabled(std::vector&& args) { flags_ |= SVC_DISABLED; flags_ |= SVC_RC_DISABLED; - return Success(); + return {}; } -Result Service::ParseEnterNamespace(std::vector&& args) { +Result Service::ParseEnterNamespace(std::vector&& args) { if (args[1] != "net") { return Error() << "Init only supports entering network namespaces"; } @@ -377,10 +377,10 @@ Result Service::ParseEnterNamespace(std::vector&& args) { // present. Therefore, they also require mount namespaces. namespaces_.flags |= CLONE_NEWNS; namespaces_.namespaces_to_enter.emplace_back(CLONE_NEWNET, std::move(args[2])); - return Success(); + return {}; } -Result Service::ParseGroup(std::vector&& args) { +Result Service::ParseGroup(std::vector&& args) { auto gid = DecodeUid(args[1]); if (!gid) { return Error() << "Unable to decode GID for '" << args[1] << "': " << gid.error(); @@ -394,10 +394,10 @@ Result Service::ParseGroup(std::vector&& args) { } proc_attr_.supp_gids.emplace_back(*gid); } - return Success(); + return {}; } -Result Service::ParsePriority(std::vector&& args) { +Result Service::ParsePriority(std::vector&& args) { proc_attr_.priority = 0; if (!ParseInt(args[1], &proc_attr_.priority, static_cast(ANDROID_PRIORITY_HIGHEST), // highest is negative @@ -405,10 +405,10 @@ Result Service::ParsePriority(std::vector&& args) { return Error() << StringPrintf("process priority value must be range %d - %d", ANDROID_PRIORITY_HIGHEST, ANDROID_PRIORITY_LOWEST); } - return Success(); + return {}; } -Result Service::ParseInterface(std::vector&& args) { +Result Service::ParseInterface(std::vector&& args) { const std::string& interface_name = args[1]; const std::string& instance_name = args[2]; @@ -436,10 +436,10 @@ Result Service::ParseInterface(std::vector&& args) { interfaces_.insert(fullname); - return Success(); + return {}; } -Result Service::ParseIoprio(std::vector&& args) { +Result Service::ParseIoprio(std::vector&& args) { if (!ParseInt(args[2], &proc_attr_.ioprio_pri, 0, 7)) { return Error() << "priority value must be range 0 - 7"; } @@ -454,10 +454,10 @@ Result Service::ParseIoprio(std::vector&& args) { return Error() << "ioprio option usage: ioprio <0-7>"; } - return Success(); + return {}; } -Result Service::ParseKeycodes(std::vector&& args) { +Result Service::ParseKeycodes(std::vector&& args) { auto it = args.begin() + 1; if (args.size() == 2 && StartsWith(args[1], "$")) { std::string expanded; @@ -468,7 +468,7 @@ Result Service::ParseKeycodes(std::vector&& args) { // If the property is not set, it defaults to none, in which case there are no keycodes // for this service. if (expanded == "none") { - return Success(); + return {}; } args = Split(expanded, ","); @@ -486,24 +486,24 @@ Result Service::ParseKeycodes(std::vector&& args) { return Error() << "invalid keycode: " << *it; } } - return Success(); + return {}; } -Result Service::ParseOneshot(std::vector&& args) { +Result Service::ParseOneshot(std::vector&& args) { flags_ |= SVC_ONESHOT; - return Success(); + return {}; } -Result Service::ParseOnrestart(std::vector&& args) { +Result Service::ParseOnrestart(std::vector&& args) { args.erase(args.begin()); int line = onrestart_.NumCommands() + 1; if (auto result = onrestart_.AddCommand(std::move(args), line); !result) { return Error() << "cannot add Onrestart command: " << result.error(); } - return Success(); + return {}; } -Result Service::ParseNamespace(std::vector&& args) { +Result Service::ParseNamespace(std::vector&& args) { for (size_t i = 1; i < args.size(); i++) { if (args[i] == "pid") { namespaces_.flags |= CLONE_NEWPID; @@ -515,105 +515,105 @@ Result Service::ParseNamespace(std::vector&& args) { return Error() << "namespace must be 'pid' or 'mnt'"; } } - return Success(); + return {}; } -Result Service::ParseOomScoreAdjust(std::vector&& args) { +Result Service::ParseOomScoreAdjust(std::vector&& args) { if (!ParseInt(args[1], &oom_score_adjust_, -1000, 1000)) { return Error() << "oom_score_adjust value must be in range -1000 - +1000"; } - return Success(); + return {}; } -Result Service::ParseOverride(std::vector&& args) { +Result Service::ParseOverride(std::vector&& args) { override_ = true; - return Success(); + return {}; } -Result Service::ParseMemcgSwappiness(std::vector&& args) { +Result Service::ParseMemcgSwappiness(std::vector&& args) { if (!ParseInt(args[1], &swappiness_, 0)) { return Error() << "swappiness value must be equal or greater than 0"; } - return Success(); + return {}; } -Result Service::ParseMemcgLimitInBytes(std::vector&& args) { +Result Service::ParseMemcgLimitInBytes(std::vector&& args) { if (!ParseInt(args[1], &limit_in_bytes_, 0)) { return Error() << "limit_in_bytes value must be equal or greater than 0"; } - return Success(); + return {}; } -Result Service::ParseMemcgLimitPercent(std::vector&& args) { +Result Service::ParseMemcgLimitPercent(std::vector&& args) { if (!ParseInt(args[1], &limit_percent_, 0)) { return Error() << "limit_percent value must be equal or greater than 0"; } - return Success(); + return {}; } -Result Service::ParseMemcgLimitProperty(std::vector&& args) { +Result Service::ParseMemcgLimitProperty(std::vector&& args) { limit_property_ = std::move(args[1]); - return Success(); + return {}; } -Result Service::ParseMemcgSoftLimitInBytes(std::vector&& args) { +Result Service::ParseMemcgSoftLimitInBytes(std::vector&& args) { if (!ParseInt(args[1], &soft_limit_in_bytes_, 0)) { return Error() << "soft_limit_in_bytes value must be equal or greater than 0"; } - return Success(); + return {}; } -Result Service::ParseProcessRlimit(std::vector&& args) { +Result Service::ParseProcessRlimit(std::vector&& args) { auto rlimit = ParseRlimit(args); if (!rlimit) return rlimit.error(); proc_attr_.rlimits.emplace_back(*rlimit); - return Success(); + return {}; } -Result Service::ParseRestartPeriod(std::vector&& args) { +Result Service::ParseRestartPeriod(std::vector&& args) { int period; if (!ParseInt(args[1], &period, 5)) { return Error() << "restart_period value must be an integer >= 5"; } restart_period_ = std::chrono::seconds(period); - return Success(); + return {}; } -Result Service::ParseSeclabel(std::vector&& args) { +Result Service::ParseSeclabel(std::vector&& args) { seclabel_ = std::move(args[1]); - return Success(); + return {}; } -Result Service::ParseSigstop(std::vector&& args) { +Result Service::ParseSigstop(std::vector&& args) { sigstop_ = true; - return Success(); + return {}; } -Result Service::ParseSetenv(std::vector&& args) { +Result Service::ParseSetenv(std::vector&& args) { environment_vars_.emplace_back(std::move(args[1]), std::move(args[2])); - return Success(); + return {}; } -Result Service::ParseShutdown(std::vector&& args) { +Result Service::ParseShutdown(std::vector&& args) { if (args[1] == "critical") { flags_ |= SVC_SHUTDOWN_CRITICAL; - return Success(); + return {}; } return Error() << "Invalid shutdown option"; } -Result Service::ParseTimeoutPeriod(std::vector&& args) { +Result Service::ParseTimeoutPeriod(std::vector&& args) { int period; if (!ParseInt(args[1], &period, 1)) { return Error() << "timeout_period value must be an integer >= 1"; } timeout_period_ = std::chrono::seconds(period); - return Success(); + return {}; } template -Result Service::AddDescriptor(std::vector&& args) { +Result Service::AddDescriptor(std::vector&& args) { int perm = args.size() > 3 ? std::strtoul(args[3].c_str(), 0, 8) : -1; Result uid = 0; Result gid = 0; @@ -644,11 +644,11 @@ Result Service::AddDescriptor(std::vector&& args) { } descriptors_.emplace_back(std::move(descriptor)); - return Success(); + return {}; } // name type perm [ uid gid context ] -Result Service::ParseSocket(std::vector&& args) { +Result Service::ParseSocket(std::vector&& args) { if (!StartsWith(args[2], "dgram") && !StartsWith(args[2], "stream") && !StartsWith(args[2], "seqpacket")) { return Error() << "socket type must be 'dgram', 'stream' or 'seqpacket'"; @@ -657,7 +657,7 @@ Result Service::ParseSocket(std::vector&& args) { } // name type perm [ uid gid context ] -Result Service::ParseFile(std::vector&& args) { +Result Service::ParseFile(std::vector&& args) { if (args[2] != "r" && args[2] != "w" && args[2] != "rw") { return Error() << "file type must be 'r', 'w' or 'rw'"; } @@ -672,24 +672,24 @@ Result Service::ParseFile(std::vector&& args) { return AddDescriptor(std::move(args)); } -Result Service::ParseUser(std::vector&& args) { +Result Service::ParseUser(std::vector&& args) { auto uid = DecodeUid(args[1]); if (!uid) { return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error(); } proc_attr_.uid = *uid; - return Success(); + return {}; } -Result Service::ParseWritepid(std::vector&& args) { +Result Service::ParseWritepid(std::vector&& args) { args.erase(args.begin()); writepid_files_ = std::move(args); - return Success(); + return {}; } -Result Service::ParseUpdatable(std::vector&& args) { +Result Service::ParseUpdatable(std::vector&& args) { updatable_ = true; - return Success(); + return {}; } class Service::OptionParserMap : public KeywordMap { @@ -752,7 +752,7 @@ const Service::OptionParserMap::Map& Service::OptionParserMap::map() const { return option_parsers; } -Result Service::ParseLine(std::vector&& args) { +Result Service::ParseLine(std::vector&& args) { static const OptionParserMap parser_map; auto parser = parser_map.FindFunction(args); @@ -761,7 +761,7 @@ Result Service::ParseLine(std::vector&& args) { return std::invoke(*parser, this, std::move(args)); } -Result Service::ExecStart() { +Result Service::ExecStart() { if (is_updatable() && !ServiceList::GetInstance().IsServicesUpdated()) { // Don't delay the service for ExecStart() as the semantic is that // the caller might depend on the side effect of the execution. @@ -782,10 +782,10 @@ Result Service::ExecStart() { << " gid " << proc_attr_.gid << "+" << proc_attr_.supp_gids.size() << " context " << (!seclabel_.empty() ? seclabel_ : "default") << ") started; waiting..."; - return Success(); + return {}; } -Result Service::Start() { +Result Service::Start() { if (is_updatable() && !ServiceList::GetInstance().IsServicesUpdated()) { ServiceList::GetInstance().DelayService(*this); return Error() << "Cannot start an updatable service '" << name_ @@ -808,7 +808,7 @@ Result Service::Start() { flags_ |= SVC_RESTART; } // It is not an error to try to start a service that is already running. - return Success(); + return {}; } bool needs_console = (flags_ & SVC_CONSOLE); @@ -960,24 +960,24 @@ Result Service::Start() { } NotifyStateChange("running"); - return Success(); + return {}; } -Result Service::StartIfNotDisabled() { +Result Service::StartIfNotDisabled() { if (!(flags_ & SVC_DISABLED)) { return Start(); } else { flags_ |= SVC_DISABLED_START; } - return Success(); + return {}; } -Result Service::Enable() { +Result Service::Enable() { flags_ &= ~(SVC_DISABLED | SVC_RC_DISABLED); if (flags_ & SVC_DISABLED_START) { return Start(); } - return Success(); + return {}; } void Service::Reset() { @@ -993,14 +993,14 @@ void Service::ResetIfPostData() { } } -Result Service::StartIfPostData() { +Result Service::StartIfPostData() { // Start the service, but only if it was started after /data was mounted, // and it was still running when we reset the post-data services. if (running_at_post_data_reset_) { return Start(); } - return Success(); + return {}; } void Service::Stop() { @@ -1211,8 +1211,8 @@ void ServiceList::DelayService(const Service& service) { delayed_service_names_.emplace_back(service.name()); } -Result ServiceParser::ParseSection(std::vector&& args, - const std::string& filename, int line) { +Result ServiceParser::ParseSection(std::vector&& args, + const std::string& filename, int line) { if (args.size() < 3) { return Error() << "services must have a name and a program"; } @@ -1243,14 +1243,14 @@ Result ServiceParser::ParseSection(std::vector&& args, } service_ = std::make_unique(name, restart_action_subcontext, str_args); - return Success(); + return {}; } -Result ServiceParser::ParseLineSection(std::vector&& args, int line) { - return service_ ? service_->ParseLine(std::move(args)) : Success(); +Result ServiceParser::ParseLineSection(std::vector&& args, int line) { + return service_ ? service_->ParseLine(std::move(args)) : Result{}; } -Result ServiceParser::EndSection() { +Result ServiceParser::EndSection() { if (service_) { Service* old_service = service_list_->FindService(service_->name()); if (old_service) { @@ -1271,7 +1271,7 @@ Result ServiceParser::EndSection() { service_list_->AddService(std::move(service_)); } - return Success(); + return {}; } bool ServiceParser::IsValidName(const std::string& name) const { diff --git a/init/service.h b/init/service.h index 93b5a5c3e..b4356c839 100644 --- a/init/service.h +++ b/init/service.h @@ -76,12 +76,12 @@ class Service { static std::unique_ptr MakeTemporaryOneshotService(const std::vector& args); bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } - Result ParseLine(std::vector&& args); - Result ExecStart(); - Result Start(); - Result StartIfNotDisabled(); - Result StartIfPostData(); - Result Enable(); + Result ParseLine(std::vector&& args); + Result ExecStart(); + Result Start(); + Result StartIfNotDisabled(); + Result StartIfPostData(); + Result Enable(); void Reset(); void ResetIfPostData(); void Stop(); @@ -130,7 +130,7 @@ class Service { bool is_post_data() const { return post_data_; } private: - using OptionParser = Result (Service::*)(std::vector&& args); + using OptionParser = Result (Service::*)(std::vector&& args); class OptionParserMap; void NotifyStateChange(const std::string& new_state) const; @@ -138,42 +138,42 @@ class Service { void KillProcessGroup(int signal); void SetProcessAttributesAndCaps(); - Result ParseCapabilities(std::vector&& args); - Result ParseClass(std::vector&& args); - Result ParseConsole(std::vector&& args); - Result ParseCritical(std::vector&& args); - Result ParseDisabled(std::vector&& args); - Result ParseEnterNamespace(std::vector&& args); - Result ParseGroup(std::vector&& args); - Result ParsePriority(std::vector&& args); - Result ParseInterface(std::vector&& args); - Result ParseIoprio(std::vector&& args); - Result ParseKeycodes(std::vector&& args); - Result ParseOneshot(std::vector&& args); - Result ParseOnrestart(std::vector&& args); - Result ParseOomScoreAdjust(std::vector&& args); - Result ParseOverride(std::vector&& args); - Result ParseMemcgLimitInBytes(std::vector&& args); - Result ParseMemcgLimitPercent(std::vector&& args); - Result ParseMemcgLimitProperty(std::vector&& args); - Result ParseMemcgSoftLimitInBytes(std::vector&& args); - Result ParseMemcgSwappiness(std::vector&& args); - Result ParseNamespace(std::vector&& args); - Result ParseProcessRlimit(std::vector&& args); - Result ParseRestartPeriod(std::vector&& args); - Result ParseSeclabel(std::vector&& args); - Result ParseSetenv(std::vector&& args); - Result ParseShutdown(std::vector&& args); - Result ParseSigstop(std::vector&& args); - Result ParseSocket(std::vector&& args); - Result ParseTimeoutPeriod(std::vector&& args); - Result ParseFile(std::vector&& args); - Result ParseUser(std::vector&& args); - Result ParseWritepid(std::vector&& args); - Result ParseUpdatable(std::vector&& args); + Result ParseCapabilities(std::vector&& args); + Result ParseClass(std::vector&& args); + Result ParseConsole(std::vector&& args); + Result ParseCritical(std::vector&& args); + Result ParseDisabled(std::vector&& args); + Result ParseEnterNamespace(std::vector&& args); + Result ParseGroup(std::vector&& args); + Result ParsePriority(std::vector&& args); + Result ParseInterface(std::vector&& args); + Result ParseIoprio(std::vector&& args); + Result ParseKeycodes(std::vector&& args); + Result ParseOneshot(std::vector&& args); + Result ParseOnrestart(std::vector&& args); + Result ParseOomScoreAdjust(std::vector&& args); + Result ParseOverride(std::vector&& args); + Result ParseMemcgLimitInBytes(std::vector&& args); + Result ParseMemcgLimitPercent(std::vector&& args); + Result ParseMemcgLimitProperty(std::vector&& args); + Result ParseMemcgSoftLimitInBytes(std::vector&& args); + Result ParseMemcgSwappiness(std::vector&& args); + Result ParseNamespace(std::vector&& args); + Result ParseProcessRlimit(std::vector&& args); + Result ParseRestartPeriod(std::vector&& args); + Result ParseSeclabel(std::vector&& args); + Result ParseSetenv(std::vector&& args); + Result ParseShutdown(std::vector&& args); + Result ParseSigstop(std::vector&& args); + Result ParseSocket(std::vector&& args); + Result ParseTimeoutPeriod(std::vector&& args); + Result ParseFile(std::vector&& args); + Result ParseUser(std::vector&& args); + Result ParseWritepid(std::vector&& args); + Result ParseUpdatable(std::vector&& args); template - Result AddDescriptor(std::vector&& args); + Result AddDescriptor(std::vector&& args); static unsigned long next_start_order_; static bool is_exec_service_running_; @@ -295,10 +295,10 @@ class ServiceParser : public SectionParser { public: ServiceParser(ServiceList* service_list, std::vector* subcontexts) : service_list_(service_list), subcontexts_(subcontexts), service_(nullptr) {} - Result ParseSection(std::vector&& args, const std::string& filename, - int line) override; - Result ParseLineSection(std::vector&& args, int line) override; - Result EndSection() override; + Result ParseSection(std::vector&& args, const std::string& filename, + int line) override; + Result ParseLineSection(std::vector&& args, int line) override; + Result EndSection() override; void EndFile() override { filename_ = ""; } private: diff --git a/init/service_utils.cpp b/init/service_utils.cpp index 17fc9c8b0..f88ea9758 100644 --- a/init/service_utils.cpp +++ b/init/service_utils.cpp @@ -42,7 +42,7 @@ namespace init { namespace { -Result EnterNamespace(int nstype, const char* path) { +Result EnterNamespace(int nstype, const char* path) { auto fd = unique_fd{open(path, O_RDONLY | O_CLOEXEC)}; if (fd == -1) { return ErrnoError() << "Could not open namespace at " << path; @@ -50,10 +50,10 @@ Result EnterNamespace(int nstype, const char* path) { if (setns(fd, nstype) == -1) { return ErrnoError() << "Could not setns() namespace at " << path; } - return Success(); + return {}; } -Result SetUpMountNamespace(bool remount_proc, bool remount_sys) { +Result SetUpMountNamespace(bool remount_proc, bool remount_sys) { constexpr unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; // Recursively remount / as slave like zygote does so unmounting and mounting /proc @@ -83,10 +83,10 @@ Result SetUpMountNamespace(bool remount_proc, bool remount_sys) { return ErrnoError() << "Could not mount(/sys)"; } } - return Success(); + return {}; } -Result SetUpPidNamespace(const char* name) { +Result SetUpPidNamespace(const char* name) { if (prctl(PR_SET_NAME, name) == -1) { return ErrnoError() << "Could not set name"; } @@ -116,7 +116,7 @@ Result SetUpPidNamespace(const char* name) { } _exit(WEXITSTATUS(init_exitstatus)); } - return Success(); + return {}; } void ZapStdio() { @@ -140,8 +140,7 @@ void OpenConsole(const std::string& console) { } // namespace -Result EnterNamespaces(const NamespaceInfo& info, const std::string& name, - bool pre_apexd) { +Result EnterNamespaces(const NamespaceInfo& info, const std::string& name, bool pre_apexd) { for (const auto& [nstype, path] : info.namespaces_to_enter) { if (auto result = EnterNamespace(nstype, path.c_str()); !result) { return result; @@ -173,10 +172,10 @@ Result EnterNamespaces(const NamespaceInfo& info, const std::string& na } } - return Success(); + return {}; } -Result SetProcessAttributes(const ProcessAttributes& attr) { +Result SetProcessAttributes(const ProcessAttributes& attr) { if (attr.ioprio_class != IoSchedClass_NONE) { if (android_set_ioprio(getpid(), attr.ioprio_class, attr.ioprio_pri)) { PLOG(ERROR) << "failed to set pid " << getpid() << " ioprio=" << attr.ioprio_class @@ -221,10 +220,10 @@ Result SetProcessAttributes(const ProcessAttributes& attr) { return ErrnoError() << "setpriority failed"; } } - return Success(); + return {}; } -Result WritePidToFiles(std::vector* files) { +Result WritePidToFiles(std::vector* files) { // See if there were "writepid" instructions to write to files under cpuset path. std::string cpuset_path; if (CgroupGetControllerPath("cpuset", &cpuset_path)) { @@ -258,7 +257,7 @@ Result WritePidToFiles(std::vector* files) { return ErrnoError() << "couldn't write " << pid_str << " to " << file; } } - return Success(); + return {}; } } // namespace init diff --git a/init/service_utils.h b/init/service_utils.h index f7502a9b6..c26b12343 100644 --- a/init/service_utils.h +++ b/init/service_utils.h @@ -34,7 +34,7 @@ struct NamespaceInfo { // Pair of namespace type, path to name. std::vector> namespaces_to_enter; }; -Result EnterNamespaces(const NamespaceInfo& info, const std::string& name, bool pre_apexd); +Result EnterNamespaces(const NamespaceInfo& info, const std::string& name, bool pre_apexd); struct ProcessAttributes { std::string console; @@ -46,9 +46,9 @@ struct ProcessAttributes { std::vector supp_gids; int priority; }; -Result SetProcessAttributes(const ProcessAttributes& attr); +Result SetProcessAttributes(const ProcessAttributes& attr); -Result WritePidToFiles(std::vector* files); +Result WritePidToFiles(std::vector* files); } // namespace init } // namespace android diff --git a/init/subcontext.cpp b/init/subcontext.cpp index 0ff479a22..02ed507a7 100644 --- a/init/subcontext.cpp +++ b/init/subcontext.cpp @@ -72,7 +72,7 @@ Result ReadMessage(int socket) { } template -Result SendMessage(int socket, const T& message) { +Result SendMessage(int socket, const T& message) { std::string message_string; if (!message.SerializeToString(&message_string)) { return Error() << "Unable to serialize message"; @@ -87,7 +87,7 @@ Result SendMessage(int socket, const T& message) { result != static_cast(message_string.size())) { return ErrnoError() << "send() failed to send message contents"; } - return Success(); + return {}; } std::vector> properties_to_set; @@ -123,7 +123,7 @@ void SubcontextProcess::RunCommand(const SubcontextCommand::ExecuteCommand& exec } auto map_result = function_map_->FindFunction(args); - Result result; + Result result; if (!map_result) { result = Error() << "Cannot find command: " << map_result.error(); } else { @@ -299,7 +299,7 @@ Result Subcontext::TransmitMessage(const SubcontextCommand& sub return subcontext_reply; } -Result Subcontext::Execute(const std::vector& args) { +Result Subcontext::Execute(const std::vector& args) { auto subcontext_command = SubcontextCommand(); std::copy( args.begin(), args.end(), @@ -329,7 +329,7 @@ Result Subcontext::Execute(const std::vector& args) { << subcontext_reply->reply_case(); } - return Success(); + return {}; } Result> Subcontext::ExpandArgs(const std::vector& args) { diff --git a/init/subcontext.h b/init/subcontext.h index 628fd50e7..16bd87028 100644 --- a/init/subcontext.h +++ b/init/subcontext.h @@ -42,7 +42,7 @@ class Subcontext { Fork(); } - Result Execute(const std::vector& args); + Result Execute(const std::vector& args); Result> ExpandArgs(const std::vector& args); void Restart(); diff --git a/init/subcontext_benchmark.cpp b/init/subcontext_benchmark.cpp index 630799378..fdbbc4167 100644 --- a/init/subcontext_benchmark.cpp +++ b/init/subcontext_benchmark.cpp @@ -53,7 +53,7 @@ BENCHMARK(BenchmarkSuccess); TestFunctionMap BuildTestFunctionMap() { TestFunctionMap test_function_map; test_function_map.Add("return_success", 0, 0, true, - [](const BuiltinArguments& args) { return Success(); }); + [](const BuiltinArguments& args) { return Result{}; }); return test_function_map; } diff --git a/init/subcontext_test.cpp b/init/subcontext_test.cpp index 263568300..55912d63b 100644 --- a/init/subcontext_test.cpp +++ b/init/subcontext_test.cpp @@ -175,14 +175,14 @@ TestFunctionMap BuildTestFunctionMap() { TestFunctionMap test_function_map; // For CheckDifferentPid test_function_map.Add("return_pids_as_error", 0, 0, true, - [](const BuiltinArguments& args) -> Result { + [](const BuiltinArguments& args) -> Result { return Error() << getpid() << " " << getppid(); }); // For SetProp test_function_map.Add("setprop", 2, 2, true, [](const BuiltinArguments& args) { android::base::SetProperty(args[1], args[2]); - return Success(); + return Result{}; }); // For MultipleCommands @@ -190,26 +190,26 @@ TestFunctionMap BuildTestFunctionMap() { auto words = std::make_shared>(); test_function_map.Add("add_word", 1, 1, true, [words](const BuiltinArguments& args) { words->emplace_back(args[1]); - return Success(); + return Result{}; }); test_function_map.Add("return_words_as_error", 0, 0, true, - [words](const BuiltinArguments& args) -> Result { + [words](const BuiltinArguments& args) -> Result { return Error() << Join(*words, " "); }); // For RecoverAfterAbort test_function_map.Add("cause_log_fatal", 0, 0, true, - [](const BuiltinArguments& args) -> Result { + [](const BuiltinArguments& args) -> Result { return Error() << std::string(4097, 'f'); }); test_function_map.Add( - "generate_sane_error", 0, 0, true, - [](const BuiltinArguments& args) -> Result { return Error() << "Sane error!"; }); + "generate_sane_error", 0, 0, true, + [](const BuiltinArguments& args) -> Result { return Error() << "Sane error!"; }); // For ContextString test_function_map.Add( - "return_context_as_error", 0, 0, true, - [](const BuiltinArguments& args) -> Result { return Error() << args.context; }); + "return_context_as_error", 0, 0, true, + [](const BuiltinArguments& args) -> Result { return Error() << args.context; }); return test_function_map; } diff --git a/init/test_function_map.h b/init/test_function_map.h index 583df1af8..293f1f9b4 100644 --- a/init/test_function_map.h +++ b/init/test_function_map.h @@ -14,13 +14,13 @@ * limitations under the License. */ -#ifndef _INIT_TEST_FUNCTION_MAP_H -#define _INIT_TEST_FUNCTION_MAP_H +#pragma once #include #include #include "builtin_arguments.h" +#include "builtins.h" #include "keyword_map.h" namespace android { @@ -33,7 +33,7 @@ class TestFunctionMap : public KeywordFunctionMap { void Add(const std::string& name, const BuiltinFunctionNoArgs function) { Add(name, 0, 0, false, [function](const BuiltinArguments&) { function(); - return Success(); + return Result{}; }); } @@ -51,5 +51,3 @@ class TestFunctionMap : public KeywordFunctionMap { } // namespace init } // namespace android - -#endif diff --git a/init/ueventd_parser.cpp b/init/ueventd_parser.cpp index aac3fe5c1..25bab9338 100644 --- a/init/ueventd_parser.cpp +++ b/init/ueventd_parser.cpp @@ -29,9 +29,9 @@ using android::base::ParseByteCount; namespace android { namespace init { -Result ParsePermissionsLine(std::vector&& args, - std::vector* out_sysfs_permissions, - std::vector* out_dev_permissions) { +Result ParsePermissionsLine(std::vector&& args, + std::vector* out_sysfs_permissions, + std::vector* out_dev_permissions) { bool is_sysfs = out_sysfs_permissions != nullptr; if (is_sysfs && args.size() != 5) { return Error() << "/sys/ lines must have 5 entries"; @@ -74,22 +74,22 @@ Result ParsePermissionsLine(std::vector&& args, } else { out_dev_permissions->emplace_back(name, perm, uid, gid); } - return Success(); + return {}; } -Result ParseFirmwareDirectoriesLine(std::vector&& args, - std::vector* firmware_directories) { +Result ParseFirmwareDirectoriesLine(std::vector&& args, + std::vector* firmware_directories) { if (args.size() < 2) { return Error() << "firmware_directories must have at least 1 entry"; } std::move(std::next(args.begin()), args.end(), std::back_inserter(*firmware_directories)); - return Success(); + return {}; } -Result ParseModaliasHandlingLine(std::vector&& args, - bool* enable_modalias_handling) { +Result ParseModaliasHandlingLine(std::vector&& args, + bool* enable_modalias_handling) { if (args.size() != 2) { return Error() << "modalias_handling lines take exactly one parameter"; } @@ -102,11 +102,11 @@ Result ParseModaliasHandlingLine(std::vector&& args, return Error() << "modalias_handling takes either 'enabled' or 'disabled' as a parameter"; } - return Success(); + return {}; } -Result ParseUeventSocketRcvbufSizeLine(std::vector&& args, - size_t* uevent_socket_rcvbuf_size) { +Result ParseUeventSocketRcvbufSizeLine(std::vector&& args, + size_t* uevent_socket_rcvbuf_size) { if (args.size() != 2) { return Error() << "uevent_socket_rcvbuf_size lines take exactly one parameter"; } @@ -118,27 +118,27 @@ Result ParseUeventSocketRcvbufSizeLine(std::vector&& args, *uevent_socket_rcvbuf_size = parsed_size; - return Success(); + return {}; } class SubsystemParser : public SectionParser { public: SubsystemParser(std::vector* subsystems) : subsystems_(subsystems) {} - Result ParseSection(std::vector&& args, const std::string& filename, - int line) override; - Result ParseLineSection(std::vector&& args, int line) override; - Result EndSection() override; + Result ParseSection(std::vector&& args, const std::string& filename, + int line) override; + Result ParseLineSection(std::vector&& args, int line) override; + Result EndSection() override; private: - Result ParseDevName(std::vector&& args); - Result ParseDirName(std::vector&& args); + Result ParseDevName(std::vector&& args); + Result ParseDirName(std::vector&& args); Subsystem subsystem_; std::vector* subsystems_; }; -Result SubsystemParser::ParseSection(std::vector&& args, - const std::string& filename, int line) { +Result SubsystemParser::ParseSection(std::vector&& args, + const std::string& filename, int line) { if (args.size() != 2) { return Error() << "subsystems must have exactly one name"; } @@ -149,33 +149,33 @@ Result SubsystemParser::ParseSection(std::vector&& args, subsystem_ = Subsystem(std::move(args[1])); - return Success(); + return {}; } -Result SubsystemParser::ParseDevName(std::vector&& args) { +Result SubsystemParser::ParseDevName(std::vector&& args) { if (args[1] == "uevent_devname") { subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVNAME; - return Success(); + return {}; } if (args[1] == "uevent_devpath") { subsystem_.devname_source_ = Subsystem::DEVNAME_UEVENT_DEVPATH; - return Success(); + return {}; } return Error() << "invalid devname '" << args[1] << "'"; } -Result SubsystemParser::ParseDirName(std::vector&& args) { +Result SubsystemParser::ParseDirName(std::vector&& args) { if (args[1].front() != '/') { return Error() << "dirname '" << args[1] << " ' does not start with '/'"; } subsystem_.dir_name_ = args[1]; - return Success(); + return {}; } -Result SubsystemParser::ParseLineSection(std::vector&& args, int line) { - using OptionParser = Result (SubsystemParser::*)(std::vector && args); +Result SubsystemParser::ParseLineSection(std::vector&& args, int line) { + using OptionParser = Result (SubsystemParser::*)(std::vector && args); static class OptionParserMap : public KeywordMap { private: @@ -197,10 +197,10 @@ Result SubsystemParser::ParseLineSection(std::vector&& arg return std::invoke(*parser, this, std::move(args)); } -Result SubsystemParser::EndSection() { +Result SubsystemParser::EndSection() { subsystems_->emplace_back(std::move(subsystem_)); - return Success(); + return {}; } UeventdConfiguration ParseConfig(const std::vector& configs) { diff --git a/init/util.cpp b/init/util.cpp index 243e5f012..14acaa270 100644 --- a/init/util.cpp +++ b/init/util.cpp @@ -197,7 +197,7 @@ static int OpenFile(const std::string& path, int flags, mode_t mode) { return rc; } -Result WriteFile(const std::string& path, const std::string& content) { +Result WriteFile(const std::string& path, const std::string& content) { android::base::unique_fd fd(TEMP_FAILURE_RETRY( OpenFile(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600))); if (fd == -1) { @@ -206,7 +206,7 @@ Result WriteFile(const std::string& path, const std::string& content) { if (!android::base::WriteStringToFd(content, fd)) { return ErrnoError() << "Unable to write file contents"; } - return Success(); + return {}; } bool mkdir_recursive(const std::string& path, mode_t mode) { diff --git a/init/util.h b/init/util.h index 767620b16..770084b87 100644 --- a/init/util.h +++ b/init/util.h @@ -42,7 +42,7 @@ int CreateSocket(const char* name, int type, bool passcred, mode_t perm, uid_t u const char* socketcon); Result ReadFile(const std::string& path); -Result WriteFile(const std::string& path, const std::string& content); +Result WriteFile(const std::string& path, const std::string& content); Result DecodeUid(const std::string& name);