Ahao's Technical Blog Ahao's Technical Blog
首页
  • 001.基础篇
  • 002.玩转AOSP篇
  • 003.学穿Binder篇
  • 004.基础组件篇
  • 005.系统启动过程分析
  • 006.Hal开发入门与实践
  • 007.显示系统
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

阿豪讲Framework

不积跬步无以至千里
首页
  • 001.基础篇
  • 002.玩转AOSP篇
  • 003.学穿Binder篇
  • 004.基础组件篇
  • 005.系统启动过程分析
  • 006.Hal开发入门与实践
  • 007.显示系统
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 基础篇

  • 玩转AOSP篇

  • 学穿Binder篇

    • 000.Binder 专题导学 —— 如何深入掌握 Binder
    • 001.学习 Binder 的预备知识
    • 002.Binder 基本原理
    • 003.Binder 程序示例之 C 语言篇
    • 004.Binder 服务注册过程情景分析之 C 语言篇
    • 005.Binder 服务获取与使用过程情景分析之C语言篇
    • 006.Android Binder 驱动框架设计与分析
    • 007.Binder 驱动情景分析之 ServiceManager 启动过程
    • 008.Binder 驱动情景分析之服务注册过程
    • 009.Binder 驱动情景分析之服务获取与使用过程
    • 010.Binder 程序示例之 C++ 篇
    • 011.Binder C++ 程序分析之主要类解析
    • 012.Binder 服务注册过程情景分析之 C++ 篇
    • 013.Binder 服务获取与使用过程情景分析之C++篇
    • 014.Binder 程序示例之 aidl-cpp 篇
    • 015.添加 Android Native 系统服务
      • Selinux 配置
    • 016.添加 Native 系统服务回调
    • 017.Binder 程序示例之 Java 篇
    • 018.Binder Java 层初始化
    • 019.Binder Java 层服务注册过程分析
    • 020.Binder Java 层服务获取与使用过程分析
    • 021.添加 Java 系统服务
    • 022.Android Java 系统服务框架与第三方 App 使用自定义 Java 系统服务
    • 023.添加 Java 系统服务回调
    • 024.AIDL 数据类型详解之 Java 篇
    • 025.AIDL 数据类型详解之 C++ 篇
    • 026.Java 调用 Native 服务
    • 027.Native 调用 Java Binder 服务
    • 028.AIDL 关键字 in out inout oneway 解析
    • 029.Binder 驱动 Debug 入门指南
    • 030.Binder 匿名服务源码分析
    • 031.Binder 中的 Parcel 数据结构分析(C++)
    • 032.Binder 中的 Parcel 数据结构分析(Java)
    • 033.Binder 多线程情景分析
    • 034.Binder 线程池溢出问题
    • 035.Binder 代理对象泄露问题分析
    • 036.Binder 死亡通知情景分析
    • 037.Binder 异常处理机制
    • 038.Binder 系统源码演进
    • 039.Binder 面试题汇总
    • 补充——LocalService
  • 基础组件篇

  • 系统启动过程分析

  • Hal开发入门与实践

  • 显示系统

  • Framework
  • 学穿Binder篇
阿豪
2023-07-26
目录

015.添加 Android Native 系统服务

工作学习过程中,我们可能需要去阅读不同类型的 Native 系统服务,也有可能会自己去完成一个 Native 系统服务。无论哪种情况都需要我们了解基本的 Native 如何去添加。就像我们写 Android App 得先了解一下四大组件才行。接着我们就来看看如何添加一个 Android Native 系统服务。使用的源码版本是 AOSP android10_r41。

文章配套源码:https://github.com/yuandaimaahao/HelloNativeService

接下来,我们就来添加我们的 Native 系统服务

在 device/jelly/rice14/HelloNativeService 目录下创建包目录 com/yuandaima

接着在包目录下创建:

package com.yuandaima;

interface IHello {
    void hello();
    int sum(int x, int y);
}
1
2
3
4
5
6

接着在项目目录下执行下面的命令,生产源文件:

aidl-cpp com/yuandaima/IHello.aidl ./ ./IHello.cpp
1

接着我们完善 HelloServer.cpp 程序

#define LOG_TAG "helloserver"
#include <log/log.h>

#include <unistd.h>
#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>

#include "com/yuandaima/IHello.h"
#include "com/yuandaima/BnHello.h"

using namespace android;

class MyHelloService : public com::yuandaima::BnHello
{
    public:
    binder::Status hello() 
    {
        ALOGI("server hello function is running");
        return binder::Status();
    }


