title: [Writing an OS Kernel from Scratch] 334 – SystemServer: Service Management Mechanism Startup
category: os
tag: [android, systemserver, zygote, binder]
source: https://github.com/golang12306/os-kernel-from-scratch

[Writing an OS Kernel from Scratch] 334 – SystemServer: Service Management Mechanism Startup

1. What Is SystemServer?

In Android’s Java world, all system services do not live inside application processes. They run in a special process called SystemServer. Android’s designers centralized the entire framework-layer service management in this single process, making services available to applications through the Binder mechanism.

Analogy to Windows NT: NT Executive has a similar Services.exe, which manages Windows services. Android’s SystemServer can be understood as the Android version of a service manager.

Bash
# View Android system services
adb shell dumpsys -l | head -50

Bash
# View SystemServer process PID
adb shell ps -A | grep system_server

2. Startup Flow: from Zygote to SystemServer

SystemServer is not started by init. Its predecessor is Zygote — the first process forked by Android:

Bash
┌──────────────┐
│  init.rc     │  → Start zygote process
└──────┬───────┘
       │
       ▼
┌──────────────┐
│  ZygoteInit  │  → fork() + preload classes
└──────┬───────┘
       │ fork()
       ▼
┌──────────────────────────────────┐
│  SystemServer.main()              │  ← We are here
└──────┬───────────────────────────┘
       │
       ▼
┌──────────────────────────────────┐
│  startSystemServer()              │
│  Start all system services        │
└──────────────────────────────────┘

Zygote preloads commonly used classes (saving class loading time for each app’s startup), then forks SystemServer.

3. SystemServer Core Code Analysis

Source path after Android 9: frameworks/base/services/java/com/android/server/

Key files:

Bash
frameworks/base/services/java/com/android/server/SystemServer.java
frameworks/base/core/java/android/os/SystemService.java

3.1 main() Entry

Java
// frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer {
    private void run() {
        // 1. Create main thread Looper
        Looper.prepareMainLooper();

        // 2. Load native services (AudioFlinger, SurfaceFlinger, etc.)
        System.loadLibrary("android_servers");

        // 3. Create SystemServerManager
        mSystemServerManager = new SystemServerManager();

        // 4. Start services in phases
        // Phase 1: Critical services (Binder, Power, Package Manager)
        startBootstrapServices();

        // Phase 2: Core services (Activity Manager, Window Manager)
        startCoreServices();

        // Phase 3: Other services (Location, Network, Media, Camera...)
        startOtherServices();

        // 5. Enter main loop
        Looper.loop();
    }
}

3.2 Service Startup Phases

Bash
Service startup phases:

Phase 0 — Native services:
  SurfaceFlinger (display composition)
  AudioFlinger (audio)
  MediaServer (media playback)

Phase 1 — Bootstrap services:
  Installer (package installation)
  DeviceIdentifiers
  UriGrantsManager
  PowerManagerService
  PackageManagerService
  DisplayManagerService
  UserManagerService

Phase 2 — Core services:
  ActivityManagerService (AMS)
  WindowManagerService (WMS)
  InputManagerService
  SensorService
  NetworkManagementService

Phase 3 — Other services (~80+):
  LocationManagerService
  NotificationManagerService
  DevicePolicyManagerService
  ConnectivityService
  WifiService
  BluetoothService
  CameraService
  ...

3.3 Service Registration with ServiceManager

Java
// Every service registers with ServiceManager
ServiceManager.addService(Context.WINDOW_SERVICE, wms);
ServiceManager.addService(Context.ACTIVITY_SERVICE, ams);
ServiceManager.addService(Context.POWER_SERVICE, power);

// After registration, other processes can get the service reference:
IBinder b = ServiceManager.getService(Context.WINDOW_SERVICE);
IWindowManager wm = IWindowManager.Stub.asInterface(b);

4. SystemServer Lifecycle

Bash
SystemServer process lifecycle:

  ┌─────────────────────────────────────────────────────────┐
  │  SystemServer main loop                                  │
  │                                                         │
  │  ┌─────────────────────────────────────────────────┐   │
  │  │  Binder thread pool (16 threads by default)       │   │
  │  │  Handles cross-process calls from apps            │   │
  │  └─────────────────────────────────────────────────┘   │
  │                                                         │
  │  ┌─────────────────────────────────────────────────┐   │
  │  │  Watchdog monitoring loop                         │   │
  │  │  Kills SystemServer if frozen for > 60 seconds    │   │
  │  │  Zygote then restarts SystemServer                 │   │
  │  └─────────────────────────────────────────────────┘   │
  │                                                         │
  │  ┌─────────────────────────────────────────────────┐   │
  │  │  Looper main loop (main thread)                  │   │
  │  │  Processes system events, broadcasts, etc.       │   │
  │  └─────────────────────────────────────────────────┘   │
  └─────────────────────────────────────────────────────────┘

When SystemServer crashes, Android enters a Zygote restart loop:

  1. SystemServer process dies → Binder → Zygote gets SIGCHLD
  2. Zygote restarts SystemServer (fork + exec)
  3. All system services restart
  4. All app processes also restart
Last modified: 2024年1月14日

Author

Comments

Write a Reply or Comment

Your email address will not be published.