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