AOSP – Adding a Native Daemon

Android is a great OS for many devices and not mobile only. You can even use Android on devices without display (headless). The most important thing about Android is the fact that it is open source and you can build everything from source.

Android structure:

The lower layer is Linux kernel with some changes (little)

The Native Layer is built above the kernel and hosts the C library, other system libraries, and many daemons

The Application Framework layer hosts all the Android services used by Android Applications

In this post, we will build a Native Daemon in the native layer, make it run on system startup and using system property and build application to communicate with our daemon.

 

Create a directory in device/generic/goldfish/bin/mylogger

Add mylogger.c

Add Android.mk file

 

If we add the service definition to init.rc file without a security label we will get:

service mylogger does not have a SELinux domain defined

So we define the security label for it:

We need to define the system property for SE Android. Add the following line to property_contexts file:

 

Writing Client Application

We can write a client application using Android Studio. Simply add a button and write a click handler, use a thread to send a UDP message to the daemon:

Add INTERNET permission request to AndroidManifest.xml file

To set the system property we need to write a system application with C++ code:

To use property_set function you need to add libcutils to Android.mk:

The last thing to do is add an allow rule to system_app to set the property:

Add the following rule to system_app.te

Now setting the system property is triggering start and stop and using the client application we can send messages to the daemon. To see the result cat the file data1:

 

Tagged , ,

1 thought on “AOSP – Adding a Native Daemon

  1. How will the system property be set? From Settings UI?

Leave a Reply

Your email address will not be published. Required fields are marked *