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