init: use rvalues references for callees of ParseLineSection()
ParseLineSection() provides 'args' as an rvalue reference, so its
callers can and should use it as such. This saves some copying
overhead and cleans up the code a bit.
Test: boot
Change-Id: Ib906318583dc81de9ea585f5f09fdff35403be1b
diff --git a/init/action.cpp b/init/action.cpp
index 11335ca..94ccef2 100644
--- a/init/action.cpp
+++ b/init/action.cpp
@@ -44,9 +44,12 @@
return function(builtin_arguments);
}
-Command::Command(BuiltinFunction f, bool execute_in_subcontext,
- const std::vector<std::string>& 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<std::string>&& args,
+ int line)
+ : func_(std::move(f)),
+ execute_in_subcontext_(execute_in_subcontext),
+ args_(std::move(args)),
+ line_(line) {}
Result<Success> Command::InvokeFunc(Subcontext* subcontext) const {
if (subcontext) {
@@ -80,7 +83,7 @@
const KeywordFunctionMap* Action::function_map_ = nullptr;
-Result<Success> Action::AddCommand(const std::vector<std::string>& args, int line) {
+Result<Success> Action::AddCommand(std::vector<std::string>&& args, int line) {
if (!function_map_) {
return Error() << "no function map available";
}
@@ -88,12 +91,12 @@
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<std::string>& args, int line) {
- commands_.emplace_back(f, false, args, line);
+void Action::AddCommand(BuiltinFunction f, std::vector<std::string>&& 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 4f063cc..967c682 100644
--- a/init/action.h
+++ b/init/action.h
@@ -36,7 +36,7 @@
class Command {
public:
- Command(BuiltinFunction f, bool execute_in_subcontext, const std::vector<std::string>& args,
+ Command(BuiltinFunction f, bool execute_in_subcontext, std::vector<std::string>&& args,
int line);
Result<Success> InvokeFunc(Subcontext* subcontext) const;
@@ -61,8 +61,8 @@
const std::string& event_trigger,
const std::map<std::string, std::string>& property_triggers);
- Result<Success> AddCommand(const std::vector<std::string>& args, int line);
- void AddCommand(BuiltinFunction f, const std::vector<std::string>& args, int line);
+ Result<Success> AddCommand(std::vector<std::string>&& args, int line);
+ void AddCommand(BuiltinFunction f, std::vector<std::string>&& 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 22977bb..9de4085 100644
--- a/init/action_manager.cpp
+++ b/init/action_manager.cpp
@@ -47,9 +47,7 @@
void ActionManager::QueueBuiltinAction(BuiltinFunction func, const std::string& name) {
auto action = std::make_unique<Action>(true, nullptr, "<Builtin Action>", 0, name,
std::map<std::string, std::string>{});
- std::vector<std::string> 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 0b1425d..b7de45d 100644
--- a/init/service.cpp
+++ b/init/service.cpp
@@ -400,7 +400,7 @@
[] (const auto& info) { LOG(INFO) << *info; });
}
-Result<Success> Service::ParseCapabilities(const std::vector<std::string>& args) {
+Result<Success> Service::ParseCapabilities(std::vector<std::string>&& args) {
capabilities_ = 0;
if (!CapAmbientSupported()) {
@@ -429,29 +429,29 @@
return Success();
}
-Result<Success> Service::ParseClass(const std::vector<std::string>& args) {
+Result<Success> Service::ParseClass(std::vector<std::string>&& args) {
classnames_ = std::set<std::string>(args.begin() + 1, args.end());
return Success();
}
-Result<Success> Service::ParseConsole(const std::vector<std::string>& args) {
+Result<Success> Service::ParseConsole(std::vector<std::string>&& args) {
flags_ |= SVC_CONSOLE;
console_ = args.size() > 1 ? "/dev/" + args[1] : "";
return Success();
}
-Result<Success> Service::ParseCritical(const std::vector<std::string>& args) {
+Result<Success> Service::ParseCritical(std::vector<std::string>&& args) {
flags_ |= SVC_CRITICAL;
return Success();
}
-Result<Success> Service::ParseDisabled(const std::vector<std::string>& args) {
+Result<Success> Service::ParseDisabled(std::vector<std::string>&& args) {
flags_ |= SVC_DISABLED;
flags_ |= SVC_RC_DISABLED;
return Success();
}
-Result<Success> Service::ParseEnterNamespace(const std::vector<std::string>& args) {
+Result<Success> Service::ParseEnterNamespace(std::vector<std::string>&& args) {
if (args[1] != "net") {
return Error() << "Init only supports entering network namespaces";
}
@@ -461,11 +461,11 @@
// 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<Success> Service::ParseGroup(const std::vector<std::string>& args) {
+Result<Success> Service::ParseGroup(std::vector<std::string>&& args) {
auto gid = DecodeUid(args[1]);
if (!gid) {
return Error() << "Unable to decode GID for '" << args[1] << "': " << gid.error();
@@ -482,7 +482,7 @@
return Success();
}
-Result<Success> Service::ParsePriority(const std::vector<std::string>& args) {
+Result<Success> Service::ParsePriority(std::vector<std::string>&& args) {
priority_ = 0;
if (!ParseInt(args[1], &priority_,
static_cast<int>(ANDROID_PRIORITY_HIGHEST), // highest is negative
@@ -493,7 +493,7 @@
return Success();
}
-Result<Success> Service::ParseInterface(const std::vector<std::string>& args) {
+Result<Success> Service::ParseInterface(std::vector<std::string>&& args) {
const std::string& interface_name = args[1];
const std::string& instance_name = args[2];
@@ -524,7 +524,7 @@
return Success();
}
-Result<Success> Service::ParseIoprio(const std::vector<std::string>& args) {
+Result<Success> Service::ParseIoprio(std::vector<std::string>&& args) {
if (!ParseInt(args[2], &ioprio_pri_, 0, 7)) {
return Error() << "priority value must be range 0 - 7";
}
@@ -542,7 +542,7 @@
return Success();
}
-Result<Success> Service::ParseKeycodes(const std::vector<std::string>& args) {
+Result<Success> Service::ParseKeycodes(std::vector<std::string>&& args) {
for (std::size_t i = 1; i < args.size(); i++) {
int code;
if (ParseInt(args[i], &code, 0, KEY_MAX)) {
@@ -557,21 +557,21 @@
return Success();
}
-Result<Success> Service::ParseOneshot(const std::vector<std::string>& args) {
+Result<Success> Service::ParseOneshot(std::vector<std::string>&& args) {
flags_ |= SVC_ONESHOT;
return Success();
}
-Result<Success> Service::ParseOnrestart(const std::vector<std::string>& args) {
- std::vector<std::string> str_args(args.begin() + 1, args.end());
+Result<Success> Service::ParseOnrestart(std::vector<std::string>&& 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<Success> Service::ParseNamespace(const std::vector<std::string>& args) {
+Result<Success> Service::ParseNamespace(std::vector<std::string>&& args) {
for (size_t i = 1; i < args.size(); i++) {
if (args[i] == "pid") {
namespace_flags_ |= CLONE_NEWPID;
@@ -586,40 +586,40 @@
return Success();
}
-Result<Success> Service::ParseOomScoreAdjust(const std::vector<std::string>& args) {
+Result<Success> Service::ParseOomScoreAdjust(std::vector<std::string>&& 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<Success> Service::ParseOverride(const std::vector<std::string>& args) {
+Result<Success> Service::ParseOverride(std::vector<std::string>&& args) {
override_ = true;
return Success();
}
-Result<Success> Service::ParseMemcgSwappiness(const std::vector<std::string>& args) {
+Result<Success> Service::ParseMemcgSwappiness(std::vector<std::string>&& args) {
if (!ParseInt(args[1], &swappiness_, 0)) {
return Error() << "swappiness value must be equal or greater than 0";
}
return Success();
}
-Result<Success> Service::ParseMemcgLimitInBytes(const std::vector<std::string>& args) {
+Result<Success> Service::ParseMemcgLimitInBytes(std::vector<std::string>&& 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<Success> Service::ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args) {
+Result<Success> Service::ParseMemcgSoftLimitInBytes(std::vector<std::string>&& 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<Success> Service::ParseProcessRlimit(const std::vector<std::string>& args) {
+Result<Success> Service::ParseProcessRlimit(std::vector<std::string>&& args) {
auto rlimit = ParseRlimit(args);
if (!rlimit) return rlimit.error();
@@ -627,7 +627,7 @@
return Success();
}
-Result<Success> Service::ParseRestartPeriod(const std::vector<std::string>& args) {
+Result<Success> Service::ParseRestartPeriod(std::vector<std::string>&& args) {
int period;
if (!ParseInt(args[1], &period, 5)) {
return Error() << "restart_period value must be an integer >= 5";
@@ -636,22 +636,22 @@
return Success();
}
-Result<Success> Service::ParseSeclabel(const std::vector<std::string>& args) {
- seclabel_ = args[1];
+Result<Success> Service::ParseSeclabel(std::vector<std::string>&& args) {
+ seclabel_ = std::move(args[1]);
return Success();
}
-Result<Success> Service::ParseSigstop(const std::vector<std::string>& args) {
+Result<Success> Service::ParseSigstop(std::vector<std::string>&& args) {
sigstop_ = true;
return Success();
}
-Result<Success> Service::ParseSetenv(const std::vector<std::string>& args) {
- environment_vars_.emplace_back(args[1], args[2]);
+Result<Success> Service::ParseSetenv(std::vector<std::string>&& args) {
+ environment_vars_.emplace_back(std::move(args[1]), std::move(args[2]));
return Success();
}
-Result<Success> Service::ParseShutdown(const std::vector<std::string>& args) {
+Result<Success> Service::ParseShutdown(std::vector<std::string>&& args) {
if (args[1] == "critical") {
flags_ |= SVC_SHUTDOWN_CRITICAL;
return Success();
@@ -659,7 +659,7 @@
return Error() << "Invalid shutdown option";
}
-Result<Success> Service::ParseTimeoutPeriod(const std::vector<std::string>& args) {
+Result<Success> Service::ParseTimeoutPeriod(std::vector<std::string>&& args) {
int period;
if (!ParseInt(args[1], &period, 1)) {
return Error() << "timeout_period value must be an integer >= 1";
@@ -669,7 +669,7 @@
}
template <typename T>
-Result<Success> Service::AddDescriptor(const std::vector<std::string>& args) {
+Result<Success> Service::AddDescriptor(std::vector<std::string>&& args) {
int perm = args.size() > 3 ? std::strtoul(args[3].c_str(), 0, 8) : -1;
Result<uid_t> uid = 0;
Result<gid_t> gid = 0;
@@ -704,26 +704,26 @@
}
// name type perm [ uid gid context ]
-Result<Success> Service::ParseSocket(const std::vector<std::string>& args) {
+Result<Success> Service::ParseSocket(std::vector<std::string>&& 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<SocketInfo>(args);
+ return AddDescriptor<SocketInfo>(std::move(args));
}
// name type perm [ uid gid context ]
-Result<Success> Service::ParseFile(const std::vector<std::string>& args) {
+Result<Success> Service::ParseFile(std::vector<std::string>&& 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<FileInfo>(args);
+ return AddDescriptor<FileInfo>(std::move(args));
}
-Result<Success> Service::ParseUser(const std::vector<std::string>& args) {
+Result<Success> Service::ParseUser(std::vector<std::string>&& args) {
auto uid = DecodeUid(args[1]);
if (!uid) {
return Error() << "Unable to find UID for '" << args[1] << "': " << uid.error();
@@ -732,8 +732,9 @@
return Success();
}
-Result<Success> Service::ParseWritepid(const std::vector<std::string>& args) {
- writepid_files_.assign(args.begin() + 1, args.end());
+Result<Success> Service::ParseWritepid(std::vector<std::string>&& args) {
+ args.erase(args.begin());
+ writepid_files_ = std::move(args);
return Success();
}
@@ -792,13 +793,13 @@
return option_parsers;
}
-Result<Success> Service::ParseLine(const std::vector<std::string>& args) {
+Result<Success> Service::ParseLine(std::vector<std::string>&& 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<Success> Service::ExecStart() {
diff --git a/init/service.h b/init/service.h
index ee53adf..c7beee9 100644
--- a/init/service.h
+++ b/init/service.h
@@ -75,7 +75,7 @@
static std::unique_ptr<Service> MakeTemporaryOneshotService(const std::vector<std::string>& args);
bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; }
- Result<Success> ParseLine(const std::vector<std::string>& args);
+ Result<Success> ParseLine(std::vector<std::string>&& args);
Result<Success> ExecStart();
Result<Success> Start();
Result<Success> StartIfNotDisabled();
@@ -125,7 +125,7 @@
const std::vector<std::string>& args() const { return args_; }
private:
- using OptionParser = Result<Success> (Service::*)(const std::vector<std::string>& args);
+ using OptionParser = Result<Success> (Service::*)(std::vector<std::string>&& args);
class OptionParserMap;
Result<Success> SetUpMountNamespace() const;
@@ -138,39 +138,39 @@
void KillProcessGroup(int signal);
void SetProcessAttributes();
- Result<Success> ParseCapabilities(const std::vector<std::string>& args);
- Result<Success> ParseClass(const std::vector<std::string>& args);
- Result<Success> ParseConsole(const std::vector<std::string>& args);
- Result<Success> ParseCritical(const std::vector<std::string>& args);
- Result<Success> ParseDisabled(const std::vector<std::string>& args);
- Result<Success> ParseEnterNamespace(const std::vector<std::string>& args);
- Result<Success> ParseGroup(const std::vector<std::string>& args);
- Result<Success> ParsePriority(const std::vector<std::string>& args);
- Result<Success> ParseInterface(const std::vector<std::string>& args);
- Result<Success> ParseIoprio(const std::vector<std::string>& args);
- Result<Success> ParseKeycodes(const std::vector<std::string>& args);
- Result<Success> ParseOneshot(const std::vector<std::string>& args);
- Result<Success> ParseOnrestart(const std::vector<std::string>& args);
- Result<Success> ParseOomScoreAdjust(const std::vector<std::string>& args);
- Result<Success> ParseOverride(const std::vector<std::string>& args);
- Result<Success> ParseMemcgLimitInBytes(const std::vector<std::string>& args);
- Result<Success> ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args);
- Result<Success> ParseMemcgSwappiness(const std::vector<std::string>& args);
- Result<Success> ParseNamespace(const std::vector<std::string>& args);
- Result<Success> ParseProcessRlimit(const std::vector<std::string>& args);
- Result<Success> ParseRestartPeriod(const std::vector<std::string>& args);
- Result<Success> ParseSeclabel(const std::vector<std::string>& args);
- Result<Success> ParseSetenv(const std::vector<std::string>& args);
- Result<Success> ParseShutdown(const std::vector<std::string>& args);
- Result<Success> ParseSigstop(const std::vector<std::string>& args);
- Result<Success> ParseSocket(const std::vector<std::string>& args);
- Result<Success> ParseTimeoutPeriod(const std::vector<std::string>& args);
- Result<Success> ParseFile(const std::vector<std::string>& args);
- Result<Success> ParseUser(const std::vector<std::string>& args);
- Result<Success> ParseWritepid(const std::vector<std::string>& args);
+ Result<Success> ParseCapabilities(std::vector<std::string>&& args);
+ Result<Success> ParseClass(std::vector<std::string>&& args);
+ Result<Success> ParseConsole(std::vector<std::string>&& args);
+ Result<Success> ParseCritical(std::vector<std::string>&& args);
+ Result<Success> ParseDisabled(std::vector<std::string>&& args);
+ Result<Success> ParseEnterNamespace(std::vector<std::string>&& args);
+ Result<Success> ParseGroup(std::vector<std::string>&& args);
+ Result<Success> ParsePriority(std::vector<std::string>&& args);
+ Result<Success> ParseInterface(std::vector<std::string>&& args);
+ Result<Success> ParseIoprio(std::vector<std::string>&& args);
+ Result<Success> ParseKeycodes(std::vector<std::string>&& args);
+ Result<Success> ParseOneshot(std::vector<std::string>&& args);
+ Result<Success> ParseOnrestart(std::vector<std::string>&& args);
+ Result<Success> ParseOomScoreAdjust(std::vector<std::string>&& args);
+ Result<Success> ParseOverride(std::vector<std::string>&& args);
+ Result<Success> ParseMemcgLimitInBytes(std::vector<std::string>&& args);
+ Result<Success> ParseMemcgSoftLimitInBytes(std::vector<std::string>&& args);
+ Result<Success> ParseMemcgSwappiness(std::vector<std::string>&& args);
+ Result<Success> ParseNamespace(std::vector<std::string>&& args);
+ Result<Success> ParseProcessRlimit(std::vector<std::string>&& args);
+ Result<Success> ParseRestartPeriod(std::vector<std::string>&& args);
+ Result<Success> ParseSeclabel(std::vector<std::string>&& args);
+ Result<Success> ParseSetenv(std::vector<std::string>&& args);
+ Result<Success> ParseShutdown(std::vector<std::string>&& args);
+ Result<Success> ParseSigstop(std::vector<std::string>&& args);
+ Result<Success> ParseSocket(std::vector<std::string>&& args);
+ Result<Success> ParseTimeoutPeriod(std::vector<std::string>&& args);
+ Result<Success> ParseFile(std::vector<std::string>&& args);
+ Result<Success> ParseUser(std::vector<std::string>&& args);
+ Result<Success> ParseWritepid(std::vector<std::string>&& args);
template <typename T>
- Result<Success> AddDescriptor(const std::vector<std::string>& args);
+ Result<Success> AddDescriptor(std::vector<std::string>&& args);
static unsigned long next_start_order_;
static bool is_exec_service_running_;