Integrating a Custom Daemon into AOSP: A Step-by-Step Guide
Vikash Choudhary
Engineering Professional | Custom ROM Development, Android Development & Framework (AOSP) | Android Automotive (AAOS) | IoT & Embedded Linux | Technical Blogger |
As Android developers, we often encounter the need to extend AOSP functionalities with custom background services. Recently, I implemented a custom daemon in Android with full integration into the AOSP build system, SELinux policies, and init.rc configuration. Here's a comprehensive guide with commands and details to help you replicate the process.
?? Steps to Create a Custom Daemon in AOSP
1?? Write the Daemon Code
Start by writing the C/C++ code for your daemon. Here's an example of a basic daemon that logs messages:
custom_daemon.c:
#include <stdio.h>
#include <unistd.h>
int main() {
while (1) {
printf("Custom daemon is running...\n");
sleep(10); // Replace with your daemon logic
}
return 0;
}
2?? Integrate the Daemon into AOSP Build System
Android.bp:
cc_binary {
name: "custom_daemon",
srcs: ["custom_daemon.c"],
shared_libs: ["liblog"], // Add required libraries
init_rc: ["custom_daemon.rc"], // Link init.rc configuration
}
3?? Configure init.rc
Create an init.rc file to define your service and add it to the daemon directory:
custom_daemon.rc:
service custom_daemon /system/bin/custom_daemon
class main
user system
group system
oneshot
??? Add SELinux Policy
4?? Define SELinux Rules
custom_daemon.te:
type custom_daemon, domain;
type custom_daemon_exec, exec_type, file_type;
# Allow init to start the daemon
init_daemon_domain(custom_daemon)
# Grant specific permissions
allow custom_daemon self:capability { sys_nice };
allow custom_daemon self:netlink_socket { create bind sendto };
5?? Update File Contexts
Map the daemon binary to the correct SELinux type in file_contexts:
file_contexts:
领英推荐
/system/bin/custom_daemon u:object_r:custom_daemon_exec:s0
?? Build and Flash AOSP
6?? Rebuild AOSP
Run the following commands to build AOSP with your daemon:
source build/envsetup.sh
lunch <device_target>
make -j$(nproc)
7?? Flash the Image to the Device
Once the build is complete, flash the updated image:
adb reboot bootloader
fastboot flashall
fastboot reboot
?? Verify the Daemon
8?? Check if Daemon is Running
After the device boots, verify that the daemon is running:
ps | grep custom_daemon
9?? Validate SELinux Policies
Check for any SELinux denials using dmesg:
dmesg | grep avc
?? Key Takeaways
This hands-on project highlighted the intricacies of AOSP development and the importance of secure, scalable system design. If you're working on similar challenges or exploring AOSP internals, feel free to connect or share your insights!
#Android #AOSP #AAOS #DaemonDevelopment #SELinux #EmbeddedSystems #Linux #SoftwareEngineering
Android Framework Engineer || Android OTA || AOSP || HAL || AIDL
4 个月Need more like this as explained from scratch, indeed useful for newbies/experienced who doesn't know how these work or to implement. Thanks for sharing
Thanks for this blog Vikash C. Very much helpful