配套系列教学视频链接:
说明
系统:Android10.0
设备: FireFly RK3399 (ROC-RK3399-PC-PLUS)
前言
Android init启动的rc脚本中包含了很多后台服务(service),这些服务负责不同功能, 比如netd负责网络管理, rild负责无线通信, installd负责app的安装等。在实际的系统ROM定制开发中, 我们需要定制化自己服务,本章节重点介绍如何编写一个Native服务,并完成开机自启动。
一, 需求
- 创造一个开机启动守护进程, 模仿init进程中建立epoll机制,可循环监听各种事件,如网络链接和数据处理,各种信号处理,文件的数据处理等。
- 本例主要建立模型, 只监听一个本地套接字,通过epoll监听套接字上的链接和数据。 后期需要监控其他事件,直接进行扩展即可。
二, 服务程序内容
用传统的epoll编程大体如下步骤:
#include <sys/epoll.h>
//创建一个epoll句柄
int epoll_create(int size);
//利用epoll_ctl在epoll例程内注册监视对象文件描述符。
int epoll_ctl(int epfd, int op, int fd, struct epoll_event* event);
//等待事件的产生
int epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout);
//关闭epoll
int close(int fd);
编程例子(伪代码):
//创建一个epoll句柄
int epfd = epoll_create(EPOLL_SIZE);
//将需要监控的fd加入监控
int serv_sock = socket(PF_INET, SOCK_STREAM, 0);
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = serv_sock;
epoll_ctl(epfd, EPOLL_CTL_ADD, serv_sock, &event);
//开始监控
struct epoll_event * ep_events = malloc(sizeof(struct epoll_event) * EPOLL_SIZE);
while (1)
{
event_cnt = epoll_wait(epfd, ep_events, EPOLL_SIZE, -1);
if (ep_events[i].data.fd == serv_sock)
{
xxx
}
}
而Android 的init代码中通过面向对象,封装了epoll, 用起来简单方便,扩展性好,代码路径在:
system/core/init/epoll.cpp
我直接使用这个代码进行编程,文件列表如下: (epoll.cpp, epoll.h, result.h都直接拷贝自system/core/init)
device/rockchip/qh100_rk3399/test_se/myservice/
├── Android.bp
├── client_main.cpp
├── epoll.cpp
├── epoll.h
├── main.cpp
├── myservice.rc
└── result.h
此处我主要列出守护进程主程序main.cpp:
#include <string.h>
#include <errno.h>
#include <sys/un.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include "result.h"
#include "epoll.h"
#include <cutils/sockets.h>
#define LOG_TAG "mysrv_test"
//#include <android/log.h>
#include <string.h>
#include <errno.h>
#include <sys/un.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include "result.h"
#include "epoll.h"
#include <cutils/sockets.h>
#define LOG_TAG "mysrv_test"
//#include <android/log.h>
#include <log/log.h>
using namespace android::init;
#define SOCKET_NAME_TEST "mysock"
static int s_fdListen = -1;
void handle_socket_fd()
{
int ret = -1;
int new_fd = -1;
char cmd[256];
if(s_fdListen > 0){
new_fd = accept(s_fdListen, nullptr, nullptr);
if (new_fd < 0) {
return;
}
bzero(cmd, sizeof(cmd));
ret = recv(new_fd, cmd, 256, 0);
if(ret < 0){
ALOGE("recv socket error : %s ", strerror(errno));
return;
}
cmd[ret] = '\0';
}
ALOGE("recv client cmd: %s \n",cmd);
close(new_fd);
}
void setup_socket(Epoll* epoll)
{
//s_fdListen = android_get_control_socket(SOCKET_NAME_TEST);
s_fdListen = socket(AF_LOCAL, SOCK_STREAM, 0);
if (s_fdListen < 0) {
ALOGE("get control socket error, name : %s", SOCKET_NAME_TEST);
exit(-1);
}
std::string path = "/dev/socket/mysocket" ;
unlink(path.c_str());
struct sockaddr_un addr;
addr.sun_family = AF_LOCAL;
strlcpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path));
bind(s_fdListen, (const struct sockaddr *)&addr, sizeof(addr));
listen(s_fdListen, 4);
if (auto result = epoll->RegisterHandler(s_fdListen, handle_socket_fd); !result) {
ALOGE("epoll RegisterHandler error: %s", result.error().error_string.c_str());
exit(1);
}
}
int main(int argc, char *argv[])
{
//初始化epoll
Epoll epoll;
if (auto result = epoll.Open(); !result) {
ALOGE("epoll Open error: %s", result.error().error_string.c_str());
exit(1);
}
setup_socket(&epoll);
while(true){
auto epoll_timeout = std::optional<std::chrono::milliseconds>{};
if (auto result = epoll.Wait(epoll_timeout); !result) {
ALOGE("epoll Wait error: %s", result.error().error_string.c_str());
}
}
return 0;
}
客户端测试程序client_main.cpp:
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/un.h>
#include <sys/socket.h>
#define LOG_TAG "mysrv_test"
#include <log/log.h>
#define SOCKET_NAME_TEST "mysock"
int main(int argc, char *argv[])
{
int fd = -1;
int ret = -1;
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if(fd < 0){
ALOGE("socket error: %s", strerror(errno));
exit(1);
}
struct sockaddr_un srv_addr;
srv_addr.sun_family = AF_LOCAL;
strlcpy(srv_addr.sun_path, "/dev/socket/mysocket", sizeof(srv_addr.sun_path));
ret = connect(fd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
if(ret < 0){
ALOGE("connect error: %s", strerror(errno));
exit(1);
}
char content[256];
snprintf(content, 256, "hello from client pid = %d", getpid());
ret = write(fd, content, 256);
if(ret < 0){
ALOGE("write error: %s", strerror(errno));
exit(1);
}
close(fd);
return 0;
}
对应编译规则(包含客户端测试程序):
cc_binary {
name: "myservice",
init_rc: ["myservice.rc"],
srcs: [
"main.cpp",
"epoll.cpp"
],
shared_libs: [
"liblog",
"libcutils",
"libbase",
],
proprietary: true,
cflags: [
"-Werror",
"-Wno-unused-parameter"
],
}
cc_binary {
name: "myclient_main",
srcs: [
"client_main.cpp",
],
shared_libs: [
"liblog",
"libcutils",
"libbase",
],
cflags: [
"-Werror",
"-Wno-unused-parameter"
],
proprietary: true,
}
通过编译:
source FFTools/build.sh
lunch qh100_rk3399-userdebug
mmm device/rockchip/qh100_rk3399/test_se/myservice/
最后生成:
out/target/product/qh100_rk3399/vendor/bin/myservice
out/target/product/qh100_rk3399/vendor/bin/myclient_main
通过adb将两个程序push到开发版/vendor/bin进行运行验证代码逻辑是否有问题
三,开机启动定制
1, 定义rc文件
device/rockchip/qh100_rk3399/test_se/myservice/myservice.rc
service mynativeservice /vendor/bin/myservice
class main
user root
group root system log
2, Android.bp定义规则
Android.bp中init_rc用于指定当前模块编译的同时需要将rc文件也编译进系统:
cc_binary {
name: "myservice",
init_rc: ["myservice.rc"],
//省略xxx
proprietary: true,
}
3,编译生成
out/target/product/qh100_rk3399/vendor/vendor/etc/init/myservice.rc
4, 通过adb将rc文件push到开发版/vendor/etc/init, 开机重启测试是否启动:
adb root
adb remount
adb push .\myclient_main /vendor/bin
adb push .\myservice /vendor/bin
adb push .\myservice.rc /vendor/etc/init/
四, 配置selinux规则
以上操作暂时还无法完成开机启动,需要配置selinux规则,没有规则,init在启动子进程的时候无法切换到指定的进程域中去。
device/rockchip/qh100_rk3399/test_se/sepolicy/myservice.te
# subject context in proccess status
type myservice_dt, domain;
# object context as a file
type myservice_dt_exec, exec_type, vendor_file_type, file_type;
#grant perm as domain
init_daemon_domain(myservice_dt)
device/rockchip/qh100_rk3399/test_se/sepolicy/file_contexts
/dev/myse_dev u:object_r:myse_testdev_t:s0
/vendor/bin/myse_test u:object_r:myse_test_dt_exec:s0
/vendor/bin/prop_test u:object_r:myprop_test_dt_exec:s0
/vendor/bin/myservice u:object_r:myservice_dt_exec:s0
编译:
make selinux_policy -j2
更新开发板验证:按照我们之前的教程:Android系统10 RK3399 init进程启动(三十二) SeAndroid实战之策略更新和验证_旗浩QH的博客-CSDN博客_seandroid 实战, 替换 vendor和odm分区中的selinux目录文件,然后重启开发板查看:
qh100_rk3399:/ $ ps -elf | grep my
root 338 1 0 15:06:41 ? 00:00:00 myservice
shell 2080 2074 8 15:12:01 pts/0 00:00:00 grep my
qh100_rk3399:/ $ getprop | grep my
[init.svc.mynativeservice]: [running]
[ro.boottime.mynativeservice]: [4325580126]
qh100_rk3399:/ $ su
:/ # kill -9 338 //杀死进程
:/ # ps -elf | grep my //发现系统重新拉起
root 2157 1 79 15:13:03 ? 00:00:04 myservice
root 2167 2147 3 15:13:09 pts/1 00:00:00 grep my