SystemServer.java revision 8a5f10b30dae663eb01372490df3497e8a2ad56a
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
406        // make sure the ADB_ENABLED setting value matches the secure property value
407        Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
408                "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
409
410        // register observer to listen for settings changes
411        mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
412                false, new AdbSettingsObserver());
413
414        // Before things start rolling, be sure we have decided whether
415        // we are in safe mode.
416        final boolean safeMode = wm.detectSafeMode();
417        if (safeMode) {
418            try {
419                ActivityManagerNative.getDefault().enterSafeMode();
420                // Post the safe mode state in the Zygote class
421                Zygote.systemInSafeMode = true;
422                // Disable the JIT for the system_server process
423                VMRuntime.getRuntime().disableJitCompilation();
424            } catch (RemoteException e) {
425            }
426        } else {
427            // Enable the JIT for the system_server process
428            VMRuntime.getRuntime().startJitCompilation();
429        }
430
431        // It is now time to start up the app processes...
432
433        if (devicePolicy != null) {
434            devicePolicy.systemReady();
435        }
436
437        if (notification != null) {
438            notification.systemReady();
439        }
440
441        if (statusBar != null) {
442            statusBar.systemReady();
443        }
444        wm.systemReady();
445        power.systemReady();
446        try {
447            pm.systemReady();
448        } catch (RemoteException e) {
449        }
450
451        // These are needed to propagate to the runnable below.
452        final StatusBarManagerService statusBarF = statusBar;
453        final BatteryService batteryF = battery;
454        final ConnectivityService connectivityF = connectivity;
455        final DockObserver dockF = dock;
456        final ThrottleService throttleF = throttle;
457        final UiModeManagerService uiModeF = uiMode;
458        final AppWidgetService appWidgetF = appWidget;
459        final WallpaperManagerService wallpaperF = wallpaper;
460        final InputMethodManagerService immF = imm;
461        final RecognitionManagerService recognitionF = recognition;
462        final LocationManagerService locationF = location;
463
464        // We now tell the activity manager it is okay to run third party
465        // code.  It will call back into us once it has gotten to the state
466        // where third party code can really run (but before it has actually
467        // started launching the initial applications), for us to complete our
468        // initialization.
469        ((ActivityManagerService)ActivityManagerNative.getDefault())
470                .systemReady(new Runnable() {
471            public void run() {
472                Slog.i(TAG, "Making services ready");
473
474                if (statusBarF != null) statusBarF.systemReady2();
475                if (batteryF != null) batteryF.systemReady();
476                if (connectivityF != null) connectivityF.systemReady();
477                if (dockF != null) dockF.systemReady();
478                if (uiModeF != null) uiModeF.systemReady();
479                if (recognitionF != null) recognitionF.systemReady();
480                Watchdog.getInstance().start();
481
482                // It is now okay to let the various system services start their
483                // third party code...
484
485                if (appWidgetF != null) appWidgetF.systemReady(safeMode);
486                if (wallpaperF != null) wallpaperF.systemReady();
487                if (immF != null) immF.systemReady();
488                if (locationF != null) locationF.systemReady();
489                if (throttleF != null) throttleF.systemReady();
490            }
491        });
492
493        Looper.loop();
494        Slog.d(TAG, "System ServerThread is exiting!");
495    }
496}
497
498public class SystemServer {
499    private static final String TAG = "SystemServer";
500
501    public static final int FACTORY_TEST_OFF = 0;
502    public static final int FACTORY_TEST_LOW_LEVEL = 1;
503    public static final int FACTORY_TEST_HIGH_LEVEL = 2;
504
505    static Timer timer;
506    static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr
507
508    /**
509     * This method is called from Zygote to initialize the system. This will cause the native
510     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
511     * up into init2() to start the Android services.
512     */
513    native public static void init1(String[] args);
514
515    public static void main(String[] args) {
516        if (SamplingProfilerIntegration.isEnabled()) {
517            SamplingProfilerIntegration.start();
518            timer = new Timer();
519            timer.schedule(new TimerTask() {
520                @Override
521                public void run() {
522                    SamplingProfilerIntegration.writeSnapshot("system_server");
523                }
524            }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
525        }
526
527        // The system server has to run all of the time, so it needs to be
528        // as efficient as possible with its memory usage.
529        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
530
531        System.loadLibrary("android_servers");
532        init1(args);
533    }
534
535    public static final void init2() {
536        Slog.i(TAG, "Entered the Android system server!");
537        Thread thr = new ServerThread();
538        thr.setName("android.server.ServerThread");
539        thr.start();
540    }
541}
542