diff --git a/init/README.md b/init/README.md index f938ccc3b..6c51b3735 100644 --- a/init/README.md +++ b/init/README.md @@ -226,6 +226,15 @@ runs the service. keycodes are pressed at once, the service will start. This is typically used to start the bugreport service. +> This option may take a property instead of a list of keycodes. In this case, only one option is + provided: the property name in the typical property expansion format. The property must contain + a comma separated list of keycode values or the text 'none' to indicate that + this service does not respond to keycodes. + +> For example, `keycodes ${some.property.name:-none}` where some.property.name expands + to "123,124,125". Since keycodes are handled very early in init, + only PRODUCT_DEFAULT_PROPERTY_OVERRIDES properties can be used. + `memcg.limit_in_bytes ` > Sets the child's memory.limit_in_bytes to the specified value (only if memcg is mounted), which must be equal or greater than 0. diff --git a/init/action.cpp b/init/action.cpp index 11335ca14..94ccef2aa 100644 --- a/init/action.cpp +++ b/init/action.cpp @@ -44,9 +44,12 @@ Result RunBuiltinFunction(const BuiltinFunction& function, return function(builtin_arguments); } -Command::Command(BuiltinFunction f, bool execute_in_subcontext, - const std::vector& args, int line) - : func_(std::move(f)), execute_in_subcontext_(execute_in_subcontext), args_(args), line_(line) {} +Command::Command(BuiltinFunction f, bool execute_in_subcontext, std::vector&& args, + int line) + : func_(std::move(f)), + execute_in_subcontext_(execute_in_subcontext), + args_(std::move(args)), + line_(line) {} Result Command::InvokeFunc(Subcontext* subcontext) const { if (subcontext) { @@ -80,7 +83,7 @@ Action::Action(bool oneshot, Subcontext* subcontext, const std::string& filename const KeywordFunctionMap* Action::function_map_ = nullptr; -Result Action::AddCommand(const std::vector& args, int line) { +Result Action::AddCommand(std::vector&& args, int line) { if (!function_map_) { return Error() << "no function map available"; } @@ -88,12 +91,12 @@ Result Action::AddCommand(const std::vector& args, int lin auto function = function_map_->FindFunction(args); if (!function) return Error() << function.error(); - commands_.emplace_back(function->second, function->first, args, line); + commands_.emplace_back(function->second, function->first, std::move(args), line); return Success(); } -void Action::AddCommand(BuiltinFunction f, const std::vector& args, int line) { - commands_.emplace_back(f, false, args, line); +void Action::AddCommand(BuiltinFunction f, std::vector&& args, int line) { + commands_.emplace_back(f, false, std::move(args), line); } std::size_t Action::NumCommands() const { diff --git a/init/action.h b/init/action.h index 4f063cc54..967c68230 100644 --- a/init/action.h +++ b/init/action.h @@ -36,7 +36,7 @@ Result RunBuiltinFunction(const BuiltinFunction& function, class Command { public: - Command(BuiltinFunction f, bool execute_in_subcontext, const std::vector& args, + Command(BuiltinFunction f, bool execute_in_subcontext, std::vector&& args, int line); Result InvokeFunc(Subcontext* subcontext) const; @@ -61,8 +61,8 @@ class Action { const std::string& event_trigger, const std::map& property_triggers); - Result AddCommand(const std::vector& args, int line); - void AddCommand(BuiltinFunction f, const 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; void ExecuteAllCommands() const; diff --git a/init/action_manager.cpp b/init/action_manager.cpp index 22977bb29..9de40859f 100644 --- a/init/action_manager.cpp +++ b/init/action_manager.cpp @@ -47,9 +47,7 @@ void ActionManager::QueueAllPropertyActions() { void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) { auto action = std::make_unique(true, nullptr, "", 0, name, std::map{}); - std::vector name_vector{name}; - - action->AddCommand(func, name_vector, 0); + action->AddCommand(func, {name}, 0); event_queue_.emplace(action.get()); actions_.emplace_back(std::move(action)); diff --git a/init/service.cpp b/init/service.cpp index 0b1425d77..7f4942373 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -60,6 +60,7 @@ using android::base::boot_clock; using android::base::GetProperty; using android::base::Join; using android::base::ParseInt; +using android::base::Split; using android::base::StartsWith; using android::base::StringPrintf; using android::base::unique_fd; @@ -400,7 +401,7 @@ void Service::DumpState() const { [] (const auto& info) { LOG(INFO) << *info; }); } -Result Service::ParseCapabilities(const std::vector& args) { +Result Service::ParseCapabilities(std::vector&& args) { capabilities_ = 0; if (!CapAmbientSupported()) { @@ -429,29 +430,29 @@ Result Service::ParseCapabilities(const std::vector& args) return Success(); } -Result Service::ParseClass(const std::vector& args) { +Result Service::ParseClass(std::vector&& args) { classnames_ = std::set(args.begin() + 1, args.end()); return Success(); } -Result Service::ParseConsole(const std::vector& args) { +Result Service::ParseConsole(std::vector&& args) { flags_ |= SVC_CONSOLE; console_ = args.size() > 1 ? "/dev/" + args[1] : ""; return Success(); } -Result Service::ParseCritical(const std::vector& args) { +Result Service::ParseCritical(std::vector&& args) { flags_ |= SVC_CRITICAL; return Success(); } -Result Service::ParseDisabled(const std::vector& args) { +Result Service::ParseDisabled(std::vector&& args) { flags_ |= SVC_DISABLED; flags_ |= SVC_RC_DISABLED; return Success(); } -Result Service::ParseEnterNamespace(const std::vector& args) { +Result Service::ParseEnterNamespace(std::vector&& args) { if (args[1] != "net") { return Error() << "Init only supports entering network namespaces"; } @@ -461,11 +462,11 @@ Result Service::ParseEnterNamespace(const std::vector& arg // Network namespaces require that /sys is remounted, otherwise the old adapters will still be // present. Therefore, they also require mount namespaces. namespace_flags_ |= CLONE_NEWNS; - namespaces_to_enter_.emplace_back(CLONE_NEWNET, args[2]); + namespaces_to_enter_.emplace_back(CLONE_NEWNET, std::move(args[2])); return Success(); } -Result Service::ParseGroup(const 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(); @@ -482,7 +483,7 @@ Result Service::ParseGroup(const std::vector& args) { return Success(); } -Result Service::ParsePriority(const std::vector& args) { +Result Service::ParsePriority(std::vector&& args) { priority_ = 0; if (!ParseInt(args[1], &priority_, static_cast(ANDROID_PRIORITY_HIGHEST), // highest is negative @@ -493,7 +494,7 @@ Result Service::ParsePriority(const std::vector& args) { return Success(); } -Result Service::ParseInterface(const std::vector& args) { +Result Service::ParseInterface(std::vector&& args) { const std::string& interface_name = args[1]; const std::string& instance_name = args[2]; @@ -524,7 +525,7 @@ Result Service::ParseInterface(const std::vector& args) { return Success(); } -Result Service::ParseIoprio(const std::vector& args) { +Result Service::ParseIoprio(std::vector&& args) { if (!ParseInt(args[2], &ioprio_pri_, 0, 7)) { return Error() << "priority value must be range 0 - 7"; } @@ -542,36 +543,53 @@ Result Service::ParseIoprio(const std::vector& args) { return Success(); } -Result Service::ParseKeycodes(const std::vector& args) { - for (std::size_t i = 1; i < args.size(); i++) { +Result Service::ParseKeycodes(std::vector&& args) { + auto it = args.begin() + 1; + if (args.size() == 2 && StartsWith(args[1], "$")) { + std::string expanded; + if (!expand_props(args[1], &expanded)) { + return Error() << "Could not expand property '" << args[1] << "'"; + } + + // 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(); + } + + args = Split(expanded, ","); + it = args.begin(); + } + + for (; it != args.end(); ++it) { int code; - if (ParseInt(args[i], &code, 0, KEY_MAX)) { + if (ParseInt(*it, &code, 0, KEY_MAX)) { for (auto& key : keycodes_) { - if (key == code) return Error() << "duplicate keycode: " << args[i]; + if (key == code) return Error() << "duplicate keycode: " << *it; } keycodes_.insert(std::upper_bound(keycodes_.begin(), keycodes_.end(), code), code); } else { - return Error() << "invalid keycode: " << args[i]; + return Error() << "invalid keycode: " << *it; } } return Success(); } -Result Service::ParseOneshot(const std::vector& args) { +Result Service::ParseOneshot(std::vector&& args) { flags_ |= SVC_ONESHOT; return Success(); } -Result Service::ParseOnrestart(const std::vector& args) { - std::vector str_args(args.begin() + 1, args.end()); +Result Service::ParseOnrestart(std::vector&& args) { + args.erase(args.begin()); int line = onrestart_.NumCommands() + 1; - if (auto result = onrestart_.AddCommand(str_args, line); !result) { + if (auto result = onrestart_.AddCommand(std::move(args), line); !result) { return Error() << "cannot add Onrestart command: " << result.error(); } return Success(); } -Result Service::ParseNamespace(const std::vector& args) { +Result Service::ParseNamespace(std::vector&& args) { for (size_t i = 1; i < args.size(); i++) { if (args[i] == "pid") { namespace_flags_ |= CLONE_NEWPID; @@ -586,40 +604,40 @@ Result Service::ParseNamespace(const std::vector& args) { return Success(); } -Result Service::ParseOomScoreAdjust(const 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(); } -Result Service::ParseOverride(const std::vector& args) { +Result Service::ParseOverride(std::vector&& args) { override_ = true; return Success(); } -Result Service::ParseMemcgSwappiness(const 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(); } -Result Service::ParseMemcgLimitInBytes(const 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(); } -Result Service::ParseMemcgSoftLimitInBytes(const 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(); } -Result Service::ParseProcessRlimit(const std::vector& args) { +Result Service::ParseProcessRlimit(std::vector&& args) { auto rlimit = ParseRlimit(args); if (!rlimit) return rlimit.error(); @@ -627,7 +645,7 @@ Result Service::ParseProcessRlimit(const std::vector& args return Success(); } -Result Service::ParseRestartPeriod(const 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"; @@ -636,22 +654,22 @@ Result Service::ParseRestartPeriod(const std::vector& args return Success(); } -Result Service::ParseSeclabel(const std::vector& args) { - seclabel_ = args[1]; +Result Service::ParseSeclabel(std::vector&& args) { + seclabel_ = std::move(args[1]); return Success(); } -Result Service::ParseSigstop(const std::vector& args) { +Result Service::ParseSigstop(std::vector&& args) { sigstop_ = true; return Success(); } -Result Service::ParseSetenv(const std::vector& args) { - environment_vars_.emplace_back(args[1], args[2]); +Result Service::ParseSetenv(std::vector&& args) { + environment_vars_.emplace_back(std::move(args[1]), std::move(args[2])); return Success(); } -Result Service::ParseShutdown(const std::vector& args) { +Result Service::ParseShutdown(std::vector&& args) { if (args[1] == "critical") { flags_ |= SVC_SHUTDOWN_CRITICAL; return Success(); @@ -659,7 +677,7 @@ Result Service::ParseShutdown(const std::vector& args) { return Error() << "Invalid shutdown option"; } -Result Service::ParseTimeoutPeriod(const 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"; @@ -669,7 +687,7 @@ Result Service::ParseTimeoutPeriod(const std::vector& args } template -Result Service::AddDescriptor(const 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; @@ -704,26 +722,26 @@ Result Service::AddDescriptor(const std::vector& args) { } // name type perm [ uid gid context ] -Result Service::ParseSocket(const 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'"; } - return AddDescriptor(args); + return AddDescriptor(std::move(args)); } // name type perm [ uid gid context ] -Result Service::ParseFile(const 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'"; } if ((args[1][0] != '/') || (args[1].find("../") != std::string::npos)) { return Error() << "file name must not be relative"; } - return AddDescriptor(args); + return AddDescriptor(std::move(args)); } -Result Service::ParseUser(const 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(); @@ -732,8 +750,9 @@ Result Service::ParseUser(const std::vector& args) { return Success(); } -Result Service::ParseWritepid(const std::vector& args) { - writepid_files_.assign(args.begin() + 1, args.end()); +Result Service::ParseWritepid(std::vector&& args) { + args.erase(args.begin()); + writepid_files_ = std::move(args); return Success(); } @@ -792,13 +811,13 @@ const Service::OptionParserMap::Map& Service::OptionParserMap::map() const { return option_parsers; } -Result Service::ParseLine(const std::vector& args) { +Result Service::ParseLine(std::vector&& args) { static const OptionParserMap parser_map; auto parser = parser_map.FindFunction(args); if (!parser) return parser.error(); - return std::invoke(*parser, this, args); + return std::invoke(*parser, this, std::move(args)); } Result Service::ExecStart() { diff --git a/init/service.h b/init/service.h index ee53adfed..c7beee926 100644 --- a/init/service.h +++ b/init/service.h @@ -75,7 +75,7 @@ class Service { static std::unique_ptr MakeTemporaryOneshotService(const std::vector& args); bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } - Result ParseLine(const std::vector& args); + Result ParseLine(std::vector&& args); Result ExecStart(); Result Start(); Result StartIfNotDisabled(); @@ -125,7 +125,7 @@ class Service { const std::vector& args() const { return args_; } private: - using OptionParser = Result (Service::*)(const std::vector& args); + using OptionParser = Result (Service::*)(std::vector&& args); class OptionParserMap; Result SetUpMountNamespace() const; @@ -138,39 +138,39 @@ class Service { void KillProcessGroup(int signal); void SetProcessAttributes(); - Result ParseCapabilities(const std::vector& args); - Result ParseClass(const std::vector& args); - Result ParseConsole(const std::vector& args); - Result ParseCritical(const std::vector& args); - Result ParseDisabled(const std::vector& args); - Result ParseEnterNamespace(const std::vector& args); - Result ParseGroup(const std::vector& args); - Result ParsePriority(const std::vector& args); - Result ParseInterface(const std::vector& args); - Result ParseIoprio(const std::vector& args); - Result ParseKeycodes(const std::vector& args); - Result ParseOneshot(const std::vector& args); - Result ParseOnrestart(const std::vector& args); - Result ParseOomScoreAdjust(const std::vector& args); - Result ParseOverride(const std::vector& args); - Result ParseMemcgLimitInBytes(const std::vector& args); - Result ParseMemcgSoftLimitInBytes(const std::vector& args); - Result ParseMemcgSwappiness(const std::vector& args); - Result ParseNamespace(const std::vector& args); - Result ParseProcessRlimit(const std::vector& args); - Result ParseRestartPeriod(const std::vector& args); - Result ParseSeclabel(const std::vector& args); - Result ParseSetenv(const std::vector& args); - Result ParseShutdown(const std::vector& args); - Result ParseSigstop(const std::vector& args); - Result ParseSocket(const std::vector& args); - Result ParseTimeoutPeriod(const std::vector& args); - Result ParseFile(const std::vector& args); - Result ParseUser(const std::vector& args); - Result ParseWritepid(const 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 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); template - Result AddDescriptor(const std::vector& args); + Result AddDescriptor(std::vector&& args); static unsigned long next_start_order_; static bool is_exec_service_running_;