diff --git a/init/README.md b/init/README.md index b10ca9e2b..ebab07359 100644 --- a/init/README.md +++ b/init/README.md @@ -496,8 +496,9 @@ Commands currently running, without disabling them. They can be restarted later using `class_start`. -`class_restart ` -> Restarts all services of the specified class. +`class_restart [--only-enabled] ` +> Restarts all services of the specified class. If `--only-enabled` is + specified, then disabled services are skipped. `copy ` > Copies a file. Similar to write, but useful for binary/large diff --git a/init/builtins.cpp b/init/builtins.cpp index 50a0cb2ca..98831e10d 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -190,7 +190,31 @@ 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 {}; - ForEachServiceInClass(args[1], &Service::Restart); + + std::string classname; + + CHECK(args.size() == 2 || args.size() == 3); + + bool only_enabled = false; + if (args.size() == 3) { + if (args[1] != "--only-enabled") { + return Error() << "Unexpected argument: " << args[1]; + } + only_enabled = true; + classname = args[2]; + } else if (args.size() == 2) { + classname = args[1]; + } + + for (const auto& service : ServiceList::GetInstance()) { + if (!service->classnames().count(classname)) { + continue; + } + if (only_enabled && !service->IsEnabled()) { + continue; + } + service->Restart(); + } return {}; } @@ -1392,7 +1416,7 @@ const BuiltinFunctionMap& GetBuiltinFunctionMap() { {"chmod", {2, 2, {true, do_chmod}}}, {"chown", {2, 3, {true, do_chown}}}, {"class_reset", {1, 1, {false, do_class_reset}}}, - {"class_restart", {1, 1, {false, do_class_restart}}}, + {"class_restart", {1, 2, {false, do_class_restart}}}, {"class_start", {1, 1, {false, do_class_start}}}, {"class_stop", {1, 1, {false, do_class_stop}}}, {"copy", {2, 2, {true, do_copy}}},