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