SystemServer.java revision 6734b9f68062b04e602be0069ab028045c1fe3ec
1/* 2 * Copyright (C) 2006 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 19import com.android.server.am.ActivityManagerService; 20import com.android.server.sip.SipService; 21import com.android.internal.os.BinderInternal; 22import com.android.internal.os.SamplingProfilerIntegration; 23 24import dalvik.system.VMRuntime; 25import dalvik.system.Zygote; 26 27import android.accounts.AccountManagerService; 28import android.app.ActivityManagerNative; 29import android.bluetooth.BluetoothAdapter; 30import android.content.ContentResolver; 31import android.content.ContentService; 32import android.content.Context; 33import android.content.pm.IPackageManager; 34import android.database.ContentObserver; 35import android.media.AudioService; 36import android.os.Build; 37import android.os.Looper; 38import android.os.RemoteException; 39import android.os.ServiceManager; 40import android.os.StrictMode; 41import android.os.SystemClock; 42import android.os.SystemProperties; 43import android.provider.Settings; 44import android.server.BluetoothA2dpService; 45import android.server.BluetoothService; 46import android.server.search.SearchManagerService; 47import android.util.EventLog; 48import android.util.Slog; 49 50import java.io.File; 51import java.util.Timer; 52import java.util.TimerTask; 53 54class ServerThread extends Thread { 55 private static final String TAG = "SystemServer"; 56 57 ContentResolver mContentResolver; 58 59 private class AdbSettingsObserver extends ContentObserver { 60 public AdbSettingsObserver() { 61 super(null); 62 } 63 @Override 64 public void onChange(boolean selfChange) { 65 boolean enableAdb = (Settings.Secure.getInt(mContentResolver, 66 Settings.Secure.ADB_ENABLED, 0) > 0); 67 // setting this secure property will start or stop adbd 68 SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0"); 69 } 70 } 71 72 @Override 73 public void run() { 74 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, 75 SystemClock.uptimeMillis()); 76 77 Looper.prepare(); 78 79 android.os.Process.setThreadPriority( 80 android.os.Process.THREAD_PRIORITY_FOREGROUND); 81 82 BinderInternal.disableBackgroundScheduling(true); 83 android.os.Process.setCanSelfBackground(false); 84 85 String factoryTestStr = SystemProperties.get("ro.factorytest"); 86 int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF 87 : Integer.parseInt(factoryTestStr); 88 89 LightsService lights = null; 90 PowerManagerService power = null; 91 BatteryService battery = null; 92 ConnectivityService connectivity = null; 93 IPackageManager pm = null; 94 Context context = null; 95 WindowManagerService wm = null; 96 BluetoothService bluetooth = null; 97 BluetoothA2dpService bluetoothA2dp = null; 98 HeadsetObserver headset = null; 99 DockObserver dock = null; 100 UsbObserver usb = null; 101 UiModeManagerService uiMode = null; 102 RecognitionManagerService recognition = null; 103 ThrottleService throttle = null; 104 NetworkTimeUpdateService networkTimeUpdater = null; 105 106 // Critical services... 107 try { 108 Slog.i(TAG, "Entropy Service"); 109 ServiceManager.addService("entropy", new EntropyService()); 110 111 Slog.i(TAG, "Power Manager"); 112 power = new PowerManagerService(); 113 ServiceManager.addService(Context.POWER_SERVICE, power); 114 115 Slog.i(TAG, "Activity Manager"); 116 context = ActivityManagerService.main(factoryTest); 117 118 Slog.i(TAG, "Telephony Registry"); 119 ServiceManager.addService("telephony.registry", new TelephonyRegistry(context)); 120 121 AttributeCache.init(context); 122 123 Slog.i(TAG, "Package Manager"); 124 pm = PackageManagerService.main(context, 125 factoryTest != SystemServer.FACTORY_TEST_OFF); 126 127 ActivityManagerService.setSystemProcess(); 128 129 mContentResolver = context.getContentResolver(); 130 131 // The AccountManager must come before the ContentService 132 try { 133 Slog.i(TAG, "Account Manager"); 134 ServiceManager.addService(Context.ACCOUNT_SERVICE, 135 new AccountManagerService(context)); 136 } catch (Throwable e) { 137 Slog.e(TAG, "Failure starting Account Manager", e); 138 } 139 140 Slog.i(TAG, "Content Manager"); 141 ContentService.main(context, 142 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL); 143 144 Slog.i(TAG, "System Content Providers"); 145 ActivityManagerService.installSystemProviders(); 146 147 Slog.i(TAG, "Battery Service"); 148 battery = new BatteryService(context); 149 ServiceManager.addService("battery", battery); 150 151 Slog.i(TAG, "Lights Service"); 152 lights = new LightsService(context); 153 154 Slog.i(TAG, "Vibrator Service"); 155 ServiceManager.addService("vibrator", new VibratorService(context)); 156 157 // only initialize the power service after we have started the 158 // lights service, content providers and the battery service. 159 power.init(context, lights, ActivityManagerService.getDefault(), battery); 160 161 Slog.i(TAG, "Alarm Manager"); 162 AlarmManagerService alarm = new AlarmManagerService(context); 163 ServiceManager.addService(Context.ALARM_SERVICE, alarm); 164 165 Slog.i(TAG, "Init Watchdog"); 166 Watchdog.getInstance().init(context, battery, power, alarm, 167 ActivityManagerService.self()); 168 169 Slog.i(TAG, "Window Manager"); 170 wm = WindowManagerService.main(context, power, 171 factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL); 172 ServiceManager.addService(Context.WINDOW_SERVICE, wm); 173 174 ((ActivityManagerService)ServiceManager.getService("activity")) 175 .setWindowManager(wm); 176 177 // Skip Bluetooth if we have an emulator kernel 178 // TODO: Use a more reliable check to see if this product should 179 // support Bluetooth - see bug 988521 180 if (SystemProperties.get("ro.kernel.qemu").equals("1")) { 181 Slog.i(TAG, "Registering null Bluetooth Service (emulator)"); 182 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null); 183 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { 184 Slog.i(TAG, "Registering null Bluetooth Service (factory test)"); 185 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null); 186 } else { 187 Slog.i(TAG, "Bluetooth Service"); 188 bluetooth = new BluetoothService(context); 189 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, bluetooth); 190 bluetooth.initAfterRegistration(); 191 bluetoothA2dp = new BluetoothA2dpService(context, bluetooth); 192 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE, 193 bluetoothA2dp); 194 195 int bluetoothOn = Settings.Secure.getInt(mContentResolver, 196 Settings.Secure.BLUETOOTH_ON, 0); 197 if (bluetoothOn > 0) { 198 bluetooth.enable(); 199 } 200 } 201 202 } catch (RuntimeException e) { 203 Slog.e("System", "Failure starting core service", e); 204 } 205 206 DevicePolicyManagerService devicePolicy = null; 207 StatusBarManagerService statusBar = null; 208 InputMethodManagerService imm = null; 209 AppWidgetService appWidget = null; 210 NotificationManagerService notification = null; 211 WallpaperManagerService wallpaper = null; 212 LocationManagerService location = null; 213 CountryDetectorService countryDetector = null; 214 215 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { 216 try { 217 Slog.i(TAG, "Device Policy"); 218 devicePolicy = new DevicePolicyManagerService(context); 219 ServiceManager.addService(Context.DEVICE_POLICY_SERVICE, devicePolicy); 220 } catch (Throwable e) { 221 Slog.e(TAG, "Failure starting DevicePolicyService", e); 222 } 223 224 try { 225 Slog.i(TAG, "Status Bar"); 226 statusBar = new StatusBarManagerService(context); 227 ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar); 228 } catch (Throwable e) { 229 Slog.e(TAG, "Failure starting StatusBarManagerService", e); 230 } 231 232 try { 233 Slog.i(TAG, "Clipboard Service"); 234 ServiceManager.addService(Context.CLIPBOARD_SERVICE, 235 new ClipboardService(context)); 236 } catch (Throwable e) { 237 Slog.e(TAG, "Failure starting Clipboard Service", e); 238 } 239 240 try { 241 Slog.i(TAG, "Input Method Service"); 242 imm = new InputMethodManagerService(context, statusBar); 243 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm); 244 } catch (Throwable e) { 245 Slog.e(TAG, "Failure starting Input Manager Service", e); 246 } 247 248 try { 249 Slog.i(TAG, "NetStat Service"); 250 ServiceManager.addService("netstat", new NetStatService(context)); 251 } catch (Throwable e) { 252 Slog.e(TAG, "Failure starting NetStat Service", e); 253 } 254 255 try { 256 Slog.i(TAG, "NetworkManagement Service"); 257 ServiceManager.addService( 258 Context.NETWORKMANAGEMENT_SERVICE, new NetworkManagementService(context)); 259 } catch (Throwable e) { 260 Slog.e(TAG, "Failure starting NetworkManagement Service", e); 261 } 262 263 try { 264 Slog.i(TAG, "Connectivity Service"); 265 connectivity = ConnectivityService.getInstance(context); 266 ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity); 267 } catch (Throwable e) { 268 Slog.e(TAG, "Failure starting Connectivity Service", e); 269 } 270 271 try { 272 Slog.i(TAG, "Throttle Service"); 273 throttle = new ThrottleService(context); 274 ServiceManager.addService( 275 Context.THROTTLE_SERVICE, throttle); 276 } catch (Throwable e) { 277 Slog.e(TAG, "Failure starting ThrottleService", e); 278 } 279 280 try { 281 Slog.i(TAG, "Accessibility Manager"); 282 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE, 283 new AccessibilityManagerService(context)); 284 } catch (Throwable e) { 285 Slog.e(TAG, "Failure starting Accessibility Manager", e); 286 } 287 288 try { 289 /* 290 * NotificationManagerService is dependant on MountService, 291 * (for media / usb notifications) so we must start MountService first. 292 */ 293 Slog.i(TAG, "Mount Service"); 294 ServiceManager.addService("mount", new MountService(context)); 295 } catch (Throwable e) { 296 Slog.e(TAG, "Failure starting Mount Service", e); 297 } 298 299 try { 300 Slog.i(TAG, "Notification Manager"); 301 notification = new NotificationManagerService(context, statusBar, lights); 302 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification); 303 } catch (Throwable e) { 304 Slog.e(TAG, "Failure starting Notification Manager", e); 305 } 306 307 try { 308 Slog.i(TAG, "Device Storage Monitor"); 309 ServiceManager.addService(DeviceStorageMonitorService.SERVICE, 310 new DeviceStorageMonitorService(context)); 311 } catch (Throwable e) { 312 Slog.e(TAG, "Failure starting DeviceStorageMonitor service", e); 313 } 314 315 try { 316 Slog.i(TAG, "Location Manager"); 317 location = new LocationManagerService(context); 318 ServiceManager.addService(Context.LOCATION_SERVICE, location); 319 } catch (Throwable e) { 320 Slog.e(TAG, "Failure starting Location Manager", e); 321 } 322 323 try { 324 Slog.i(TAG, "Country Detector"); 325 countryDetector = new CountryDetectorService(context); 326 ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector); 327 } catch (Throwable e) { 328 Slog.e(TAG, "Failure starting Country Detector", e); 329 } 330 331 try { 332 Slog.i(TAG, "Search Service"); 333 ServiceManager.addService(Context.SEARCH_SERVICE, 334 new SearchManagerService(context)); 335 } catch (Throwable e) { 336 Slog.e(TAG, "Failure starting Search Service", e); 337 } 338 339 try { 340 Slog.i(TAG, "DropBox Service"); 341 ServiceManager.addService(Context.DROPBOX_SERVICE, 342 new DropBoxManagerService(context, new File("/data/system/dropbox"))); 343 } catch (Throwable e) { 344 Slog.e(TAG, "Failure starting DropBoxManagerService", e); 345 } 346 347 try { 348 Slog.i(TAG, "Wallpaper Service"); 349 wallpaper = new WallpaperManagerService(context); 350 ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper); 351 } catch (Throwable e) { 352 Slog.e(TAG, "Failure starting Wallpaper Service", e); 353 } 354 355 try { 356 Slog.i(TAG, "Audio Service"); 357 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context)); 358 } catch (Throwable e) { 359 Slog.e(TAG, "Failure starting Audio Service", e); 360 } 361 362 try { 363 Slog.i(TAG, "Headset Observer"); 364 // Listen for wired headset changes 365 headset = new HeadsetObserver(context); 366 } catch (Throwable e) { 367 Slog.e(TAG, "Failure starting HeadsetObserver", e); 368 } 369 370 try { 371 Slog.i(TAG, "Dock Observer"); 372 // Listen for dock station changes 373 dock = new DockObserver(context, power); 374 } catch (Throwable e) { 375 Slog.e(TAG, "Failure starting DockObserver", e); 376 } 377 378 try { 379 Slog.i(TAG, "USB Observer"); 380 // Listen for USB changes 381 usb = new UsbObserver(context); 382 } catch (Throwable e) { 383 Slog.e(TAG, "Failure starting UsbObserver", e); 384 } 385 386 try { 387 Slog.i(TAG, "UI Mode Manager Service"); 388 // Listen for UI mode changes 389 uiMode = new UiModeManagerService(context); 390 } catch (Throwable e) { 391 Slog.e(TAG, "Failure starting UiModeManagerService", e); 392 } 393 394 try { 395 Slog.i(TAG, "Backup Service"); 396 ServiceManager.addService(Context.BACKUP_SERVICE, 397 new BackupManagerService(context)); 398 } catch (Throwable e) { 399 Slog.e(TAG, "Failure starting Backup Service", e); 400 } 401 402 try { 403 Slog.i(TAG, "AppWidget Service"); 404 appWidget = new AppWidgetService(context); 405 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget); 406 } catch (Throwable e) { 407 Slog.e(TAG, "Failure starting AppWidget Service", e); 408 } 409 410 try { 411 Slog.i(TAG, "Recognition Service"); 412 recognition = new RecognitionManagerService(context); 413 } catch (Throwable e) { 414 Slog.e(TAG, "Failure starting Recognition Service", e); 415 } 416 417 try { 418 Slog.i(TAG, "DiskStats Service"); 419 ServiceManager.addService("diskstats", new DiskStatsService(context)); 420 } catch (Throwable e) { 421 Slog.e(TAG, "Failure starting DiskStats Service", e); 422 } 423 424 try { 425 // need to add this service even if SamplingProfilerIntegration.isEnabled() 426 // is false, because it is this service that detects system property change and 427 // turns on SamplingProfilerIntegration. Plus, when sampling profiler doesn't work, 428 // there is little overhead for running this service. 429 Slog.i(TAG, "SamplingProfiler Service"); 430 ServiceManager.addService("samplingprofiler", 431 new SamplingProfilerService(context)); 432 } catch (Throwable e) { 433 Slog.e(TAG, "Failure starting SamplingProfiler Service", e); 434 } 435 436 try { 437 SipService sipService = SipService.create(context); 438 if (sipService != null) { 439 Slog.i(TAG, "Sip Service"); 440 ServiceManager.addService("sip", sipService); 441 } 442 } catch (Throwable e) { 443 Slog.e(TAG, "Failure starting SIP Service", e); 444 } 445 446 try { 447 Slog.i(TAG, "NetworkTimeUpdateService"); 448 networkTimeUpdater = new NetworkTimeUpdateService(context); 449 } catch (Throwable e) { 450 Slog.e(TAG, "Failure starting NetworkTimeUpdate service"); 451 } 452 } 453 454 // make sure the ADB_ENABLED setting value matches the secure property value 455 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED, 456 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0); 457 458 // register observer to listen for settings changes 459 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED), 460 false, new AdbSettingsObserver()); 461 462 // Before things start rolling, be sure we have decided whether 463 // we are in safe mode. 464 final boolean safeMode = wm.detectSafeMode(); 465 if (safeMode) { 466 try { 467 ActivityManagerNative.getDefault().enterSafeMode(); 468 // Post the safe mode state in the Zygote class 469 Zygote.systemInSafeMode = true; 470 // Disable the JIT for the system_server process 471 VMRuntime.getRuntime().disableJitCompilation(); 472 } catch (RemoteException e) { 473 } 474 } else { 475 // Enable the JIT for the system_server process 476 VMRuntime.getRuntime().startJitCompilation(); 477 } 478 479 // It is now time to start up the app processes... 480 481 if (devicePolicy != null) { 482 devicePolicy.systemReady(); 483 } 484 485 if (notification != null) { 486 notification.systemReady(); 487 } 488 489 if (statusBar != null) { 490 statusBar.systemReady(); 491 } 492 wm.systemReady(); 493 power.systemReady(); 494 try { 495 pm.systemReady(); 496 } catch (RemoteException e) { 497 } 498 499 // These are needed to propagate to the runnable below. 500 final StatusBarManagerService statusBarF = statusBar; 501 final BatteryService batteryF = battery; 502 final ConnectivityService connectivityF = connectivity; 503 final DockObserver dockF = dock; 504 final UsbObserver usbF = usb; 505 final ThrottleService throttleF = throttle; 506 final UiModeManagerService uiModeF = uiMode; 507 final AppWidgetService appWidgetF = appWidget; 508 final WallpaperManagerService wallpaperF = wallpaper; 509 final InputMethodManagerService immF = imm; 510 final RecognitionManagerService recognitionF = recognition; 511 final LocationManagerService locationF = location; 512 final CountryDetectorService countryDetectorF = countryDetector; 513 final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater; 514 515 // We now tell the activity manager it is okay to run third party 516 // code. It will call back into us once it has gotten to the state 517 // where third party code can really run (but before it has actually 518 // started launching the initial applications), for us to complete our 519 // initialization. 520 ((ActivityManagerService)ActivityManagerNative.getDefault()) 521 .systemReady(new Runnable() { 522 public void run() { 523 Slog.i(TAG, "Making services ready"); 524 525 if (statusBarF != null) statusBarF.systemReady2(); 526 if (batteryF != null) batteryF.systemReady(); 527 if (connectivityF != null) connectivityF.systemReady(); 528 if (dockF != null) dockF.systemReady(); 529 if (usbF != null) usbF.systemReady(); 530 if (uiModeF != null) uiModeF.systemReady(); 531 if (recognitionF != null) recognitionF.systemReady(); 532 Watchdog.getInstance().start(); 533 534 // It is now okay to let the various system services start their 535 // third party code... 536 537 if (appWidgetF != null) appWidgetF.systemReady(safeMode); 538 if (wallpaperF != null) wallpaperF.systemReady(); 539 if (immF != null) immF.systemReady(); 540 if (locationF != null) locationF.systemReady(); 541 if (countryDetectorF != null) countryDetectorF.systemReady(); 542 if (throttleF != null) throttleF.systemReady(); 543 if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemReady(); 544 } 545 }); 546 547 // For debug builds, log event loop stalls to dropbox for analysis. 548 if (StrictMode.conditionallyEnableDebugLogging()) { 549 Slog.i(TAG, "Enabled StrictMode for system server main thread."); 550 } 551 552 Looper.loop(); 553 Slog.d(TAG, "System ServerThread is exiting!"); 554 } 555} 556 557public class SystemServer { 558 private static final String TAG = "SystemServer"; 559 560 public static final int FACTORY_TEST_OFF = 0; 561 public static final int FACTORY_TEST_LOW_LEVEL = 1; 562 public static final int FACTORY_TEST_HIGH_LEVEL = 2; 563 564 static Timer timer; 565 static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr 566 567 /** 568 * This method is called from Zygote to initialize the system. This will cause the native 569 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back 570 * up into init2() to start the Android services. 571 */ 572 native public static void init1(String[] args); 573 574 public static void main(String[] args) { 575 if (SamplingProfilerIntegration.isEnabled()) { 576 SamplingProfilerIntegration.start(); 577 timer = new Timer(); 578 timer.schedule(new TimerTask() { 579 @Override 580 public void run() { 581 SamplingProfilerIntegration.writeSnapshot("system_server", null); 582 } 583 }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL); 584 } 585 586 // The system server has to run all of the time, so it needs to be 587 // as efficient as possible with its memory usage. 588 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f); 589 590 System.loadLibrary("android_servers"); 591 init1(args); 592 } 593 594 public static final void init2() { 595 Slog.i(TAG, "Entered the Android system server!"); 596 Thread thr = new ServerThread(); 597 thr.setName("android.server.ServerThread"); 598 thr.start(); 599 } 600} 601