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