blob: 46314cfcf1bfa105dec256568c23b8da02d5d5f6 [file] [log] [blame]
Tom Cherry65a1ee82019-06-05 10:26:54 -07001/*
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
26using android::base::GetProperty;
27using android::base::SetProperty;
28using namespace std::literals;
29
30static 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
38static void ControlDefaultServices(bool start) {
Ytai Ben-Tsvi6025b732020-04-22 10:42:44 -070039 std::vector<std::string> services = {
Ytai Ben-Tsvi6025b732020-04-22 10:42:44 -070040 "netd",
41 "surfaceflinger",
42 "audioserver",
43 "zygote",
44 };
Tom Cherry65a1ee82019-06-05 10:26:54 -070045
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
63static 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
88extern "C" int start_main(int argc, char** argv) {
89 return StartStop(argc, argv, true);
90}
91
92extern "C" int stop_main(int argc, char** argv) {
93 return StartStop(argc, argv, false);
yawanng762653b2020-11-16 18:45:47 +000094}