SlideShare a Scribd company logo
Understanding the Android
     System Server

     AnDevCon – March 9th 2011

  Karim Yaghmour / @karimyaghmour
About ...
●   Author of:




●   Introduced Linux Trace Toolkit in 1999
●   Originated Adeos and relayfs (kernel/relay.c)
1. Architecture recap
2. Bootup sequence
3. Services run by the System Server
4. Observing the System Server in action
5. Binder
6. Calling on System Services
7. Inside a few System Services
8. Creating your own System Service
1. Architecture recap
2. Bootup sequence
●   Init:
    ●   app_process -Xzygote (Zygote)
●   frameworks/base/cmds/app_process/app_main.cpp:
    ●   runtime.start(“com.android.internal.os.Zygote”, ...
●   frameworks/base/core/jni/AndroidRuntime.cpp:
    ●   startVM()
    ●   Call Zygote's main()
●   frameworks/base/core/java/com/android/internal/os/Zy
    goteInit.java:
    ●   ...
●   preloadClasses()
    ●   startSystemServer()
    ●   ... magic ...
    ●   Call SystemServer's run()
●   frameworks/base/services/java/com/android/server
    /SystemServer.java:
    ●   Start all system services/managers
    ●   Start ActivityManager:
         –   Send Intent.CATEGORY_HOME
         –   Launcher2 kicks in
3. Services run by the System
                    Server
Entropy Service            Device Policy               Audio Service
Power Manager              Status Bar                  Headset Observer
Activity Manager           Clipboard Service           Dock Observer
Telephone Registry         Input Method Service        UI Mode Manager Service
Package Manager            NetStat Service             Backup Service
Account Manager            NetworkManagement Service   AppWidget Service
Content Manager            Connectivity Service        Recognition Service
System Content Providers   Throttle Service            Status Bar Icons
Battery Service            Accessibility Manager       DiskStats Service
Lights Service             Mount Service               ADB Settings Observer
Vibrator Service           Notification Manager
Alarm Manager              Device Storage Monitor
Init Watchdog              Location Manager
Sensor Service             Search Service
Window Manager             DropBox Service
Bluetooth Service          Wallpaper Service
3.1. Some stats
●   frameworks/base/services/java/com/android/ser
    ver:
    ●   3.5 M
    ●   ~100 files
    ●   85 kloc
●   Activity manager:
    ●   920K
    ●   30+ files
    ●   20 kloc
4. Observing the System Server in
                   action
 ●   Find the System Server's PID
          $ adb shell ps | grep system_server
          system 63 32 120160 35408 ffffffff afd0c738 S system_server
 ●   Look for its output:
          $ adb logcat | grep “63)”