    binder::Status sum(int32_t x, int32_t y, int32_t* _aidl_return) 
    {
         ALOGI("server sum function is running");
         *_aidl_return = x + y;
         return binder::Status();
    }
    
};

int main(int argc, char const *argv[])
{
    
    ALOGD("Hello Server is runing");
    
    defaultServiceManager()->addService(String16("MyHelloService"), new MyHelloService());
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();
    
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

接着我们写一个 HelloClient 来测试我们的服务程序:

#define LOG_TAG "aidl_cpp"
#include <log/log.h>

#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>

#include "com/yuandaima/IHello.h"
#include "com/yuandaima/BpHello.h"

using namespace android;

int main(int argc, char const *argv[])
{
    sp<IServiceManager> sm = defaultServiceManager();
    sp<IBinder> binder = sm->getService(String16("MyHelloService"));
    sp<com::yuandaima::IHello> hello = interface_cast<com::yuandaima::IHello>(binder);

    hello->hello();
    int ret = 0;
    hello->sum(1, 2, &ret);
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

然后,完善 Android.bp

cc_binary {
    name: "HelloServer",
    srcs: ["HelloServer.cpp", "IHello.cpp"],
    shared_libs: [
        "liblog",
        "libcutils",
        "libutils",
        "libbinder",
    ],
    init_rc: ["HelloServer.rc"],
}

cc_binary {
    name: "HelloClient",
    srcs: ["HelloClient.cpp", "IHello.cpp"],
    shared_libs: [
        "liblog",
        "libcutils",
        "libutils",
        "libbinder",
    ],
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# Selinux 配置

我们的 Native 程序是添加在 System 分区的,不能在自定义 Product 的 sepolicy 中添加 selinux 配置,需要修改系统的 sepolicy 配置文件:

在 system/sepolicy/private 和system/sepolicy/prebuilts/api/29.0/private 中同时添加:

helloserver.te:

type helloserver_dt, domain, coredomain;
type helloserver_dt_exec, exec_type, file_type, system_file_type;

init_daemon_domain(helloserver_dt)

allow helloserver_dt servicemanager:binder { call transfer };
allow helloserver_dt HelloServer_service:service_manager { add find };

# binder_use(helloserver_dt)
# add_service(helloserver_dt,HelloServer_service)
1
2
3
4
5
6
7
8
9
10

编译时,编译系统会同时检查这两个目录,如果不同就会报错,所以我们要同时修改两个地方。

在 system/sepolicy/private/file_contexts 和 system/sepolicy/prebuilts/api/29.0/private/file_contexts 中添加:

/system/bin/HelloServer     u:object_r:helloserver_dt_exec:s0

1
2

注意 file_contexts 最后一行必须是空行,不然无法编译过。

在 system/sepolicy/private/service_contexts 和 system/sepolicy/prebuilts/api/29.0/private/service_contexts 中倒数第二行添加

MyHelloService                            u:object_r:HelloServer_service:s0

1
2

注意 service_contexts 最后一行必须是空行,不然无法编译过。

在 system/sepolicy/private/service.te 和 system/sepolicy/prebuilts/api/29.0/private/service.te 最后一行中添加:

type HelloServer_service,           service_manager_type;
1

最后编译启动模拟器:

source build/envsetup.sh
lunch rice14-eng
make -j32
emulator
# 进入 Android 模拟器
adb shell 
logcat | grep hello
1
2
3
4
5
6
7

然后就可以看到 Log 了:

07-16 16:42:11.616  1534  1534 D helloserver: Hello Server is runing
1

接着我们运行我们的客户端程序,再查看 Log:

logcat | grep "hello"                                                           
07-16 16:57:46.794  1531  1531 D helloserver: Hello Server is runing
07-16 16:58:52.638  1531  1577 I helloserver: server hello function is running
07-16 16:58:52.638  1531  1577 I helloserver: server sum function is running
1
2
3
4

这样,我们的远程调用就成功了。

最后给同学们留一个问题,HelloClient 为什么没有配 Selinux 权限也能执行,如果要配置,应该怎么配置?(这部分内容我们会留在 SeAndroid 部分来解答)

014.Binder 程序示例之 aidl-cpp 篇
016.添加 Native 系统服务回调

← 014.Binder 程序示例之 aidl-cpp 篇 016.添加 Native 系统服务回调→

最近更新
01
如何调试 SurfaceFlinger
10-05
02
SurfaceFlinger 概述
10-05
03
HWC 接口分析
10-05
更多文章>
Theme by Vdoing | Copyright © 2020-2025 AHao Framework | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式