| Tom Cherry | 65a1ee8 | 2019-06-05 10:26:54 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <stdlib.h> | 
|  | 18 | #include <unistd.h> | 
|  | 19 |  | 
|  | 20 | #include <iostream> | 
|  | 21 | #include <string> | 
|  | 22 | #include <vector> | 
|  | 23 |  | 
|  | 24 | #include <android-base/properties.h> | 
|  | 25 |  | 
|  | 26 | using android::base::GetProperty; | 
|  | 27 | using android::base::SetProperty; | 
|  | 28 | using namespace std::literals; | 
|  | 29 |  | 
|  | 30 | static void ControlService(bool start, const std::string& service) { | 
|  | 31 | if (!android::base::SetProperty(start ? "ctl.start" : "ctl.stop", service)) { | 
|  | 32 | std::cerr << "Unable to " << (start ? "start" : "stop") << " service '" << service | 
|  | 33 | << "'\nSee dmesg for error reason." << std::endl; | 
|  | 34 | exit(EXIT_FAILURE); | 
|  | 35 | } | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | static void ControlDefaultServices(bool start) { | 
|  | 39 | std::vector<std::string> services = {"netd", "surfaceflinger", "zygote"}; | 
|  | 40 |  | 
|  | 41 | // Only start zygote_secondary if not single arch. | 
|  | 42 | std::string zygote_configuration = GetProperty("ro.zygote", ""); | 
|  | 43 | if (zygote_configuration != "zygote32" && zygote_configuration != "zygote64") { | 
|  | 44 | services.emplace_back("zygote_secondary"); | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | if (start) { | 
|  | 48 | for (const auto& service : services) { | 
|  | 49 | ControlService(true, service); | 
|  | 50 | } | 
|  | 51 | } else { | 
|  | 52 | for (auto it = services.crbegin(); it != services.crend(); ++it) { | 
|  | 53 | ControlService(false, *it); | 
|  | 54 | } | 
|  | 55 | } | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | static int StartStop(int argc, char** argv, bool start) { | 
|  | 59 | if (getuid()) { | 
|  | 60 | std::cerr << "Must be root" << std::endl; | 
|  | 61 | return EXIT_FAILURE; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | if (argc == 1) { | 
|  | 65 | ControlDefaultServices(start); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | if (argc == 2 && argv[1] == "--help"s) { | 
|  | 69 | std::cout << "usage: " << (start ? "start" : "stop") | 
|  | 70 | << " [SERVICE...]\n" | 
|  | 71 | "\n" | 
|  | 72 | << (start ? "Starts" : "Stops") | 
|  | 73 | << " the given system service, or netd/surfaceflinger/zygotes." << std::endl; | 
|  | 74 | return EXIT_SUCCESS; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | for (int i = 1; i < argc; ++i) { | 
|  | 78 | ControlService(start, argv[i]); | 
|  | 79 | } | 
|  | 80 | return EXIT_SUCCESS; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | extern "C" int start_main(int argc, char** argv) { | 
|  | 84 | return StartStop(argc, argv, true); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | extern "C" int stop_main(int argc, char** argv) { | 
|  | 88 | return StartStop(argc, argv, false); | 
|  | 89 | } |