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