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