Android System Services AOSP
(Implementation of a android SystemService in AOSP in Hikey960 board)
The system services play a key role in exposing the low-level functions of the hardware and the Linux kernel to the high-level applications.
What makes System service different from Android Services? System Services are started by SystemServer
so they run as a System process which gives them additional privileges which normal Android Service will never get.
To create a system service, we need the following components:
- AIDL service interface
- Application to host the service
- Client library for untrusted applications (from the play store) to use the service
- Simple client application for testing our service
A default system service we all know about:
(LocationManager)mContext.getSystemService(context);
Images provided below will help you understand the basic working of System Service.
- Call to SystemServiceRegistry from application Context.getSystemService(SERVICE_NAME)
2. Call to SystemServer
Step 1 : Creating AIDL and Stubs
AIDL (Android Interface Definition Language) allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).
More details:
We need to create an AIDL interface with generated Stub.
You can find my files for reference below
Path: AOSP-codebase/frameworks/base/core/java/android/app/
Step 2 :
Now we need to create the service class which will extend our Stub to get the callback.
public class KoushikService extends IKoushikServiceManagerAidlInterface.Stub {
private final static String LOG_TAG = "KoushikService";
private final Object mLock = new Object();
private final Context mContext;
KoushikService(Context context) {
mContext = context;
}
@Override
public void blinkBulb() throws RemoteException {
Slog.i(LOG_TAG,"blinkBulb blinkBulb");
}
}
Find my files for reference below
Path:
AOSP-codebase/frameworks/base/services/core/java/com/android/server/
Step 3 :
Time to join the team by registering out SystemService in SystemServiceRegistry.java and adding it into SystemServer.java.
Path: frameworks/base/core/java/android/app/
SystemServiceRegistry.java
registerService(Context.KOUSHIK_SERVICE, KoushikServiceManager.class,
new CachedServiceFetcher<KoushikServiceManager>() {
@Override
public KoushikServiceManager createService(ContextImpl ctx) throws ServiceNotFoundException {
IBinder binder;
if (true){//ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
binder = ServiceManager.getServiceOrThrow(Context.KOUSHIK_SERVICE);
} else {
binder = ServiceManager.getService(Context.KOUSHIK_SERVICE);
}
return new KoushikServiceManager(ctx,IKoushikServiceManagerAidlInterface.Stub.asInterface(binder));
}});
In SystemServer add your service inside startBootstrapServices(). You will find other default system services over their.
Path: /frameworks/base/services/java/com/android/server/SystemServer.java /
KoushikService androidservice = null;
try{
traceBeginAndSlog("KoushikService");
androidservice = new KoushikService(mSystemContext);
ServiceManager.addService(Context.KOUSHIK_SERVICE,androidservice);
}catch(Throwable e){
Slog.e(TAG, "Starting KoushikService failed!!! ", e);
}
traceEnd();
Don’t forget to declare it’s name in Context.java
Path: /frameworks/base/core/java/android/content/Context.java
public static final String KOUSHIK_SERVICE = "koushikservice";
Step 4:
Compilation error !!!
Don’t beak it. You just have to let the system know there is a new stub api before building the image.
make api-stubs-docs-update-current-api
Step 5:
Think it’s working … is it ?? SELinux deny the service unless explicitly allowed.
SELinux : avc: denied { add }
We need to add SELinux rules to allow that service.
Path: system/sepolicy/private/service_contexts
koushikservice u:object_r:koushik_service:s0 //your service name
Check out this link for detailed discussion:
and please provide some upvote to my question also Rick’s answer
And Now let’s test it
And It’s done.