blob: 72ff6068cd6863bb762b98859fb2b63233aae94f [file] [log] [blame]
Adam Lesinski182f73f2013-12-05 16:48:06 -08001/*
2 * Copyright (C) 2013 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
17package com.android.server;
18
MÃ¥rten Kongstad2e0d0f32016-06-02 09:35:31 +020019import android.annotation.NonNull;
Adam Lesinski182f73f2013-12-05 16:48:06 -080020import android.content.Context;
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +090021import android.os.Trace;
Adam Lesinski182f73f2013-12-05 16:48:06 -080022import android.util.Slog;
23
Jeff Brownb880d882014-02-10 19:47:07 -080024import java.lang.reflect.Constructor;
25import java.lang.reflect.InvocationTargetException;
Adam Lesinski182f73f2013-12-05 16:48:06 -080026import java.util.ArrayList;
27
28/**
Adam Lesinskib102b2c2013-12-20 11:46:14 -080029 * Manages creating, starting, and other lifecycle events of
Jeff Brownb880d882014-02-10 19:47:07 -080030 * {@link com.android.server.SystemService system services}.
Adam Lesinskib102b2c2013-12-20 11:46:14 -080031 *
32 * {@hide}
Adam Lesinski182f73f2013-12-05 16:48:06 -080033 */
34public class SystemServiceManager {
35 private static final String TAG = "SystemServiceManager";
Fyodor Kupolov701a85f2017-04-25 12:52:08 -070036 private static final int SERVICE_CALL_WARN_TIME_MS = 50;
Adam Lesinski182f73f2013-12-05 16:48:06 -080037
38 private final Context mContext;
Amith Yamasani91588252013-11-22 08:25:26 -080039 private boolean mSafeMode;
Fyodor Kupolov1d87e402017-01-10 18:34:10 -080040 private boolean mRuntimeRestarted;
Adam Lesinski182f73f2013-12-05 16:48:06 -080041
42 // Services that should receive lifecycle events.
43 private final ArrayList<SystemService> mServices = new ArrayList<SystemService>();
44
45 private int mCurrentPhase = -1;
46
Fyodor Kupolov1d87e402017-01-10 18:34:10 -080047 SystemServiceManager(Context context) {
Adam Lesinski182f73f2013-12-05 16:48:06 -080048 mContext = context;
49 }
50
Jeff Brown6f357d32014-01-15 20:40:55 -080051 /**
Adam Lesinski2cb6c602014-02-14 17:19:56 -080052 * Starts a service by class name.
Jeff Brown6f357d32014-01-15 20:40:55 -080053 *
Adam Lesinski2cb6c602014-02-14 17:19:56 -080054 * @return The service instance.
Jeff Brown6f357d32014-01-15 20:40:55 -080055 */
56 @SuppressWarnings("unchecked")
Jeff Brown2c43c332014-06-12 22:38:59 -070057 public SystemService startService(String className) {
58 final Class<SystemService> serviceClass;
59 try {
60 serviceClass = (Class<SystemService>)Class.forName(className);
61 } catch (ClassNotFoundException ex) {
62 Slog.i(TAG, "Starting " + className);
63 throw new RuntimeException("Failed to create service " + className
64 + ": service class not found, usually indicates that the caller should "
65 + "have called PackageManager.hasSystemFeature() to check whether the "
66 + "feature is available on this device before trying to start the "
67 + "services that implement it", ex);
68 }
69 return startService(serviceClass);
Amith Yamasani91588252013-11-22 08:25:26 -080070 }
71
Adam Lesinski182f73f2013-12-05 16:48:06 -080072 /**
73 * Creates and starts a system service. The class must be a subclass of
74 * {@link com.android.server.SystemService}.
75 *
76 * @param serviceClass A Java class that implements the SystemService interface.
Jeff Brown6f357d32014-01-15 20:40:55 -080077 * @return The service instance, never null.
Adam Lesinski182f73f2013-12-05 16:48:06 -080078 * @throws RuntimeException if the service fails to start.
79 */
Jeff Brown6f357d32014-01-15 20:40:55 -080080 @SuppressWarnings("unchecked")
81 public <T extends SystemService> T startService(Class<T> serviceClass) {
Adam Lesinski182f73f2013-12-05 16:48:06 -080082 try {
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +090083 final String name = serviceClass.getName();
84 Slog.i(TAG, "Starting " + name);
85 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
Adam Lesinski182f73f2013-12-05 16:48:06 -080086
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +090087 // Create the service.
88 if (!SystemService.class.isAssignableFrom(serviceClass)) {
89 throw new RuntimeException("Failed to create " + name
90 + ": service must extend " + SystemService.class.getName());
91 }
92 final T service;
93 try {
94 Constructor<T> constructor = serviceClass.getConstructor(Context.class);
95 service = constructor.newInstance(mContext);
96 } catch (InstantiationException ex) {
97 throw new RuntimeException("Failed to create service " + name
98 + ": service could not be instantiated", ex);
99 } catch (IllegalAccessException ex) {
100 throw new RuntimeException("Failed to create service " + name
101 + ": service must have a public constructor with a Context argument", ex);
102 } catch (NoSuchMethodException ex) {
103 throw new RuntimeException("Failed to create service " + name
104 + ": service must have a public constructor with a Context argument", ex);
105 } catch (InvocationTargetException ex) {
106 throw new RuntimeException("Failed to create service " + name
107 + ": service constructor threw an exception", ex);
108 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800109
MÃ¥rten Kongstad2e0d0f32016-06-02 09:35:31 +0200110 startService(service);
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +0900111 return service;
112 } finally {
113 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800114 }
115 }
116
MÃ¥rten Kongstad2e0d0f32016-06-02 09:35:31 +0200117 public void startService(@NonNull final SystemService service) {
118 // Register it.
119 mServices.add(service);
120 // Start it.
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700121 long time = System.currentTimeMillis();
MÃ¥rten Kongstad2e0d0f32016-06-02 09:35:31 +0200122 try {
123 service.onStart();
124 } catch (RuntimeException ex) {
125 throw new RuntimeException("Failed to start service " + service.getClass().getName()
126 + ": onStart threw an exception", ex);
127 }
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700128 warnIfTooLong(System.currentTimeMillis() - time, service, "onStart");
MÃ¥rten Kongstad2e0d0f32016-06-02 09:35:31 +0200129 }
130
Adam Lesinski182f73f2013-12-05 16:48:06 -0800131 /**
132 * Starts the specified boot phase for all system services that have been started up to
133 * this point.
134 *
135 * @param phase The boot phase to start.
136 */
137 public void startBootPhase(final int phase) {
138 if (phase <= mCurrentPhase) {
139 throw new IllegalArgumentException("Next phase must be larger than previous");
140 }
141 mCurrentPhase = phase;
142
143 Slog.i(TAG, "Starting phase " + mCurrentPhase);
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +0900144 try {
145 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "OnBootPhase " + phase);
146 final int serviceLen = mServices.size();
147 for (int i = 0; i < serviceLen; i++) {
148 final SystemService service = mServices.get(i);
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700149 long time = System.currentTimeMillis();
Philip Cuadra4345c0d2016-08-17 18:02:24 -0700150 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, service.getClass().getName());
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +0900151 try {
152 service.onBootPhase(mCurrentPhase);
153 } catch (Exception ex) {
154 throw new RuntimeException("Failed to boot service "
155 + service.getClass().getName()
156 + ": onBootPhase threw an exception during phase "
157 + mCurrentPhase, ex);
158 }
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700159 warnIfTooLong(System.currentTimeMillis() - time, service, "onBootPhase");
Philip Cuadra4345c0d2016-08-17 18:02:24 -0700160 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800161 }
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +0900162 } finally {
163 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Adam Lesinski182f73f2013-12-05 16:48:06 -0800164 }
165 }
166
Keun-young Park4136d2d2017-05-08 14:51:59 -0700167 /**
168 * @return true if system has completed the boot; false otherwise.
169 */
170 public boolean isBootCompleted() {
171 return mCurrentPhase >= SystemService.PHASE_BOOT_COMPLETED;
172 }
173
Dianne Hackborn91097de2014-04-04 18:02:06 -0700174 public void startUser(final int userHandle) {
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700175 Slog.i(TAG, "Calling onStartUser u" + userHandle);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700176 final int serviceLen = mServices.size();
177 for (int i = 0; i < serviceLen; i++) {
178 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700179 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onStartUser "
180 + service.getClass().getName());
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700181 long time = System.currentTimeMillis();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700182 try {
183 service.onStartUser(userHandle);
184 } catch (Exception ex) {
185 Slog.wtf(TAG, "Failure reporting start of user " + userHandle
186 + " to service " + service.getClass().getName(), ex);
187 }
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700188 warnIfTooLong(System.currentTimeMillis() - time, service, "onStartUser ");
Philip Cuadraca3329c2016-06-24 12:02:06 -0700189 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700190 }
191 }
192
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700193 public void unlockUser(final int userHandle) {
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700194 Slog.i(TAG, "Calling onUnlockUser u" + userHandle);
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700195 final int serviceLen = mServices.size();
196 for (int i = 0; i < serviceLen; i++) {
197 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700198 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onUnlockUser "
199 + service.getClass().getName());
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700200 long time = System.currentTimeMillis();
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700201 try {
202 service.onUnlockUser(userHandle);
203 } catch (Exception ex) {
204 Slog.wtf(TAG, "Failure reporting unlock of user " + userHandle
205 + " to service " + service.getClass().getName(), ex);
206 }
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700207 warnIfTooLong(System.currentTimeMillis() - time, service, "onUnlockUser ");
Philip Cuadraca3329c2016-06-24 12:02:06 -0700208 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Jeff Sharkeybedbaa92015-12-02 16:42:25 -0700209 }
210 }
211
Dianne Hackborn91097de2014-04-04 18:02:06 -0700212 public void switchUser(final int userHandle) {
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700213 Slog.i(TAG, "Calling switchUser u" + userHandle);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700214 final int serviceLen = mServices.size();
215 for (int i = 0; i < serviceLen; i++) {
216 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700217 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onSwitchUser "
218 + service.getClass().getName());
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700219 long time = System.currentTimeMillis();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700220 try {
221 service.onSwitchUser(userHandle);
222 } catch (Exception ex) {
223 Slog.wtf(TAG, "Failure reporting switch of user " + userHandle
224 + " to service " + service.getClass().getName(), ex);
225 }
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700226 warnIfTooLong(System.currentTimeMillis() - time, service, "onSwitchUser");
Philip Cuadraca3329c2016-06-24 12:02:06 -0700227 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700228 }
229 }
230
231 public void stopUser(final int userHandle) {
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700232 Slog.i(TAG, "Calling onStopUser u" + userHandle);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700233 final int serviceLen = mServices.size();
234 for (int i = 0; i < serviceLen; i++) {
235 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700236 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onStopUser "
237 + service.getClass().getName());
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700238 long time = System.currentTimeMillis();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700239 try {
240 service.onStopUser(userHandle);
241 } catch (Exception ex) {
242 Slog.wtf(TAG, "Failure reporting stop of user " + userHandle
243 + " to service " + service.getClass().getName(), ex);
244 }
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700245 warnIfTooLong(System.currentTimeMillis() - time, service, "onStopUser");
Philip Cuadraca3329c2016-06-24 12:02:06 -0700246 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700247 }
248 }
249
250 public void cleanupUser(final int userHandle) {
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700251 Slog.i(TAG, "Calling onCleanupUser u" + userHandle);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700252 final int serviceLen = mServices.size();
253 for (int i = 0; i < serviceLen; i++) {
254 final SystemService service = mServices.get(i);
Philip Cuadraca3329c2016-06-24 12:02:06 -0700255 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onCleanupUser "
256 + service.getClass().getName());
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700257 long time = System.currentTimeMillis();
Dianne Hackborn91097de2014-04-04 18:02:06 -0700258 try {
259 service.onCleanupUser(userHandle);
260 } catch (Exception ex) {
261 Slog.wtf(TAG, "Failure reporting cleanup of user " + userHandle
262 + " to service " + service.getClass().getName(), ex);
263 }
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700264 warnIfTooLong(System.currentTimeMillis() - time, service, "onCleanupUser");
Philip Cuadraca3329c2016-06-24 12:02:06 -0700265 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
Dianne Hackborn91097de2014-04-04 18:02:06 -0700266 }
267 }
268
Amith Yamasani91588252013-11-22 08:25:26 -0800269 /** Sets the safe mode flag for services to query. */
Fyodor Kupolov1d87e402017-01-10 18:34:10 -0800270 void setSafeMode(boolean safeMode) {
Amith Yamasani91588252013-11-22 08:25:26 -0800271 mSafeMode = safeMode;
272 }
273
274 /**
275 * Returns whether we are booting into safe mode.
276 * @return safe mode flag
277 */
278 public boolean isSafeMode() {
279 return mSafeMode;
280 }
281
Adam Lesinski182f73f2013-12-05 16:48:06 -0800282 /**
Fyodor Kupolov1d87e402017-01-10 18:34:10 -0800283 * @return true if runtime was restarted, false if it's normal boot
284 */
285 public boolean isRuntimeRestarted() {
286 return mRuntimeRestarted;
287 }
288
289 void setRuntimeRestarted(boolean runtimeRestarted) {
290 mRuntimeRestarted = runtimeRestarted;
291 }
292
Fyodor Kupolov701a85f2017-04-25 12:52:08 -0700293 private void warnIfTooLong(long duration, SystemService service, String operation) {
294 if (duration > SERVICE_CALL_WARN_TIME_MS) {
295 Slog.w(TAG, "Service " + service.getClass().getName() + " took " + duration + " ms in "
296 + operation);
297 }
298 }
Fyodor Kupolov1d87e402017-01-10 18:34:10 -0800299
300 /**
Adam Lesinski182f73f2013-12-05 16:48:06 -0800301 * Outputs the state of this manager to the System log.
302 */
303 public void dump() {
304 StringBuilder builder = new StringBuilder();
305 builder.append("Current phase: ").append(mCurrentPhase).append("\n");
306 builder.append("Services:\n");
307 final int startedLen = mServices.size();
308 for (int i = 0; i < startedLen; i++) {
309 final SystemService service = mServices.get(i);
310 builder.append("\t")
311 .append(service.getClass().getSimpleName())
312 .append("\n");
313 }
314
315 Slog.e(TAG, builder.toString());
316 }
Adam Lesinski182f73f2013-12-05 16:48:06 -0800317}