...
D/PowerManagerService( 63): bootCompleted
I/TelephonyRegistry( 63): notifyServiceState: 0 home Android Android 310260 UMTS CSS not supp...
I/TelephonyRegistry( 63): notifyDataConnection: state=0 isDataConnectivityPossible=false reason=null
interfaceName=null networkType=3
I/SearchManagerService( 63): Building list of searchable activities
I/WifiService( 63): WifiService trying to setNumAllowed to 11 with persist set to true
I/ActivityManager( 63): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 ...
I/TelephonyRegistry( 63): notifyMessageWaitingChanged: false
I/TelephonyRegistry( 63): notifyCallForwardingChanged: false
I/TelephonyRegistry( 63): notifyDataConnection: state=1 isDataConnectivityPossible=true reason=simL...
I/TelephonyRegistry( 63): notifyDataConnection: state=2 isDataConnectivityPossible=true reason=simL...
D/Tethering( 63): MasterInitialState.processMessage what=3
I/ActivityManager( 63): Start proc android.process.media for broadcast
com.android.providers.downloads/.DownloadReceiver: pid=223 uid=10002 gids={1015, 2001, 3003}
I/RecoverySystem( 63): No recovery log file
W/WindowManager( 63): App freeze timeout expired.
...
5. Binder
●   CORBA/COM-like IPC
●   Data sent through “parcels” in “transactions”
●   Kernel-supported mechanism
●   Check /proc/binder/*
●   android.* API connected to System Server
    through Binder.
6. Calling on System Services
●   Use getSystemServer
●   Ex: NotificationManager Object reference:
     String ns = Context.NOTIFICATION_SERVICE;
     NotificationManager mNotificationManager = (NotificationManager)
     getSystemServService(ns);

●   Prepare your content
●   Call on the object:
     mNotificationManager.notify(HELLO_ID, notification);
7. Inside a few System Services
●   Get the AOSP ... repo, etc.
●   Tricks:
    ●   Import into Eclipse and collapse methods
    ●   Use reverse-engineering tools:
        –   Imagix
        –   Rationale
        –   Lattix
        –   Scitools
        –   ...
●   Be patient, this isn't documented anywhere ...
7.1. ActivityManager
●   Start new Activities, Services
●   Fetch Content Providers
●   Intent broadcasting
●   OOM adj. maintenance
●   Application Not Responding
●   Permissions
●   Task management
●   Lifecycle management
●   Ex. starting new app from Launcher:
      ●   onClick(Launcher)
      ●   startActivity(Activity.java)
      ●   <Binder>
      ●   ActivityManagerService
      ●   startViaZygote(Process.java)
      ●   <Socket>
      ●   Zygote
7.2. Package Manager
●   10 kloc
●   450 K
●   Installation / removal
●   Permissions
●   Intent resolution (also IntentResolver.java)
●   Called by Activity Manager
7.3. Window Manager
●   Main thread
●   Window manipulation
●   Wallpaper handling
●   Orientation
●   Focus
●   Layering
●   Input event management
7.4. Notification Manager
●   Toasts
●   Notifications
●   Sound playback (see NotificationPlayer.java)
7.5. Power Manager
●   Wakelocks
●   Sleep
●   Brightness
●   Lock
7.6. Network Management Service
●   Talks to “netd” /system/netd
●   Interface configuration
●   Tethering
●   DNS
7.7. Mount Service
●   Mount / Unmount
●   Format
●   USB mass storage
●   OBB
7.8. Location Manager
●   Manage location providers
●   getBestProvider()
●   Proximity alerts
●   Last known location
7.9. Status Bar Manager
●   Expand / collapse
●   Icon visibility
●   Reveal callbacks
●   Callbacks for notification manager
7.10. Backup Manager
●   Enable / disable
●   Transport management
●   backupNow()
●   ...
8. Creating your own System
                    Service
●   Have new funky hardware?
●   Add your code to:
    frameworks/base/services/java/com/android/server/
●   Have the SystemServer.java init your service
●   Expose through:
    ●   frameworks/base/core/java/android/os/[server].aidl
●   Call on native “driver” code through JNI
●   Create an app that calls on service
●   May need to generate custom SDK ...
Thank you ...



karim.yaghmour@opersys.com




     www.opersys.com

More Related Content

PDF
Explore Android Internals
PDF
Low Level View of Android System Architecture
PDF
Android's HIDL: Treble in the HAL
PDF
Android IPC Mechanism
PPT
Learning AOSP - Android Booting Process
PDF
Embedded Android Workshop with Pie
PPTX
Binder: Android IPC
PDF
Booting Android: bootloaders, fastboot and boot images
Explore Android Internals
Low Level View of Android System Architecture
Android's HIDL: Treble in the HAL
Android IPC Mechanism
Learning AOSP - Android Booting Process
Embedded Android Workshop with Pie
Binder: Android IPC
Booting Android: bootloaders, fastboot and boot images

What's hot (20)

ODP
Q4.11: Porting Android to new Platforms
PDF
Embedded Android : System Development - Part I
PDF
Embedded Android : System Development - Part II (HAL)
PDF
Android Boot Time Optimization
PDF
Android Things : Building Embedded Devices
PDF
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
PPTX
Overview of Android binder IPC implementation
PDF
Android Binder IPC for Linux
PDF
The Android graphics path, in depth
PDF
Android OS Porting: Introduction
PDF
Embedded Android : System Development - Part II (Linux device drivers)
PPT
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
PDF
Android for Embedded Linux Developers
ODP
Embedded Android : System Development - Part III
PDF
Design and Concepts of Android Graphics
PPTX
Android Binder: Deep Dive
PDF
Introduction to Android Window System
PPTX
Android Booting Sequence
PDF
Embedded Android : System Development - Part IV
PDF
Android Internals at Linaro Connect Asia 2013
Q4.11: Porting Android to new Platforms
Embedded Android : System Development - Part I
Embedded Android : System Development - Part II (HAL)
Android Boot Time Optimization
Android Things : Building Embedded Devices
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Overview of Android binder IPC implementation
Android Binder IPC for Linux
The Android graphics path, in depth
Android OS Porting: Introduction
Embedded Android : System Development - Part II (Linux device drivers)
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
Android for Embedded Linux Developers
Embedded Android : System Development - Part III
Design and Concepts of Android Graphics
Android Binder: Deep Dive
Introduction to Android Window System
Android Booting Sequence
Embedded Android : System Development - Part IV
Android Internals at Linaro Connect Asia 2013
Ad

Similar to Understanding the Android System Server (20)

PDF
Android Internals
PDF
Android Internals
PPTX
Android internals By Rajesh Khetan
PDF
Extending Android's Platform Toolsuite
PDF
Developing Android Platform Tools
ODP
Android. behind the scenes_programatica 2012
PPTX
Android training course
PDF
Marakana android-java developers
PDF
An Introduction To Android
PPTX
Introduction of Android Architecture
PDF
Core Android
PDF
Applied Computer Science Concepts in Android
PDF
Android101
PDF
Inside The Android Os Building Customizing Managing And Operating Android Sys...
PDF
Running Code in the Android Stack at ABS 2014
PDF
Running Code in the Android Stack at ELCE 2013
PDF
Headless Android at AnDevCon3
PDF
Deep Dive Into Android Security
PDF
Android Services Black Magic by Aleksandar Gargenta
PDF
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Android Internals
Android Internals
Android internals By Rajesh Khetan
Extending Android's Platform Toolsuite
Developing Android Platform Tools
Android. behind the scenes_programatica 2012
Android training course
Marakana android-java developers
An Introduction To Android
Introduction of Android Architecture
Core Android
Applied Computer Science Concepts in Android
Android101
Inside The Android Os Building Customizing Managing And Operating Android Sys...
Running Code in the Android Stack at ABS 2014
Running Code in the Android Stack at ELCE 2013
Headless Android at AnDevCon3
Deep Dive Into Android Security
Android Services Black Magic by Aleksandar Gargenta
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Ad

More from Opersys inc. (20)

PDF
Android Automotive
PDF
Android 10 Internals Update
PDF
Android Security Internals
PDF
Android Treble: Blessing or Trouble?
PDF
Embedded Android Workshop with Oreo
PDF
Scheduling in Android
PDF
Android Things Internals
PDF
Android Platform Debugging and Development
PDF
Embedded Android Workshop with Nougat
PDF
Embedded Android Workshop with Nougat
PDF
Android Things: Android for IoT
PDF
Android Things Internals
PDF
Scheduling in Android
PDF
Brillo / Weave Internals
PDF
Android Platform Debugging and Development
PDF
Memory Management in Android
PDF
Embedded Android Workshop with Nougat
PDF
Brillo / Weave Internals
PDF
Project Ara
PDF
Android Platform Debugging and Development
Android Automotive
Android 10 Internals Update
Android Security Internals
Android Treble: Blessing or Trouble?
Embedded Android Workshop with Oreo
Scheduling in Android
Android Things Internals
Android Platform Debugging and Development
Embedded Android Workshop with Nougat
Embedded Android Workshop with Nougat
Android Things: Android for IoT
Android Things Internals
Scheduling in Android
Brillo / Weave Internals
Android Platform Debugging and Development
Memory Management in Android
Embedded Android Workshop with Nougat
Brillo / Weave Internals
Project Ara
Android Platform Debugging and Development

Understanding the Android System Server

  • 1. Understanding the Android System Server AnDevCon – March 9th 2011 Karim Yaghmour / @karimyaghmour
  • 2. About ... ● Author of: ● Introduced Linux Trace Toolkit in 1999 ● Originated Adeos and relayfs (kernel/relay.c)
  • 3. 1. Architecture recap 2. Bootup sequence 3. Services run by the System Server 4. Observing the System Server in action 5. Binder 6. Calling on System Services 7. Inside a few System Services 8. Creating your own System Service
  • 5. 2. Bootup sequence ● Init: ● app_process -Xzygote (Zygote) ● frameworks/base/cmds/app_process/app_main.cpp: ● runtime.start(“com.android.internal.os.Zygote”, ... ● frameworks/base/core/jni/AndroidRuntime.cpp: ● startVM() ● Call Zygote's main() ● frameworks/base/core/java/com/android/internal/os/Zy goteInit.java: ● ...
  • 6. preloadClasses() ● startSystemServer() ● ... magic ... ● Call SystemServer's run() ● frameworks/base/services/java/com/android/server /SystemServer.java: ● Start all system services/managers ● Start ActivityManager: – Send Intent.CATEGORY_HOME – Launcher2 kicks in
  • 7. 3. Services run by the System Server Entropy Service Device Policy Audio Service Power Manager Status Bar Headset Observer Activity Manager Clipboard Service Dock Observer Telephone Registry Input Method Service UI Mode Manager Service Package Manager NetStat Service Backup Service Account Manager NetworkManagement Service AppWidget Service Content Manager Connectivity Service Recognition Service System Content Providers Throttle Service Status Bar Icons Battery Service Accessibility Manager DiskStats Service Lights Service Mount Service ADB Settings Observer Vibrator Service Notification Manager Alarm Manager Device Storage Monitor Init Watchdog Location Manager Sensor Service Search Service Window Manager DropBox Service Bluetooth Service Wallpaper Service
  • 8. 3.1. Some stats ● frameworks/base/services/java/com/android/ser ver: ● 3.5 M ● ~100 files ● 85 kloc ● Activity manager: ● 920K ● 30+ files ● 20 kloc
  • 9. 4. Observing the System Server in action ● Find the System Server's PID $ adb shell ps | grep system_server system 63 32 120160 35408 ffffffff afd0c738 S system_server ● Look for its output: $ adb logcat | grep “63)” ... D/PowerManagerService( 63): bootCompleted I/TelephonyRegistry( 63): notifyServiceState: 0 home Android Android 310260 UMTS CSS not supp... I/TelephonyRegistry( 63): notifyDataConnection: state=0 isDataConnectivityPossible=false reason=null interfaceName=null networkType=3 I/SearchManagerService( 63): Building list of searchable activities I/WifiService( 63): WifiService trying to setNumAllowed to 11 with persist set to true I/ActivityManager( 63): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 ... I/TelephonyRegistry( 63): notifyMessageWaitingChanged: false I/TelephonyRegistry( 63): notifyCallForwardingChanged: false I/TelephonyRegistry( 63): notifyDataConnection: state=1 isDataConnectivityPossible=true reason=simL... I/TelephonyRegistry( 63): notifyDataConnection: state=2 isDataConnectivityPossible=true reason=simL... D/Tethering( 63): MasterInitialState.processMessage what=3 I/ActivityManager( 63): Start proc android.process.media for broadcast com.android.providers.downloads/.DownloadReceiver: pid=223 uid=10002 gids={1015, 2001, 3003} I/RecoverySystem( 63): No recovery log file W/WindowManager( 63): App freeze timeout expired. ...
  • 10. 5. Binder ● CORBA/COM-like IPC ● Data sent through “parcels” in “transactions” ● Kernel-supported mechanism ● Check /proc/binder/* ● android.* API connected to System Server through Binder.
  • 11. 6. Calling on System Services ● Use getSystemServer ● Ex: NotificationManager Object reference: String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemServService(ns); ● Prepare your content ● Call on the object: mNotificationManager.notify(HELLO_ID, notification);
  • 12. 7. Inside a few System Services ● Get the AOSP ... repo, etc. ● Tricks: ● Import into Eclipse and collapse methods ● Use reverse-engineering tools: – Imagix – Rationale – Lattix – Scitools – ... ● Be patient, this isn't documented anywhere ...
  • 13. 7.1. ActivityManager ● Start new Activities, Services ● Fetch Content Providers ● Intent broadcasting ● OOM adj. maintenance ● Application Not Responding ● Permissions ● Task management ● Lifecycle management
  • 14. Ex. starting new app from Launcher: ● onClick(Launcher) ● startActivity(Activity.java) ● <Binder> ● ActivityManagerService ● startViaZygote(Process.java) ● <Socket> ● Zygote
  • 15. 7.2. Package Manager ● 10 kloc ● 450 K ● Installation / removal ● Permissions ● Intent resolution (also IntentResolver.java) ● Called by Activity Manager
  • 16. 7.3. Window Manager ● Main thread ● Window manipulation ● Wallpaper handling ● Orientation ● Focus ● Layering ● Input event management
  • 17. 7.4. Notification Manager ● Toasts ● Notifications ● Sound playback (see NotificationPlayer.java)
  • 18. 7.5. Power Manager ● Wakelocks ● Sleep ● Brightness ● Lock
  • 19. 7.6. Network Management Service ● Talks to “netd” /system/netd ● Interface configuration ● Tethering ● DNS
  • 20. 7.7. Mount Service ● Mount / Unmount ● Format ● USB mass storage ● OBB
  • 21. 7.8. Location Manager ● Manage location providers ● getBestProvider() ● Proximity alerts ● Last known location
  • 22. 7.9. Status Bar Manager ● Expand / collapse ● Icon visibility ● Reveal callbacks ● Callbacks for notification manager
  • 23. 7.10. Backup Manager ● Enable / disable ● Transport management ● backupNow() ● ...
  • 24. 8. Creating your own System Service ● Have new funky hardware? ● Add your code to: frameworks/base/services/java/com/android/server/ ● Have the SystemServer.java init your service ● Expose through: ● frameworks/base/core/java/android/os/[server].aidl ● Call on native “driver” code through JNI ● Create an app that calls on service ● May need to generate custom SDK ...