Netd 服务注册过程(Android 10)

在 SystemServiceRegistry 类中,将 netd 注册成系统服务:

registerService(Context.NETD_SERVICE, IBinder.class, new StaticServiceFetcher<IBinder>() {
    @Override
    public IBinder createService() throws ServiceNotFoundException {
        return ServiceManager.getServiceOrThrow(Context.NETD_SERVICE);
    }
});

此处注册后,可以通过 Context 的 getSystemService 获取此服务。其中服务名 NETD_SERVICE 的值为 “netd”,createService 函数是向 service manager 获取服务的 binder 代理。

在 system/netd/server/main.cpp 的 main 函数中向 service manager 注册了服务:

int main() {
    ...
    if ((ret = NetdNativeService::start()) != android::OK) {
        ALOGE("Unable to start NetdNativeService: %d", ret);
        exit(1);
    }
    ...
    IPCThreadState::self()->joinThreadPool();

    exit(0);
}

看一下 NetdNativeService 的 start 函数:

status_t NetdNativeService::start() {
    IPCThreadState::self()->disableBackgroundScheduling(true);
    const status_t ret = BinderService<NetdNativeService>::publish();
    if (ret != android::OK) {
        return ret;
    }
    sp<ProcessState> ps(ProcessState::self());
    ps->startThreadPool();
    ps->giveThreadPoolName();

    return android::OK;
}

为了简化代码,一般会调用 BinderService 类的 publish 函数进行服务注册:

template<typename SERVICE>
static status_t publish(bool allowIsolated = false,
                        int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
    sp<IServiceManager> sm(defaultServiceManager());
    return sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated,
                          dumpFlags);
}

其中服务名由 NetdNativeService 的静态成员函数 getServiceName 指定:

static char const* getServiceName() { return "netd"; }

注意使用 BinderService 类注册服务的方式,服务类一般可以继承 BinderService:

class NetdNativeService : public BinderService<NetdNativeService>, public BnNetd
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翻滚吧香香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值