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 
ControlService(bool start,const std::string & service)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 
ControlDefaultServices(bool start)38 static void ControlDefaultServices(bool start) {
39     std::vector<std::string> services = {
40         "netd",
41         "surfaceflinger",
42         "audioserver",
43         "zygote",
44     };
45 
46     // Only start zygote_secondary if not single arch.
47     std::string zygote_configuration = GetProperty("ro.zygote", "");
48     if (zygote_configuration != "zygote32" && zygote_configuration != "zygote64") {
49         services.emplace_back("zygote_secondary");
50     }
51 
52     if (start) {
53         for (const auto& service : services) {
54             ControlService(true, service);
55         }
56     } else {
57         for (auto it = services.crbegin(); it != services.crend(); ++it) {
58             ControlService(false, *it);
59         }
60     }
61 }
62 
StartStop(int argc,char ** argv,bool start)63 static int StartStop(int argc, char** argv, bool start) {
64     if (getuid()) {
65         std::cerr << "Must be root" << std::endl;
66         return EXIT_FAILURE;
67     }
68 
69     if (argc == 1) {
70         ControlDefaultServices(start);
71     }
72 
73     if (argc == 2 && argv[1] == "--help"s) {
74         std::cout << "usage: " << (start ? "start" : "stop")
75                   << " [SERVICE...]\n"
76                      "\n"
77                   << (start ? "Starts" : "Stops")
78                   << " the given system service, or netd/surfaceflinger/zygotes." << std::endl;
79         return EXIT_SUCCESS;
80     }
81 
82     for (int i = 1; i < argc; ++i) {
83         ControlService(start, argv[i]);
84     }
85     return EXIT_SUCCESS;
86 }
87 
start_main(int argc,char ** argv)88 extern "C" int start_main(int argc, char** argv) {
89     return StartStop(argc, argv, true);
90 }
91 
stop_main(int argc,char ** argv)92 extern "C" int stop_main(int argc, char** argv) {
93     return StartStop(argc, argv, false);
94 }
95