diff --git a/init/README.md b/init/README.md index ebab07359..c102b1f9e 100644 --- a/init/README.md +++ b/init/README.md @@ -641,9 +641,10 @@ provides the `aidl_lazy_test_1` interface. configurations. Intended to be used only once when apexd notifies the mount event by setting `apexd.status` to ready. -`restart ` +`restart [--only-if-running] ` > Stops and restarts a running service, does nothing if the service is currently - restarting, otherwise, it just starts the service. + restarting, otherwise, it just starts the service. If "--only-if-running" is + specified, the service is only restarted if it is already running. `restorecon [ \* ]` > Restore the file named by _path_ to the security context specified diff --git a/init/builtins.cpp b/init/builtins.cpp index 98831e10d..8045c7179 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -774,8 +774,21 @@ static Result do_stop(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"; + bool only_if_running = false; + if (args.size() == 3) { + if (args[1] == "--only-if-running") { + only_if_running = true; + } else { + return Error() << "Unknown argument to restart: " << args[1]; + } + } + + const auto& classname = args[args.size() - 1]; + Service* svc = ServiceList::GetInstance().FindService(classname); + if (!svc) return Error() << "service " << classname << " not found"; + if (only_if_running && !svc->IsRunning()) { + return {}; + } svc->Restart(); return {}; } @@ -1453,7 +1466,7 @@ const BuiltinFunctionMap& GetBuiltinFunctionMap() { {"update_linker_config", {0, 0, {false, do_update_linker_config}}}, {"readahead", {1, 2, {true, do_readahead}}}, {"remount_userdata", {0, 0, {false, do_remount_userdata}}}, - {"restart", {1, 1, {false, do_restart}}}, + {"restart", {1, 2, {false, do_restart}}}, {"restorecon", {1, kMax, {true, do_restorecon}}}, {"restorecon_recursive", {1, kMax, {true, do_restorecon_recursive}}}, {"rm", {1, 1, {true, do_rm}}},