ServiceManager(二)构建

 

Client:发送请求的一方

Server:接收请求并提供服务的一方

ServiveManager自身也同样是Binder Server;

在Android系统中Binder Server另一个常见的称谓是"xxx Service",如Camera Service、MediaPlayer Service;

ServiceManager启动的很早,能够保证是系统中第一个向Binder驱动注册成“管家”的程序;

 

http://androidxref.com/9.0.0_r3/xref/frameworks/native/cmds/servicemanager/service_manager.c


374int main(int argc, char** argv)
375{
376    struct binder_state *bs;
377    union selinux_callback cb;
378    char *driver;
379
380    if (argc > 1) {
381        driver = argv[1];
382    } else {
383        driver = "/dev/binder";
384    }
385
386    bs = binder_open(driver, 128*1024);/* 打开设备驱动节点,映射128KB到进程空间 */
387    if (!bs) {
388#ifdef VENDORSERVICEMANAGER
389        ALOGW("failed to open binder driver %s\n", driver);
390        while (true) {
391            sleep(UINT_MAX);
392        }
393#else
394        ALOGE("failed to open binder driver %s\n", driver);
395#endif
396        return -1;
397    }
398
399    if (binder_become_context_manager(bs)) {/* 将servicemanager注册到系统中,为其他server提供注册服务,并成为其他server的管理者 */
400        ALOGE("cannot become context manager (%s)\n", strerror(errno));
401        return -1;
402    }
403
404    cb.func_audit = audit_callback;
405    selinux_set_callback(SELINUX_CB_AUDIT, cb);
406    cb.func_log = selinux_log_callback;
407    selinux_set_callback(SELINUX_CB_LOG, cb);
408
409#ifdef VENDORSERVICEMANAGER
410    sehandle = selinux_android_vendor_service_context_handle();
411#else
412    sehandle = selinux_android_service_context_handle();
413#endif
414    selinux_status_open(true);
415
416    if (sehandle == NULL) {
417        ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n");
418        abort();
419    }
420
421    if (getcon(&service_manager_context) != 0) {
422        ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n");
423        abort();
424    }
425
426
427    binder_loop(bs, svcmgr_handler);/* 进入循环,等待Client的请求 */
428
429    return 0;
430}

 

http://androidxref.com/9.0.0_r3/xref/frameworks/native/cmds/servicemanager/binder.h中定义了binder的接口函数及相关的结构体;

这个文件需要多看两眼;



30/* the one magic handle */
31#define BINDER_SERVICE_MANAGER  0U
32
33#define SVC_MGR_NAME "android.os.IServiceManager"
34
35enum {
36    /* Must match definitions in IBinder.h and IServiceManager.h */
37    PING_TRANSACTION  = B_PACK_CHARS('_','P','N','G'),
38    SVC_MGR_GET_SERVICE = 1,
39    SVC_MGR_CHECK_SERVICE,
40    SVC_MGR_ADD_SERVICE,
41    SVC_MGR_LIST_SERVICES,
42};
43
44typedef int (*binder_handler)(struct binder_state *bs,
45                              struct binder_transaction_data *txn,
46                              struct binder_io *msg,
47                              struct binder_io *reply);
48
49struct binder_state *binder_open(const char* driver, size_t mapsize);
50void binder_close(struct binder_state *bs);
51
52/* initiate a blocking binder call
53 * - returns zero on success
54 */
55int binder_call(struct binder_state *bs,
56                struct binder_io *msg, struct binder_io *reply,
57                uint32_t target, uint32_t code);
58
59/* release any state associate with the binder_io
60 * - call once any necessary data has been extracted from the
61 *   binder_io after binder_call() returns
62 * - can safely be called even if binder_call() fails
63 */
64void binder_done(struct binder_state *bs,
65                 struct binder_io *msg, struct binder_io *reply);
66
67/* manipulate strong references */
68void binder_acquire(struct binder_state *bs, uint32_t target);
69void binder_release(struct binder_state *bs, uint32_t target);
70
71void binder_link_to_death(struct binder_state *bs, uint32_t target, struct binder_death *death);
72
73void binder_loop(struct binder_state *bs, binder_handler func);
74
75int binder_become_context_manager(struct binder_state *bs);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值