SystemServer.java revision 8dfabd9c6ffc64a9f558610e98ae19fbe41940fb
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            // Sensor Service is needed by Window Manager, so this goes first
166            Slog.i(TAG, "Sensor Service");
167            ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
168
169            Slog.i(TAG, "Window Manager");
170            wm = WindowManagerService.main(context, power,
171                    factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
172            ServiceManager.addService(Context.WINDOW_SERVICE, wm);
173
174            ((ActivityManagerService)ServiceManager.getService("activity"))
175                    .setWindowManager(wm);
176
177            // Skip Bluetooth if we have an emulator kernel
178            // TODO: Use a more reliable check to see if this product should
179            // support Bluetooth - see bug 988521
180            if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
181                Slog.i(TAG, "Registering null Bluetooth Service (emulator)");
182                ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null);
183            } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
184                Slog.i(TAG, "Registering null Bluetooth Service (factory test)");
185                ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null);
186            } else {
187                Slog.i(TAG, "Bluetooth Service");
188                bluetooth = new BluetoothService(context);
189                ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, bluetooth);
190                bluetooth.initAfterRegistration();
191                bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
192                ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
193                                          bluetoothA2dp);
194
195                int bluetoothOn = Settings.Secure.getInt(mContentResolver,
196                    Settings.Secure.BLUETOOTH_ON, 0);
197                if (bluetoothOn > 0) {
198                    bluetooth.enable();
199                }
200            }
201
202        } catch (RuntimeException e) {
203            Slog.e("System", "Failure starting core service", e);
204        }
205
206        DevicePolicyManagerService devicePolicy = null;
207        StatusBarManagerService statusBar = null;
208        InputMethodManagerService imm = null;
209        AppWidgetService appWidget = null;
210        NotificationManagerService notification = null;
211        WallpaperManagerService wallpaper = null;
212        LocationManagerService location = null;
213
214        if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
215            try {
216                Slog.i(TAG, "Device Policy");
217                devicePolicy = new DevicePolicyManagerService(context);
218                ServiceManager.addService(Context.DEVICE_POLICY_SERVICE, devicePolicy);
219            } catch (Throwable e) {
220                Slog.e(TAG, "Failure starting DevicePolicyService", e);
221            }
222
223            try {
224                Slog.i(TAG, "Status Bar");
225                statusBar = new StatusBarManagerService(context);
226                ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
227            } catch (Throwable e) {
228                Slog.e(TAG, "Failure starting StatusBarManagerService", e);
229            }
230
231            try {
232                Slog.i(TAG, "Clipboard Service");
233                ServiceManager.addService(Context.CLIPBOARD_SERVICE,
234                        new ClipboardService(context));
235            } catch (Throwable e) {
236                Slog.e(TAG, "Failure starting Clipboard Service", e);
237            }
238
239            try {
240                Slog.i(TAG, "Input Method Service");
241                imm = new InputMethodManagerService(context, statusBar);
242                ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
243            } catch (Throwable e) {
244                Slog.e(TAG, "Failure starting Input Manager Service", e);
245            }
246
247            try {
248                Slog.i(TAG, "NetStat Service");
249                ServiceManager.addService("netstat", new NetStatService(context));
250            } catch (Throwable e) {
251                Slog.e(TAG, "Failure starting NetStat Service", e);
252            }
253
254            try {
255                Slog.i(TAG, "NetworkManagement Service");
256                ServiceManager.addService(
257                        Context.NETWORKMANAGEMENT_SERVICE, new NetworkManagementService(context));
258            } catch (Throwable e) {
259                Slog.e(TAG, "Failure starting NetworkManagement Service", e);
260            }
261
262            try {
263                Slog.i(TAG, "Connectivity Service");
264                connectivity = ConnectivityService.getInstance(context);
265                ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
266            } catch (Throwable e) {
267                Slog.e(TAG, "Failure starting Connectivity Service", e);
268            }
269
270            try {
271                Slog.i(TAG, "Throttle Service");
272                throttle = new ThrottleService(context);
273                ServiceManager.addService(
274                        Context.THROTTLE_SERVICE, throttle);
275            } catch (Throwable e) {
276                Slog.e(TAG, "Failure starting ThrottleService", e);
277            }
278
279            try {
280              Slog.i(TAG, "Accessibility Manager");
281              ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
282                      new AccessibilityManagerService(context));
283            } catch (Throwable e) {
284              Slog.e(TAG, "Failure starting Accessibility Manager", e);
285            }
286
287            try {
288                /*
289                 * NotificationManagerService is dependant on MountService,
290                 * (for media / usb notifications) so we must start MountService first.
291                 */
292                Slog.i(TAG, "Mount Service");
293                ServiceManager.addService("mount", new MountService(context));
294            } catch (Throwable e) {
295                Slog.e(TAG, "Failure starting Mount Service", e);
296            }
297
298            try {
299                Slog.i(TAG, "Notification Manager");
300                notification = new NotificationManagerService(context, statusBar, lights);
301                ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
302            } catch (Throwable e) {
303                Slog.e(TAG, "Failure starting Notification Manager", e);
304            }
305
306            try {
307                Slog.i(TAG, "Device Storage Monitor");
308                ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
309                        new DeviceStorageMonitorService(context));
310            } catch (Throwable e) {
311                Slog.e(TAG, "Failure starting DeviceStorageMonitor service", e);
312            }
313
314            try {
315                Slog.i(TAG, "Location Manager");
316                location = new LocationManagerService(context);
317                ServiceManager.addService(Context.LOCATION_SERVICE, location);
318            } catch (Throwable e) {
319                Slog.e(TAG, "Failure starting Location Manager", e);
320            }
321
322            try {
323                Slog.i(TAG, "Search Service");
324                ServiceManager.addService(Context.SEARCH_SERVICE,
325                        new SearchManagerService(context));
326            } catch (Throwable e) {
327                Slog.e(TAG, "Failure starting Search Service", e);
328            }
329
330            try {
331                Slog.i(TAG, "DropBox Service");
332                ServiceManager.addService(Context.DROPBOX_SERVICE,
333                        new DropBoxManagerService(context, new File("/data/system/dropbox")));
334            } catch (Throwable e) {
335                Slog.e(TAG, "Failure starting DropBoxManagerService", e);
336            }
337
338            try {
339                Slog.i(TAG, "Wallpaper Service");
340                wallpaper = new WallpaperManagerService(context);
341                ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
342            } catch (Throwable e) {
343                Slog.e(TAG, "Failure starting Wallpaper Service", e);
344            }
345
346            try {
347                Slog.i(TAG, "Audio Service");
348                ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
349            } catch (Throwable e) {
350                Slog.e(TAG, "Failure starting Audio Service", e);
351            }
352
353            try {
354                Slog.i(TAG, "Headset Observer");
355                // Listen for wired headset changes
356                headset = new HeadsetObserver(context);
357            } catch (Throwable e) {
358                Slog.e(TAG, "Failure starting HeadsetObserver", e);
359            }
360
361            try {
362                Slog.i(TAG, "Dock Observer");
363                // Listen for dock station changes
364                dock = new DockObserver(context, power);
365            } catch (Throwable e) {
366                Slog.e(TAG, "Failure starting DockObserver", e);
367            }
368
369            try {
370                Slog.i(TAG, "USB Observer");
371                // Listen for USB changes
372                usb = new UsbObserver(context);
373            } catch (Throwable e) {
374                Slog.e(TAG, "Failure starting UsbObserver", e);
375            }
376
377            try {
378                Slog.i(TAG, "UI Mode Manager Service");
379                // Listen for UI mode changes
380                uiMode = new UiModeManagerService(context);
381            } catch (Throwable e) {
382                Slog.e(TAG, "Failure starting UiModeManagerService", e);
383            }
384
385            try {
386                Slog.i(TAG, "Backup Service");
387                ServiceManager.addService(Context.BACKUP_SERVICE,
388                        new BackupManagerService(context));
389            } catch (Throwable e) {
390                Slog.e(TAG, "Failure starting Backup Service", e);
391            }
392
393            try {
394                Slog.i(TAG, "AppWidget Service");
395                appWidget = new AppWidgetService(context);
396                ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
397            } catch (Throwable e) {
398                Slog.e(TAG, "Failure starting AppWidget Service", e);
399            }
400
401            try {
402                Slog.i(TAG, "Recognition Service");
403                recognition = new RecognitionManagerService(context);
404            } catch (Throwable e) {
405                Slog.e(TAG, "Failure starting Recognition Service", e);
406            }
407
408            try {
409                Slog.i(TAG, "DiskStats Service");
410                ServiceManager.addService("diskstats", new DiskStatsService(context));
411            } catch (Throwable e) {
412                Slog.e(TAG, "Failure starting DiskStats Service", e);
413            }
414
415            try {
416                // need to add this service even if SamplingProfilerIntegration.isEnabled()
417                // is false, because it is this service that detects system property change and
418                // turns on SamplingProfilerIntegration. Plus, when sampling profiler doesn't work,
419                // there is little overhead for running this service.
420                Slog.i(TAG, "SamplingProfiler Service");
421                ServiceManager.addService("samplingprofiler",
422                            new SamplingProfilerService(context));
423            } catch (Throwable e) {
424                Slog.e(TAG, "Failure starting SamplingProfiler Service", e);
425            }
426        }
427
428        // make sure the ADB_ENABLED setting value matches the secure property value
429        Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
430                "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
431
432        // register observer to listen for settings changes
433        mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
434                false, new AdbSettingsObserver());
435
436        // Before things start rolling, be sure we have decided whether
437        // we are in safe mode.
438        final boolean safeMode = wm.detectSafeMode();
439        if (safeMode) {
440            try {
441                ActivityManagerNative.getDefault().enterSafeMode();
442                // Post the safe mode state in the Zygote class
443                Zygote.systemInSafeMode = true;
444                // Disable the JIT for the system_server process
445                VMRuntime.getRuntime().disableJitCompilation();
446            } catch (RemoteException e) {
447            }
448        } else {
449            // Enable the JIT for the system_server process
450            VMRuntime.getRuntime().startJitCompilation();
451        }
452
453        // It is now time to start up the app processes...
454
455        if (devicePolicy != null) {
456            devicePolicy.systemReady();
457        }
458
459        if (notification != null) {
460            notification.systemReady();
461        }
462
463        if (statusBar != null) {
464            statusBar.systemReady();
465        }
466        wm.systemReady();
467        power.systemReady();
468        try {
469            pm.systemReady();
470        } catch (RemoteException e) {
471        }
472
473        // These are needed to propagate to the runnable below.
474        final StatusBarManagerService statusBarF = statusBar;
475        final BatteryService batteryF = battery;
476        final ConnectivityService connectivityF = connectivity;
477        final DockObserver dockF = dock;
478        final UsbObserver usbF = usb;
479        final ThrottleService throttleF = throttle;
480        final UiModeManagerService uiModeF = uiMode;
481        final AppWidgetService appWidgetF = appWidget;
482        final WallpaperManagerService wallpaperF = wallpaper;
483        final InputMethodManagerService immF = imm;
484        final RecognitionManagerService recognitionF = recognition;
485        final LocationManagerService locationF = location;
486
487        // We now tell the activity manager it is okay to run third party
488        // code.  It will call back into us once it has gotten to the state
489        // where third party code can really run (but before it has actually
490        // started launching the initial applications), for us to complete our
491        // initialization.
492        ((ActivityManagerService)ActivityManagerNative.getDefault())
493                .systemReady(new Runnable() {
494            public void run() {
495                Slog.i(TAG, "Making services ready");
496
497                if (statusBarF != null) statusBarF.systemReady2();
498                if (batteryF != null) batteryF.systemReady();
499                if (connectivityF != null) connectivityF.systemReady();
500                if (dockF != null) dockF.systemReady();
501                if (usbF != null) usbF.systemReady();
502                if (uiModeF != null) uiModeF.systemReady();
503                if (recognitionF != null) recognitionF.systemReady();
504                Watchdog.getInstance().start();
505
506                // It is now okay to let the various system services start their
507                // third party code...
508
509                if (appWidgetF != null) appWidgetF.systemReady(safeMode);
510                if (wallpaperF != null) wallpaperF.systemReady();
511                if (immF != null) immF.systemReady();
512                if (locationF != null) locationF.systemReady();
513                if (throttleF != null) throttleF.systemReady();
514            }
515        });
516
517        Looper.loop();
518        Slog.d(TAG, "System ServerThread is exiting!");
519    }
520}
521
522public class SystemServer {
523    private static final String TAG = "SystemServer";
524
525    public static final int FACTORY_TEST_OFF = 0;
526    public static final int FACTORY_TEST_LOW_LEVEL = 1;
527    public static final int FACTORY_TEST_HIGH_LEVEL = 2;
528
529    static Timer timer;
530    static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr
531
532    /**
533     * This method is called from Zygote to initialize the system. This will cause the native
534     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
535     * up into init2() to start the Android services.
536     */
537    native public static void init1(String[] args);
538
539    public static void main(String[] args) {
540        if (SamplingProfilerIntegration.isEnabled()) {
541            SamplingProfilerIntegration.start();
542            timer = new Timer();
543            timer.schedule(new TimerTask() {
544                @Override
545                public void run() {
546                    SamplingProfilerIntegration.writeSnapshot("system_server", null);
547                }
548            }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
549        }
550
551        // The system server has to run all of the time, so it needs to be
552        // as efficient as possible with its memory usage.
553        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
554
555        System.loadLibrary("android_servers");
556        init1(args);
557    }
558
559    public static final void init2() {
560        Slog.i(TAG, "Entered the Android system server!");
561        Thread thr = new ServerThread();
562        thr.setName("android.server.ServerThread");
563        thr.start();
564    }
565}
566