How to create a native daemon and run from init.rc in Android ?

Android’s init is located at the root of the filesystem i.e. /init and also it uses different naming conventions than normal linux init, so its advised to first try and understand the details of android init Language, from system/core/init/readme.txt you can also check in the source code you have or click the link here.

Now, if you have a compiled source code, create a directory called exampleservice inside external folder,

$ mkdir external/exampleservice
$ cd external/exampleservice

Create an Android.mk as below,

$ vim Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := exampleservice.c
LOCAL_MODULE := exampleservice
LOCAL_MODULE_TAGS := debug
LOCAL_SYSTEM_SHARED_LIBRARIES := libc
LOCAL_SHARED_LIBRARIES := liblog
include $(BUILD_EXECUTABLE)
$ cd ../../
$ make exampleservice

Once, you compiled above code, you may get a binary in out folder at out/target/product/generic/system/bin/exampleservice or you can use a command like $ find out/ -name exampleservice to find exact location.

Now, modify init.rc as below, you can also append below code to the end of init.rc

service exampleservice /system/bin/exampleservice
user exampleservice
group exampleservice
oneshot

Boot android by pushing changed init.rc at /init.rc and exampleservice binary to /system/bin/exampleservice

Free Server Monitoring Cloud Servic... x
Free Server Monitoring Cloud Service Providers - Netdata & New Relic

1 thought on “How to create a native daemon and run from init.rc in Android ?”

Leave a Comment

Sign up to our newsletter!

ADB Commands Android Android Applications Android Build system Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia NDK / Middleware / HAL Operating System Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto Embedded Linux